max-priority-queue-typed 2.4.5 → 2.5.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 (94) hide show
  1. package/README.md +63 -0
  2. package/dist/cjs/index.cjs +694 -119
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +693 -118
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +694 -119
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +693 -118
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/base/index.d.ts +1 -0
  11. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
  26. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  27. package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
  28. package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
  29. package/dist/types/data-structures/heap/heap.d.ts +581 -99
  30. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  31. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  32. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
  33. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
  34. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
  35. package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
  36. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  37. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  39. package/dist/types/data-structures/queue/deque.d.ts +593 -71
  40. package/dist/types/data-structures/queue/queue.d.ts +463 -42
  41. package/dist/types/data-structures/stack/stack.d.ts +384 -32
  42. package/dist/types/data-structures/trie/trie.d.ts +470 -48
  43. package/dist/types/interfaces/graph.d.ts +1 -1
  44. package/dist/types/types/common.d.ts +2 -2
  45. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  46. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  47. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  48. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  49. package/dist/types/types/utils/validate-type.d.ts +4 -4
  50. package/dist/umd/max-priority-queue-typed.js +691 -116
  51. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  52. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  53. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  54. package/package.json +2 -2
  55. package/src/data-structures/base/index.ts +1 -0
  56. package/src/data-structures/base/iterable-element-base.ts +4 -5
  57. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  58. package/src/data-structures/base/linear-base.ts +3 -3
  59. package/src/data-structures/binary-tree/avl-tree.ts +386 -51
  60. package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
  61. package/src/data-structures/binary-tree/binary-tree.ts +956 -81
  62. package/src/data-structures/binary-tree/bst.ts +840 -35
  63. package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
  64. package/src/data-structures/binary-tree/segment-tree.ts +498 -249
  65. package/src/data-structures/binary-tree/tree-map.ts +3784 -7
  66. package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
  67. package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
  68. package/src/data-structures/binary-tree/tree-set.ts +3531 -10
  69. package/src/data-structures/graph/abstract-graph.ts +4 -4
  70. package/src/data-structures/graph/directed-graph.ts +429 -47
  71. package/src/data-structures/graph/map-graph.ts +59 -1
  72. package/src/data-structures/graph/undirected-graph.ts +393 -59
  73. package/src/data-structures/hash/hash-map.ts +476 -92
  74. package/src/data-structures/heap/heap.ts +581 -99
  75. package/src/data-structures/heap/max-heap.ts +46 -0
  76. package/src/data-structures/heap/min-heap.ts +59 -0
  77. package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
  78. package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
  79. package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
  80. package/src/data-structures/matrix/matrix.ts +584 -12
  81. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  82. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  83. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  84. package/src/data-structures/queue/deque.ts +592 -70
  85. package/src/data-structures/queue/queue.ts +463 -42
  86. package/src/data-structures/stack/stack.ts +384 -32
  87. package/src/data-structures/trie/trie.ts +470 -48
  88. package/src/interfaces/graph.ts +1 -1
  89. package/src/types/common.ts +2 -2
  90. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  91. package/src/types/data-structures/heap/heap.ts +1 -0
  92. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  93. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  94. package/src/types/utils/validate-type.ts +4 -4
@@ -3,54 +3,6 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
 
6
- // src/common/error.ts
7
- var ERR = {
8
- // Range / index
9
- indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
10
- invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
11
- // Type / argument
12
- invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
13
- comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
14
- invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
15
- notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
16
- invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
17
- invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
18
- invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
19
- reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
20
- callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
21
- // State / operation
22
- invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
23
- // Matrix
24
- matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
25
- matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
26
- matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
27
- matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
28
- matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
29
- };
30
-
31
- // src/common/index.ts
32
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
33
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
34
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
35
- return DFSOperation2;
36
- })(DFSOperation || {});
37
- var _Range = class _Range {
38
- constructor(low, high, includeLow = true, includeHigh = true) {
39
- this.low = low;
40
- this.high = high;
41
- this.includeLow = includeLow;
42
- this.includeHigh = includeHigh;
43
- }
44
- // Determine whether a key is within the range
45
- isInRange(key, comparator) {
46
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
47
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
48
- return lowCheck && highCheck;
49
- }
50
- };
51
- __name(_Range, "Range");
52
- var Range = _Range;
53
-
54
6
  // src/data-structures/base/iterable-element-base.ts
55
7
  var _IterableElementBase = class _IterableElementBase {
56
8
  /**
@@ -73,7 +25,7 @@ var _IterableElementBase = class _IterableElementBase {
73
25
  if (options) {
74
26
  const { toElementFn } = options;
75
27
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
76
- else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
28
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
77
29
  }
78
30
  }
79
31
  /**
@@ -229,7 +181,7 @@ var _IterableElementBase = class _IterableElementBase {
229
181
  acc = initialValue;
230
182
  } else {
231
183
  const first = iter.next();
232
- if (first.done) throw new TypeError(ERR.reduceEmpty());
184
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
233
185
  acc = first.value;
234
186
  index = 1;
235
187
  }
@@ -273,6 +225,54 @@ var _IterableElementBase = class _IterableElementBase {
273
225
  __name(_IterableElementBase, "IterableElementBase");
274
226
  var IterableElementBase = _IterableElementBase;
275
227
 
228
+ // src/common/error.ts
229
+ var ERR = {
230
+ // Range / index
231
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
232
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
233
+ // Type / argument
234
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
235
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
236
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
237
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
238
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
239
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
240
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
241
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
242
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
243
+ // State / operation
244
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
245
+ // Matrix
246
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
247
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
248
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
249
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
250
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
251
+ };
252
+
253
+ // src/common/index.ts
254
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
255
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
256
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
257
+ return DFSOperation2;
258
+ })(DFSOperation || {});
259
+ var _Range = class _Range {
260
+ constructor(low, high, includeLow = true, includeHigh = true) {
261
+ this.low = low;
262
+ this.high = high;
263
+ this.includeLow = includeLow;
264
+ this.includeHigh = includeHigh;
265
+ }
266
+ // Determine whether a key is within the range
267
+ isInRange(key, comparator) {
268
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
269
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
270
+ return lowCheck && highCheck;
271
+ }
272
+ };
273
+ __name(_Range, "Range");
274
+ var Range = _Range;
275
+
276
276
  // src/data-structures/heap/heap.ts
277
277
  var _Heap = class _Heap extends IterableElementBase {
278
278
  /**
@@ -310,10 +310,51 @@ var _Heap = class _Heap extends IterableElementBase {
310
310
  return this._elements;
311
311
  }
312
312
  /**
313
- * Get the number of elements.
314
- * @remarks Time O(1), Space O(1)
315
- * @returns Heap size.
316
- */
313
+ * Get the number of elements.
314
+ * @remarks Time O(1), Space O(1)
315
+ * @returns Heap size.
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+ * @example
349
+ * // Track heap capacity
350
+ * const heap = new Heap<number>();
351
+ * console.log(heap.size); // 0;
352
+ * heap.add(10);
353
+ * heap.add(20);
354
+ * console.log(heap.size); // 2;
355
+ * heap.poll();
356
+ * console.log(heap.size); // 1;
357
+ */
317
358
  get size() {
318
359
  return this.elements.length;
319
360
  }
@@ -352,21 +393,103 @@ var _Heap = class _Heap extends IterableElementBase {
352
393
  return new _Heap(elements, options);
353
394
  }
354
395
  /**
355
- * Insert an element.
356
- * @remarks Time O(1) amortized, Space O(1)
357
- * @param element - Element to insert.
358
- * @returns True.
359
- */
396
+ * Insert an element.
397
+ * @remarks Time O(1) amortized, Space O(1)
398
+ * @param element - Element to insert.
399
+ * @returns True.
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+
424
+
425
+
426
+
427
+
428
+
429
+
430
+
431
+
432
+ * @example
433
+ * // basic Heap creation and add operation
434
+ * // Create a min heap (default)
435
+ * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
436
+ *
437
+ * // Verify size
438
+ * console.log(minHeap.size); // 6;
439
+ *
440
+ * // Add new element
441
+ * minHeap.add(4);
442
+ * console.log(minHeap.size); // 7;
443
+ *
444
+ * // Min heap property: smallest element at root
445
+ * const min = minHeap.peek();
446
+ * console.log(min); // 1;
447
+ */
360
448
  add(element) {
361
449
  this._elements.push(element);
362
450
  return this._bubbleUp(this.elements.length - 1);
363
451
  }
364
452
  /**
365
- * Insert many elements from an iterable.
366
- * @remarks Time O(N log N), Space O(1)
367
- * @param elements - Iterable of elements or raw values.
368
- * @returns Array of per-element success flags.
369
- */
453
+ * Insert many elements from an iterable.
454
+ * @remarks Time O(N log N), Space O(1)
455
+ * @param elements - Iterable of elements or raw values.
456
+ * @returns Array of per-element success flags.
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+ * @example
487
+ * // Add multiple elements
488
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
489
+ * heap.addMany([5, 3, 7, 1]);
490
+ * console.log(heap.peek()); // 1;
491
+ * console.log(heap.size); // 4;
492
+ */
370
493
  addMany(elements) {
371
494
  const flags = [];
372
495
  for (const el of elements) {
@@ -381,10 +504,67 @@ var _Heap = class _Heap extends IterableElementBase {
381
504
  return flags;
382
505
  }
383
506
  /**
384
- * Remove and return the top element.
385
- * @remarks Time O(log N), Space O(1)
386
- * @returns Top element or undefined.
387
- */
507
+ * Remove and return the top element.
508
+ * @remarks Time O(log N), Space O(1)
509
+ * @returns Top element or undefined.
510
+
511
+
512
+
513
+
514
+
515
+
516
+
517
+
518
+
519
+
520
+
521
+
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+ * @example
543
+ * // Heap with custom comparator (MaxHeap behavior)
544
+ * interface Task {
545
+ * id: number;
546
+ * priority: number;
547
+ * name: string;
548
+ * }
549
+ *
550
+ * // Custom comparator for max heap behavior (higher priority first)
551
+ * const tasks: Task[] = [
552
+ * { id: 1, priority: 5, name: 'Email' },
553
+ * { id: 2, priority: 3, name: 'Chat' },
554
+ * { id: 3, priority: 8, name: 'Alert' }
555
+ * ];
556
+ *
557
+ * const maxHeap = new Heap(tasks, {
558
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
559
+ * });
560
+ *
561
+ * console.log(maxHeap.size); // 3;
562
+ *
563
+ * // Peek returns highest priority task
564
+ * const topTask = maxHeap.peek();
565
+ * console.log(topTask?.priority); // 8;
566
+ * console.log(topTask?.name); // 'Alert';
567
+ */
388
568
  poll() {
389
569
  if (this.elements.length === 0) return;
390
570
  const value = this.elements[0];
@@ -396,26 +576,188 @@ var _Heap = class _Heap extends IterableElementBase {
396
576
  return value;
397
577
  }
398
578
  /**
399
- * Get the current top element without removing it.
400
- * @remarks Time O(1), Space O(1)
401
- * @returns Top element or undefined.
402
- */
579
+ * Get the current top element without removing it.
580
+ * @remarks Time O(1), Space O(1)
581
+ * @returns Top element or undefined.
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+
596
+
597
+
598
+
599
+
600
+
601
+
602
+
603
+
604
+
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+ * @example
615
+ * // Heap for event processing with priority
616
+ * interface Event {
617
+ * id: number;
618
+ * type: 'critical' | 'warning' | 'info';
619
+ * timestamp: number;
620
+ * message: string;
621
+ * }
622
+ *
623
+ * // Custom priority: critical > warning > info
624
+ * const priorityMap = { critical: 3, warning: 2, info: 1 };
625
+ *
626
+ * const eventHeap = new Heap<Event>([], {
627
+ * comparator: (a: Event, b: Event) => {
628
+ * const priorityA = priorityMap[a.type];
629
+ * const priorityB = priorityMap[b.type];
630
+ * return priorityB - priorityA; // Higher priority first
631
+ * }
632
+ * });
633
+ *
634
+ * // Add events in random order
635
+ * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
636
+ * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
637
+ * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
638
+ * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
639
+ * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
640
+ *
641
+ * console.log(eventHeap.size); // 5;
642
+ *
643
+ * // Process events by priority (critical first)
644
+ * const processedOrder: Event[] = [];
645
+ * while (eventHeap.size > 0) {
646
+ * const event = eventHeap.poll();
647
+ * if (event) {
648
+ * processedOrder.push(event);
649
+ * }
650
+ * }
651
+ *
652
+ * // Verify critical events came first
653
+ * console.log(processedOrder[0].type); // 'critical';
654
+ * console.log(processedOrder[1].type); // 'critical';
655
+ * console.log(processedOrder[2].type); // 'warning';
656
+ * console.log(processedOrder[3].type); // 'info';
657
+ * console.log(processedOrder[4].type); // 'info';
658
+ *
659
+ * // Verify O(log n) operations
660
+ * const newHeap = new Heap<number>([5, 3, 7, 1]);
661
+ *
662
+ * // Add - O(log n)
663
+ * newHeap.add(2);
664
+ * console.log(newHeap.size); // 5;
665
+ *
666
+ * // Poll - O(log n)
667
+ * const removed = newHeap.poll();
668
+ * console.log(removed); // 1;
669
+ *
670
+ * // Peek - O(1)
671
+ * const top = newHeap.peek();
672
+ * console.log(top); // 2;
673
+ */
403
674
  peek() {
404
675
  return this.elements[0];
405
676
  }
406
677
  /**
407
- * Check whether the heap is empty.
408
- * @remarks Time O(1), Space O(1)
409
- * @returns True if size is 0.
410
- */
678
+ * Check whether the heap is empty.
679
+ * @remarks Time O(1), Space O(1)
680
+ * @returns True if size is 0.
681
+
682
+
683
+
684
+
685
+
686
+
687
+
688
+
689
+
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+
709
+
710
+
711
+ * @example
712
+ * // Check if heap is empty
713
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
714
+ * console.log(heap.isEmpty()); // true;
715
+ * heap.add(1);
716
+ * console.log(heap.isEmpty()); // false;
717
+ */
411
718
  isEmpty() {
412
719
  return this.size === 0;
413
720
  }
414
721
  /**
415
- * Remove all elements.
416
- * @remarks Time O(1), Space O(1)
417
- * @returns void
418
- */
722
+ * Remove all elements.
723
+ * @remarks Time O(1), Space O(1)
724
+ * @returns void
725
+
726
+
727
+
728
+
729
+
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+
738
+
739
+
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+ * @example
756
+ * // Remove all elements
757
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
758
+ * heap.clear();
759
+ * console.log(heap.isEmpty()); // true;
760
+ */
419
761
  clear() {
420
762
  this._elements = [];
421
763
  }
@@ -430,21 +772,83 @@ var _Heap = class _Heap extends IterableElementBase {
430
772
  return this.fix();
431
773
  }
432
774
  /**
433
- * Check if an equal element exists in the heap.
434
- * @remarks Time O(N), Space O(1)
435
- * @param element - Element to search for.
436
- * @returns True if found.
437
- */
775
+ * Check if an equal element exists in the heap.
776
+ * @remarks Time O(N), Space O(1)
777
+ * @param element - Element to search for.
778
+ * @returns True if found.
779
+
780
+
781
+
782
+
783
+
784
+
785
+
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+ * @example
803
+ * // Check element existence
804
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
805
+ * console.log(heap.has(1)); // true;
806
+ * console.log(heap.has(99)); // false;
807
+ */
438
808
  has(element) {
439
809
  for (const el of this.elements) if (this._equals(el, element)) return true;
440
810
  return false;
441
811
  }
442
812
  /**
443
- * Delete one occurrence of an element.
444
- * @remarks Time O(N), Space O(1)
445
- * @param element - Element to delete.
446
- * @returns True if an element was removed.
447
- */
813
+ * Delete one occurrence of an element.
814
+ * @remarks Time O(N), Space O(1)
815
+ * @param element - Element to delete.
816
+ * @returns True if an element was removed.
817
+
818
+
819
+
820
+
821
+
822
+
823
+
824
+
825
+
826
+
827
+
828
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
844
+
845
+
846
+ * @example
847
+ * // Remove specific element
848
+ * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
849
+ * heap.delete(4);
850
+ * console.log(heap.toArray().includes(4)); // false;
851
+ */
448
852
  delete(element) {
449
853
  let index = -1;
450
854
  for (let i = 0; i < this.elements.length; i++) {
@@ -502,11 +906,39 @@ var _Heap = class _Heap extends IterableElementBase {
502
906
  return this;
503
907
  }
504
908
  /**
505
- * Traverse the binary heap as a complete binary tree and collect elements.
506
- * @remarks Time O(N), Space O(H)
507
- * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
508
- * @returns Array of visited elements.
509
- */
909
+ * Traverse the binary heap as a complete binary tree and collect elements.
910
+ * @remarks Time O(N), Space O(H)
911
+ * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
912
+ * @returns Array of visited elements.
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+ * @example
937
+ * // Depth-first traversal
938
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
939
+ * const result = heap.dfs('IN');
940
+ * console.log(result.length); // 3;
941
+ */
510
942
  dfs(order = "PRE") {
511
943
  const result = [];
512
944
  const _dfs = /* @__PURE__ */ __name((index) => {
@@ -543,10 +975,47 @@ var _Heap = class _Heap extends IterableElementBase {
543
975
  return results;
544
976
  }
545
977
  /**
546
- * Return all elements in ascending order by repeatedly polling.
547
- * @remarks Time O(N log N), Space O(N)
548
- * @returns Sorted array of elements.
549
- */
978
+ * Return all elements in ascending order by repeatedly polling.
979
+ * @remarks Time O(N log N), Space O(N)
980
+ * @returns Sorted array of elements.
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+
989
+
990
+
991
+
992
+
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+
1004
+
1005
+
1006
+
1007
+
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+ * @example
1014
+ * // Sort elements using heap
1015
+ * const heap = new Heap<number>([5, 1, 3, 2, 4]);
1016
+ * const sorted = heap.sort();
1017
+ * console.log(sorted); // [1, 2, 3, 4, 5];
1018
+ */
550
1019
  sort() {
551
1020
  const visited = [];
552
1021
  const cloned = this._createInstance();
@@ -558,22 +1027,94 @@ var _Heap = class _Heap extends IterableElementBase {
558
1027
  return visited;
559
1028
  }
560
1029
  /**
561
- * Deep clone this heap.
562
- * @remarks Time O(N), Space O(N)
563
- * @returns A new heap with the same elements.
564
- */
1030
+ * Deep clone this heap.
1031
+ * @remarks Time O(N), Space O(N)
1032
+ * @returns A new heap with the same elements.
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
1057
+
1058
+
1059
+
1060
+
1061
+
1062
+
1063
+ * @example
1064
+ * // Create independent copy
1065
+ * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
1066
+ * const copy = heap.clone();
1067
+ * copy.poll();
1068
+ * console.log(heap.size); // 3;
1069
+ * console.log(copy.size); // 2;
1070
+ */
565
1071
  clone() {
566
1072
  const next = this._createInstance();
567
1073
  for (const x of this.elements) next.add(x);
568
1074
  return next;
569
1075
  }
570
1076
  /**
571
- * Filter elements into a new heap of the same class.
572
- * @remarks Time O(N log N), Space O(N)
573
- * @param callback - Predicate (element, index, heap) → boolean to keep element.
574
- * @param [thisArg] - Value for `this` inside the callback.
575
- * @returns A new heap with the kept elements.
576
- */
1077
+ * Filter elements into a new heap of the same class.
1078
+ * @remarks Time O(N log N), Space O(N)
1079
+ * @param callback - Predicate (element, index, heap) → boolean to keep element.
1080
+ * @param [thisArg] - Value for `this` inside the callback.
1081
+ * @returns A new heap with the kept elements.
1082
+
1083
+
1084
+
1085
+
1086
+
1087
+
1088
+
1089
+
1090
+
1091
+
1092
+
1093
+
1094
+
1095
+
1096
+
1097
+
1098
+
1099
+
1100
+
1101
+
1102
+
1103
+
1104
+
1105
+
1106
+
1107
+
1108
+
1109
+
1110
+
1111
+
1112
+ * @example
1113
+ * // Filter elements
1114
+ * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
1115
+ * const evens = heap.filter(x => x % 2 === 0);
1116
+ * console.log(evens.size); // 2;
1117
+ */
577
1118
  filter(callback, thisArg) {
578
1119
  const out = this._createInstance();
579
1120
  let i = 0;
@@ -587,15 +1128,49 @@ var _Heap = class _Heap extends IterableElementBase {
587
1128
  return out;
588
1129
  }
589
1130
  /**
590
- * Map elements into a new heap of possibly different element type.
591
- * @remarks Time O(N log N), Space O(N)
592
- * @template EM
593
- * @template RM
594
- * @param callback - Mapping function (element, index, heap) → newElement.
595
- * @param options - Options for the output heap, including comparator for EM.
596
- * @param [thisArg] - Value for `this` inside the callback.
597
- * @returns A new heap with mapped elements.
598
- */
1131
+ * Map elements into a new heap of possibly different element type.
1132
+ * @remarks Time O(N log N), Space O(N)
1133
+ * @template EM
1134
+ * @template RM
1135
+ * @param callback - Mapping function (element, index, heap) → newElement.
1136
+ * @param options - Options for the output heap, including comparator for EM.
1137
+ * @param [thisArg] - Value for `this` inside the callback.
1138
+ * @returns A new heap with mapped elements.
1139
+
1140
+
1141
+
1142
+
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+
1166
+
1167
+
1168
+ * @example
1169
+ * // Transform elements
1170
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
1171
+ * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
1172
+ * console.log(doubled.peek()); // 2;
1173
+ */
599
1174
  map(callback, options, thisArg) {
600
1175
  const { comparator, toElementFn, ...rest } = options != null ? options : {};
601
1176
  if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));