data-structure-typed 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/graph/abstract-graph.js +14 -14
  3. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  4. package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
  5. package/dist/cjs/data-structures/hash/hash-map.js +46 -0
  6. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  7. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +66 -0
  8. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +66 -0
  9. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  10. package/dist/cjs/data-structures/queue/queue.d.ts +47 -0
  11. package/dist/cjs/data-structures/queue/queue.js +47 -0
  12. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  13. package/dist/cjs/data-structures/stack/stack.d.ts +121 -0
  14. package/dist/cjs/data-structures/stack/stack.js +121 -0
  15. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  16. package/dist/esm/data-structures/graph/abstract-graph.js +14 -14
  17. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  18. package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
  19. package/dist/esm/data-structures/hash/hash-map.js +46 -0
  20. package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
  21. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +66 -0
  22. package/dist/esm/data-structures/linked-list/singly-linked-list.js +66 -0
  23. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  24. package/dist/esm/data-structures/queue/queue.d.ts +47 -0
  25. package/dist/esm/data-structures/queue/queue.js +47 -0
  26. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  27. package/dist/esm/data-structures/stack/stack.d.ts +121 -0
  28. package/dist/esm/data-structures/stack/stack.js +121 -0
  29. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  30. package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
  31. package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
  32. package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
  33. package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
  34. package/dist/individuals/binary-tree/bst.mjs +3858 -0
  35. package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
  36. package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
  37. package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
  38. package/dist/individuals/graph/directed-graph.mjs +2910 -0
  39. package/dist/individuals/graph/undirected-graph.mjs +2745 -0
  40. package/dist/individuals/hash/hash-map.mjs +1040 -0
  41. package/dist/individuals/heap/heap.mjs +909 -0
  42. package/dist/individuals/heap/max-heap.mjs +671 -0
  43. package/dist/individuals/heap/min-heap.mjs +659 -0
  44. package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
  45. package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
  46. package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
  47. package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
  48. package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
  49. package/dist/individuals/queue/deque.mjs +1262 -0
  50. package/dist/individuals/queue/queue.mjs +1865 -0
  51. package/dist/individuals/stack/stack.mjs +415 -0
  52. package/dist/individuals/trie/trie.mjs +687 -0
  53. package/dist/umd/data-structure-typed.js +14 -14
  54. package/dist/umd/data-structure-typed.min.js +2 -2
  55. package/dist/umd/data-structure-typed.min.js.map +1 -1
  56. package/package.json +2 -2
  57. package/src/data-structures/graph/abstract-graph.ts +14 -14
  58. package/src/data-structures/hash/hash-map.ts +46 -0
  59. package/src/data-structures/linked-list/singly-linked-list.ts +66 -0
  60. package/src/data-structures/queue/queue.ts +47 -0
  61. package/src/data-structures/stack/stack.ts +121 -0
  62. package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
  63. package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
  64. package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
  65. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +72 -1
  66. package/test/unit/data-structures/queue/queue.test.ts +214 -0
  67. package/test/unit/data-structures/stack/stack.test.ts +165 -0
@@ -0,0 +1,670 @@
1
+ // src/data-structures/base/iterable-element-base.ts
2
+ var IterableElementBase = class {
3
+ /**
4
+ * The protected constructor initializes the options for the IterableElementBase class, including the
5
+ * toElementFn function.
6
+ * @param [options] - An optional object that contains the following properties:
7
+ */
8
+ constructor(options) {
9
+ if (options) {
10
+ const { toElementFn } = options;
11
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
12
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
13
+ }
14
+ }
15
+ _toElementFn;
16
+ get toElementFn() {
17
+ return this._toElementFn;
18
+ }
19
+ /**
20
+ * Time Complexity: O(n)
21
+ * Space Complexity: O(1)
22
+ *
23
+ * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
24
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
25
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
26
+ * parameter is used to pass any number of arguments to the `_getIterator` method.
27
+ */
28
+ *[Symbol.iterator](...args) {
29
+ yield* this._getIterator(...args);
30
+ }
31
+ /**
32
+ * Time Complexity: O(n)
33
+ * Space Complexity: O(n)
34
+ *
35
+ * The function returns an iterator that yields all the values in the object.
36
+ */
37
+ *values() {
38
+ for (const item of this) {
39
+ yield item;
40
+ }
41
+ }
42
+ /**
43
+ * Time Complexity: O(n)
44
+ * Space Complexity: O(1)
45
+ *
46
+ * The `every` function checks if every element in the array satisfies a given predicate.
47
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
48
+ * the current element being processed, its index, and the array it belongs to. It should return a
49
+ * boolean value indicating whether the element satisfies a certain condition or not.
50
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
51
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
52
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
53
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
54
+ * the array satisfies the provided predicate function, and `false` otherwise.
55
+ */
56
+ every(predicate, thisArg) {
57
+ let index = 0;
58
+ for (const item of this) {
59
+ if (!predicate.call(thisArg, item, index++, this)) {
60
+ return false;
61
+ }
62
+ }
63
+ return true;
64
+ }
65
+ /**
66
+ * Time Complexity: O(n)
67
+ * Space Complexity: O(1)
68
+ *
69
+ * The "some" function checks if at least one element in a collection satisfies a given predicate.
70
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
71
+ * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
72
+ * element satisfies the condition.
73
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
74
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
75
+ * it will be passed as the `this` value to the `predicate` function. If `thisArg
76
+ * @returns a boolean value. It returns true if the predicate function returns true for any element
77
+ * in the collection, and false otherwise.
78
+ */
79
+ some(predicate, thisArg) {
80
+ let index = 0;
81
+ for (const item of this) {
82
+ if (predicate.call(thisArg, item, index++, this)) {
83
+ return true;
84
+ }
85
+ }
86
+ return false;
87
+ }
88
+ /**
89
+ * Time Complexity: O(n)
90
+ * Space Complexity: O(1)
91
+ *
92
+ * The `forEach` function iterates over each element in an array-like object and calls a callback
93
+ * function for each element.
94
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
95
+ * the array. It takes three arguments: the current element being processed, the index of the current
96
+ * element, and the array that forEach was called upon.
97
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
98
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
99
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
100
+ */
101
+ forEach(callbackfn, thisArg) {
102
+ let index = 0;
103
+ for (const item of this) {
104
+ callbackfn.call(thisArg, item, index++, this);
105
+ }
106
+ }
107
+ /**
108
+ * Time Complexity: O(n)
109
+ * Space Complexity: O(1)
110
+ *
111
+ * The `find` function iterates over the elements of an array-like object and returns the first
112
+ * element that satisfies the provided callback function.
113
+ * @param predicate - The predicate parameter is a function that will be called for each element in
114
+ * the array. It takes three arguments: the current element being processed, the index of the current
115
+ * element, and the array itself. The function should return a boolean value indicating whether the
116
+ * current element matches the desired condition.
117
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
118
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
119
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
120
+ * @returns The `find` method returns the first element in the array that satisfies the provided
121
+ * callback function. If no element satisfies the callback function, `undefined` is returned.
122
+ */
123
+ find(predicate, thisArg) {
124
+ let index = 0;
125
+ for (const item of this) {
126
+ if (predicate.call(thisArg, item, index++, this)) return item;
127
+ }
128
+ return;
129
+ }
130
+ /**
131
+ * Time Complexity: O(n)
132
+ * Space Complexity: O(1)
133
+ *
134
+ * The function checks if a given element exists in a collection.
135
+ * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
136
+ * represents the element that we want to check for existence in the collection.
137
+ * @returns a boolean value. It returns true if the element is found in the collection, and false
138
+ * otherwise.
139
+ */
140
+ has(element) {
141
+ for (const ele of this) {
142
+ if (ele === element) return true;
143
+ }
144
+ return false;
145
+ }
146
+ /**
147
+ * Time Complexity: O(n)
148
+ * Space Complexity: O(1)
149
+ *
150
+ * The `reduce` function iterates over the elements of an array-like object and applies a callback
151
+ * function to reduce them into a single value.
152
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
153
+ * the array. It takes four arguments:
154
+ * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
155
+ * is the value that the accumulator starts with before the reduction operation begins.
156
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
157
+ * all the elements in the array and applying the callback function to each element.
158
+ */
159
+ reduce(callbackfn, initialValue) {
160
+ let accumulator = initialValue ?? 0;
161
+ let index = 0;
162
+ for (const item of this) {
163
+ accumulator = callbackfn(accumulator, item, index++, this);
164
+ }
165
+ return accumulator;
166
+ }
167
+ /**
168
+ * Time Complexity: O(n)
169
+ * Space Complexity: O(n)
170
+ *
171
+ * The `toArray` function converts a linked list into an array.
172
+ * @returns The `toArray()` method is returning an array of type `E[]`.
173
+ */
174
+ toArray() {
175
+ return [...this];
176
+ }
177
+ /**
178
+ * Time Complexity: O(n)
179
+ * Space Complexity: O(n)
180
+ *
181
+ * The print function logs the elements of an array to the console.
182
+ */
183
+ toVisual() {
184
+ return [...this];
185
+ }
186
+ /**
187
+ * Time Complexity: O(n)
188
+ * Space Complexity: O(n)
189
+ *
190
+ * The print function logs the elements of an array to the console.
191
+ */
192
+ print() {
193
+ console.log(this.toVisual());
194
+ }
195
+ };
196
+
197
+ // src/data-structures/heap/heap.ts
198
+ var Heap = class _Heap extends IterableElementBase {
199
+ /**
200
+ * The constructor initializes a heap data structure with optional elements and options.
201
+ * @param elements - The `elements` parameter is an iterable object that contains the initial
202
+ * elements to be added to the heap.
203
+ * It is an optional parameter, and if not provided, the heap will
204
+ * be initialized as empty.
205
+ * @param [options] - The `options` parameter is an optional object that can contain additional
206
+ * configuration options for the heap.
207
+ * In this case, it is used to specify a custom comparator
208
+ * function for comparing elements in the heap.
209
+ * The comparator function is used to determine the
210
+ * order of elements in the heap.
211
+ */
212
+ constructor(elements = [], options) {
213
+ super(options);
214
+ if (options) {
215
+ const { comparator } = options;
216
+ if (comparator) this._comparator = comparator;
217
+ }
218
+ this.addMany(elements);
219
+ }
220
+ _elements = [];
221
+ /**
222
+ * The function returns an array of elements.
223
+ * @returns The element array is being returned.
224
+ */
225
+ get elements() {
226
+ return this._elements;
227
+ }
228
+ /**
229
+ * Get the size (number of elements) of the heap.
230
+ */
231
+ get size() {
232
+ return this.elements.length;
233
+ }
234
+ /**
235
+ * Get the last element in the heap, which is not necessarily a leaf node.
236
+ * @returns The last element or undefined if the heap is empty.
237
+ */
238
+ get leaf() {
239
+ return this.elements[this.size - 1] ?? void 0;
240
+ }
241
+ /**
242
+ * Static method that creates a binary heap from an array of elements and a comparison function.
243
+ * @returns A new Heap instance.
244
+ * @param elements
245
+ * @param options
246
+ */
247
+ static heapify(elements, options) {
248
+ return new _Heap(elements, options);
249
+ }
250
+ /**
251
+ * Time Complexity: O(log n)
252
+ * Space Complexity: O(1)
253
+ *
254
+ * The add function pushes an element into an array and then triggers a bubble-up operation.
255
+ * @param {E} element - The `element` parameter represents the element that you want to add to the
256
+ * data structure.
257
+ * @returns The `add` method is returning a boolean value, which is the result of calling the
258
+ * `_bubbleUp` method with the index `this.elements.length - 1` as an argument.
259
+ */
260
+ add(element) {
261
+ this._elements.push(element);
262
+ return this._bubbleUp(this.elements.length - 1);
263
+ }
264
+ /**
265
+ * Time Complexity: O(k log n)
266
+ * Space Complexity: O(1)
267
+ *
268
+ * The `addMany` function iterates over elements and adds them to a collection, returning an array of
269
+ * boolean values indicating success or failure.
270
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `addMany` method is
271
+ * an iterable containing elements of type `E` or `R`. The method iterates over each element in the
272
+ * iterable and adds them to the data structure. If a transformation function `_toElementFn` is
273
+ * provided, it transforms the element
274
+ * @returns The `addMany` method returns an array of boolean values indicating whether each element
275
+ * in the input iterable was successfully added to the data structure.
276
+ */
277
+ addMany(elements) {
278
+ const ans = [];
279
+ for (const el of elements) {
280
+ if (this._toElementFn) {
281
+ ans.push(this.add(this._toElementFn(el)));
282
+ continue;
283
+ }
284
+ ans.push(this.add(el));
285
+ }
286
+ return ans;
287
+ }
288
+ /**
289
+ * Time Complexity: O(log n)
290
+ * Space Complexity: O(1)
291
+ *
292
+ * Remove and return the top element (the smallest or largest element) from the heap.
293
+ * @returns The top element or undefined if the heap is empty.
294
+ */
295
+ poll() {
296
+ if (this.elements.length === 0) return;
297
+ const value = this.elements[0];
298
+ const last = this.elements.pop();
299
+ if (this.elements.length) {
300
+ this.elements[0] = last;
301
+ this._sinkDown(0, this.elements.length >> 1);
302
+ }
303
+ return value;
304
+ }
305
+ /**
306
+ * Time Complexity: O(1)
307
+ * Space Complexity: O(1)
308
+ *
309
+ * Peek at the top element of the heap without removing it.
310
+ * @returns The top element or undefined if the heap is empty.
311
+ */
312
+ peek() {
313
+ return this.elements[0];
314
+ }
315
+ /**
316
+ * Check if the heap is empty.
317
+ * @returns True if the heap is empty, otherwise false.
318
+ */
319
+ isEmpty() {
320
+ return this.size === 0;
321
+ }
322
+ /**
323
+ * Reset the elements of the heap. Make the elements empty.
324
+ */
325
+ clear() {
326
+ this._elements = [];
327
+ }
328
+ /**
329
+ * Time Complexity: O(n)
330
+ * Space Complexity: O(n)
331
+ *
332
+ * Clear and add elements of the heap
333
+ * @param elements
334
+ */
335
+ refill(elements) {
336
+ this._elements = elements;
337
+ return this.fix();
338
+ }
339
+ /**
340
+ * Time Complexity: O(n)
341
+ * Space Complexity: O(1)
342
+ *
343
+ * Use a comparison function to check whether a binary heap contains a specific element.
344
+ * @param element - the element to check.
345
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
346
+ */
347
+ has(element) {
348
+ return this.elements.includes(element);
349
+ }
350
+ /**
351
+ * Time Complexity: O(n)
352
+ * Space Complexity: O(1)
353
+ *
354
+ * The `delete` function removes an element from an array-like data structure, maintaining the order
355
+ * and structure of the remaining elements.
356
+ * @param {E} element - The `element` parameter represents the element that you want to delete from
357
+ * the array `this.elements`.
358
+ * @returns The `delete` function is returning a boolean value. It returns `true` if the element was
359
+ * successfully deleted from the array, and `false` if the element was not found in the array.
360
+ */
361
+ delete(element) {
362
+ const index = this.elements.indexOf(element);
363
+ if (index < 0) return false;
364
+ if (index === 0) {
365
+ this.poll();
366
+ } else if (index === this.elements.length - 1) {
367
+ this.elements.pop();
368
+ } else {
369
+ this.elements.splice(index, 1, this.elements.pop());
370
+ this._bubbleUp(index);
371
+ this._sinkDown(index, this.elements.length >> 1);
372
+ }
373
+ return true;
374
+ }
375
+ /**
376
+ * Time Complexity: O(n)
377
+ * Space Complexity: O(log n)
378
+ *
379
+ * Depth-first search (DFS) method, different traversal orders can be selected。
380
+ * @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
381
+ * @returns An array containing elements traversed in the specified order.
382
+ */
383
+ dfs(order = "PRE") {
384
+ const result = [];
385
+ const _dfs = (index) => {
386
+ const left = 2 * index + 1, right = left + 1;
387
+ if (index < this.size) {
388
+ if (order === "IN") {
389
+ _dfs(left);
390
+ result.push(this.elements[index]);
391
+ _dfs(right);
392
+ } else if (order === "PRE") {
393
+ result.push(this.elements[index]);
394
+ _dfs(left);
395
+ _dfs(right);
396
+ } else if (order === "POST") {
397
+ _dfs(left);
398
+ _dfs(right);
399
+ result.push(this.elements[index]);
400
+ }
401
+ }
402
+ };
403
+ _dfs(0);
404
+ return result;
405
+ }
406
+ /**
407
+ * Time Complexity: O(n)
408
+ * Space Complexity: O(n)
409
+ *
410
+ * Clone the heap, creating a new heap with the same elements.
411
+ * @returns A new Heap instance containing the same elements.
412
+ */
413
+ clone() {
414
+ return new _Heap(this, { comparator: this.comparator, toElementFn: this.toElementFn });
415
+ }
416
+ /**
417
+ * Time Complexity: O(n log n)
418
+ * Space Complexity: O(n)
419
+ *
420
+ * Sort the elements in the heap and return them as an array.
421
+ * @returns An array containing the elements sorted in ascending order.
422
+ */
423
+ sort() {
424
+ const visitedNode = [];
425
+ const cloned = new _Heap(this, { comparator: this.comparator });
426
+ while (cloned.size !== 0) {
427
+ const top = cloned.poll();
428
+ if (top !== void 0) visitedNode.push(top);
429
+ }
430
+ return visitedNode;
431
+ }
432
+ /**
433
+ * Time Complexity: O(n log n)
434
+ * Space Complexity: O(n)
435
+ *
436
+ * Fix the entire heap to maintain heap properties.
437
+ */
438
+ fix() {
439
+ const results = [];
440
+ for (let i = Math.floor(this.size / 2); i >= 0; i--) results.push(this._sinkDown(i, this.elements.length >> 1));
441
+ return results;
442
+ }
443
+ /**
444
+ * Time Complexity: O(n)
445
+ * Space Complexity: O(n)
446
+ *
447
+ * The `filter` function creates a new Heap object containing elements that pass a given callback
448
+ * function.
449
+ * @param callback - The `callback` parameter is a function that will be called for each element in
450
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
451
+ * heap itself. The callback function should return a boolean value indicating whether the current
452
+ * element should be included in the filtered list
453
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
454
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
455
+ * passed as the `this` value to the `callback` function. If `thisArg` is
456
+ * @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
457
+ * the filter condition specified by the `callback` function.
458
+ */
459
+ filter(callback, thisArg) {
460
+ const filteredList = new _Heap([], { toElementFn: this.toElementFn, comparator: this.comparator });
461
+ let index = 0;
462
+ for (const current of this) {
463
+ if (callback.call(thisArg, current, index, this)) {
464
+ filteredList.add(current);
465
+ }
466
+ index++;
467
+ }
468
+ return filteredList;
469
+ }
470
+ /**
471
+ * Time Complexity: O(n)
472
+ * Space Complexity: O(n)
473
+ *
474
+ * The `map` function creates a new heap by applying a callback function to each element of the
475
+ * original heap.
476
+ * @param callback - The `callback` parameter is a function that will be called for each element in
477
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
478
+ * element), and `this` (the heap itself). The callback function should return a value of
479
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
480
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
481
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
482
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
483
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
484
+ * returns a value of type `T`. This function is used to transform the elements of the original
485
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
486
+ * specify the value of `this` within the callback function. It is used to set the context or scope
487
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
488
+ * value of
489
+ * @returns a new instance of the `Heap` class with the mapped elements.
490
+ */
491
+ map(callback, comparator, toElementFn, thisArg) {
492
+ const mappedHeap = new _Heap([], { comparator, toElementFn });
493
+ let index = 0;
494
+ for (const el of this) {
495
+ mappedHeap.add(callback.call(thisArg, el, index, this));
496
+ index++;
497
+ }
498
+ return mappedHeap;
499
+ }
500
+ _DEFAULT_COMPARATOR = (a, b) => {
501
+ if (typeof a === "object" || typeof b === "object") {
502
+ throw TypeError(
503
+ `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
504
+ );
505
+ }
506
+ if (a > b) return 1;
507
+ if (a < b) return -1;
508
+ return 0;
509
+ };
510
+ _comparator = this._DEFAULT_COMPARATOR;
511
+ /**
512
+ * The function returns the value of the _comparator property.
513
+ * @returns The `_comparator` property is being returned.
514
+ */
515
+ get comparator() {
516
+ return this._comparator;
517
+ }
518
+ /**
519
+ * The function `_getIterator` returns an iterable iterator for the elements in the class.
520
+ */
521
+ *_getIterator() {
522
+ for (const element of this.elements) {
523
+ yield element;
524
+ }
525
+ }
526
+ /**
527
+ * Time Complexity: O(log n)
528
+ * Space Complexity: O(1)
529
+ *
530
+ * Float operation to maintain heap properties after adding an element.
531
+ * @param index - The index of the newly added element.
532
+ */
533
+ _bubbleUp(index) {
534
+ const element = this.elements[index];
535
+ while (index > 0) {
536
+ const parent = index - 1 >> 1;
537
+ const parentItem = this.elements[parent];
538
+ if (this.comparator(parentItem, element) <= 0) break;
539
+ this.elements[index] = parentItem;
540
+ index = parent;
541
+ }
542
+ this.elements[index] = element;
543
+ return true;
544
+ }
545
+ /**
546
+ * Time Complexity: O(log n)
547
+ * Space Complexity: O(1)
548
+ *
549
+ * Sinking operation to maintain heap properties after removing the top element.
550
+ * @param index - The index from which to start sinking.
551
+ * @param halfLength
552
+ */
553
+ _sinkDown(index, halfLength) {
554
+ const element = this.elements[index];
555
+ while (index < halfLength) {
556
+ let left = index << 1 | 1;
557
+ const right = left + 1;
558
+ let minItem = this.elements[left];
559
+ if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
560
+ left = right;
561
+ minItem = this.elements[right];
562
+ }
563
+ if (this.comparator(minItem, element) >= 0) break;
564
+ this.elements[index] = minItem;
565
+ index = left;
566
+ }
567
+ this.elements[index] = element;
568
+ return true;
569
+ }
570
+ };
571
+
572
+ // src/data-structures/priority-queue/priority-queue.ts
573
+ var PriorityQueue = class _PriorityQueue extends Heap {
574
+ /**
575
+ * The constructor initializes a priority queue with optional elements and options.
576
+ * @param elements - The `elements` parameter is an iterable object that contains the initial
577
+ * elements to be added to the priority queue. It is an optional parameter, and if not provided, the
578
+ * priority queue will be initialized as empty.
579
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
580
+ * behavior of the priority queue. It can contain the following properties:
581
+ */
582
+ constructor(elements = [], options) {
583
+ super(elements, options);
584
+ }
585
+ /**
586
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
587
+ * and toElementFn as the original instance.
588
+ * @returns The method is returning a new instance of the `PriorityQueue` class with the same
589
+ * elements and properties as the current instance.
590
+ */
591
+ clone() {
592
+ return new _PriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
593
+ }
594
+ /**
595
+ * Time Complexity: O(n)
596
+ * Space Complexity: O(n)
597
+ *
598
+ * The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
599
+ * function.
600
+ * @param callback - The `callback` parameter is a function that will be called for each element in
601
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
602
+ * heap itself. The callback function should return a boolean value indicating whether the current
603
+ * element should be included in the filtered list
604
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
605
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
606
+ * passed as the `this` value to the `callback` function. If `thisArg` is
607
+ * @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
608
+ * the filter condition specified by the `callback` function.
609
+ */
610
+ filter(callback, thisArg) {
611
+ const filteredPriorityQueue = new _PriorityQueue([], {
612
+ toElementFn: this.toElementFn,
613
+ comparator: this.comparator
614
+ });
615
+ let index = 0;
616
+ for (const current of this) {
617
+ if (callback.call(thisArg, current, index, this)) {
618
+ filteredPriorityQueue.add(current);
619
+ }
620
+ index++;
621
+ }
622
+ return filteredPriorityQueue;
623
+ }
624
+ /**
625
+ * Time Complexity: O(n log n)
626
+ * Space Complexity: O(n)
627
+ *
628
+ * The `map` function creates a new heap by applying a callback function to each element of the
629
+ * original heap.
630
+ * @param callback - The `callback` parameter is a function that will be called for each element in
631
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
632
+ * element), and `this` (the heap itself). The callback function should return a value of
633
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
634
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
635
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
636
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
637
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
638
+ * returns a value of type `T`. This function is used to transform the elements of the original
639
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
640
+ * specify the value of `this` within the callback function. It is used to set the context or scope
641
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
642
+ * value of
643
+ * @returns a new instance of the `PriorityQueue` class with the mapped elements.
644
+ */
645
+ map(callback, comparator, toElementFn, thisArg) {
646
+ const mappedPriorityQueue = new _PriorityQueue([], { comparator, toElementFn });
647
+ let index = 0;
648
+ for (const el of this) {
649
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
650
+ index++;
651
+ }
652
+ return mappedPriorityQueue;
653
+ }
654
+ };
655
+ export {
656
+ PriorityQueue
657
+ };
658
+ /**
659
+ * data-structure-typed
660
+ * @author Kirk Qi
661
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
662
+ * @license MIT License
663
+ */
664
+ /**
665
+ * data-structure-typed
666
+ *
667
+ * @author Kirk Qi
668
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
669
+ * @license MIT License
670
+ */