ndhistogram/axis/
variablenoflow.rs1use crate::error::AxisError;
2
3use super::{Axis, BinInterval, Variable};
4
5use std::fmt::{Debug, Display};
6
7#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
32pub struct VariableNoFlow<T = f64> {
33 axis: Variable<T>,
34}
35
36impl<T: PartialOrd + Copy> VariableNoFlow<T> {
37 pub fn new<I: IntoIterator<Item = T>>(bin_edges: I) -> Result<Self, AxisError> {
42 Ok(Self {
43 axis: Variable::new(bin_edges)?,
44 })
45 }
46
47 pub fn low(&self) -> &T {
49 self.axis.low()
50 }
51
52 pub fn high(&self) -> &T {
54 self.axis.high()
55 }
56}
57
58impl<T: PartialOrd + Copy> Axis for VariableNoFlow<T> {
59 type Coordinate = T;
60 type BinInterval = BinInterval<T>;
61
62 #[inline]
63 fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
64 let index = self.axis.index(coordinate)?;
65 if index == 0 || index + 1 == self.axis.num_bins() {
66 return None;
67 }
68 Some(index - 1)
69 }
70
71 fn num_bins(&self) -> usize {
72 self.axis.num_bins() - 2
73 }
74
75 fn bin(&self, index: usize) -> Option<Self::BinInterval> {
76 let bin = self.axis.bin(index + 1)?;
77 match bin {
78 BinInterval::Underflow { end: _ } => None,
79 BinInterval::Overflow { start: _ } => None,
80 BinInterval::Bin { start: _, end: _ } => Some(bin),
81 }
82 }
83}
84
85impl<'a, T: PartialOrd + Copy> IntoIterator for &'a VariableNoFlow<T> {
86 type Item = (usize, <VariableNoFlow<T> as Axis>::BinInterval);
87 type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
88
89 fn into_iter(self) -> Self::IntoIter {
90 self.iter()
91 }
92}
93
94impl<T: PartialOrd + Copy + Display> Display for VariableNoFlow<T>
95where
96 Self: Axis,
97{
98 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99 write!(
100 f,
101 "Axis{{# bins={}, range=[{}, {}), class={}}}",
102 self.num_bins(),
103 self.axis.low(),
104 self.axis.high(),
105 stringify!(VariableNoFlow)
106 )
107 }
108}