pub struct SparseVector { /* private fields */ }Expand description
An immutable Elias-Fano encoded bitvector supporting, rank, select, and related queries.
This structure should be used for sparse bitvectors, where frequency of set bits is low.
For dense bitvectors or when SelectZero is needed, BitVector is usually a better choice.
Because most queries require support structures for one of the components, the bitvector itself is immutable.
The maximum length of the vector is approximately usize::MAX bits.
Conversions between various BitVec types are possible using the From trait.
SparseVector supports partial multiset semantics.
A multiset bitvector is one that contains duplicate values in the integer array interpretation.
Queries that operate on present values work correctly with a multiset, while Rank::rank_zero and SelectZero do not.
Multiset vectors can be built with SparseBuilder::multiset and SparseVector::try_from_iter.
SparseVector implements the following simple_sds traits:
- Basic functionality:
BitVec - Queries and operations:
Rank,Select,SelectZero,PredSucc - Serialization:
Serialize
§Examples
use simple_sds_sbwt::ops::{BitVec, Rank, Select, SelectZero, PredSucc};
use simple_sds_sbwt::sparse_vector::{SparseVector, SparseBuilder};
use std::convert::TryFrom;
let mut builder = SparseBuilder::new(137, 4).unwrap();
builder.set(1); builder.set(33); builder.set(95); builder.set(123);
let sv = SparseVector::try_from(builder).unwrap();
// BitVec
assert_eq!(sv.len(), 137);
assert!(!sv.is_empty());
assert_eq!(sv.count_ones(), 4);
assert_eq!(sv.count_zeros(), 133);
assert!(sv.get(33));
assert!(!sv.get(34));
for (index, value) in sv.iter().enumerate() {
assert_eq!(value, sv.get(index));
}
// Rank
assert!(sv.supports_rank());
assert_eq!(sv.rank(33), 1);
assert_eq!(sv.rank(34), 2);
assert_eq!(sv.rank_zero(65), 63);
// Select
assert!(sv.supports_select());
assert_eq!(sv.select(1), Some(33));
let mut iter = sv.select_iter(2);
assert_eq!(iter.next(), Some((2, 95)));
assert_eq!(iter.next(), Some((3, 123)));
assert!(iter.next().is_none());
let v: Vec<(usize, usize)> = sv.one_iter().collect();
assert_eq!(v, vec![(0, 1), (1, 33), (2, 95), (3, 123)]);
// SelectZero
assert!(sv.supports_select_zero());
assert_eq!(sv.select_zero(35), Some(37));
let mut iter = sv.select_zero_iter(92);
assert_eq!(iter.next(), Some((92, 94)));
assert_eq!(iter.next(), Some((93, 96)));
// PredSucc
assert!(sv.supports_pred_succ());
assert!(sv.predecessor(0).next().is_none());
assert_eq!(sv.predecessor(1).next(), Some((0, 1)));
assert_eq!(sv.predecessor(2).next(), Some((0, 1)));
assert_eq!(sv.successor(122).next(), Some((3, 123)));
assert_eq!(sv.successor(123).next(), Some((3, 123)));
assert!(sv.successor(124).next().is_none());§Notes
SparseVectornever panics from I/O errors.- All
SparseVectorqueries are always enabled without additional support structures.
Implementations§
Source§impl SparseVector
impl SparseVector
Sourcepub fn copy_bit_vec<'a, T: BitVec<'a> + Select<'a>>(source: &'a T) -> Self
pub fn copy_bit_vec<'a, T: BitVec<'a> + Select<'a>>(source: &'a T) -> Self
Returns a copy of the source bitvector as SparseVector.
The copy is created by iterating over the set bits using Select::one_iter.
From implementations from other bitvector types should generally use this function.
§Examples
use simple_sds_sbwt::bit_vector::BitVector;
use simple_sds_sbwt::ops::BitVec;
use simple_sds_sbwt::sparse_vector::SparseVector;
use std::iter::FromIterator;
let source: Vec<bool> = vec![true, false, true, true, false, true, true, false];
let bv = BitVector::from_iter(source);
let sv = SparseVector::copy_bit_vec(&bv);
assert_eq!(sv.len(), bv.len());
assert_eq!(sv.count_ones(), bv.count_ones());
assert!(!sv.is_multiset());Sourcepub fn try_from_iter<T: Iterator<Item = usize> + DoubleEndedIterator + ExactSizeIterator>(
iter: T,
) -> Result<SparseVector, &'static str>
pub fn try_from_iter<T: Iterator<Item = usize> + DoubleEndedIterator + ExactSizeIterator>( iter: T, ) -> Result<SparseVector, &'static str>
Builds a vector from the values in the iterator using multiset semantics.
Returns an error message if the values are not sorted. Universe size is set to be barely large enough for the values.
§Examples
use simple_sds_sbwt::sparse_vector::SparseVector;
use simple_sds_sbwt::ops::{BitVec, Select};
let source: Vec<usize> = vec![3, 4, 4, 7, 11, 19];
let sv = SparseVector::try_from_iter(source.iter().cloned()).unwrap();
assert_eq!(sv.len(), 20);
assert_eq!(sv.count_ones(), source.len());
assert!(sv.is_multiset());
for (index, value) in sv.one_iter() {
assert_eq!(value, source[index]);
}Sourcepub fn is_multiset(&self) -> bool
pub fn is_multiset(&self) -> bool
Returns true if the vector is a multiset (contains duplicate values).
This method is somewhat expensive, as it iterates over the vector.
Trait Implementations§
Source§impl<'a> BitVec<'a> for SparseVector
impl<'a> BitVec<'a> for SparseVector
Source§fn len(&self) -> usize
fn len(&self) -> usize
Source§fn count_ones(&self) -> usize
fn count_ones(&self) -> usize
Source§fn count_zeros(&self) -> usize
fn count_zeros(&self) -> usize
Source§impl Clone for SparseVector
impl Clone for SparseVector
Source§fn clone(&self) -> SparseVector
fn clone(&self) -> SparseVector
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SparseVector
impl Debug for SparseVector
Source§impl From<BitVector> for SparseVector
impl From<BitVector> for SparseVector
Source§impl From<RLVector> for SparseVector
impl From<RLVector> for SparseVector
Source§impl From<SparseVector> for BitVector
impl From<SparseVector> for BitVector
Source§fn from(source: SparseVector) -> Self
fn from(source: SparseVector) -> Self
Source§impl From<SparseVector> for RLVector
impl From<SparseVector> for RLVector
Source§fn from(source: SparseVector) -> Self
fn from(source: SparseVector) -> Self
Source§impl PartialEq for SparseVector
impl PartialEq for SparseVector
Source§impl<'a> PredSucc<'a> for SparseVector
impl<'a> PredSucc<'a> for SparseVector
Source§type OneIter = OneIter<'a>
type OneIter = OneIter<'a>
Source§fn supports_pred_succ(&self) -> bool
fn supports_pred_succ(&self) -> bool
true if predecessor/successor support has been enabled.Source§fn enable_pred_succ(&mut self)
fn enable_pred_succ(&mut self)
Source§impl<'a> Rank<'a> for SparseVector
impl<'a> Rank<'a> for SparseVector
Source§impl<'a> Select<'a> for SparseVector
impl<'a> Select<'a> for SparseVector
Source§type OneIter = OneIter<'a>
type OneIter = OneIter<'a>
Source§fn supports_select(&self) -> bool
fn supports_select(&self) -> bool
true if select support has been enabled.Source§fn enable_select(&mut self)
fn enable_select(&mut self)
Source§impl<'a> SelectZero<'a> for SparseVector
impl<'a> SelectZero<'a> for SparseVector
Source§type ZeroIter = ZeroIter<'a>
type ZeroIter = ZeroIter<'a>
Source§fn supports_select_zero(&self) -> bool
fn supports_select_zero(&self) -> bool
true if select support has been enabled for the complement.