ndhistogram/axis/
categorynoflow.rs1use super::{category::Value, Axis, Category, SingleValueBinInterval};
2use std::fmt::{Debug, Display};
3use std::hash::Hash;
4
5#[derive(Default, Clone, PartialEq, Eq, Debug)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub struct CategoryNoFlow<T>
22where
23 T: Eq + Hash,
24{
25 axis: Category<T>,
26}
27
28impl<T: Value> CategoryNoFlow<T> {
29 pub fn new<I: IntoIterator<Item = T>>(values: I) -> Self {
34 Self {
35 axis: Category::new(values),
36 }
37 }
38}
39
40impl<T: Value> Axis for CategoryNoFlow<T> {
41 type Coordinate = T;
42 type BinInterval = SingleValueBinInterval<T>;
43
44 fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
45 let index = self.axis.index(coordinate)?;
46 if index == self.axis.num_bins() - 1 {
47 return None;
48 }
49 Some(index)
50 }
51
52 fn num_bins(&self) -> usize {
53 self.axis.num_bins() - 1
54 }
55
56 fn bin(&self, index: usize) -> Option<Self::BinInterval> {
57 let bin = self.axis.bin(index)?;
58 match bin {
59 SingleValueBinInterval::Overflow => None,
60 SingleValueBinInterval::Bin { value: _ } => Some(bin),
61 }
62 }
63}
64
65impl<'a, T: Value> IntoIterator for &'a CategoryNoFlow<T> {
66 type Item = (usize, <Category<T> as Axis>::BinInterval);
67 type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
68
69 fn into_iter(self) -> Self::IntoIter {
70 self.iter()
71 }
72}
73
74impl<T: Display + Value> Display for CategoryNoFlow<T> {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 write!(f, "{}", self.axis)
77 }
78}