Menu

Hash Tables

Mark Anthony Taylor (Shyreman)

A hash table is a keyed data structure with constant time retrieval, insertion and deletion.

The syntax is:

(map <key-type> <value-type> <local-variable-name>)

Example: (map IString Int32 agents)

Key types can include any primitive type and IString references. Currently no other type is
implemented. As with all containers hash tables support method Length, giving the number of items
in the map:

Example: (Int32 length = agents.Length)

Insertion is done with method Insert:

(agents.Insert "Joe" 90) // inserts element 90 into the hash table with key 'Joe'

If an item already exists with a matching key, then it is overwritten. Retrieval is done by using the
node assignment:

(node n = (agents "Joe"))

Method Exists can be invoked on such nodes, which evaluates to true if the element with key "Joe"
is found, otherwise is false.

(Bool wasFound = n.Exists)

if the Exists method for a map node evaluates to true, and the element type is primitive
or an archetype then one can use method Value:

(Int32 joeAtomicWeight = n.Value)

Otherwise, for the case of derived types (structures) use the reference operator to
initialize a reference variable:

(map Int64 Vec3 mapIdToVec3)
...
(node n = (mapIdToVec3 24))
(Vec3 v = & n) // v now refers to the Vec3 value associated with element 24

Nodes are deleted with method Pop:

(n.Pop)

Related

Wiki: Content