ndhistogram/axis/
categorynoflow.rsuse super::{category::Value, Axis, Category, SingleValueBinInterval};
use std::fmt::{Debug, Display};
use std::hash::Hash;
#[derive(Default, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CategoryNoFlow<T>
where
T: Eq + Hash,
{
axis: Category<T>,
}
impl<T: Value> CategoryNoFlow<T> {
pub fn new<I: IntoIterator<Item = T>>(values: I) -> Self {
Self {
axis: Category::new(values),
}
}
}
impl<T: Value> Axis for CategoryNoFlow<T> {
type Coordinate = T;
type BinInterval = SingleValueBinInterval<T>;
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
let index = self.axis.index(coordinate)?;
if index == self.axis.num_bins() - 1 {
return None;
}
Some(index)
}
fn num_bins(&self) -> usize {
self.axis.num_bins() - 1
}
fn bin(&self, index: usize) -> Option<Self::BinInterval> {
let bin = self.axis.bin(index)?;
match bin {
SingleValueBinInterval::Overflow => None,
SingleValueBinInterval::Bin { value: _ } => Some(bin),
}
}
}
impl<'a, T: Value> IntoIterator for &'a CategoryNoFlow<T> {
type Item = (usize, <Category<T> as Axis>::BinInterval);
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: Display + Value> Display for CategoryNoFlow<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.axis)
}
}