Skip to main content

FixedBitMap

Struct FixedBitMap 

Source
pub struct FixedBitMap<const BLOCKS: usize> { /* private fields */ }
Expand description

A fixed-size bitmap with range operations.

This is the main type of the library, providing a stack-allocated bitmap with a size determined at compile time. It offers efficient operations for both individual bits and ranges of bits.

The bitmap is stored as an array of usize blocks, where each block contains usize::BITS bits. For example, on a 64-bit system, FixedBitMap<4> provides 256 bits of storage.

§Type Parameters

  • BLOCKS - The number of usize blocks in the bitmap

§Examples

use ranged_bitmap::FixedBitMap;

const BLOCKS: usize = ranged_bitmap::blocks_number_for_bits(100);

let mut bitmap = FixedBitMap::<BLOCKS>::new();

// Set bits individually
bitmap.set(0);
bitmap.set(99);

// Set a range efficiently
bitmap.set_range(10, 50);

assert!(bitmap.get(0));
assert!(bitmap.get(99));
assert!(bitmap.check_range_is_set(10, 50));

// Count set bits
assert_eq!(bitmap.count_ones(), 52); // 2 individual + 50 range

Implementations§

Source§

impl<const BLOCKS: usize> FixedBitMap<BLOCKS>

Source

pub const fn new() -> Self

Create a new bitmap with all bits initially cleared.

§Example
use ranged_bitmap::FixedBitMap;

const BLOCKS: usize = ranged_bitmap::blocks_number_for_bits(100);

let mut bitmap = FixedBitMap::<BLOCKS>::new();

assert_eq!(bitmap.count_ones(), 0);
assert_eq!(bitmap.count_zeros(), 128); // 2 * 64 bits on 64-bit system
Source

pub const fn set(&mut self, bit: usize)

Set a single bit to true.

This function sets the specified bit to true using a single bitwise OR operation.

§Panics

In debug builds, this function will panic if bit is out of bounds (i.e., bit >= BLOCKS * usize::BITS). In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<1>::new();

bitmap.set(10);
bitmap.set(30);

assert!(bitmap.get(10));
assert!(bitmap.get(30));
Source

pub const fn clear(&mut self, bit: usize)

Clear a single bit to false.

This function sets the specified bit to false using a single bitwise AND operation with an inverted mask.

§Panics

In debug builds, this function will panic if bit is out of bounds. In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<1>::new();

bitmap.set_range(0, 32); // Set all bits from 0 to 31
bitmap.clear(10);

assert!(!bitmap.get(10));
assert!(bitmap.get(11));
Source

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

Get the value of a single bit.

This function returns true if the specified bit is set, false otherwise.

§Panics

In debug builds, this function will panic if bit is out of bounds. In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<1>::new();

bitmap.set(10);

assert!(bitmap.get(10));
assert!(!bitmap.get(11));
Source

pub const fn count_ones(&self) -> usize

Count the number of set bits (ones) in the bitmap.

This function iterates over all blocks and uses the hardware-accelerated count_ones() method to count set bits efficiently. The operation is O(n) where n is the number of blocks.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<2>::new();

bitmap.set(10);
bitmap.set(70);
bitmap.set(127);

assert_eq!(bitmap.count_ones(), 3);
Source

pub const fn count_zeros(&self) -> usize

Count the number of clear bits (zeros) in the bitmap.

This function iterates over all blocks and uses the hardware-accelerated count_zeros() method to count clear bits efficiently. The operation is O(n) where n is the number of blocks.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<1>::new();

assert_eq!(bitmap.count_zeros(), usize::BITS as usize);

bitmap.set(10);
assert_eq!(bitmap.count_zeros(), usize::BITS as usize - 1); // One bit is set
Source

pub const fn iter_range( &self, start: usize, len: usize, ) -> impl Iterator<Item = (usize, bool)> + use<'_, BLOCKS>

Create an iterator over a range of bits.

This function returns an iterator that yields tuples of (index, value) for each bit in the specified range. The iterator is exact-sized and provides efficient range traversal.

§Panics

In debug builds, this function will panic if the range is out of bounds. In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<1>::new();

bitmap.set(10);
bitmap.set(12);
bitmap.set(15);

let bits: Vec<(usize, bool)> = bitmap.iter_range(8, 10).collect();
assert_eq!(bits, vec![(8, false), (9, false), (10, true), (11, false), (12, true), (13, false), (14, false), (15, true), (16, false), (17, false)]);
Source

pub const fn set_range(&mut self, start: usize, len: usize)

Set a range of bits to true.

This function sets all bits in the specified range to true using optimized block operations. The operation is much more efficient than setting bits individually, especially for large ranges.

§Panics

In debug builds, this function will panic if the range is out of bounds. In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<2>::new();

bitmap.set_range(10, 50);  // Set bits 10-59

for i in 10..60 {
    assert!(bitmap.get(i));
}

assert!(!bitmap.get(9));   // Before range is not set
assert!(!bitmap.get(60));  // After range is not set
Source

pub const fn clear_range(&mut self, start: usize, len: usize)

Clear a range of bits to false.

This function clears all bits in the specified range to false using optimized block operations. The operation is much more efficient than clearing bits individually, especially for large ranges.

§Panics

In debug builds, this function will panic if the range is out of bounds. In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<2>::new();

bitmap.set_range(0, usize::BITS as usize * 2);  // Set all bits
bitmap.clear_range(10, 50); // Clear bits 10-59

for i in 10..60 {
    assert!(!bitmap.get(i));
}

assert!(bitmap.get(9));   // Before range remains set
assert!(bitmap.get(60));  // After range remains set
Source

pub const fn check_range_is_set(&mut self, start: usize, len: usize) -> bool

Check if all bits in a range are set.

This function returns true if and only if every bit in the specified range is set to true. The operation is optimized using precomputed lookup tables and early termination for large ranges.

§Panics

In debug builds, this function will panic if the range is out of bounds. In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<2>::new();

// Initially no bits are set
assert!(!bitmap.check_range_is_set(10, 50));

// Set the entire range
bitmap.set_range(10, 50);
assert!(bitmap.check_range_is_set(10, 50));
Source

pub const fn check_range_is_unset(&mut self, start: usize, len: usize) -> bool

Check if all bits in a range are unset.

This function returns true if and only if every bit in the specified range is set to false. The operation is optimized using precomputed lookup tables and early termination for large ranges.

§Panics

In debug builds, this function will panic if the range is out of bounds. In release builds, bound checking is omitted for performance.

§Example
use ranged_bitmap::FixedBitMap;

let mut bitmap = FixedBitMap::<2>::new();

// Initially all bits are unset
assert!(bitmap.check_range_is_unset(10, 50));

// Set some bits in the range
bitmap.set_range(10, 25);  // Set first half
assert!(!bitmap.check_range_is_unset(10, 50)); // No longer all unset

// Clear the range again
let mut bitmap = FixedBitMap::<2>::new();
assert!(bitmap.check_range_is_unset(10, 50)); // All unset again

Trait Implementations§

Source§

impl<const BLOCKS: usize> Clone for FixedBitMap<BLOCKS>

Source§

fn clone(&self) -> FixedBitMap<BLOCKS>

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<const BLOCKS: usize> Default for FixedBitMap<BLOCKS>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<const BLOCKS: usize> Freeze for FixedBitMap<BLOCKS>

§

impl<const BLOCKS: usize> RefUnwindSafe for FixedBitMap<BLOCKS>

§

impl<const BLOCKS: usize> Send for FixedBitMap<BLOCKS>

§

impl<const BLOCKS: usize> Sync for FixedBitMap<BLOCKS>

§

impl<const BLOCKS: usize> Unpin for FixedBitMap<BLOCKS>

§

impl<const BLOCKS: usize> UnwindSafe for FixedBitMap<BLOCKS>

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.