From 848dbd41d63e6293af51394955a1981e9d5da4e6 Mon Sep 17 00:00:00 2001 From: Nagata Kana Date: Fri, 29 Nov 2024 14:01:33 +0900 Subject: [PATCH] add i2powm1 --- libs/lg/src/lib.rs | 10 +++++----- libs/riff/src/bitmask_operations.rs | 15 +++++++++++++++ libs/riff/src/lib.rs | 2 ++ libs/riff/src/numeric_traits.rs | 4 ++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 libs/riff/src/bitmask_operations.rs diff --git a/libs/lg/src/lib.rs b/libs/lg/src/lib.rs index c4f41367..e3f3b9f7 100644 --- a/libs/lg/src/lib.rs +++ b/libs/lg/src/lib.rs @@ -64,7 +64,7 @@ macro_rules! __lg_internal { #[macro_export] macro_rules! table { ($vec2:expr) => { - eprintln!( + eprint!( "{}", $crate::vec2($crate::remove_ampersand(stringify!($vec2)), $vec2) ); @@ -74,7 +74,7 @@ macro_rules! table { #[macro_export] macro_rules! vmap { ($map:expr) => { - eprintln!( + eprint!( "{}", $crate::vmap($crate::remove_ampersand(stringify!($map)), $map) ); @@ -84,7 +84,7 @@ macro_rules! vmap { #[macro_export] macro_rules! hmap { ($map:expr) => { - eprintln!( + eprint!( "{}", $crate::hmap($crate::remove_ampersand(stringify!($map)), $map) ); @@ -115,7 +115,7 @@ macro_rules! vvec { vecs.push((name.to_owned(), values)) } )+ - eprintln!("{}", $crate::vvec(&vecs)); + eprint!("{}", $crate::vvec(&vecs)); }; } @@ -143,7 +143,7 @@ macro_rules! hvec { vecs.push((name.to_owned(), values)) } )+ - eprintln!("{}", $crate::hvec(&vecs)); + eprint!("{}", $crate::hvec(&vecs)); }; } diff --git a/libs/riff/src/bitmask_operations.rs b/libs/riff/src/bitmask_operations.rs new file mode 100644 index 00000000..082b44a2 --- /dev/null +++ b/libs/riff/src/bitmask_operations.rs @@ -0,0 +1,15 @@ +use crate::Unsigned; + +/// Returns $2^n - 1$. +/// +/// # Examples +/// ``` +/// use crate::i2powm1; +/// assert_eq!(i2powm1::(0), 0); +/// assert_eq!(i2powm1::(1), 1); +/// assert_eq!(i2powm1::(2), 3); +/// assert_eq!(i2powm1::(3), 7); +/// ``` +pub fn i2powm1(n: u32) -> T { + T::MAX >> (T::BITS - n) +} diff --git a/libs/riff/src/lib.rs b/libs/riff/src/lib.rs index 2c129752..30b0fa14 100644 --- a/libs/riff/src/lib.rs +++ b/libs/riff/src/lib.rs @@ -2,6 +2,7 @@ mod binary_search; mod bitmask_iterators; +mod bitmask_operations; mod change_min_max; mod numeric_traits; mod pop_if; @@ -9,6 +10,7 @@ mod pop_if; pub use binary_search::BinarySearch; pub use bitmask_iterators::bitmask_combinations; pub use bitmask_iterators::bitmask_subsets; +pub use bitmask_operations::i2powm1; 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 index 58843d81..76d65ec8 100644 --- a/libs/riff/src/numeric_traits.rs +++ b/libs/riff/src/numeric_traits.rs @@ -48,6 +48,8 @@ pub trait Unsigned: + ShrAssign + Not { + const BITS: u32; + const MAX: Self; fn zero() -> Self; fn one() -> Self; fn wrapping_neg(self) -> Self; @@ -59,6 +61,8 @@ pub trait Unsigned: macro_rules! impl_unsigned { ($($T:ty),* $(,)?) => {$( impl Unsigned for $T { + const BITS: u32 = <$T>::BITS; + const MAX: Self = <$T>::MAX; fn zero() -> Self { 0 } fn one() -> Self { 1 } fn wrapping_neg(self) -> Self { self.wrapping_neg() }