⚙️ ArrayQueue Class

⚙️ ArrayQueue Class — Sorting by order of insertion

The ArrayQueue class provides a minimal, array-backed implementation of the abstract Queue. It supports basic FIFO or LIFO ordering, making it suitable for general-purpose queue and stack operations.


๐Ÿ“š Overview

Internally, ArrayQueue stores items in a plain JavaScript array. Its behavior is controlled by a fifo flag:

  • fifo: true  FIFO (queue)
  • fifo: false  LIFO (stack)

All items are treated as distinct — no identity or value-based equality checks are performed. This means:

  • has(item) always returns false
  • remove(item) always returns false

๐Ÿงฉ Constructor

new ArrayQueue(fifo = true, items = [])
  • fifo: Boolean flag to toggle between FIFO and LIFO behavior.
  • items: Optional array to seed the queue.

๐Ÿงช Public API

get fifo(): boolean

Returns whether the queue uses FIFO ordering.


get items(): Array<any>

Exposes the internal storage array (use with caution).


n(): number

Returns the current number of elements in the queue.


has(item): boolean

Always returns false.
In ArrayQueue, no item is considered equivalent to any other — not even itself.


add(item): boolean

Adds an item to the end of the queue. Always returns true.


remove(item): boolean

Always returns false. Value-based removal is unsupported.


peek(first = true): any

Returns the item at the head or tail:

  • first = true: returns the first item (according to fifo).
  • first = false: returns the last item.

poll(first = true): any

Removes and returns the item at the head or tail:

  • first = true: removes from front.
  • first = false: removes from end.

clear(): void

Removes all items from the queue.


[Symbol.iterator](): Iterator<any>

Returns a forward iterator over the queue’s items.


reverse(): Each<any>

Returns an iterable (Each) yielding items in reverse order.
This is a view — not a reversed copy or a new queue.


๐Ÿง  Design Notes

  • ArrayQueue is optimized for simplicity, speed, and minimal assumptions.
  • Items are added fast, removed fast, and iterated efficiently.
  • Use it when you need a lightweight FIFO or LIFO queue with no equivalence checking.
  • All items are treated as distinct — even identical ones. No value is considered equal to any other.

✍️ Example

const q = new ArrayQueue(true);
q.add("๐ŸŽ");
q.add("๐ŸŒ");
q.add("๐Ÿ“");

q.peek();   // → "๐ŸŽ"
q.poll();   // → "๐ŸŽ"
q.reverse(); // → Iterable over ["๐Ÿ“", "๐ŸŒ"]


“In ArrayQueue, nothing is equal — not even to itself.”
 @fizzwiz ✨


Comments

Popular posts from this blog

๐Ÿง  Contemplating a True Set

๐Ÿงฑ v0.0.0-dev.1 — First Brick

☯️ v0.0.0-dev.3 — Classifier & TrueSet