CompressedBitmap

Struct CompressedBitmap 

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

A sparse, 2-level bitmap with a low memory footprint, optimised for reads.

A CompressedBitmap splits the bitmap up into blocks of usize bits, and uses a second bitmap to mark populated blocks, lazily allocating them as required. This strategy represents a sparsely populated bitmap such as:

   ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
   │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │
   └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

As two bitmaps, here initialising only a single blocks of usize bits in the second bitmap:

                 ┌───┬───┬───┬───┐
      Block map: │ 0 │ 1 │ 0 │ 0 │
                 └───┴─┬─┴───┴───┘
                       └──────┐
    ┌ ─ ┬ ─ ┬ ─ ┬ ─ ┐ ┌───┬───▼───┬───┐ ┌ ─ ┬ ─ ┬ ─ ┬ ─ ┐
      0   0   0   0   │ 1 │ 0 │ 0 │ 1 │   0   0   0   0
    └ ─ ┴ ─ ┴ ─ ┴ ─ ┘ └───┴───┴───┴───┘ └ ─ ┴ ─ ┴ ─ ┴ ─ ┘

This amortised O(1) insert operation takes ~4ns, while reading a value takes a constant time ~1ns on a Core i7 @ 2.60GHz.

In practice inserting large numbers of values into a CompressedBitmap can be slow - for higher write performance, use a VecBitmap and later convert to a CompressedBitmap when possible.

§Features

If the serde feature is enabled, a CompressedBitmap supports (de)serialisation with serde.

Implementations§

Source§

impl CompressedBitmap

Source

pub fn new(max_key: usize) -> Self

Construct a CompressedBitmap for space to hold up to max_key number of bits.

Source

pub fn size(&self) -> usize

Source

pub fn shrink_to_fit(&mut self)

Reduces the allocated memory usage of the bitmap to the minimum required for the current bitmap contents.

This is useful to minimise the memory footprint of a populated, read-only CompressedBitmap.

See Vec::shrink_to_fit.

Source

pub fn clear(&mut self)

Resets the state of the bitmap.

An efficient way to remove all elements in the bitmap to allow it to be reused. Does not shrink the allocated backing memory, instead retaining the capacity to avoid reallocations.

Source

pub fn set(&mut self, key: usize, value: bool)

Inserts key into the bitmap.

§Panics

This method MAY panic if key is more than the max_key value provided when initialising the bitmap.

If debug_assertions are enabled (such as in debug builds) inserting key > max will always panic. In release builds, this may not panic for values of key that are only slightly larger than max_key for performance reasons.

Source

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

Returns the value at key.

If a value for key was not previously set, false is returned.

§Panics

This method MAY panic if key is more than the max_key value provided when initialising the bitmap.

Source

pub fn or(&self, other: &Self) -> Self

Perform a bitwise OR against self and other, returning the resulting merged CompressedBitmap.

§Panics

This method panics if other was not configured with the same max_key.

Trait Implementations§

Source§

impl Bitmap for CompressedBitmap

Source§

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

Return true if the given bit index was previously set to true.
Source§

fn set(&mut self, key: usize, value: bool)

Set bit indexed by key to value.
Source§

fn byte_size(&self) -> usize

Return the size of the bitmap in bytes.
Source§

fn or(&self, other: &Self) -> Self

Return the bitwise OR of both self and other.`
Source§

fn new_with_capacity(max_key: usize) -> Self

Construct a new Bitmap impl with capacity to hold at least max_key number of bits.
Source§

impl Clone for CompressedBitmap

Source§

fn clone(&self) -> CompressedBitmap

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 Debug for CompressedBitmap

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for CompressedBitmap

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<VecBitmap> for CompressedBitmap

Source§

fn from(bitmap: VecBitmap) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for CompressedBitmap

Source§

fn eq(&self, other: &CompressedBitmap) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for CompressedBitmap

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for CompressedBitmap

Source§

impl StructuralPartialEq for CompressedBitmap

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> 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,