pub struct BTree<K: BTreeKey, V, A: Allocator = Global> { /* private fields */ }
Expand description
An ordered map based on a B+ Tree.
This is similar to the standard library’s BTreeMap
but differs in several
ways:
- Lookups and insertions are 2-4x faster than
BTreeMap
. BTree
can optionally be used as a multi-map and hold duplicate keys.- Keys must be
Copy
and convertible to and from integers via theBTreeKey
trait. - The maximum integer value is reserved for internal use and cannot be used by keys.
- Elements in the tree are ordered by the integer value of the key instead
of the
Ord
implementation of the keys. Cursor
andCursorMut
can be used to seek back-and-forth in the tree while inserting or removing elements.- Iterators only support forward iteration.
The data structure design is based on the B- Tree by Sergey Slotin, but has been significantly extended.
Implementations§
Source§impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A>
impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A>
Sourcepub fn cursor(&self) -> Cursor<'_, K, V, A>
pub fn cursor(&self) -> Cursor<'_, K, V, A>
Returns a Cursor
pointing at the first element of the tree.
Sourcepub fn cursor_at(&self, bound: Bound<K>) -> Cursor<'_, K, V, A>
pub fn cursor_at(&self, bound: Bound<K>) -> Cursor<'_, K, V, A>
Returns a Cursor
pointing at the first element with key greater
than bound
.
Sourcepub fn cursor_mut(&mut self) -> CursorMut<'_, K, V, A>
pub fn cursor_mut(&mut self) -> CursorMut<'_, K, V, A>
Returns a CursorMut
pointing at the first element of the tree.
Sourcepub fn cursor_mut_at(&mut self, bound: Bound<K>) -> CursorMut<'_, K, V, A>
pub fn cursor_mut_at(&mut self, bound: Bound<K>) -> CursorMut<'_, K, V, A>
Returns a CursorMut
pointing at the first element with key greater
than bound
.
Source§impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A>
impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A>
Sourcepub fn iter(&self) -> Iter<'_, K, V, A> ⓘ
pub fn iter(&self) -> Iter<'_, K, V, A> ⓘ
Gets an iterator over the entries of the map, sorted by key.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, A> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, A> ⓘ
Gets a mutable iterator over the entries of the map, sorted by key.
Sourcepub fn iter_from(&self, bound: Bound<K>) -> Iter<'_, K, V, A> ⓘ
pub fn iter_from(&self, bound: Bound<K>) -> Iter<'_, K, V, A> ⓘ
Gets an iterator over the entries of the map starting from the given bound.
Sourcepub fn iter_mut_from(&mut self, bound: Bound<K>) -> IterMut<'_, K, V, A> ⓘ
pub fn iter_mut_from(&mut self, bound: Bound<K>) -> IterMut<'_, K, V, A> ⓘ
Gets a mutable iterator over the entries of the map starting from the given bound.
Sourcepub fn keys(&self) -> Keys<'_, K, V, A> ⓘ
pub fn keys(&self) -> Keys<'_, K, V, A> ⓘ
Gets an iterator over the keys of the map, in sorted order.
Sourcepub fn values(&self) -> Values<'_, K, V, A> ⓘ
pub fn values(&self) -> Values<'_, K, V, A> ⓘ
Gets an iterator over the values of the map, in order by key.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V, A> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, A> ⓘ
Gets a mutable iterator over the values of the map, in order by key.
Sourcepub fn range(&self, range: impl RangeBounds<K>) -> Range<'_, K, V, A> ⓘ
pub fn range(&self, range: impl RangeBounds<K>) -> Range<'_, K, V, A> ⓘ
Constructs an iterator over a sub-range of elements in the map.
Unlike BTreeMap
, this is not a DoubleEndedIterator
: it only allows
forward iteration.
Sourcepub fn range_mut(&mut self, range: impl RangeBounds<K>) -> RangeMut<'_, K, V, A> ⓘ
pub fn range_mut(&mut self, range: impl RangeBounds<K>) -> RangeMut<'_, K, V, A> ⓘ
Constructs a mutable iterator over a sub-range of elements in the map.
Unlike BTreeMap
, this is not a DoubleEndedIterator
: it only allows
forward iteration.
Source§impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A>
impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A>
Sourcepub fn new_in(alloc: A) -> Self
pub fn new_in(alloc: A) -> Self
Creates a new, empty BTree
with the given allocator.
This requires an initial memory allocation on creation.
Sourcepub fn get(&self, key: K) -> Option<&V>
pub fn get(&self, key: K) -> Option<&V>
Returns a reference to the value corresponding to the key.
Sourcepub fn get_mut(&mut self, key: K) -> Option<&mut V>
pub fn get_mut(&mut self, key: K) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None
is returned.
If the map did have this key present, the value is updated, and the old value is returned.
Sourcepub fn insert_multi(&mut self, key: K, value: V)
pub fn insert_multi(&mut self, key: K, value: V)
Inserts a key-value pair into the map while allowing for multiple identical keys.
This allows the BTree
to be used as a multi-map where each key can
have multiple values. In this case BTree::get
, BTree::get_mut
and BTree::remove
will only operate on one of the associated values
(arbitrarily chosen).
Trait Implementations§
Source§impl<'a, K: BTreeKey, V: Clone, A: Allocator> Extend<(K, &'a V)> for BTree<K, V, A>
impl<'a, K: BTreeKey, V: Clone, A: Allocator> Extend<(K, &'a V)> for BTree<K, V, A>
Source§fn extend<T: IntoIterator<Item = (K, &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, &'a V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<K: BTreeKey, V, A: Allocator> Extend<(K, V)> for BTree<K, V, A>
impl<K: BTreeKey, V, A: Allocator> Extend<(K, V)> for BTree<K, V, A>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)