Class buckets.PriorityQueue
In a priority queue each element is associated with a "priority", elements are dequeued in highest-priority-first order (the elements with the highest priority are dequeued first). Priority Queues are implemented as heaps. If the inserted elements are custom objects a compare function must be provided, otherwise the <=, === and >= operators are used to compare object priority.
function compare(a, b) { if (a is less than b by some ordering criterion) { return -1; } if (a is greater than b by the ordering criterion) { return 1; } // a must be equal to b return 0; }
Defined in: <../buckets.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
buckets.PriorityQueue(compareFunction)
Creates an empty priority queue.
|
Method Attributes | Method Name and Description |
---|---|
add(element)
Inserts the specified element into this priority queue.
|
|
clear()
Removes all of the elements from this priority queue.
|
|
contains(element)
Returns true if this priority queue contains the specified element.
|
|
dequeue()
Retrieves and removes the highest priority element of this queue.
|
|
enqueue(element)
Inserts the specified element into this priority queue.
|
|
forEach(callback)
Executes the provided function once for each element present in this queue in
no particular order.
|
|
isEmpty()
Checks if this priority queue is empty.
|
|
peek()
Retrieves, but does not remove, the highest priority element of this queue.
|
|
size()
Returns the number of elements in this priority queue.
|
Class Detail
buckets.PriorityQueue(compareFunction)
Creates an empty priority queue.
- Parameters:
- {function(Object|Object):number=} compareFunction
- optional function used to compare two element priorities. Must return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Method Detail
{boolean}
add(element)
Inserts the specified element into this priority queue.
- Parameters:
- {Object} element
- the element to insert.
- Returns:
- {boolean} true if the element was inserted, or false if it is undefined.
clear()
Removes all of the elements from this priority queue.
{boolean}
contains(element)
Returns true if this priority queue contains the specified element.
- Parameters:
- {Object} element
- element to search for.
- Returns:
- {boolean} true if this priority queue contains the specified element, false otherwise.
{*}
dequeue()
Retrieves and removes the highest priority element of this queue.
- Returns:
- {*} the the highest priority element of this queue, or undefined if this queue is empty.
{boolean}
enqueue(element)
Inserts the specified element into this priority queue.
- Parameters:
- {Object} element
- the element to insert.
- Returns:
- {boolean} true if the element was inserted, or false if it is undefined.
forEach(callback)
Executes the provided function once for each element present in this queue in
no particular order.
- Parameters:
- {function(Object):*} callback
- function to execute, it is invoked with one argument: the element value, to break the iteration you can optionally return false.
{boolean}
isEmpty()
Checks if this priority queue is empty.
- Returns:
- {boolean} true if and only if this priority queue contains no items; false otherwise.
{*}
peek()
Retrieves, but does not remove, the highest priority element of this queue.
- Returns:
- {*} the highest priority element of this queue, or undefined if this queue is empty.
{number}
size()
Returns the number of elements in this priority queue.
- Returns:
- {number} the number of elements in this priority queue.