-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpriorityqueue.js
More file actions
38 lines (31 loc) · 780 Bytes
/
priorityqueue.js
File metadata and controls
38 lines (31 loc) · 780 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
37
38
function binaryInsert(arr, value, comparator) {
var low = 0,
high = arr.length - 1;
var i;
while (low <= high) {
i = Math.floor((low + high) / 2);
var comparison = comparator(arr[i], value);
if (comparison < 0) { low = i + 1; continue; };
if (comparison > 0) { high = i - 1; continue; };
low = i;
break;
}
arr.splice(low, 0, value);
};
exports.make = function(indexFunction){
var list = [];
return {
add: function(value){
binaryInsert(list, value, indexFunction);
},
pop: function(){//remove last value
return list.pop();
},
shift: function(){//remove first value
return list.shift();
},
size: function(){return list.length;},
list: function(){return [].concat(list);},
empty: function(){return list.length === 0;}
};
}