RLVector

Struct RLVector 

Source
pub struct RLVector { /* private fields */ }
Expand description

An immutable run-length encoded bitvector supporting rank, select, and related queries.

This type should be used when the bitvector contains long runs of both set and unset bits. Other bitvector types are more appropriate for dense (no long runs) and sparse (long runs of unset bits) bitvectors. The bitvector is immutable, though it would be easy to implement a mutable version by storing the blocks in a B+-tree rather than a vector. The maximum length of the vector is approximately usize::MAX bits.

Conversions between various BitVec types are possible using the From trait.

RLVector implements the following simple_sds traits:

§Examples

use simple_sds_sbwt::ops::{BitVec, Rank, Select, SelectZero, PredSucc};
use simple_sds_sbwt::rl_vector::{RLVector, RLBuilder};

let mut builder = RLBuilder::new();
builder.try_set(18, 22);
builder.try_set(95, 15);
builder.try_set(110, 10);
builder.try_set(140, 12);
builder.set_len(200);
let rv = RLVector::from(builder);

// BitVec
assert_eq!(rv.len(), 200);
assert!(!rv.is_empty());
assert_eq!(rv.count_ones(), 59);
assert_eq!(rv.count_zeros(), 141);
assert!(rv.get(119));
assert!(!rv.get(120));
for (index, value) in rv.iter().enumerate() {
    assert_eq!(value, rv.get(index));
}

// Rank
assert!(rv.supports_rank());
assert_eq!(rv.rank(100), 27);
assert_eq!(rv.rank(130), 47);
assert_eq!(rv.rank_zero(60), 38);

// Select
assert!(rv.supports_select());
assert_eq!(rv.select(24), Some(97));
let mut iter = rv.select_iter(46);
assert_eq!(iter.next(), Some((46, 119)));
assert_eq!(iter.next(), Some((47, 140)));
let v: Vec<(usize, usize)> = rv.one_iter().take(4).collect();
assert_eq!(v, vec![(0, 18), (1, 19), (2, 20), (3, 21)]);

// SelectZero
assert!(rv.supports_select_zero());
assert_eq!(rv.select_zero(130), Some(189));
let mut iter = rv.select_zero_iter(72);
assert_eq!(iter.next(), Some((72, 94)));
assert_eq!(iter.next(), Some((73, 120)));
let v: Vec<(usize, usize)> = rv.zero_iter().take(4).collect();
assert_eq!(v, vec![(0, 0), (1, 1), (2, 2), (3, 3)]);

// PredSucc
assert!(rv.supports_pred_succ());
assert!(rv.predecessor(17).next().is_none());
assert_eq!(rv.predecessor(18).next(), Some((0, 18)));
assert_eq!(rv.predecessor(40).next(), Some((21, 39)));
assert_eq!(rv.successor(139).next(), Some((47, 140)));
assert_eq!(rv.successor(140).next(), Some((47, 140)));
assert!(rv.successor(152).next().is_none());

§Notes

  • RLVector never panics from I/O errors.
  • All RLVector queries are always enabled without additional support structures.

Implementations§

Source§

impl RLVector

Source

pub const CODE_SIZE: usize = 4usize

Number of bits in a code unit.

Source

pub const BLOCK_SIZE: usize = 64usize

Number of code units in a block.

Source

pub fn copy_bit_vec<'a, T: BitVec<'a> + Select<'a>>(source: &'a T) -> Self

Returns a copy of the source bitvector as RLVector.

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::rl_vector::RLVector;
use std::iter::FromIterator;

let source: Vec<bool> = vec![true, false, true, true, false, true, true, false];
let bv = BitVector::from_iter(source);
let rv = RLVector::copy_bit_vec(&bv);
assert_eq!(rv.len(), bv.len());
assert_eq!(rv.count_ones(), bv.count_ones());
Source

pub fn run_iter(&self) -> RunIter<'_>

Returns an iterator over the runs of set bits in the bitvector.

See RunIter for an example.

Trait Implementations§

Source§

impl<'a> BitVec<'a> for RLVector

Source§

type Iter = Iter<'a>

Iterator type over the bit array.
Source§

fn len(&self) -> usize

Returns the length of the bit array or the universe size of the integer array.
Source§

fn count_ones(&self) -> usize

Returns the length of the integer array or the number of ones in the bit array. Read more
Source§

fn get(&self, index: usize) -> bool

Reads a bit from the bit array. Read more
Source§

fn iter(&'a self) -> Self::Iter

Returns an iterator over the bit array. Read more
Source§

fn is_empty(&self) -> bool

Returns true if the bitvector is empty.
Source§

fn count_zeros(&self) -> usize

Returns the number of zeros in the bit array.
Source§

impl Clone for RLVector

Source§

fn clone(&self) -> RLVector

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 RLVector

Source§

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

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

impl From<BitVector> for RLVector

Source§

fn from(source: BitVector) -> Self

Converts to this type from the input type.
Source§

impl From<RLBuilder> for RLVector

Source§

fn from(builder: RLBuilder) -> Self

Converts to this type from the input type.
Source§

impl From<RLVector> for BitVector

Source§

fn from(source: RLVector) -> Self

Converts to this type from the input type.
Source§

impl From<RLVector> for SparseVector

Source§

fn from(source: RLVector) -> Self

Converts to this type from the input type.
Source§

impl From<SparseVector> for RLVector

Source§

fn from(source: SparseVector) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for RLVector

Source§

fn eq(&self, other: &RLVector) -> 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<'a> PredSucc<'a> for RLVector

Source§

type OneIter = OneIter<'a>

Iterator type over (index, value) pairs in the integer array. Read more
Source§

fn supports_pred_succ(&self) -> bool

Returns true if predecessor/successor support has been enabled.
Source§

fn enable_pred_succ(&mut self)

Enables predecessor/successor support for the vector. Read more
Source§

fn predecessor(&'a self, value: usize) -> Self::OneIter

Returns an iterator at the largest v <= value in the integer array. Read more
Source§

fn successor(&'a self, value: usize) -> Self::OneIter

Returns an iterator at the smallest v >= value in the integer array. Read more
Source§

impl<'a> Rank<'a> for RLVector

Source§

fn supports_rank(&self) -> bool

Returns true if rank support has been enabled.
Source§

fn enable_rank(&mut self)

Enables rank support for the vector. Read more
Source§

fn rank(&self, index: usize) -> usize

Returns the number of indexes i < index in the bit array such that self.get(i) == true. Read more
Source§

fn rank_zero(&self, index: usize) -> usize

Returns the number of indexes i < index in the bit array such that self.get(i) == false. Read more
Source§

impl<'a> Select<'a> for RLVector

Source§

type OneIter = OneIter<'a>

Iterator type over (index, value) pairs in the integer array. Read more
Source§

fn supports_select(&self) -> bool

Returns true if select support has been enabled.
Source§

fn enable_select(&mut self)

Enables select support for the vector. Read more
Source§

fn one_iter(&'a self) -> Self::OneIter

Returns an iterator over the integer array. Read more
Source§

fn select(&'a self, rank: usize) -> Option<usize>

Returns the specified value in the integer array or None if no such value exists. Read more
Source§

fn select_iter(&'a self, rank: usize) -> Self::OneIter

Returns an iterator at the specified rank in the integer array. Read more
Source§

impl<'a> SelectZero<'a> for RLVector

Source§

type ZeroIter = ZeroIter<'a>

Iterator type over (index, value) pairs in the complement of the integer array. Read more
Source§

fn supports_select_zero(&self) -> bool

Returns true if select support has been enabled for the complement.
Source§

fn enable_select_zero(&mut self)

Enables select support for the complement vector. Read more
Source§

fn zero_iter(&'a self) -> Self::ZeroIter

Returns an iterator over the integer array of the complement vector. Read more
Source§

fn select_zero(&'a self, rank: usize) -> Option<usize>

Returns the specified value in the complement of the integer array or None if no such value exists. Read more
Source§

fn select_zero_iter(&'a self, rank: usize) -> Self::ZeroIter

Returns an iterator at the specified rank in the complement of the integer array. Read more
Source§

impl Serialize for RLVector

Source§

fn serialize_header<T: Write>(&self, writer: &mut T) -> Result<()>

Serializes the header to the writer. Read more
Source§

fn serialize_body<T: Write>(&self, writer: &mut T) -> Result<()>

Serializes the body to the writer. Read more
Source§

fn load<T: Read>(reader: &mut T) -> Result<Self>

Loads the struct from the reader. Read more
Source§

fn size_in_elements(&self) -> usize

Returns the size of the serialized struct in u64 elements. Read more
Source§

fn serialize<T: Write>(&self, writer: &mut T) -> Result<()>

Serializes the struct to the writer. Read more
Source§

fn size_in_bytes(&self) -> usize

Returns the size of the serialized struct in bytes. Read more
Source§

impl Eq for RLVector

Source§

impl StructuralPartialEq for RLVector

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.