Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
function arrayMove(src, srcIndex, dst, dstIndex, len) {
3
for (var j = 0; j < len; ++j) {
4
dst[j + dstIndex] = src[j + srcIndex];
5
src[j + srcIndex] = void 0;
6
}
7
}
8
9
function Queue(capacity) {
10
this._capacity = capacity;
11
this._length = 0;
12
this._front = 0;
13
}
14
15
Queue.prototype._willBeOverCapacity = function (size) {
16
return this._capacity < size;
17
};
18
19
Queue.prototype._pushOne = function (arg) {
20
var length = this.length();
21
this._checkCapacity(length + 1);
22
var i = (this._front + length) & (this._capacity - 1);
23
this[i] = arg;
24
this._length = length + 1;
25
};
26
27
Queue.prototype._unshiftOne = function(value) {
28
var capacity = this._capacity;
29
this._checkCapacity(this.length() + 1);
30
var front = this._front;
31
var i = (((( front - 1 ) &
32
( capacity - 1) ) ^ capacity ) - capacity );
33
this[i] = value;
34
this._front = i;
35
this._length = this.length() + 1;
36
};
37
38
Queue.prototype.unshift = function(fn, receiver, arg) {
39
this._unshiftOne(arg);
40
this._unshiftOne(receiver);
41
this._unshiftOne(fn);
42
};
43
44
Queue.prototype.push = function (fn, receiver, arg) {
45
var length = this.length() + 3;
46
if (this._willBeOverCapacity(length)) {
47
this._pushOne(fn);
48
this._pushOne(receiver);
49
this._pushOne(arg);
50
return;
51
}
52
var j = this._front + length - 3;
53
this._checkCapacity(length);
54
var wrapMask = this._capacity - 1;
55
this[(j + 0) & wrapMask] = fn;
56
this[(j + 1) & wrapMask] = receiver;
57
this[(j + 2) & wrapMask] = arg;
58
this._length = length;
59
};
60
61
Queue.prototype.shift = function () {
62
var front = this._front,
63
ret = this[front];
64
65
this[front] = undefined;
66
this._front = (front + 1) & (this._capacity - 1);
67
this._length--;
68
return ret;
69
};
70
71
Queue.prototype.length = function () {
72
return this._length;
73
};
74
75
Queue.prototype._checkCapacity = function (size) {
76
if (this._capacity < size) {
77
this._resizeTo(this._capacity << 1);
78
}
79
};
80
81
Queue.prototype._resizeTo = function (capacity) {
82
var oldCapacity = this._capacity;
83
this._capacity = capacity;
84
var front = this._front;
85
var length = this._length;
86
var moveItemsCount = (front + length) & (oldCapacity - 1);
87
arrayMove(this, 0, this, oldCapacity, moveItemsCount);
88
};
89
90
module.exports = Queue;
91
92