Skip to main content

int_interval_stack/
height_stats.rs

1use std::num::NonZeroUsize;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub struct HeightStats {
5    min_positive_height_or_zero: usize,
6    max_height: usize,
7}
8
9impl Default for HeightStats {
10    fn default() -> Self {
11        Self {
12            min_positive_height_or_zero: 0,
13            max_height: 0,
14        }
15    }
16}
17
18impl HeightStats {
19    #[inline]
20    pub(crate) fn observe(&mut self, h: usize) {
21        self.max_height = self.max_height.max(h);
22
23        if h == 0 {
24            return;
25        }
26
27        if self.min_positive_height_or_zero == 0 || h < self.min_positive_height_or_zero {
28            self.min_positive_height_or_zero = h;
29        }
30    }
31}
32
33impl HeightStats {
34    #[inline]
35    pub const fn min_positive_height_or_zero(&self) -> usize {
36        self.min_positive_height_or_zero
37    }
38
39    #[inline]
40    pub const fn max_height(&self) -> usize {
41        self.max_height
42    }
43}
44
45impl HeightStats {
46    /// Returns whether any positive stack height was observed.
47    #[inline]
48    pub const fn has_positive_height(&self) -> bool {
49        self.max_height != 0
50    }
51
52    /// Returns whether at least one coordinate range is covered by multiple
53    /// intervals.
54    #[inline]
55    pub const fn has_overlap(&self) -> bool {
56        self.max_height > 1
57    }
58
59    /// Returns whether all positive-height regions share the same height.
60    #[inline]
61    pub const fn is_uniform_positive_height(&self) -> bool {
62        self.min_positive_height_or_zero != 0 && self.min_positive_height_or_zero == self.max_height
63    }
64}
65
66impl HeightStats {
67    #[inline]
68    pub const fn uniform_positive_height(&self) -> Option<NonZeroUsize> {
69        if self.min_positive_height_or_zero == self.max_height {
70            NonZeroUsize::new(self.max_height)
71        } else {
72            None
73        }
74    }
75}
76
77#[cfg(test)]
78pub(crate) mod test_support;