Skip to content

Commit 4b66c7d

Browse files
Merge pull request #8 from davidnguyen179/feature/add_contains_clear_method
Add clear & contains method & add comments to interface
2 parents adbc262 + 1511c78 commit 4b66c7d

File tree

6 files changed

+263
-56
lines changed

6 files changed

+263
-56
lines changed

README.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ _with array of numbers_
9090
const p = new PriorityQueue(function (a, b) {
9191
return a > b;
9292
});
93+
9394
p.push(2);
9495
p.push(7);
9596
p.push(4);
@@ -128,7 +129,7 @@ p.push({ text: 'f', value: 1 });
128129

129130
## API
130131

131-
### push(value)
132+
### push(value: T)
132133

133134
Add elements to queue
134135

@@ -141,7 +142,7 @@ p.push(3); // adding "3" to queue
141142
// The queue: [3, 1, 2]
142143
```
143144

144-
### pop
145+
### pop()
145146

146147
Extract the largest or smallest element from the queue
147148

@@ -156,7 +157,7 @@ const elmenet = p.pop(); // Output: 3
156157

157158
The queue looks like this `[2, 1]`
158159

159-
### top
160+
### top()
160161

161162
Peek the element (get the largest or smallest element without removing it from queue)
162163

@@ -171,7 +172,7 @@ const elmenet = p.pop(); // Output: 3
171172
// The queue is remained the same
172173
```
173174

174-
### size
175+
### size()
175176

176177
Get the size of the queue
177178

@@ -185,14 +186,14 @@ p.push(4); // adding "4" to queue
185186
const length = p.size(); // Output: 4
186187
```
187188

188-
### empty
189+
### empty()
189190

190191
Check whether the queue is empty or not.
191192

192193
- true: if the queue is empty
193194
- false: if the queue has data
194195

195-
### toArray
196+
### toArray()
196197

197198
Extract queue to array
198199

@@ -205,6 +206,54 @@ p.push(3); // adding "3" to queue
205206
const array = p.toArray(); // Output: [3, 1, 2]
206207
```
207208

209+
### clear()
210+
211+
Removes all of the elements from this priority queue.
212+
213+
### contains(value: T, comparator?: (item: T) => boolean)
214+
215+
Returns true if this queue contains the specified element.
216+
217+
- true: if the element exists in queue
218+
- false: if the element does not exist in queue
219+
220+
_with array of numbers_
221+
222+
```ts
223+
const p = new PriorityQueue();
224+
p.push(2);
225+
p.push(7);
226+
p.push(4);
227+
p.push(1);
228+
p.push(8);
229+
p.push(1);
230+
231+
p.contains(8); // true
232+
p.contains(100); // false
233+
```
234+
235+
_with array of objects_
236+
237+
```ts
238+
const p = new PriorityQueue(function (a, b) {
239+
return a.value > b.value;
240+
});
241+
242+
p.push({ text: 'a', value: 2 });
243+
p.push({ text: 'b', value: 7 });
244+
p.push({ text: 'c', value: 4 });
245+
p.push({ text: 'd', value: 1 });
246+
p.push({ text: 'e', value: 8 });
247+
p.push({ text: 'f', value: 1 });
248+
249+
function callback(item: any) {
250+
return item.value === element.value;
251+
}
252+
253+
p.contains(8, callback); // true
254+
p.contains(100, callback); // false
255+
```
256+
208257
## Running time
209258

210259
You can check the performance of the package here: [https://jsperf.com/p-queue-ts](https://jsperf.com/p-queue-ts)

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 },

0 commit comments

Comments
 (0)