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 union(&self, other: &Self) -> Option<Self> {
52 self.clone()
53 .zip(other.clone())
54 .map(|(lhs, rhs)| partial_min(&lhs, &rhs).cloned())
55 .transpose()
56 }
57
58 fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>> {
59 Some(match (self, other) {
60 (Precision::Exact(lhs), Precision::Exact(rhs)) => {
61 if lhs.partial_cmp(rhs)? == Ordering::Equal {
62 IntersectionResult::Value(Precision::Exact(lhs.clone()))
63 } else {
64 IntersectionResult::None
65 }
66 }
67 (Precision::Exact(exact), Precision::Inexact(inexact))
68 | (Precision::Inexact(inexact), Precision::Exact(exact)) => {
69 if exact.partial_cmp(inexact)? == Ordering::Less {
70 IntersectionResult::Value(Precision::Inexact(exact.clone()))
71 } else {
72 IntersectionResult::Value(Precision::Exact(exact.clone()))
73 }
74 }
75 (Precision::Inexact(lhs), Precision::Inexact(rhs)) => {
76 IntersectionResult::Value(Precision::Inexact(partial_min(lhs, rhs)?.clone()))
77 }
78 })
79 }
80
81 fn to_exact(&self) -> Option<&T> {
82 match self {
83 Precision::Exact(val) => Some(val),
84 _ => None,
85 }
86 }
87
88 fn into_value(self) -> Precision<T> {
89 self
90 }
91}