SparseRSVec

Struct SparseRSVec 

Source
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

Source

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.
Source

pub fn from_bitvec(input: &BitVec) -> Self

Creates a new SparseRSVec from a BitVec, by compressing the sparse 1-bits.

§Parameters
  • input: The input BitVec to compress.
Source

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 input BitVec to 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);
Source

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.

Source

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.

Source

pub fn get_unchecked(&self, i: u64) -> u64

Gets the bit at position i. Returns 1 if the bit is set, 0 if it is not set.

§Panics

If i is out of bounds the function might panic or produce incorrect results. Use get for a checked version.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn iter1(&self) -> impl Iterator<Item = u64> + '_

Returns an iterator over the 1-bits in the vector. The iterator yields the positions of the 1-bits in ascending order.

Source

pub fn len(&self) -> u64

Returns the length of the bit vector if it was uncompressed.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector is empty.

Source

pub fn heap_size(&self) -> usize

Returns the number of bytes used by the vector on the heap. Does not include allocated memory that isn’t used.

Trait Implementations§

Source§

impl Clone for SparseRSVec

Source§

fn clone(&self) -> SparseRSVec

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 Debug for SparseRSVec

Source§

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

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

impl<'de> Deserialize<'de> for SparseRSVec

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<'a> From<&'a BitVec> for SparseRSVec

Source§

fn from(input: &'a BitVec) -> Self

Converts to this type from the input type.
Source§

impl From<BitVec> for SparseRSVec

Source§

fn from(input: BitVec) -> Self

Converts to this type from the input type.
Source§

impl Serialize for SparseRSVec

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

Auto Trait Implementations§

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> 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, 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>,