HashTable

Hash-based key-value container with direct key lookup and iteration over stored pairs.


HashTable

Construct with key and value schemas in front of the module name, for example Int32 Int32 HashTable.

Fields

Public methods

Internal helpers


Key requirements and iteration order


Lookup and reference conventions


hash (value -- hashValue)

Returns one Nat32 hash for supported values.


toHashTable (source -- hashTable)

Builds one HashTable from an iteration source of key-value pairs.


Examples

Lookup and erasure

"HashTable" use
"String" use
"control" use

{} Int32 {} [
  h: Int32 Int32 HashTable;
  1 10 @h.insert
  2 20 @h.insert
  3 30 @h.insert
  r: 3 @h.find;
  ("size=" h.size LF
   "at2=" 2 @h.at new LF
   "found3=" r.success LF
   "value3=" @r.value new LF) printList
  2 @h.erase
  ("size2=" h.size LF) printList
  0
] "main" exportFunction

Expected Output

size=3
at2=20
found3=TRUE
value3=30
size2=2

Lookup of a missing key

"HashTable" use
"String" use
"control" use

{} Int32 {} [
  h: Int32 Int32 HashTable;
  1 10 @h.insert
  r0: 1 @h.find;
  r1: 5 @h.find;
  ("found0=" r0.success LF
   "value0=" @r0.value new LF
   "found1=" r1.success LF
   "size=" h.size LF) printList
  0
] "main" exportFunction

Expected Output

found0=TRUE
value0=10
found1=FALSE
size=1

Building from pairs

"HashTable" use
"String" use
"control" use

{} Int32 {} [
  t: ((1 10) (2 20)) toHashTable;
  ("size=" t.size LF
   "at2=" 2 @t.at new LF) printList
  0
] "main" exportFunction

Expected Output

size=2
at2=20

Runtime example: iter, keys, and values

"HashTable" use
"String" use
"control" use

{} Int32 {} [
  h: Int32 Int32 HashTable;
  1 10 @h.insert
  2 20 @h.insert
  pairs: @h.iter;
  pair0: ok0: @pairs.next;;
  pair1: ok1: @pairs.next;;
  pair2: ok2: @pairs.next;;
  keys: @h.keys;
  key0: hasKey0: @keys.next;;
  key1: hasKey1: @keys.next;;
  values: @h.values;
  value0: hasValue0: @values.next;;
  value1: hasValue1: @values.next;;
  ("pair0Key=" pair0.key LF
   "pair0Value=" @pair0.value new LF
   "pair1Key=" pair1.key LF
   "pair1Value=" @pair1.value new LF
   "iterDone=" ok2 LF
   "key0=" key0 LF
   "key1=" key1 LF
   "value0=" value0 new LF
   "value1=" value1 new LF) printList
  0
] "main" exportFunction

Expected Output

pair0Key=1
pair0Value=10
pair1Key=2
pair1Value=20
iterDone=FALSE
key0=1
key1=2
value0=10
value1=20

See also