| 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 backed by arrays with a reversed dequeue buffer to avoid the cost
|
|---|
| 10 | * of repeated `Array#shift` operations.
|
|---|
| 11 | * @template T
|
|---|
| 12 | */
|
|---|
| 13 | class ArrayQueue {
|
|---|
| 14 | /**
|
|---|
| 15 | * Seeds the queue with an optional iterable of items in dequeue order.
|
|---|
| 16 | * @param {Iterable<T>=} items The initial elements.
|
|---|
| 17 | */
|
|---|
| 18 | constructor(items) {
|
|---|
| 19 | /**
|
|---|
| 20 | * @private
|
|---|
| 21 | * @type {T[]}
|
|---|
| 22 | */
|
|---|
| 23 | this._list = items ? [...items] : [];
|
|---|
| 24 | /**
|
|---|
| 25 | * @private
|
|---|
| 26 | * @type {T[]}
|
|---|
| 27 | */
|
|---|
| 28 | this._listReversed = [];
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Returns the current number of items waiting in either internal buffer.
|
|---|
| 33 | * @returns {number} The number of elements in this queue.
|
|---|
| 34 | */
|
|---|
| 35 | get length() {
|
|---|
| 36 | return this._list.length + this._listReversed.length;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Removes all pending items from both internal buffers.
|
|---|
| 41 | */
|
|---|
| 42 | clear() {
|
|---|
| 43 | this._list.length = 0;
|
|---|
| 44 | this._listReversed.length = 0;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Appends an item to the tail of the queue.
|
|---|
| 49 | * @param {T} item The element to add.
|
|---|
| 50 | * @returns {void}
|
|---|
| 51 | */
|
|---|
| 52 | enqueue(item) {
|
|---|
| 53 | this._list.push(item);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Removes and returns the next item in FIFO order, switching to a reversed
|
|---|
| 58 | * buffer when that is cheaper than shifting from the front of the array.
|
|---|
| 59 | * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
|
|---|
| 60 | */
|
|---|
| 61 | dequeue() {
|
|---|
| 62 | if (this._listReversed.length === 0) {
|
|---|
| 63 | if (this._list.length === 0) return;
|
|---|
| 64 | if (this._list.length === 1) return this._list.pop();
|
|---|
| 65 | if (this._list.length < 16) return this._list.shift();
|
|---|
| 66 | const temp = this._listReversed;
|
|---|
| 67 | this._listReversed = this._list;
|
|---|
| 68 | this._listReversed.reverse();
|
|---|
| 69 | this._list = temp;
|
|---|
| 70 | }
|
|---|
| 71 | return this._listReversed.pop();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Removes the first matching item from whichever internal buffer currently
|
|---|
| 76 | * contains it.
|
|---|
| 77 | * @param {T} item the item
|
|---|
| 78 | * @returns {void}
|
|---|
| 79 | */
|
|---|
| 80 | delete(item) {
|
|---|
| 81 | const i = this._list.indexOf(item);
|
|---|
| 82 | if (i >= 0) {
|
|---|
| 83 | this._list.splice(i, 1);
|
|---|
| 84 | } else {
|
|---|
| 85 | const i = this._listReversed.indexOf(item);
|
|---|
| 86 | if (i >= 0) this._listReversed.splice(i, 1);
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | [Symbol.iterator]() {
|
|---|
| 91 | return {
|
|---|
| 92 | next: () => {
|
|---|
| 93 | const item = this.dequeue();
|
|---|
| 94 | if (item) {
|
|---|
| 95 | return {
|
|---|
| 96 | done: false,
|
|---|
| 97 | value: item
|
|---|
| 98 | };
|
|---|
| 99 | }
|
|---|
| 100 | return {
|
|---|
| 101 | done: true,
|
|---|
| 102 | value: undefined
|
|---|
| 103 | };
|
|---|
| 104 | }
|
|---|
| 105 | };
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | module.exports = ArrayQueue;
|
|---|