vortex_array/stats/
stat_bound.rs1use std::cmp::Ordering;
2
3use crate::partial_ord::partial_min;
4use crate::stats::bound::IntersectionResult;
5use crate::stats::{LowerBound, Precision, Stat};
6
7pub trait StatType<T> {
10 type Bound: StatBound<T>;
11
12 const STAT: Stat;
13}
14
15pub trait StatBound<T>: Sized {
18 fn lift(value: Precision<T>) -> Self;
20
21 fn union(&self, other: &Self) -> Option<Self>;
24
25 fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>>;
30
31 fn to_exact(&self) -> Option<&T>;
33}
34
35impl<T> Precision<T> {
37 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}