Struct datazoo::bitset::Bitset

source ·
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>

source

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>

source

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);
source

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);
source

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>

source

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);
source

pub fn bit(&self, at: usize) -> bool

True if bit at at is enabled, false if out of bound or disabled.

source

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));
source

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.

source

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(..));
source

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<B: Clone + AsRef<[u32]>> Clone for Bitset<B>

source§

fn clone(&self) -> Bitset<B>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<B: AsRef<[u32]>> Debug for Bitset<B>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<B: Default + AsRef<[u32]>> Default for Bitset<B>

source§

fn default() -> Bitset<B>

Returns the “default value” for a type. Read more
source§

impl Extend<u32> for Bitset<Box<[u32]>>

source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl Extend<u32> for Bitset<Vec<u32>>

source§

fn extend<T: IntoIterator<Item = u32>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl Extend<usize> for Bitset<Box<[u32]>>

source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl Extend<usize> for Bitset<Vec<u32>>

source§

fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl FromIterator<u32> for Bitset<Box<[u32]>>

source§

fn from_iter<T: IntoIterator<Item = u32>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl FromIterator<u32> for Bitset<Vec<u32>>

source§

fn from_iter<T: IntoIterator<Item = u32>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl FromIterator<usize> for Bitset<Box<[u32]>>

source§

fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl FromIterator<usize> for Bitset<Vec<u32>>

source§

fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<'a, B: AsRef<[u32]>> IntoIterator for &'a Bitset<B>

§

type Item = u32

The type of the elements being iterated over.
§

type IntoIter = Ones<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<B: PartialEq + AsRef<[u32]>> PartialEq for Bitset<B>

source§

fn eq(&self, other: &Bitset<B>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<B: Copy + AsRef<[u32]>> Copy for Bitset<B>

source§

impl<B: Eq + AsRef<[u32]>> Eq for Bitset<B>

source§

impl<B: AsRef<[u32]>> StructuralEq for Bitset<B>

source§

impl<B: AsRef<[u32]>> StructuralPartialEq for Bitset<B>

Auto Trait Implementations§

§

impl<B> RefUnwindSafe for Bitset<B>where B: RefUnwindSafe,

§

impl<B> Send for Bitset<B>where B: Send,

§

impl<B> Sync for Bitset<B>where B: Sync,

§

impl<B> Unpin for Bitset<B>where B: Unpin,

§

impl<B> UnwindSafe for Bitset<B>where B: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.