BinVector

Struct BinVector 

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

Wrapper around vob::Vob

Implementations§

Source§

impl BinVector

Source

pub fn new() -> Self

Create a new BinVector

Source

pub fn from(vec: Vob) -> Self

Source

pub fn from_elem(len: usize, elem: bool) -> Self

Create with a certain length and all the same element

Source

pub fn from_bools(bools: &[bool]) -> BinVector

Set it up from bools

Source

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));
Source

pub fn random(len: usize) -> BinVector

Randomized

Source

pub fn with_capacity(len: usize) -> Self

initialise with a set capacity

Source

pub fn from_bytes(bytes: &[u8]) -> BinVector

Create a new BinVector from an &[u8].

Source

pub fn count_ones(&self) -> u32

Get the hamming weight

Source

pub fn extend_from_binvec(&mut self, other: &BinVector)

Extend from a binary vector

Source

pub fn into_vob(self) -> Vob

Obtain the inner Vob

Source

pub fn as_matrix(&self) -> BinMatrix

Obtain this as a row matrix

Source

pub fn as_column_matrix(&self) -> BinMatrix

Obtain this as a column matrix

Source

pub fn as_u32(&self) -> u32

Get an u32 in the order as it’s stored

Source

pub fn as_u64(&self) -> u64

Get an u64 in the order as it’s stored

Methods from Deref<Target = Vob>§

Source

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);
Source

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);
Source

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.

Source

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]);
Source

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));
Source

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);
Source

pub fn len(&self) -> usize

Returns the number of elements in the Vob.

Source

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);
Source

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));
Source

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);
Source

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.

Source

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);
Source

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);
Source

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);
Source

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));
Source

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));
Source

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]);
Source

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]);
Source

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.

Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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.

Source

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 than ceil(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);
Source

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);
Source

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> Add for &'a BinVector

Source§

type Output = BinVector

The resulting type after applying the + operator.
Source§

fn add(self, other: &BinVector) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for BinVector

Source§

type Output = BinVector

The resulting type after applying the + operator.
Source§

fn add(self, other: BinVector) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> AddAssign<&'a BinVector> for BinVector

Source§

fn add_assign(&mut self, other: &BinVector)

Performs the += operation. Read more
Source§

impl AddAssign for BinVector

Source§

fn add_assign(&mut self, other: BinVector)

Performs the += operation. Read more
Source§

impl Clone for BinVector

Source§

fn clone(&self) -> BinVector

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 Debug for BinVector

Source§

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

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

impl Default for BinVector

Source§

fn default() -> BinVector

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

impl Deref for BinVector

Source§

type Target = Vob

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for BinVector

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl From<Vob> for BinVector

Source§

fn from(v: Vob) -> BinVector

Converts to this type from the input type.
Source§

impl Hash for BinVector

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<'a> Mul<&'a BinMatrix> for &'a BinVector

Source§

fn mul(self, other: &BinMatrix) -> Self::Output

computes v^T * A

Source§

type Output = BinVector

The resulting type after applying the * operator.
Source§

impl<'a> Mul<&'a BinVector> for &'a BinMatrix

Source§

fn mul(self, other: &BinVector) -> Self::Output

Computes (A * v^T)

Source§

type Output = BinVector

The resulting type after applying the * operator.
Source§

impl Mul<BinMatrix> for BinVector

Source§

fn mul(self, other: BinMatrix) -> Self::Output

computes v^T * A

Source§

type Output = BinVector

The resulting type after applying the * operator.
Source§

impl Mul<BinVector> for BinMatrix

Source§

fn mul(self, other: BinVector) -> Self::Output

Computes (A * v^T)

Source§

type Output = BinVector

The resulting type after applying the * operator.
Source§

impl<'a> Mul for &'a BinVector

Source§

type Output = bool

The resulting type after applying the * operator.
Source§

fn mul(self, other: &BinVector) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for BinVector

Source§

fn mul(self, other: BinVector) -> Self::Output

Compute the inner product between two vectors

Source§

type Output = bool

The resulting type after applying the * operator.
Source§

impl PartialEq for BinVector

Source§

fn eq(&self, other: &BinVector) -> 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 BinVector

Source§

impl StructuralPartialEq for BinVector

Auto Trait Implementations§

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V