Skip to content

Latest commit

 

History

History
141 lines (84 loc) · 2.97 KB

priority-buffer.md

File metadata and controls

141 lines (84 loc) · 2.97 KB

Table of Contents

insertInSortOrder

Simplest O(n/2) insertion possible.

Parameters

  • list List<Type> list where value should be inserted
  • comparator CompareFunction<Type> provides sort order of list
  • value Type value to be inserted

CompareFunction

Type: Function

Parameters

  • a Type first value to compare
  • b 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

PriorityBuffer

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.

Parameters

  • 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, default 100)

buffer

length

Getter to provide current number of elements in buffer. Can never be larger than capacity.

Returns number

clear

Empties the buffer. After this operation buffer.length === 0

back

Returns the lowest priority element in the buffer.

Returns Type the lowest priority element, or undefined if empty

front

Returns the highest priority value.

Returns Type the the highest priority value or undefined if empty

push

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.

Parameters

  • value Type value to push

Returns number the current length of the buffer

shift

Removes the highest priority value and returns it.

Returns Type the value removed from the queue or undefined if empty.

iterator

Iterator that goes from highest priority to lowest priority.

Returns IterableIterator<Type> iterates from highest priority to lowest priority