-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
48 lines (40 loc) · 940 Bytes
/
index.js
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
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
// Simple FIFO queue implementation to avoid having to do shift()
// on an array, which is slow.
function Queue() {
this.length = 0;
}
Queue.prototype.push = function (item) {
var node = {item: item};
if (this.last) {
this.last = this.last.next = node;
} else {
this.last = this.first = node;
}
this.length++;
};
Queue.prototype.shift = function () {
var node = this.first;
if (node) {
this.first = node.next;
if (!(--this.length)) {
this.last = undefined;
}
return node.item;
}
};
Queue.prototype.slice = function (start, end) {
start = typeof start === 'undefined' ? 0 : start;
end = typeof end === 'undefined' ? Infinity : end;
var output = [];
var i = 0;
for (var node = this.first; node; node = node.next) {
if (--end < 0) {
break;
} else if (++i > start) {
output.push(node.item);
}
}
return output;
}
module.exports = Queue;