Struct sparse_bin_mat::SparseBinVecBase [−][src]
pub struct SparseBinVecBase<T> { /* fields omitted */ }Expand description
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
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);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);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]);Creates a new vector with the given length and list of non trivial positions.
Example
use sparse_bin_mat::error::InvalidPositions;
let vector = SparseBinVec::new(5, vec![0, 2]);
assert_eq!(vector.len(), 5);
assert_eq!(vector.weight(), 2);Creates a new vector with the given length and list of non trivial positions or returns as error if the positions are unsorted, greater or equal to length or contain duplicates.
Example
use sparse_bin_mat::error::InvalidPositions;
let vector = SparseBinVec::try_new(5, vec![0, 2]);
assert_eq!(vector, Ok(SparseBinVec::new(5, vec![0, 2])));
let vector = SparseBinVec::try_new(5, vec![2, 0]);
assert_eq!(vector, Err(InvalidPositions::Unsorted));
let vector = SparseBinVec::try_new(5, vec![0, 10]);
assert_eq!(vector, Err(InvalidPositions::OutOfBound));
let vector = SparseBinVec::try_new(5, vec![0, 0]);
assert_eq!(vector, Err(InvalidPositions::Duplicated));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!(vector.get(0).unwrap().is_one());
assert!(vector.get(1).unwrap().is_zero());
assert!(vector.get(2).unwrap().is_one());
assert!(vector.get(3).is_none());Returns true if the value at the given position is 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);Returns true if the value at the given position is 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) -> NonTrivialPositions<'a>ⓘNotable traits for NonTrivialPositions<'vec>impl<'vec> Iterator for NonTrivialPositions<'vec> type Item = usize;
pub fn non_trivial_positions<'a>(&'a self) -> NonTrivialPositions<'a>ⓘNotable traits for NonTrivialPositions<'vec>impl<'vec> Iterator for NonTrivialPositions<'vec> type Item = usize;
impl<'vec> Iterator for NonTrivialPositions<'vec> type Item = usize;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);Returns an iterator over all value in the vector.
Example
let vector = SparseBinVec::new(4, vec![0, 2]);
let mut iter = vector.iter_dense();
assert_eq!(iter.next(), Some(BinNum::one()));
assert_eq!(iter.next(), Some(BinNum::zero()));
assert_eq!(iter.next(), Some(BinNum::one()));
assert_eq!(iter.next(), Some(BinNum::zero()));
assert_eq!(iter.next(), None);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]
) -> Result<SparseBinVec, InvalidPositions>
pub fn keep_only_positions(
&self,
positions: &[usize]
) -> Result<SparseBinVec, InvalidPositions>
Returns a new vector keeping only the given positions or an error if the positions are unsorted, out of bound or contain deplicate.
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]), Ok(truncated));
assert_eq!(vector.keep_only_positions(&[1, 2]).map(|vec| vec.len()), Ok(2));pub fn without_positions(
&self,
positions: &[usize]
) -> Result<SparseBinVec, InvalidPositions>
pub fn without_positions(
&self,
positions: &[usize]
) -> Result<SparseBinVec, InvalidPositions>
Returns a truncated vector where the given positions are remove or an error if the positions are unsorted or out of bound.
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]), Ok(truncated));
assert_eq!(vector.without_positions(&[1, 2]).map(|vec| vec.len()), Ok(3));Returns a view over the vector.
Returns a slice of the non trivial positions.
Returns an owned version of the vector.
Returns the dot product of two vectors or an error if the vectors have different length.
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), Ok(0.into()));
assert_eq!(first.dot_with(&third), Ok((1.into())));pub fn bitwise_xor_with<S: Deref<Target = [usize]>>(
&self,
other: &SparseBinVecBase<S>
) -> Result<SparseBinVec, IncompatibleDimensions<usize, usize>>
pub fn bitwise_xor_with<S: Deref<Target = [usize]>>(
&self,
other: &SparseBinVecBase<S>
) -> Result<SparseBinVec, IncompatibleDimensions<usize, usize>>
Returns the bitwise xor of two vectors or an error if the vectors have different length.
Use the Add (+) operator if you want a version that panics instead or returning an error.
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.bitwise_xor_with(&second), Ok(third));Trait Implementations
type Output = SparseBinVec
type Output = SparseBinVec
The resulting type after applying the + operator.
Performs the + operation. Read more
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
type Output = SparseBinVec
type Output = SparseBinVec
The resulting type after applying the * operator.
Performs the * operation. Read more
This method tests for self and other values to be equal, and is used
by ==. Read more
This method tests for !=.
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
Mutably borrows from an owned value. Read more