BTree Index-map
BTree-based maps and sets with stable entry indices.
This crate provides ordered map and set types that combine the
sorted-key lookup of a B-tree with stable integer indices backed by a
contiguous Vec:
BTreeIndexMap<K, V>— each key maps to exactly one value.BTreeIndexMultiMap<K, V>— each key may map to multiple values.BTreeIndexSet<T>— each value is stored at most once.BTreeIndexMultiSet<T>— each value may occur any number of times.
The set types are thin wrappers around their map counterparts
(BTreeIndexMap<T, ()> and BTreeIndexMultiMap<T, ()> respectively).
Entries are stored in a Vec and assigned a stable index equal to
their position in that Vec. The index of an entry does not change as
long as no entry is removed. A B-tree over the keys provides O(log n)
lookup, insertion, and removal, while the Vec backing allows O(1)
random access by index.
Usage
BTreeIndexMap
use BTreeIndexMap;
let mut map = new;
// Entries are stored in insertion order and assigned a stable index.
map.insert;
map.insert;
// O(1) access by index (insertion order).
assert_eq!;
assert_eq!;
// O(log n) lookup by key, returning the stable index alongside the value.
assert_eq!;
// Iterate in insertion order.
let by_insertion: = map.iter.map.collect;
assert_eq!;
// Iterate in sorted key order.
let by_key: = map.iter_sorted.map.collect;
assert_eq!;
BTreeIndexMultiMap
use BTreeIndexMultiMap;
let mut map = new;
// Multiple values per key, each with its own stable index.
map.push_back;
map.push_back;
map.push_back;
assert_eq!;
let fruits: = map.get.copied.collect;
assert_eq!;
// Iterate in sorted key order, values in position order within each key.
let by_key: = map.iter_sorted.map.collect;
assert_eq!;
BTreeIndexSet
use BTreeIndexSet;
let mut set = new;
// Values are stored in insertion order and assigned a stable index.
set.insert;
set.insert;
// O(1) access by index (insertion order).
assert_eq!;
// O(log n) lookup, returning the stable index alongside the value.
assert_eq!;
BTreeIndexMultiSet
use BTreeIndexMultiSet;
let mut set = new;
// Multiple occurrences of the same value, each with its own stable
// index.
set.push_back;
set.push_back;
set.push_back;
assert_eq!;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.