Hash-based key-value container with direct key lookup and iteration over stored pairs.
Construct with key and value schemas in front of the module name, for example Int32 Int32 HashTable.
Key: key schema.Value: value schema.Node: stored key-value node schema.data: bucket storage.dataSize: current stored-item count.iter (-- iter): iterates stored {key value} pairs.keys (-- iter): iterates keys only.values (-- iter): iterates values only.size (-- count): returns the current stored-item count.at (key -- valueRef): returns Ref to the value at key. Missing keys are invalid.find (key -- result): returns one record with fields success and value.erase (key --): removes the item at key. Missing keys are invalid.insert (key value --): inserts one new item. Existing keys are invalid.insertUnsafe (key value --): inserts without the existing-key precheck.clear (--): removes all items and resets the stored-item count.release (--): releases all bucket storage and resets the stored-item count.makeIter (-- iter): low-level iterator helper used by the view methods.rebuild (newBucketSize --): rebuilds the bucket array for one new bucket count.=..hash method is required.find.success reports whether the key was found.find.value is meaningful only when find.success is TRUE.at and erase require an existing key and are invalid on missing keys.at and find.value return references into the current bucket storage.insert, insertUnsafe, erase, clear, release, and bucket rebuild may invalidate earlier references and iteration sources.(value -- hashValue)Returns one Nat32 hash for supported values.
.hash method when present.(source -- hashTable)Builds one HashTable from an iteration source of key-value pairs.
insert semantics."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
size=3
at2=20
found3=TRUE
value3=30
size2=2
"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
found0=TRUE
value0=10
found1=FALSE
size=1
"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
size=2
at2=20
"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
pair0Key=1
pair0Value=10
pair1Key=2
pair1Value=20
iterDone=FALSE
key0=1
key1=2
value0=10
value1=20