UNPKG

dastal

Version:

Data Structures & Algorithms implementations

43 lines 1.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayQueue = void 0; /** * An implementation of the {@link Queue} interface using an array */ class ArrayQueue { /** * Instantiate the queue. * * @param elements - A set of elements to initialize the queue with. */ constructor(elements) { this.array = elements ? Array.from(elements) : []; } clear() { this.array.length = 0; } dequeue() { return this.size < 1 ? undefined : this.array.shift(); } enqueue(element) { return this.array.push(element); } peek() { return this.size < 1 ? undefined : this.array[0]; } get size() { return this.array.length; } /** * Receive an iterator through the queue. * * **Note:** Unexpected behavior can occur if the collection is modified during iteration. * * @returns An iterator through the queue */ [Symbol.iterator]() { return this.array[Symbol.iterator](); } } exports.ArrayQueue = ArrayQueue; //# sourceMappingURL=arrayQueue.js.map