pub struct Bitset<B: AsRef<[u32]>>(pub B);
Expand description
A slice of u32
accessed on the bit level, see wikipedia.
Usage
Bitset
is parametrized on the storage type, to let you chose whether
this needs to be a reference, a Box
, a Vec
, or even a 3rd party slice
type such as SmallVec
.
Mutable methods are only available when the underlying storage allows mutable access.
use datazoo::Bitset;
let bunch_of_bits = [0xf0f0_00ff, 0xfff0_000f, 0xfff0_0f0f];
let as_array: Bitset<[u32; 3]> = Bitset(bunch_of_bits);
let mut as_vec: Bitset<Vec<u32>> = Bitset(bunch_of_bits.to_vec());
let as_slice: Bitset<&[u32]> = Bitset(&bunch_of_bits);
let as_box: Bitset<Box<[u32]>> = Bitset(Box::new(bunch_of_bits));
assert_eq!(
as_array.ones_in_range(5..91),
as_vec.ones_in_range(5..91),
);
assert_eq!(
as_vec.ones_in_range(5..91),
as_slice.ones_in_range(5..91),
);
assert_eq!(
as_slice.ones_in_range(5..91),
as_box.ones_in_range(5..91),
);
assert_eq!(
as_box.ones_in_range(5..91),
as_array.ones_in_range(5..91),
);
To use mutable methods (Bitset::enable_bit
is currently the only one),
the backing storage B
must be mutable. Otherwise, you just can’t use them.
let as_slice: Bitset<&[u32]> = Bitset(&bunch_of_bits);
as_slice.enable_bit(11);
Vec<_>
and &mut [_]
do support mutable access, so the following works:
let mut as_vec: Bitset<Vec<u32>> = Bitset(bunch_of_bits.to_vec());
let mut as_mut_slice: Bitset<&mut [u32]> = Bitset(&mut bunch_of_bits);
assert_eq!(
as_vec.ones_in_range(5..91),
as_mut_slice.ones_in_range(5..91),
);
as_vec.enable_bit(11);
assert_ne!(
as_vec.ones_in_range(5..91),
as_mut_slice.ones_in_range(5..91),
);
as_mut_slice.enable_bit(11);
assert_eq!(
as_vec.ones_in_range(5..91),
as_mut_slice.ones_in_range(5..91),
);
Tuple Fields§
§0: B
Implementations§
source§impl<B: ExtendBlocks> Bitset<B>
impl<B: ExtendBlocks> Bitset<B>
sourcepub fn enable_bit_extending(&mut self, bit: usize)
pub fn enable_bit_extending(&mut self, bit: usize)
Enables bit at position bit
, extending B
if necessary.
When Bitset::bit
will be called next, it will always be true
.
Example
let mut as_vec = Bitset(vec![]);
assert!(as_vec.enable_bit(64).is_none());
assert_eq!(as_vec.0.len(), 0);
as_vec.enable_bit_extending(73);
assert!(as_vec.bit(73));
assert!(as_vec.enable_bit(64).is_some());
assert!(as_vec.bit(64));
assert_eq!(as_vec.0.len(), 3);
Note that you can use this with Box<[u32]>
:
let mut as_box = Bitset(Box::<[u32]>::default());
as_box.enable_bit_extending(73);
assert!(as_box.bit(73));
assert!(as_box.enable_bit(64).is_some());
assert!(as_box.bit(64));
source§impl<B: AsRef<[u32]> + AsMut<[u32]>> Bitset<B>
impl<B: AsRef<[u32]> + AsMut<[u32]>> Bitset<B>
sourcepub fn enable_bit(&mut self, bit: usize) -> Option<()>
pub fn enable_bit(&mut self, bit: usize) -> Option<()>
Enables bit at position bit
.
Returns None
and does nothing if bit
is out of range.
When Bitset::bit
will be called next, it will be true
if this returned Some
.
Example
let mut bitset = Bitset([0, 0, 0]);
assert_eq!(bitset.bit(12), false);
assert_eq!(bitset.bit(54), false);
bitset.enable_bit(12);
assert_eq!(bitset.bit(12), true);
bitset.enable_bit(54);
assert_eq!(bitset.bit(54), true);
sourcepub fn disable_bit(&mut self, bit: usize) -> Option<()>
pub fn disable_bit(&mut self, bit: usize) -> Option<()>
Disables bit at position bit
.
Returns None
and does nothing if bit
is out of range.
When Bitset::bit
will be called next, it will always return false
.
Example
let mut bitset = Bitset([0, 0, 0]);
assert_eq!(bitset.bit(73), false);
bitset.enable_bit(73);
assert_eq!(bitset.bit(73), true);
bitset.disable_bit(73);
assert_eq!(bitset.bit(73), false);
sourcepub fn disable_range(&mut self, range: Range<usize>)
pub fn disable_range(&mut self, range: Range<usize>)
Disables all bits in given range.
Example
let mut bitset = Bitset(vec![0xffff_ffff, 0xffff_ffff, 0xffff_ffff]);
bitset.disable_range(0..16);
bitset.disable_range(35..54);
assert!(bitset.bit(0).not());
assert!(bitset.bit(16));
assert!(bitset.bit(35).not());
assert!(bitset.bit(53).not());
source§impl<B: AsRef<[u32]>> Bitset<B>
impl<B: AsRef<[u32]>> Bitset<B>
sourcepub fn bit_len(&self) -> usize
pub fn bit_len(&self) -> usize
How many bits in this array?
Note that this will always return a multiple of 32.
Example
let bitset = Bitset(&[0x0000_0000, 0x0000_0000, 0x0000_0000]);
assert_eq!(bitset.bit_len(), 32 * 3);
assert_eq!(Bitset(vec![0x0000_1000]).bit_len(), 32);
assert_eq!(Bitset([]).bit_len(), 0);
sourcepub fn bit(&self, at: usize) -> bool
pub fn bit(&self, at: usize) -> bool
True if bit at at
is enabled, false if out of bound or disabled.
sourcepub fn u32_at(&self, at: usize) -> Result<u32, u32>
pub fn u32_at(&self, at: usize) -> Result<u32, u32>
Returns the 32 bits in the bitset starting at at
.
Errors
Returns an Err
with a truncated value if at + 32
is larger than the bitset.
Example
let bitset = Bitset(&[0xf0f0_00ff, 0xfff0_000f, 0xfff0_0f0f]);
assert_eq!(bitset.u32_at(0), Ok(0xf0f0_00ff));
assert_eq!(bitset.u32_at(4), Ok(0xff0f_000f));
assert_eq!(bitset.u32_at(16), Ok(0x000f_f0f0));
assert_eq!(bitset.u32_at(64), Ok(0xfff0_0f0f));
assert_eq!(bitset.u32_at(96), Err(0));
assert_eq!(bitset.u32_at(80), Err(0xfff0));
sourcepub fn n_at(&self, n: u32, at: usize) -> Option<u32>
pub fn n_at(&self, n: u32, at: usize) -> Option<u32>
Like Self::u32_at
, but limited to n
bits. n <= 32
.
Returns None
if at + n
is larger than the bitset.
sourcepub fn ones(&self) -> Ones<'_> ⓘ
pub fn ones(&self) -> Ones<'_> ⓘ
Same as self.ones_in_range(..)
.
Example
let bitset = Bitset(&[0xf0f0_00ff, 0xfff0_000f, 0xfff0_0f0f]);
assert_eq!(bitset.ones(), bitset.ones_in_range(..));
sourcepub fn ones_in_range(&self, range: impl RangeBounds<usize>) -> Ones<'_> ⓘ
pub fn ones_in_range(&self, range: impl RangeBounds<usize>) -> Ones<'_> ⓘ
Get an iterator over the index of enabled bits within provided range
.
Trait Implementations§
source§impl Extend<u32> for Bitset<Box<[u32]>>
impl Extend<u32> for Bitset<Box<[u32]>>
source§fn extend<T: IntoIterator<Item = u32>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = u32>>(&mut self, iter: T)
Add the iterator items to the Bitset
, will not increase the
bitset size.
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl Extend<u32> for Bitset<Vec<u32>>
impl Extend<u32> for Bitset<Vec<u32>>
source§fn extend<T: IntoIterator<Item = u32>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = u32>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl Extend<usize> for Bitset<Box<[u32]>>
impl Extend<usize> for Bitset<Box<[u32]>>
source§fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)
Add the iterator items to the Bitset
, will not increase the
bitset size.
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl Extend<usize> for Bitset<Vec<u32>>
impl Extend<usize> for Bitset<Vec<u32>>
source§fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)