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 = [];
};
this.clone = function () {
const r = new Queue();
r.data = this.data.slice(0);
return r;
};
* 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 = [];
};
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
this.clone = function () {
const r = new BoundedQueue(this.capacity);
r.data = this.data.slice(0);
return r;
};
};
/**
* a bounded stack. Use push and pop to add or remove elements.
* when stack is full, it discard early entries.
* you can also read the raw elements in this.data.
*/
const BoundedStack = function (capacity) {
this.capacity = capacity;
this.data = [];
/**
* push new element to the BoundedStack. if there is no room for it,
* remove the oldest element from the stack.
*/
this.push = function (e) {
console.assert(this.data.length <= this.capacity, "bounded stack 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.pop();
};
this.clear = function () {
this.data = [];
};
this.clone = function () {
const r = new BoundedStack(this.capacity);
r.data = this.data.slice(0);
return r;
};
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
};
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);
q = new BoundedStack(3);
q.push(1);
q.push(2);
q.push(3);
console.assert(q.pop() === 3);
console.assert(q.pop() === 2);
console.assert(q.pop() === 1);
q = new BoundedStack(3);
q.push(1);
q.push(2);
q.push(3);
q.push(4);
console.assert(q.pop() === 4);
console.assert(q.pop() === 3);
console.assert(q.pop() === 2);
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,