An array hash set for storing integers

Example:

var o = new polygonal.ds.IntHashSet(16);
for (i in 0...4) o.set(i);
trace(o); //outputs:

[ IntHashSet size=4 capacity=16 load=0.25
  0
  1
  2
  3
]

Constructor

@:value({ initialCapacity : -1 })new (slotCount:Int, initialCapacity:Int = -1)

Parameters:

slotCount

the total number of slots into which the hashed values are distributed. This defines the space-time trade off of this set. A high slotCount value leads to better performance but requires more memory. This value can only be changed later on by calling this.rehash(), which in turn rebuilds the entire hash table (expensive).

capacity

the initial physical space for storing the elements at the time this set is initialized. This also defines the minimum allowed size. If omitted, the initial capacity is set to slotCount. If more space is required to accommodate new elements, capacity grows according to this.growthRate.

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).

@:value(GrowthRate.DOUBLE)growthRate:Int = GrowthRate.DOUBLE

The growth rate of the container.

See:

@:value(HashKey.next())read onlykey:Int = HashKey.next()

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 set and is proportional to the time cost to look up an entry.

E.g. assuming that the elements are perfectly distributed, a load factor of 4.0 indicates that each slot stores 4 elements, which have to be sequentially searched in order to find an element.

A high load factor thus indicates poor performance.

If the load factor gets too high, additional slots can be allocated by calling this.rehash().

@:value(false)reuseIterator:Bool = false

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.

read onlysize:Int

The total number of elements.

read onlyslotCount:Int

The total number of allocated slots.

Methods

@:value({ gc : false })clear (gc:Bool = false):Void

Removes all elements.

The gc parameter has no effect.

@:value({ copier : null, byRef : true })clone (byRef:Bool = true, ?copier:Int ‑> Int):Collection<Int>

Duplicates this hash set by creating a deep copy (byRef and copier are ignored).

inlinecontains (val:Int):Bool

Same as this.has().

free ():Void

Destroys this object by explicitly nullifying all elements.

Improves GC efficiency/performance (optional).

getCollisionCount ():Int

Counts the total number of collisions.

A collision occurs when two distinct elements are hashed into the same slot.

inlinehas (val:Int):Bool

Returns true if this set contains val.

inlinehasFront (val:Int):Bool

Returns true if this set contains val.

Uses move-to-front-on-access which reduces access time when similar elements are frequently queried.

inlineisEmpty ():Bool

Returns true only if this.size is 0.

inlineiter (f:Int ‑> Void):IntHashSet

Calls f on all values in random order.

iterator ():Itr<Int>

Returns a new IntHashSetIterator object to iterate over all elements contained in this hash set.

The elements are visited in a random order.

See:

pack ():IntHashSet

Free up resources by reducing the capacity of the internal container to the initial capacity.

rehash (slotCount:Int):IntHashSet

Redistributes all elements over slotCount.

This is an expensive operations as the set is rebuild from scratch.

inlineremove (val:Int):Bool

Removes val.

Returns:

true if val was successfully removed, false if val does not exist.

inlineset (val:Int):Bool

Adds val to this set if possible.

Returns:

true if val was added to this set, false if val already exists.

toArray ():Array<Int>

Returns an unordered array containing all elements in this set.

toString ():String

Prints out all elements.

inlineunset (val:Int):Bool

Removes val from this set if possible.

Returns:

true if val was removed from this set, false if val does not exist.

Static variables

@:value(MathTools.INT32_MIN)staticinlineread onlyVAL_ABSENT:Int = MathTools.INT32_MIN

Return code for a non-existing element.