Skip to main content

FlexBitMap

Struct FlexBitMap 

Source
pub struct FlexBitMap { /* private fields */ }
Expand description

A flexible bitmap that can grow dynamically.

This bitmap uses a boxed slice internally and automatically grows when needed. It’s designed for situations where the maximum size isn’t known at compile time.

§Performance

  • Growing incurs allocation cost, but subsequent operations are O(1)
  • Uses the same high-performance range operations as FixedBitMap
  • Memory usage grows in 64-bit blocks

§Examples

use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

// Set individual bits
bitmap.set(10);
bitmap.set(20);

// Set ranges - automatically grows if needed
bitmap.set_range(100, 50);

// Check bits
assert!(bitmap.get(10));
assert!(bitmap.get(125));
assert!(!bitmap.get(200));

// Count bits
assert_eq!(bitmap.count_ones(), 52);

Implementations§

Source§

impl FlexBitMap

Source

pub fn new() -> Self

Creates a new empty bitmap.

The bitmap starts empty and will grow as needed when bits are set.

§Examples
use ranged_bitmap::FlexBitMap;

let bitmap = FlexBitMap::new();
assert_eq!(bitmap.count_ones(), 0);
Source

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

Sets a bit to 1.

Automatically grows the bitmap if the bit is beyond current capacity.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set(42);

assert!(bitmap.get(42));
Source

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

Clears a bit to 0.

If the bit is beyond current capacity, this operation does nothing.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set(42);
bitmap.clear(42);

assert!(!bitmap.get(42));
Source

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

Gets the value of a bit.

Returns false for bits beyond current capacity.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

assert!(!bitmap.get(100)); // Beyond capacity, returns false

bitmap.set(100);

assert!(bitmap.get(100));
Source

pub fn count_ones(&self) -> usize

Counts the number of set bits in the bitmap.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set(10);
bitmap.set(20);

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

pub fn count_zeros(&self) -> usize

Counts the number of clear bits in the bitmap

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set_range(0, 10);

// count_zeros counts all zeros in allocated blocks, not just the set range
assert_eq!(bitmap.count_zeros(), bitmap.capacity() - 10);
Source

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

Iterates over a range of bits, returning (position, value) pairs.

For bits beyond current capacity, the value is always false.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set(5);
bitmap.set(7);

let bits: Vec<_> = bitmap.iter_range(4, 4).collect();

assert_eq!(bits, vec![(4, false), (5, true), (6, false), (7, true)]);
Source§

impl FlexBitMap

Source

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

Sets all bits in a range to 1.

Automatically grows the bitmap if the range extends beyond current capacity.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();
bitmap.set_range(100, 50); // Automatically grows

for i in 100..150 {
    assert!(bitmap.get(i));
}
Source

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

Clears all bits in a range to 0.

For bits beyond current capacity, this operation does nothing.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set_range(0, 100);
bitmap.clear_range(50, 25);

// Bits 50-74 should be clear, others set
assert!(!bitmap.get(60));
assert!(bitmap.get(40));
assert!(bitmap.get(80));
Source

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

Checks if all bits in a range are set to 1.

For bits beyond current capacity, they are considered unset.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set_range(10, 5);

assert!(bitmap.check_range_is_set(10, 5));
assert!(!bitmap.check_range_is_set(10, 6)); // Bit 15 is not set
Source

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

Checks if all bits in a range are clear to 0.

For bits beyond current capacity, they are considered clear.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();

bitmap.set_range(10, 5);

assert!(bitmap.check_range_is_unset(0, 10));
assert!(!bitmap.check_range_is_unset(10, 5));
Source

pub fn capacity(&self) -> usize

Returns the current capacity in bits.

This is the highest bit position that can be accessed without growing.

§Examples
use ranged_bitmap::FlexBitMap;

let mut bitmap = FlexBitMap::new();
assert_eq!(bitmap.capacity(), 0);

bitmap.set(100);
assert!(bitmap.capacity() >= 100);
Source

pub fn blocks(&self) -> usize

Returns the number of allocated blocks.

Each block is usize bits (typically 64 bits).

Trait Implementations§

Source§

impl Default for FlexBitMap

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> 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, 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.