@@ -90,6 +90,7 @@ _with array of numbers_
90
90
const p = new PriorityQueue (function (a , b ) {
91
91
return a > b ;
92
92
});
93
+
93
94
p .push (2 );
94
95
p .push (7 );
95
96
p .push (4 );
@@ -128,7 +129,7 @@ p.push({ text: 'f', value: 1 });
128
129
129
130
## API
130
131
131
- ### push(value)
132
+ ### push(value: T )
132
133
133
134
Add elements to queue
134
135
@@ -141,7 +142,7 @@ p.push(3); // adding "3" to queue
141
142
// The queue: [3, 1, 2]
142
143
```
143
144
144
- ### pop
145
+ ### pop()
145
146
146
147
Extract the largest or smallest element from the queue
147
148
@@ -156,7 +157,7 @@ const elmenet = p.pop(); // Output: 3
156
157
157
158
The queue looks like this ` [2, 1] `
158
159
159
- ### top
160
+ ### top()
160
161
161
162
Peek the element (get the largest or smallest element without removing it from queue)
162
163
@@ -171,7 +172,7 @@ const elmenet = p.pop(); // Output: 3
171
172
// The queue is remained the same
172
173
```
173
174
174
- ### size
175
+ ### size()
175
176
176
177
Get the size of the queue
177
178
@@ -185,14 +186,14 @@ p.push(4); // adding "4" to queue
185
186
const length = p .size (); // Output: 4
186
187
```
187
188
188
- ### empty
189
+ ### empty()
189
190
190
191
Check whether the queue is empty or not.
191
192
192
193
- true: if the queue is empty
193
194
- false: if the queue has data
194
195
195
- ### toArray
196
+ ### toArray()
196
197
197
198
Extract queue to array
198
199
@@ -205,6 +206,54 @@ p.push(3); // adding "3" to queue
205
206
const array = p .toArray (); // Output: [3, 1, 2]
206
207
```
207
208
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
+
208
257
## Running time
209
258
210
259
You can check the performance of the package here: [ https://jsperf.com/p-queue-ts ] ( https://jsperf.com/p-queue-ts )
0 commit comments