A queue based on a linked list
A queue is a linear list for which all insertions are made at one end of the list; all deletions (and usually all accesses) are made at the other end.
This is called a FIFO structure (First In, First Out).
Example:
var o = new polygonal.ds.LinkedQueue<Int>();
for (i in 0...4) o.enqueue(i);
trace(o); //outputs:
[ LinkedQueue size=4
front
0 -> 0
1 -> 1
2 -> 2
3 -> 3
]
Constructor
new (reservedSize:Int = 0, ?source:Array<T>)
Parameters:
reservedSize | if > 0, this queue maintains an object pool of node objects. Prevents frequent node allocation and thus increases performance at the cost of using more memory. |
---|
Variables
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.
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.
Methods
clear (gc:Bool = false):Void
Removes all elements.
Parameters:
gc | if true, elements are nullified upon removal so the garbage collector can reclaim used memory. |
---|
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 queue.
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
.
inlineforEach (f:T ‑> Int ‑> T):LinkedQueue<T>
Calls f
on all elements.
The function signature is: f(input, index):output
- input: current element
- index: position relative to the front(=0) of the queue
- output: element to be stored at given index
free ():Void
Destroys this object by explicitly nullifying all nodes, pointers and elements.
Improves GC efficiency/performance (optional).
iterator ():Itr<T>
Returns a new LinkedQueue
object to iterate over all elements contained in this queue.
Preserves the natural order of a queue (First-In-First-Out).
See:
remove (val:T):Bool
Removes and nullifies all occurrences of val
.
Returns:
true if at least one occurrence of val
was removed.
shuffle (?rvals:Array<Float>):LinkedQueue<T>
Shuffles the elements of this collection by using the Fisher-Yates algorithm.
Parameters:
rvals | a list of random double values in the interval [0, 1) defining the new positions of the elements.
If omitted, random values are generated on-the-fly by calling |
---|
toArray ():Array<T>
Returns an array containing all elements in this queue.
Preserves the natural order of this queue (First-In-First-Out).