@datastructures-js/priority-queue
Advanced tools
Comparing version 6.3.1 to 6.3.2
@@ -8,2 +8,7 @@ # Changelog | ||
## [Unreleased] | ||
## [6.3.2] - 2025-01-05 | ||
### Fixed | ||
- ts types. | ||
## [6.3.1] - 2024-01-23 | ||
@@ -10,0 +15,0 @@ ### Fixed |
{ | ||
"name": "@datastructures-js/priority-queue", | ||
"version": "6.3.1", | ||
"version": "6.3.2", | ||
"description": "A heap-based implementation of priority queue in javascript with typescript support.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,2 +26,3 @@ # @datastructures-js/priority-queue | ||
* [dequeue (pop)](#dequeue-pop) | ||
* [contains](#contains) | ||
* [remove](#remove) | ||
@@ -252,2 +253,12 @@ * [isEmpty](#isEmpty) | ||
### contains | ||
checks if the queue contains an element that meet a criteria in O(n*log(n)) runtime. | ||
```js | ||
carsQueue.contains((car) => car.price === 50000); // true | ||
carsQueue.contains((car) => car.price === 200000); // false | ||
numbersQueue.contains((n) => n === 4); // true | ||
numbersQueue.contains((n) => n === 10); // false | ||
``` | ||
### remove | ||
@@ -254,0 +265,0 @@ removes all elements that meet a criteria in O(n*log(n)) runtime and returns a list of the removed elements. |
@@ -8,9 +8,10 @@ import { MaxHeap, IGetCompareValue } from '@datastructures-js/heap'; | ||
isEmpty(): boolean; | ||
front(): T; | ||
back(): T; | ||
front(): T | null; | ||
back(): T | null; | ||
enqueue(value: T): MaxPriorityQueue<T>; | ||
push(value: T): MaxPriorityQueue<T>; | ||
dequeue(): T; | ||
pop(): T; | ||
dequeue(): T | null; | ||
pop(): T | null; | ||
remove(cb: (value: T) => boolean): T[]; | ||
contains(cb: (value: T) => boolean): boolean; | ||
toArray(): T[]; | ||
@@ -17,0 +18,0 @@ clear(): void; |
@@ -109,2 +109,28 @@ /** | ||
/** | ||
* Checks if the queue contains an element that matches a criteria | ||
* @public | ||
* @param {function} cb | ||
* @returns {boolean} | ||
*/ | ||
contains(cb) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('MaxPriorityQueue contains expects a callback'); | ||
} | ||
let found = false; | ||
const dequeued = []; | ||
while (!this.isEmpty()) { | ||
const popped = this.pop(); | ||
dequeued.push(popped); | ||
if (cb(popped)) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
dequeued.forEach((val) => this.push(val)); | ||
return found; | ||
} | ||
/** | ||
* Returns the number of elements in the queue | ||
@@ -111,0 +137,0 @@ * @public |
@@ -8,9 +8,10 @@ import { MinHeap, IGetCompareValue } from '@datastructures-js/heap'; | ||
isEmpty(): boolean; | ||
front(): T; | ||
back(): T; | ||
front(): T | null; | ||
back(): T | null; | ||
enqueue(value: T): MinPriorityQueue<T>; | ||
push(value: T): MinPriorityQueue<T>; | ||
dequeue(): T; | ||
pop(): T; | ||
dequeue(): T | null; | ||
pop(): T | null; | ||
remove(cb: (value: T) => boolean): T[]; | ||
contains(cb: (value: T) => boolean): boolean; | ||
toArray(): T[]; | ||
@@ -17,0 +18,0 @@ clear(): void; |
@@ -108,2 +108,28 @@ /** | ||
/** | ||
* Checks if the queue contains an element that matches a criteria | ||
* @public | ||
* @param {function} cb | ||
* @returns {boolean} | ||
*/ | ||
contains(cb) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('MinPriorityQueue contains expects a callback'); | ||
} | ||
let found = false; | ||
const dequeued = []; | ||
while (!this.isEmpty()) { | ||
const popped = this.pop(); | ||
dequeued.push(popped); | ||
if (cb(popped)) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
dequeued.forEach((val) => this.push(val)); | ||
return found; | ||
} | ||
/** | ||
* Returns the number of elements in the queue | ||
@@ -110,0 +136,0 @@ * @public |
@@ -8,9 +8,10 @@ import { ICompare } from '@datastructures-js/heap'; | ||
isEmpty(): boolean; | ||
front(): T; | ||
back(): T; | ||
front(): T | null; | ||
back(): T | null; | ||
enqueue(value: T): PriorityQueue<T>; | ||
push(value: T): PriorityQueue<T>; | ||
dequeue(): T; | ||
pop(): T; | ||
dequeue(): T | null; | ||
pop(): T | null; | ||
remove(cb: (value: T) => boolean): T[]; | ||
contains(cb: (value: T) => boolean): boolean; | ||
toArray(): T[]; | ||
@@ -17,0 +18,0 @@ clear(): void; |
@@ -109,2 +109,28 @@ /** | ||
/** | ||
* Checks if the queue contains an element that matches a criteria | ||
* @public | ||
* @param {function} cb | ||
* @returns {boolean} | ||
*/ | ||
contains(cb) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('PriorityQueue contains expects a callback'); | ||
} | ||
let found = false; | ||
const dequeued = []; | ||
while (!this.isEmpty()) { | ||
const popped = this.pop(); | ||
dequeued.push(popped); | ||
if (cb(popped)) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
dequeued.forEach((val) => this.push(val)); | ||
return found; | ||
} | ||
/** | ||
* Returns the number of elements in the queue | ||
@@ -111,0 +137,0 @@ * @public |
28591
600
373