1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
#![doc = include_str!("../README.md")]
pub mod perf_and_test_utils;
pub mod qvector;
pub use qvector::QVector;
pub use qvector::QVectorBuilder;
pub mod bitvector;
pub use bitvector::rs_narrow::RSNarrow;
pub use bitvector::BitVector;
pub mod utils;
pub use qvector::rs_qvector::RSQVector;
pub use qvector::rs_qvector::RSQVector256;
pub use qvector::rs_qvector::RSQVector512;
pub mod quadwt;
pub use quadwt::QWaveletTree;
pub use quadwt::WTIndexable;
pub type QWT256<T> = QWaveletTree<T, RSQVector256>;
pub type QWT512<T> = QWaveletTree<T, RSQVector512>;
// Quad Wavelet tree with support for prefetching
pub type QWT256Pfs<T> = QWaveletTree<T, RSQVector256, true>;
pub type QWT512Pfs<T> = QWaveletTree<T, RSQVector512, true>;
use num_traits::Unsigned;
/// A trait fro the support `get` query over an `Unsigned` alphabet.
pub trait AccessUnsigned {
type Item: Unsigned;
/// Returns the symbol at position `i`.
fn get(&self, i: usize) -> Option<Self::Item>;
/// Returns the symbol at position `i`.
///
/// # Safety
/// Calling this method with an out-of-bounds index is undefined behavior.
unsafe fn get_unchecked(&self, i: usize) -> Self::Item;
}
/// A trait for the support of `rank` query over an `Unsigned` alphabet.
pub trait RankUnsigned: AccessUnsigned {
/// Returns the number of occurrences in the indexed sequence of `symbol` up to
/// position `i` excluded.
fn rank(&self, symbol: Self::Item, i: usize) -> Option<usize>;
/// Returns the number of occurrences in the indexed sequence of `symbol` up to
/// position `i` excluded. The function does not check boundaries.
///
/// # Safety
/// Calling this method with an out-of-bounds index is undefined behavior.
unsafe fn rank_unchecked(&self, symbol: Self::Item, i: usize) -> usize;
}
/// A trait for the support of ``select` query over an `Unsigned` alphabet.
pub trait SelectUnsigned: AccessUnsigned {
/// Returns the position in the indexed sequence of the `i`th occurrence of
/// `symbol`.
/// We start counting from 1, so that `select(symbol, 1)` refers to the first
/// occurrence of `symbol`. `select(symbol, 0)` returns `None`.
fn select(&self, symbol: Self::Item, i: usize) -> Option<usize>;
/// Returns the position in the indexed sequence of the `i`th occurrence of
/// `symbol`.
/// We start counting from 1, so that `select(symbol, 1)` refers to the first
/// occurrence of `symbol`.
///
/// # Safety
/// Calling this method if the `i`th occurrence of `symbol` does not exist is undefined behavior.
unsafe fn select_unchecked(&self, symbol: Self::Item, i: usize) -> usize;
}
/// A trait for the support of `get` query over the binary alphabet.
pub trait AccessBin {
/// Returns the bit at the given position `i`,
/// or [`None`] if ```i``` is out of bounds.
fn get(&self, i: usize) -> Option<bool>;
/// Returns the symbol at the given position `i`.
///
/// # Safety
/// Calling this method with an out-of-bounds index is undefined behavior.
unsafe fn get_unchecked(&self, i: usize) -> bool;
}
/// A trait for the support of `rank` query over the binary alphabet.
pub trait RankBin {
/// Returns the number of zeros in the indexed sequence up to
/// position `i` excluded.
#[inline]
fn rank0(&self, i: usize) -> Option<usize> {
if let Some(k) = self.rank1(i) {
return Some(i - k);
}
None
}
/// Returns the number of ones in the indexed sequence up to
/// position `i` excluded.
fn rank1(&self, i: usize) -> Option<usize>;
/// Returns the number of ones in the indexed sequence up to
/// position `i` excluded. `None` if the position is out of bound.
///
/// # Safety
/// Calling this method with an out-of-bounds index is undefined behavior.
unsafe fn rank1_unchecked(&self, i: usize) -> usize;
/// Returns the number of zeros in the indexed sequence up to
/// position `i` excluded.
///
/// # Safety
/// Calling this method with an out-of-bounds index is undefined behavior.
#[inline]
unsafe fn rank0_unchecked(&self, i: usize) -> usize {
i - self.rank1_unchecked(i)
}
}
// TODO: Add SelectBin trait when select will be implemented
/// A trait for the support of `get` query over the alphabet [0..3].
pub trait AccessQuad {
/// Returns the symbol at position `i`, `None` if the position is out of bound.
fn get(&self, i: usize) -> Option<u8>;
/// Returns the symbol at position `i`.
///
/// # Safety
/// Calling this method with an out-of-bounds index is undefined behavior.
unsafe fn get_unchecked(&self, i: usize) -> u8;
}
/// A trait for the support of `rank` query over the alphabet [0..3].
pub trait RankQuad {
/// Returns the number of occurrences in the indexed sequence of `symbol` up to
/// position `i` excluded. `None` if the position is out of bound.
fn rank(&self, symbol: u8, i: usize) -> Option<usize>;
/// Returns the number of occurrences in the indexed sequence of `symbol` up to
/// position `i` excluded. The function does not check boundaries.
///
/// # Safety
/// Calling this method with an out-of-bounds index or with a symbol larger than
/// 3 is undefined behavior.
unsafe fn rank_unchecked(&self, symbol: u8, i: usize) -> usize;
}
/// A trait for the support of `select` query over the alphabet [0..3].
pub trait SelectQuad {
/// Returns the position in the indexed sequence of the `i`th occurrence of
/// `symbol`.
/// We start counting from 1, so that `select(symbol, 1)` refers to the first
/// occurrence of `symbol`. `select(symbol, 0)` returns `None`.
fn select(&self, symbol: u8, i: usize) -> Option<usize>;
/// Returns the position in the indexed sequence of the `i`th occurrence of
/// `symbol`.
/// We start counting from 1, so that `select(symbol, 1)` refers to the first
/// occurrence of `symbol`.
///
/// # Safety
/// Calling this method if the `i`th occurrence of `symbol` does not exist or with a symbol larger than 3 is undefined behavior.
unsafe fn select_unchecked(&self, symbol: u8, i: usize) -> usize;
}
/// An interface to report the space usage of a data structure.
pub trait SpaceUsage {
/// Gives the space usage of the data structure in bytes.
fn space_usage_byte(&self) -> usize;
/// Gives the space usage of the data structure in KiB.
#[allow(non_snake_case)]
fn space_usage_KiB(&self) -> f64 {
let bytes = self.space_usage_byte();
(bytes as f64) / (1024_f64)
}
/// Gives the space usage of the data structure in MiB.
#[allow(non_snake_case)]
fn space_usage_MiB(&self) -> f64 {
let bytes = self.space_usage_byte();
(bytes as f64) / ((1024 * 1024) as f64)
}
/// Gives the space usage of the data structure in GiB.
#[allow(non_snake_case)]
fn space_usage_GiB(&self) -> f64 {
let bytes = self.space_usage_byte();
(bytes as f64) / ((1024 * 1024 * 1024) as f64)
}
}
/// A trait for the operations that a quad vector implementation needs
/// to provide to be used in a Quad Wavelet Tree.
pub trait WTSupport: AccessQuad + RankQuad + SelectQuad {
/// Returns the number of occurrences of `symbol` in the indexed sequence,
/// `None` if `symbol` is larger than 3, i.e., `symbol` is not valid.
fn occs(&self, symbol: u8) -> Option<usize>;
/// Returns the number of occurrences of `symbol` in the indexed sequence.
///
/// # Safety
/// Calling this method if the `symbol` is larger than 3 (i.e., `symbol` is not valid)
/// is undefined behavior.
unsafe fn occs_unchecked(&self, symbol: u8) -> usize;
/// Returns the number of occurrences of all the symbols smaller than the
/// input `symbol`, `None` if `symbol` is larger than 3,
/// i.e., `symbol` is not valid.
fn occs_smaller(&self, symbol: u8) -> Option<usize>;
/// Returns the rank of `symbol` up to the block that contains the position
/// `i`.
///
/// # Safety
/// Calling this method if the `symbol` is larger than 3 of
/// if the position `i` is out of bound is undefined behavior.
unsafe fn rank_block_unchecked(&self, symbol: u8, i: usize) -> usize;
/// Returns the number of occurrences of all the symbols smaller than the input
/// `symbol` in the indexed sequence.
///
/// # Safety
/// Calling this method if the `symbol` is larger than 3 is undefined behavior.
unsafe fn occs_smaller_unchecked(&self, symbol: u8) -> usize;
/// Prefetches counter of superblock and blocks containing the position `pos`.
fn prefetch_info(&self, pos: usize);
/// Prefetches data containing the position `pos`.
fn prefetch_data(&self, pos: usize);
}
use std::mem;
/// TODO: Improve and generalize. Incorrect if T is not a primitive type.
/// It is also error-prone to implement this for every data structure.
/// Make a macro to go over the member of a struct!
impl<T> SpaceUsage for Vec<T>
where
T: SpaceUsage + Copy,
{
fn space_usage_byte(&self) -> usize {
if !self.is_empty() {
mem::size_of::<Self>() + self.get(0).unwrap().space_usage_byte() * self.capacity()
} else {
mem::size_of::<Self>() + mem::size_of::<T>() * self.capacity()
}
}
}
impl<T> SpaceUsage for Box<[T]>
where
T: SpaceUsage + Copy,
{
fn space_usage_byte(&self) -> usize {
if !self.is_empty() {
mem::size_of::<Self>() + self.get(0).unwrap().space_usage_byte() * self.len()
} else {
mem::size_of::<Self>()
}
}
}
macro_rules! impl_space_usage {
($($t:ty),*) => {
$(impl SpaceUsage for $t {
fn space_usage_byte(&self) -> usize {
mem::size_of::<Self>()
}
})*
}
}
impl_space_usage![bool, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, f32, f64];