Class in collections::tree coil-stdlib

TreeMap

class TreeMap<K, V>
Import
use collections::tree::{TreeMap};
Source: src/collections/tree.hy

An ordered map from keys of type K to values of type V.

Keys need Ord and Eq. Iteration visits entries in ascending key order, and min_key / max_key give the ends. The tree is not rebalanced: inserting keys that are already sorted makes lookups linear, so prefer HashMap when you do not need the order.

Fields#

FieldTypeDescription
keysVec<K>Node keys, including removed nodes. Prefer iteration.
valsVec<V>Node values parallel to keys.
leftVec<int>Index of each node’s left child, or -1.
rightVec<int>Index of each node’s right child, or -1.
rootintIndex of the root node, or -1 when the map is empty.

Associated functions#

NameDescription
newCreates an empty map.
emptyCreates an empty map; the same as TreeMap::new.

Methods#

NameDescription
sizeReturns the number of entries.
is_emptyReturns true when the map has no entries.
clearRemoves every entry.
iterReturns an iterator over the entries in ascending key order.
insertStores v under k, replacing any value already there.
containsReturns true when the map has an entry for k.
get_orReturns the value stored under k, or fallback when there is none.
getReturns the value stored under k, or None when there is none.
removeRemoves the entry for k.
min_keyReturns the smallest key, or fallback when the map is empty.
max_keyReturns the largest key, or fallback when the map is empty.

Trait implementations#

Invariants#

Checked according to --contracts (see Contracts).

invariant self.len >= 0 && self.len <= self.keys.len()
invariant self.vals.len() == self.keys.len()
invariant self.left.len() == self.keys.len() && self.right.len() == self.keys.len()
invariant (self.root < 0) == (self.len == 0)

Examples#

use collections::tree::{TreeMap};
use tests::assert;
let m: TreeMap<int, string> = TreeMap::new();
m.insert(30, "c");
m.insert(10, "a");
m.insert(20, "b");
let order = "";
for e in m {
    order = order + e.value;
}
assert!(order == "abc");
assert!(m.min_key(0) == 10);

See also#

↑ ↓ navigate ↵ open