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::{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 main 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    /// Converts `Self` back to `Precision<T>`, inverse of `lift`.
22    fn into_value(self) -> Precision<T>;
23
24    /// Finds the smallest bound that covers both bounds.
25    /// A.k.a. the `meet` of the bound.
26    fn union(&self, other: &Self) -> Option<Self>;
27
28    /// Refines the bounds to the most precise estimate we can make for that bound.
29    /// If the bounds are disjoint, then the result is `None`.
30    /// e.g. `Precision::Inexact(5)` and `Precision::Exact(6)` would result in `Precision::Inexact(5)`.
31    /// A.k.a. the `join` of the bound.
32    fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>>;
33
34    /// Returns the exact value from the bound if that value is exact, otherwise `None`.
35    fn to_exact(&self) -> Option<&T>;
36}
37
38/// This allows a stat with a `Precision` to be interpreted as a bound.
39impl<T> Precision<T> {
40    /// Applied the stat associated bound to the precision value
41    pub fn bound<S: StatType<T>>(self) -> S::Bound {
42        S::Bound::lift(self)
43    }
44}
45
46impl<T: PartialOrd + Clone> StatBound<T> for Precision<T> {
47    fn lift(value: Precision<T>) -> Self {
48        value
49    }
50
51    fn into_value(self) -> Precision<T> {
52        self
53    }
54
55    fn union(&self, other: &Self) -> Option<Self> {
56        self.clone()
57            .zip(other.clone())
58            .map(|(lhs, rhs)| partial_min(&lhs, &rhs).cloned())
59            .transpose()
60    }
61
62    fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>> {
63        Some(match (self, other) {
64            (Precision::Exact(lhs), Precision::Exact(rhs)) => {
65                if lhs.partial_cmp(rhs)? == Ordering::Equal {
66                    IntersectionResult::Value(Precision::Exact(lhs.clone()))
67                } else {
68                    IntersectionResult::None
69                }
70            }
71            (Precision::Exact(exact), Precision::Inexact(inexact))
72            | (Precision::Inexact(inexact), Precision::Exact(exact)) => {
73                if exact.partial_cmp(inexact)? == Ordering::Less {
74                    IntersectionResult::Value(Precision::Inexact(exact.clone()))
75                } else {
76                    IntersectionResult::Value(Precision::Exact(exact.clone()))
77                }
78            }
79            (Precision::Inexact(lhs), Precision::Inexact(rhs)) => {
80                IntersectionResult::Value(Precision::Inexact(partial_min(lhs, rhs)?.clone()))
81            }
82        })
83    }
84
85    fn to_exact(&self) -> Option<&T> {
86        match self {
87            Precision::Exact(val) => Some(val),
88            _ => None,
89        }
90    }
91}