tabled/settings/peaker/
min.rs

1use super::Peaker;
2
3/// A Peaker which goes over the smallest column first.
4#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Copy, Hash)]
5pub struct PriorityMin {
6    side: bool,
7}
8
9impl PriorityMin {
10    /// Creates a [`PriorityMin`] object with a side set to right or left,
11    /// It's crusial in cases where both columns has equal widths and we need to peak a left or right.
12    ///
13    /// Passing true means a right side.
14    /// Passing false means a left side.
15    pub fn new(priorities_right: bool) -> Self {
16        Self {
17            side: priorities_right,
18        }
19    }
20
21    /// Creates a [`PriorityMin`] object with left side prioritized,
22    /// See [`PriorityMin::new`].
23    pub fn left() -> Self {
24        Self::new(false)
25    }
26
27    /// Creates a [`PriorityMin`] object with right side prioritized,
28    /// See [`PriorityMin::new`].
29    pub fn right() -> Self {
30        Self::new(true)
31    }
32}
33
34impl Peaker for PriorityMin {
35    fn peak(&mut self, mins: &[usize], widths: &[usize]) -> Option<usize> {
36        match self.side {
37            true => (0..widths.len())
38                .filter(|&i| mins.is_empty() || widths[i] > mins[i])
39                .min_by_key(|&i| widths[i])
40                .filter(|&col| widths[col] != 0),
41            false => (0..widths.len())
42                .rev()
43                .filter(|&i| mins.is_empty() || widths[i] > mins[i])
44                .min_by_key(|&i| widths[i])
45                .filter(|&col| widths[col] != 0),
46        }
47    }
48}