use crate::util::Id;
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use std::cmp::PartialOrd;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::Add;
#[derive(PartialEq, Clone, Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct SimpleStatistic<I, V> {
id: I,
min: V,
max: V,
value: V,
}
impl<I: Send, V: Copy + Default> SimpleStatistic<I, V> {
pub fn new(id: I, max: V) -> Self {
Self::with_value(id, V::default(), max, max)
}
pub fn with_value(id: I, min: V, max: V, value: V) -> Self {
Self {
id,
min,
max,
value,
}
}
}
impl<I: Send, V> SimpleStatistic<I, V>
where
V: Copy + PartialOrd + Add<Output = V>,
{
pub fn value(&self) -> V {
self.value
}
pub fn min(&self) -> V {
self.min
}
pub fn max(&self) -> V {
self.max
}
pub fn set_value(&mut self, value: V) {
self.value = value;
if self.value < self.min {
self.value = self.min;
} else if self.value > self.max {
self.value = self.max;
}
}
pub fn add(&mut self, inc: V) {
self.set_value(self.value + inc);
}
}
#[cfg(not(feature = "serialization"))]
impl<I, V> Id for SimpleStatistic<I, V>
where
I: Debug + Hash + Eq + Clone + Send,
{
type Id = I;
fn id(&self) -> &Self::Id {
&self.id
}
}
#[cfg(feature = "serialization")]
impl<I, V> Id for SimpleStatistic<I, V>
where
I: Debug + Hash + Eq + Clone + Send + Serialize + for<'a> Deserialize<'a>,
{
type Id = I;
fn id(&self) -> &Self::Id {
&self.id
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_statistic_bounds() {
let mut stat = SimpleStatistic::with_value(1, 10, 20, 15);
stat.add(100);
assert_eq!(stat.value(), stat.max());
stat.add(-100);
assert_eq!(stat.value(), stat.min());
}
}