[][src]Struct sparse_bin_mat::SparseBinVecBase

pub struct SparseBinVecBase<T> { /* fields omitted */ }

A sparse binary vector.

There are two main variants of a vector, the owned one, SparseBinVec, and the borrowed one, SparseBinSlice. Most of the time, you want to create a owned version. However, some iterators, such as those defined on SparseBinMat returns the borrowed version.

Implementations

impl SparseBinVecBase<Vec<usize, Global>>[src]

pub fn new(length: usize, positions: Vec<usize>) -> Self[src]

Creates a new vector with the given length and list of non trivial positions.

Example

let vector = SparseBinVec::new(5, vec![0, 2]);

assert_eq!(vector.len(), 5);
assert_eq!(vector.weight(), 2);

Panic

Panics if a position is greater or equal to the length.

This example panics
let vector = SparseBinVec::new(2, vec![1, 3]);

pub fn zeros(length: usize) -> Self[src]

Creates a vector fill with zeros of the given length.

Example

let vector = SparseBinVec::zeros(3);

assert_eq!(vector.len(), 3);
assert_eq!(vector.weight(), 0);

pub fn empty() -> Self[src]

Creates an empty vector.

This allocate minimally, so it is a good placeholder.

Example

let vector = SparseBinVec::empty();

assert_eq!(vector.len(), 0);
assert_eq!(vector.weight(), 0);

pub fn to_positions_vec(self) -> Vec<usize>[src]

Converts the sparse binary vector to a Vec of the non trivial positions.

Example

let vector = SparseBinVec::new(3, vec![0, 2]);

assert_eq!(vector.to_positions_vec(), vec![0, 2]);

impl<'a> SparseBinVecBase<&'a [usize]>[src]

pub fn new(length: usize, positions: &'a mut [usize]) -> Self[src]

Creates a new vector with the given length and list of non trivial positions.

This take a mutable reference to the positions in order to sort them. If you know the positions are sorted, you can instead use new_from_sorted.

Example

let mut positions = vec![2, 0];
let vector = SparseBinSlice::new(5, &mut positions);

assert_eq!(vector.len(), 5);
assert_eq!(vector.weight(), 2);
assert_eq!(vector.non_trivial_positions().collect::<Vec<_>>(), vec![0, 2]);

Panic

Panics if a position is greater or equal to the length.

This example panics
let vector = SparseBinSlice::new(2, &mut [1, 3]);

pub fn new_from_sorted(length: usize, positions: &'a [usize]) -> Self[src]

Creates a new vector with the given length and a sorted list of non trivial positions.

let mut positions = vec![0, 2];
let vector = SparseBinSlice::new_from_sorted(5, &positions);

assert_eq!(vector.len(), 5);
assert_eq!(vector.weight(), 2);

Panics

Panics if the list of positions is unsorted or if a position is greater or equal to the length.

This example panics
let mut positions = vec![2, 0];
let vector = SparseBinSlice::new_from_sorted(5, &positions);

impl<T: Deref<Target = [usize]>> SparseBinVecBase<T>[src]

pub fn len(&self) -> usize[src]

Returns the length (number of elements) of the vector.

pub fn weight(&self) -> usize[src]

Returns the number of elements with value 1 in the vector.

pub fn is_empty(&self) -> bool[src]

Returns true if the length of the vector is 0.

pub fn is_zero(&self) -> bool[src]

Returns true of all the element in the vector are 0.

pub fn get(&self, position: usize) -> Option<u8>[src]

Returns the value at the given position or None if the position is out of bound.

Example

let vector = SparseBinVec::new(3, vec![0, 2]);

assert_eq!(vector.get(0), Some(1));
assert_eq!(vector.get(1), Some(0));
assert_eq!(vector.get(2), Some(1));
assert_eq!(vector.get(3), None);

pub fn is_zero_at(&self, position: usize) -> Option<bool>[src]

Returns true if the value at the given position 0 or None if the position is out of bound.

Example

let vector = SparseBinVec::new(3, vec![0, 2]);

assert_eq!(vector.is_zero_at(0), Some(false));
assert_eq!(vector.is_zero_at(1), Some(true));
assert_eq!(vector.is_zero_at(2), Some(false));
assert_eq!(vector.is_zero_at(3), None);

pub fn is_one_at(&self, position: usize) -> Option<bool>[src]

Returns true if the value at the given position 1 or None if the position is out of bound.

Example

let vector = SparseBinVec::new(3, vec![0, 2]);

assert_eq!(vector.is_one_at(0), Some(true));
assert_eq!(vector.is_one_at(1), Some(false));
assert_eq!(vector.is_one_at(2), Some(true));
assert_eq!(vector.is_one_at(3), None);

pub fn non_trivial_positions<'a>(&'a self) -> impl Iterator<Item = usize> + 'a[src]

Returns an iterator over all positions where the value is 1.

Example

let vector = SparseBinVec::new(5, vec![0, 1, 3]);
let mut iter = vector.non_trivial_positions();

assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);

pub fn concat(&self, other: &Self) -> SparseBinVec[src]

Returns the concatenation of two vectors.

Example

let left_vector = SparseBinVec::new(3, vec![0, 1]);
let right_vector = SparseBinVec::new(4, vec![2, 3]);

let concatened = left_vector.concat(&right_vector);

let expected = SparseBinVec::new(7, vec![0, 1, 5, 6]);

assert_eq!(concatened, expected);

pub fn keep_only_positions(&self, positions: &[usize]) -> SparseBinVec[src]

Returns a new vector keeping only the given positions.

Positions are relabeled to the fit new number of positions.

Example

use sparse_bin_mat::SparseBinVec;
let vector = SparseBinVec::new(5, vec![0, 2, 4]);
let truncated = SparseBinVec::new(3, vec![0, 2]);

assert_eq!(vector.keep_only_positions(&[0, 1, 2]), truncated);
assert_eq!(vector.keep_only_positions(&[1, 2]).len(), 2);

Panic

Panics if some positions are out of bound.

pub fn without_positions(&self, positions: &[usize]) -> SparseBinVec[src]

Returns a truncated vector where the given positions are removed.

Positions are relabeled to fit the new number of positions.

Example

let vector = SparseBinVec::new(5, vec![0, 2, 4]);
let truncated = SparseBinVec::new(3, vec![0, 2]);

assert_eq!(vector.without_positions(&[3, 4]), truncated);
assert_eq!(vector.without_positions(&[1, 2]).len(), 3);

Panic

Panics if some positions are out of bound.

pub fn as_view(&self) -> SparseBinSlice<'_>[src]

Returns a view over the vector.

pub fn as_slice(&self) -> &[usize][src]

Returns a slice of the non trivial positions.

pub fn to_owned(self) -> SparseBinVec[src]

Returns an owned version of the vector.

pub fn dot_with<S: Deref<Target = [usize]>>(
    &self,
    other: &SparseBinVecBase<S>
) -> u8
[src]

Returns the dot product of two vectors.

Example

let first = SparseBinVec::new(4, vec![0, 1, 2]);
let second = SparseBinVec::new(4, vec![1, 2, 3]);
let third = SparseBinVec::new(4, vec![0, 3]);

assert_eq!(first.dot_with(&second), 0);
assert_eq!(first.dot_with(&third), 1);

Trait Implementations

impl<S, T, '_, '_> Add<&'_ SparseBinVecBase<S>> for &'_ SparseBinVecBase<T> where
    S: Deref<Target = [usize]>,
    T: Deref<Target = [usize]>, 
[src]

type Output = SparseBinVec

The resulting type after applying the + operator.

impl<T: Clone> Clone for SparseBinVecBase<T>[src]

impl<T: Debug> Debug for SparseBinVecBase<T>[src]

impl<T: Deref<Target = [usize]>> Display for SparseBinVecBase<T>[src]

impl<T: Eq> Eq for SparseBinVecBase<T>[src]

impl<T: Hash> Hash for SparseBinVecBase<T>[src]

impl<T: PartialEq> PartialEq<SparseBinVecBase<T>> for SparseBinVecBase<T>[src]

impl<T> StructuralEq for SparseBinVecBase<T>[src]

impl<T> StructuralPartialEq for SparseBinVecBase<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for SparseBinVecBase<T> where
    T: RefUnwindSafe

impl<T> Send for SparseBinVecBase<T> where
    T: Send

impl<T> Sync for SparseBinVecBase<T> where
    T: Sync

impl<T> Unpin for SparseBinVecBase<T> where
    T: Unpin

impl<T> UnwindSafe for SparseBinVecBase<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.