priority-deque
v1.1.0
Published
A double-ended priority queue based on min-max heaps.
Downloads
12
Maintainers
Readme
priority-deque
This package implements a double-ended priority queue, or priority deque, with optional size limits, based on an underlying min-max heap structure. This data structure provides efficient Insert, FindMin, FindMax, DeleteMin, and DeleteMax operations. The package has a single non-default export { PriorityDeque }
.
API
new PriorityDeque<T>(opts?: { compare?: (a: T, b: T) => number, limit?: number, items?: Iterable<T> })
Constructs a newPriorityDeque
. By default, there are no size limits, numbers will be compared numerically, and all other objects will be compared by converting to strings and callingString.localeCompare()
. If a finite non-negativelimit
is provided, the deque will automatically prune maximal elements to keep the total size of the structure less than or equal tolimit
.clone(): PriorityDeque<T>
Creates a shallow copy of the deque with the same size limits and comparison function in O(n) time.set(elements: Iterable<T>)
Replaces the contents of the deque with the smallest members, up to the size limit, of a new element set.append(elements: T[])
Adds new elements to the deque.clear()
Empties the deque.
Array-Like Methods
[Symbol.iterator](): IterableIterator<T>
Returns an iterator over all of the currently-stored values in the deque, suitable for use withfor(... of ...)
loops. No particular iteration order is guaranteed.readonly length: number
Indicates how many total items are currently stored in the deque.push(...elements: T[])
Adds new elements to the deque.pop(): T | undefined
Removes and returns the minimum element from the deque, orundefined
iflength
is zero.shift(): T | undefined
Removes and returns the maximum element from the deque, orundefined
iflengt
is zero.unshift(...elements: T[])
Synonym forpush()
.map<U>(fn: (e: T) => U, compare?: (a: U, b: U) => number): PriorityDeque<U>
Creates a newPriorityDeque
with the same limits as the current one by applying a mapping function to each element of the current deque. If an explicit comparison function is not provided for the new deque, the default numeric / string comparator will be used.filter(fn: (e: T) => boolean): PriorityDeque<T>
Creates a newPriorityDeque
with the same limits and comparison function as the current one but containing only a subset of the current deque's elements which satisfy the provided filtering predicate.collect<U>(fn: (e: T) => Iterable<U>, compare?: (a: U, b: U) => number): PriorityDeque<U>
Creates a newPriorityDeque
with the same limits as the current one by applying a collection function return 0 or more mapped elements to each element of the current deque. This is more efficient than callingfilter()
andmap()
in sequence. If an explicit comparison function is not provided for the new deque, the default numeric / string comparator will be used.contains(e: T): boolean
Determines whether or not the deque contains a specific element, via===
comparison.some(fn: (e: T) => boolean): boolean
Determines whether or not any element of the deque satisfies the given predicate.every(fn: (e: T) => boolean): boolean
Determines whether or not all elements of the deque satisfy the given predicate.find(fn: (e: T) => boolean): T | undefined
Returns an element of the deque which satisfies the given predicate, orundefined
if there is no such element.forEach(fn: (e: T) => void)
Executes the given callback function once for each element of the deque; no specific ordering is guaranteed.
Heap-Specific Methods
findMin(): T | undefined
Returns the minimum element of the deque, orundefined
iflength
is zero.findMax(): T | undefined
Returns the maximum element of the deque, orundefined
iflength
is zero.replaceMin(e: T): T | undefined
Removes and returns the minimum element of the deque, orundefined
iflength
is zero, and inserts the new elemente
. This is more efficient than callingpop()
andpush(e)
separately.replaceMax(e: T): T | undefined
Removes and returns the maximum element of the deque, orundefined
iflength
is zero, and inserts the new elemente
. This is more efficient than callingshift()
andpush(e)
separately.remove(e: T): boolean
Removes the elemente
from the deque and returns true ife
is in the deque; returns false otherwise.replace(a: T, b: T): boolean
Removes the elementa
from the deque, inserts the elementb
, and returns true ifa
is in the deque. Ifa
is not in the deque,b
is not inserted, and false is returned. This is more efficient than callingremove(a)
andpush(b)
separately.