|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * FIFO queue that keeps items unique by storing them in insertion order inside
|
|---|
| 10 | * a `Set`.
|
|---|
| 11 | * @template T
|
|---|
| 12 | */
|
|---|
| 13 | class Queue {
|
|---|
| 14 | /**
|
|---|
| 15 | * Seeds the queue with an optional iterable of initial unique items.
|
|---|
| 16 | * @param {Iterable<T>=} items The initial elements.
|
|---|
| 17 | */
|
|---|
| 18 | constructor(items) {
|
|---|
| 19 | /**
|
|---|
| 20 | * @private
|
|---|
| 21 | * @type {Set<T>}
|
|---|
| 22 | */
|
|---|
| 23 | this._set = new Set(items);
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Returns the number of unique items currently waiting in the queue.
|
|---|
| 28 | * @returns {number} The number of elements in this queue.
|
|---|
| 29 | */
|
|---|
| 30 | get length() {
|
|---|
| 31 | return this._set.size;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Enqueues an item, moving nothing if that value is already present.
|
|---|
| 36 | * @param {T} item The element to add.
|
|---|
| 37 | * @returns {void}
|
|---|
| 38 | */
|
|---|
| 39 | enqueue(item) {
|
|---|
| 40 | this._set.add(item);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Removes and returns the oldest enqueued item.
|
|---|
| 45 | * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
|
|---|
| 46 | */
|
|---|
| 47 | dequeue() {
|
|---|
| 48 | const result = this._set[Symbol.iterator]().next();
|
|---|
| 49 | if (result.done) return;
|
|---|
| 50 | this._set.delete(result.value);
|
|---|
| 51 | return result.value;
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | module.exports = Queue;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.