queue-lit
Version:
queue-lit is a tiny queue data structure in case you `Array#push()` or `Array#shift()` on large arrays very often
72 lines (71 loc) • 1.55 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
class Node {
constructor(value) {
__publicField(this, "value");
__publicField(this, "next");
this.value = value;
}
}
class Queue {
constructor() {
__publicField(this, "head");
__publicField(this, "tail");
__publicField(this, "_size", 0);
this.clear();
}
/**
* Removes all elements from the queue.
*/
clear() {
this.head = void 0;
this.tail = void 0;
this._size = 0;
}
/**
* Adds a new element to the queue.
*/
push(value) {
const node = new Node(value);
if (this.head && this.tail) {
this.tail.next = node;
this.tail = node;
} else {
this.head = node;
this.tail = node;
}
this._size++;
return this._size;
}
/**
* Removes and returns the first element in the queue.
*/
pop() {
if (!this.head)
return;
const current = this.head;
this.head = this.head.next;
this._size--;
return current.value;
}
/**
* Returns the number of elements in the queue.
*/
get size() {
return this._size;
}
*[Symbol.iterator]() {
let current = this.head;
while (current) {
yield current.value;
current = current.next;
}
}
}
export {
Queue
};