diff --git a/libs/riff/src/bitmask_iterators.rs b/libs/riff/src/bitmask_iterators.rs index 8cd6bee0..35476380 100644 --- a/libs/riff/src/bitmask_iterators.rs +++ b/libs/riff/src/bitmask_iterators.rs @@ -1,74 +1,6 @@ -use std::fmt::Debug; -use std::mem::size_of; -use std::ops::Add; -use std::ops::AddAssign; -use std::ops::BitAnd; -use std::ops::BitAndAssign; -use std::ops::BitOr; -use std::ops::BitOrAssign; -use std::ops::BitXor; -use std::ops::BitXorAssign; -use std::ops::Div; -use std::ops::DivAssign; -use std::ops::Mul; -use std::ops::MulAssign; -use std::ops::Not; -use std::ops::Shl; -use std::ops::ShlAssign; -use std::ops::Shr; -use std::ops::ShrAssign; -use std::ops::Sub; -use std::ops::SubAssign; +use super::numeric_traits::Unsigned; -/// Adapter trait of this crate. Already implemented for all the unsigned integer types. -pub trait Unsigned: - Sized - + PartialEq - + PartialOrd - + Debug - + Clone - + Copy - + Add - + AddAssign - + Sub - + SubAssign - + Mul - + MulAssign - + Div - + DivAssign - + BitAnd - + BitAndAssign - + BitOr - + BitOrAssign - + BitXor - + BitXorAssign - + Shl - + ShlAssign - + Shr - + ShrAssign - + Not -{ - fn zero() -> Self; - fn one() -> Self; - fn wrapping_neg(self) -> Self; - fn bit_length() -> u32 { - size_of::() as u32 * 8 - } -} - -macro_rules! impl_unsigned { - ($($T:ty),* $(,)?) => {$( - impl Unsigned for $T { - fn zero() -> Self { 0 } - fn one() -> Self { 1 } - fn wrapping_neg(self) -> Self { self.wrapping_neg() } - } - )*} -} - -impl_unsigned! { usize, u8, u16, u32, u64, u128 } - -/// Returns an iterator over k-subsets of `(T::one() << n) - T::one()`. +/// Generates all the $k$-subsets of $[0, N[$ /// /// # Examples /// @@ -76,7 +8,7 @@ impl_unsigned! { usize, u8, u16, u32, u64, u128 } /// ``` /// use bitutils::combinations; /// -/// assert_eq!(combinations::(3, 2).collect::>(), vec![3, 5, 6],); +/// assert_eq!(combinations::(3, 2).collect::>(), vec![3, 5, 6]); /// ``` pub fn bitmask_combinations(n: u32, k: u32) -> BitmaskCombinations { assert!(k < T::bit_length() && k < T::bit_length()); @@ -86,7 +18,6 @@ pub fn bitmask_combinations(n: u32, k: u32) -> BitmaskCombinations< } } -/// [See the document of `combinations`](combinations) #[derive(Clone, Debug, Default, Hash, PartialEq, Eq)] pub struct BitmaskCombinations { n: u32, @@ -111,7 +42,7 @@ impl Iterator for BitmaskCombinations { } } -/// Returns an iterator over subsets of `bs`. +/// Generates all the subsets of `bs`. /// /// # Examples /// @@ -119,7 +50,7 @@ impl Iterator for BitmaskCombinations { /// ``` /// use bitutils::subsets; /// -/// assert_eq!(subsets(10u32).collect::>(), vec![0, 2, 8, 10],); +/// assert_eq!(subsets(10u32).collect::>(), vec![0, 2, 8, 10]); /// ``` pub fn bitmask_subsets(bs: T) -> BitmaskSubsets { BitmaskSubsets { @@ -129,7 +60,6 @@ pub fn bitmask_subsets(bs: T) -> BitmaskSubsets { } } -/// [See the document of `subsets`](subsets) #[derive(Clone, Debug, Default, Hash, PartialEq, Eq)] pub struct BitmaskSubsets { bs: T, diff --git a/libs/riff/src/lib.rs b/libs/riff/src/lib.rs index 0634a986..2c129752 100644 --- a/libs/riff/src/lib.rs +++ b/libs/riff/src/lib.rs @@ -3,10 +3,12 @@ mod binary_search; mod bitmask_iterators; mod change_min_max; +mod numeric_traits; mod pop_if; pub use binary_search::BinarySearch; pub use bitmask_iterators::bitmask_combinations; pub use bitmask_iterators::bitmask_subsets; pub use change_min_max::ChangeMinMax; +pub use numeric_traits::Unsigned; pub use pop_if::PopIf; diff --git a/libs/riff/src/numeric_traits.rs b/libs/riff/src/numeric_traits.rs new file mode 100644 index 00000000..58843d81 --- /dev/null +++ b/libs/riff/src/numeric_traits.rs @@ -0,0 +1,69 @@ +use std::fmt::Debug; +use std::mem::size_of; +use std::ops::Add; +use std::ops::AddAssign; +use std::ops::BitAnd; +use std::ops::BitAndAssign; +use std::ops::BitOr; +use std::ops::BitOrAssign; +use std::ops::BitXor; +use std::ops::BitXorAssign; +use std::ops::Div; +use std::ops::DivAssign; +use std::ops::Mul; +use std::ops::MulAssign; +use std::ops::Not; +use std::ops::Shl; +use std::ops::ShlAssign; +use std::ops::Shr; +use std::ops::ShrAssign; +use std::ops::Sub; +use std::ops::SubAssign; + +/// Unsigned integer types. +pub trait Unsigned: + Sized + + PartialEq + + PartialOrd + + Debug + + Clone + + Copy + + Add + + AddAssign + + Sub + + SubAssign + + Mul + + MulAssign + + Div + + DivAssign + + BitAnd + + BitAndAssign + + BitOr + + BitOrAssign + + BitXor + + BitXorAssign + + Shl + + ShlAssign + + Shr + + ShrAssign + + Not +{ + fn zero() -> Self; + fn one() -> Self; + fn wrapping_neg(self) -> Self; + fn bit_length() -> u32 { + size_of::() as u32 * 8 + } +} + +macro_rules! impl_unsigned { + ($($T:ty),* $(,)?) => {$( + impl Unsigned for $T { + fn zero() -> Self { 0 } + fn one() -> Self { 1 } + fn wrapping_neg(self) -> Self { self.wrapping_neg() } + } + )*} +} + +impl_unsigned! { usize, u8, u16, u32, u64, u128 }