@eagleoutice/flowr-dev
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
38 lines • 1.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayQueue = void 0;
/**
* A FIFO queue over a growable array with amortized O(1) dequeue.
* Entries are consumed via an index (instead of costly `Array#shift` calls),
* and the consumed prefix is dropped once it dominates the array,
* bounding the memory overhead to twice the live queue size.
*/
class ArrayQueue {
elements;
idx = 0;
constructor(initial) {
this.elements = initial ? initial.slice() : [];
}
enqueue(item) {
this.elements.push(item);
}
/** Returns the oldest element, or `undefined` if the queue {@link isEmpty}. */
dequeue() {
if (this.idx >= this.elements.length) {
return undefined;
}
if (this.idx > 1024 && this.idx * 2 > this.elements.length) {
this.elements = this.elements.slice(this.idx);
this.idx = 0;
}
return this.elements[this.idx++];
}
get size() {
return this.elements.length - this.idx;
}
isEmpty() {
return this.idx >= this.elements.length;
}
}
exports.ArrayQueue = ArrayQueue;
//# sourceMappingURL=queue.js.map