vortex_array/stats/
stat_bound.rs1use std::cmp::Ordering;
2
3use crate::partial_ord::partial_min;
4use crate::stats::bound::IntersectionResult;
5use crate::stats::{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 into_value(self) -> Precision<T>;
23
24 fn union(&self, other: &Self) -> Option<Self>;
27
28 fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>>;
33
34 fn to_exact(&self) -> Option<&T>;
36}
37
38impl<T> Precision<T> {
40 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}