-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (36 loc) · 786 Bytes
/
index.js
File metadata and controls
36 lines (36 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Queue() {
this.queue = [];
this.offset = 0;
}
Queue.prototype.push = function(val) {
this.queue.push(val);
};
Queue.prototype.pop = function() {
var queue = this.queue;
if(queue.length === 0) {
return undefined;
}
var old = queue[this.offset];
this.offset ++;
if(this.offset * 2 > queue.length) {
queue = queue.slice(this.offset);
this.offset = 0;
}
return old;
};
Queue.prototype.reset = function() {
this.queue = [];
this.offset = 0;
};
Object.defineProperty(Queue.prototype, "length", {
get: function length() {
return this.queue.length - this.offset;
}
});
Object.defineProperty(Queue.prototype, "top", {
get: function length() {
var queue = this.queue;
return (queue.length > 0 ? queue[this.offset] : undefined);
}
});
module.exports = Queue;