priority-queue-typed 2.4.4 → 2.5.0

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 (85) hide show
  1. package/README.md +66 -0
  2. package/dist/cjs/index.cjs +404 -101
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +403 -100
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +404 -102
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +403 -101
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/priority-queue-typed.js +401 -98
  46. package/dist/umd/priority-queue-typed.js.map +1 -1
  47. package/dist/umd/priority-queue-typed.min.js +1 -1
  48. package/dist/umd/priority-queue-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -24,6 +24,7 @@ var priorityQueueTyped = (() => {
24
24
  var src_exports = {};
25
25
  __export(src_exports, {
26
26
  DFSOperation: () => DFSOperation,
27
+ ERR: () => ERR,
27
28
  FibonacciHeap: () => FibonacciHeap,
28
29
  FibonacciHeapNode: () => FibonacciHeapNode,
29
30
  Heap: () => Heap,
@@ -255,6 +256,52 @@ var priorityQueueTyped = (() => {
255
256
  }
256
257
  };
257
258
 
259
+ // src/common/error.ts
260
+ var ERR = {
261
+ // Range / index
262
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
263
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
264
+ // Type / argument
265
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
266
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
267
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
268
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
269
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
270
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
271
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
272
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
273
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
274
+ // State / operation
275
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
276
+ // Matrix
277
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
278
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
279
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
280
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
281
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
282
+ };
283
+
284
+ // src/common/index.ts
285
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
286
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
287
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
288
+ return DFSOperation2;
289
+ })(DFSOperation || {});
290
+ var Range = class {
291
+ constructor(low, high, includeLow = true, includeHigh = true) {
292
+ this.low = low;
293
+ this.high = high;
294
+ this.includeLow = includeLow;
295
+ this.includeHigh = includeHigh;
296
+ }
297
+ // Determine whether a key is within the range
298
+ isInRange(key, comparator) {
299
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
300
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
301
+ return lowCheck && highCheck;
302
+ }
303
+ };
304
+
258
305
  // src/data-structures/heap/heap.ts
259
306
  var Heap = class _Heap extends IterableElementBase {
260
307
  /**
@@ -270,7 +317,7 @@ var priorityQueueTyped = (() => {
270
317
  __publicField(this, "_elements", []);
271
318
  __publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
272
319
  if (typeof a === "object" || typeof b === "object") {
273
- throw TypeError("When comparing object types, define a custom comparator in options.");
320
+ throw new TypeError(ERR.comparatorRequired("Heap"));
274
321
  }
275
322
  if (a > b) return 1;
276
323
  if (a < b) return -1;
@@ -292,10 +339,30 @@ var priorityQueueTyped = (() => {
292
339
  return this._elements;
293
340
  }
294
341
  /**
295
- * Get the number of elements.
296
- * @remarks Time O(1), Space O(1)
297
- * @returns Heap size.
298
- */
342
+ * Get the number of elements.
343
+ * @remarks Time O(1), Space O(1)
344
+ * @returns Heap size.
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+ * @example
357
+ * // Track heap capacity
358
+ * const heap = new Heap<number>();
359
+ * console.log(heap.size); // 0;
360
+ * heap.add(10);
361
+ * heap.add(20);
362
+ * console.log(heap.size); // 2;
363
+ * heap.poll();
364
+ * console.log(heap.size); // 1;
365
+ */
299
366
  get size() {
300
367
  return this.elements.length;
301
368
  }
@@ -334,21 +401,61 @@ var priorityQueueTyped = (() => {
334
401
  return new _Heap(elements, options);
335
402
  }
336
403
  /**
337
- * Insert an element.
338
- * @remarks Time O(1) amortized, Space O(1)
339
- * @param element - Element to insert.
340
- * @returns True.
341
- */
404
+ * Insert an element.
405
+ * @remarks Time O(1) amortized, Space O(1)
406
+ * @param element - Element to insert.
407
+ * @returns True.
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+ * @example
420
+ * // basic Heap creation and add operation
421
+ * // Create a min heap (default)
422
+ * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
423
+ *
424
+ * // Verify size
425
+ * console.log(minHeap.size); // 6;
426
+ *
427
+ * // Add new element
428
+ * minHeap.add(4);
429
+ * console.log(minHeap.size); // 7;
430
+ *
431
+ * // Min heap property: smallest element at root
432
+ * const min = minHeap.peek();
433
+ * console.log(min); // 1;
434
+ */
342
435
  add(element) {
343
436
  this._elements.push(element);
344
437
  return this._bubbleUp(this.elements.length - 1);
345
438
  }
346
439
  /**
347
- * Insert many elements from an iterable.
348
- * @remarks Time O(N log N), Space O(1)
349
- * @param elements - Iterable of elements or raw values.
350
- * @returns Array of per-element success flags.
351
- */
440
+ * Insert many elements from an iterable.
441
+ * @remarks Time O(N log N), Space O(1)
442
+ * @param elements - Iterable of elements or raw values.
443
+ * @returns Array of per-element success flags.
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+ * @example
453
+ * // Add multiple elements
454
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
455
+ * heap.addMany([5, 3, 7, 1]);
456
+ * console.log(heap.peek()); // 1;
457
+ * console.log(heap.size); // 4;
458
+ */
352
459
  addMany(elements) {
353
460
  const flags = [];
354
461
  for (const el of elements) {
@@ -363,10 +470,46 @@ var priorityQueueTyped = (() => {
363
470
  return flags;
364
471
  }
365
472
  /**
366
- * Remove and return the top element.
367
- * @remarks Time O(log N), Space O(1)
368
- * @returns Top element or undefined.
369
- */
473
+ * Remove and return the top element.
474
+ * @remarks Time O(log N), Space O(1)
475
+ * @returns Top element or undefined.
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+ * @example
488
+ * // Heap with custom comparator (MaxHeap behavior)
489
+ * interface Task {
490
+ * id: number;
491
+ * priority: number;
492
+ * name: string;
493
+ * }
494
+ *
495
+ * // Custom comparator for max heap behavior (higher priority first)
496
+ * const tasks: Task[] = [
497
+ * { id: 1, priority: 5, name: 'Email' },
498
+ * { id: 2, priority: 3, name: 'Chat' },
499
+ * { id: 3, priority: 8, name: 'Alert' }
500
+ * ];
501
+ *
502
+ * const maxHeap = new Heap(tasks, {
503
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
504
+ * });
505
+ *
506
+ * console.log(maxHeap.size); // 3;
507
+ *
508
+ * // Peek returns highest priority task
509
+ * const topTask = maxHeap.peek();
510
+ * console.log(topTask?.priority); // 8;
511
+ * console.log(topTask?.name); // 'Alert';
512
+ */
370
513
  poll() {
371
514
  if (this.elements.length === 0) return;
372
515
  const value = this.elements[0];
@@ -378,26 +521,125 @@ var priorityQueueTyped = (() => {
378
521
  return value;
379
522
  }
380
523
  /**
381
- * Get the current top element without removing it.
382
- * @remarks Time O(1), Space O(1)
383
- * @returns Top element or undefined.
384
- */
524
+ * Get the current top element without removing it.
525
+ * @remarks Time O(1), Space O(1)
526
+ * @returns Top element or undefined.
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+ * @example
539
+ * // Heap for event processing with priority
540
+ * interface Event {
541
+ * id: number;
542
+ * type: 'critical' | 'warning' | 'info';
543
+ * timestamp: number;
544
+ * message: string;
545
+ * }
546
+ *
547
+ * // Custom priority: critical > warning > info
548
+ * const priorityMap = { critical: 3, warning: 2, info: 1 };
549
+ *
550
+ * const eventHeap = new Heap<Event>([], {
551
+ * comparator: (a: Event, b: Event) => {
552
+ * const priorityA = priorityMap[a.type];
553
+ * const priorityB = priorityMap[b.type];
554
+ * return priorityB - priorityA; // Higher priority first
555
+ * }
556
+ * });
557
+ *
558
+ * // Add events in random order
559
+ * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
560
+ * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
561
+ * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
562
+ * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
563
+ * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
564
+ *
565
+ * console.log(eventHeap.size); // 5;
566
+ *
567
+ * // Process events by priority (critical first)
568
+ * const processedOrder: Event[] = [];
569
+ * while (eventHeap.size > 0) {
570
+ * const event = eventHeap.poll();
571
+ * if (event) {
572
+ * processedOrder.push(event);
573
+ * }
574
+ * }
575
+ *
576
+ * // Verify critical events came first
577
+ * console.log(processedOrder[0].type); // 'critical';
578
+ * console.log(processedOrder[1].type); // 'critical';
579
+ * console.log(processedOrder[2].type); // 'warning';
580
+ * console.log(processedOrder[3].type); // 'info';
581
+ * console.log(processedOrder[4].type); // 'info';
582
+ *
583
+ * // Verify O(log n) operations
584
+ * const newHeap = new Heap<number>([5, 3, 7, 1]);
585
+ *
586
+ * // Add - O(log n)
587
+ * newHeap.add(2);
588
+ * console.log(newHeap.size); // 5;
589
+ *
590
+ * // Poll - O(log n)
591
+ * const removed = newHeap.poll();
592
+ * console.log(removed); // 1;
593
+ *
594
+ * // Peek - O(1)
595
+ * const top = newHeap.peek();
596
+ * console.log(top); // 2;
597
+ */
385
598
  peek() {
386
599
  return this.elements[0];
387
600
  }
388
601
  /**
389
- * Check whether the heap is empty.
390
- * @remarks Time O(1), Space O(1)
391
- * @returns True if size is 0.
392
- */
602
+ * Check whether the heap is empty.
603
+ * @remarks Time O(1), Space O(1)
604
+ * @returns True if size is 0.
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+ * @example
615
+ * // Check if heap is empty
616
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
617
+ * console.log(heap.isEmpty()); // true;
618
+ * heap.add(1);
619
+ * console.log(heap.isEmpty()); // false;
620
+ */
393
621
  isEmpty() {
394
622
  return this.size === 0;
395
623
  }
396
624
  /**
397
- * Remove all elements.
398
- * @remarks Time O(1), Space O(1)
399
- * @returns void
400
- */
625
+ * Remove all elements.
626
+ * @remarks Time O(1), Space O(1)
627
+ * @returns void
628
+
629
+
630
+
631
+
632
+
633
+
634
+
635
+
636
+
637
+ * @example
638
+ * // Remove all elements
639
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
640
+ * heap.clear();
641
+ * console.log(heap.isEmpty()); // true;
642
+ */
401
643
  clear() {
402
644
  this._elements = [];
403
645
  }
@@ -412,21 +654,41 @@ var priorityQueueTyped = (() => {
412
654
  return this.fix();
413
655
  }
414
656
  /**
415
- * Check if an equal element exists in the heap.
416
- * @remarks Time O(N), Space O(1)
417
- * @param element - Element to search for.
418
- * @returns True if found.
419
- */
657
+ * Check if an equal element exists in the heap.
658
+ * @remarks Time O(N), Space O(1)
659
+ * @param element - Element to search for.
660
+ * @returns True if found.
661
+
662
+
663
+ * @example
664
+ * // Check element existence
665
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
666
+ * console.log(heap.has(1)); // true;
667
+ * console.log(heap.has(99)); // false;
668
+ */
420
669
  has(element) {
421
670
  for (const el of this.elements) if (this._equals(el, element)) return true;
422
671
  return false;
423
672
  }
424
673
  /**
425
- * Delete one occurrence of an element.
426
- * @remarks Time O(N), Space O(1)
427
- * @param element - Element to delete.
428
- * @returns True if an element was removed.
429
- */
674
+ * Delete one occurrence of an element.
675
+ * @remarks Time O(N), Space O(1)
676
+ * @param element - Element to delete.
677
+ * @returns True if an element was removed.
678
+
679
+
680
+
681
+
682
+
683
+
684
+
685
+
686
+ * @example
687
+ * // Remove specific element
688
+ * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
689
+ * heap.delete(4);
690
+ * console.log(heap.toArray().includes(4)); // false;
691
+ */
430
692
  delete(element) {
431
693
  let index = -1;
432
694
  for (let i = 0; i < this.elements.length; i++) {
@@ -484,11 +746,18 @@ var priorityQueueTyped = (() => {
484
746
  return this;
485
747
  }
486
748
  /**
487
- * Traverse the binary heap as a complete binary tree and collect elements.
488
- * @remarks Time O(N), Space O(H)
489
- * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
490
- * @returns Array of visited elements.
491
- */
749
+ * Traverse the binary heap as a complete binary tree and collect elements.
750
+ * @remarks Time O(N), Space O(H)
751
+ * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
752
+ * @returns Array of visited elements.
753
+
754
+
755
+ * @example
756
+ * // Depth-first traversal
757
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
758
+ * const result = heap.dfs('IN');
759
+ * console.log(result.length); // 3;
760
+ */
492
761
  dfs(order = "PRE") {
493
762
  const result = [];
494
763
  const _dfs = (index) => {
@@ -525,10 +794,26 @@ var priorityQueueTyped = (() => {
525
794
  return results;
526
795
  }
527
796
  /**
528
- * Return all elements in ascending order by repeatedly polling.
529
- * @remarks Time O(N log N), Space O(N)
530
- * @returns Sorted array of elements.
531
- */
797
+ * Return all elements in ascending order by repeatedly polling.
798
+ * @remarks Time O(N log N), Space O(N)
799
+ * @returns Sorted array of elements.
800
+
801
+
802
+
803
+
804
+
805
+
806
+
807
+
808
+
809
+
810
+
811
+ * @example
812
+ * // Sort elements using heap
813
+ * const heap = new Heap<number>([5, 1, 3, 2, 4]);
814
+ * const sorted = heap.sort();
815
+ * console.log(sorted); // [1, 2, 3, 4, 5];
816
+ */
532
817
  sort() {
533
818
  const visited = [];
534
819
  const cloned = this._createInstance();
@@ -540,22 +825,52 @@ var priorityQueueTyped = (() => {
540
825
  return visited;
541
826
  }
542
827
  /**
543
- * Deep clone this heap.
544
- * @remarks Time O(N), Space O(N)
545
- * @returns A new heap with the same elements.
546
- */
828
+ * Deep clone this heap.
829
+ * @remarks Time O(N), Space O(N)
830
+ * @returns A new heap with the same elements.
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+ * @example
841
+ * // Create independent copy
842
+ * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
843
+ * const copy = heap.clone();
844
+ * copy.poll();
845
+ * console.log(heap.size); // 3;
846
+ * console.log(copy.size); // 2;
847
+ */
547
848
  clone() {
548
849
  const next = this._createInstance();
549
850
  for (const x of this.elements) next.add(x);
550
851
  return next;
551
852
  }
552
853
  /**
553
- * Filter elements into a new heap of the same class.
554
- * @remarks Time O(N log N), Space O(N)
555
- * @param callback - Predicate (element, index, heap) → boolean to keep element.
556
- * @param [thisArg] - Value for `this` inside the callback.
557
- * @returns A new heap with the kept elements.
558
- */
854
+ * Filter elements into a new heap of the same class.
855
+ * @remarks Time O(N log N), Space O(N)
856
+ * @param callback - Predicate (element, index, heap) → boolean to keep element.
857
+ * @param [thisArg] - Value for `this` inside the callback.
858
+ * @returns A new heap with the kept elements.
859
+
860
+
861
+
862
+
863
+
864
+
865
+
866
+
867
+
868
+ * @example
869
+ * // Filter elements
870
+ * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
871
+ * const evens = heap.filter(x => x % 2 === 0);
872
+ * console.log(evens.size); // 2;
873
+ */
559
874
  filter(callback, thisArg) {
560
875
  const out = this._createInstance();
561
876
  let i = 0;
@@ -569,18 +884,31 @@ var priorityQueueTyped = (() => {
569
884
  return out;
570
885
  }
571
886
  /**
572
- * Map elements into a new heap of possibly different element type.
573
- * @remarks Time O(N log N), Space O(N)
574
- * @template EM
575
- * @template RM
576
- * @param callback - Mapping function (element, index, heap) → newElement.
577
- * @param options - Options for the output heap, including comparator for EM.
578
- * @param [thisArg] - Value for `this` inside the callback.
579
- * @returns A new heap with mapped elements.
580
- */
887
+ * Map elements into a new heap of possibly different element type.
888
+ * @remarks Time O(N log N), Space O(N)
889
+ * @template EM
890
+ * @template RM
891
+ * @param callback - Mapping function (element, index, heap) → newElement.
892
+ * @param options - Options for the output heap, including comparator for EM.
893
+ * @param [thisArg] - Value for `this` inside the callback.
894
+ * @returns A new heap with mapped elements.
895
+
896
+
897
+
898
+
899
+
900
+
901
+
902
+
903
+ * @example
904
+ * // Transform elements
905
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
906
+ * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
907
+ * console.log(doubled.peek()); // 2;
908
+ */
581
909
  map(callback, options, thisArg) {
582
910
  const { comparator, toElementFn, ...rest } = options != null ? options : {};
583
- if (!comparator) throw new TypeError("Heap.map requires options.comparator for EM");
911
+ if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
584
912
  const out = this._createLike([], { ...rest, comparator, toElementFn });
585
913
  let i = 0;
586
914
  for (const x of this) {
@@ -708,7 +1036,7 @@ var priorityQueueTyped = (() => {
708
1036
  __publicField(this, "_comparator");
709
1037
  this.clear();
710
1038
  this._comparator = comparator || this._defaultComparator;
711
- if (typeof this.comparator !== "function") throw new Error("FibonacciHeap: comparator must be a function.");
1039
+ if (typeof this.comparator !== "function") throw new TypeError(ERR.notAFunction("comparator", "FibonacciHeap"));
712
1040
  }
713
1041
  /**
714
1042
  * Get the circular root list head.
@@ -917,9 +1245,7 @@ var priorityQueueTyped = (() => {
917
1245
  super(elements, {
918
1246
  comparator: (a, b) => {
919
1247
  if (typeof a === "object" || typeof b === "object") {
920
- throw TypeError(
921
- `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
922
- );
1248
+ throw new TypeError(ERR.comparatorRequired("MaxHeap"));
923
1249
  }
924
1250
  if (a < b) return 1;
925
1251
  if (a > b) return -1;
@@ -975,9 +1301,7 @@ var priorityQueueTyped = (() => {
975
1301
  super(elements, {
976
1302
  comparator: (a, b) => {
977
1303
  if (typeof a === "object" || typeof b === "object") {
978
- throw TypeError(
979
- `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
980
- );
1304
+ throw new TypeError(ERR.comparatorRequired("MaxPriorityQueue"));
981
1305
  }
982
1306
  if (a < b) return 1;
983
1307
  if (a > b) return -1;
@@ -987,27 +1311,6 @@ var priorityQueueTyped = (() => {
987
1311
  });
988
1312
  }
989
1313
  };
990
-
991
- // src/common/index.ts
992
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
993
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
994
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
995
- return DFSOperation2;
996
- })(DFSOperation || {});
997
- var Range = class {
998
- constructor(low, high, includeLow = true, includeHigh = true) {
999
- this.low = low;
1000
- this.high = high;
1001
- this.includeLow = includeLow;
1002
- this.includeHigh = includeHigh;
1003
- }
1004
- // Determine whether a key is within the range
1005
- isInRange(key, comparator) {
1006
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1007
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1008
- return lowCheck && highCheck;
1009
- }
1010
- };
1011
1314
  return __toCommonJS(src_exports);
1012
1315
  })();
1013
1316
  /**