diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
commit | ef24de24a82fe681581cc130f342363c47c0969a (patch) | |
tree | 0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/tabled/src/settings/peaker/mod.rs | |
parent | Releasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip |
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tabled/src/settings/peaker/mod.rs')
-rw-r--r-- | vendor/tabled/src/settings/peaker/mod.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/vendor/tabled/src/settings/peaker/mod.rs b/vendor/tabled/src/settings/peaker/mod.rs new file mode 100644 index 000000000..a49796b02 --- /dev/null +++ b/vendor/tabled/src/settings/peaker/mod.rs @@ -0,0 +1,94 @@ +//! The module contains [`Peaker`] trait and its implementations to be used in [`Height`] and [`Width`]. +//! +//! [`Width`]: crate::settings::width::Width +//! [`Height`]: crate::settings::height::Height + +/// A strategy of width function. +/// It determines the order how the function is applied. +pub trait Peaker { + /// Creates a new instance. + fn create() -> Self; + /// This function returns a column index which will be changed. + /// Or `None` if no changes are necessary. + fn peak(&mut self, min_widths: &[usize], widths: &[usize]) -> Option<usize>; +} + +/// A Peaker which goes over column 1 by 1. +#[derive(Debug, Default, Clone)] +pub struct PriorityNone { + i: usize, +} + +impl Peaker for PriorityNone { + fn create() -> Self { + Self { i: 0 } + } + + fn peak(&mut self, _: &[usize], widths: &[usize]) -> Option<usize> { + let mut i = self.i; + let mut count_empty = 0; + while widths[i] == 0 { + i += 1; + if i >= widths.len() { + i = 0; + } + + count_empty += 1; + if count_empty == widths.len() { + return None; + } + } + + let col = i; + + i += 1; + if i >= widths.len() { + i = 0; + } + + self.i = i; + + Some(col) + } +} + +/// A Peaker which goes over the biggest column first. +#[derive(Debug, Default, Clone)] +pub struct PriorityMax; + +impl Peaker for PriorityMax { + fn create() -> Self { + Self + } + + fn peak(&mut self, _: &[usize], widths: &[usize]) -> Option<usize> { + let col = (0..widths.len()).max_by_key(|&i| widths[i]).unwrap(); + if widths[col] == 0 { + None + } else { + Some(col) + } + } +} + +/// A Peaker which goes over the smallest column first. +#[derive(Debug, Default, Clone)] +pub struct PriorityMin; + +impl Peaker for PriorityMin { + fn create() -> Self { + Self + } + + fn peak(&mut self, min_widths: &[usize], widths: &[usize]) -> Option<usize> { + let col = (0..widths.len()) + .filter(|&i| min_widths.is_empty() || widths[i] > min_widths[i]) + .min_by_key(|&i| widths[i]) + .unwrap(); + if widths[col] == 0 { + None + } else { + Some(col) + } + } +} |