irox_stats/
sampling.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-License-Identifier: MIT
// Copyright 2024 IROX Contributors
//

use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use irox_time::epoch::Timestamp;
use irox_time::Time64;

///
/// A sample with a time resolution of 64 bits and a value resolution of 64 bits. 128b total.
#[derive(Default, Debug, Copy, Clone)]
pub struct Sample64 {
    pub time: Time64,
    pub value: f64,
}
impl PartialEq for Sample64 {
    fn eq(&self, other: &Self) -> bool {
        self.time == other.time && self.value == other.value
    }
}
impl Eq for Sample64 {}
impl PartialOrd for Sample64 {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for Sample64 {
    fn cmp(&self, other: &Self) -> Ordering {
        self.time.cmp(&other.time)
    }
}
impl Hash for Sample64 {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.time.hash(state);
        self.value.to_bits().hash(state);
    }
}

impl Sample64 {
    #[must_use]
    pub const fn new(time: Time64, value: f64) -> Self {
        Sample64 { time, value }
    }
    #[must_use]
    pub const fn value(&self) -> f64 {
        self.value
    }
    #[must_use]
    pub const fn time(&self) -> Time64 {
        self.time
    }
    pub fn set_time(&mut self, time: Time64) {
        self.time = time;
    }
    pub fn set_value(&mut self, value: f64) {
        self.value = value;
    }
}

///
/// A more generic sample that uses [`Timestamp<T>`] rather than [`Time64`]
#[derive(Debug, Copy, Clone)]
pub struct Sample<T: Copy> {
    pub time: Timestamp<T>,
    pub value: f64,
}
impl<T: Copy> Sample<T> {
    #[must_use]
    pub const fn new(value: f64, time: Timestamp<T>) -> Self {
        Sample { time, value }
    }
    #[must_use]
    pub const fn time(&self) -> Timestamp<T> {
        self.time
    }
    #[must_use]
    pub const fn value(&self) -> f64 {
        self.value
    }
}
impl<T: Copy> Ord for Sample<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.time.cmp(&other.time)
    }
}
impl<T: Copy> PartialOrd for Sample<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl<T: Copy> Eq for Sample<T> {}
impl<T: Copy> PartialEq for Sample<T> {
    fn eq(&self, other: &Self) -> bool {
        self.time == other.time && self.value == other.value
    }
}