Skip to content

Commit

Permalink
add .contains
Browse files Browse the repository at this point in the history
  • Loading branch information
eyas-ranjous committed Jul 13, 2024
1 parent 3428a49 commit c20eb81
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 0 deletions.
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

0 comments on commit c20eb81

Please sign in to comment.