-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a85454
commit 4087a32
Showing
7 changed files
with
76 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! Add `biterator` method that treats an integer as a set, iterating over each element where | ||
//! the respective bit is set. For example `1101` would return 0, 2 and 3. | ||
use crate::util::integer::*; | ||
|
||
pub trait BitOps<T> { | ||
fn biterator(self) -> Bitset<T>; | ||
} | ||
|
||
impl<T> BitOps<T> for T | ||
where | ||
T: Integer<T>, | ||
{ | ||
fn biterator(self) -> Bitset<T> { | ||
Bitset { t: self } | ||
} | ||
} | ||
|
||
pub struct Bitset<T> { | ||
t: T, | ||
} | ||
|
||
impl<T> Iterator for Bitset<T> | ||
where | ||
T: Integer<T>, | ||
T: TryInto<usize>, | ||
{ | ||
type Item = usize; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.t == T::ZERO { | ||
return None; | ||
} | ||
|
||
let tz = self.t.trailing_zeros(); | ||
self.t = self.t ^ (T::ONE << tz); | ||
|
||
tz.try_into().ok() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters