pub struct SmallBitVec {
    pub(crate) data: usize,
}
Expand description

A resizable bit vector, optimized for size and inline storage.

SmallBitVec is exactly one word wide. Depending on the required capacity, this word either stores the bits inline, or it stores a pointer to a separate buffer on the heap.

Fields§

§data: usize

Implementations§

source§

impl SmallBitVec

source

pub const fn new() -> SmallBitVec

Create an empty vector.

source

pub fn from_elem(len: usize, val: bool) -> SmallBitVec

Create a vector containing len bits, each set to val.

source

pub fn with_capacity(cap: usize) -> SmallBitVec

Create an empty vector with enough storage pre-allocated to store at least cap bits without resizing.

source

pub fn len(&self) -> usize

The number of bits stored in this bit vector.

source

pub fn is_empty(&self) -> bool

Returns true if this vector contains no bits.

source

pub fn capacity(&self) -> usize

The number of bits that can be stored in this bit vector without re-allocating.

source

pub fn get(&self, n: usize) -> Option<bool>

Get the nth bit in this bit vector.

source

pub fn last(&self) -> Option<bool>

Get the last bit in this bit vector.

source

pub unsafe fn get_unchecked(&self, n: usize) -> bool

Get the nth bit in this bit vector, without bounds checks.

source

pub fn set(&mut self, n: usize, val: bool)

Set the nth bit in this bit vector to val. Panics if the index is out of bounds.

source

pub unsafe fn set_unchecked(&mut self, n: usize, val: bool)

Set the nth bit in this bit vector to val, without bounds checks.

source

pub fn push(&mut self, val: bool)

Append a bit to the end of the vector.

use smallbitvec::SmallBitVec;
let mut v = SmallBitVec::new();
v.push(true);

assert_eq!(v.len(), 1);
assert_eq!(v.get(0), Some(true));
source

pub fn pop(&mut self) -> Option<bool>

Remove the last bit from the vector and return it, if there is one.

use smallbitvec::SmallBitVec;
let mut v = SmallBitVec::new();
v.push(false);

assert_eq!(v.pop(), Some(false));
assert_eq!(v.len(), 0);
assert_eq!(v.pop(), None);
source

pub fn remove(&mut self, idx: usize) -> bool

Remove and return the bit at index idx, shifting all later bits toward the front.

Panics if the index is out of bounds.

source

pub fn clear(&mut self)

Remove all elements from the vector, without deallocating its buffer.

source

pub fn reserve(&mut self, additional: usize)

Reserve capacity for at least additional more elements to be inserted.

May reserve more space than requested, to avoid frequent reallocations.

Panics if the new capacity overflows usize.

Re-allocates only if self.capacity() < self.len() + additional.

source

pub(crate) unsafe fn set_len(&mut self, len: usize)

Set the length of the vector. The length must not exceed the capacity.

If this makes the vector longer, then the values of its new elements are not specified.

source

pub fn iter(&self) -> Iter<'_>

Returns an iterator that yields the bits of the vector in order, as bool values.

source

pub fn range(&self, range: Range<usize>) -> VecRange<'_>

Returns an immutable view of a range of bits from this vec.

#[macro_use] extern crate smallbitvec;
let v = sbvec![true, false, true];
let r = v.range(1..3);
assert_eq!(r[1], true);
source

pub fn all_false(&self) -> bool

Returns true if all the bits in the vec are set to zero/false.

On an empty vector, returns true.

source

pub fn all_true(&self) -> bool

Returns true if all the bits in the vec are set to one/true.

On an empty vector, returns true.

source

pub fn truncate(&mut self, len: usize)

Shorten the vector, keeping the first len elements and dropping the rest.

If len is greater than or equal to the vector’s current length, this has no effect.

This does not re-allocate.

source

pub fn resize(&mut self, len: usize, value: bool)

Resizes the vector so that its length is equal to len.

If len is less than the current length, the vector simply truncated.

If len is greater than the current length, value is appended to the vector until its length equals len.

source

pub(crate) fn reallocate(&mut self, cap: usize)

Resize the vector to have capacity for at least cap bits.

cap must be at least as large as the length of the vector.

source

pub fn heap_ptr(&self) -> Option<*const usize>

If the vector owns a heap allocation, returns a pointer to the start of the allocation.

The layout of the data at this allocation is a private implementation detail.

source

pub fn into_storage(self) -> InternalStorage

Converts this SmallBitVec into its internal representation.

The layout of the data inside both enum variants is a private implementation detail.

source

pub unsafe fn from_storage(storage: InternalStorage) -> SmallBitVec

Creates a SmallBitVec directly from the internal storage of another SmallBitVec.

Safety

This is highly unsafe. storage needs to have been previously generated via SmallBitVec::into_storage (at least, it’s highly likely to be incorrect if it wasn’t.) Violating this may cause problems like corrupting the allocator’s internal data structures.

Examples

fn main() {
    let v = SmallBitVec::from_elem(200, false);

    // Get the internal representation of the SmallBitVec.
    // unless we transfer its ownership somewhere else.
    let storage = v.into_storage();

    /// Make a copy of the SmallBitVec's data.
    let cloned_storage = match storage {
        InternalStorage::Spilled(vs) => InternalStorage::Spilled(vs.clone()),
        inline => inline,
    };

    /// Create a new SmallBitVec from the coped storage.
    let v = unsafe { SmallBitVec::from_storage(cloned_storage) };
}
source

pub(crate) fn is_inline(&self) -> bool

If the rightmost bit is unset, then we treat it as inline storage.

source

pub(crate) fn is_heap(&self) -> bool

Otherwise, data is a pointer to a heap allocation.

source

pub(crate) fn header_raw(&self) -> *mut Header

Get the header of a heap-allocated vector.

source

pub(crate) fn header_mut(&mut self) -> &mut Header

source

pub(crate) fn header(&self) -> &Header

source

pub(crate) fn buffer_raw(&self) -> *mut [usize]

Get the buffer of a heap-allocated vector.

source

pub(crate) fn buffer_mut(&mut self) -> &mut [usize]

source

pub(crate) fn buffer(&self) -> &[usize]

Trait Implementations§

source§

impl Clone for SmallBitVec

source§

fn clone(&self) -> Self

Returns a copy 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 SmallBitVec

source§

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

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

impl Default for SmallBitVec

source§

fn default() -> Self

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

impl Drop for SmallBitVec

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Extend<bool> for SmallBitVec

source§

fn extend<I: IntoIterator<Item = bool>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl FromIterator<bool> for SmallBitVec

source§

fn from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl Hash for SmallBitVec

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 Index<usize> for SmallBitVec

§

type Output = bool

The returned type after indexing.
source§

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

Performs the indexing (container[index]) operation. Read more
source§

impl<'a> IntoIterator for &'a SmallBitVec

§

type Item = bool

The type of the elements being iterated over.
§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Iter<'a>

Creates an iterator from a value. Read more
source§

impl IntoIterator for SmallBitVec

§

type Item = bool

The type of the elements being iterated over.
§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq<SmallBitVec> for SmallBitVec

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for SmallBitVec

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.