[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 | /**
|
---|
| 9 | * @template T
|
---|
| 10 | */
|
---|
| 11 | class ArrayQueue {
|
---|
| 12 | /**
|
---|
| 13 | * @param {Iterable<T>=} items The initial elements.
|
---|
| 14 | */
|
---|
| 15 | constructor(items) {
|
---|
| 16 | /**
|
---|
| 17 | * @private
|
---|
| 18 | * @type {T[]}
|
---|
| 19 | */
|
---|
| 20 | this._list = items ? Array.from(items) : [];
|
---|
| 21 | /**
|
---|
| 22 | * @private
|
---|
| 23 | * @type {T[]}
|
---|
| 24 | */
|
---|
| 25 | this._listReversed = [];
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Returns the number of elements in this queue.
|
---|
| 30 | * @returns {number} The number of elements in this queue.
|
---|
| 31 | */
|
---|
| 32 | get length() {
|
---|
| 33 | return this._list.length + this._listReversed.length;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Empties the queue.
|
---|
| 38 | */
|
---|
| 39 | clear() {
|
---|
| 40 | this._list.length = 0;
|
---|
| 41 | this._listReversed.length = 0;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Appends the specified element to this queue.
|
---|
| 46 | * @param {T} item The element to add.
|
---|
| 47 | * @returns {void}
|
---|
| 48 | */
|
---|
| 49 | enqueue(item) {
|
---|
| 50 | this._list.push(item);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Retrieves and removes the head of this queue.
|
---|
| 55 | * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
|
---|
| 56 | */
|
---|
| 57 | dequeue() {
|
---|
| 58 | if (this._listReversed.length === 0) {
|
---|
| 59 | if (this._list.length === 0) return;
|
---|
| 60 | if (this._list.length === 1) return this._list.pop();
|
---|
| 61 | if (this._list.length < 16) return this._list.shift();
|
---|
| 62 | const temp = this._listReversed;
|
---|
| 63 | this._listReversed = this._list;
|
---|
| 64 | this._listReversed.reverse();
|
---|
| 65 | this._list = temp;
|
---|
| 66 | }
|
---|
| 67 | return this._listReversed.pop();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * Finds and removes an item
|
---|
| 72 | * @param {T} item the item
|
---|
| 73 | * @returns {void}
|
---|
| 74 | */
|
---|
| 75 | delete(item) {
|
---|
| 76 | const i = this._list.indexOf(item);
|
---|
| 77 | if (i >= 0) {
|
---|
| 78 | this._list.splice(i, 1);
|
---|
| 79 | } else {
|
---|
| 80 | const i = this._listReversed.indexOf(item);
|
---|
| 81 | if (i >= 0) this._listReversed.splice(i, 1);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | [Symbol.iterator]() {
|
---|
| 86 | return {
|
---|
| 87 | next: () => {
|
---|
| 88 | const item = this.dequeue();
|
---|
| 89 | if (item) {
|
---|
| 90 | return {
|
---|
| 91 | done: false,
|
---|
| 92 | value: item
|
---|
| 93 | };
|
---|
| 94 | }
|
---|
| 95 | return {
|
---|
| 96 | done: true,
|
---|
| 97 | value: undefined
|
---|
| 98 | };
|
---|
| 99 | }
|
---|
| 100 | };
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | module.exports = ArrayQueue;
|
---|