Skip to content

add .contains #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A heap-based implementation of priority queue in javascript with typescript supp
* [front](#front)
* [back](#back)
* [dequeue (pop)](#dequeue-pop)
* [contains](#contains)
* [remove](#remove)
* [isEmpty](#isEmpty)
* [size](#size)
Expand Down Expand Up @@ -248,6 +249,16 @@ console.log(bidsQueue.pop()); // { id: 5, value: 12000 }
console.log(bidsQueue.pop()); // { id: 7, value: 8000 }
```

### 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
removes all elements that meet a criteria in O(n*log(n)) runtime and returns a list of the removed elements.

Expand Down
1 change: 1 addition & 0 deletions src/maxPriorityQueue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class MaxPriorityQueue<T> {
dequeue(): T;
pop(): T;
remove(cb: (value: T) => boolean): T[];
contains(cb: (value: T) => boolean): boolean;
toArray(): T[];
clear(): void;
static fromArray<T>(values: T[], getCompareValue?: IGetCompareValue<T>): MaxPriorityQueue<T>;
Expand Down
26 changes: 26 additions & 0 deletions src/maxPriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ class MaxPriorityQueue {
return removed;
}

/**
* 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
* @public
Expand Down
1 change: 1 addition & 0 deletions src/minPriorityQueue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class MinPriorityQueue<T> {
dequeue(): T;
pop(): T;
remove(cb: (value: T) => boolean): T[];
contains(cb: (value: T) => boolean): boolean;
toArray(): T[];
clear(): void;
static fromArray<T>(values: T[], getCompareValue?: IGetCompareValue<T>): MinPriorityQueue<T>;
Expand Down
26 changes: 26 additions & 0 deletions src/minPriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ class MinPriorityQueue {
return removed;
}

/**
* 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
* @public
Expand Down
1 change: 1 addition & 0 deletions src/priorityQueue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class PriorityQueue<T> {
dequeue(): T;
pop(): T;
remove(cb: (value: T) => boolean): T[];
contains(cb: (value: T) => boolean): boolean;
toArray(): T[];
clear(): void;
static fromArray<T>(values: T[], compare: ICompare<T>): PriorityQueue<T>;
Expand Down
26 changes: 26 additions & 0 deletions src/priorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ class PriorityQueue {
return removed;
}

/**
* 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
* @public
Expand Down
9 changes: 9 additions & 0 deletions test/PriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ describe('PriorityQueue', () => {
});
});

describe('contains', () => {
it('checks if the queue contains an element', () => {
const maxQ = new PriorityQueue(charComparator);
charValues.forEach((value) => maxQ.push(value));
expect(maxQ.contains((c) => c.id === 'z')).to.equal(true);
expect(maxQ.contains((c) => c.id === 'y')).to.equal(false);
});
});

describe('remove', () => {
it('remove elements that match a criteria', () => {
const testArr = [20, 30, 40, 50, 80, 90];
Expand Down
9 changes: 9 additions & 0 deletions test/maxPriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ describe('MaxPriorityQueue', () => {
});
});

describe('contains', () => {
it('checks if the queue contains an element', () => {
const testArr = [90, 80, 50, 40, 30, 20];
const qTest = MaxPriorityQueue.fromArray(testArr.slice());
expect(qTest.contains((n) => n === 50)).to.equal(true);
expect(qTest.contains((n) => n === 100)).to.equal(false);
});
});

describe('remove', () => {
it('remove elements that match a criteria', () => {
const testArr = [20, 30, 40, 50, 80, 90];
Expand Down
9 changes: 9 additions & 0 deletions test/minPriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ describe('MinPriorityQueue', () => {
});
});

describe('contains', () => {
it('checks if the queue contains an element', () => {
const testArr = [90, 80, 50, 40, 30, 20];
const qTest = MinPriorityQueue.fromArray(testArr.slice());
expect(qTest.contains((n) => n === 50)).to.equal(true);
expect(qTest.contains((n) => n === 100)).to.equal(false);
});
});

describe('remove', () => {
it('remove elements that match a criteria', () => {
const testArr = [20, 30, 40, 50, 80, 90];
Expand Down
Loading
close