| Crates.io | wabi_tree |
| lib.rs | wabi_tree |
| version | 0.1.0 |
| created_at | 2025-04-26 16:01:29.890477+00 |
| updated_at | 2026-01-25 21:44:16.771985+00 |
| description | Order-statistic B-tree map and set with O(log n) rank operations. Drop-in replacements for BTreeMap/BTreeSet. |
| homepage | |
| repository | https://github.com/ckmjreynolds/wabi_tree |
| max_upload_size | |
| id | 1650396 |
| size | 309,858 |
Order-statistic B-tree collections for Rust.
wabi_tree provides OSBTreeMap and OSBTreeSet, which are drop-in replacements for the standard library's BTreeMap and BTreeSet with additional O(log n) order-statistic operations:
get_by_rank(rank) - Get the element at a given sorted positionrank_of(key) - Get the sorted position of a keyRank - e.g., map[Rank(0)] for the first elementno_std compatible - Only requires alloc, no standard library dependencystd::collections::BTreeMap/BTreeSetAdd this to your Cargo.toml:
[dependencies]
wabi_tree = "0.1"
use wabi_tree::{OSBTreeMap, Rank};
let mut scores = OSBTreeMap::new();
scores.insert("Alice", 100);
scores.insert("Bob", 85);
scores.insert("Carol", 92);
// Standard BTreeMap operations work as expected
assert_eq!(scores.get(&"Bob"), Some(&85));
assert_eq!(scores.len(), 3);
// Order-statistic operations (O(log n))
// Get the median (rank 1 = second element in sorted order by key)
let (name, score) = scores.get_by_rank(1).unwrap();
assert_eq!(*name, "Bob"); // Keys are sorted alphabetically
// Find the rank of a key
assert_eq!(scores.rank_of(&"Carol"), Some(2)); // Carol is third alphabetically
// Index by rank
assert_eq!(scores[Rank(0)], 100); // Alice's score (first alphabetically)
All standard BTreeMap methods are supported, plus:
| Method | Description | Complexity |
|---|---|---|
get_by_rank(rank) |
Get key-value pair at sorted position | O(log n) |
get_by_rank_mut(rank) |
Get key and mutable value at sorted position | O(log n) |
rank_of(key) |
Get the sorted position of a key | O(log n) |
map[Rank(i)] |
Index by rank (panics if out of bounds) | O(log n) |
All standard BTreeSet methods are supported, plus:
| Method | Description | Complexity |
|---|---|---|
get_by_rank(rank) |
Get element at sorted position | O(log n) |
rank_of(value) |
Get the sorted position of a value | O(log n) |
set[Rank(i)] |
Index by rank (panics if out of bounds) | O(log n) |
| Operation | Complexity |
|---|---|
get, contains_key |
O(log n) |
insert, remove |
O(log n) |
first_key_value, last_key_value |
O(1) |
pop_first, pop_last |
O(log n) |
len, is_empty |
O(1) |
iter, keys, values |
O(1) to create, O(1) amortized per step |
range |
O(log n) to create, O(1) amortized per step |
Order-statistic trees are useful when you need both:
Example applications:
wabi_tree uses a B+tree variant where:
This design provides:
| Feature | BTreeMap |
OSBTreeMap |
|---|---|---|
| Key-based lookup | O(log n) | O(log n) |
| Insertion/removal | O(log n) | O(log n) |
| Get by rank | O(n) | O(log n) |
| Find rank of key | O(n) | O(log n) |
| Memory overhead | Lower | Higher (stores subtree sizes) |
no_std support |
No | Yes |
Licensed under either of:
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.