Newer
Older
* an unbounded fifo queue. Use push and pop to add or remove elements.
* you can also read the raw elements in this.data.
*/
const Queue = function () {
this.data = [];
this.push = function (e) {
this.data.push(e);
};
/**
* note: pop() will return undefined when there is not enough element.
*/
this.pop = function () {
return this.data.shift();
};
this.clear = function () {
this.data = [];
};
* a bounded fifo queue. Use push and pop to add or remove elements.
* you can also read the raw elements in this.data.
*/
const BoundedQueue = function (capacity) {
this.capacity = capacity;
this.data = [];
/**
* push new element to the BoundedQueue. if there is no room for it,
* remove the oldest element from the queue.
*/
this.push = function (e) {
console.assert(this.data.length <= this.capacity, "bounded queue data size is invalid");
if (this.data.length === this.capacity) {
this.data.shift();
}
this.data.push(e);
};
/**
* note: pop() will return undefined when there is not enough element.
*/
this.pop = function () {
return this.data.shift();
};
this.clear = function () {
this.data = [];
};
};
const testFIFO = function () {
var q = new Queue();
q.push(1);
q.push(2);
q.push(3);
console.assert(q.pop() === 1);
console.assert(q.pop() === 2);
console.assert(q.pop() === 3);
q = new BoundedQueue(3);
q.push(1);
q.push(2);
q.push(3);
console.assert(q.pop() === 1);
console.assert(q.pop() === 2);
console.assert(q.pop() === 3);
q = new BoundedQueue(3);
q.push(1);
q.push(2);
q.push(3);
q.push(4);
console.assert(q.pop() === 2);
console.assert(q.pop() === 3);
console.assert(q.pop() === 4);
};
const testClearFunction = function () {
q = new BoundedQueue(3);
q.push(1);
q.push(2);
q.clear();
console.assert(q.pop() === undefined);
console.assert(q.data.length === 0);
};
const runAllTests = function () {
testFIFO();
testClearFunction();
};
return {
Queue: Queue,
BoundedQueue: BoundedQueue,
};
}();