The Wavelet Matrix is a space-efficient variant of Wavelet Tree data structure.
After adding to Cargo.toml, add this line to lib.rs or main.rs.
rust
extern crate wavelet_matrix;
Add to main.rs:
```rust use wavelet_matrix::*;
fn main() {
let vec: Vec
assert_eq!(wm.len(), 12);
assert_eq!(wm.lookup(7), 6);
// Counting
assert_eq!(wm.count(0..wm.len(), 2), 3);
assert_eq!(wm.count(0..wm.len(), 4), 2);
assert_eq!(wm.count(0..wm.len(), 5), 1);
assert_eq!(wm.count(0..wm.len(), 7), 0);
assert_eq!(wm.count(0..wm.len(), 39), 0);
assert_eq!(wm.count_prefix(0..wm.len(), 8, 3), 1);
assert_eq!(wm.count_prefix(0..wm.len(), 6, 1), 1);
assert_eq!(wm.count_prefix(0..wm.len(), 0, 1), 4);
assert_eq!(wm.count_prefix(0..wm.len(), 0, 2), 7);
assert_eq!(wm.count_lt(0..wm.len(), 2), 4);
assert_eq!(wm.count_lt(0..wm.len(), 7), 11);
assert_eq!(wm.count_gt(0..wm.len(), 2), 5);
assert_eq!(wm.count_gt(0..wm.len(), 7), 1);
assert_eq!(wm.count_range(0..wm.len(), 0..10), 12);
assert_eq!(wm.count_range(0..wm.len(), 4..6), 3);
// Searching
assert_eq!(wm.search(0..wm.len(), 4).collect::<Vec<usize>>(),
vec![2, 6]);
assert_eq!(wm.search(3..wm.len(), 4).collect::<Vec<usize>>(), vec![6]);
assert_eq!(wm.search(0..wm.len(), 7).collect::<Vec<usize>>(), vec![]);
// Statistics
assert_eq!(wm.top_k(0..wm.len(), 0..10, 12),
vec![(2, 3), (1, 2), (4, 2), (0, 2), (5, 1), (6, 1), (9, 1)]);
assert_eq!(wm.top_k(0..wm.len(), 0..10, 4),
vec![(2, 3), (1, 2), (4, 2), (0, 2)]);
assert_eq!(wm.top_k(0..wm.len(), 2..9, 12),
vec![(2, 3), (4, 2), (5, 1), (6, 1)]);
// classic .rank()/.select() API
assert_eq!(wm.rank(5, 1), 2);
assert_eq!(wm.select(2, 2), 10);
} ```
Given an unsigned integer sequence A, it provides the following queries.
.len()
:
.lookup(pos)
:
.count(start..end, value)
:
e == value
included in A[start..end]
.count_prefix(start..end, value, ignore_bit)
:
e >> ignore_bit == value >> ignore_bit
included in A[start..end]
192.168.10.0/24
. In this case, the ignore_bit will be 8..count_lt(start..end, value)
:
e < value
included in A[start..end]
.count_gt(start..end, value)
:
e > value
included in A[start..end]
.count_range(start..end, val_start..val_end)
:
val_start <= e < val_end
included in A[start..end]
.search(start..end, value)
:
e == value
included in A[start..end]
.search_prefix(start..end, value, ignore_bit)
:
e >> ignore_bit == value >> ignore_bit
included in A[start..end]
.top_k(start..end, val_start..val_end, k)
:
val_start..val_end
..max_k(start..end, val_start..val_end, k)
:
val_start..val_end
..min_k(start..end, val_start..val_end, k)
:
val_start..val_end
..rank(pos, value)
: counts value included in A[0..pos].
.select(rank, value)
: return the position of the (rank+1)-th value
.top_k()
, .max_k()
and min_k()
..search()
and .search_prefix()
..count()
, .count_prefix()
, .count_lt()
, .count_gt()
and .count_range()
.&Vec<u64>
, instead of Vec<u64>
Add u128 support or arbitrary-length integer support
The fastest implementation on the planet
A Go package for myriad array operations using wavelet trees
excellent slides in Japanese
The original inventor's pdf: