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
@@ -5,54 +5,6 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
5
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
6
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
7
 
8
- // src/common/error.ts
9
- var ERR = {
10
- // Range / index
11
- indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
12
- invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
13
- // Type / argument
14
- invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
15
- comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
16
- invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
17
- notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
18
- invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
19
- invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
20
- invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
21
- reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
22
- callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
23
- // State / operation
24
- invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
25
- // Matrix
26
- matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
27
- matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
28
- matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
29
- matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
30
- matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
31
- };
32
-
33
- // src/common/index.ts
34
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
35
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
36
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
37
- return DFSOperation2;
38
- })(DFSOperation || {});
39
- var _Range = class _Range {
40
- constructor(low, high, includeLow = true, includeHigh = true) {
41
- this.low = low;
42
- this.high = high;
43
- this.includeLow = includeLow;
44
- this.includeHigh = includeHigh;
45
- }
46
- // Determine whether a key is within the range
47
- isInRange(key, comparator) {
48
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
49
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
50
- return lowCheck && highCheck;
51
- }
52
- };
53
- __name(_Range, "Range");
54
- var Range = _Range;
55
-
56
8
  // src/data-structures/base/iterable-element-base.ts
57
9
  var _IterableElementBase = class _IterableElementBase {
58
10
  /**
@@ -75,7 +27,7 @@ var _IterableElementBase = class _IterableElementBase {
75
27
  if (options) {
76
28
  const { toElementFn } = options;
77
29
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
78
- else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
30
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
79
31
  }
80
32
  }
81
33
  /**
@@ -231,7 +183,7 @@ var _IterableElementBase = class _IterableElementBase {
231
183
  acc = initialValue;
232
184
  } else {
233
185
  const first = iter.next();
234
- if (first.done) throw new TypeError(ERR.reduceEmpty());
186
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
235
187
  acc = first.value;
236
188
  index = 1;
237
189
  }
@@ -275,6 +227,54 @@ var _IterableElementBase = class _IterableElementBase {
275
227
  __name(_IterableElementBase, "IterableElementBase");
276
228
  var IterableElementBase = _IterableElementBase;
277
229
 
230
+ // src/common/error.ts
231
+ var ERR = {
232
+ // Range / index
233
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
234
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
235
+ // Type / argument
236
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
237
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
238
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
239
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
240
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
241
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
242
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
243
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
244
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
245
+ // State / operation
246
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
247
+ // Matrix
248
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
249
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
250
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
251
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
252
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
253
+ };
254
+
255
+ // src/common/index.ts
256
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
257
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
258
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
259
+ return DFSOperation2;
260
+ })(DFSOperation || {});
261
+ var _Range = class _Range {
262
+ constructor(low, high, includeLow = true, includeHigh = true) {
263
+ this.low = low;
264
+ this.high = high;
265
+ this.includeLow = includeLow;
266
+ this.includeHigh = includeHigh;
267
+ }
268
+ // Determine whether a key is within the range
269
+ isInRange(key, comparator) {
270
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
271
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
272
+ return lowCheck && highCheck;
273
+ }
274
+ };
275
+ __name(_Range, "Range");
276
+ var Range = _Range;
277
+
278
278
  // src/data-structures/heap/heap.ts
279
279
  var _Heap = class _Heap extends IterableElementBase {
280
280
  /**
@@ -312,10 +312,51 @@ var _Heap = class _Heap extends IterableElementBase {
312
312
  return this._elements;
313
313
  }
314
314
  /**
315
- * Get the number of elements.
316
- * @remarks Time O(1), Space O(1)
317
- * @returns Heap size.
318
- */
315
+ * Get the number of elements.
316
+ * @remarks Time O(1), Space O(1)
317
+ * @returns Heap size.
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
+
349
+
350
+ * @example
351
+ * // Track heap capacity
352
+ * const heap = new Heap<number>();
353
+ * console.log(heap.size); // 0;
354
+ * heap.add(10);
355
+ * heap.add(20);
356
+ * console.log(heap.size); // 2;
357
+ * heap.poll();
358
+ * console.log(heap.size); // 1;
359
+ */
319
360
  get size() {
320
361
  return this.elements.length;
321
362
  }
@@ -354,21 +395,103 @@ var _Heap = class _Heap extends IterableElementBase {
354
395
  return new _Heap(elements, options);
355
396
  }
356
397
  /**
357
- * Insert an element.
358
- * @remarks Time O(1) amortized, Space O(1)
359
- * @param element - Element to insert.
360
- * @returns True.
361
- */
398
+ * Insert an element.
399
+ * @remarks Time O(1) amortized, Space O(1)
400
+ * @param element - Element to insert.
401
+ * @returns True.
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
+
433
+
434
+ * @example
435
+ * // basic Heap creation and add operation
436
+ * // Create a min heap (default)
437
+ * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
438
+ *
439
+ * // Verify size
440
+ * console.log(minHeap.size); // 6;
441
+ *
442
+ * // Add new element
443
+ * minHeap.add(4);
444
+ * console.log(minHeap.size); // 7;
445
+ *
446
+ * // Min heap property: smallest element at root
447
+ * const min = minHeap.peek();
448
+ * console.log(min); // 1;
449
+ */
362
450
  add(element) {
363
451
  this._elements.push(element);
364
452
  return this._bubbleUp(this.elements.length - 1);
365
453
  }
366
454
  /**
367
- * Insert many elements from an iterable.
368
- * @remarks Time O(N log N), Space O(1)
369
- * @param elements - Iterable of elements or raw values.
370
- * @returns Array of per-element success flags.
371
- */
455
+ * Insert many elements from an iterable.
456
+ * @remarks Time O(N log N), Space O(1)
457
+ * @param elements - Iterable of elements or raw values.
458
+ * @returns Array of per-element success flags.
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
+
487
+
488
+ * @example
489
+ * // Add multiple elements
490
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
491
+ * heap.addMany([5, 3, 7, 1]);
492
+ * console.log(heap.peek()); // 1;
493
+ * console.log(heap.size); // 4;
494
+ */
372
495
  addMany(elements) {
373
496
  const flags = [];
374
497
  for (const el of elements) {
@@ -383,10 +506,67 @@ var _Heap = class _Heap extends IterableElementBase {
383
506
  return flags;
384
507
  }
385
508
  /**
386
- * Remove and return the top element.
387
- * @remarks Time O(log N), Space O(1)
388
- * @returns Top element or undefined.
389
- */
509
+ * Remove and return the top element.
510
+ * @remarks Time O(log N), Space O(1)
511
+ * @returns Top element or undefined.
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
+
543
+
544
+ * @example
545
+ * // Heap with custom comparator (MaxHeap behavior)
546
+ * interface Task {
547
+ * id: number;
548
+ * priority: number;
549
+ * name: string;
550
+ * }
551
+ *
552
+ * // Custom comparator for max heap behavior (higher priority first)
553
+ * const tasks: Task[] = [
554
+ * { id: 1, priority: 5, name: 'Email' },
555
+ * { id: 2, priority: 3, name: 'Chat' },
556
+ * { id: 3, priority: 8, name: 'Alert' }
557
+ * ];
558
+ *
559
+ * const maxHeap = new Heap(tasks, {
560
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
561
+ * });
562
+ *
563
+ * console.log(maxHeap.size); // 3;
564
+ *
565
+ * // Peek returns highest priority task
566
+ * const topTask = maxHeap.peek();
567
+ * console.log(topTask?.priority); // 8;
568
+ * console.log(topTask?.name); // 'Alert';
569
+ */
390
570
  poll() {
391
571
  if (this.elements.length === 0) return;
392
572
  const value = this.elements[0];
@@ -398,26 +578,188 @@ var _Heap = class _Heap extends IterableElementBase {
398
578
  return value;
399
579
  }
400
580
  /**
401
- * Get the current top element without removing it.
402
- * @remarks Time O(1), Space O(1)
403
- * @returns Top element or undefined.
404
- */
581
+ * Get the current top element without removing it.
582
+ * @remarks Time O(1), Space O(1)
583
+ * @returns Top element or undefined.
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
+
615
+
616
+ * @example
617
+ * // Heap for event processing with priority
618
+ * interface Event {
619
+ * id: number;
620
+ * type: 'critical' | 'warning' | 'info';
621
+ * timestamp: number;
622
+ * message: string;
623
+ * }
624
+ *
625
+ * // Custom priority: critical > warning > info
626
+ * const priorityMap = { critical: 3, warning: 2, info: 1 };
627
+ *
628
+ * const eventHeap = new Heap<Event>([], {
629
+ * comparator: (a: Event, b: Event) => {
630
+ * const priorityA = priorityMap[a.type];
631
+ * const priorityB = priorityMap[b.type];
632
+ * return priorityB - priorityA; // Higher priority first
633
+ * }
634
+ * });
635
+ *
636
+ * // Add events in random order
637
+ * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
638
+ * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
639
+ * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
640
+ * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
641
+ * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
642
+ *
643
+ * console.log(eventHeap.size); // 5;
644
+ *
645
+ * // Process events by priority (critical first)
646
+ * const processedOrder: Event[] = [];
647
+ * while (eventHeap.size > 0) {
648
+ * const event = eventHeap.poll();
649
+ * if (event) {
650
+ * processedOrder.push(event);
651
+ * }
652
+ * }
653
+ *
654
+ * // Verify critical events came first
655
+ * console.log(processedOrder[0].type); // 'critical';
656
+ * console.log(processedOrder[1].type); // 'critical';
657
+ * console.log(processedOrder[2].type); // 'warning';
658
+ * console.log(processedOrder[3].type); // 'info';
659
+ * console.log(processedOrder[4].type); // 'info';
660
+ *
661
+ * // Verify O(log n) operations
662
+ * const newHeap = new Heap<number>([5, 3, 7, 1]);
663
+ *
664
+ * // Add - O(log n)
665
+ * newHeap.add(2);
666
+ * console.log(newHeap.size); // 5;
667
+ *
668
+ * // Poll - O(log n)
669
+ * const removed = newHeap.poll();
670
+ * console.log(removed); // 1;
671
+ *
672
+ * // Peek - O(1)
673
+ * const top = newHeap.peek();
674
+ * console.log(top); // 2;
675
+ */
405
676
  peek() {
406
677
  return this.elements[0];
407
678
  }
408
679
  /**
409
- * Check whether the heap is empty.
410
- * @remarks Time O(1), Space O(1)
411
- * @returns True if size is 0.
412
- */
680
+ * Check whether the heap is empty.
681
+ * @remarks Time O(1), Space O(1)
682
+ * @returns True if size is 0.
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
+
712
+
713
+ * @example
714
+ * // Check if heap is empty
715
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
716
+ * console.log(heap.isEmpty()); // true;
717
+ * heap.add(1);
718
+ * console.log(heap.isEmpty()); // false;
719
+ */
413
720
  isEmpty() {
414
721
  return this.size === 0;
415
722
  }
416
723
  /**
417
- * Remove all elements.
418
- * @remarks Time O(1), Space O(1)
419
- * @returns void
420
- */
724
+ * Remove all elements.
725
+ * @remarks Time O(1), Space O(1)
726
+ * @returns void
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
+
756
+
757
+ * @example
758
+ * // Remove all elements
759
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
760
+ * heap.clear();
761
+ * console.log(heap.isEmpty()); // true;
762
+ */
421
763
  clear() {
422
764
  this._elements = [];
423
765
  }
@@ -432,21 +774,83 @@ var _Heap = class _Heap extends IterableElementBase {
432
774
  return this.fix();
433
775
  }
434
776
  /**
435
- * Check if an equal element exists in the heap.
436
- * @remarks Time O(N), Space O(1)
437
- * @param element - Element to search for.
438
- * @returns True if found.
439
- */
777
+ * Check if an equal element exists in the heap.
778
+ * @remarks Time O(N), Space O(1)
779
+ * @param element - Element to search for.
780
+ * @returns True if found.
781
+
782
+
783
+
784
+
785
+
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
803
+
804
+ * @example
805
+ * // Check element existence
806
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
807
+ * console.log(heap.has(1)); // true;
808
+ * console.log(heap.has(99)); // false;
809
+ */
440
810
  has(element) {
441
811
  for (const el of this.elements) if (this._equals(el, element)) return true;
442
812
  return false;
443
813
  }
444
814
  /**
445
- * Delete one occurrence of an element.
446
- * @remarks Time O(N), Space O(1)
447
- * @param element - Element to delete.
448
- * @returns True if an element was removed.
449
- */
815
+ * Delete one occurrence of an element.
816
+ * @remarks Time O(N), Space O(1)
817
+ * @param element - Element to delete.
818
+ * @returns True if an element was removed.
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
+
847
+
848
+ * @example
849
+ * // Remove specific element
850
+ * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
851
+ * heap.delete(4);
852
+ * console.log(heap.toArray().includes(4)); // false;
853
+ */
450
854
  delete(element) {
451
855
  let index = -1;
452
856
  for (let i = 0; i < this.elements.length; i++) {
@@ -504,11 +908,39 @@ var _Heap = class _Heap extends IterableElementBase {
504
908
  return this;
505
909
  }
506
910
  /**
507
- * Traverse the binary heap as a complete binary tree and collect elements.
508
- * @remarks Time O(N), Space O(H)
509
- * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
510
- * @returns Array of visited elements.
511
- */
911
+ * Traverse the binary heap as a complete binary tree and collect elements.
912
+ * @remarks Time O(N), Space O(H)
913
+ * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
914
+ * @returns Array of visited elements.
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+
937
+
938
+ * @example
939
+ * // Depth-first traversal
940
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
941
+ * const result = heap.dfs('IN');
942
+ * console.log(result.length); // 3;
943
+ */
512
944
  dfs(order = "PRE") {
513
945
  const result = [];
514
946
  const _dfs = /* @__PURE__ */ __name((index) => {
@@ -545,10 +977,47 @@ var _Heap = class _Heap extends IterableElementBase {
545
977
  return results;
546
978
  }
547
979
  /**
548
- * Return all elements in ascending order by repeatedly polling.
549
- * @remarks Time O(N log N), Space O(N)
550
- * @returns Sorted array of elements.
551
- */
980
+ * Return all elements in ascending order by repeatedly polling.
981
+ * @remarks Time O(N log N), Space O(N)
982
+ * @returns Sorted array of elements.
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
+
1014
+
1015
+ * @example
1016
+ * // Sort elements using heap
1017
+ * const heap = new Heap<number>([5, 1, 3, 2, 4]);
1018
+ * const sorted = heap.sort();
1019
+ * console.log(sorted); // [1, 2, 3, 4, 5];
1020
+ */
552
1021
  sort() {
553
1022
  const visited = [];
554
1023
  const cloned = this._createInstance();
@@ -560,22 +1029,94 @@ var _Heap = class _Heap extends IterableElementBase {
560
1029
  return visited;
561
1030
  }
562
1031
  /**
563
- * Deep clone this heap.
564
- * @remarks Time O(N), Space O(N)
565
- * @returns A new heap with the same elements.
566
- */
1032
+ * Deep clone this heap.
1033
+ * @remarks Time O(N), Space O(N)
1034
+ * @returns A new heap with the same elements.
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
+
1064
+
1065
+ * @example
1066
+ * // Create independent copy
1067
+ * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
1068
+ * const copy = heap.clone();
1069
+ * copy.poll();
1070
+ * console.log(heap.size); // 3;
1071
+ * console.log(copy.size); // 2;
1072
+ */
567
1073
  clone() {
568
1074
  const next = this._createInstance();
569
1075
  for (const x of this.elements) next.add(x);
570
1076
  return next;
571
1077
  }
572
1078
  /**
573
- * Filter elements into a new heap of the same class.
574
- * @remarks Time O(N log N), Space O(N)
575
- * @param callback - Predicate (element, index, heap) → boolean to keep element.
576
- * @param [thisArg] - Value for `this` inside the callback.
577
- * @returns A new heap with the kept elements.
578
- */
1079
+ * Filter elements into a new heap of the same class.
1080
+ * @remarks Time O(N log N), Space O(N)
1081
+ * @param callback - Predicate (element, index, heap) → boolean to keep element.
1082
+ * @param [thisArg] - Value for `this` inside the callback.
1083
+ * @returns A new heap with the kept elements.
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
+
1113
+
1114
+ * @example
1115
+ * // Filter elements
1116
+ * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
1117
+ * const evens = heap.filter(x => x % 2 === 0);
1118
+ * console.log(evens.size); // 2;
1119
+ */
579
1120
  filter(callback, thisArg) {
580
1121
  const out = this._createInstance();
581
1122
  let i = 0;
@@ -589,15 +1130,49 @@ var _Heap = class _Heap extends IterableElementBase {
589
1130
  return out;
590
1131
  }
591
1132
  /**
592
- * Map elements into a new heap of possibly different element type.
593
- * @remarks Time O(N log N), Space O(N)
594
- * @template EM
595
- * @template RM
596
- * @param callback - Mapping function (element, index, heap) → newElement.
597
- * @param options - Options for the output heap, including comparator for EM.
598
- * @param [thisArg] - Value for `this` inside the callback.
599
- * @returns A new heap with mapped elements.
600
- */
1133
+ * Map elements into a new heap of possibly different element type.
1134
+ * @remarks Time O(N log N), Space O(N)
1135
+ * @template EM
1136
+ * @template RM
1137
+ * @param callback - Mapping function (element, index, heap) → newElement.
1138
+ * @param options - Options for the output heap, including comparator for EM.
1139
+ * @param [thisArg] - Value for `this` inside the callback.
1140
+ * @returns A new heap with mapped elements.
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
+
1169
+
1170
+ * @example
1171
+ * // Transform elements
1172
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
1173
+ * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
1174
+ * console.log(doubled.peek()); // 2;
1175
+ */
601
1176
  map(callback, options, thisArg) {
602
1177
  const { comparator, toElementFn, ...rest } = options != null ? options : {};
603
1178
  if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));