A simple set using an array

Example:

var o = new polygonal.ds.ListSet<String>();
o.set("a");
o.set("b");
o.set("b");
o.set("c");
o.set("c");
o.set("c");
trace(o); //outputs:

[ ListSet size=3
  a
  b
  c
]

Constructor

@:value({ initialCapacity : 16 })new (initialCapacity:Int = 16, ?source:Array<T>)

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.NORMAL)growthRate:Int = GrowthRate.NORMAL

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.

@: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.

Methods

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

Removes all elements.

Parameters:

gc

if true, nullifies references upon removal so the garbage collector can reclaim used memory.

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

Creates and returns a shallow copy (structure only - default) or deep copy (structure & elements) of this set.

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.

contains (val:T):Bool

Same as this.has().

free ():Void

Destroys this object by explicitly nullifying all elements.

Improves GC efficiency/performance (optional).

has (val:T):Bool

Returns true if this set contains val.

isEmpty ():Bool

Returns true only if this.size is 0.

inlineiter (f:T ‑> Void):ListSet<T>

Calls 'f` on all elements in random order.

iterator ():Itr<T>

Iterates over all elements contained in this set.

The elements are visited in a random order.

See:

@:value({ copier : null })merge (set:Set<T>, ?assign:Bool, ?copier:T ‑> T):Void

Adds all elements of the set other to this set.

Parameters:

assign

if true, the copier parameter is ignored and primitive elements are copied by value whereas objects are copied by reference. If false, the this.clone() method is called on each element.
In this case all elements have to implement Cloneable.

copier

a custom function for copying elements. Replaces element.clone() if assign is false.

pack ():ListSet<T>

Reduces the capacity of the internal container to the initial capacity.

May cause a reallocation, but has no effect on this.size and its elements. An application can use this operation to free up memory by unlocking resources for the garbage collector.

remove (val:T):Bool

Removes val.

Returns:

true if val was successfully removed.

reserve (n:Int):ListSet<T>

Preallocates storage for n elements.

May cause a reallocation, but has no effect on this.size and its elements. Useful before inserting a large number of elements as this reduces the amount of incremental reallocation.

set (val:T):Bool

Adds val to this set if possible.

Returns:

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

toArray ():Array<T>

Returns an unordered array containing all elements in this set.

toString ():String

Prints out all elements.

inlineunset (val:T):Bool

Removes val from this set if possible.

Returns:

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