Struct SparseBinVecBase

Source
pub struct SparseBinVecBase<T> { /* private fields */ }
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§

Source§

impl SparseBinVecBase<Vec<usize>>

Source

pub fn zeros(length: usize) -> Self

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

pub fn empty() -> Self

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

pub fn to_positions_vec(self) -> Vec<usize>

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

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

Source

pub fn new(length: usize, positions: T) -> Self

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

pub fn try_new(length: usize, positions: T) -> Result<Self, InvalidPositions>

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

pub fn len(&self) -> usize

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

Source

pub fn weight(&self) -> usize

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

Source

pub fn is_empty(&self) -> bool

Returns true if the length of the vector is 0.

Source

pub fn is_zero(&self) -> bool

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

Source

pub fn get(&self, position: usize) -> Option<BinNum>

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

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

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

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

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

pub fn non_trivial_positions<'a>(&'a self) -> NonTrivialPositions<'a>

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

pub fn iter_dense<'a>(&'a self) -> IterDense<'a, T>

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

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

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

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

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

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

Returns a view over the vector.

Source

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

Returns a slice of the non trivial positions.

Source

pub fn to_vec(self) -> SparseBinVec

Returns an owned version of the vector.

Source

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

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

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

pub fn as_json(&self) -> Result<String, Error>
where T: Serialize,

Returns a json string for the vector.

Trait Implementations§

Source§

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

Source§

type Output = SparseBinVecBase<Vec<usize>>

The resulting type after applying the + operator.
Source§

fn add(self, other: &SparseBinVecBase<S>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for SparseBinVecBase<T>

Source§

fn clone(&self) -> SparseBinVecBase<T>

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<T: Debug> Debug for SparseBinVecBase<T>

Source§

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

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

impl<'de, T> Deserialize<'de> for SparseBinVecBase<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

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

Source§

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

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

impl<T: Hash> Hash for SparseBinVecBase<T>

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<S, T> Mul<&SparseBinVecBase<S>> for &SparseBinVecBase<T>
where S: Deref<Target = [usize]>, T: Deref<Target = [usize]>,

Source§

type Output = BinNum

The resulting type after applying the * operator.
Source§

fn mul(self, other: &SparseBinVecBase<S>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Deref<Target = [usize]>> Mul<&SparseBinVecBase<T>> for &SparseBinMat

Source§

type Output = SparseBinVecBase<Vec<usize>>

The resulting type after applying the * operator.
Source§

fn mul(self, other: &SparseBinVecBase<T>) -> SparseBinVec

Performs the * operation. Read more
Source§

impl<T: PartialEq> PartialEq for SparseBinVecBase<T>

Source§

fn eq(&self, other: &SparseBinVecBase<T>) -> 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<T> Serialize for SparseBinVecBase<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq> Eq for SparseBinVecBase<T>

Source§

impl<T> StructuralPartialEq for SparseBinVecBase<T>

Auto Trait Implementations§

§

impl<T> Freeze for SparseBinVecBase<T>
where T: Freeze,

§

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§

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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,