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
//! Order-statistic B-tree collections for Rust.
//!
//! This crate 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`](OSBTreeMap::get_by_rank) - Get the element at a given sorted position
//! - [`rank_of`](OSBTreeMap::rank_of) - Get the sorted position of a key
//! - Indexing by [`Rank`] - e.g., `map[Rank(0)]` for the first element
//!
//! # Example
//!
//! ```
//! 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)
//! 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)
//! ```
//!
//! # Features
//!
//! - **`no_std` compatible** - Only requires `alloc`, no standard library dependency
//! - **Drop-in replacement** - API mirrors `std::collections::BTreeMap`/`BTreeSet`
//! - **O(log n) rank operations** - Efficient order-statistic queries via subtree size augmentation
//! - **Cache-efficient** - B+tree structure with contiguous node storage
//!
//! # Implementation
//!
//! The collections are implemented as B+trees (all data in leaves, linked leaf chain)
//! with subtree size augmentation. Each internal node tracks the sizes of its subtrees,
//! enabling O(log n) rank-based access without full traversal.
// These forbid rules and lint groups are meant to be very restrictive.
// NOTE: We have to allow unsafe code in order to performantly match BTreeMap and BTreeSet's functionality.
// #![forbid(unsafe_code)]
// Enable coverage attributes for nightly builds.
// Allow unused code during initial development.
// Allow todo!() placeholders during API scaffolding.
extern crate alloc;
pub use Rank;
pub use OSBTreeMap;
pub use OSBTreeSet;