vendor PriorityQueue.js

fetched from https://github.com/augustohp/Priority-Queue-NodeJS @ afae37d40
This commit is contained in:
Marc Fournier 2013-12-03 22:36:41 +01:00
parent c0fea9bab0
commit d2271e03ee
2 changed files with 159 additions and 0 deletions

View file

@ -0,0 +1,158 @@
/**
* Object to be appended in the priority queue.
*
* @class
* @param mixed v
* @param integer p
* @author Augusto Pascutti
*/
var QueueItem = function(v, p) {
this.value = v;
this.priority = p;
};
/**
* Priority queue class.
*
* @class
* @param Function[optional] c Compare function to be used
* @param integer[optional] m Max number of elements to hold
* @author Augusto Pascutti
*/
var PriorityQueue = function(c, m) { this.init(c, m) };
PriorityQueue.prototype = {
_queue: [],
_compare: undefined,
_size: 0,
/**
* Priority queue class constructor.
*
* @class
* @param Function[optional] compare_function Compare function to be used
* @param integer[optional] maximum_size Max number of elements to hold
*/
init: function(compare_function, maximum_size) {
this._compare = compare_function || undefined;
this._size = maximum_size || 0 ;
this.reset();
},
/**
* Pushes something to the priority queue.
*
* @param mixed value
* @param integer priority
* @return void
*/
push: function(value, priority) {
this._queue.push(new QueueItem(value, priority));
this._queue.sort(this.compare());
this._maitain();
},
/**
* Removes the most important item and return its value.
*
* @return mixed
*/
pop: function() {
item = this._queue.shift();
this._maitain();
return (item) ? item.value : undefined;
},
/**
* Returns most important item value from this queue, without removing it.
*
* @return mixed
*/
top: function() {
item = this._queue[0];
return (item) ? item.value : undefined;
},
/**
* Removes the less important item and return its value.
*
* @return mixed
*/
shift: function() {
item = this._queue.pop();
this._maitain();
return (item) ? item.value : undefined;
},
/**
* Returns the less important item value, without removing it.
*
* @return mixed
*/
bottom: function() {
idx = this.length-1;
item = this._queue[idx];
return (item) ? item.value : undefined;
},
/**
* Returns the ordered queue as an Array.
*
* @return Array
*/
getArray: function() {
return this._queue || new Array();
},
/**
* Resets the queue.
*
* @return void
*/
reset: function() {
this._queue = [];
this._maitain();
},
/**
* Returns the compare function.
* If no compare function is set, defines a default one.
*
* @return Function
*/
compare: function() {
if (!this._compare) {
this._compare = function(a,b) {
return b.priority - a.priority;
};
}
return this._compare;
},
/**
* Defines a fixed size to the queue.
* Zero for no limit and any other number to set it as the highest number
* of items allowed in this queue.
*
* @param integer i
* @return void
*/
size: function(i) {
this._size = i;
},
/**
* Keeps the size of the queue by removing the less important item of it and length
* information atribute.
*
* @return void
*/
_maitain: function() {
this.length = this._queue.length;
if ( this._size == 0 ) return;
while (this._size < this.length) {
this.shift();
}
},
};
module.exports = PriorityQueue;

View file

@ -24,6 +24,7 @@
<script src="vendor/flot/jquery.flot.canvas.min.js"></script>
<script src="vendor/flot/jquery.flot.time.min.js"></script>
<script src="vendor/flot/jquery.flot.stack.min.js"></script>
<script src="vendor/PriorityQueue.js"></script>
<script> // turn underscore templates into mustache style templates
_.templateSettings = {
evaluate : /\{\%([\s\S]+?)\%\}/g, // {% eval(js); %}