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 moreSource§impl Debug for SparseRSVec
impl Debug for SparseRSVec
Source§impl<'de> Deserialize<'de> for SparseRSVec
impl<'de> Deserialize<'de> for SparseRSVec
Source§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>,
Source§impl FlatType for SparseRSVec
impl FlatType for SparseRSVec
Source§impl<'a> From<&'a BitVec> for SparseRSVec
impl<'a> From<&'a BitVec> for SparseRSVec
Source§impl From<BitVec> for SparseRSVec
impl From<BitVec> for SparseRSVec
Source§impl MemDbgImpl for SparseRSVec
impl MemDbgImpl for SparseRSVec
fn _mem_dbg_rec_on( &self, _memdbg_writer: &mut impl Write, _memdbg_total_size: usize, _memdbg_max_depth: usize, _memdbg_prefix: &mut String, _memdbg_is_last: bool, _memdbg_flags: DbgFlags, _memdbg_refs: &mut HashSet<usize>, ) -> Result
fn _mem_dbg_depth_on( &self, writer: &mut impl Write, total_size: usize, max_depth: usize, prefix: &mut String, field_name: Option<&str>, is_last: bool, padded_size: usize, flags: DbgFlags, dbg_refs: &mut HashSet<usize>, ) -> Result<(), Error>
Source§fn _mem_dbg_depth_on_impl(
&self,
writer: &mut impl Write,
total_size: usize,
max_depth: usize,
prefix: &mut String,
field_name: Option<&str>,
is_last: bool,
padded_size: usize,
flags: DbgFlags,
dbg_refs: &mut HashSet<usize>,
ref_display: RefDisplay,
) -> Result<(), Error>
fn _mem_dbg_depth_on_impl( &self, writer: &mut impl Write, total_size: usize, max_depth: usize, prefix: &mut String, field_name: Option<&str>, is_last: bool, padded_size: usize, flags: DbgFlags, dbg_refs: &mut HashSet<usize>, ref_display: RefDisplay, ) -> Result<(), Error>
Auto Trait Implementations§
impl Freeze for SparseRSVec
impl RefUnwindSafe for SparseRSVec
impl Send for SparseRSVec
impl Sync for SparseRSVec
impl Unpin for SparseRSVec
impl UnsafeUnpin for SparseRSVec
impl UnwindSafe for SparseRSVec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> MemDbg for Twhere
T: MemDbgImpl,
impl<T> MemDbg for Twhere
T: MemDbgImpl,
Source§fn mem_dbg(&self, flags: DbgFlags) -> Result<(), Error>
fn mem_dbg(&self, flags: DbgFlags) -> Result<(), Error>
std only.Source§fn mem_dbg_on(
&self,
writer: &mut impl Write,
flags: DbgFlags,
) -> Result<(), Error>
fn mem_dbg_on( &self, writer: &mut impl Write, flags: DbgFlags, ) -> Result<(), Error>
core::fmt::Write debug info about the structure memory
usage, expanding all levels of nested structures.Source§fn mem_dbg_depth(&self, max_depth: usize, flags: DbgFlags) -> Result<(), Error>
fn mem_dbg_depth(&self, max_depth: usize, flags: DbgFlags) -> Result<(), Error>
std only.mem_dbg, but expanding only up to max_depth
levels of nested structures.Source§fn mem_dbg_depth_on(
&self,
writer: &mut impl Write,
max_depth: usize,
flags: DbgFlags,
) -> Result<(), Error>
fn mem_dbg_depth_on( &self, writer: &mut impl Write, max_depth: usize, flags: DbgFlags, ) -> Result<(), Error>
core::fmt::Write debug info about the structure memory
usage as mem_dbg_on, but expanding only up to
max_depth levels of nested structures.