Class: ArrayQueue

ArrayQueue(fifoopt, itemsopt)

A basic queue implementation based on an internal array.

In ArrayQueue, newly added items are always appended to the end of the internal array.
The extraction order is controlled by a Boolean flag:

Flag:

When fifo is true, items follow First-In-First-Out (FIFO) behavior; otherwise, they follow LIFO.

Notably, all items are treated as unique: no equivalence or identity checking is used.
Even identical items are considered distinct, and:

always return false.

The class is optimized for usage of:

Constructor

new ArrayQueue(fifoopt, itemsopt)

Creates an ArrayQueue.

Parameters:
Name Type Attributes Default Description
fifo boolean <optional>
true

If true, uses FIFO ordering; otherwise, LIFO.

items Array.<any> <optional>
[]

Optional initial array of items.

Source:

Classes

ArrayQueue

Members

fifo

Source:

items

Source:

Methods

add(item) → {boolean}

Appends an item to the queue.

Parameters:
Name Type Description
item *

The item to add.

Source:
Returns:

Always true.

Type
boolean

clear()

Removes all items from the queue.

Source:

has() → {boolean}

Always returns false because this queue does not support item equivalence.

Source:
Returns:
Type
boolean

n() → {number}

Source:
Returns:

The number of items in the queue.

Type
number

peek(firstopt) → {*}

Retrieves (without removing) the first or last item based on direction.

Parameters:
Name Type Attributes Default Description
first boolean <optional>
true

If false, retrieves the last item.

Source:
Returns:

The retrieved item.

Type
*

poll(firstopt) → {*}

Retrieves and removes the first or last item based on direction.

Parameters:
Name Type Attributes Default Description
first boolean <optional>
true

If false, removes from the end.

Source:
Returns:

The extracted item.

Type
*

remove(item) → {boolean}

Always returns false. Items cannot be removed by value.

Parameters:
Name Type Description
item *

The item to remove.

Source:
Returns:

Always false.

Type
boolean

reverse() → {Each.<any>}

Returns an iterable view over this queue in reverse order.

Source:
Returns:

An Each instance yielding the queue items from end to start.

Type
Each.<any>

select(n, firstopt) → {Array.<*>}

Selects in place the first or last n items, returning the items that were removed.

This overrides Queue.select() with a more performant batch implementation using Array.splice().

Behavior:

  • If first === true: keep the first n items, discard the rest.
  • If first === false: keep the last n items, discard the rest.

Edge cases:

  • If n >= this.items.length, nothing is discarded.
  • If n <= 0, all items are discarded.
Parameters:
Name Type Attributes Default Description
n number

Number of items to keep.

first boolean <optional>
true

Whether to keep items from the start (true) or end (false).

Source:
Returns:
  • The discarded items.
Type
Array.<*>