vortex_array/stats/
stat_bound.rs1use std::cmp::Ordering;
5
6use crate::partial_ord::partial_min;
7use crate::stats::bound::IntersectionResult;
8use crate::stats::{Precision, Stat};
9
10pub trait StatType<T> {
13 type Bound: StatBound<T>;
14
15 const STAT: Stat;
16}
17
18pub trait StatBound<T>: Sized {
21 fn lift(value: Precision<T>) -> Self;
23
24 fn into_value(self) -> Precision<T>;
26
27 fn union(&self, other: &Self) -> Option<Self>;
30
31 fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>>;
36
37 fn to_exact(&self) -> Option<&T>;
39}
40
41impl<T> Precision<T> {
43 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}