priority-deque
v1.1.0
Published
A double-ended priority queue based on min-max heaps.
Downloads
18
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-negativelimitis 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: numberIndicates how many total items are currently stored in the deque.push(...elements: T[])Adds new elements to the deque.pop(): T | undefinedRemoves and returns the minimum element from the deque, orundefinediflengthis zero.shift(): T | undefinedRemoves and returns the maximum element from the deque, orundefinediflengtis zero.unshift(...elements: T[])Synonym forpush().map<U>(fn: (e: T) => U, compare?: (a: U, b: U) => number): PriorityDeque<U>Creates a newPriorityDequewith 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 newPriorityDequewith 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 newPriorityDequewith 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): booleanDetermines whether or not the deque contains a specific element, via===comparison.some(fn: (e: T) => boolean): booleanDetermines whether or not any element of the deque satisfies the given predicate.every(fn: (e: T) => boolean): booleanDetermines whether or not all elements of the deque satisfy the given predicate.find(fn: (e: T) => boolean): T | undefinedReturns an element of the deque which satisfies the given predicate, orundefinedif 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 | undefinedReturns the minimum element of the deque, orundefinediflengthis zero.findMax(): T | undefinedReturns the maximum element of the deque, orundefinediflengthis zero.replaceMin(e: T): T | undefinedRemoves and returns the minimum element of the deque, orundefinediflengthis zero, and inserts the new elemente. This is more efficient than callingpop()andpush(e)separately.replaceMax(e: T): T | undefinedRemoves and returns the maximum element of the deque, orundefinediflengthis zero, and inserts the new elemente. This is more efficient than callingshift()andpush(e)separately.remove(e: T): booleanRemoves the elementefrom the deque and returns true ifeis in the deque; returns false otherwise.replace(a: T, b: T): booleanRemoves the elementafrom the deque, inserts the elementb, and returns true ifais in the deque. Ifais not in the deque,bis not inserted, and false is returned. This is more efficient than callingremove(a)andpush(b)separately.
