Simplest O(n/2) insertion possible.
list
List<Type> list where value should be insertedcomparator
CompareFunction<Type> provides sort order of listvalue
Type value to be inserted
Type: Function
a
Type first value to compareb
Type second value to compare
Returns number three possibilities a > b, returns > 0, and b sorts before a a === b, returns 0, keep original order of a and b a < b, returns < 0, and a sorts before b
Priority buffer with highest priority elements at front, and fixed maximum length.
Buffers are expected to be short, so that the naive prioritization process is quick.
comparator
CompareFunction<Type> comparator that matches the Array.sort() comparator API. High priority items should sort before lower priority items.capacity
number maximum length of buffer (optional, default100
)
Getter to provide current number of elements in buffer. Can never be larger than capacity.
Returns number
Empties the buffer. After this operation buffer.length === 0
Returns the lowest priority element in the buffer.
Returns Type the lowest priority element, or undefined
if empty
Returns the highest priority value.
Returns Type the the highest priority value or undefined
if empty
Inserts a value into the queue. If length === capacity, the lowest priority value is discarded. If two items with the same priority are in the queue, the older one is before the newer one.
value
Type value to push
Returns number the current length of the buffer
Removes the highest priority value and returns it.
Returns Type the value removed from the queue
or undefined
if empty.
Iterator that goes from highest priority to lowest priority.
Returns IterableIterator<Type> iterates from highest priority to lowest priority