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
impl CompressedBitmap
Sourcepub fn new(max_key: usize) -> Self
pub fn new(max_key: usize) -> Self
Construct a CompressedBitmap for space to hold up to max_key number
of bits.
pub fn size(&self) -> usize
Sourcepub fn shrink_to_fit(&mut self)
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.
Sourcepub fn clear(&mut self)
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.
Sourcepub fn set(&mut self, key: usize, value: bool)
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.
Sourcepub fn get(&self, key: usize) -> bool
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.
Sourcepub fn or(&self, other: &Self) -> Self
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
impl Bitmap for CompressedBitmap
Source§impl Clone for CompressedBitmap
impl Clone for CompressedBitmap
Source§fn clone(&self) -> CompressedBitmap
fn clone(&self) -> CompressedBitmap
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more