A deque is a "double-ended queue"

This is a linear list for which all insertions and deletions (and usually all accesses) are made at ends of the list.

Example:

var o = new polygonal.ds.ArrayedDeque<Int>();
for (i in 0...4) o.pushFront(i);
trace(o); //outputs:

[ ArrayedDeque size=4
  front
  0 -> 3
  1 -> 2
  2 -> 1
  3 -> 0
]

Constructor

@:value({ blockPoolCapacity : 4, blockSize : 64 })new (blockSize:Int = 64, blockPoolCapacity:Int = 4, ?source:Array<T>)

Parameters:

blockSize

a block represents a contiguous piece of memory; whenever the deque runs out of space an additional block with a capacity of blockSize elements is allocated and added to the list of existing blocks. The parameter affects the performance-memory trade-off: a large blockSize improves performances but wastes memory if the utilization is low; a small blockSize uses memory more efficiently but is slower due to frequent allocation of blocks. The default value is 64; the minimum value is 4. blockSize has to be a power of two.

blockPoolCapacity

the total number of blocks to reuse when blocks are removed or relocated (from front to back or vice-versa). This improves performances but uses more memory. The default value is 4; a value of 0 disables block pooling.

source

copies all values from source in the range [0, source.length - 1] to this collection.

Variables

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

inlineback ():T

Returns the last element of the deque (index this.size - 1).

@:value({ gc : false })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.

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

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

Returns true if this deque contains val.

forEach (f:T ‑> Int ‑> T):ArrayedDeque<T>

Calls f on all elements.

The function signature is: f(input, index):output

  • input: current element
  • index: position relative to the front(=0)
  • output: element to be stored at given index

@:access(polygonal.ds.BlockList)free ():Void

Destroys this object by explicitly nullifying all elements for GC'ing used resources.

Improves GC efficiency/performance (optional).

inlinefront ():T

Returns the first element of this deque (index 0).

getBack (i:Int):T

Returns the element at index i relative to the back of this deque.

The back element is at index [0], the front element is at index [this.size - 1].

getFront (i:Int):T

Returns the element at index i relative to the front of this deque.

The front element is at index [0], the back element is at index [this.size - 1].

indexOfBack (val:T):Int

Returns the index of the first occurence of val or -1 if val does not exist.

The back element is at index [0], the front element is at index [this.size - 1].

indexOfFront (val:T):Int

Returns the index of the first occurence of val or -1 if val does not exist.

The front element is at index [0], the back element is at index [this.size - 1].

inlineisEmpty ():Bool

Returns true only if this.size is 0.

iter (f:T ‑> Void):ArrayedDeque<T>

Calls 'f` on all elements in order.

iterator ():Itr<T>

Returns a new ArrayedDequeIterator object to iterate over all elements contained in this deque.

Preserves the natural order of a deque.

See:

pack ():ArrayedDeque<T>

Removes all superfluous blocks and overwrites elements stored in empty locations with null.

An application can use this operation to free up memory by unlocking resources for the garbage collector.

popBack ():T

Deletes the element at the end of the deque.

inlinepopFront ():T

Removes and returns the element at the beginning of this deque.

inlinepushBack (val:T):Void

Inserts val at the back of the deque.

inlinepushFront (val:T):Void

Inserts val at the front of this deque.

remove (val:T):Bool

Removes and nullifies all occurrences of val.

Returns:

true if at least one occurrence of val was removed.

toArray ():Array<T>

Returns an array containing all elements in this deque in the natural order.

toString ():String

Prints out all elements.