@datastructures-js/priority-queue
Advanced tools
Comparing version 6.2.0 to 6.3.0
@@ -8,2 +8,6 @@ # Changelog | ||
## [Unreleased] | ||
## [6.3.0] - 2023-05-30 | ||
### Added | ||
- `.remove(cb)` to remove all elements that meet a criteria. | ||
## [6.2.0] - 2023-01-16 | ||
@@ -10,0 +14,0 @@ ### Added |
{ | ||
"name": "@datastructures-js/priority-queue", | ||
"version": "6.2.0", | ||
"version": "6.3.0", | ||
"description": "A heap-based implementation of priority queue in javascript with typescript support.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,2 +21,3 @@ # @datastructures-js/priority-queue | ||
* [dequeue (pop)](#dequeue-pop) | ||
* [remove](#remove) | ||
* [isEmpty](#isEmpty) | ||
@@ -246,2 +247,13 @@ * [size](#size) | ||
### remove | ||
removes all elements that meet a criteria in O(n*log(n)) runtime and returns a list of the removed elements. | ||
```js | ||
carsQueue.remove((car) => car.price === 35000); // [{ year: 2013, price: 35000 }] | ||
numbersQueue.remove((n) => n === 4); // [4] | ||
bidsQueue.remove((bid) => bid.id === 3); // [{ id: 3, value: 1000 }] | ||
``` | ||
### isEmpty | ||
@@ -260,5 +272,5 @@ checks if the queue is empty. | ||
```js | ||
console.log(carsQueue.size()); // 4 | ||
console.log(numbersQueue.size()); // 4 | ||
console.log(bidsQueue.size()); // 4 | ||
console.log(carsQueue.size()); // 3 | ||
console.log(numbersQueue.size()); // 3 | ||
console.log(bidsQueue.size()); // 3 | ||
``` | ||
@@ -275,3 +287,2 @@ | ||
{ year: 2013, price: 30000 }, | ||
{ year: 2013, price: 35000 }, | ||
{ year: 2010, price: 2000 } | ||
@@ -281,3 +292,3 @@ ] | ||
console.log(numbersQueue.toArray()); // [ 0, 3, 4, 5 ] | ||
console.log(numbersQueue.toArray()); // [ 0, 3, 5 ] | ||
@@ -289,3 +300,2 @@ console.log(bidsQueue.toArray()); | ||
{ id: 4, value: 1500 }, | ||
{ id: 3, value: 1000 }, | ||
{ id: 1, value: 1000 } | ||
@@ -304,3 +314,2 @@ ] | ||
{ year: 2013, price: 30000 }, | ||
{ year: 2013, price: 35000 }, | ||
{ year: 2010, price: 2000 } | ||
@@ -311,3 +320,3 @@ ] | ||
console.log([...numbersQueue]); // [ 0, 3, 4, 5 ] | ||
console.log([...numbersQueue]); // [ 0, 3, 5 ] | ||
console.log(numbersQueue.size()); // 0 | ||
@@ -321,3 +330,2 @@ | ||
{ id: 4, value: 1500 }, | ||
{ id: 3, value: 1000 }, | ||
{ id: 1, value: 1000 } | ||
@@ -324,0 +332,0 @@ */ |
@@ -14,2 +14,3 @@ import { MaxHeap, IGetCompareValue } from '@datastructures-js/heap'; | ||
pop(): T; | ||
remove(cb: (value: T) => boolean): T[]; | ||
toArray(): T[]; | ||
@@ -16,0 +17,0 @@ clear(): void; |
@@ -83,2 +83,28 @@ /** | ||
/** | ||
* Removes all elements that match a criteria in the callback | ||
* @public | ||
* @param {function} cb | ||
* @returns {array} | ||
*/ | ||
remove(cb) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('MaxPriorityQueue remove expects a callback'); | ||
} | ||
const removed = []; | ||
const dequeued = []; | ||
while (!this.isEmpty()) { | ||
const popped = this.pop(); | ||
if (cb(popped)) { | ||
removed.push(popped); | ||
} else { | ||
dequeued.push(popped); | ||
} | ||
} | ||
dequeued.forEach((val) => this.push(val)); | ||
return removed; | ||
} | ||
/** | ||
* Returns the number of elements in the queue | ||
@@ -85,0 +111,0 @@ * @public |
@@ -14,2 +14,3 @@ import { MinHeap, IGetCompareValue } from '@datastructures-js/heap'; | ||
pop(): T; | ||
remove(cb: (value: T) => boolean): T[]; | ||
toArray(): T[]; | ||
@@ -16,0 +17,0 @@ clear(): void; |
@@ -82,2 +82,28 @@ /** | ||
/** | ||
* Removes all elements that match a criteria in the callback | ||
* @public | ||
* @param {function} cb | ||
* @returns {array} | ||
*/ | ||
remove(cb) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('MinPriorityQueue remove expects a callback'); | ||
} | ||
const removed = []; | ||
const dequeued = []; | ||
while (!this.isEmpty()) { | ||
const popped = this.pop(); | ||
if (cb(popped)) { | ||
removed.push(popped); | ||
} else { | ||
dequeued.push(popped); | ||
} | ||
} | ||
dequeued.forEach((val) => this.push(val)); | ||
return removed; | ||
} | ||
/** | ||
* Returns the number of elements in the queue | ||
@@ -84,0 +110,0 @@ * @public |
@@ -14,2 +14,3 @@ import { ICompare } from '@datastructures-js/heap'; | ||
pop(): T; | ||
remove(cb: (value: T) => boolean): T[]; | ||
toArray(): T[]; | ||
@@ -16,0 +17,0 @@ clear(): void; |
@@ -83,2 +83,28 @@ /** | ||
/** | ||
* Removes all elements that match a criteria in the callback | ||
* @public | ||
* @param {function} cb | ||
* @returns {array} | ||
*/ | ||
remove(cb) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('PriorityQueue remove expects a callback'); | ||
} | ||
const removed = []; | ||
const dequeued = []; | ||
while (!this.isEmpty()) { | ||
const popped = this.pop(); | ||
if (cb(popped)) { | ||
removed.push(popped); | ||
} else { | ||
dequeued.push(popped); | ||
} | ||
} | ||
dequeued.forEach((val) => this.push(val)); | ||
return removed; | ||
} | ||
/** | ||
* Returns the number of elements in the queue | ||
@@ -85,0 +111,0 @@ * @public |
25836
528
357