Struct smallbitvec::SmallBitVec
source · 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
impl SmallBitVec
sourcepub const fn new() -> SmallBitVec
pub const fn new() -> SmallBitVec
Create an empty vector.
sourcepub fn from_elem(len: usize, val: bool) -> SmallBitVec
pub fn from_elem(len: usize, val: bool) -> SmallBitVec
Create a vector containing len
bits, each set to val
.
sourcepub fn with_capacity(cap: usize) -> SmallBitVec
pub fn with_capacity(cap: usize) -> SmallBitVec
Create an empty vector with enough storage pre-allocated to store at least cap
bits
without resizing.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
The number of bits that can be stored in this bit vector without re-allocating.
sourcepub unsafe fn get_unchecked(&self, n: usize) -> bool
pub unsafe fn get_unchecked(&self, n: usize) -> bool
Get the nth bit in this bit vector, without bounds checks.
sourcepub fn set(&mut self, n: usize, val: bool)
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.
sourcepub unsafe fn set_unchecked(&mut self, n: usize, val: bool)
pub unsafe fn set_unchecked(&mut self, n: usize, val: bool)
Set the nth bit in this bit vector to val
, without bounds checks.
sourcepub fn push(&mut self, val: bool)
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));
sourcepub fn pop(&mut self) -> Option<bool>
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);
sourcepub fn remove(&mut self, idx: usize) -> bool
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.
sourcepub fn reserve(&mut self, additional: usize)
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
.
sourcepub(crate) unsafe fn set_len(&mut self, len: usize)
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.
sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Returns an iterator that yields the bits of the vector in order, as bool
values.
sourcepub fn range(&self, range: Range<usize>) -> VecRange<'_>
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);
sourcepub fn all_false(&self) -> bool
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.
sourcepub fn all_true(&self) -> bool
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.
sourcepub fn truncate(&mut self, len: usize)
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.
sourcepub fn resize(&mut self, len: usize, value: bool)
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
.
sourcepub(crate) fn reallocate(&mut self, cap: usize)
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.
sourcepub fn heap_ptr(&self) -> Option<*const usize>
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.
sourcepub fn into_storage(self) -> InternalStorage
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.
sourcepub unsafe fn from_storage(storage: InternalStorage) -> SmallBitVec
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) };
}
sourcepub(crate) fn is_inline(&self) -> bool
pub(crate) fn is_inline(&self) -> bool
If the rightmost bit is unset, then we treat it as inline storage.
sourcepub(crate) fn header_raw(&self) -> *mut Header
pub(crate) fn header_raw(&self) -> *mut Header
Get the header of a heap-allocated vector.
pub(crate) fn header_mut(&mut self) -> &mut Header
pub(crate) fn header(&self) -> &Header
sourcepub(crate) fn buffer_raw(&self) -> *mut [usize]
pub(crate) fn buffer_raw(&self) -> *mut [usize]
Get the buffer of a heap-allocated vector.
pub(crate) fn buffer_mut(&mut self) -> &mut [usize]
pub(crate) fn buffer(&self) -> &[usize]
Trait Implementations§
source§impl Clone for SmallBitVec
impl Clone for SmallBitVec
source§impl Debug for SmallBitVec
impl Debug for SmallBitVec
source§impl Default for SmallBitVec
impl Default for SmallBitVec
source§impl Drop for SmallBitVec
impl Drop for SmallBitVec
source§impl Extend<bool> for SmallBitVec
impl Extend<bool> for SmallBitVec
source§fn extend<I: IntoIterator<Item = bool>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = bool>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl FromIterator<bool> for SmallBitVec
impl FromIterator<bool> for SmallBitVec
source§impl Hash for SmallBitVec
impl Hash for SmallBitVec
source§impl Index<usize> for SmallBitVec
impl Index<usize> for SmallBitVec
source§impl<'a> IntoIterator for &'a SmallBitVec
impl<'a> IntoIterator for &'a SmallBitVec
source§impl IntoIterator for SmallBitVec
impl IntoIterator for SmallBitVec
source§impl PartialEq for SmallBitVec
impl PartialEq for SmallBitVec
impl Eq for SmallBitVec
Auto Trait Implementations§
impl Freeze for SmallBitVec
impl RefUnwindSafe for SmallBitVec
impl Send for SmallBitVec
impl Sync for SmallBitVec
impl Unpin for SmallBitVec
impl UnwindSafe for SmallBitVec
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)