Sha256: e3e334c6fd29f9b082e155e12569b0c5bba85b02c6ae0704ce81cac17c55bedd
Contents?: true
Size: 940 Bytes
Versions: 27
Compression:
Stored size: 940 Bytes
Contents
"use strict"; /** * @template T */ class Queue { /** * @param {Iterable<T>=} items The initial elements. */ constructor(items) { /** @private @type {Set<T>} */ this.set = new Set(items); /** @private @type {Iterator<T>} */ this.iterator = this.set[Symbol.iterator](); } /** * Returns the number of elements in this queue. * @returns {number} The number of elements in this queue. */ get length() { return this.set.size; } /** * Appends the specified element to this queue. * @param {T} item The element to add. * @returns {void} */ enqueue(item) { this.set.add(item); } /** * Retrieves and removes the head of this queue. * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty. */ dequeue() { const result = this.iterator.next(); if (result.done) return undefined; this.set.delete(result.value); return result.value; } } module.exports = Queue;
Version data entries
27 entries across 26 versions & 9 rubygems