diff --git a/libs/cmpmore/Cargo.toml b/libs/cmpmore/Cargo.toml deleted file mode 100644 index 34197b38..00000000 --- a/libs/cmpmore/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "cmpmore" -version = "0.1.0" -authors = ["ngtkana "] -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/libs/cmpmore/src/lib.rs b/libs/cmpmore/src/lib.rs deleted file mode 100644 index cca9be90..00000000 --- a/libs/cmpmore/src/lib.rs +++ /dev/null @@ -1,104 +0,0 @@ -//! Utility `change_{min,max}`. - -/// If `lhs` is larger than `rhs`, override `lhs` by `rhs`. -/// -/// # Examples -/// -/// ``` -/// # use cmpmore::change_min; -/// let mut x: i32 = 3; -/// change_min(&mut x, 2); -/// assert_eq!(x, 2); -/// ``` -pub fn change_min(lhs: &mut T, rhs: T) { - if *lhs > rhs { - *lhs = rhs; - } -} - -/// If `lhs` is smaller than `rhs`, override `lhs` by `rhs`. -/// -/// # Examples -/// -/// ``` -/// # use cmpmore::change_max; -/// let mut x: i32 = 3; -/// change_max(&mut x, 4); -/// assert_eq!(x, 4); -/// ``` -pub fn change_max(lhs: &mut T, rhs: T) { - if *lhs < rhs { - *lhs = rhs; - } -} - -/// Macro version of [`change_min()`]. This is useful to avoid borrow checkers. -/// -/// # Examples -/// -/// ``` -/// # use cmpmore::change_min; -/// let mut a = [3, 2]; -/// change_min!(&mut a[0], a[1]); -/// assert_eq!(a, [2, 2]); -/// ``` -#[macro_export] -macro_rules! change_min { - ($lhs:expr, $rhs:expr) => { - let rhs = $rhs; - let lhs = $lhs; - $crate::change_min(lhs, rhs); - }; -} - -/// Macro version of [`change_max()`]. This is useful to avoid borrow checkers. -/// -/// # Examples -/// -/// ``` -/// # use cmpmore::change_max; -/// let mut a = [3, 4]; -/// change_max!(&mut a[0], a[1]); -/// assert_eq!(a, [4, 4]); -/// ``` -#[macro_export] -macro_rules! change_max { - ($lhs:expr, $rhs:expr) => { - let rhs = $rhs; - let lhs = $lhs; - $crate::change_max(lhs, rhs); - }; -} - -/// Provide method versions of [`change_min()`], [`change_max()`] -pub trait CmpMore: PartialOrd + Sized { - /// If `self` is larger than `rhs`, override `self` by `rhs`. - /// - /// # Examples - /// - /// ``` - /// # use cmpmore::CmpMore; - /// let mut x: i32 = 3; - /// x.change_min(2); - /// assert_eq!(x, 2); - /// ``` - fn change_min(&mut self, rhs: Self) { - change_min(self, rhs) - } - - /// If `self` is smaller than `rhs`, override `self` by `rhs`. - /// - /// # Examples - /// - /// ``` - /// # use cmpmore::CmpMore; - /// let mut x: i32 = 3; - /// x.change_max(4); - /// assert_eq!(x, 4); - /// ``` - fn change_max(&mut self, rhs: Self) { - change_max(self, rhs) - } -} - -impl CmpMore for T {} diff --git a/libs/riff/src/binary_search.rs b/libs/riff/src/binary_search.rs index 13a144a7..8518882b 100644 --- a/libs/riff/src/binary_search.rs +++ b/libs/riff/src/binary_search.rs @@ -3,7 +3,7 @@ use std::cmp::Ordering::Equal; use std::cmp::Ordering::Greater; use std::cmp::Ordering::Less; -/// Method versions of functions. +/// `{lower,upper}_bound`, etc pub trait BinarySearch { fn partition_point bool>(&self, pred: F) -> usize; fn lower_bound_by Ordering>(&self, mut f: F) -> usize { diff --git a/libs/riff/src/change_min_max.rs b/libs/riff/src/change_min_max.rs new file mode 100644 index 00000000..c7657733 --- /dev/null +++ b/libs/riff/src/change_min_max.rs @@ -0,0 +1,16 @@ +/// `change_min` and `change_max` +pub trait ChangeMinMax: PartialOrd + Sized { + fn change_min(&mut self, rhs: Self) { + if *self > rhs { + *self = rhs + } + } + + fn change_max(&mut self, rhs: Self) { + if *self < rhs { + *self = rhs + } + } +} + +impl ChangeMinMax for T {} diff --git a/libs/riff/src/lib.rs b/libs/riff/src/lib.rs index 2d306298..4acda280 100644 --- a/libs/riff/src/lib.rs +++ b/libs/riff/src/lib.rs @@ -1,7 +1,9 @@ //! Future and otherworldly Rust features. mod binary_search; +mod change_min_max; mod pop_if; pub use binary_search::BinarySearch; +pub use change_min_max::ChangeMinMax; pub use pop_if::PopIf; diff --git a/libs/riff/src/pop_if.rs b/libs/riff/src/pop_if.rs index 23b6f933..72a64cdc 100644 --- a/libs/riff/src/pop_if.rs +++ b/libs/riff/src/pop_if.rs @@ -1,21 +1,6 @@ use std::collections::BinaryHeap; -/// Removes and returns the "top" element in a vector if the predicate -/// returns `true`, or [`None`] if the predicate returns false or the container -/// is empty. -/// -/// # Examples -/// -/// ``` -/// use riff::PopIf; -/// -/// let mut vec = vec![1, 2, 3, 4]; -/// let pred = |x: &mut i32| *x % 2 == 0; -/// -/// assert_eq!(vec.pop_if(pred), Some(4)); -/// assert_eq!(vec, [1, 2, 3]); -/// assert_eq!(vec.pop_if(pred), None); -/// ``` +/// Conditional `pop` function. pub trait PopIf { type Value;