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

Creates new bitmap from container.

Returns number of ones in the bitmap.

Returns number of zeros in the bitmap.

Converts bitmap into inner container.

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

Returns iterator over slots.

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

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Gets value of stored slot.

Gets number of stored slots.

Gets max number of bits.

Gets mutable reference to stored slot.

Formats the value using the given formatter. Read more

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

Converts to this type from the input type.

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

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

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

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

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

Creates new container with specified slots number.

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

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

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.