d-ary-heap 2.2.0 → 2.4.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.
package/dist/index.js CHANGED
@@ -10,6 +10,10 @@ var PriorityQueue = class _PriorityQueue {
10
10
  comparator;
11
11
  /** Key extractor for identity-based lookup */
12
12
  keyExtractor;
13
+ /** Optional hook called before operations (for instrumentation) */
14
+ onBeforeOperation;
15
+ /** Optional hook called after operations (for instrumentation) */
16
+ onAfterOperation;
13
17
  /**
14
18
  * Create a new d-ary heap priority queue.
15
19
  *
@@ -34,6 +38,8 @@ var PriorityQueue = class _PriorityQueue {
34
38
  this.depth = d;
35
39
  this.comparator = options.comparator;
36
40
  this.keyExtractor = options.keyExtractor;
41
+ this.onBeforeOperation = options.onBeforeOperation;
42
+ this.onAfterOperation = options.onAfterOperation;
37
43
  const capacity = options.initialCapacity ?? 0;
38
44
  this.container = capacity > 0 ? new Array(capacity) : [];
39
45
  if (capacity > 0) this.container.length = 0;
@@ -161,14 +167,17 @@ var PriorityQueue = class _PriorityQueue {
161
167
  * to update existing items.
162
168
  */
163
169
  insert(item) {
170
+ this.onBeforeOperation?.("insert");
164
171
  const index = this.container.length;
165
172
  this.container.push(item);
166
173
  if (index === 0) {
167
174
  this.positions.set(this.keyExtractor(item), 0);
175
+ this.onAfterOperation?.();
168
176
  return;
169
177
  }
170
178
  this.positions.set(this.keyExtractor(item), index);
171
179
  this.moveUp(index);
180
+ this.onAfterOperation?.();
172
181
  }
173
182
  /**
174
183
  * Insert multiple items into the heap.
@@ -227,13 +236,16 @@ var PriorityQueue = class _PriorityQueue {
227
236
  * This method only moves items upward for performance.
228
237
  */
229
238
  increasePriority(updatedItem) {
239
+ this.onBeforeOperation?.("increasePriority");
230
240
  const key = this.keyExtractor(updatedItem);
231
241
  const index = this.positions.get(key);
232
242
  if (index === void 0) {
243
+ this.onAfterOperation?.();
233
244
  throw new Error("Item not found in priority queue");
234
245
  }
235
246
  this.container[index] = updatedItem;
236
247
  this.moveUp(index);
248
+ this.onAfterOperation?.();
237
249
  }
238
250
  /** Alias for increasePriority() - snake_case for cross-language consistency */
239
251
  increase_priority(updatedItem) {
@@ -272,14 +284,17 @@ var PriorityQueue = class _PriorityQueue {
272
284
  * This method checks both directions for robustness.
273
285
  */
274
286
  decreasePriority(updatedItem) {
287
+ this.onBeforeOperation?.("decreasePriority");
275
288
  const key = this.keyExtractor(updatedItem);
276
289
  const index = this.positions.get(key);
277
290
  if (index === void 0) {
291
+ this.onAfterOperation?.();
278
292
  throw new Error("Item not found in priority queue");
279
293
  }
280
294
  this.container[index] = updatedItem;
281
295
  this.moveUp(index);
282
296
  this.moveDown(index);
297
+ this.onAfterOperation?.();
283
298
  }
284
299
  /** Alias for decreasePriority() - snake_case for cross-language consistency */
285
300
  decrease_priority(updatedItem) {
@@ -292,9 +307,11 @@ var PriorityQueue = class _PriorityQueue {
292
307
  * @returns The removed item, or undefined if empty
293
308
  */
294
309
  pop() {
310
+ this.onBeforeOperation?.("pop");
295
311
  const container = this.container;
296
312
  const n = container.length;
297
313
  if (n === 0) {
314
+ this.onAfterOperation?.();
298
315
  return void 0;
299
316
  }
300
317
  const keyExtractor = this.keyExtractor;
@@ -302,6 +319,7 @@ var PriorityQueue = class _PriorityQueue {
302
319
  this.positions.delete(keyExtractor(top));
303
320
  if (n === 1) {
304
321
  container.length = 0;
322
+ this.onAfterOperation?.();
305
323
  return top;
306
324
  }
307
325
  const lastItem = container[n - 1];
@@ -309,6 +327,7 @@ var PriorityQueue = class _PriorityQueue {
309
327
  this.positions.set(keyExtractor(lastItem), 0);
310
328
  container.length = n - 1;
311
329
  this.moveDown(0);
330
+ this.onAfterOperation?.();
312
331
  return top;
313
332
  }
314
333
  /**
@@ -470,6 +489,61 @@ function chain(...comparators) {
470
489
  return false;
471
490
  };
472
491
  }
492
+
493
+ // src/instrumentation.ts
494
+ function createComparisonStats() {
495
+ const stats = {
496
+ insert: 0,
497
+ pop: 0,
498
+ decreasePriority: 0,
499
+ increasePriority: 0,
500
+ get total() {
501
+ return this.insert + this.pop + this.decreasePriority + this.increasePriority;
502
+ },
503
+ reset() {
504
+ this.insert = 0;
505
+ this.pop = 0;
506
+ this.decreasePriority = 0;
507
+ this.increasePriority = 0;
508
+ }
509
+ };
510
+ return stats;
511
+ }
512
+ function instrumentComparator(comparator) {
513
+ const stats = createComparisonStats();
514
+ let currentOperation = null;
515
+ const instrumented = ((a, b) => {
516
+ if (currentOperation !== null) {
517
+ stats[currentOperation]++;
518
+ }
519
+ return comparator(a, b);
520
+ });
521
+ Object.defineProperty(instrumented, "stats", {
522
+ value: stats,
523
+ writable: false,
524
+ enumerable: true
525
+ });
526
+ instrumented.startOperation = (type) => {
527
+ currentOperation = type;
528
+ };
529
+ instrumented.endOperation = () => {
530
+ currentOperation = null;
531
+ };
532
+ return instrumented;
533
+ }
534
+ function theoreticalInsertComparisons(n, d) {
535
+ if (n <= 1) return 0;
536
+ return Math.floor(Math.log(n) / Math.log(d));
537
+ }
538
+ function theoreticalPopComparisons(n, d) {
539
+ if (n <= 1) return 0;
540
+ const height = Math.floor(Math.log(n) / Math.log(d));
541
+ return d * height;
542
+ }
543
+ function theoreticalDecreasePriorityComparisons(n, d) {
544
+ if (n <= 1) return 0;
545
+ return Math.floor(Math.log(n) / Math.log(d));
546
+ }
473
547
  /**
474
548
  * d-ary Heap Priority Queue - TypeScript Implementation
475
549
  *
@@ -480,8 +554,9 @@ function chain(...comparators) {
480
554
  * - O(1) access to highest-priority item
481
555
  * - O(log_d n) insert and priority increase operations
482
556
  * - O(d · log_d n) pop and priority decrease operations
557
+ * - Optional instrumentation hooks for performance analysis
483
558
  *
484
- * @version 2.2.0
559
+ * @version 2.4.0
485
560
  * @license Apache-2.0
486
561
  * @copyright 2023-2025 Eric Jacopin
487
562
  */
@@ -489,7 +564,85 @@ function chain(...comparators) {
489
564
  * Pre-built comparator factories for common use cases.
490
565
  *
491
566
  * @module comparators
492
- * @version 2.2.0
567
+ * @version 2.4.0
568
+ * @license Apache-2.0
569
+ */
570
+ /**
571
+ * Instrumentation utilities for d-ary heap performance analysis.
572
+ *
573
+ * This module provides opt-in instrumentation to count comparisons performed
574
+ * during heap operations. It is designed for:
575
+ *
576
+ * - **Educational purposes**: Understanding the theoretical vs actual cost of heap operations
577
+ * - **Benchmarking**: Measuring real comparison counts across different arities
578
+ * - **Visualization**: Powering interactive demos that show heap behavior
579
+ *
580
+ * ## Design Philosophy: Zero-Cost When Disabled
581
+ *
582
+ * Instrumentation follows these principles:
583
+ *
584
+ * 1. **Opt-in only**: No overhead when not using instrumentation
585
+ * 2. **Non-breaking**: Existing code continues to work unchanged
586
+ * 3. **Per-operation tracking**: Distinguish insert/pop/decreasePriority comparisons
587
+ *
588
+ * ## Cross-Language Consistency
589
+ *
590
+ * Currently, instrumentation is implemented in TypeScript only. The table below
591
+ * shows the idiomatic zero-cost approach for each language, planned for v2.5.0:
592
+ *
593
+ * | Language | Mechanism | Overhead When Disabled | Status |
594
+ * |------------|----------------------------------|------------------------|--------|
595
+ * | TypeScript | Optional hooks + instrumented comparator | Zero (JIT optimization) | ✅ Implemented |
596
+ * | Go | Nil stats pointer | ~1 cycle (nil check) | Planned v2.5.0 |
597
+ * | Rust | Generic over StatsCollector trait | Zero (monomorphization) | Planned v2.5.0 |
598
+ * | C++ | Template policy class | Zero (inlining) | Planned v2.5.0 |
599
+ * | Zig | Comptime bool parameter | Zero (branch elimination) | Planned v2.5.0 |
600
+ *
601
+ * ## Usage Example
602
+ *
603
+ * ```typescript
604
+ * import { PriorityQueue, minBy, instrumentComparator } from 'd-ary-heap';
605
+ *
606
+ * // 1. Wrap your comparator with instrumentation
607
+ * const comparator = instrumentComparator(minBy((v: Vertex) => v.distance));
608
+ *
609
+ * // 2. Create priority queue with operation hooks
610
+ * const pq = new PriorityQueue({
611
+ * d: 4,
612
+ * comparator,
613
+ * keyExtractor: (v) => v.id,
614
+ * onBeforeOperation: (op) => comparator.startOperation(op),
615
+ * onAfterOperation: () => comparator.endOperation(),
616
+ * });
617
+ *
618
+ * // 3. Use normally - comparisons are tracked automatically
619
+ * pq.insert({ id: 'A', distance: 0 });
620
+ * pq.insert({ id: 'B', distance: 5 });
621
+ * pq.pop();
622
+ *
623
+ * // 4. Access statistics
624
+ * console.log(comparator.stats);
625
+ * // { insert: 1, pop: 2, decreasePriority: 0, total: 3 }
626
+ *
627
+ * // 5. Reset for next measurement
628
+ * comparator.stats.reset();
629
+ * ```
630
+ *
631
+ * ## Theoretical Complexity Reference
632
+ *
633
+ * For a d-ary heap with n elements:
634
+ *
635
+ * | Operation | Comparisons (worst case) |
636
+ * |------------------|----------------------------|
637
+ * | insert | ⌊log_d(n)⌋ |
638
+ * | pop | d × ⌊log_d(n)⌋ |
639
+ * | decreasePriority | ⌊log_d(n)⌋ (upward only) |
640
+ * | increasePriority | d × ⌊log_d(n)⌋ (downward) |
641
+ *
642
+ * The demo visualization compares actual counts against these theoretical bounds.
643
+ *
644
+ * @module instrumentation
645
+ * @version 2.4.0
493
646
  * @license Apache-2.0
494
647
  */
495
648
  /**
@@ -499,11 +652,11 @@ function chain(...comparators) {
499
652
  *
500
653
  * @packageDocumentation
501
654
  * @module d-ary-heap
502
- * @version 2.2.0
655
+ * @version 2.4.0
503
656
  * @license Apache-2.0
504
657
  * @copyright 2023-2025 Eric Jacopin
505
658
  */
506
659
 
507
- export { PriorityQueue, chain, maxBy, maxNumber, maxString, minBy, minNumber, minString, reverse };
660
+ export { PriorityQueue, chain, createComparisonStats, instrumentComparator, maxBy, maxNumber, maxString, minBy, minNumber, minString, reverse, theoreticalDecreasePriorityComparisons, theoreticalInsertComparisons, theoreticalPopComparisons };
508
661
  //# sourceMappingURL=index.js.map
509
662
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/PriorityQueue.ts","../src/comparators.ts"],"names":[],"mappings":";AAqEO,IAAM,aAAA,GAAN,MAAM,cAAA,CAAsC;AAAA;AA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IAAM,SAAA,GAAgC,CAAC,CAAA,EAAG,CAAA,KAAM,CAAA,GAAI;AAapD,SAAS,QAAW,GAAA,EAAmC;AAC5D,EAAA,OAAO,CAAC,CAAA,EAAM,CAAA,KAAS,GAAA,CAAI,GAAG,CAAC,CAAA;AACjC;AAkBO,SAAS,SAAY,WAAA,EAA6C;AACvE,EAAA,OAAO,CAAC,GAAM,CAAA,KAAS;AACrB,IAAA,KAAA,MAAW,OAAO,WAAA,EAAa;AAC7B,MAAA,IAAI,GAAA,CAAI,CAAA,EAAG,CAAC,CAAA,EAAG,OAAO,IAAA;AACtB,MAAA,IAAI,GAAA,CAAI,CAAA,EAAG,CAAC,CAAA,EAAG,OAAO,KAAA;AAAA,IACxB;AACA,IAAA,OAAO,KAAA;AAAA,EACT,CAAA;AACF","file":"index.js","sourcesContent":["/**\r\n * d-ary Heap Priority Queue - TypeScript Implementation\r\n *\r\n * A generic d-ary heap (d-heap) priority queue with:\r\n * - Configurable arity (d): number of children per node\r\n * - Min-heap or max-heap behavior via comparator functions\r\n * - O(1) item lookup using Map for efficient priority updates\r\n * - O(1) access to highest-priority item\r\n * - O(log_d n) insert and priority increase operations\r\n * - O(d · log_d n) pop and priority decrease operations\r\n *\r\n * @version 2.2.0\r\n * @license Apache-2.0\r\n * @copyright 2023-2025 Eric Jacopin\r\n */\r\n\r\n/** Type alias for position indices (cross-language consistency) */\r\nexport type Position = number;\r\n\r\n/**\r\n * Comparator function type for priority comparison.\r\n * Returns true if `a` has higher priority than `b`.\r\n */\r\nexport type Comparator<T> = (a: T, b: T) => boolean;\r\n\r\n/**\r\n * Key extractor function type for identity-based lookup.\r\n * Must return a value that can be used as a Map key (string or number recommended).\r\n */\r\nexport type KeyExtractor<T, K> = (item: T) => K;\r\n\r\n/**\r\n * Configuration options for PriorityQueue construction.\r\n */\r\nexport interface PriorityQueueOptions<T, K> {\r\n /** Number of children per node (arity). Must be >= 1. Default: 2 */\r\n d?: number;\r\n /** Comparator function. Returns true if first arg has higher priority. */\r\n comparator: Comparator<T>;\r\n /** Key extractor for identity-based lookup. Required for decrease/increase priority. */\r\n keyExtractor: KeyExtractor<T, K>;\r\n /** Initial capacity hint for pre-allocation */\r\n initialCapacity?: number;\r\n}\r\n\r\n/**\r\n * Generic d-ary heap priority queue with O(1) lookup.\r\n *\r\n * A d-ary heap is a tree structure where:\r\n * - Each node has at most d children\r\n * - The root contains the highest-priority item\r\n * - Each parent has higher priority than all its children\r\n * - The tree is complete (filled left-to-right, level by level)\r\n *\r\n * This implementation uses an array-based representation with O(1) item lookup\r\n * via a Map that tracks each item's position in the heap.\r\n *\r\n * ## Time Complexities\r\n * - front(), peek(): O(1)\r\n * - insert(): O(log_d n)\r\n * - pop(): O(d · log_d n)\r\n * - increasePriority(): O(log_d n)\r\n * - decreasePriority(): O(d · log_d n)\r\n * - contains(): O(1)\r\n * - len(), isEmpty(), d(): O(1)\r\n *\r\n * @typeParam T - Item type stored in the queue\r\n * @typeParam K - Key type for identity lookup (typically string or number)\r\n */\r\nexport class PriorityQueue<T, K = string | number> {\r\n /** Array-based heap storage (complete tree representation) */\r\n private container: T[];\r\n\r\n /** Maps each item's key to its position in the container for O(1) lookup */\r\n private positions: Map<K, Position>;\r\n\r\n /** Number of children per node (arity of the heap) */\r\n private depth: number;\r\n\r\n /** Comparator determining heap order (min vs max) */\r\n private readonly comparator: Comparator<T>;\r\n\r\n /** Key extractor for identity-based lookup */\r\n private readonly keyExtractor: KeyExtractor<T, K>;\r\n\r\n /**\r\n * Create a new d-ary heap priority queue.\r\n *\r\n * @param options - Configuration options\r\n * @throws Error if d < 1\r\n *\r\n * @example\r\n * ```typescript\r\n * // Min-heap by cost\r\n * const pq = new PriorityQueue<Item, number>({\r\n * d: 4,\r\n * comparator: (a, b) => a.cost < b.cost,\r\n * keyExtractor: (item) => item.id\r\n * });\r\n * ```\r\n */\r\n constructor(options: PriorityQueueOptions<T, K>) {\r\n const d = options.d ?? 2;\r\n if (d < 1) {\r\n throw new Error('Heap arity (d) must be >= 1');\r\n }\r\n\r\n this.depth = d;\r\n this.comparator = options.comparator;\r\n this.keyExtractor = options.keyExtractor;\r\n\r\n // Pre-allocate if capacity hint provided\r\n const capacity = options.initialCapacity ?? 0;\r\n this.container = capacity > 0 ? new Array<T>(capacity) : [];\r\n if (capacity > 0) this.container.length = 0; // Reset length but keep capacity\r\n this.positions = new Map<K, Position>();\r\n }\r\n\r\n /**\r\n * Create a new priority queue with an initial item already inserted.\r\n * Equivalent to Rust's `with_first()` constructor.\r\n *\r\n * @param options - Configuration options\r\n * @param firstItem - First item to insert\r\n * @returns New PriorityQueue with the item already inserted\r\n */\r\n static withFirst<T, K>(\r\n options: PriorityQueueOptions<T, K>,\r\n firstItem: T\r\n ): PriorityQueue<T, K> {\r\n const pq = new PriorityQueue<T, K>(options);\r\n pq.insert(firstItem);\r\n return pq;\r\n }\r\n\r\n // ===========================================================================\r\n // Public API - Query Operations\r\n // ===========================================================================\r\n\r\n /**\r\n * Get the number of items in the heap.\r\n * Time complexity: O(1)\r\n */\r\n len(): number {\r\n return this.container.length;\r\n }\r\n\r\n /** Alias for len() - backward compatibility */\r\n get size(): number {\r\n return this.container.length;\r\n }\r\n\r\n /**\r\n * Check if the heap is empty.\r\n * Time complexity: O(1)\r\n */\r\n isEmpty(): boolean {\r\n return this.container.length === 0;\r\n }\r\n\r\n /** Alias for isEmpty() - snake_case for cross-language consistency */\r\n is_empty(): boolean {\r\n return this.isEmpty();\r\n }\r\n\r\n /**\r\n * Get the arity (number of children per node) of the heap.\r\n * Time complexity: O(1)\r\n */\r\n d(): number {\r\n return this.depth;\r\n }\r\n\r\n /**\r\n * Check if an item with the given key exists in the heap.\r\n * Time complexity: O(1)\r\n *\r\n * @param item - Item to check (uses keyExtractor for identity)\r\n */\r\n contains(item: T): boolean {\r\n return this.positions.has(this.keyExtractor(item));\r\n }\r\n\r\n /**\r\n * Check if an item with the given key exists in the heap.\r\n * Time complexity: O(1)\r\n *\r\n * @param key - Key to check directly\r\n */\r\n containsKey(key: K): boolean {\r\n return this.positions.has(key);\r\n }\r\n\r\n /**\r\n * Get the current position (index) of an item in the heap.\r\n * Time complexity: O(1)\r\n *\r\n * @param item - Item to find (uses keyExtractor for identity)\r\n * @returns Position index, or undefined if not found\r\n */\r\n getPosition(item: T): Position | undefined {\r\n return this.positions.get(this.keyExtractor(item));\r\n }\r\n\r\n /**\r\n * Get the current position (index) of an item by its key.\r\n * Time complexity: O(1)\r\n *\r\n * @param key - Key to find\r\n * @returns Position index, or undefined if not found\r\n */\r\n getPositionByKey(key: K): Position | undefined {\r\n return this.positions.get(key);\r\n }\r\n\r\n /**\r\n * Get the highest-priority item without removing it.\r\n * Time complexity: O(1)\r\n *\r\n * @returns The highest-priority item\r\n * @throws Error if heap is empty\r\n */\r\n front(): T {\r\n const item = this.container[0];\r\n if (item === undefined) {\r\n throw new Error('front() called on empty priority queue');\r\n }\r\n return item;\r\n }\r\n\r\n /**\r\n * Get the highest-priority item without removing it.\r\n * Safe alternative to front().\r\n * Time complexity: O(1)\r\n *\r\n * @returns The highest-priority item, or undefined if empty\r\n */\r\n peek(): T | undefined {\r\n return this.container.length > 0 ? this.container[0] : undefined;\r\n }\r\n\r\n // ===========================================================================\r\n // Public API - Modification Operations\r\n // ===========================================================================\r\n\r\n /**\r\n * Insert a new item into the heap.\r\n * Time complexity: O(log_d n)\r\n *\r\n * @param item - Item to insert\r\n *\r\n * @remarks\r\n * If an item with the same key already exists, behavior is undefined.\r\n * Use contains() to check first, or use increasePriority()/decreasePriority()\r\n * to update existing items.\r\n */\r\n insert(item: T): void {\r\n const index = this.container.length;\r\n this.container.push(item);\r\n\r\n // Fast path: first item doesn't need sift-up\r\n if (index === 0) {\r\n this.positions.set(this.keyExtractor(item), 0);\r\n return;\r\n }\r\n\r\n this.positions.set(this.keyExtractor(item), index);\r\n this.moveUp(index);\r\n }\r\n\r\n /**\r\n * Insert multiple items into the heap.\r\n * Uses heapify algorithm which is O(n) for bulk insertion vs O(n log n) for individual inserts.\r\n * Time complexity: O(n) where n is total items after insertion\r\n *\r\n * @param items - Array of items to insert\r\n *\r\n * @remarks\r\n * More efficient than calling insert() repeatedly when adding many items at once.\r\n * If any item has a key that already exists, behavior is undefined.\r\n */\r\n insertMany(items: T[]): void {\r\n if (items.length === 0) return;\r\n\r\n const keyExtractor = this.keyExtractor;\r\n const container = this.container;\r\n const positions = this.positions;\r\n const startIndex = container.length;\r\n\r\n // Add all items to container and positions map\r\n for (let i = 0; i < items.length; i++) {\r\n const item = items[i]!;\r\n container.push(item);\r\n positions.set(keyExtractor(item), startIndex + i);\r\n }\r\n\r\n // If this was an empty heap, use heapify (O(n)) instead of n insertions (O(n log n))\r\n if (startIndex === 0 && items.length > 1) {\r\n this.heapify();\r\n } else {\r\n // Otherwise, sift up each new item\r\n for (let i = startIndex; i < container.length; i++) {\r\n this.moveUp(i);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Build heap property from unordered array.\r\n * Uses Floyd's algorithm - O(n) time complexity.\r\n * Called internally by insertMany when starting from empty heap.\r\n */\r\n private heapify(): void {\r\n const n = this.container.length;\r\n if (n <= 1) return;\r\n\r\n const d = this.depth;\r\n // Start from last non-leaf node and sift down each\r\n // Last non-leaf is parent of last element: floor((n-2)/d)\r\n const lastNonLeaf = ((n - 2) / d) | 0;\r\n\r\n for (let i = lastNonLeaf; i >= 0; i--) {\r\n this.moveDown(i);\r\n }\r\n }\r\n\r\n /**\r\n * Increase the priority of an existing item (move toward root).\r\n * Time complexity: O(log_d n)\r\n *\r\n * @param updatedItem - Item with same identity but updated priority\r\n * @throws Error if item not found\r\n *\r\n * @remarks\r\n * For min-heap: decreasing the priority value increases importance.\r\n * For max-heap: increasing the priority value increases importance.\r\n * This method only moves items upward for performance.\r\n */\r\n increasePriority(updatedItem: T): void {\r\n const key = this.keyExtractor(updatedItem);\r\n const index = this.positions.get(key);\r\n\r\n if (index === undefined) {\r\n throw new Error('Item not found in priority queue');\r\n }\r\n\r\n this.container[index] = updatedItem;\r\n this.moveUp(index);\r\n }\r\n\r\n /** Alias for increasePriority() - snake_case for cross-language consistency */\r\n increase_priority(updatedItem: T): void {\r\n this.increasePriority(updatedItem);\r\n }\r\n\r\n /**\r\n * Increase the priority of the item at the given index.\r\n * Time complexity: O(log_d n)\r\n *\r\n * @param index - Index of the item in the heap array\r\n * @throws Error if index is out of bounds\r\n *\r\n * @remarks\r\n * This is a lower-level method. Prefer increasePriority() with the item itself.\r\n */\r\n increasePriorityByIndex(index: number): void {\r\n if (index < 0 || index >= this.container.length) {\r\n throw new Error('Index out of bounds');\r\n }\r\n this.moveUp(index);\r\n }\r\n\r\n /** Alias for increasePriorityByIndex() - snake_case for cross-language consistency */\r\n increase_priority_by_index(index: number): void {\r\n this.increasePriorityByIndex(index);\r\n }\r\n\r\n /**\r\n * Decrease the priority of an existing item (move toward leaves).\r\n * Time complexity: O(d · log_d n)\r\n *\r\n * @param updatedItem - Item with same identity but updated priority\r\n * @throws Error if item not found\r\n *\r\n * @remarks\r\n * For min-heap: increasing the priority value decreases importance.\r\n * For max-heap: decreasing the priority value decreases importance.\r\n * This method checks both directions for robustness.\r\n */\r\n decreasePriority(updatedItem: T): void {\r\n const key = this.keyExtractor(updatedItem);\r\n const index = this.positions.get(key);\r\n\r\n if (index === undefined) {\r\n throw new Error('Item not found in priority queue');\r\n }\r\n\r\n this.container[index] = updatedItem;\r\n // Check both directions since we don't know if priority actually decreased\r\n this.moveUp(index);\r\n this.moveDown(index);\r\n }\r\n\r\n /** Alias for decreasePriority() - snake_case for cross-language consistency */\r\n decrease_priority(updatedItem: T): void {\r\n this.decreasePriority(updatedItem);\r\n }\r\n\r\n /**\r\n * Remove and return the highest-priority item.\r\n * Time complexity: O(d · log_d n)\r\n *\r\n * @returns The removed item, or undefined if empty\r\n */\r\n pop(): T | undefined {\r\n const container = this.container;\r\n const n = container.length;\r\n\r\n if (n === 0) {\r\n return undefined;\r\n }\r\n\r\n const keyExtractor = this.keyExtractor;\r\n const top = container[0]!;\r\n this.positions.delete(keyExtractor(top));\r\n\r\n if (n === 1) {\r\n container.length = 0;\r\n return top;\r\n }\r\n\r\n // Move last item to root and sift down\r\n const lastItem = container[n - 1]!;\r\n container[0] = lastItem;\r\n this.positions.set(keyExtractor(lastItem), 0);\r\n container.length = n - 1;\r\n\r\n this.moveDown(0);\r\n\r\n return top;\r\n }\r\n\r\n /**\r\n * Remove and return multiple highest-priority items.\r\n * More efficient than calling pop() repeatedly.\r\n * Time complexity: O(count · d · log_d n)\r\n *\r\n * @param count - Number of items to remove\r\n * @returns Array of removed items in priority order\r\n */\r\n popMany(count: number): T[] {\r\n const result: T[] = [];\r\n const actualCount = count < this.container.length ? count : this.container.length;\r\n\r\n for (let i = 0; i < actualCount; i++) {\r\n const item = this.pop();\r\n if (item !== undefined) {\r\n result.push(item);\r\n }\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Clear all items from the heap, optionally changing the arity.\r\n * Time complexity: O(1) (references cleared, GC handles memory)\r\n *\r\n * @param newD - Optional new arity value (must be >= 1 if provided)\r\n * @throws Error if newD < 1\r\n */\r\n clear(newD?: number): void {\r\n this.container.length = 0;\r\n this.positions.clear();\r\n\r\n if (newD !== undefined) {\r\n if (newD < 1) {\r\n throw new Error('Heap arity (d) must be >= 1');\r\n }\r\n this.depth = newD;\r\n }\r\n }\r\n\r\n /**\r\n * Get a string representation of the heap contents.\r\n * Time complexity: O(n)\r\n *\r\n * @returns Formatted string showing all items in heap order\r\n */\r\n toString(): string {\r\n return '{' + this.container.map(String).join(', ') + '}';\r\n }\r\n\r\n /** Alias for toString() - snake_case for cross-language consistency */\r\n to_string(): string {\r\n return this.toString();\r\n }\r\n\r\n /**\r\n * Get all items in heap order (for debugging/iteration).\r\n * Time complexity: O(n) - creates a copy\r\n *\r\n * @returns Copy of internal array\r\n */\r\n toArray(): T[] {\r\n return [...this.container];\r\n }\r\n\r\n /**\r\n * Iterate over items in heap order (not priority order).\r\n */\r\n *[Symbol.iterator](): Iterator<T> {\r\n for (const item of this.container) {\r\n yield item;\r\n }\r\n }\r\n\r\n // ===========================================================================\r\n // Private Methods - Heap Operations\r\n // ===========================================================================\r\n\r\n /**\r\n * Swap two items in the heap and update their position mappings.\r\n * V8 optimizes simple swap patterns well.\r\n */\r\n private swap(i: number, j: number): void {\r\n const container = this.container;\r\n const temp = container[i]!;\r\n container[i] = container[j]!;\r\n container[j] = temp;\r\n\r\n // Update positions\r\n this.positions.set(this.keyExtractor(container[i]!), i);\r\n this.positions.set(this.keyExtractor(container[j]!), j);\r\n }\r\n\r\n /**\r\n * Find the child with highest priority among all children of node i.\r\n */\r\n private bestChildPosition(i: number): number {\r\n const d = this.depth;\r\n const container = this.container;\r\n const n = container.length;\r\n const left = i * d + 1;\r\n\r\n if (left >= n) return left;\r\n\r\n let best = left;\r\n const right = Math.min((i + 1) * d, n - 1);\r\n\r\n for (let j = left + 1; j <= right; j++) {\r\n if (this.comparator(container[j]!, container[best]!)) {\r\n best = j;\r\n }\r\n }\r\n\r\n return best;\r\n }\r\n\r\n /**\r\n * Move an item upward in the heap to restore heap property.\r\n * Uses simple swap pattern which V8 optimizes well.\r\n */\r\n private moveUp(i: number): void {\r\n const d = this.depth;\r\n const container = this.container;\r\n\r\n while (i > 0) {\r\n const p = Math.floor((i - 1) / d);\r\n if (this.comparator(container[i]!, container[p]!)) {\r\n this.swap(i, p);\r\n i = p;\r\n } else {\r\n break;\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Move an item downward in the heap to restore heap property.\r\n */\r\n private moveDown(i: number): void {\r\n const d = this.depth;\r\n const container = this.container;\r\n\r\n while (true) {\r\n const firstChild = i * d + 1;\r\n if (firstChild >= container.length) break;\r\n\r\n const best = this.bestChildPosition(i);\r\n if (this.comparator(container[best]!, container[i]!)) {\r\n this.swap(i, best);\r\n i = best;\r\n } else {\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n","/**\r\n * Pre-built comparator factories for common use cases.\r\n *\r\n * @module comparators\r\n * @version 2.2.0\r\n * @license Apache-2.0\r\n */\r\n\r\nimport type { Comparator } from './PriorityQueue';\r\n\r\n/**\r\n * Create a min-heap comparator using a key extractor.\r\n * Lower key values have higher priority (appear closer to root).\r\n *\r\n * @param keyFn - Function to extract the comparable key from an item\r\n * @returns Comparator function for min-heap behavior\r\n *\r\n * @example\r\n * ```typescript\r\n * const minByCost = minBy<Item, number>(item => item.cost);\r\n * ```\r\n */\r\nexport function minBy<T, K>(keyFn: (item: T) => K): Comparator<T> {\r\n return (a: T, b: T) => keyFn(a) < keyFn(b);\r\n}\r\n\r\n/**\r\n * Create a max-heap comparator using a key extractor.\r\n * Higher key values have higher priority (appear closer to root).\r\n *\r\n * @param keyFn - Function to extract the comparable key from an item\r\n * @returns Comparator function for max-heap behavior\r\n *\r\n * @example\r\n * ```typescript\r\n * const maxByCost = maxBy<Item, number>(item => item.cost);\r\n * ```\r\n */\r\nexport function maxBy<T, K>(keyFn: (item: T) => K): Comparator<T> {\r\n return (a: T, b: T) => keyFn(a) > keyFn(b);\r\n}\r\n\r\n/**\r\n * Min-heap comparator for primitive number values.\r\n * Lower numbers have higher priority.\r\n */\r\nexport const minNumber: Comparator<number> = (a, b) => a < b;\r\n\r\n/**\r\n * Max-heap comparator for primitive number values.\r\n * Higher numbers have higher priority.\r\n */\r\nexport const maxNumber: Comparator<number> = (a, b) => a > b;\r\n\r\n/**\r\n * Min-heap comparator for primitive string values.\r\n * Lexicographically smaller strings have higher priority.\r\n */\r\nexport const minString: Comparator<string> = (a, b) => a < b;\r\n\r\n/**\r\n * Max-heap comparator for primitive string values.\r\n * Lexicographically larger strings have higher priority.\r\n */\r\nexport const maxString: Comparator<string> = (a, b) => a > b;\r\n\r\n/**\r\n * Create a comparator that reverses another comparator.\r\n *\r\n * @param cmp - Original comparator to reverse\r\n * @returns Reversed comparator\r\n *\r\n * @example\r\n * ```typescript\r\n * const maxByCost = reverse(minBy<Item, number>(item => item.cost));\r\n * ```\r\n */\r\nexport function reverse<T>(cmp: Comparator<T>): Comparator<T> {\r\n return (a: T, b: T) => cmp(b, a);\r\n}\r\n\r\n/**\r\n * Create a comparator that compares by multiple keys in order.\r\n * Falls back to subsequent comparators when items are equal.\r\n *\r\n * @param comparators - Array of comparators to apply in order\r\n * @returns Combined comparator\r\n *\r\n * @example\r\n * ```typescript\r\n * // Sort by priority first, then by timestamp\r\n * const cmp = chain(\r\n * minBy<Task, number>(t => t.priority),\r\n * minBy<Task, number>(t => t.timestamp)\r\n * );\r\n * ```\r\n */\r\nexport function chain<T>(...comparators: Comparator<T>[]): Comparator<T> {\r\n return (a: T, b: T) => {\r\n for (const cmp of comparators) {\r\n if (cmp(a, b)) return true;\r\n if (cmp(b, a)) return false;\r\n }\r\n return false;\r\n };\r\n}\r\n"]}
1
+ {"version":3,"sources":["../src/PriorityQueue.ts","../src/comparators.ts","../src/instrumentation.ts"],"names":[],"mappings":";AAuGO,IAAM,aAAA,GAAN,MAAM,cAAA,CAAsC;AAAA;AAAA,EAEzC,SAAA;AAAA;AAAA,EAGA,SAAA;AAAA;AAAA,EAGA,KAAA;AAAA;AAAA,EAGS,UAAA;AAAA;AAAA,EAGA,YAAA;AAAA;AAAA,EAGA,iBAAA;AAAA;AAAA,EAGA,gBAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBjB,YAAY,OAAA,EAAqC;AAC/C,IAAA,MAAM,CAAA,GAAI,QAAQ,CAAA,IAAK,CAAA;AACvB,IAAA,IAAI,IAAI,CAAA,EAAG;AACT,MAAA,MAAM,IAAI,MAAM,6BAA6B,CAAA;AAAA,IAC/C;AAEA,IAAA,IAAA,CAAK,KAAA,GAAQ,CAAA;AACb,IAAA,IAAA,CAAK,aAAa,OAAA,CAAQ,UAAA;AAC1B,IAAA,IAAA,CAAK,eAAe,OAAA,CAAQ,YAAA;AAC5B,IAAA,IAAA,CAAK,oBAAoB,OAAA,CAAQ,iBAAA;AACj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,OAAO,IAAA,CAAK,MAAM,IAAA,CAAK,GAAA,CAAI,CAAC,CAAA,GAAI,IAAA,CAAK,GAAA,CAAI,CAAC,CAAC,CAAA;AAC7C","file":"index.js","sourcesContent":["/**\r\n * d-ary Heap Priority Queue - TypeScript Implementation\r\n *\r\n * A generic d-ary heap (d-heap) priority queue with:\r\n * - Configurable arity (d): number of children per node\r\n * - Min-heap or max-heap behavior via comparator functions\r\n * - O(1) item lookup using Map for efficient priority updates\r\n * - O(1) access to highest-priority item\r\n * - O(log_d n) insert and priority increase operations\r\n * - O(d · log_d n) pop and priority decrease operations\r\n * - Optional instrumentation hooks for performance analysis\r\n *\r\n * @version 2.4.0\r\n * @license Apache-2.0\r\n * @copyright 2023-2025 Eric Jacopin\r\n */\r\n\r\nimport type { OperationType } from './instrumentation';\r\n\r\n/** Type alias for position indices (cross-language consistency) */\r\nexport type Position = number;\r\n\r\n/**\r\n * Comparator function type for priority comparison.\r\n * Returns true if `a` has higher priority than `b`.\r\n */\r\nexport type Comparator<T> = (a: T, b: T) => boolean;\r\n\r\n/**\r\n * Key extractor function type for identity-based lookup.\r\n * Must return a value that can be used as a Map key (string or number recommended).\r\n */\r\nexport type KeyExtractor<T, K> = (item: T) => K;\r\n\r\n/**\r\n * Configuration options for PriorityQueue construction.\r\n */\r\nexport interface PriorityQueueOptions<T, K> {\r\n /** Number of children per node (arity). Must be >= 1. Default: 2 */\r\n d?: number;\r\n /** Comparator function. Returns true if first arg has higher priority. */\r\n comparator: Comparator<T>;\r\n /** Key extractor for identity-based lookup. Required for decrease/increase priority. */\r\n keyExtractor: KeyExtractor<T, K>;\r\n /** Initial capacity hint for pre-allocation */\r\n initialCapacity?: number;\r\n\r\n /**\r\n * Optional hook called before each heap operation.\r\n *\r\n * This enables opt-in instrumentation for performance analysis without\r\n * adding overhead when not used. Pair with an instrumented comparator\r\n * to track comparison counts per operation type.\r\n *\r\n * @param type - The operation about to be performed\r\n *\r\n * @example\r\n * ```typescript\r\n * const cmp = instrumentComparator(minBy((v) => v.priority));\r\n * const pq = new PriorityQueue({\r\n * comparator: cmp,\r\n * keyExtractor: (v) => v.id,\r\n * onBeforeOperation: (op) => cmp.startOperation(op),\r\n * onAfterOperation: () => cmp.endOperation(),\r\n * });\r\n * ```\r\n *\r\n * @see instrumentComparator from './instrumentation'\r\n */\r\n onBeforeOperation?: (type: OperationType) => void;\r\n\r\n /**\r\n * Optional hook called after each heap operation completes.\r\n *\r\n * @see onBeforeOperation for usage example\r\n */\r\n onAfterOperation?: () => void;\r\n}\r\n\r\n/**\r\n * Generic d-ary heap priority queue with O(1) lookup.\r\n *\r\n * A d-ary heap is a tree structure where:\r\n * - Each node has at most d children\r\n * - The root contains the highest-priority item\r\n * - Each parent has higher priority than all its children\r\n * - The tree is complete (filled left-to-right, level by level)\r\n *\r\n * This implementation uses an array-based representation with O(1) item lookup\r\n * via a Map that tracks each item's position in the heap.\r\n *\r\n * ## Time Complexities\r\n * - front(), peek(): O(1)\r\n * - insert(): O(log_d n)\r\n * - pop(): O(d · log_d n)\r\n * - increasePriority(): O(log_d n)\r\n * - decreasePriority(): O(d · log_d n)\r\n * - contains(): O(1)\r\n * - len(), isEmpty(), d(): O(1)\r\n *\r\n * @typeParam T - Item type stored in the queue\r\n * @typeParam K - Key type for identity lookup (typically string or number)\r\n */\r\nexport class PriorityQueue<T, K = string | number> {\r\n /** Array-based heap storage (complete tree representation) */\r\n private container: T[];\r\n\r\n /** Maps each item's key to its position in the container for O(1) lookup */\r\n private positions: Map<K, Position>;\r\n\r\n /** Number of children per node (arity of the heap) */\r\n private depth: number;\r\n\r\n /** Comparator determining heap order (min vs max) */\r\n private readonly comparator: Comparator<T>;\r\n\r\n /** Key extractor for identity-based lookup */\r\n private readonly keyExtractor: KeyExtractor<T, K>;\r\n\r\n /** Optional hook called before operations (for instrumentation) */\r\n private readonly onBeforeOperation: ((type: OperationType) => void) | undefined;\r\n\r\n /** Optional hook called after operations (for instrumentation) */\r\n private readonly onAfterOperation: (() => void) | undefined;\r\n\r\n /**\r\n * Create a new d-ary heap priority queue.\r\n *\r\n * @param options - Configuration options\r\n * @throws Error if d < 1\r\n *\r\n * @example\r\n * ```typescript\r\n * // Min-heap by cost\r\n * const pq = new PriorityQueue<Item, number>({\r\n * d: 4,\r\n * comparator: (a, b) => a.cost < b.cost,\r\n * keyExtractor: (item) => item.id\r\n * });\r\n * ```\r\n */\r\n constructor(options: PriorityQueueOptions<T, K>) {\r\n const d = options.d ?? 2;\r\n if (d < 1) {\r\n throw new Error('Heap arity (d) must be >= 1');\r\n }\r\n\r\n this.depth = d;\r\n this.comparator = options.comparator;\r\n this.keyExtractor = options.keyExtractor;\r\n this.onBeforeOperation = options.onBeforeOperation;\r\n this.onAfterOperation = options.onAfterOperation;\r\n\r\n // Pre-allocate if capacity hint provided\r\n const capacity = options.initialCapacity ?? 0;\r\n this.container = capacity > 0 ? new Array<T>(capacity) : [];\r\n if (capacity > 0) this.container.length = 0; // Reset length but keep capacity\r\n this.positions = new Map<K, Position>();\r\n }\r\n\r\n /**\r\n * Create a new priority queue with an initial item already inserted.\r\n * Equivalent to Rust's `with_first()` constructor.\r\n *\r\n * @param options - Configuration options\r\n * @param firstItem - First item to insert\r\n * @returns New PriorityQueue with the item already inserted\r\n */\r\n static withFirst<T, K>(\r\n options: PriorityQueueOptions<T, K>,\r\n firstItem: T\r\n ): PriorityQueue<T, K> {\r\n const pq = new PriorityQueue<T, K>(options);\r\n pq.insert(firstItem);\r\n return pq;\r\n }\r\n\r\n // ===========================================================================\r\n // Public API - Query Operations\r\n // ===========================================================================\r\n\r\n /**\r\n * Get the number of items in the heap.\r\n * Time complexity: O(1)\r\n */\r\n len(): number {\r\n return this.container.length;\r\n }\r\n\r\n /** Alias for len() - backward compatibility */\r\n get size(): number {\r\n return this.container.length;\r\n }\r\n\r\n /**\r\n * Check if the heap is empty.\r\n * Time complexity: O(1)\r\n */\r\n isEmpty(): boolean {\r\n return this.container.length === 0;\r\n }\r\n\r\n /** Alias for isEmpty() - snake_case for cross-language consistency */\r\n is_empty(): boolean {\r\n return this.isEmpty();\r\n }\r\n\r\n /**\r\n * Get the arity (number of children per node) of the heap.\r\n * Time complexity: O(1)\r\n */\r\n d(): number {\r\n return this.depth;\r\n }\r\n\r\n /**\r\n * Check if an item with the given key exists in the heap.\r\n * Time complexity: O(1)\r\n *\r\n * @param item - Item to check (uses keyExtractor for identity)\r\n */\r\n contains(item: T): boolean {\r\n return this.positions.has(this.keyExtractor(item));\r\n }\r\n\r\n /**\r\n * Check if an item with the given key exists in the heap.\r\n * Time complexity: O(1)\r\n *\r\n * @param key - Key to check directly\r\n */\r\n containsKey(key: K): boolean {\r\n return this.positions.has(key);\r\n }\r\n\r\n /**\r\n * Get the current position (index) of an item in the heap.\r\n * Time complexity: O(1)\r\n *\r\n * @param item - Item to find (uses keyExtractor for identity)\r\n * @returns Position index, or undefined if not found\r\n */\r\n getPosition(item: T): Position | undefined {\r\n return this.positions.get(this.keyExtractor(item));\r\n }\r\n\r\n /**\r\n * Get the current position (index) of an item by its key.\r\n * Time complexity: O(1)\r\n *\r\n * @param key - Key to find\r\n * @returns Position index, or undefined if not found\r\n */\r\n getPositionByKey(key: K): Position | undefined {\r\n return this.positions.get(key);\r\n }\r\n\r\n /**\r\n * Get the highest-priority item without removing it.\r\n * Time complexity: O(1)\r\n *\r\n * @returns The highest-priority item\r\n * @throws Error if heap is empty\r\n */\r\n front(): T {\r\n const item = this.container[0];\r\n if (item === undefined) {\r\n throw new Error('front() called on empty priority queue');\r\n }\r\n return item;\r\n }\r\n\r\n /**\r\n * Get the highest-priority item without removing it.\r\n * Safe alternative to front().\r\n * Time complexity: O(1)\r\n *\r\n * @returns The highest-priority item, or undefined if empty\r\n */\r\n peek(): T | undefined {\r\n return this.container.length > 0 ? this.container[0] : undefined;\r\n }\r\n\r\n // ===========================================================================\r\n // Public API - Modification Operations\r\n // ===========================================================================\r\n\r\n /**\r\n * Insert a new item into the heap.\r\n * Time complexity: O(log_d n)\r\n *\r\n * @param item - Item to insert\r\n *\r\n * @remarks\r\n * If an item with the same key already exists, behavior is undefined.\r\n * Use contains() to check first, or use increasePriority()/decreasePriority()\r\n * to update existing items.\r\n */\r\n insert(item: T): void {\r\n this.onBeforeOperation?.('insert');\r\n\r\n const index = this.container.length;\r\n this.container.push(item);\r\n\r\n // Fast path: first item doesn't need sift-up\r\n if (index === 0) {\r\n this.positions.set(this.keyExtractor(item), 0);\r\n this.onAfterOperation?.();\r\n return;\r\n }\r\n\r\n this.positions.set(this.keyExtractor(item), index);\r\n this.moveUp(index);\r\n\r\n this.onAfterOperation?.();\r\n }\r\n\r\n /**\r\n * Insert multiple items into the heap.\r\n * Uses heapify algorithm which is O(n) for bulk insertion vs O(n log n) for individual inserts.\r\n * Time complexity: O(n) where n is total items after insertion\r\n *\r\n * @param items - Array of items to insert\r\n *\r\n * @remarks\r\n * More efficient than calling insert() repeatedly when adding many items at once.\r\n * If any item has a key that already exists, behavior is undefined.\r\n */\r\n insertMany(items: T[]): void {\r\n if (items.length === 0) return;\r\n\r\n const keyExtractor = this.keyExtractor;\r\n const container = this.container;\r\n const positions = this.positions;\r\n const startIndex = container.length;\r\n\r\n // Add all items to container and positions map\r\n for (let i = 0; i < items.length; i++) {\r\n const item = items[i]!;\r\n container.push(item);\r\n positions.set(keyExtractor(item), startIndex + i);\r\n }\r\n\r\n // If this was an empty heap, use heapify (O(n)) instead of n insertions (O(n log n))\r\n if (startIndex === 0 && items.length > 1) {\r\n this.heapify();\r\n } else {\r\n // Otherwise, sift up each new item\r\n for (let i = startIndex; i < container.length; i++) {\r\n this.moveUp(i);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Build heap property from unordered array.\r\n * Uses Floyd's algorithm - O(n) time complexity.\r\n * Called internally by insertMany when starting from empty heap.\r\n */\r\n private heapify(): void {\r\n const n = this.container.length;\r\n if (n <= 1) return;\r\n\r\n const d = this.depth;\r\n // Start from last non-leaf node and sift down each\r\n // Last non-leaf is parent of last element: floor((n-2)/d)\r\n const lastNonLeaf = ((n - 2) / d) | 0;\r\n\r\n for (let i = lastNonLeaf; i >= 0; i--) {\r\n this.moveDown(i);\r\n }\r\n }\r\n\r\n /**\r\n * Increase the priority of an existing item (move toward root).\r\n * Time complexity: O(log_d n)\r\n *\r\n * @param updatedItem - Item with same identity but updated priority\r\n * @throws Error if item not found\r\n *\r\n * @remarks\r\n * For min-heap: decreasing the priority value increases importance.\r\n * For max-heap: increasing the priority value increases importance.\r\n * This method only moves items upward for performance.\r\n */\r\n increasePriority(updatedItem: T): void {\r\n this.onBeforeOperation?.('increasePriority');\r\n\r\n const key = this.keyExtractor(updatedItem);\r\n const index = this.positions.get(key);\r\n\r\n if (index === undefined) {\r\n this.onAfterOperation?.();\r\n throw new Error('Item not found in priority queue');\r\n }\r\n\r\n this.container[index] = updatedItem;\r\n this.moveUp(index);\r\n\r\n this.onAfterOperation?.();\r\n }\r\n\r\n /** Alias for increasePriority() - snake_case for cross-language consistency */\r\n increase_priority(updatedItem: T): void {\r\n this.increasePriority(updatedItem);\r\n }\r\n\r\n /**\r\n * Increase the priority of the item at the given index.\r\n * Time complexity: O(log_d n)\r\n *\r\n * @param index - Index of the item in the heap array\r\n * @throws Error if index is out of bounds\r\n *\r\n * @remarks\r\n * This is a lower-level method. Prefer increasePriority() with the item itself.\r\n */\r\n increasePriorityByIndex(index: number): void {\r\n if (index < 0 || index >= this.container.length) {\r\n throw new Error('Index out of bounds');\r\n }\r\n this.moveUp(index);\r\n }\r\n\r\n /** Alias for increasePriorityByIndex() - snake_case for cross-language consistency */\r\n increase_priority_by_index(index: number): void {\r\n this.increasePriorityByIndex(index);\r\n }\r\n\r\n /**\r\n * Decrease the priority of an existing item (move toward leaves).\r\n * Time complexity: O(d · log_d n)\r\n *\r\n * @param updatedItem - Item with same identity but updated priority\r\n * @throws Error if item not found\r\n *\r\n * @remarks\r\n * For min-heap: increasing the priority value decreases importance.\r\n * For max-heap: decreasing the priority value decreases importance.\r\n * This method checks both directions for robustness.\r\n */\r\n decreasePriority(updatedItem: T): void {\r\n this.onBeforeOperation?.('decreasePriority');\r\n\r\n const key = this.keyExtractor(updatedItem);\r\n const index = this.positions.get(key);\r\n\r\n if (index === undefined) {\r\n this.onAfterOperation?.();\r\n throw new Error('Item not found in priority queue');\r\n }\r\n\r\n this.container[index] = updatedItem;\r\n // Check both directions since we don't know if priority actually decreased\r\n this.moveUp(index);\r\n this.moveDown(index);\r\n\r\n this.onAfterOperation?.();\r\n }\r\n\r\n /** Alias for decreasePriority() - snake_case for cross-language consistency */\r\n decrease_priority(updatedItem: T): void {\r\n this.decreasePriority(updatedItem);\r\n }\r\n\r\n /**\r\n * Remove and return the highest-priority item.\r\n * Time complexity: O(d · log_d n)\r\n *\r\n * @returns The removed item, or undefined if empty\r\n */\r\n pop(): T | undefined {\r\n this.onBeforeOperation?.('pop');\r\n\r\n const container = this.container;\r\n const n = container.length;\r\n\r\n if (n === 0) {\r\n this.onAfterOperation?.();\r\n return undefined;\r\n }\r\n\r\n const keyExtractor = this.keyExtractor;\r\n const top = container[0]!;\r\n this.positions.delete(keyExtractor(top));\r\n\r\n if (n === 1) {\r\n container.length = 0;\r\n this.onAfterOperation?.();\r\n return top;\r\n }\r\n\r\n // Move last item to root and sift down\r\n const lastItem = container[n - 1]!;\r\n container[0] = lastItem;\r\n this.positions.set(keyExtractor(lastItem), 0);\r\n container.length = n - 1;\r\n\r\n this.moveDown(0);\r\n\r\n this.onAfterOperation?.();\r\n return top;\r\n }\r\n\r\n /**\r\n * Remove and return multiple highest-priority items.\r\n * More efficient than calling pop() repeatedly.\r\n * Time complexity: O(count · d · log_d n)\r\n *\r\n * @param count - Number of items to remove\r\n * @returns Array of removed items in priority order\r\n */\r\n popMany(count: number): T[] {\r\n const result: T[] = [];\r\n const actualCount = count < this.container.length ? count : this.container.length;\r\n\r\n for (let i = 0; i < actualCount; i++) {\r\n const item = this.pop();\r\n if (item !== undefined) {\r\n result.push(item);\r\n }\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Clear all items from the heap, optionally changing the arity.\r\n * Time complexity: O(1) (references cleared, GC handles memory)\r\n *\r\n * @param newD - Optional new arity value (must be >= 1 if provided)\r\n * @throws Error if newD < 1\r\n */\r\n clear(newD?: number): void {\r\n this.container.length = 0;\r\n this.positions.clear();\r\n\r\n if (newD !== undefined) {\r\n if (newD < 1) {\r\n throw new Error('Heap arity (d) must be >= 1');\r\n }\r\n this.depth = newD;\r\n }\r\n }\r\n\r\n /**\r\n * Get a string representation of the heap contents.\r\n * Time complexity: O(n)\r\n *\r\n * @returns Formatted string showing all items in heap order\r\n */\r\n toString(): string {\r\n return '{' + this.container.map(String).join(', ') + '}';\r\n }\r\n\r\n /** Alias for toString() - snake_case for cross-language consistency */\r\n to_string(): string {\r\n return this.toString();\r\n }\r\n\r\n /**\r\n * Get all items in heap order (for debugging/iteration).\r\n * Time complexity: O(n) - creates a copy\r\n *\r\n * @returns Copy of internal array\r\n */\r\n toArray(): T[] {\r\n return [...this.container];\r\n }\r\n\r\n /**\r\n * Iterate over items in heap order (not priority order).\r\n */\r\n *[Symbol.iterator](): Iterator<T> {\r\n for (const item of this.container) {\r\n yield item;\r\n }\r\n }\r\n\r\n // ===========================================================================\r\n // Private Methods - Heap Operations\r\n // ===========================================================================\r\n\r\n /**\r\n * Swap two items in the heap and update their position mappings.\r\n * V8 optimizes simple swap patterns well.\r\n */\r\n private swap(i: number, j: number): void {\r\n const container = this.container;\r\n const temp = container[i]!;\r\n container[i] = container[j]!;\r\n container[j] = temp;\r\n\r\n // Update positions\r\n this.positions.set(this.keyExtractor(container[i]!), i);\r\n this.positions.set(this.keyExtractor(container[j]!), j);\r\n }\r\n\r\n /**\r\n * Find the child with highest priority among all children of node i.\r\n */\r\n private bestChildPosition(i: number): number {\r\n const d = this.depth;\r\n const container = this.container;\r\n const n = container.length;\r\n const left = i * d + 1;\r\n\r\n if (left >= n) return left;\r\n\r\n let best = left;\r\n const right = Math.min((i + 1) * d, n - 1);\r\n\r\n for (let j = left + 1; j <= right; j++) {\r\n if (this.comparator(container[j]!, container[best]!)) {\r\n best = j;\r\n }\r\n }\r\n\r\n return best;\r\n }\r\n\r\n /**\r\n * Move an item upward in the heap to restore heap property.\r\n * Uses simple swap pattern which V8 optimizes well.\r\n */\r\n private moveUp(i: number): void {\r\n const d = this.depth;\r\n const container = this.container;\r\n\r\n while (i > 0) {\r\n const p = Math.floor((i - 1) / d);\r\n if (this.comparator(container[i]!, container[p]!)) {\r\n this.swap(i, p);\r\n i = p;\r\n } else {\r\n break;\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Move an item downward in the heap to restore heap property.\r\n */\r\n private moveDown(i: number): void {\r\n const d = this.depth;\r\n const container = this.container;\r\n\r\n while (true) {\r\n const firstChild = i * d + 1;\r\n if (firstChild >= container.length) break;\r\n\r\n const best = this.bestChildPosition(i);\r\n if (this.comparator(container[best]!, container[i]!)) {\r\n this.swap(i, best);\r\n i = best;\r\n } else {\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n","/**\r\n * Pre-built comparator factories for common use cases.\r\n *\r\n * @module comparators\r\n * @version 2.4.0\r\n * @license Apache-2.0\r\n */\r\n\r\nimport type { Comparator } from './PriorityQueue';\r\n\r\n/**\r\n * Create a min-heap comparator using a key extractor.\r\n * Lower key values have higher priority (appear closer to root).\r\n *\r\n * @param keyFn - Function to extract the comparable key from an item\r\n * @returns Comparator function for min-heap behavior\r\n *\r\n * @example\r\n * ```typescript\r\n * const minByCost = minBy<Item, number>(item => item.cost);\r\n * ```\r\n */\r\nexport function minBy<T, K>(keyFn: (item: T) => K): Comparator<T> {\r\n return (a: T, b: T) => keyFn(a) < keyFn(b);\r\n}\r\n\r\n/**\r\n * Create a max-heap comparator using a key extractor.\r\n * Higher key values have higher priority (appear closer to root).\r\n *\r\n * @param keyFn - Function to extract the comparable key from an item\r\n * @returns Comparator function for max-heap behavior\r\n *\r\n * @example\r\n * ```typescript\r\n * const maxByCost = maxBy<Item, number>(item => item.cost);\r\n * ```\r\n */\r\nexport function maxBy<T, K>(keyFn: (item: T) => K): Comparator<T> {\r\n return (a: T, b: T) => keyFn(a) > keyFn(b);\r\n}\r\n\r\n/**\r\n * Min-heap comparator for primitive number values.\r\n * Lower numbers have higher priority.\r\n */\r\nexport const minNumber: Comparator<number> = (a, b) => a < b;\r\n\r\n/**\r\n * Max-heap comparator for primitive number values.\r\n * Higher numbers have higher priority.\r\n */\r\nexport const maxNumber: Comparator<number> = (a, b) => a > b;\r\n\r\n/**\r\n * Min-heap comparator for primitive string values.\r\n * Lexicographically smaller strings have higher priority.\r\n */\r\nexport const minString: Comparator<string> = (a, b) => a < b;\r\n\r\n/**\r\n * Max-heap comparator for primitive string values.\r\n * Lexicographically larger strings have higher priority.\r\n */\r\nexport const maxString: Comparator<string> = (a, b) => a > b;\r\n\r\n/**\r\n * Create a comparator that reverses another comparator.\r\n *\r\n * @param cmp - Original comparator to reverse\r\n * @returns Reversed comparator\r\n *\r\n * @example\r\n * ```typescript\r\n * const maxByCost = reverse(minBy<Item, number>(item => item.cost));\r\n * ```\r\n */\r\nexport function reverse<T>(cmp: Comparator<T>): Comparator<T> {\r\n return (a: T, b: T) => cmp(b, a);\r\n}\r\n\r\n/**\r\n * Create a comparator that compares by multiple keys in order.\r\n * Falls back to subsequent comparators when items are equal.\r\n *\r\n * @param comparators - Array of comparators to apply in order\r\n * @returns Combined comparator\r\n *\r\n * @example\r\n * ```typescript\r\n * // Sort by priority first, then by timestamp\r\n * const cmp = chain(\r\n * minBy<Task, number>(t => t.priority),\r\n * minBy<Task, number>(t => t.timestamp)\r\n * );\r\n * ```\r\n */\r\nexport function chain<T>(...comparators: Comparator<T>[]): Comparator<T> {\r\n return (a: T, b: T) => {\r\n for (const cmp of comparators) {\r\n if (cmp(a, b)) return true;\r\n if (cmp(b, a)) return false;\r\n }\r\n return false;\r\n };\r\n}\r\n","/**\r\n * Instrumentation utilities for d-ary heap performance analysis.\r\n *\r\n * This module provides opt-in instrumentation to count comparisons performed\r\n * during heap operations. It is designed for:\r\n *\r\n * - **Educational purposes**: Understanding the theoretical vs actual cost of heap operations\r\n * - **Benchmarking**: Measuring real comparison counts across different arities\r\n * - **Visualization**: Powering interactive demos that show heap behavior\r\n *\r\n * ## Design Philosophy: Zero-Cost When Disabled\r\n *\r\n * Instrumentation follows these principles:\r\n *\r\n * 1. **Opt-in only**: No overhead when not using instrumentation\r\n * 2. **Non-breaking**: Existing code continues to work unchanged\r\n * 3. **Per-operation tracking**: Distinguish insert/pop/decreasePriority comparisons\r\n *\r\n * ## Cross-Language Consistency\r\n *\r\n * Currently, instrumentation is implemented in TypeScript only. The table below\r\n * shows the idiomatic zero-cost approach for each language, planned for v2.5.0:\r\n *\r\n * | Language | Mechanism | Overhead When Disabled | Status |\r\n * |------------|----------------------------------|------------------------|--------|\r\n * | TypeScript | Optional hooks + instrumented comparator | Zero (JIT optimization) | ✅ Implemented |\r\n * | Go | Nil stats pointer | ~1 cycle (nil check) | Planned v2.5.0 |\r\n * | Rust | Generic over StatsCollector trait | Zero (monomorphization) | Planned v2.5.0 |\r\n * | C++ | Template policy class | Zero (inlining) | Planned v2.5.0 |\r\n * | Zig | Comptime bool parameter | Zero (branch elimination) | Planned v2.5.0 |\r\n *\r\n * ## Usage Example\r\n *\r\n * ```typescript\r\n * import { PriorityQueue, minBy, instrumentComparator } from 'd-ary-heap';\r\n *\r\n * // 1. Wrap your comparator with instrumentation\r\n * const comparator = instrumentComparator(minBy((v: Vertex) => v.distance));\r\n *\r\n * // 2. Create priority queue with operation hooks\r\n * const pq = new PriorityQueue({\r\n * d: 4,\r\n * comparator,\r\n * keyExtractor: (v) => v.id,\r\n * onBeforeOperation: (op) => comparator.startOperation(op),\r\n * onAfterOperation: () => comparator.endOperation(),\r\n * });\r\n *\r\n * // 3. Use normally - comparisons are tracked automatically\r\n * pq.insert({ id: 'A', distance: 0 });\r\n * pq.insert({ id: 'B', distance: 5 });\r\n * pq.pop();\r\n *\r\n * // 4. Access statistics\r\n * console.log(comparator.stats);\r\n * // { insert: 1, pop: 2, decreasePriority: 0, total: 3 }\r\n *\r\n * // 5. Reset for next measurement\r\n * comparator.stats.reset();\r\n * ```\r\n *\r\n * ## Theoretical Complexity Reference\r\n *\r\n * For a d-ary heap with n elements:\r\n *\r\n * | Operation | Comparisons (worst case) |\r\n * |------------------|----------------------------|\r\n * | insert | ⌊log_d(n)⌋ |\r\n * | pop | d × ⌊log_d(n)⌋ |\r\n * | decreasePriority | ⌊log_d(n)⌋ (upward only) |\r\n * | increasePriority | d × ⌊log_d(n)⌋ (downward) |\r\n *\r\n * The demo visualization compares actual counts against these theoretical bounds.\r\n *\r\n * @module instrumentation\r\n * @version 2.4.0\r\n * @license Apache-2.0\r\n */\r\n\r\nimport type { Comparator } from './PriorityQueue';\r\n\r\n/**\r\n * Operation types that can be tracked.\r\n *\r\n * Note: `increasePriority` is tracked separately because in Dijkstra's algorithm\r\n * it manifests as decreasePriority (lowering distance = higher priority in min-heap).\r\n */\r\nexport type OperationType = 'insert' | 'pop' | 'decreasePriority' | 'increasePriority';\r\n\r\n/**\r\n * Statistics tracking comparison counts per operation type.\r\n *\r\n * All counts start at zero and accumulate until `reset()` is called.\r\n */\r\nexport interface ComparisonStats {\r\n /** Comparisons during insert operations (moveUp) */\r\n insert: number;\r\n\r\n /** Comparisons during pop operations (moveDown + bestChildPosition) */\r\n pop: number;\r\n\r\n /** Comparisons during decreasePriority operations (moveUp + moveDown) */\r\n decreasePriority: number;\r\n\r\n /** Comparisons during increasePriority operations (moveUp) */\r\n increasePriority: number;\r\n\r\n /** Total comparisons across all operation types */\r\n readonly total: number;\r\n\r\n /** Reset all counters to zero */\r\n reset(): void;\r\n}\r\n\r\n/**\r\n * An instrumented comparator that tracks comparison counts.\r\n *\r\n * This extends a regular comparator with:\r\n * - `stats`: Current comparison counts\r\n * - `startOperation(type)`: Begin tracking for an operation\r\n * - `endOperation()`: Stop tracking current operation\r\n *\r\n * The comparator itself remains a valid `Comparator<T>` and can be used\r\n * anywhere a regular comparator is expected.\r\n */\r\nexport interface InstrumentedComparator<T> extends Comparator<T> {\r\n /** Current comparison statistics */\r\n readonly stats: ComparisonStats;\r\n\r\n /**\r\n * Signal the start of a heap operation.\r\n * Comparisons will be attributed to this operation type until `endOperation()`.\r\n *\r\n * @param type - The operation type being started\r\n */\r\n startOperation(type: OperationType): void;\r\n\r\n /**\r\n * Signal the end of the current heap operation.\r\n * Subsequent comparisons will not be counted until the next `startOperation()`.\r\n */\r\n endOperation(): void;\r\n}\r\n\r\n/**\r\n * Create comparison statistics tracker.\r\n *\r\n * @returns Fresh stats object with all counts at zero\r\n *\r\n * @example\r\n * ```typescript\r\n * const stats = createComparisonStats();\r\n * stats.insert = 5;\r\n * stats.pop = 10;\r\n * console.log(stats.total); // 15\r\n * stats.reset();\r\n * console.log(stats.total); // 0\r\n * ```\r\n */\r\nexport function createComparisonStats(): ComparisonStats {\r\n const stats: ComparisonStats = {\r\n insert: 0,\r\n pop: 0,\r\n decreasePriority: 0,\r\n increasePriority: 0,\r\n\r\n get total(): number {\r\n return this.insert + this.pop + this.decreasePriority + this.increasePriority;\r\n },\r\n\r\n reset(): void {\r\n this.insert = 0;\r\n this.pop = 0;\r\n this.decreasePriority = 0;\r\n this.increasePriority = 0;\r\n },\r\n };\r\n\r\n return stats;\r\n}\r\n\r\n/**\r\n * Wrap a comparator with instrumentation to track comparison counts.\r\n *\r\n * The returned comparator:\r\n * - Behaves identically to the original for comparison purposes\r\n * - Tracks how many times it's called, attributed to operation types\r\n * - Has zero overhead when `startOperation()` hasn't been called\r\n *\r\n * ## How It Works\r\n *\r\n * 1. Call `startOperation('insert')` before `pq.insert()`\r\n * 2. The comparator increments `stats.insert` for each comparison\r\n * 3. Call `endOperation()` after the operation completes\r\n * 4. Repeat for other operations\r\n *\r\n * The `PriorityQueue` class supports `onBeforeOperation` and `onAfterOperation`\r\n * hooks to automate this.\r\n *\r\n * ## Performance Note\r\n *\r\n * When `currentOperation` is null (between operations), the instrumented\r\n * comparator performs only a single null check before calling the original.\r\n * Modern JavaScript engines optimize this extremely well.\r\n *\r\n * @param comparator - The original comparator to instrument\r\n * @returns An instrumented comparator with stats tracking\r\n *\r\n * @example\r\n * ```typescript\r\n * import { minBy, instrumentComparator } from 'd-ary-heap';\r\n *\r\n * const cmp = instrumentComparator(minBy<number, number>(x => x));\r\n *\r\n * // Manual usage (without hooks)\r\n * cmp.startOperation('insert');\r\n * console.log(cmp(5, 3)); // false, and stats.insert++\r\n * console.log(cmp(3, 5)); // true, and stats.insert++\r\n * cmp.endOperation();\r\n *\r\n * console.log(cmp.stats.insert); // 2\r\n * ```\r\n */\r\nexport function instrumentComparator<T>(comparator: Comparator<T>): InstrumentedComparator<T> {\r\n const stats = createComparisonStats();\r\n let currentOperation: OperationType | null = null;\r\n\r\n // Create the instrumented function\r\n const instrumented = ((a: T, b: T): boolean => {\r\n // Only count when actively tracking an operation\r\n if (currentOperation !== null) {\r\n stats[currentOperation]++;\r\n }\r\n return comparator(a, b);\r\n }) as InstrumentedComparator<T>;\r\n\r\n // Attach stats (read-only from outside)\r\n Object.defineProperty(instrumented, 'stats', {\r\n value: stats,\r\n writable: false,\r\n enumerable: true,\r\n });\r\n\r\n // Attach operation control methods\r\n instrumented.startOperation = (type: OperationType): void => {\r\n currentOperation = type;\r\n };\r\n\r\n instrumented.endOperation = (): void => {\r\n currentOperation = null;\r\n };\r\n\r\n return instrumented;\r\n}\r\n\r\n/**\r\n * Calculate theoretical comparison count for an insert operation.\r\n *\r\n * Insert performs at most ⌊log_d(n)⌋ comparisons (one per level during moveUp).\r\n *\r\n * @param n - Number of elements in heap AFTER insert\r\n * @param d - Heap arity\r\n * @returns Theoretical worst-case comparison count\r\n */\r\nexport function theoreticalInsertComparisons(n: number, d: number): number {\r\n if (n <= 1) return 0;\r\n return Math.floor(Math.log(n) / Math.log(d));\r\n}\r\n\r\n/**\r\n * Calculate theoretical comparison count for a pop operation.\r\n *\r\n * Pop performs at most d × ⌊log_d(n)⌋ comparisons:\r\n * - At each level, find best among d children (d-1 comparisons)\r\n * - Compare best child with current (1 comparison)\r\n * - Total: d comparisons per level × ⌊log_d(n)⌋ levels\r\n *\r\n * @param n - Number of elements in heap BEFORE pop\r\n * @param d - Heap arity\r\n * @returns Theoretical worst-case comparison count\r\n */\r\nexport function theoreticalPopComparisons(n: number, d: number): number {\r\n if (n <= 1) return 0;\r\n const height = Math.floor(Math.log(n) / Math.log(d));\r\n return d * height;\r\n}\r\n\r\n/**\r\n * Calculate theoretical comparison count for a decreasePriority operation.\r\n *\r\n * DecreasePriority in our implementation calls both moveUp and moveDown\r\n * for safety, but typically only moveUp executes (upward movement).\r\n * Worst case: ⌊log_d(n)⌋ comparisons.\r\n *\r\n * @param n - Number of elements in heap\r\n * @param d - Heap arity\r\n * @returns Theoretical worst-case comparison count (moveUp path)\r\n */\r\nexport function theoreticalDecreasePriorityComparisons(n: number, d: number): number {\r\n if (n <= 1) return 0;\r\n return Math.floor(Math.log(n) / Math.log(d));\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "d-ary-heap",
4
- "version": "2.2.0",
4
+ "version": "2.4.0",
5
5
  "description": "High-performance d-ary heap priority queue with O(1) item lookup, configurable arity, and min/max heap support",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -8,12 +8,15 @@
8
8
  * - O(1) access to highest-priority item
9
9
  * - O(log_d n) insert and priority increase operations
10
10
  * - O(d · log_d n) pop and priority decrease operations
11
+ * - Optional instrumentation hooks for performance analysis
11
12
  *
12
- * @version 2.2.0
13
+ * @version 2.4.0
13
14
  * @license Apache-2.0
14
15
  * @copyright 2023-2025 Eric Jacopin
15
16
  */
16
17
 
18
+ import type { OperationType } from './instrumentation';
19
+
17
20
  /** Type alias for position indices (cross-language consistency) */
18
21
  export type Position = number;
19
22
 
@@ -41,6 +44,37 @@ export interface PriorityQueueOptions<T, K> {
41
44
  keyExtractor: KeyExtractor<T, K>;
42
45
  /** Initial capacity hint for pre-allocation */
43
46
  initialCapacity?: number;
47
+
48
+ /**
49
+ * Optional hook called before each heap operation.
50
+ *
51
+ * This enables opt-in instrumentation for performance analysis without
52
+ * adding overhead when not used. Pair with an instrumented comparator
53
+ * to track comparison counts per operation type.
54
+ *
55
+ * @param type - The operation about to be performed
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const cmp = instrumentComparator(minBy((v) => v.priority));
60
+ * const pq = new PriorityQueue({
61
+ * comparator: cmp,
62
+ * keyExtractor: (v) => v.id,
63
+ * onBeforeOperation: (op) => cmp.startOperation(op),
64
+ * onAfterOperation: () => cmp.endOperation(),
65
+ * });
66
+ * ```
67
+ *
68
+ * @see instrumentComparator from './instrumentation'
69
+ */
70
+ onBeforeOperation?: (type: OperationType) => void;
71
+
72
+ /**
73
+ * Optional hook called after each heap operation completes.
74
+ *
75
+ * @see onBeforeOperation for usage example
76
+ */
77
+ onAfterOperation?: () => void;
44
78
  }
45
79
 
46
80
  /**
@@ -83,6 +117,12 @@ export class PriorityQueue<T, K = string | number> {
83
117
  /** Key extractor for identity-based lookup */
84
118
  private readonly keyExtractor: KeyExtractor<T, K>;
85
119
 
120
+ /** Optional hook called before operations (for instrumentation) */
121
+ private readonly onBeforeOperation: ((type: OperationType) => void) | undefined;
122
+
123
+ /** Optional hook called after operations (for instrumentation) */
124
+ private readonly onAfterOperation: (() => void) | undefined;
125
+
86
126
  /**
87
127
  * Create a new d-ary heap priority queue.
88
128
  *
@@ -108,6 +148,8 @@ export class PriorityQueue<T, K = string | number> {
108
148
  this.depth = d;
109
149
  this.comparator = options.comparator;
110
150
  this.keyExtractor = options.keyExtractor;
151
+ this.onBeforeOperation = options.onBeforeOperation;
152
+ this.onAfterOperation = options.onAfterOperation;
111
153
 
112
154
  // Pre-allocate if capacity hint provided
113
155
  const capacity = options.initialCapacity ?? 0;
@@ -255,17 +297,22 @@ export class PriorityQueue<T, K = string | number> {
255
297
  * to update existing items.
256
298
  */
257
299
  insert(item: T): void {
300
+ this.onBeforeOperation?.('insert');
301
+
258
302
  const index = this.container.length;
259
303
  this.container.push(item);
260
304
 
261
305
  // Fast path: first item doesn't need sift-up
262
306
  if (index === 0) {
263
307
  this.positions.set(this.keyExtractor(item), 0);
308
+ this.onAfterOperation?.();
264
309
  return;
265
310
  }
266
311
 
267
312
  this.positions.set(this.keyExtractor(item), index);
268
313
  this.moveUp(index);
314
+
315
+ this.onAfterOperation?.();
269
316
  }
270
317
 
271
318
  /**
@@ -337,15 +384,20 @@ export class PriorityQueue<T, K = string | number> {
337
384
  * This method only moves items upward for performance.
338
385
  */
339
386
  increasePriority(updatedItem: T): void {
387
+ this.onBeforeOperation?.('increasePriority');
388
+
340
389
  const key = this.keyExtractor(updatedItem);
341
390
  const index = this.positions.get(key);
342
391
 
343
392
  if (index === undefined) {
393
+ this.onAfterOperation?.();
344
394
  throw new Error('Item not found in priority queue');
345
395
  }
346
396
 
347
397
  this.container[index] = updatedItem;
348
398
  this.moveUp(index);
399
+
400
+ this.onAfterOperation?.();
349
401
  }
350
402
 
351
403
  /** Alias for increasePriority() - snake_case for cross-language consistency */
@@ -388,10 +440,13 @@ export class PriorityQueue<T, K = string | number> {
388
440
  * This method checks both directions for robustness.
389
441
  */
390
442
  decreasePriority(updatedItem: T): void {
443
+ this.onBeforeOperation?.('decreasePriority');
444
+
391
445
  const key = this.keyExtractor(updatedItem);
392
446
  const index = this.positions.get(key);
393
447
 
394
448
  if (index === undefined) {
449
+ this.onAfterOperation?.();
395
450
  throw new Error('Item not found in priority queue');
396
451
  }
397
452
 
@@ -399,6 +454,8 @@ export class PriorityQueue<T, K = string | number> {
399
454
  // Check both directions since we don't know if priority actually decreased
400
455
  this.moveUp(index);
401
456
  this.moveDown(index);
457
+
458
+ this.onAfterOperation?.();
402
459
  }
403
460
 
404
461
  /** Alias for decreasePriority() - snake_case for cross-language consistency */
@@ -413,10 +470,13 @@ export class PriorityQueue<T, K = string | number> {
413
470
  * @returns The removed item, or undefined if empty
414
471
  */
415
472
  pop(): T | undefined {
473
+ this.onBeforeOperation?.('pop');
474
+
416
475
  const container = this.container;
417
476
  const n = container.length;
418
477
 
419
478
  if (n === 0) {
479
+ this.onAfterOperation?.();
420
480
  return undefined;
421
481
  }
422
482
 
@@ -426,6 +486,7 @@ export class PriorityQueue<T, K = string | number> {
426
486
 
427
487
  if (n === 1) {
428
488
  container.length = 0;
489
+ this.onAfterOperation?.();
429
490
  return top;
430
491
  }
431
492
 
@@ -437,6 +498,7 @@ export class PriorityQueue<T, K = string | number> {
437
498
 
438
499
  this.moveDown(0);
439
500
 
501
+ this.onAfterOperation?.();
440
502
  return top;
441
503
  }
442
504
 
@@ -2,7 +2,7 @@
2
2
  * Pre-built comparator factories for common use cases.
3
3
  *
4
4
  * @module comparators
5
- * @version 2.2.0
5
+ * @version 2.4.0
6
6
  * @license Apache-2.0
7
7
  */
8
8