main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[79a0317] | 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 | /**
|
---|
| 19 | * @private
|
---|
| 20 | * @type {TupleSet<T>}
|
---|
| 21 | */
|
---|
| 22 | this._set = new TupleSet(items);
|
---|
| 23 | /**
|
---|
| 24 | * @private
|
---|
| 25 | * @type {Iterator<T>}
|
---|
| 26 | */
|
---|
| 27 | this._iterator = this._set[Symbol.iterator]();
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Returns the number of elements in this queue.
|
---|
| 32 | * @returns {number} The number of elements in this queue.
|
---|
| 33 | */
|
---|
| 34 | get length() {
|
---|
| 35 | return this._set.size;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Appends the specified element to this queue.
|
---|
| 40 | * @param {T} item The element to add.
|
---|
| 41 | * @returns {void}
|
---|
| 42 | */
|
---|
| 43 | enqueue(...item) {
|
---|
| 44 | this._set.add(...item);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Retrieves and removes the head of this queue.
|
---|
| 49 | * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
|
---|
| 50 | */
|
---|
| 51 | dequeue() {
|
---|
| 52 | const result = this._iterator.next();
|
---|
| 53 | if (result.done) {
|
---|
| 54 | if (this._set.size > 0) {
|
---|
| 55 | this._iterator = this._set[Symbol.iterator]();
|
---|
| 56 | const value = this._iterator.next().value;
|
---|
| 57 | this._set.delete(...value);
|
---|
| 58 | return value;
|
---|
| 59 | }
|
---|
| 60 | return;
|
---|
| 61 | }
|
---|
| 62 | this._set.delete(...result.value);
|
---|
| 63 | return result.value;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | module.exports = TupleQueue;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.