vortex_array/expr/stats/
stat_bound.rs1use std::cmp::Ordering;
5
6use crate::expr::stats::Precision;
7use crate::expr::stats::Stat;
8use crate::expr::stats::bound::IntersectionResult;
9use crate::partial_ord::partial_min;
10
11pub trait StatType<T> {
14 type Bound: StatBound<T>;
15
16 const STAT: Stat;
17}
18
19pub trait StatBound<T>: Sized {
22 fn lift(value: Precision<T>) -> Self;
24
25 fn into_value(self) -> Precision<T>;
27
28 fn union(&self, other: &Self) -> Option<Self>;
31
32 fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>>;
37
38 fn to_exact(&self) -> Option<&T>;
40}
41
42impl<T> Precision<T> {
44 pub fn bound<S: StatType<T>>(self) -> S::Bound {
46 S::Bound::lift(self)
47 }
48}
49
50impl<T: PartialOrd + Clone> StatBound<T> for Precision<T> {
51 fn lift(value: Precision<T>) -> Self {
52 value
53 }
54
55 fn into_value(self) -> Precision<T> {
56 self
57 }
58
59 fn union(&self, other: &Self) -> Option<Self> {
60 self.clone()
61 .zip(other.clone())
62 .map(|(lhs, rhs)| partial_min(&lhs, &rhs).cloned())
63 .transpose()
64 }
65
66 fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>> {
67 Some(match (self, other) {
68 (Precision::Exact(lhs), Precision::Exact(rhs)) => {
69 if lhs.partial_cmp(rhs)? == Ordering::Equal {
70 IntersectionResult::Value(Precision::Exact(lhs.clone()))
71 } else {
72 IntersectionResult::None
73 }
74 }
75 (Precision::Exact(exact), Precision::Inexact(inexact))
76 | (Precision::Inexact(inexact), Precision::Exact(exact)) => {
77 if exact.partial_cmp(inexact)? == Ordering::Less {
78 IntersectionResult::Value(Precision::Inexact(exact.clone()))
79 } else {
80 IntersectionResult::Value(Precision::Exact(exact.clone()))
81 }
82 }
83 (Precision::Inexact(lhs), Precision::Inexact(rhs)) => {
84 IntersectionResult::Value(Precision::Inexact(partial_min(lhs, rhs)?.clone()))
85 }
86 })
87 }
88
89 fn to_exact(&self) -> Option<&T> {
90 match self {
91 Precision::Exact(val) => Some(val),
92 _ => None,
93 }
94 }
95}