ndhistogram/axis/bininterval/singlevaluebininterval.rs
1use std::fmt::{Display, Formatter};
2
3/// A bin interval that contains only a single value.
4///
5/// Similar to [BinInterval](crate::axis::BinInterval), except each interval only covers a single value.
6/// The only exception is the Overflow bin which may be used to mean "all other bin values".
7#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
8pub enum SingleValueBinInterval<T> {
9 /// An interval to represent "other values".
10 Overflow,
11 /// An interval including only one value.
12 Bin {
13 /// The value included in this interval.
14 value: T,
15 },
16}
17
18impl<T> SingleValueBinInterval<T> {
19 /// A factory method to create a new single valued bin interval.
20 pub fn new(value: T) -> Self {
21 Self::Bin { value }
22 }
23
24 /// A factory method to create a new overflow bin interval.
25 pub fn overflow() -> Self {
26 Self::Overflow {}
27 }
28
29 /// Returns the value included in the interval where it is well-defined.
30 ///
31 /// For the overflow bin (which may cover many values), it returns None.
32 pub fn value(&self) -> Option<&T> {
33 match self {
34 Self::Overflow => None,
35 Self::Bin { value } => Some(value),
36 }
37 }
38}
39
40impl<T: Display> Display for SingleValueBinInterval<T> {
41 fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
42 match self {
43 Self::Overflow => write!(f, "{{overflow}}"),
44 Self::Bin { value } => write!(f, "{{{}}}", value),
45 }
46 }
47}