vortex_array/stats/
stat_bound.rs

1use std::cmp::Ordering;
2
3use crate::partial_ord::partial_min;
4use crate::stats::bound::IntersectionResult;
5use crate::stats::{LowerBound, Precision, Stat};
6
7/// `StatType` define the bound of a given statistic. (e.g. `Max` is an upper bound),
8/// this is used to extract the bound from a `Precision` value, (e.g. `p::bound<Max>()`).
9pub trait StatType<T> {
10    type Bound: StatBound<T>;
11
12    const STAT: Stat;
13}
14
15/// `StatBound` defines the operations that can be performed on a bound.
16/// The mains bounds are Upper (e.g. max) and Lower (e.g. min).
17pub trait StatBound<T>: Sized {
18    /// Creates a new bound from a Precision statistic.
19    fn lift(value: Precision<T>) -> Self;
20
21    /// Finds the smallest bound that covers both bounds.
22    /// A.k.a. the `meet` of the bound.
23    fn union(&self, other: &Self) -> Option<Self>;
24
25    /// Refines the bounds to the most precise estimate we can make for that bound.
26    /// If the bounds are disjoint, then the result is `None`.
27    /// e.g. `Precision::Inexact(5)` and `Precision::Exact(6)` would result in `Precision::Inexact(5)`.
28    /// A.k.a. the `join` of the bound.
29    fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>>;
30
31    /// Returns the exact value from the bound if that value is exact, otherwise `None`.
32    fn to_exact(&self) -> Option<&T>;
33}
34
35/// This allows a stat with a `Precision` to be interpreted as a bound.
36impl<T> Precision<T> {
37    /// Applied the stat associated bound to the precision value
38    pub fn bound<S: StatType<T>>(self) -> S::Bound {
39        S::Bound::lift(self)
40    }
41}
42
43impl<T: PartialOrd + Clone> LowerBound<T> {
44    pub fn into_value(self) -> Precision<T> {
45        self.0
46    }
47}
48
49impl<T: PartialOrd + Clone> StatBound<T> for Precision<T> {
50    fn lift(value: Precision<T>) -> Self {
51        value
52    }
53
54    fn union(&self, other: &Self) -> Option<Self> {
55        self.clone()
56            .zip(other.clone())
57            .map(|(lhs, rhs)| partial_min(&lhs, &rhs).cloned())
58            .transpose()
59    }
60
61    fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>> {
62        Some(match (self, other) {
63            (Precision::Exact(lhs), Precision::Exact(rhs)) => {
64                if lhs.partial_cmp(rhs)? == Ordering::Equal {
65                    IntersectionResult::Value(Precision::Exact(lhs.clone()))
66                } else {
67                    IntersectionResult::None
68                }
69            }
70            (Precision::Exact(exact), Precision::Inexact(inexact))
71            | (Precision::Inexact(inexact), Precision::Exact(exact)) => {
72                if exact.partial_cmp(inexact)? == Ordering::Less {
73                    IntersectionResult::Value(Precision::Inexact(exact.clone()))
74                } else {
75                    IntersectionResult::Value(Precision::Exact(exact.clone()))
76                }
77            }
78            (Precision::Inexact(lhs), Precision::Inexact(rhs)) => {
79                IntersectionResult::Value(Precision::Inexact(partial_min(lhs, rhs)?.clone()))
80            }
81        })
82    }
83
84    fn to_exact(&self) -> Option<&T> {
85        match self {
86            Precision::Exact(val) => Some(val),
87            _ => None,
88        }
89    }
90}