From d55931ed3d39c03c264300baf7f0df582e346d1f Mon Sep 17 00:00:00 2001 From: Nagata Kana Date: Tue, 10 Dec 2024 19:30:28 +0900 Subject: [PATCH] fix riff --- libs/riff/src/bitmask_iterators.rs | 12 ++++++------ libs/riff/src/bitmask_operations.rs | 10 ++++++++-- libs/riff/src/numeric_traits.rs | 8 ++++---- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/libs/riff/src/bitmask_iterators.rs b/libs/riff/src/bitmask_iterators.rs index a6c08fde..3f6c7471 100644 --- a/libs/riff/src/bitmask_iterators.rs +++ b/libs/riff/src/bitmask_iterators.rs @@ -16,7 +16,7 @@ pub fn bitmask_combinations(n: u32, k: u32) -> BitmaskCombinations< assert!(k < T::bit_length() && k < T::bit_length()); BitmaskCombinations { n, - bs: (T::one() << k) - T::one(), + bs: (T::ONE << k) - T::ONE, } } @@ -29,12 +29,12 @@ impl Iterator for BitmaskCombinations { type Item = T; fn next(&mut self) -> Option { - if (T::one() << self.n) <= self.bs { + if (T::ONE << self.n) <= self.bs { return None; } let res = Some(self.bs); - self.bs = if self.bs == T::zero() { - T::one() << self.n + self.bs = if self.bs == T::ZERO { + T::ONE << self.n } else { let x = self.bs & self.bs.wrapping_neg(); let y = self.bs + x; @@ -78,10 +78,10 @@ impl Iterator for BitmaskSubsets { return None; } let res = Some(self.bs ^ self.full); - if self.bs == T::zero() { + if self.bs == T::ZERO { self.finished = true; } else { - self.bs -= T::one(); + self.bs -= T::ONE; self.bs &= self.full; } res diff --git a/libs/riff/src/bitmask_operations.rs b/libs/riff/src/bitmask_operations.rs index 082b44a2..dfb35d39 100644 --- a/libs/riff/src/bitmask_operations.rs +++ b/libs/riff/src/bitmask_operations.rs @@ -4,12 +4,18 @@ use crate::Unsigned; /// /// # Examples /// ``` -/// use crate::i2powm1; +/// use riff::i2powm1; /// assert_eq!(i2powm1::(0), 0); /// assert_eq!(i2powm1::(1), 1); /// assert_eq!(i2powm1::(2), 3); /// assert_eq!(i2powm1::(3), 7); +/// assert_eq!(i2powm1::(31), 0x7FFF_FFFF); +/// assert_eq!(i2powm1::(32), 0xFFFF_FFFF); /// ``` pub fn i2powm1(n: u32) -> T { - T::MAX >> (T::BITS - n) + if n == T::bit_length() { + T::MAX + } else { + (T::ONE << n) - T::ONE + } } diff --git a/libs/riff/src/numeric_traits.rs b/libs/riff/src/numeric_traits.rs index 76d65ec8..80be872d 100644 --- a/libs/riff/src/numeric_traits.rs +++ b/libs/riff/src/numeric_traits.rs @@ -50,8 +50,8 @@ pub trait Unsigned: { const BITS: u32; const MAX: Self; - fn zero() -> Self; - fn one() -> Self; + const ZERO: Self; + const ONE: Self; fn wrapping_neg(self) -> Self; fn bit_length() -> u32 { size_of::() as u32 * 8 @@ -63,8 +63,8 @@ macro_rules! impl_unsigned { impl Unsigned for $T { const BITS: u32 = <$T>::BITS; const MAX: Self = <$T>::MAX; - fn zero() -> Self { 0 } - fn one() -> Self { 1 } + const ZERO: Self = 0; + const ONE: Self = 1; fn wrapping_neg(self) -> Self { self.wrapping_neg() } } )*}