vortex_array/stats/
stat_bound.rs

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