An array hash table for mapping integer keys to generic elements
The implementation is based IntIntHashTable.
Example:
class Element extends de.polygonal.ds.HashableItem {
var val:Int;
public function new(val:Int) {
super();
this.val = val;
}
public function toString():String {
return "Element" + val;
}
}
...
var o = new de.polygonal.ds.IntHashTable<Element>(16);
for (i in 0...4) o.set(i, new Element(i));
trace(o); //outputs:
[ IntHashTable size=4 capacity=16 load=0.25
0 -> Element0
1 -> Element1
2 -> Element2
3 -> Element3
]
Constructor
new (slotCount:Int, ?initialCapacity:Int)
Parameters:
slotCount | the total number of slots into which the hashed keys are distributed.
This defines the space-time trade off of this hash table.
A high |
|---|---|
capacity | the initial physical space for storing the elements at the time this hash table is initialized.
This also defines the minimum allowed size.
If omitted, the initial |
Variables
read onlycapacity:Int
The size of the allocated storage space for the elements.
If more space is required to accommodate new elements, capacity grows according to this.growthRate.
The capacity never falls below the initial size defined in the constructor and is usually a bit larger than this.size (mild overallocation).
read onlykey:Int
A unique identifier for this object.
A hash table transforms this key into an index of an array element by using a hash function.
read onlyloadFactor:Float
The load factor measure the "denseness" of a hash table and is proportional to the time cost to look up an entry.
E.g. assuming that the keys are perfectly distributed, a load factor of 4.0 indicates that each slot stores 4 keys, which have to be sequentially searched in order to find a value.
A high load factor thus indicates poor performance.
If the load factor gets too high, additional slots can be allocated by calling this.rehash().
reuseIterator:Bool
If true, reuses the iterator object instead of allocating a new one when calling this.iterator().
The default is false.
If this value is true, nested iterations will fail as only one iteration is allowed at a time.
Methods
clone (?byRef:Bool, ?copier:T ‑> T):Collection<T>
Creates and returns a shallow copy (structure only - default) or deep copy (structure & elements) of this hash table.
If byRef is true, primitive elements are copied by value whereas objects are copied by reference.
If byRef is false, the copier function is used for copying elements. If omitted, clone() is called on each element assuming all elements implement Cloneable.
free ():Void
Destroys this object by explicitly nullifying all key/values.
If compiled with -D alchemy , always call this method when the life cycle of this object ends to prevent a memory leak.
getAll (key:Int, out:Array<T>):Int
Stores all values that are mapped to key in out or returns 0 if key does not exist.
Returns:
the total number of values mapped to key.
getCollisionCount ():Int
Counts the total number of collisions.
A collision occurs when two distinct keys are hashed into the same slot.
inline getFront (key:Int):T
Returns the value that is mapped to key or null if key does not exist.
Uses move-to-front-on-access which reduces access time when similar keys are frequently queried.
Returns a new IntHashTableIterator object to iterate over all values contained in this hash table.
The values are visited in a random order.
See:
Returns a new IntIntHashTableKeyIterator object to iterate over all keys stored in this map.
The keys are visited in a random order.
See:
pack ():IntHashTable<T>
Free up resources by reducing the capacity of the internal container to the initial capacity.
rehash (slotCount:Int):IntHashTable<T>
Redistributes all keys over slotCount.
This is an expensive operations as the hash table is rebuild from scratch.
inline remap (key:Int, val:T):Bool
Remaps the first occurrence of key to a new value val.
Returns:
true if val was successfully remapped to key.
remove (val:T):Bool
Removes all occurrences of the value val.
Returns:
true if val was removed, false if val does not exist.
set (key:Int, val:T):Bool
Maps the value val to key.
The method allows duplicate keys.
To ensure unique keys either use this.hasKey() before this.set() or this.setIfAbsent().
Returns:
true if key was added for the first time, false if another instance of key was inserted.
inline setIfAbsent (key:Int, val:T):Bool
Maps val to key in this map, but only if key does not exist yet.
Returns:
true if key was mapped to val for the first time.
unset (key:Int):Bool
Removes the first occurrence of key.
Returns:
true if key is successfully removed.