irox_stats/
sampling.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2025 IROX Contributors
3//
4
5use core::cmp::Ordering;
6use core::hash::{Hash, Hasher};
7use irox_time::epoch::Timestamp;
8use irox_time::Time64;
9use irox_tools::StrWrapper;
10use irox_tools::ToU64;
11
12macro_rules! sample64 {
13    ($name:ident, $prim:ty) => {
14        /// A sample with a time resolution of 64 bits and a value resolution of 64 bits. 128b total.
15        #[derive(Default, Debug, Copy, Clone)]
16        pub struct $name {
17            pub time: Time64,
18            pub value: $prim,
19        }
20        impl PartialEq for $name {
21            fn eq(&self, other: &Self) -> bool {
22                self.time == other.time && self.value == other.value
23            }
24        }
25        impl Eq for $name {}
26        impl PartialOrd for $name {
27            fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
28                Some(self.cmp(other))
29            }
30        }
31        impl Ord for $name {
32            fn cmp(&self, other: &Self) -> Ordering {
33                self.time.cmp(&other.time)
34            }
35        }
36        impl Hash for $name {
37            fn hash<H: Hasher>(&self, state: &mut H) {
38                self.time.hash(state);
39                self.value.to_u64().hash(state);
40            }
41        }
42
43        impl $name {
44            #[must_use]
45            pub const fn new(time: Time64, value: $prim) -> Self {
46                Self { time, value }
47            }
48            #[must_use]
49            pub const fn value(&self) -> $prim {
50                self.value
51            }
52            #[must_use]
53            pub const fn time(&self) -> Time64 {
54                self.time
55            }
56            pub fn set_time(&mut self, time: Time64) {
57                self.time = time;
58            }
59            pub fn set_value(&mut self, value: $prim) {
60                self.value = value;
61            }
62        }
63    };
64}
65sample64!(Sample64, f64);
66sample64!(IntSample64, u64);
67#[derive(Debug, Clone, Eq, PartialEq, Hash)]
68pub struct StrSample64<'a> {
69    pub time: Time64,
70    pub value: StrWrapper<'a>,
71}
72impl PartialOrd for StrSample64<'_> {
73    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
74        Some(self.cmp(other))
75    }
76}
77impl Ord for StrSample64<'_> {
78    fn cmp(&self, other: &Self) -> Ordering {
79        self.time.cmp(&other.time)
80    }
81}
82impl<'a> StrSample64<'a> {
83    #[must_use]
84    pub const fn new(time: Time64, value: StrWrapper<'a>) -> Self {
85        Self { time, value }
86    }
87    #[must_use]
88    pub const fn value(&self) -> &StrWrapper<'a> {
89        &self.value
90    }
91    #[must_use]
92    pub const fn time(&self) -> Time64 {
93        self.time
94    }
95    pub fn set_time(&mut self, time: Time64) {
96        self.time = time;
97    }
98    pub fn set_value(&mut self, value: StrWrapper<'a>) {
99        self.value = value;
100    }
101}
102
103///
104/// A more generic sample that uses [`Timestamp<T>`] rather than [`Time64`]
105#[derive(Debug, Copy, Clone)]
106pub struct Sample<T: Copy> {
107    pub time: Timestamp<T>,
108    pub value: f64,
109}
110impl<T: Copy> Sample<T> {
111    #[must_use]
112    pub const fn new(value: f64, time: Timestamp<T>) -> Self {
113        Sample { time, value }
114    }
115    #[must_use]
116    pub const fn time(&self) -> Timestamp<T> {
117        self.time
118    }
119    #[must_use]
120    pub const fn value(&self) -> f64 {
121        self.value
122    }
123}
124impl<T: Copy> Ord for Sample<T> {
125    fn cmp(&self, other: &Self) -> Ordering {
126        self.time.cmp(&other.time)
127    }
128}
129impl<T: Copy> PartialOrd for Sample<T> {
130    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
131        Some(self.cmp(other))
132    }
133}
134impl<T: Copy> Eq for Sample<T> {}
135impl<T: Copy> PartialEq for Sample<T> {
136    fn eq(&self, other: &Self) -> bool {
137        self.time == other.time && self.value == other.value
138    }
139}
140
141#[derive(Debug, Clone)]
142pub enum SampleValue<'a> {
143    String(StrWrapper<'a>),
144    Float(f64),
145    Int(u64),
146}
147impl Default for SampleValue<'_> {
148    fn default() -> Self {
149        SampleValue::Int(0)
150    }
151}
152impl PartialEq for SampleValue<'_> {
153    fn eq(&self, other: &Self) -> bool {
154        match (self, other) {
155            (SampleValue::String(a), SampleValue::String(b)) => a == b,
156            (SampleValue::Float(a), SampleValue::Float(b)) => a == b,
157            (SampleValue::Int(a), SampleValue::Int(b)) => a == b,
158            _ => false,
159        }
160    }
161}
162impl Eq for SampleValue<'_> {}
163impl Hash for SampleValue<'_> {
164    fn hash<H: Hasher>(&self, state: &mut H) {
165        match self {
166            SampleValue::String(s) => s.hash(state),
167            SampleValue::Int(i) => i.hash(state),
168            SampleValue::Float(f) => f.to_bits().hash(state),
169        }
170    }
171}