Struct StaticBitmap

Source
pub struct StaticBitmap<D, B> { /* private fields */ }
Expand description

A bitmap that cannot be resized.

Any structure that implements the ContainerRead (for read-only access) and ContainerWrite (for mutable access) traits can be a container of bitmap (e.g. [T; N], &[T], Vec<T>, etc.).

Usage example:

use bitmac::{StaticBitmap, LSB};

// You can directly check every single bit
let bitmap = StaticBitmap::<_, LSB>::new([0b0000_0001u8, 0b0000_1000]);
assert!(bitmap.get(0));
assert!(bitmap.get(11));
assert!(!bitmap.get(13));
// Out of bounds bits always return false
assert!(!bitmap.get(128));

// You can iterate over bits
let bitmap = StaticBitmap::<_, LSB>::new([0b0000_1001u8, 0b0000_1000]);
let mut iter = bitmap.iter().by_bits().enumerate();
assert_eq!(iter.next(), Some((0, true)));
assert_eq!(iter.next(), Some((1, false)));
assert_eq!(iter.next(), Some((2, false)));
assert_eq!(iter.next(), Some((3, true)));
assert_eq!(iter.next(), Some((4, false)));

// You can check multiple bits at the same time through the intersection
use bitmac::Intersection;
let bitmap = StaticBitmap::<_, LSB>::new([0b0000_1001u8, 0b0000_1000]);
// .. by creating specific new container for result
let test = [0b0000_1001u8, 0b0000_0000];
assert_eq!(bitmap.intersection::<[u8; 2]>(&test), test);
// .. by using preallocated container for result
let test = [0b0000_1001u8, 0b0000_0000];
let mut result = [0u8; 2];
bitmap.intersection_in(&test, &mut result);
assert_eq!(result, test);
// .. by comparing length of difference that is equivalent to count of ones (bits) in result
let test = [0b0000_1001u8, 0b0000_0000];
assert_eq!(bitmap.intersection_len(&test), test.iter().fold(0, |acc, &v| acc + v.count_ones() as usize));

// You can directly change every single bit
let mut bitmap = StaticBitmap::<_, LSB>::new([0b0000_1001u8, 0b0001_1000]);
assert!(bitmap.get(0));
assert!(bitmap.get(3));
assert!(bitmap.get(11));
assert!(bitmap.get(12));
assert!(!bitmap.get(13));
assert!(!bitmap.get(128));
bitmap.set(12, false);
assert!(!bitmap.get(12));
bitmap.set(13, true);
assert!(bitmap.get(13));
// Out of bounds bits return error
assert!(bitmap.try_set(128, true).is_err());
assert!(!bitmap.get(128));

Implementations§

Source§

impl<D, N, B> StaticBitmap<D, B>
where D: ContainerRead<B, Slot = N>, N: Number, B: BitAccess,

Source

pub fn new(data: D) -> Self

Creates new bitmap from container.

Source

pub fn count_ones(&self) -> usize

Returns number of ones in the bitmap.

Source

pub fn count_zeros(&self) -> usize

Returns number of zeros in the bitmap.

Source§

impl<D, B> StaticBitmap<D, B>

Source

pub fn into_inner(self) -> D

Converts bitmap into inner container.

Source§

impl<D, B> StaticBitmap<D, B>
where D: ContainerRead<B>, B: BitAccess,

Source

pub fn get(&self, idx: usize) -> bool

Gets single bit state.

Usage example:

use bitmac::{StaticBitmap, LSB};

let bitmap = StaticBitmap::<_, LSB>::new([0b0000_0001u8, 0b0000_1000]);
assert!(bitmap.get(0));
assert!(bitmap.get(11));
assert!(!bitmap.get(13));
// Out of bounds bits always return false
assert!(!bitmap.get(128));
Source

pub fn iter(&self) -> Iter<'_, D, B>

Returns iterator over slots.

Source§

impl<D, B> StaticBitmap<D, B>
where D: ContainerWrite<B>, B: BitAccess,

Source

pub fn set(&mut self, idx: usize, val: bool)

Sets new state for a single bit.

§Panic

Panics if idx is out of bounds. See non-panic function try_set.

§Usage example:
use bitmac::{StaticBitmap, LSB};

let mut bitmap = StaticBitmap::<_, LSB>::new([0b0000_1001u8, 0b0001_1000]);
bitmap.set(12, false);
assert!(!bitmap.get(12));
bitmap.set(13, true);
assert!(bitmap.get(13));
Source

pub fn try_set(&mut self, idx: usize, val: bool) -> Result<(), OutOfBoundsError>

Sets new state for a single bit.

Returns Err(_) if idx is out of bounds.

§Usage example:
use bitmac::{StaticBitmap, LSB};

let mut bitmap = StaticBitmap::<_, LSB>::new([0b0000_1001u8, 0b0001_1000]);
assert!(bitmap.try_set(12, true).is_ok());
assert!(bitmap.get(12));
assert!(bitmap.try_set(12, false).is_ok());
assert!(!bitmap.get(12));
// Out of bounds bits return error
assert!(bitmap.try_set(128, true).is_err());
assert!(!bitmap.get(128));

Trait Implementations§

Source§

impl<D, B> AsMut<D> for StaticBitmap<D, B>

Source§

fn as_mut(&mut self) -> &mut D

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<D, B> AsRef<D> for StaticBitmap<D, B>

Source§

fn as_ref(&self) -> &D

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<D: Clone, B: Clone> Clone for StaticBitmap<D, B>

Source§

fn clone(&self) -> StaticBitmap<D, B>

Returns a duplicate 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<D, B> ContainerRead<B> for StaticBitmap<D, B>
where D: ContainerRead<B>, B: BitAccess,

Source§

type Slot = <D as ContainerRead<B>>::Slot

Source§

fn get_slot(&self, idx: usize) -> Self::Slot

Gets value of stored slot.
Source§

fn slots_count(&self) -> usize

Gets number of stored slots.
Source§

fn bits_count(&self) -> usize

Gets max number of bits.
Source§

impl<D, B> ContainerWrite<B> for StaticBitmap<D, B>
where D: ContainerWrite<B>, B: BitAccess,

Source§

fn get_mut_slot(&mut self, idx: usize) -> &mut Self::Slot

Gets mutable reference to stored slot.
Source§

impl<D, N, B> Debug for StaticBitmap<D, B>
where D: ContainerRead<B, Slot = N>, N: Number, B: BitAccess,

Source§

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

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

impl<D: Default, B: Default> Default for StaticBitmap<D, B>

Source§

fn default() -> StaticBitmap<D, B>

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

impl<D, N, B> From<D> for StaticBitmap<D, B>
where D: ContainerRead<B, Slot = N>, N: Number, B: BitAccess,

Source§

fn from(f: D) -> Self

Converts to this type from the input type.
Source§

impl<D, B, Rhs, N> Intersection<Rhs, N, B> for StaticBitmap<D, B>
where D: ContainerRead<B, Slot = N>, B: BitAccess, Rhs: ContainerRead<B, Slot = N>, N: Number,

Source§

fn intersection_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst)
where Dst: ContainerWrite<B, Slot = N>,

Calculates intersection in-place. Result will be stored in dst. Read more
Source§

fn try_intersection_in<Dst>( &self, rhs: &Rhs, dst: &mut Dst, ) -> Result<(), IntersectionError>
where Dst: ContainerWrite<B, Slot = N>,

Calculates intersection in-place. Result will be stored in dst. Read more
Source§

fn intersection<Dst>(&self, rhs: &Rhs) -> Dst
where Dst: ContainerWrite<B, Slot = N> + TryWithSlots,

Calculates intersection. Result container will be created with try_with_slots function. Read more
Source§

fn try_intersection<Dst>(&self, rhs: &Rhs) -> Result<Dst, IntersectionError>
where Dst: ContainerWrite<B, Slot = N> + TryWithSlots,

Calculates intersection. Result container will be created with try_with_slots function. Read more
Source§

fn intersection_len(&self, rhs: &Rhs) -> usize

Calculates intersection length - ones count. It doesn’t allocate for storing intersection result. Read more
Source§

impl<'a, D, B> IntoIterator for &'a StaticBitmap<D, B>
where D: ContainerRead<B>, B: BitAccess,

Source§

type Item = <Iter<'a, D, B> as Iterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, D, B>

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<D, B> IntoIterator for StaticBitmap<D, B>
where D: ContainerRead<B>, B: BitAccess,

Source§

type Item = <IntoIter<D, B> as Iterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<D, B>

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<D: PartialEq, B: PartialEq> PartialEq for StaticBitmap<D, B>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<D, B> TryWithSlots for StaticBitmap<D, B>
where D: TryWithSlots, B: BitAccess,

Source§

fn try_with_slots(len: usize) -> Result<Self, WithSlotsError>

Creates new container with specified slots number.
Source§

impl<D, B, Rhs, N> Union<Rhs, N, B> for StaticBitmap<D, B>
where D: ContainerRead<B, Slot = N>, B: BitAccess, Rhs: ContainerRead<B, Slot = N>, N: Number,

Source§

fn union_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst)
where Dst: ContainerWrite<B, Slot = N>,

Calculates union in-place. Result will be stored in dst. Read more
Source§

fn try_union_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst) -> Result<(), UnionError>
where Dst: ContainerWrite<B, Slot = N>,

Calculates union in-place. Result will be stored in dst. Read more
Source§

fn union<Dst>(&self, rhs: &Rhs) -> Dst
where Dst: ContainerWrite<B, Slot = N> + TryWithSlots,

Calculates union. Result container will be created with try_with_slots function. Read more
Source§

fn try_union<Dst>(&self, rhs: &Rhs) -> Result<Dst, UnionError>
where Dst: ContainerWrite<B, Slot = N> + TryWithSlots,

Calculates union. Result container will be created with try_with_slots function. Read more
Source§

fn union_len(&self, rhs: &Rhs) -> usize

Calculates union length - ones count. It doesn’t allocate for storing union result. Read more
Source§

impl<D: Eq, B: Eq> Eq for StaticBitmap<D, B>

Source§

impl<D, B> StructuralPartialEq for StaticBitmap<D, B>

Auto Trait Implementations§

§

impl<D, B> Freeze for StaticBitmap<D, B>
where D: Freeze,

§

impl<D, B> RefUnwindSafe for StaticBitmap<D, B>

§

impl<D, B> Send for StaticBitmap<D, B>
where D: Send, B: Send,

§

impl<D, B> Sync for StaticBitmap<D, B>
where D: Sync, B: Sync,

§

impl<D, B> Unpin for StaticBitmap<D, B>
where D: Unpin, B: Unpin,

§

impl<D, B> UnwindSafe for StaticBitmap<D, B>
where D: UnwindSafe, B: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.