It's a simple implementation of a priority queue for Node.js. It uses binary search to keep items ordered as
they are inserted. By doing so, its performances are much more stable and attractive than the manual method of pushing
items into an array and using
How do you use it?
var biggestNumber = new PriorityQueue(); biggestNumber.push(1,2,3) biggestNumber.pop() // 3 (greater integers are more important) biggestNumber.pop(2) // [1,2] (the next "2" more important items) var bestAuthors = new PriorityQueue(); bestAuthors.push(["Douglas Adams", 100]); bestAuthors.push(["Isaac Azimov", 90]); bestAuthors.push(["J. K. Rowling", 1]); bestAuthors.pop(); // ["Douglas Adams", 100]
What are the options?
comparator: A comparator
function(a,b)that returns -1 if
a<b, +1 if
a>bor 0 if ... you know
priority: A function that returns the rank of a given item, used as the basis for comparison. Only used if
comparatorwas not provided.
capacity: The maximum number of items the queue will contain. Lower priority items will be discarded, unless ...
reversed: If true, higher priority items will be discarded instead of lower ones (only used when
What are the methods?
q.add(item[, items*])(aliased as
Adds one or more items in the queue, keeping it ordered and discarding items if necessary.
nisn't given, it removes and returns one item from the top of the queue. If
nis given, remove and return an array of
nitems from the top of the queue.
nisn't given, it removes and returns one item from the bottom of the queue. If
nis given, well ... fill the blanks.
Empty the queue
What are its properties?
q.length: its length (read-only)
q.capacity: its capacity. If set, items will be dropped when the capacity is exceeded.
q.queue: its content (read-only)
q.bottomthe top and bottom items in the queue (also read-only)