btree_slab/lib.rs
1//! This crate provides an alternative implementation to the standard `BTreeMap`
2//! and `BTreeSet` data structures based on a slab data-structure. In principle,
3//! this implementation is more flexible and more memory efficient. It is more
4//! flexible by providing an extended set of low-level operations on B-Trees
5//! through the `BTreeExt` trait which can be used to further extend the
6//! functionalities of the `BTreeMap` collection.
7//! In addition, the underlying node allocation scheme is abstracted by a type
8//! parameter that can be instantiated by any data structure implementing
9//! slab-like operations.
10//! By default, the `Slab` type (from the `slab` crate) is used, which means
11//! that every node of the tree are allocated in a contiguous memory region,
12//! reducing the number of allocations needed.
13//! In theory, another type could be used to store the entire B-Tree on the stack.
14//!
15//! ## Usage
16//!
17//! From the user point of view, the collection provided by this crate can be
18//! used just like the standard `BTreeMap` and `BTreeSet` collections.
19//! ```
20//! use btree_slab::BTreeMap;
21//!
22//! // type inference lets us omit an explicit type signature (which
23//! // would be `BTreeMap<&str, &str>` in this example).
24//! let mut movie_reviews = BTreeMap::new();
25//!
26//! // review some movies.
27//! movie_reviews.insert("Office Space",       "Deals with real issues in the workplace.");
28//! movie_reviews.insert("Pulp Fiction",       "Masterpiece.");
29//! movie_reviews.insert("The Godfather",      "Very enjoyable.");
30//! movie_reviews.insert("The Blues Brothers", "Eye lyked it a lot.");
31//!
32//! // check for a specific one.
33//! if !movie_reviews.contains_key("Les Misérables") {
34//!     println!("We've got {} reviews, but Les Misérables ain't one.",
35//!              movie_reviews.len());
36//! }
37//!
38//! // oops, this review has a lot of spelling mistakes, let's delete it.
39//! movie_reviews.remove("The Blues Brothers");
40//!
41//! // look up the values associated with some keys.
42//! let to_find = ["Up!", "Office Space"];
43//! for movie in &to_find {
44//!     match movie_reviews.get(movie) {
45//!        Some(review) => println!("{}: {}", movie, review),
46//!        None => println!("{} is unreviewed.", movie)
47//!     }
48//! }
49//!
50//! // Look up the value for a key (will panic if the key is not found).
51//! println!("Movie review: {}", movie_reviews["Office Space"]);
52//!
53//! // iterate over everything.
54//! for (movie, review) in &movie_reviews {
55//!     println!("{}: \"{}\"", movie, review);
56//! }
57//! ```
58//!
59//! ### Custom node allocation
60//!
61//! One can use `btree_slab::generic::BTreeMap` to
62//! use a custom slab type to handle nodes allocation.
63//!
64//! ```rust
65//! use slab::Slab;
66//! use btree_slab::generic::{Node, BTreeMap};
67//!
68//! # type K = u32;
69//! # type V = u32;
70//! let mut map: BTreeMap<K, V, Slab<Node<K, V>>> = BTreeMap::new();
71//! ```
72//!
73//! In this example,
74//! the `Slab<Node<_, _>>` type is a slab-like data structure responsible for the nodes allocation.
75//! It must implement all the traits defining the `cc_traits::Slab` trait alias.
76//!
77//! ## Extended API & Addressing
78//!
79//! In this implementation of B-Trees, each node of a tree is addressed
80//! by the `Address` type.
81//! The extended API, visible through the `BTreeExt` trait,
82//! allows the caller to explore, access and modify the
83//! internal structure of the tree using this addressing system.
84//! This can be used to further extend the functionalities of the `BTreeMap`
85//! collection, for example in the
86//! [`btree-range-map`](https://crates.io/crates/btree-range-map) crate.
87use slab::Slab;
88
89pub mod generic;
90pub mod utils;
91
92/// B-Tree map based on `Slab`.
93pub type BTreeMap<K, V> = generic::BTreeMap<K, V, Slab<generic::Node<K, V>>>;
94
95/// B-Tree set based on `Slab`.
96pub type BTreeSet<T> = generic::BTreeSet<T, Slab<generic::Node<T, ()>>>;