pub struct SparseRSVec { /* private fields */ }Expand description
A succinct representation of a sparse vector with rank and select support.
It is a thin wrapper around an EliasFanoVec that compresses the indices of 1-bits.
Therefore, no select0 function is provided.
However, the constructor from_bitvec_inverted can be used to cheaply invert the input BitVec,
reversing the roles of 1-bits and 0-bits.
§Examples
use vers_vecs::SparseRSVec;
let sparse = SparseRSVec::new(&[1, 3, 5, 7, 9], 12);
assert_eq!(sparse.get(5), Some(1));
assert_eq!(sparse.get(11), Some(0));
assert_eq!(sparse.get(12), None);
assert_eq!(sparse.rank1(5), 2);
assert_eq!(sparse.select1(2), 5);It cn also be constructed from a BitVec directly:
use vers_vecs::SparseRSVec;
use vers_vecs::BitVec;
let mut bv = BitVec::from_zeros(12);
bv.flip_bit(6);
bv.flip_bit(7);
let sparse = SparseRSVec::from_bitvec(&bv);
assert_eq!(sparse.rank1(5), 0);
assert_eq!(sparse.select1(0), 6);Implementations§
Source§impl SparseRSVec
impl SparseRSVec
Sourcepub fn new(input: &[u64], len: u64) -> Self
pub fn new(input: &[u64], len: u64) -> Self
Creates a new SparseRSVec from a sequence of set bits represented as indices.
The input must be sorted in ascending order and free of duplicates.
The length of the vector must be passed as well, as it cannot be inferred from the input, if the last bit in the vector is not set.
§Parameters
input: The positions of set bits, or unset bits if the sparse vector should compress zeros.len: The length of the vector, which is needed if the last bit is not in the input slice.
Sourcepub fn from_bitvec(input: &BitVec) -> Self
pub fn from_bitvec(input: &BitVec) -> Self
Creates a new SparseRSVec from a BitVec, by compressing the sparse 1-bits.
§Parameters
input: The inputBitVecto compress.
Sourcepub fn from_bitvec_inverted(input: &BitVec) -> Self
pub fn from_bitvec_inverted(input: &BitVec) -> Self
Creates a new SparseRSVec from a BitVec.
However, before compressing the 1-bits, the input is inverted.
This means that the sparse vector will compress the 0-bits instead of the 1-bits,
and the rank1 and select1 functions will return the number of 0-bits and the position of 0-bits.
This is a convenience function to allow for easy creation of sparse vectors that compress
zeros, despite the lack of a select0 function.
However, do note that get will return the inverted value of the bit at position i from
the original BitVec.
§Parameters
input: The inputBitVecto compress.
§Example
use vers_vecs::SparseRSVec;
use vers_vecs::BitVec;
let mut bv = BitVec::from_ones(12);
// set 6 and 7 to 0
bv.flip_bit(6);
bv.flip_bit(7);
let sparse = SparseRSVec::from_bitvec_inverted(&bv);
// now select1 gives the position of 0-bits
assert_eq!(sparse.select1(1), 7);Sourcepub fn is_set_unchecked(&self, i: u64) -> bool
pub fn is_set_unchecked(&self, i: u64) -> bool
Returns true if the bit at position i is set.
If i is out of bounds the function produces incorrect results.
Use is_set for a checked version.
Sourcepub fn is_set(&self, i: u64) -> Option<bool>
pub fn is_set(&self, i: u64) -> Option<bool>
Returns true if the bit at position i is set.
Returns None if i is out of bounds.
Sourcepub fn get_unchecked(&self, i: u64) -> u64
pub fn get_unchecked(&self, i: u64) -> u64
Sourcepub fn get(&self, i: u64) -> Option<u64>
pub fn get(&self, i: u64) -> Option<u64>
Gets the bit at position i.
Returns Some(1) if the bit is set, Some(0) if it is not set, and None if i is out of bounds.
Sourcepub fn select1(&self, i: usize) -> u64
pub fn select1(&self, i: usize) -> u64
Return the position of the 1-bit with the given rank.
The following holds for all pos with 1-bits:
select1(rank1(pos)) == pos
If the rank is larger than the number of sparse bits in the vector, the vector length is returned.
Sourcepub fn rank1(&self, i: u64) -> u64
pub fn rank1(&self, i: u64) -> u64
Returns the number of 1-bits in the vector up to position i.
If i is out of bounds, the number of 1-bits in the vector is returned.
Sourcepub fn rank0(&self, i: u64) -> u64
pub fn rank0(&self, i: u64) -> u64
Returns the number of 0-bits in the vector up to position i.
If i is out of bounds, the number of 0-bits in the vector is returned.
Trait Implementations§
Source§impl Clone for SparseRSVec
impl Clone for SparseRSVec
Source§fn clone(&self) -> SparseRSVec
fn clone(&self) -> SparseRSVec
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more