Skip to main content

BitMap

Struct BitMap 

Source
pub struct BitMap<const N: usize, S: State = Clean> { /* private fields */ }
Expand description

A historical bitmap that maintains one actual bitmap plus diffs for history.

Uses a type-state pattern to track whether the bitmap is clean (no pending mutations) or dirty (has pending mutations).

Commit numbers must be strictly monotonically increasing and < u64::MAX.

Implementations§

Source§

impl<const N: usize> BitMap<N>

Source

pub const fn new() -> Self

Create a new empty historical bitmap.

Source

pub fn new_with_pruned_chunks(pruned_chunks: usize) -> Result<Self, Error>

Create a new historical bitmap with the given number of pruned chunks.

Source

pub fn into_dirty(self) -> DirtyBitMap<N>

Transition to dirty state to begin making mutations.

All mutations are applied to a diff layer and do not affect the current bitmap until commit.

§Examples
let bitmap: BitMap<4> = BitMap::new();

let mut dirty = bitmap.into_dirty();
dirty.push(true);
dirty.push(false);
let bitmap = dirty.commit(1).unwrap();

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

pub fn apply_batch<F>(self, commit_number: u64, f: F) -> Result<Self, Error>
where F: FnOnce(&mut DirtyBitMap<N>),

Execute a closure with a dirty bitmap and commit it at the given commit number.

§Errors

Returns Error::NonMonotonicCommit if the commit number is not greater than the previous commit.

Returns Error::ReservedCommitNumber if the commit number is u64::MAX.

§Examples
let mut bitmap: BitMap<4> = BitMap::new();

bitmap = bitmap.apply_batch(1, |dirty| {
    dirty.push(true).push(false);
}).unwrap();

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

pub fn get_at_commit(&self, commit_number: u64) -> Option<Prunable<N>>

Get the bitmap state as it existed at a specific commit.

Returns None if the commit does not exist or if commit_number is u64::MAX (which is reserved and cannot be used as a commit number).

This reconstructs the historical state by applying reverse diffs backward from the current state. Each commit’s reverse diff describes the state before that commit, so we “undo” commits one by one until we reach the target.

§Examples
let mut bitmap: BitMap<4> = BitMap::new();

bitmap = bitmap.apply_batch(1, |dirty| {
    dirty.push(true);
    dirty.push(false);
}).unwrap();

bitmap = bitmap.apply_batch(2, |dirty| {
    dirty.set_bit(0, false);
    dirty.push(true);
}).unwrap();

// Get state as it was at commit 1
let state_at_1 = bitmap.get_at_commit(1).unwrap();
assert_eq!(state_at_1.len(), 2);
assert!(state_at_1.get_bit(0));
assert!(!state_at_1.get_bit(1));

// Current state is different
assert_eq!(bitmap.len(), 3);
assert!(!bitmap.get_bit(0));
Source

pub fn commit_exists(&self, commit_number: u64) -> bool

Check if a commit exists.

Source

pub fn commits(&self) -> impl Iterator<Item = u64> + '_

Get an iterator over all commit numbers in ascending order.

Source

pub fn latest_commit(&self) -> Option<u64>

Get the latest commit number, if any commits exist.

Source

pub fn earliest_commit(&self) -> Option<u64>

Get the earliest commit number, if any commits exist.

Source

pub const fn current(&self) -> &Prunable<N>

Get a reference to the current bitmap state.

Source

pub const fn len(&self) -> u64

Number of bits in the current bitmap.

Source

pub const fn is_empty(&self) -> bool

Returns true if the current bitmap is empty.

Source

pub fn get_bit(&self, bit: u64) -> bool

Get the value of a bit in the current bitmap.

Source

pub fn get_chunk_containing(&self, bit: u64) -> &[u8; N]

Get the chunk containing a bit in the current bitmap.

Source

pub const fn pruned_chunks(&self) -> usize

Number of pruned chunks in the current bitmap.

Source

pub fn prune_commits_before(&mut self, commit_number: u64) -> usize

Remove all commits with numbers below the commit number.

Returns the number of commits removed.

Source

pub fn clear_history(&mut self)

Clear all historical commits.

Source§

impl<const N: usize> BitMap<N, Dirty<N>>

Source

pub const fn len(&self) -> u64

Get the length of the bitmap as it would be after committing.

Source

pub const fn is_empty(&self) -> bool

Returns true if the bitmap would be empty after committing.

Source

pub const fn pruned_chunks(&self) -> usize

Get the number of pruned chunks after committing.

Source

pub fn get_bit(&self, bit: u64) -> bool

Get a bit value with read-through semantics.

Returns the bit’s value as it would be after committing. Priority: appended bits > modified bits > original bitmap.

§Panics

Panics if the bit offset is out of bounds or if the bit has been pruned.

Source

pub fn get_chunk(&self, bit: u64) -> [u8; N]

Get a chunk value with read-through semantics.

Reconstructs the chunk if it has modifications, otherwise returns from current.

§Panics

Panics if the bit offset is out of bounds or if the chunk has been pruned.

Source

pub fn set_bit(&mut self, bit: u64, value: bool) -> &mut Self

Set a bit value.

§Panics

Panics if the bit offset is out of bounds or if the bit has been pruned.

Source

pub fn push(&mut self, bit: bool) -> &mut Self

Push a bit to the end of the bitmap.

Source

pub fn push_byte(&mut self, byte: u8) -> &mut Self

Push a byte to the end of the bitmap.

Source

pub fn push_chunk(&mut self, chunk: &[u8; N]) -> &mut Self

Push a full chunk to the end of the bitmap.

Source

pub fn pop(&mut self) -> bool

Pop the last bit from the bitmap.

Returns the value of the popped bit, accounting for any modifications.

§Panics

Panics if the bitmap is empty.

Source

pub fn prune_to_bit(&mut self, bit: u64) -> &mut Self

Prune chunks up to the chunk containing the given bit offset.

Note: bit can equal projected_len when pruning at a chunk boundary.

§Panics

Panics if bit is > the projected length.

Source

pub fn commit(self, commit_number: u64) -> Result<CleanBitMap<N>, Error>

Commit the changes and return a clean bitmap with a historical snapshot.

§Errors

Returns Error::NonMonotonicCommit if the commit number is not greater than the previous commit.

Returns Error::ReservedCommitNumber if the commit number is u64::MAX.

Source

pub fn abort(self) -> CleanBitMap<N>

Abort the changes and return to clean state.

All pending mutations are discarded.

Trait Implementations§

Source§

impl<const N: usize, S: Clone + State> Clone for BitMap<N, S>

Source§

fn clone(&self) -> BitMap<N, S>

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 N: usize, S: Debug + State> Debug for BitMap<N, S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize, S> Freeze for BitMap<N, S>
where S: Freeze,

§

impl<const N: usize, S> RefUnwindSafe for BitMap<N, S>
where S: RefUnwindSafe,

§

impl<const N: usize, S> Send for BitMap<N, S>

§

impl<const N: usize, S> Sync for BitMap<N, S>

§

impl<const N: usize, S> Unpin for BitMap<N, S>
where S: Unpin,

§

impl<const N: usize, S> UnwindSafe for BitMap<N, S>
where S: 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V