tabled/settings/peaker/
max.rs

1use super::Peaker;
2
3/// A Peaker which goes over the biggest column first.
4#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Copy, Hash)]
5pub struct PriorityMax {
6    side: bool,
7}
8
9impl PriorityMax {
10    /// Creates a [`PriorityMax`] 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 [`PriorityMax`] object with left side prioritized,
22    /// See [`PriorityMax::new`].
23    pub fn left() -> Self {
24        Self::new(false)
25    }
26
27    /// Creates a [`PriorityMax`] object with right side prioritized,
28    /// See [`PriorityMax::new`].
29    pub fn right() -> Self {
30        Self::new(true)
31    }
32}
33
34impl Peaker for PriorityMax {
35    fn peak(&mut self, mins: &[usize], widths: &[usize]) -> Option<usize> {
36        if self.side {
37            (0..widths.len())
38                .filter(|&i| mins.is_empty() || widths[i] > mins[i])
39                .max_by_key(|&i| widths[i])
40                .filter(|&col| widths[col] != 0)
41        } else {
42            (0..widths.len())
43                .rev()
44                .filter(|&i| mins.is_empty() || widths[i] > mins[i])
45                .max_by_key(|&i| widths[i])
46                .filter(|&col| widths[col] != 0)
47        }
48    }
49}