ndhistogram/axis/
variablecyclic.rs1use crate::error::AxisError;
2
3use super::{Axis, BinInterval, VariableNoFlow};
4use std::fmt::{Debug, Display};
5
6use num_traits::Num;
7
8#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30pub struct VariableCyclic<T = f64> {
31 #[cfg_attr(feature = "serde", serde(flatten))]
32 axis: VariableNoFlow<T>,
33}
34
35impl<T> VariableCyclic<T>
36where
37 T: PartialOrd + Copy,
38{
39 pub fn new<I: IntoIterator<Item = T>>(bin_edges: I) -> Result<Self, AxisError> {
44 Ok(Self {
45 axis: VariableNoFlow::new(bin_edges)?,
46 })
47 }
48
49 #[inline]
51 pub fn low(&self) -> &T {
52 self.axis.low()
53 }
54
55 #[inline]
57 pub fn high(&self) -> &T {
58 self.axis.high()
59 }
60}
61
62impl<T> Axis for VariableCyclic<T>
63where
64 T: PartialOrd + Copy + Num,
65{
66 type Coordinate = T;
67 type BinInterval = BinInterval<T>;
68
69 #[inline]
70 fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
71 let (mut x, hi, lo) = (*coordinate, *self.axis.high(), *self.axis.low());
72 let range = hi - lo;
73 x = (x - lo) % range;
74 if x < T::zero() {
75 x = range + x;
76 }
77 x = x + lo;
78 self.axis.index(&x)
79 }
80
81 #[inline]
82 fn num_bins(&self) -> usize {
83 self.axis.num_bins()
84 }
85
86 #[inline]
87 fn bin(&self, index: usize) -> Option<Self::BinInterval> {
88 self.axis.bin(index)
89 }
90}
91
92impl<T> Display for VariableCyclic<T>
93where
94 T: PartialOrd + Copy + Display,
95{
96 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 write!(
98 f,
99 "Axis{{# bins={}, range=[{}, {}), class={}}}",
100 self.axis.num_bins(),
101 self.axis.low(),
102 self.axis.high(),
103 stringify!(VariableCyclic)
104 )
105 }
106}
107
108impl<'a, T> IntoIterator for &'a VariableCyclic<T>
109where
110 VariableCyclic<T>: Axis,
111{
112 type Item = (usize, <VariableCyclic<T> as Axis>::BinInterval);
113 type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
114 fn into_iter(self) -> Self::IntoIter {
115 self.iter()
116 }
117}