From 160251995ec980a9ca3996bb7095286fe32f90ea Mon Sep 17 00:00:00 2001 From: Nagata Kana Date: Sat, 14 Dec 2024 19:46:27 +0900 Subject: [PATCH] remove an old accum --- libs/accum/Cargo.toml | 9 -- libs/accum/src/lib.rs | 241 ------------------------------------------ 2 files changed, 250 deletions(-) delete mode 100644 libs/accum/Cargo.toml delete mode 100644 libs/accum/src/lib.rs diff --git a/libs/accum/Cargo.toml b/libs/accum/Cargo.toml deleted file mode 100644 index 0f2b184b..00000000 --- a/libs/accum/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "accum" -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/accum/src/lib.rs b/libs/accum/src/lib.rs deleted file mode 100644 index 02512e22..00000000 --- a/libs/accum/src/lib.rs +++ /dev/null @@ -1,241 +0,0 @@ -use std::cmp::Ord; -use std::ops::AddAssign; -use std::ops::BitAndAssign; -use std::ops::BitOrAssign; -use std::ops::BitXorAssign; -use std::ops::DivAssign; -use std::ops::MulAssign; -use std::ops::SubAssign; -pub fn add(a: &mut [T]) { - for_each_mut(a, |&mut x, y| *y += x); -} -pub fn add_inv(a: &mut [T]) { - rfor_each_mut(a, |&mut x, y| *y -= x); -} -pub fn mul(a: &mut [T]) { - for_each_mut(a, |&mut x, y| *y *= x); -} -pub fn mul_inv(a: &mut [T]) { - rfor_each_mut(a, |&mut x, y| *y /= x); -} -// -- ord -pub fn min(a: &mut [T]) { - for_each_mut(a, |&mut x, y| *y = x.min(*y)); -} -pub fn max(a: &mut [T]) { - for_each_mut(a, |&mut x, y| *y = x.max(*y)); -} -pub fn skipped_min(a: &[T], max: T) -> Vec { - skipped(a, |x, y| (*x).min(*y), || max).collect() -} -pub fn skipped_max(a: &[T], min: T) -> Vec { - skipped(a, |x, y| (*x).max(*y), || min).collect() -} -// -- bit -pub fn xor(a: &mut [T]) { - for_each_mut(a, |&mut x, y| *y ^= x); -} -pub fn xor_inv(a: &mut [T]) { - rfor_each_mut(a, |&mut x, y| *y ^= x); -} -pub fn or(a: &mut [T]) { - for_each_mut(a, |&mut x, y| *y |= x); -} -pub fn and(a: &mut [T]) { - for_each_mut(a, |&mut x, y| *y &= x); -} -// -- for_each -pub fn for_each(a: &[T], mut f: impl FnMut(&T, &T)) { - if !a.is_empty() { - for i in 1..a.len() { - let (left, right) = a.split_at(i); - f(left.last().unwrap(), right.first().unwrap()); - } - } -} -pub fn rfor_each(a: &[T], mut f: impl FnMut(&T, &T)) { - if !a.is_empty() { - for i in (1..a.len()).rev() { - let (left, right) = a.split_at(i); - f(left.last().unwrap(), right.first().unwrap()); - } - } -} -pub fn for_each_mut(a: &mut [T], mut f: impl FnMut(&mut T, &mut T)) { - if !a.is_empty() { - for i in 1..a.len() { - let (left, right) = a.split_at_mut(i); - f(left.last_mut().unwrap(), right.first_mut().unwrap()); - } - } -} -pub fn rfor_each_mut(a: &mut [T], mut f: impl FnMut(&mut T, &mut T)) { - if !a.is_empty() { - for i in (1..a.len()).rev() { - let (left, right) = a.split_at_mut(i); - f(left.last_mut().unwrap(), right.first_mut().unwrap()); - } - } -} - -pub fn skipped(a: &[T], f: F, identity: I) -> Skipped -where - F: FnMut(&T, &T) -> T, - I: FnMut() -> T, -{ - Skipped::new(a, f, identity) -} - -#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)] -pub struct Skipped<'a, T, F, I> { - a: &'a [T], - left: T, - right: Vec, - f: F, - identity: I, -} -impl<'a, T, F, I> Skipped<'a, T, F, I> -where - F: FnMut(&T, &T) -> T, - I: FnMut() -> T, -{ - fn new(a: &'a [T], mut f: F, mut identity: I) -> Self { - let right = if a.is_empty() { - Vec::new() - } else { - let mut right = vec![identity()]; - for x in a[1..].iter().rev() { - let x = f(x, right.last().unwrap()); - right.push(x); - } - right - }; - Self { - a, - left: identity(), - right, - f, - identity, - } - } -} -impl<'a, T, F, I> Iterator for Skipped<'a, T, F, I> -where - F: FnMut(&T, &T) -> T, - I: FnMut() -> T, -{ - type Item = T; - - fn next(&mut self) -> Option { - if let Some(right) = self.right.pop() { - let res = (self.f)(&self.left, &right); - self.left = (self.f)(&self.left, &self.a[0]); - self.a = &self.a[1..]; - Some(res) - } else { - None - } - } -} - -#[cfg(test)] -mod tests { - use super::skipped; - use super::skipped_max; - use super::skipped_min; - - #[test] - fn test_for_each() { - let a = vec![0, 1, 2, 3]; - let mut b = Vec::new(); - super::for_each(&a, |&x, &y| { - b.push((x, y)); - }); - let b_expected = vec![(0, 1), (1, 2), (2, 3)]; - assert_eq!(&b, &b_expected); - } - #[test] - fn test_for_each_mut() { - let mut a = vec![0, 1, 2, 3]; - let mut b = Vec::new(); - super::for_each_mut(&mut a, |&mut x, y| { - *y += x; - b.push((x, *y)); - }); - let a_expected = vec![0, 1, 3, 6]; - let b_expected = vec![(0, 1), (1, 3), (3, 6)]; - assert_eq!(&a, &a_expected); - assert_eq!(&b, &b_expected); - } - - #[test] - fn test_add() { - let mut a = vec![4, 6, 7, 3, 7]; - let orig_a = a.clone(); - super::add(&mut a); - assert_eq!(&a, &[4, 10, 17, 20, 27]); - - super::add_inv(&mut a); - assert_eq!(&a, &orig_a); - } - #[test] - fn test_mul() { - let mut a = vec![4, 6, 7, 3, 7]; - let orig_a = a.clone(); - super::mul(&mut a); - assert_eq!(&a, &[4, 24, 7 * 24, 21 * 24, 147 * 24]); - - super::mul_inv(&mut a); - assert_eq!(&a, &orig_a); - } - #[test] - fn test_xor() { - let mut a = vec![4, 6, 7, 3, 7]; - let orig_a = a.clone(); - super::xor(&mut a); - assert_eq!(&a, &[4, 2, 5, 6, 1]); - - super::xor_inv(&mut a); - assert_eq!(&a, &orig_a); - } - #[test] - fn test_min() { - let mut a = vec![4, 6, 7, 3, 7]; - super::min(&mut a); - assert_eq!(&a, &[4, 4, 4, 3, 3]); - } - #[test] - fn test_max() { - let mut a = vec![4, 6, 7, 3, 7]; - super::max(&mut a); - assert_eq!(&a, &[4, 6, 7, 7, 7]); - } - #[test] - fn test_skipped() { - let a = (0..4) - .map(|i| ((b'a' + i) as char).to_string()) - .collect::>(); - let skipped = skipped( - &a, - |s, t| s.chars().chain(t.chars()).collect::(), - String::new, - ) - .collect::>(); - assert_eq!(skipped[0].as_str(), "bcd"); - assert_eq!(skipped[1].as_str(), "acd"); - assert_eq!(skipped[2].as_str(), "abd"); - assert_eq!(skipped[3].as_str(), "abc"); - } - #[test] - fn test_skipped_min() { - let a = vec![4, 2, 6, 1]; - let skipped = skipped_min(&a, std::i32::MAX); - assert_eq!(skipped, vec![1, 1, 1, 2]); - } - #[test] - fn test_skipped_max() { - let a = vec![4, 2, 6, 1]; - let skipped = skipped_max(&a, std::i32::MIN); - assert_eq!(skipped, vec![6, 6, 4, 6]); - } -}