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 ofusizeblocks 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 rangeImplementations§
Source§impl<const BLOCKS: usize> FixedBitMap<BLOCKS>
impl<const BLOCKS: usize> FixedBitMap<BLOCKS>
Sourcepub const fn new() -> Self
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 systemSourcepub const fn set(&mut self, bit: usize)
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));Sourcepub const fn clear(&mut self, bit: usize)
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));Sourcepub const fn get(&self, bit: usize) -> bool
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));Sourcepub const fn count_ones(&self) -> usize
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);Sourcepub const fn count_zeros(&self) -> usize
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 setSourcepub const fn iter_range(
&self,
start: usize,
len: usize,
) -> impl Iterator<Item = (usize, bool)> + use<'_, BLOCKS>
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)]);Sourcepub const fn set_range(&mut self, start: usize, len: usize)
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 setSourcepub const fn clear_range(&mut self, start: usize, len: usize)
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 setSourcepub const fn check_range_is_set(&mut self, start: usize, len: usize) -> bool
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));Sourcepub const fn check_range_is_unset(&mut self, start: usize, len: usize) -> bool
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 againTrait Implementations§
Source§impl<const BLOCKS: usize> Clone for FixedBitMap<BLOCKS>
impl<const BLOCKS: usize> Clone for FixedBitMap<BLOCKS>
Source§fn clone(&self) -> FixedBitMap<BLOCKS>
fn clone(&self) -> FixedBitMap<BLOCKS>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more