forked from ge0ffray/fixedsize-fifo-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgqueue-1.3.js
95 lines (94 loc) · 2.47 KB
/
gqueue-1.3.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
49
50
51
52
53
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
94
95
/**
* GQueue
* Fixed size FIFO queue class providing associative access. Can also be used as a Circular Buffer.
* @author Geoffray Warnants <http://www.geoffray.be>
* @version 1.3.20120427
*/
/**
* Creates an empty queue
* @param {int} maxlength
* @param {Boolean} circular If true, the queue as will be defined as a "Circular Buffer".
* @constructor
*/
function GQueue(size, circular) {
if (!(this instanceof GQueue)) {
return new GQueue(size, circular);
}
this.data = {};
this.circular = Boolean(circular);
this.maxlength = parseInt(size, 10);
if (isNaN(this.maxlength)) {
this.maxlength = 0;
}
this.length = 0;
}
GQueue.prototype = {
/**
* Adds new element to the end of the queue
* @param {String} k Key
* @param {Object} v Value
* @return {int} The new length, or -1 if the queue is full
*/
push: function (k, v) {
if (typeof this.data[k] === "undefined") {
if (this.length < this.maxlength) {
this.length++;
} else if (this.circular) {
this.pop();
this.length++;
} else {
return -1;
}
}
this.data[k] = v;
return this.length;
},
/**
* Removes the last element of the queue
* @return {Object} The removed element, or null if none.
*/
pop: function () {
for (var i in this.data) {
var value = this.data[i];
delete this.data[i];
this.length--;
return value;
}
return null;
},
/**
* Checks if a key exists in the queue.
* @param {String} k Searched key
* @return {Boolean}
*/
has: function (k) {
return typeof this.data[k] !== "undefined";
},
/**
* Searches an element for a given key
* @param {String} k Searched key
* @return {Object} The found element, or null if none.
*/
find: function (k) {
return (typeof this.data[k] !== "undefined") ? this.data[k] : null;
},
/**
* Unset an element for a given key
* @param {String} k Element key
* @return {Void}
*/
unset: function (k) {
if (typeof this.data[k] !== "undefined") {
delete this.data[k];
this.length--;
}
},
/**
* Clear all elements
* @return {Void}
*/
clear: function () {
this.data = {};
this.length = 0;
}
};