Usage
var prices = new(Tree, String, Int);
set(prices, $S("Apple"), $I(12));
set(prices, $S("Banana"), $I( 6));
set(prices, $S("Pear"), $I(55));
foreach (key in prices) {
var price = get(prices, key);
println("Price of %$ is %$", key, price);
}
Manipulation
var t = new(Tree, String, Int);
set(t, $S("Hello"), $I(2));
set(t, $S("There"), $I(5));
show($I(len(t))); /* 2 */
show($I(mem(t, $S("Hello")))); /* 1 */
rem(t, $S("Hello"));
show($I(len(t))); /* 1 */
show($I(mem(t, $S("Hello")))); /* 0 */
show($I(mem(t, $S("There")))); /* 1 */
resize(t, 0);
show($I(len(t))); /* 0 */
show($I(mem(t, $S("Hello")))); /* 0 */
show($I(mem(t, $S("There")))); /* 0 */
Balanced Binary Tree
The Tree type is a self balancing binary tree implemented as a red-black tree. It provides key-value access and requires the Cmp class to be defined on the key type.
Element lookup and insertion are provided as an O(log(n)) operation. This means in general a Tree is slower than a Table but it has several other nice properties such as being able to iterate over the items in order and not having large pauses for rehashing on some insertions.
This is largely equivalent to the C++ construct std::map
assign cmp eq neq gt lt ge le name brief description definition get set mem rem key_type val_type hash hash_data foreach iter_init iter_next iter_type len mark new del construct destruct resize show look print scan