Skip to main content

int_interval_stack/
stack_height_stats.rs

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