Mutable dictionary backed by a hashtable.
public class hash set[readonly value element type]
extends base hash set[element type]
implements set[element type]
public overload hash set(equivalence with hash[element type] equivalence)
super(equivalence)
public overload hash set()
super(runtime util • default equivalence !> any value !> equivalence with hash[element type])
private void copy on write()
if !state • writable
state = state • copy()
assert state • writable
implement void clear()
implement void add(element type the value)
state • reserve(size + 1)
do add(the value)
implement void add all(readonly collection[element type] the collection)
state • reserve(size + the collection • size)
for (the element : the collection • elements)
do add(the element)
private void do add(element type the value)
hash : equivalence • hash(the value)
index : state • bucket index(hash)
var entry : state • the buckets[index]
if entry is null
state • the buckets[index] = hash cell[element type] • new(the value, hash)
state • size += 1
return
loop
if hash == entry • the hash && equivalence(the value, entry • the value)
return
next : entry • next
if next is null
entry • next = hash cell[element type] • new(the value, hash)
state • size += 1
return
else
entry = next
implement boolean remove(element type the element)
hash : equivalence • hash(the element)
index : state • bucket index(hash)
var entry : state • the buckets[index]
if entry is null
return false
if hash == entry • the hash && equivalence(the element, entry • the value)
state • the buckets[index] = entry • next
return true
loop
next : entry • next
if next is null
return false
if hash == next • the hash && equivalence(the element, next • the value)
entry • next = next • next
return true
entry = next
private void decrement size()
new size : state • size - 1
assert new size is nonnegative
state • size = new size