pub struct BinVector { /* private fields */ }
Expand description
Wrapper around vob::Vob
Implementations§
Source§impl BinVector
impl BinVector
pub fn from(vec: Vob) -> Self
Sourcepub fn from_elem(len: usize, elem: bool) -> Self
pub fn from_elem(len: usize, elem: bool) -> Self
Create with a certain length and all the same element
Sourcepub fn from_bools(bools: &[bool]) -> BinVector
pub fn from_bools(bools: &[bool]) -> BinVector
Set it up from bools
Sourcepub fn from_function(len: usize, f: fn(usize) -> bool) -> BinVector
pub fn from_function(len: usize, f: fn(usize) -> bool) -> BinVector
Construct the BinVector from the result of a function
§Example
let v = BinVector::from_function(4, |i| i % 2 == 0);
assert_eq!(v.get(0), Some(true));
assert_eq!(v.get(1), Some(false));
Sourcepub fn with_capacity(len: usize) -> Self
pub fn with_capacity(len: usize) -> Self
initialise with a set capacity
Sourcepub fn from_bytes(bytes: &[u8]) -> BinVector
pub fn from_bytes(bytes: &[u8]) -> BinVector
Create a new BinVector from an &[u8]
.
Sourcepub fn count_ones(&self) -> u32
pub fn count_ones(&self) -> u32
Get the hamming weight
Sourcepub fn extend_from_binvec(&mut self, other: &BinVector)
pub fn extend_from_binvec(&mut self, other: &BinVector)
Extend from a binary vector
Sourcepub fn as_column_matrix(&self) -> BinMatrix
pub fn as_column_matrix(&self) -> BinMatrix
Obtain this as a column matrix
Methods from Deref<Target = Vob>§
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of bits the Vob can hold without reallocating.
§Examples
use vob::Vob;
assert_eq!(Vob::new().capacity(), 0);
assert!(Vob::with_capacity(1).capacity() >= 1);
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more bits to be inserted in the Vob. The Vob
may reserve more space to avoid frequent reallocations. After calling reserve
, capacity
will be greater than or equal to self.len() + additional
. Does nothing if capacity is
already sufficient.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.reserve(1);
assert!(v.capacity() >= 1);
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the vector as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shortens the Vob, keeping the first len
elements and dropping the rest.
If len is greater than the vector’s current length, this has no effect.
The drain method can emulate truncate, but causes the excess elements to be returned instead of dropped.
Note that this method has no effect on the allocated capacity of the vector.
§Examples
use vob::vob;
let mut v = vob![true, false, true];
v.truncate(2);
assert_eq!(v, vob![true, false]);
Sourcepub fn push(&mut self, value: bool)
pub fn push(&mut self, value: bool)
Appends a bool to the back of the Vob.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(true);
v.push(false);
assert_eq!(v.get(0), Some(true));
assert_eq!(v.get(1), Some(false));
Sourcepub fn pop(&mut self) -> Option<bool>
pub fn pop(&mut self) -> Option<bool>
Removes the last element from the Vob and returns it, or None
if it is empty.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(true);
assert_eq!(v.pop(), Some(true));
assert_eq!(v.pop(), None);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the Vob has a length of 0.
§Examples
use vob::Vob;
assert_eq!(Vob::from_elem(2, true).is_empty(), false);
assert_eq!(Vob::new().is_empty(), true);
Sourcepub fn split_off(&mut self, at: usize) -> Vob<T>
pub fn split_off(&mut self, at: usize) -> Vob<T>
Splits the collection into two at the given index.
Returns a newly allocated Self. self contains elements [0, at), and the returned Self contains elements [at, len).
Note that the capacity of self does not change.
§Examples
use vob::Vob;
let mut v1 = Vob::new();
v1.push(true);
v1.push(false);
let v2 = v1.split_off(1);
assert_eq!(v1, Vob::from_elem(1, true));
assert_eq!(v2, Vob::from_elem(1, false));
Sourcepub fn get(&self, index: usize) -> Option<bool>
pub fn get(&self, index: usize) -> Option<bool>
Returns the value of the element at position index
or None
if out of bounds.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(false);
assert_eq!(v.get(0), Some(false));
assert_eq!(v.get(1), None);
Sourcepub fn set(&mut self, index: usize, value: bool) -> bool
pub fn set(&mut self, index: usize, value: bool) -> bool
Sets the value of the element at position index
. Returns true
if this led to a change
in the underlying storage or false
otherwise.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(false);
v.set(0, true);
assert_eq!(v.get(0), Some(true));
assert_eq!(v.set(0, false), true);
assert_eq!(v.set(0, false), false);
§Panics
If index
is out of bounds.
Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns an iterator over the slice.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(false);
v.push(true);
let mut iterator = v.iter();
assert_eq!(iterator.next(), Some(false));
assert_eq!(iterator.next(), Some(true));
assert_eq!(iterator.next(), None);
Sourcepub fn iter_set_bits<R>(&self, range: R) -> IterSetBits<'_, T>where
R: RangeBounds<usize>,
pub fn iter_set_bits<R>(&self, range: R) -> IterSetBits<'_, T>where
R: RangeBounds<usize>,
Returns an iterator which efficiently produces the index of each set bit in the specified range. Assuming appropriate support from your CPU, this is much more efficient than checking each bit individually.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(false);
v.push(true);
let mut iterator = v.iter_set_bits(..);
assert_eq!(iterator.next(), Some(1));
assert_eq!(iterator.next(), None);
Sourcepub fn iter_unset_bits<R>(&self, range: R) -> IterUnsetBits<'_, T>where
R: RangeBounds<usize>,
pub fn iter_unset_bits<R>(&self, range: R) -> IterUnsetBits<'_, T>where
R: RangeBounds<usize>,
Returns an iterator which efficiently produces the index of each unset bit in the specified range. Assuming appropriate support from your CPU, this is much more efficient than checking each bit individually.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(false);
v.push(true);
let mut iterator = v.iter_unset_bits(..);
assert_eq!(iterator.next(), Some(0));
assert_eq!(iterator.next(), None);
Sourcepub fn iter_storage(&self) -> StorageIter<'_, T>
pub fn iter_storage(&self) -> StorageIter<'_, T>
Return an iterator over the underlying storage blocks. The last block is guaranteed to have
“unused” bits (i.e. those past self.len()
) set to 0.
§Examples
use vob::Vob;
let v1 = Vob::from_elem(10, true);
assert_eq!(v1.iter_storage().next(), Some((1 << 10) - 1));
let v2 = Vob::from_elem(129, true);
assert_eq!(v2.iter_storage().last(), Some(1));
Sourcepub fn resize(&mut self, new_len: usize, value: bool)
pub fn resize(&mut self, new_len: usize, value: bool)
Resizes the Vob in-place so that len
is equal to new_len
.
If new_len
is greater than len
, the Vob is extended by the difference, with each
additional slot filled with value
. If new_len
is less than len
, the vob is simply
truncated.
§Examples
use vob::Vob;
let mut v = Vob::new();
v.push(false);
v.resize(129, true);
assert_eq!(v.len(), 129);
assert_eq!(v.get(0), Some(false));
assert_eq!(v.get(128), Some(true));
Sourcepub fn extend_from_slice(&mut self, other: &[bool])
pub fn extend_from_slice(&mut self, other: &[bool])
Appends all elements in a slice to the Vob.
Iterates over the slice other
and appends elements in order.
Note that this function is same as extend except that it is specialized to work with slices instead. If and when Rust gets specialization this function will likely be deprecated (but still available).
§Examples
use vob::vob;
let mut v = vob![true];
v.extend_from_slice(&vec![false, true]);
assert_eq!(v, vob![true, false, true]);
Sourcepub fn extend_from_vob(&mut self, other: &Vob<T>)
pub fn extend_from_vob(&mut self, other: &Vob<T>)
Append all elements from a second Vob to this Vob.
Use this instead of extend()
when extending with a Vob, because this method is a lot
faster.
§Examples
use vob::vob;
let mut v1 = vob![true];
let v2 = vob![false, false];
v1.extend_from_vob(&v2);
assert_eq!(v1, vob![true, false, false]);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the Vob, removing all values.
Note that this method has no effect on the allocated capacity of the Vob.
Sourcepub fn set_all(&mut self, value: bool)
pub fn set_all(&mut self, value: bool)
Sets all bits in the Vob to value
. Notice that this does not change the number of bits
stored in the Vob.
§Examples
use vob::vob;
let mut v = vob![true, false, true];
v.set_all(false);
assert_eq!(v, vob![false, false, false]);
Sourcepub fn negate(&mut self)
pub fn negate(&mut self)
Negates all bits in the Vob.
§Examples
use vob::vob;
let mut v = vob![true, false];
v.negate();
assert_eq!(v, vob![false, true]);
Sourcepub fn and(&mut self, other: &Vob<T>) -> bool
pub fn and(&mut self, other: &Vob<T>) -> bool
For each bit in this Vob, and
it with the corresponding bit in other
, returning true
if this led to any changes or false
otherwise. The two Vobs must have the same number of
bits.
§Panics
If the two Vobs are of different length.
§Examples
use vob::vob;
let mut v1 = vob![true, false, false];
let v2 = vob![true, true, false];
assert_eq!(v1.and(&v2), false);
assert_eq!(v1, vob![true, false, false]);
Sourcepub fn or(&mut self, other: &Vob<T>) -> bool
pub fn or(&mut self, other: &Vob<T>) -> bool
For each bit in this Vob, or
it with the corresponding bit in other
, returning true
if this led to any changes or false
otherwise. The two Vobs must have the same number of
bits.
§Panics
If the two Vobs are of different length.
§Examples
use vob::vob;
let mut v1 = vob![true, false, false];
let v2 = vob![false, true, false];
assert_eq!(v1.or(&v2), true);
assert_eq!(v1, vob![true, true, false]);
Sourcepub fn xor(&mut self, other: &Vob<T>) -> bool
pub fn xor(&mut self, other: &Vob<T>) -> bool
For each bit in this Vob, xor
it with the corresponding bit in other
, returning true
if this led to any changes or false
otherwise. The two Vobs must have the same number of
bits.
§Panics
If the two Vobs are of different length.
§Examples
use vob::vob;
let mut v1 = vob![true, false, true];
let v2 = vob![false, true, true];
assert_eq!(v1.xor(&v2), true);
assert_eq!(v1, vob![true, true, false]);
Sourcepub fn mask_last_block(&mut self)
pub fn mask_last_block(&mut self)
We guarantee that the last storage block has no bits set past the “last” bit: this function clears any such bits.
This otherwise private function is exposed when unsafe_internals
is enabled, to help
callers maintain the internal assumptions.
Sourcepub unsafe fn get_storage_mut(&mut self) -> &mut Vec<T>
pub unsafe fn get_storage_mut(&mut self) -> &mut Vec<T>
Get a mutable reference to the underlying data structure
This is marked as unsafe as it allows to invalidate the assumptions made by this module. It is not in itself unsafe.
§Assumptions:
Vob
stores the bits in a little-endian order.- The length can’t change, or must be updated using
Vob::set_len()
. - Any bits past
self.len()
must be set to 0 in the last block.Vob::mask_last_block()
can help you with that. storage.len()
can not be larger thanceil(self.len() / (8 * size_of::<T>))
§Examples
use vob::vob;
let mut v1 = vob![true, false, true];
let storage = unsafe { v1.get_storage_mut() };
assert_eq!(storage[0], 0b101);
Sourcepub fn get_storage(&self) -> &[T]
pub fn get_storage(&self) -> &[T]
Get a reference to the underlying data structure
Vob
stores the bits in little-endian order. Any bits beyond self.len()
in the last
block are 0
.
use vob::vob;
let v1 = vob![true, true, false];
let storage = unsafe { v1.get_storage() };
assert_eq!(storage[0], 0b011);
Sourcepub unsafe fn set_len(&mut self, len: usize)
pub unsafe fn set_len(&mut self, len: usize)
Set the length of the Vob
.
This will explicitly set the length of the Vob, without actually modifying the underlying data structure or doing any checks.
If you want to shorten your Vob
, you’re probably looking
for truncate
.
See Vob::get_storage_mut()
for the other requirements.
§Examples
use vob::Vob;
let mut v1 = Vob::<u8>::new_with_storage_type(9);
v1.push(true);
v1.push(false);
{
let mut storage = unsafe { v1.get_storage_mut() };
// push another block
storage.push(0b1);
}
// we know that 9 bits are initialised now as the previously-unused remainder of block 0 must be zero.
unsafe { v1.set_len(9); }
assert_eq!(v1[0], true);
assert_eq!(v1[1], false);
assert_eq!(v1[2], false);
assert_eq!(v1[8], true);
Trait Implementations§
Source§impl<'a> AddAssign<&'a BinVector> for BinVector
impl<'a> AddAssign<&'a BinVector> for BinVector
Source§fn add_assign(&mut self, other: &BinVector)
fn add_assign(&mut self, other: &BinVector)
+=
operation. Read moreSource§impl AddAssign for BinVector
impl AddAssign for BinVector
Source§fn add_assign(&mut self, other: BinVector)
fn add_assign(&mut self, other: BinVector)
+=
operation. Read more