add update() function to PriorityQueue

This commit is contained in:
Marc Fournier 2013-12-08 13:37:23 +01:00
parent 8217b0697a
commit e60ec9a751

View file

@ -51,6 +51,35 @@ PriorityQueue.prototype = {
this._maitain();
},
/**
* Update priority of something in the queue. Will silently add it if it
* doesn't already exist.
*
* @param mixed value
* @param integer priority
* @return void
*/
update: function(value, priority) {
known = false;
idx = 0;
this._queue.forEach(function() {
if (JSON.stringify(this._queue[idx].value) === JSON.stringify(value)) {
this._queue[idx].priority = priority;
known = true;
return;
}
idx++;
}, this);
if (!known) {
this._queue.push(new QueueItem(value, priority));
}
this._queue.sort(this.compare());
this._maitain();
},
/**
* Removes the most important item and return its value.
*