1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use num_traits::Float;
use super::{Axis, BinInterval, Uniform};
use std::fmt::{Debug, Display};
#[derive(Clone, PartialEq, Debug)]
pub struct UniformNoFlow<T> {
axis: Uniform<T>,
}
impl<T: Float> UniformNoFlow<T> {
pub fn new(num: usize, low: T, high: T) -> Self {
UniformNoFlow {
axis: Uniform::new(num, low, high),
}
}
pub fn low(&self) -> &T {
self.axis.low()
}
pub fn high(&self) -> &T {
self.axis.high()
}
}
impl<T: Float> Axis for UniformNoFlow<T> {
type Coordinate = T;
type BinInterval = BinInterval<T>;
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
let index = self.axis.index(coordinate)?;
if index == 0 || index + 1 == self.axis.numbins() {
return None;
}
Some(index - 1)
}
fn numbins(&self) -> usize {
self.axis.numbins() - 2
}
fn bin(&self, index: usize) -> Option<Self::BinInterval> {
let bin = self.axis.bin(index + 1)?;
match bin {
BinInterval::Underflow { end: _ } => None,
BinInterval::Overflow { start: _ } => None,
BinInterval::Bin { start: _, end: _ } => Some(bin),
}
}
}
impl<'a, T: Float> IntoIterator for &'a UniformNoFlow<T> {
type Item = (usize, <Uniform<T> as Axis>::BinInterval);
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: Float + Display> Display for UniformNoFlow<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Axis{{# bins={}, range=[{}, {}), class={}}}",
self.numbins(),
self.axis.low(),
self.axis.high(),
stringify!(UniformNoFlow)
)
}
}