bitvek

Struct BitVec

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

A simple bit vector implementation.

Implementations§

Source§

impl BitVec

Source

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

Returns an iterator over the bits of the vector.

§Examples
use bitvek::bitvec;

let vec = bitvec![true, false, true, false];
let mut iter = vec.iter();

assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(false));
assert_eq!(iter.next_back(), Some(false));
assert_eq!(iter.next_back(), Some(true));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
Source§

impl BitVec

Source

pub fn new() -> Self

Creates a new empty BitVec.

§Examples
use bitvek::BitVec;

let vec = BitVec::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new empty BitVec with the specified capacity.

The final capacity will be at least as large as (capacity / 8 + 1) * 8.

§Examples
use bitvek::BitVec;

let vec = BitVec::with_capacity(10);
assert!(vec.capacity() >= 16);
Source§

impl BitVec

Source

pub fn capacity(&self) -> usize

Returns the total number of bits the vector can hold without reallocating.

§Examples
use bitvek::bitvec;

let vec = bitvec![true, false, true, false];
assert!(vec.capacity() >= 8);
§Notes

The capacity of the vector is always a multiple of 8.

Source

pub fn len(&self) -> usize

Returns the number of bits in the vector.

§Examples
use bitvek::bitvec;

let vec = bitvec![true, false, true, false];
assert_eq!(vec.len(), 4);
Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no bits.

§Examples
use bitvek::bitvec;

let vec = bitvec![];
assert!(vec.is_empty());

let vec = bitvec![true, false, true, false];
assert!(!vec.is_empty());
Source§

impl BitVec

Source

pub fn shrink_to_fit(&mut self) -> &mut Self

Shrinks the capacity of the vector as much as possible.

§Examples
use bitvek::BitVec;

let mut vec = BitVec::with_capacity(10);
vec.extend([true, false, true]);
assert!(vec.capacity() >= 16);
vec.shrink_to_fit();
assert!(vec.capacity() >= 8);
Source

pub fn shrink_to(&mut self, min_capacity: usize) -> &mut Self

Shrinks the capacity of the vector with a lower bound.

The capacity will remain at least as large as (self.len().max(min_capacity) / 8 + 1) * 8.

If the current capacity is less than the lower limit, this is a no-op.

§Examples
use bitvek::BitVec;

let mut vec = BitVec::with_capacity(20);
vec.extend([true, false, true]);
assert!(vec.capacity() >= 24);
vec.shrink_to(10);
assert!(vec.capacity() >= 16);
vec.shrink_to(0);
assert!(vec.capacity() >= 8);
Source

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

Returns the bit at the specified index, if in bounds.

§Examples
use bitvek::bitvec;

let vec = bitvec![true, false, true, false];
assert_eq!(vec.get(3), Some(false));
assert_eq!(vec.get(4), None);
Source

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

Returns the bit at the specified index, without performing any bounds checking.

§Safety

Calling this method with an out-of-bounds index is undefined behavior.

§Examples
use bitvek::bitvec;

let vec = bitvec![true, false, true, false];
unsafe { assert_eq!(vec.get_unchecked(3), false) };
Source

pub fn set(&mut self, index: usize, value: bool) -> Option<&mut Self>

Sets the bit at the specified index to the specified value, if in bounds.

§Examples
use bitvek::bitvec;

let expected = bitvec![true, true, true, true];

let mut vec = bitvec![true, false, true, false];
assert!(vec.set(1, true).is_some());
assert!(vec.set(3, true).is_some());
assert!(vec.set(4, true).is_none());
assert_eq!(vec, expected);
Source

pub unsafe fn set_unchecked(&mut self, index: usize, value: bool) -> &mut Self

Sets the bit at the specified index to the specified value, without performing any bounds checking.

§Safety

Calling this method with an out-of-bounds index is undefined behavior.

§Examples
use bitvek::bitvec;

let expected = bitvec![true, true, true, true];

let mut vec = bitvec![true, false, true, false];
unsafe {
    vec.set_unchecked(1, true);
    vec.set_unchecked(3, true);
}
assert_eq!(vec, expected);
Source

pub fn push(&mut self, value: bool) -> Option<&mut Self>

Appends a bit to the end of the vector, or returns None if the length of the vector reaches usize::MAX.

§Examples
use bitvek::bitvec;

let expected = bitvec![true, false, true, false, true];

let mut vec = bitvec![true, false, true, false];
assert!(vec.push(true).is_some());
assert_eq!(vec, expected);
Source

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

Removes the last bit from the vector and returns it, or None if the vector is empty.

§Examples
use bitvek::bitvec;

let expected = bitvec![true, false, true];

let mut vec = bitvec![true, false, true, false];
assert_eq!(vec.pop(), Some(false));
assert_eq!(vec, expected);

Trait Implementations§

Source§

impl Clone for BitVec

Source§

fn clone(&self) -> BitVec

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 BitVec

Source§

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

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

impl Default for BitVec

Source§

fn default() -> BitVec

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

impl Extend<bool> for BitVec

Source§

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

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 From<&[bool]> for BitVec

Source§

fn from(value: &[bool]) -> Self

Converts to this type from the input type.
Source§

impl From<&[u8]> for BitVec

Source§

fn from(value: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&BitVec> for Vec<bool>

Source§

fn from(value: &BitVec) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<[bool; N]> for BitVec

Source§

fn from(value: [bool; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<[u8; N]> for BitVec

Source§

fn from(value: [u8; N]) -> Self

Converts to this type from the input type.
Source§

impl From<BitVec> for Vec<bool>

Source§

fn from(value: BitVec) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<bool>> for BitVec

Source§

fn from(value: Vec<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for BitVec

Source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<bool> for BitVec

Source§

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

Creates a value from an iterator. Read more
Source§

impl Index<usize> for BitVec

Source§

type Output = bool

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

impl IntoIterator for BitVec

Source§

type Item = bool

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for BitVec

Source§

fn eq(&self, other: &Self) -> 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 Eq for BitVec

Auto Trait Implementations§

§

impl Freeze for BitVec

§

impl RefUnwindSafe for BitVec

§

impl Send for BitVec

§

impl Sync for BitVec

§

impl Unpin for BitVec

§

impl UnwindSafe for BitVec

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.