Skip to content
CryoCryo home
Stdlibcollections

btree_map

import std::collections::btree_map; · source

Types

BTreeMap<K, V, A>

type struct BTreeMap<K, V, A = GlobalAllocator> where A: Allocator {
    private root:   mut BTreeNode<K, V>*;
    private length: i64;
    private alloc:  A;
}

A map kept in key order, built as a B-tree. Keys need Ord rather than Hash; finding, inserting, and removing are O(log n). Each node holds up to eleven entries side by side, so a lookup compares against a run of neighbouring keys before it follows a pointer — a tree of n entries is about log₆(n) nodes deep — and every node but the root holds at least five.

Reach for it over HashMap when the order matters: iterating in key order, the smallest or greatest key, or every key in a range.

import std::collections::btree_map::{ BTreeMap };
import std::core::pair::{ Pair };

mut ages: BTreeMap<i64, i64> = BTreeMap::new();
ages.insert(30, 1);
ages.insert(10, 2);
ages.insert(20, 3);
for (entry in ages.entries()) {
    // 10, then 20, then 30
}
const smallest: Option<Pair<i64*, i64*>> = ages.first();
const low: i64 = 15;
const from_15: i64 = ages.range(low..).count();   // 20 and 30

The access methods mirror HashMap's, though a lookup takes a reference to the key type itself: get reads by value and needs V: Copy, get_reference and get_mutable borrow, get_or_insert hands back a mut V*, insert returns the value it replaced, and remove returns the value (remove_entry the key too). first and last borrow the smallest and greatest entries; pop_first and pop_last take them out.

Constructors

static new() -> BTreeMap<K, V, GlobalAllocator>;

An empty map backed by GlobalAllocator. Allocates nothing until the first insert.

static new_in(alloc: A) -> BTreeMap<K, V, A>;

An empty map that allocates through alloc.

Methods

length(&this) -> i64;

The number of keys in the map.

is_empty(&this) -> boolean;

True when the map holds no keys.

contains_key(&this, key: &K) -> boolean where K: Ord;

True when the map holds key.

get(&this, key: &K) -> Option<V> where K: Ord, V: Copy;

A copy of the value for key, or None when the key is absent.

get_reference(&this, key: &K) -> Option<V*> where K: Ord;

A read-only borrow of the value for key, or None when the key is absent. Valid until the next insert or remove.

get_mutable(mut &this, key: &K) -> Option<mut V*> where K: Ord;

A writable borrow of the value for key, or None when the key is absent. Valid until the next insert or remove.

insert(mut &this, key: K, value: V) -> Option<V> where K: Ord;

Insert key -> value. Returns the previous value if the key was present, otherwise None; a present key keeps the key it was inserted with. Panics on allocation failure; use try_insert to recover from it.

try_insert(mut &this, key: K, value: V) -> Result<Option<V>, AllocationError> where K: Ord;

insert that returns Err(AllocationError) instead of panicking when a node cannot be allocated. The map is unchanged by a failed insert.

get_or_insert(mut &this, key: K, default: V) -> mut V* where K: Ord;

Borrow the value for key, inserting default first if the key is absent: one descent, unlike get_reference then insert. When the key is present, key and default are released. Panics on allocation failure.

try_get_or_insert(mut &this, key: K, default: V) -> Result<mut V*, AllocationError> where K: Ord;

get_or_insert that returns Err(AllocationError) instead of panicking when a node cannot be allocated.

remove(mut &this, key: &K) -> Option<V> where K: Ord;

Remove the entry for key. Returns its value, or None when the key was absent; the stored key is released.

remove_entry(mut &this, key: &K) -> Option<Pair<K, V>> where K: Ord;

Remove the entry for key and hand back both its key and its value, or None when the key was absent.

first(&this) -> Option<Pair<K*, V*>>;

The entry with the smallest key, or None when empty.

last(&this) -> Option<Pair<K*, V*>>;

The entry with the greatest key, or None when empty.

pop_first(mut &this) -> Option<Pair<K, V>> where K: Ord;

Remove and return the entry with the smallest key, or None when empty.

pop_last(mut &this) -> Option<Pair<K, V>> where K: Ord;

Remove and return the entry with the greatest key, or None when empty.

clear(mut &this) -> void;

Release every entry and every node.

iter(&this) -> implement Iterator<Pair<K, V>> + DoubleEndedIterator where K: Copy, V: Copy;

Every entry by value, in ascending key order. Gated on K: Copy and V: Copy: the pairs are bit copies of the stored entries, which for an owning key or value would be released twice. entries walks any map by borrow.

keys(&this) -> implement Iterator<K> + DoubleEndedIterator where K: Copy;

Every key by value, in ascending order. Gated on K: Copy, as iter is.

values(&this) -> implement Iterator<V> + DoubleEndedIterator where V: Copy;

Every value by value, in ascending key order. Gated on V: Copy, as iter is.

entries(&this) -> implement Iterator<Pair<K*, V*>> + DoubleEndedIterator;

Every entry as a borrowed key and value, in ascending key order: sound for any K and V, owning or not.

range<R>(&this, bounds: R) -> implement Iterator<Pair<K*, V*>> + DoubleEndedIterator where K: Ord, R: RangeBounds<K>;

The entries whose keys are in bounds, as borrowed keys and values, in ascending key order and from either end: map.range(low..high), map.range(low..=high), map.range(low..), map.range(..high), map.range(..), with bounds of the key type. Finding where the range starts and ends is O(log n).

key_references(&this) -> implement Iterator<K*> + DoubleEndedIterator;

Every key by borrow, in ascending order.

value_references(&this) -> implement Iterator<V*> + DoubleEndedIterator;

Every value by borrow, in ascending key order.

values_mutable(mut &this) -> implement Iterator<mut V*> + DoubleEndedIterator;

Every value by writable borrow, in ascending key order: the way to change owning values in place.

Iterating

Iteration runs in ascending key order and from either end: every iterator here is a DoubleEndedIterator, so .reverse() walks the keys in descending order. iter(), keys(), and values() yield by value and need Copy; entries(), key_references(), and value_references() borrow and are sound for every key and value type, and values_mutable() yields mut V*. range(bounds) takes any range form over the key type — low..high, low..=high, low.., ..high, .. — through RangeBounds. An iterator borrows the map's storage and is invalidated by any insert or remove.

Trait implementations

implement<K, V, A> trait Drop for struct BTreeMap<K, V, A> where A: Allocator
implement<K, V> trait Clone for struct BTreeMap<K, V, GlobalAllocator> where K: Ord + Clone, V: Clone
implement<K, V> trait Default for struct BTreeMap<K, V, GlobalAllocator>
implement<K, V> trait FromIterator<Pair<K, V>> for struct BTreeMap<K, V, GlobalAllocator> where K: Ord
implement<K, V, A> trait Iterable for struct BTreeMap<K, V, A> where K: Copy, V: Copy, A: Allocator
implement<V> trait ToValue for struct BTreeMap<String<GlobalAllocator>, V, GlobalAllocator> where V: ToValue
std::encoding::value::convert
implement<V> trait FromValue for struct BTreeMap<String<GlobalAllocator>, V, GlobalAllocator> where V: FromValue
std::encoding::value::convert
implement<K, V, A> trait Display for struct BTreeMap<K, V, A> where K: Display, V: Display, A: Allocator
std::fmt::display
implement<K, V, A> trait Debug for struct BTreeMap<K, V, A> where K: Debug, V: Debug, A: Allocator
std::fmt::display

Clone is a deep copy, Default a fresh empty map that allocates nothing until the first insert, and collecting an iterator of Pair<K, V> builds one.