Skip to content

Commit

Permalink
refactor numeric_traits out as a new module
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtkana committed Nov 12, 2024
1 parent 4571ab0 commit cb985c7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 75 deletions.
80 changes: 5 additions & 75 deletions libs/riff/src/bitmask_iterators.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,14 @@
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<Output = Self>
+ AddAssign
+ Sub<Output = Self>
+ SubAssign
+ Mul<Output = Self>
+ MulAssign
+ Div<Output = Self>
+ DivAssign
+ BitAnd<Output = Self>
+ BitAndAssign
+ BitOr<Output = Self>
+ BitOrAssign
+ BitXor<Output = Self>
+ BitXorAssign
+ Shl<u32, Output = Self>
+ ShlAssign<u32>
+ Shr<u32, Output = Self>
+ ShrAssign<u32>
+ Not<Output = Self>
{
fn zero() -> Self;
fn one() -> Self;
fn wrapping_neg(self) -> Self;
fn bit_length() -> u32 {
size_of::<Self>() 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
///
/// Basic usage:
/// ```
/// use bitutils::combinations;
///
/// assert_eq!(combinations::<u32>(3, 2).collect::<Vec<_>>(), vec![3, 5, 6],);
/// assert_eq!(combinations::<u32>(3, 2).collect::<Vec<_>>(), vec![3, 5, 6]);
/// ```
pub fn bitmask_combinations<T: Unsigned>(n: u32, k: u32) -> BitmaskCombinations<T> {
assert!(k < T::bit_length() && k < T::bit_length());
Expand All @@ -86,7 +18,6 @@ pub fn bitmask_combinations<T: Unsigned>(n: u32, k: u32) -> BitmaskCombinations<
}
}

/// [See the document of `combinations`](combinations)
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
pub struct BitmaskCombinations<T> {
n: u32,
Expand All @@ -111,15 +42,15 @@ impl<T: Unsigned> Iterator for BitmaskCombinations<T> {
}
}

/// Returns an iterator over subsets of `bs`.
/// Generates all the subsets of `bs`.
///
/// # Examples
///
/// Basic usage:
/// ```
/// use bitutils::subsets;
///
/// assert_eq!(subsets(10u32).collect::<Vec<_>>(), vec![0, 2, 8, 10],);
/// assert_eq!(subsets(10u32).collect::<Vec<_>>(), vec![0, 2, 8, 10]);
/// ```
pub fn bitmask_subsets<T: Unsigned>(bs: T) -> BitmaskSubsets<T> {
BitmaskSubsets {
Expand All @@ -129,7 +60,6 @@ pub fn bitmask_subsets<T: Unsigned>(bs: T) -> BitmaskSubsets<T> {
}
}

/// [See the document of `subsets`](subsets)
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
pub struct BitmaskSubsets<T> {
bs: T,
Expand Down
2 changes: 2 additions & 0 deletions libs/riff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
69 changes: 69 additions & 0 deletions libs/riff/src/numeric_traits.rs
Original file line number Diff line number Diff line change
@@ -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<Output = Self>
+ AddAssign
+ Sub<Output = Self>
+ SubAssign
+ Mul<Output = Self>
+ MulAssign
+ Div<Output = Self>
+ DivAssign
+ BitAnd<Output = Self>
+ BitAndAssign
+ BitOr<Output = Self>
+ BitOrAssign
+ BitXor<Output = Self>
+ BitXorAssign
+ Shl<u32, Output = Self>
+ ShlAssign<u32>
+ Shr<u32, Output = Self>
+ ShrAssign<u32>
+ Not<Output = Self>
{
fn zero() -> Self;
fn one() -> Self;
fn wrapping_neg(self) -> Self;
fn bit_length() -> u32 {
size_of::<Self>() 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 }

0 comments on commit cb985c7

Please sign in to comment.