ndhistogram/axis/
categorynoflow.rs

1use super::{category::Value, Axis, Category, SingleValueBinInterval};
2use std::fmt::{Debug, Display};
3use std::hash::Hash;
4
5/// An axis to represent a finite set of discrete values or categories without an overflow bin.
6///
7/// Similar to [Category], however, no overflow bin is included.
8///
9/// # Example
10///
11/// ```rust
12/// use ndhistogram::axis::{Axis, CategoryNoFlow, SingleValueBinInterval};
13/// let colors = CategoryNoFlow::new(vec!["red", "blue", "pink", "yellow", "black"]);
14/// assert_eq!(colors.index(&"red"), Some(0));
15/// assert_eq!(colors.index(&"green"), None);
16/// assert_eq!(colors.bin(1), Some(SingleValueBinInterval::new("blue")));
17/// assert_eq!(colors.bin(5), None);
18/// ```
19#[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    /// Factory method to create a category axis without an overflow bin.
30    ///
31    /// Takes a set of values that represent each category.
32    /// All other values will not be included in this axis.
33    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}