GrowableBitMap

Struct GrowableBitMap 

Source
pub struct GrowableBitMap<S>
where S: BitStorage,
{ /* private fields */ }
Expand description

A growable compact boolean array that can be parameterized on its storage.

Bits are stored contiguously. The first value is packed into the least significant bits of the first word of the backing storage.

The storage must implement the unsafe trait BitStorage.

§Caveats

  • The GrowableBitMap::set_bit method may allocate way too much memory compared to what you really need (if for example, you only plan to set the bits between 1200 and 1400). In this case, storing the offset of 1200 somewhere else and storing the values in the range 0..=200 in the GrowableBitMap is probably the most efficient solution.

Implementations§

Source§

impl<S> GrowableBitMap<S>
where S: BitStorage,

Source

pub fn new() -> Self

Creates a new, empty GrowableBitMap.

This does not allocate anything.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

assert!(GrowableBitMap::<u8>::new().is_empty());
Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty GrowableBitMap with the specified capacity in bits.

When capacity is zero, nothing is allocated.

When capacity is not zero, the bit capacity - 1 can be set without any other allocation and the returned GrowableBitMap is guaranteed to be able to hold capacity bits without reallocating (and maybe more if the given capacity is not a multiple of the number of bits in one instance of the backing storage).

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u16>::with_capacity(16);
assert!(b.is_empty());
assert_eq!(b.capacity(), 16);

b.set_bit(15);
assert_eq!(b.capacity(), 16);

b.set_bit(17);
assert!(b.capacity() >= 16);
Source

pub fn is_empty(&self) -> bool

true if the GrowableBitMap is empty.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u32>::new();
assert!(b.is_empty());

b.set_bit(25);
assert!(!b.is_empty());
Source

pub fn get_bit(&self, index: usize) -> bool

Gets the bit at the given index and returns true when it is set to 1, false when it is not.

This will not panic if the index is out of range of the backing storage, only return false.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u64>::new();
assert!(!b.get_bit(0));
assert!(!b.get_bit(15));

b.set_bit(15);
assert!(!b.get_bit(0));
assert!(b.get_bit(15));
Source

pub fn set_bit(&mut self, index: usize) -> bool

Sets the bit at the given index and returns whether the bit was set to 1 by this call or not.

Note: This will grow the backing storage as needed to have enough storage for the given index. If you set the bit 12800 with a storage of u8s the backing storage will allocate 1600 u8s since sizeof::<u8>() == 1 byte.

See also the Caveats section on GrowableBitMap.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u128>::new();
assert!(b.set_bit(0)); // Bit 0 was not set before, returns true.
assert!(!b.set_bit(0)); // Bit 0 was already set, returns false.

assert!(b.set_bit(255)); // The bitmap will grow as needed to set the bit.
Source

pub fn clear_bit(&mut self, index: usize) -> bool

Clears the bit at the given index and returns whether the bit was set to 0 by this call or not.

Note: this function will never allocate nor free memory, even when the bit being cleared is the last 1 in the value at the end of the backing storage.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u8>::new();
assert!(!b.clear_bit(3)); // Bit 0 was not set before, returns false.

b.set_bit(3);
assert!(b.clear_bit(3));

Testing the effects on capacity:

use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u8>::new();
b.set_bit(125);

let old_capa = b.capacity();
b.clear_bit(125);
assert_eq!(old_capa, b.capacity());
Source

pub fn clear(&mut self)

Clears the bitmap, removing all values (setting them all to 0).

This method has no effect on the allocated capacity of the bitmap.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u16>::new();
b.set_bit(4);
assert!(!b.is_empty());

b.clear();
assert!(b.is_empty());

Testing the effects on capacity:

use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u16>::new();
b.set_bit(125);

let old_capa = b.capacity();
b.clear();
assert_eq!(old_capa, b.capacity());
Source

pub fn count_ones(&self) -> usize

Counts the number of bits that are set to 1 in the whole bitmap.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u32>::new();
assert_eq!(b.count_ones(), 0);

b.set_bit(2);
assert_eq!(b.count_ones(), 1);

b.set_bit(9);
assert_eq!(b.count_ones(), 2);

b.clear();
assert_eq!(b.count_ones(), 0);
Source

pub fn capacity(&self) -> usize

Returns the number of bits the bitmap can hold without reallocating.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u64>::new();
assert_eq!(b.capacity(), 0);

b.set_bit(380);
assert_eq!(b.capacity(), 384);
Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the GrowableBitMap as much as possible.

It will drop down as close as possible to the length needed to store the last bit set to 1 and not more but the allocator may still inform the bitmap that there is space for a few more elements.

§Examples
use growable_bitmap::{GrowableBitMap, BitStorage};

let mut b = GrowableBitMap::<u128>::with_capacity(381);

b.set_bit(127);
b.set_bit(380);
assert_eq!(b.capacity(), 384);

b.clear_bit(380);
b.shrink_to_fit();
assert_eq!(b.capacity(), 128);

Trait Implementations§

Source§

impl<S> Clone for GrowableBitMap<S>
where S: BitStorage + Clone,

Source§

fn clone(&self) -> GrowableBitMap<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<S> Debug for GrowableBitMap<S>
where S: BitStorage + Debug,

Source§

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

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

impl<S> Hash for GrowableBitMap<S>
where S: BitStorage + Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<S> Ord for GrowableBitMap<S>
where S: BitStorage + Ord,

Source§

fn cmp(&self, other: &GrowableBitMap<S>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<S> PartialEq for GrowableBitMap<S>
where S: BitStorage + PartialEq,

Source§

fn eq(&self, other: &GrowableBitMap<S>) -> 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<S> PartialOrd for GrowableBitMap<S>

Source§

fn partial_cmp(&self, other: &GrowableBitMap<S>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<S> Eq for GrowableBitMap<S>
where S: BitStorage + Eq,

Source§

impl<S> StructuralPartialEq for GrowableBitMap<S>
where S: BitStorage,

Auto Trait Implementations§

§

impl<S> Freeze for GrowableBitMap<S>

§

impl<S> RefUnwindSafe for GrowableBitMap<S>
where S: RefUnwindSafe,

§

impl<S> Send for GrowableBitMap<S>
where S: Send,

§

impl<S> Sync for GrowableBitMap<S>
where S: Sync,

§

impl<S> Unpin for GrowableBitMap<S>
where S: Unpin,

§

impl<S> UnwindSafe for GrowableBitMap<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.