Last change
on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const TupleSet = require("./TupleSet");
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * @template {any[]} T
|
---|
| 12 | */
|
---|
| 13 | class TupleQueue {
|
---|
| 14 | /**
|
---|
| 15 | * @param {Iterable<T>=} items The initial elements.
|
---|
| 16 | */
|
---|
| 17 | constructor(items) {
|
---|
| 18 | /** @private @type {TupleSet<T>} */
|
---|
| 19 | this._set = new TupleSet(items);
|
---|
| 20 | /** @private @type {Iterator<T>} */
|
---|
| 21 | this._iterator = this._set[Symbol.iterator]();
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Returns the number of elements in this queue.
|
---|
| 26 | * @returns {number} The number of elements in this queue.
|
---|
| 27 | */
|
---|
| 28 | get length() {
|
---|
| 29 | return this._set.size;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Appends the specified element to this queue.
|
---|
| 34 | * @param {T} item The element to add.
|
---|
| 35 | * @returns {void}
|
---|
| 36 | */
|
---|
| 37 | enqueue(...item) {
|
---|
| 38 | this._set.add(...item);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Retrieves and removes the head of this queue.
|
---|
| 43 | * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
|
---|
| 44 | */
|
---|
| 45 | dequeue() {
|
---|
| 46 | const result = this._iterator.next();
|
---|
| 47 | if (result.done) {
|
---|
| 48 | if (this._set.size > 0) {
|
---|
| 49 | this._iterator = this._set[Symbol.iterator]();
|
---|
| 50 | const value = this._iterator.next().value;
|
---|
| 51 | this._set.delete(...value);
|
---|
| 52 | return value;
|
---|
| 53 | }
|
---|
| 54 | return undefined;
|
---|
| 55 | }
|
---|
| 56 | this._set.delete(...result.value);
|
---|
| 57 | return result.value;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | module.exports = TupleQueue;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.