Skip to content

Commit ce74da0

Browse files
committed
Add clear & contains method & add comments to interface
1 parent adbc262 commit ce74da0

File tree

5 files changed

+208
-50
lines changed

5 files changed

+208
-50
lines changed

src/index.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class PriorityQueue<T> implements IPriorityQueue<T> {
9191
* Checks whether the queue is empty.
9292
*/
9393
public empty() {
94-
return this._queue.length === 0;
94+
return !this._queue.length;
9595
}
9696

9797
/**
@@ -101,15 +101,54 @@ export class PriorityQueue<T> implements IPriorityQueue<T> {
101101
return [...this._queue];
102102
}
103103

104+
/**
105+
* Removes all of the elements from this priority queue.
106+
*/
107+
public clear() {
108+
this._queue = [];
109+
}
110+
111+
/**
112+
* Returns true if this queue contains the specified element.
113+
* @param value
114+
* @param comparator
115+
*/
116+
public contains(value: T, comparator?: (item: T) => boolean) {
117+
if (!this._queue.length) return false;
118+
119+
const func = comparator || ((item: T): boolean => item === value);
120+
121+
const mid = Math.floor(this._queue.length / 2);
122+
let childIndex1: number;
123+
let childIndex2: number;
124+
let index = 0;
125+
126+
while (index <= mid - 1) {
127+
childIndex1 = 2 * index + 1;
128+
childIndex2 = 2 * index + 2;
129+
130+
if (
131+
(this._queue[index] && func(this._queue[index])) ||
132+
(this._queue[childIndex1] && func(this._queue[childIndex1])) ||
133+
(this._queue[childIndex2] && func(this._queue[childIndex2]))
134+
) {
135+
return true;
136+
}
137+
138+
index++;
139+
}
140+
return false;
141+
}
142+
104143
/**
105144
* Compare parent value and children value and swap them if conditions are satisfied
106145
* @param index
107146
*/
108147
private _heapify(index: number) {
109148
const mid = Math.floor(this._queue.length / 2);
110-
let childIndex1;
111-
let childIndex2;
112-
let swapIndex;
149+
let childIndex1: number;
150+
let childIndex2: number;
151+
let swapIndex: number;
113152

114153
while (index <= mid - 1) {
115154
childIndex1 = 2 * index + 1;

test/fixtures/test-case.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const testCase1 = [2, 7, 4, 1, 8, 1];
22
export const testCase2 = [4, 3, 4, 3, 2];
3+
export const testCase4 = [1, 2, 3, 4, 4, 5, 6, 7, 9];
34

45
export const testCase3 = [
56
{ text: 'a', value: 2 },

test/priority-queue.spec.ts

Lines changed: 122 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { PriorityQueue } from "../src";
2-
import { IPriorityQueue } from "../typings/priority-queue";
3-
import { testCase1, testCase2, testCase3 } from "./fixtures/test-case";
1+
import { PriorityQueue } from '../src';
2+
import { IPriorityQueue } from '../typings/priority-queue';
3+
import {
4+
testCase1,
5+
testCase2,
6+
testCase3,
7+
testCase4,
8+
} from './fixtures/test-case';
49

5-
describe("testing priority queue", () => {
10+
describe('testing priority queue', () => {
611
let p: IPriorityQueue<number>;
712

8-
describe("max priority queue", () => {
13+
describe('max priority queue', () => {
914
beforeEach(() => {
1015
p = new PriorityQueue();
1116
});
@@ -14,7 +19,7 @@ describe("testing priority queue", () => {
1419
p = null;
1520
});
1621

17-
it("should return max heap of test case 1", () => {
22+
it('should return max heap of test case 1', () => {
1823
for (let i = 0; i < testCase1.length; i++) {
1924
p.push(testCase1[i]);
2025
}
@@ -23,7 +28,7 @@ describe("testing priority queue", () => {
2328
expect(actual).toEqual(expected);
2429
});
2530

26-
it("should return max heap of test case 2", () => {
31+
it('should return max heap of test case 2', () => {
2732
for (let i = 0; i < testCase2.length; i++) {
2833
p.push(testCase2[i]);
2934
}
@@ -32,7 +37,7 @@ describe("testing priority queue", () => {
3237
expect(actual).toEqual(expected);
3338
});
3439

35-
it("should get the max value without remove", () => {
40+
it('should get the max value without remove', () => {
3641
for (let i = 0; i < testCase2.length; i++) {
3742
p.push(testCase2[i]);
3843
}
@@ -44,7 +49,7 @@ describe("testing priority queue", () => {
4449
expect(actual).toEqual(expected);
4550
});
4651

47-
it("should extract the max value & rebuild max heap of test case 2", () => {
52+
it('should extract the max value & rebuild max heap of test case 2', () => {
4853
for (let i = 0; i < testCase2.length; i++) {
4954
p.push(testCase2[i]);
5055
}
@@ -58,7 +63,7 @@ describe("testing priority queue", () => {
5863
expect(actual).toEqual(expected);
5964
});
6065

61-
it("should call empty & call pop and top with empty queue", () => {
66+
it('should call empty & call pop and top with empty queue', () => {
6267
for (let i = 0; i < testCase1.length; i++) {
6368
p.push(testCase1[i]);
6469
}
@@ -77,10 +82,19 @@ describe("testing priority queue", () => {
7782
item = p.pop();
7883
expect(item).toEqual(null);
7984
});
85+
86+
it('should clear the queue', () => {
87+
for (let i = 0; i < testCase1.length; i++) {
88+
p.push(testCase1[i]);
89+
}
90+
expect(p.size()).toEqual(6);
91+
p.clear();
92+
expect(p.size()).toEqual(0);
93+
});
8094
});
8195

82-
describe("testing with array of object", () => {
83-
it("should return max heap of test case 3", () => {
96+
describe('testing with array of object', () => {
97+
it('should return max heap of test case 3', () => {
8498
p = new PriorityQueue(function (a: any, b: any) {
8599
return a.value < b.value;
86100
});
@@ -90,17 +104,17 @@ describe("testing priority queue", () => {
90104
}
91105
const actual = p.toArray();
92106
const expected = [
93-
{ text: "e", value: 8 },
94-
{ text: "b", value: 7 },
95-
{ text: "c", value: 4 },
96-
{ text: "d", value: 1 },
97-
{ text: "a", value: 2 },
98-
{ text: "f", value: 1 },
107+
{ text: 'e', value: 8 },
108+
{ text: 'b', value: 7 },
109+
{ text: 'c', value: 4 },
110+
{ text: 'd', value: 1 },
111+
{ text: 'a', value: 2 },
112+
{ text: 'f', value: 1 },
99113
];
100114
expect(actual).toEqual(expected);
101115
});
102116

103-
it("should return min heap of test case 3", () => {
117+
it('should return min heap of test case 3', () => {
104118
p = new PriorityQueue(function (a: any, b: any) {
105119
return a.value > b.value;
106120
});
@@ -110,12 +124,12 @@ describe("testing priority queue", () => {
110124
}
111125
const actual = p.toArray();
112126
const expected = [
113-
{ text: "d", value: 1 },
114-
{ text: "a", value: 2 },
115-
{ text: "f", value: 1 },
116-
{ text: "b", value: 7 },
117-
{ text: "e", value: 8 },
118-
{ text: "c", value: 4 },
127+
{ text: 'd', value: 1 },
128+
{ text: 'a', value: 2 },
129+
{ text: 'f', value: 1 },
130+
{ text: 'b', value: 7 },
131+
{ text: 'e', value: 8 },
132+
{ text: 'c', value: 4 },
119133
];
120134
expect(actual).toEqual(expected);
121135
});
@@ -131,16 +145,16 @@ describe("testing priority queue", () => {
131145

132146
expect(p.size()).toEqual(testCase3.length);
133147
const max = p.pop();
134-
expect(max).toEqual({ text: "e", value: 8 });
148+
expect(max).toEqual({ text: 'e', value: 8 });
135149
expect(p.size()).toEqual(testCase3.length - 1);
136150

137151
const actual = p.toArray();
138152
const expected = [
139-
{ text: "b", value: 7 },
140-
{ text: "a", value: 2 },
141-
{ text: "c", value: 4 },
142-
{ text: "d", value: 1 },
143-
{ text: "f", value: 1 },
153+
{ text: 'b', value: 7 },
154+
{ text: 'a', value: 2 },
155+
{ text: 'c', value: 4 },
156+
{ text: 'd', value: 1 },
157+
{ text: 'f', value: 1 },
144158
];
145159
expect(actual).toEqual(expected);
146160
});
@@ -156,22 +170,22 @@ describe("testing priority queue", () => {
156170

157171
expect(p.size()).toEqual(testCase3.length);
158172
const min = p.pop();
159-
expect(min).toEqual({ text: "d", value: 1 });
173+
expect(min).toEqual({ text: 'd', value: 1 });
160174
expect(p.size()).toEqual(testCase3.length - 1);
161175

162176
const actual = p.toArray();
163177
const expected = [
164-
{ text: "f", value: 1 },
165-
{ text: "a", value: 2 },
166-
{ text: "c", value: 4 },
167-
{ text: "b", value: 7 },
168-
{ text: "e", value: 8 },
178+
{ text: 'f', value: 1 },
179+
{ text: 'a', value: 2 },
180+
{ text: 'c', value: 4 },
181+
{ text: 'b', value: 7 },
182+
{ text: 'e', value: 8 },
169183
];
170184
expect(actual).toEqual(expected);
171185
});
172186
});
173187

174-
describe("min priority queue", () => {
188+
describe('min priority queue', () => {
175189
beforeEach(() => {
176190
p = new PriorityQueue(function (a, b) {
177191
return a > b;
@@ -182,7 +196,7 @@ describe("testing priority queue", () => {
182196
p = null;
183197
});
184198

185-
it("should return min heap of test case 1", () => {
199+
it('should return min heap of test case 1', () => {
186200
for (let i = 0; i < testCase1.length; i++) {
187201
p.push(testCase1[i]);
188202
}
@@ -191,7 +205,7 @@ describe("testing priority queue", () => {
191205
expect(actual).toEqual(expected);
192206
});
193207

194-
it("should return min heap of test case 2", () => {
208+
it('should return min heap of test case 2', () => {
195209
for (let i = 0; i < testCase2.length; i++) {
196210
p.push(testCase2[i]);
197211
}
@@ -200,7 +214,7 @@ describe("testing priority queue", () => {
200214
expect(actual).toEqual(expected);
201215
});
202216

203-
it("should extract the min value & rebuild min heap of test case 2", () => {
217+
it('should extract the min value & rebuild min heap of test case 2', () => {
204218
for (let i = 0; i < testCase2.length; i++) {
205219
p.push(testCase2[i]);
206220
}
@@ -212,4 +226,71 @@ describe("testing priority queue", () => {
212226
expect(actual).toEqual(expected);
213227
});
214228
});
229+
230+
describe("testing 'contains' method", () => {
231+
beforeEach(() => {
232+
p = new PriorityQueue();
233+
});
234+
235+
afterEach(() => {
236+
p = null;
237+
});
238+
239+
it('should return true - with array of numbers', () => {
240+
for (let i = 0; i < testCase4.length; i++) {
241+
p.push(testCase4[i]);
242+
}
243+
expect(p.contains(9)).toEqual(true);
244+
});
245+
246+
it('should return true - with array of objects', () => {
247+
for (let i = 0; i < testCase3.length; i++) {
248+
p = new PriorityQueue(function (a: any, b: any) {
249+
return a.value < b.value;
250+
});
251+
252+
for (let i = 0; i < testCase3.length; i++) {
253+
p.push(testCase3[i] as any);
254+
}
255+
}
256+
257+
var element: any = { text: 'e', value: 8 };
258+
259+
const contain = function (item: any) {
260+
return item.value === element.value;
261+
};
262+
expect(p.contains(element, contain)).toEqual(true);
263+
});
264+
265+
it('should return false', () => {
266+
for (let i = 0; i < testCase4.length; i++) {
267+
p.push(testCase4[i]);
268+
}
269+
expect(p.contains(10)).toEqual(false);
270+
});
271+
272+
it('should return false - with array of objects', () => {
273+
for (let i = 0; i < testCase3.length; i++) {
274+
p = new PriorityQueue(function (a: any, b: any) {
275+
return a.value < b.value;
276+
});
277+
278+
for (let i = 0; i < testCase3.length; i++) {
279+
p.push(testCase3[i] as any);
280+
}
281+
}
282+
283+
var element: any = { text: 'fff', value: 100 };
284+
285+
const contain = function (item: any) {
286+
return item.value === element.value;
287+
};
288+
289+
expect(p.contains(element, contain)).toEqual(false);
290+
});
291+
292+
it('should return false - with empty queue', () => {
293+
expect(p.contains(10)).toEqual(false);
294+
});
295+
});
215296
});

tsconfig.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"files": ["src/index.ts"],
2+
"files": [
3+
"src/index.ts"
4+
],
35
"compilerOptions": {
46
"outDir": "./dist",
57
"noImplicitAny": false,
@@ -9,8 +11,16 @@
911
"allowJs": true,
1012
"moduleResolution": "node",
1113
"esModuleInterop": true,
12-
"lib": ["ES2015"]
14+
"lib": [
15+
"ES2015",
16+
"DOM"
17+
]
1318
},
14-
"include": ["src/**/*"],
15-
"exclude": ["node_modules", "**/*.spec.ts"]
16-
}
19+
"include": [
20+
"src/**/*"
21+
],
22+
"exclude": [
23+
"node_modules",
24+
"**/*.spec.ts"
25+
]
26+
}

0 commit comments

Comments
 (0)