Base class for the dictionary backed by a linked list.
public abstract class base hash dictionary[readonly value key type, value value type]
implements readonly dictionary[key type, value type]
namespace parameters
protected class hash cell[readonly value key type, value value type]
implements dictionaryentry[key type, value type]
private key type the key
protected integer the key hash
private var value type the value
protected var hash cell[key type, value type] or null next
public overload hash cell(key type the key, integer the key hash, value type the value, hash cell[key type, value type] or null next)
this • the key = the key
this • the key hash = the key hash
this • the value = the value
this • next = next
public overload hash cell(key type the key, integer the key hash, value type the value)
this(the key, the key hash, the value, missinginstance)
override key type key()
return the key
override value type value()
return the value
protected void set value(value type new value)
this • the value = new value
Wrapper of an array that implements on-demand resizing and copy-on-write semantics. The same dictionary_state can be shared by multiple instances of base_hash_dictionarys.
protected class dictionary state[readonly value key type, value value type]
import idealmachineelementsarray
Specifies whether this instance of dictionary_state is writable. A single non-writable copy can be shared among multiple instances of base_hash_dictionary.
var boolean writable
An array used to store the elements.
var array[hash cell[key type, value type] or null] the buckets
Specifies how many elements are stored in this dictionary_state. The size is less or equal to the_buckets.size.
var nonnegative size
Construct a dictionary state with an array of specified size.
overload dictionary state(nonnegative initial size)
the buckets = array[hash cell[key type, value type] or null] • new(initial size)
size = 0
Construct a dictionary state with a table of default size.
overload dictionary state()
protected void clear()
if size != 0
Make sure the array is of at least the specified size.
void reserve(nonnegative reserve size)
if the bucketssize >= reserve size
return
var new size : the bucketssize * 2
if new size < reserve size
new size = reserve size
the buckets = array[hash cell[key type, value type] or null] • new(new size)
for var nonnegative i : 0; i < old bucketssize; i += 1
var bucket : old buckets[i]
while bucket is_not null
old next : bucketnext
bucketnext = the buckets[new index]
the buckets[new index] = bucket
bucket = old next
old bucketsscrub(0, old bucketssize)
protected nonnegative bucket index(integer hash)
index : ((hash % bucket size) + bucket size) % bucket size
assert index is nonnegative
return index
protected dictionary state[key type, value type] copy()
for var nonnegative i : 0; i < the bucketssize; i += 1
var bucket : the buckets[i]
while bucket is_not null
new cell : hash cell[key type, value type] • new(bucketkey, bucketthe key hash, bucketvalue, resultthe buckets[i])
resultthe buckets[i] = new cell
bucket = bucketnext
resultsize = this • size
return result
protected equivalence with hash[key type] equivalence
protected var dictionary state[key type, value type] state
protected overload base hash dictionary(equivalence with hash[key type] equivalence)
this • equivalence = equivalence
this • state = dictionary state[key type, value type] • new()
The number of elements in the collection.
implement nonnegative size => statesize
Specifies whether the collection has zero elements.
implement boolean is empty => statesize == 0
Specifies whether the collection has more than zero elements. Shortcut for !is_empty.
implement boolean is not empty => statesize != 0
Enumerates elements in some collection-defined order. This method returns a snapshot of the collection state, so subsequent mutations of the collection do not cause changes in the returned list.
implement immutable list[dictionaryentry[key type, value type]] elements()
if is empty
return empty[dictionaryentry[key type, value type]] • new()
for var nonnegative i : 0; i < statethe bucketssize; i += 1
for var entry : statethe buckets[i]; entry is_not null; entry = entrynext
resultappend(base dictionary entry[key type, value type] • new(entry))
return resultfrozen copy()
implement value type or null get(key type key)
assert key is_not null
hash : equivalencehash(key)
for var entry : bucket(hash); entry is_not null; entry = entrynext
if hash == entrythe key hash && equivalence(key, entrykey)
return entryvalue
return missinginstance
implement boolean contains key(key type key)
assert key is_not null
hash : equivalencehash(key)
for var entry : bucket(hash); entry is_not null; entry = entrynext
if hash == entrythe key hash && equivalence(key, entrykey)
return true
return false
implement immutable set[key type] keys()
if is empty
pass
for var nonnegative i : 0; i < statethe bucketssize; i += 1
for var entry : statethe buckets[i]; entry is_not null; entry = entrynext
resultadd(entrykey)
return resultfrozen copy()
implement readonly collection[value type] values()
for var nonnegative i : 0; i < statethe bucketssize; i += 1
for var entry : statethe buckets[i]; entry is_not null; entry = entrynext
resultappend(entryvalue)
return resultfrozen copy()
private string debug display()
var string s : ""
for var nonnegative i : 0; i < statethe bucketssize; i += 1
s = s ++ "b" ++ i ++ ": "
for var entry : statethe buckets[i]; entry is_not null; entry = entrynext
s = s ++ "<" ++ (entrykey !> string) ++ ":" ++ (entryvalue !> string) ++ ">"
s = s ++ "\n"
return s