Usage
var prices = new(Table, 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(Table, 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 */
Hash table
The Table
type is a hash table data structure that maps keys to values. It uses an open-addressing robin-hood hashing scheme which requires Hash
and Cmp
to be defined on the key type. Keys and values are copied into the collection using the Assign
class and intially have zero'd memory.
Hash tables provide O(1)
lookup, insertion and removal can but require long pauses when the table must be rehashed and all entries processed.
This is largely equivalent to the C++ construct std::unordered_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