d-ary-heap 2.1.2 → 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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ![TypeScript](https://img.shields.io/badge/TypeScript-5.3-blue.svg)
2
2
  ![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-green.svg)
3
3
 
4
- # d-Heap Priority Queue (TypeScript) v2.1.2
4
+ # d-Heap Priority Queue (TypeScript) v2.4.0
5
5
 
6
6
  A high-performance, generic d-ary heap priority queue with O(1) item lookup, supporting both min-heap and max-heap behavior.
7
7
 
@@ -154,6 +154,87 @@ const byPriorityThenTime = chain(
154
154
  );
155
155
  ```
156
156
 
157
+ ## Instrumentation (v2.4.0+)
158
+
159
+ The library provides opt-in instrumentation for counting comparisons during heap operations. This is useful for:
160
+
161
+ - **Educational purposes**: Understanding theoretical vs actual comparison costs
162
+ - **Benchmarking**: Measuring real comparison counts across different arities
163
+ - **Visualization**: Powering interactive demos that show heap behavior
164
+
165
+ ### Zero-Cost When Disabled
166
+
167
+ Instrumentation follows a zero-overhead design:
168
+ - No performance impact when not using instrumentation
169
+ - Existing code works unchanged
170
+ - Per-operation tracking distinguishes insert/pop/decreasePriority comparisons
171
+
172
+ ### Usage
173
+
174
+ ```typescript
175
+ import {
176
+ PriorityQueue,
177
+ minBy,
178
+ instrumentComparator,
179
+ theoreticalInsertComparisons,
180
+ theoreticalPopComparisons
181
+ } from 'd-ary-heap';
182
+
183
+ // 1. Wrap your comparator with instrumentation
184
+ const comparator = instrumentComparator(minBy((v: Vertex) => v.distance));
185
+
186
+ // 2. Create priority queue with operation hooks
187
+ const pq = new PriorityQueue({
188
+ d: 4,
189
+ comparator,
190
+ keyExtractor: (v) => v.id,
191
+ onBeforeOperation: (op) => comparator.startOperation(op),
192
+ onAfterOperation: () => comparator.endOperation(),
193
+ });
194
+
195
+ // 3. Use normally - comparisons are tracked automatically
196
+ pq.insert({ id: 'A', distance: 0 });
197
+ pq.insert({ id: 'B', distance: 5 });
198
+ pq.insert({ id: 'C', distance: 3 });
199
+ pq.pop();
200
+
201
+ // 4. Access statistics
202
+ console.log(comparator.stats);
203
+ // { insert: 3, pop: 2, decreasePriority: 0, total: 5 }
204
+
205
+ // 5. Compare with theoretical bounds
206
+ const n = 3;
207
+ const d = 4;
208
+ console.log('Theoretical insert (worst):', theoreticalInsertComparisons(n, d));
209
+ console.log('Theoretical pop (worst):', theoreticalPopComparisons(n, d));
210
+
211
+ // 6. Reset for next measurement
212
+ comparator.stats.reset();
213
+ ```
214
+
215
+ ### Theoretical Complexity
216
+
217
+ For a d-ary heap with n elements:
218
+
219
+ | Operation | Comparisons (worst case) |
220
+ |------------------|----------------------------|
221
+ | insert | floor(log_d(n)) |
222
+ | pop | d × floor(log_d(n)) |
223
+ | decreasePriority | floor(log_d(n)) (upward) |
224
+ | increasePriority | d × floor(log_d(n)) (downward) |
225
+
226
+ ### Cross-Language Consistency
227
+
228
+ Currently, instrumentation is implemented in TypeScript only. The table below shows the idiomatic zero-cost approach for each language, planned for v2.5.0:
229
+
230
+ | Language | Mechanism | Overhead When Disabled | Status |
231
+ |------------|----------------------------------|------------------------|--------|
232
+ | TypeScript | Optional hooks + instrumented comparator | Zero (JIT optimization) | ✅ Implemented |
233
+ | Go | Nil stats pointer | ~1 cycle (nil check) | Planned v2.5.0 |
234
+ | Rust | Generic over StatsCollector trait | Zero (monomorphization) | Planned v2.5.0 |
235
+ | C++ | Template policy class | Zero (inlining) | Planned v2.5.0 |
236
+ | Zig | Comptime bool parameter | Zero (branch elimination) | Planned v2.5.0 |
237
+
157
238
  ## Priority Update Semantics
158
239
 
159
240
  The `increasePriority()` and `decreasePriority()` methods have an intentionally **asymmetric design**:
package/dist/index.cjs CHANGED
@@ -12,6 +12,10 @@ var PriorityQueue = class _PriorityQueue {
12
12
  comparator;
13
13
  /** Key extractor for identity-based lookup */
14
14
  keyExtractor;
15
+ /** Optional hook called before operations (for instrumentation) */
16
+ onBeforeOperation;
17
+ /** Optional hook called after operations (for instrumentation) */
18
+ onAfterOperation;
15
19
  /**
16
20
  * Create a new d-ary heap priority queue.
17
21
  *
@@ -36,6 +40,8 @@ var PriorityQueue = class _PriorityQueue {
36
40
  this.depth = d;
37
41
  this.comparator = options.comparator;
38
42
  this.keyExtractor = options.keyExtractor;
43
+ this.onBeforeOperation = options.onBeforeOperation;
44
+ this.onAfterOperation = options.onAfterOperation;
39
45
  const capacity = options.initialCapacity ?? 0;
40
46
  this.container = capacity > 0 ? new Array(capacity) : [];
41
47
  if (capacity > 0) this.container.length = 0;
@@ -163,14 +169,17 @@ var PriorityQueue = class _PriorityQueue {
163
169
  * to update existing items.
164
170
  */
165
171
  insert(item) {
172
+ this.onBeforeOperation?.("insert");
166
173
  const index = this.container.length;
167
174
  this.container.push(item);
168
175
  if (index === 0) {
169
176
  this.positions.set(this.keyExtractor(item), 0);
177
+ this.onAfterOperation?.();
170
178
  return;
171
179
  }
172
180
  this.positions.set(this.keyExtractor(item), index);
173
181
  this.moveUp(index);
182
+ this.onAfterOperation?.();
174
183
  }
175
184
  /**
176
185
  * Insert multiple items into the heap.
@@ -229,13 +238,16 @@ var PriorityQueue = class _PriorityQueue {
229
238
  * This method only moves items upward for performance.
230
239
  */
231
240
  increasePriority(updatedItem) {
241
+ this.onBeforeOperation?.("increasePriority");
232
242
  const key = this.keyExtractor(updatedItem);
233
243
  const index = this.positions.get(key);
234
244
  if (index === void 0) {
245
+ this.onAfterOperation?.();
235
246
  throw new Error("Item not found in priority queue");
236
247
  }
237
248
  this.container[index] = updatedItem;
238
249
  this.moveUp(index);
250
+ this.onAfterOperation?.();
239
251
  }
240
252
  /** Alias for increasePriority() - snake_case for cross-language consistency */
241
253
  increase_priority(updatedItem) {
@@ -274,14 +286,17 @@ var PriorityQueue = class _PriorityQueue {
274
286
  * This method checks both directions for robustness.
275
287
  */
276
288
  decreasePriority(updatedItem) {
289
+ this.onBeforeOperation?.("decreasePriority");
277
290
  const key = this.keyExtractor(updatedItem);
278
291
  const index = this.positions.get(key);
279
292
  if (index === void 0) {
293
+ this.onAfterOperation?.();
280
294
  throw new Error("Item not found in priority queue");
281
295
  }
282
296
  this.container[index] = updatedItem;
283
297
  this.moveUp(index);
284
298
  this.moveDown(index);
299
+ this.onAfterOperation?.();
285
300
  }
286
301
  /** Alias for decreasePriority() - snake_case for cross-language consistency */
287
302
  decrease_priority(updatedItem) {
@@ -294,9 +309,11 @@ var PriorityQueue = class _PriorityQueue {
294
309
  * @returns The removed item, or undefined if empty
295
310
  */
296
311
  pop() {
312
+ this.onBeforeOperation?.("pop");
297
313
  const container = this.container;
298
314
  const n = container.length;
299
315
  if (n === 0) {
316
+ this.onAfterOperation?.();
300
317
  return void 0;
301
318
  }
302
319
  const keyExtractor = this.keyExtractor;
@@ -304,6 +321,7 @@ var PriorityQueue = class _PriorityQueue {
304
321
  this.positions.delete(keyExtractor(top));
305
322
  if (n === 1) {
306
323
  container.length = 0;
324
+ this.onAfterOperation?.();
307
325
  return top;
308
326
  }
309
327
  const lastItem = container[n - 1];
@@ -311,6 +329,7 @@ var PriorityQueue = class _PriorityQueue {
311
329
  this.positions.set(keyExtractor(lastItem), 0);
312
330
  container.length = n - 1;
313
331
  this.moveDown(0);
332
+ this.onAfterOperation?.();
314
333
  return top;
315
334
  }
316
335
  /**
@@ -472,6 +491,61 @@ function chain(...comparators) {
472
491
  return false;
473
492
  };
474
493
  }
494
+
495
+ // src/instrumentation.ts
496
+ function createComparisonStats() {
497
+ const stats = {
498
+ insert: 0,
499
+ pop: 0,
500
+ decreasePriority: 0,
501
+ increasePriority: 0,
502
+ get total() {
503
+ return this.insert + this.pop + this.decreasePriority + this.increasePriority;
504
+ },
505
+ reset() {
506
+ this.insert = 0;
507
+ this.pop = 0;
508
+ this.decreasePriority = 0;
509
+ this.increasePriority = 0;
510
+ }
511
+ };
512
+ return stats;
513
+ }
514
+ function instrumentComparator(comparator) {
515
+ const stats = createComparisonStats();
516
+ let currentOperation = null;
517
+ const instrumented = ((a, b) => {
518
+ if (currentOperation !== null) {
519
+ stats[currentOperation]++;
520
+ }
521
+ return comparator(a, b);
522
+ });
523
+ Object.defineProperty(instrumented, "stats", {
524
+ value: stats,
525
+ writable: false,
526
+ enumerable: true
527
+ });
528
+ instrumented.startOperation = (type) => {
529
+ currentOperation = type;
530
+ };
531
+ instrumented.endOperation = () => {
532
+ currentOperation = null;
533
+ };
534
+ return instrumented;
535
+ }
536
+ function theoreticalInsertComparisons(n, d) {
537
+ if (n <= 1) return 0;
538
+ return Math.floor(Math.log(n) / Math.log(d));
539
+ }
540
+ function theoreticalPopComparisons(n, d) {
541
+ if (n <= 1) return 0;
542
+ const height = Math.floor(Math.log(n) / Math.log(d));
543
+ return d * height;
544
+ }
545
+ function theoreticalDecreasePriorityComparisons(n, d) {
546
+ if (n <= 1) return 0;
547
+ return Math.floor(Math.log(n) / Math.log(d));
548
+ }
475
549
  /**
476
550
  * d-ary Heap Priority Queue - TypeScript Implementation
477
551
  *
@@ -482,8 +556,9 @@ function chain(...comparators) {
482
556
  * - O(1) access to highest-priority item
483
557
  * - O(log_d n) insert and priority increase operations
484
558
  * - O(d · log_d n) pop and priority decrease operations
559
+ * - Optional instrumentation hooks for performance analysis
485
560
  *
486
- * @version 2.1.2
561
+ * @version 2.4.0
487
562
  * @license Apache-2.0
488
563
  * @copyright 2023-2025 Eric Jacopin
489
564
  */
@@ -491,7 +566,85 @@ function chain(...comparators) {
491
566
  * Pre-built comparator factories for common use cases.
492
567
  *
493
568
  * @module comparators
494
- * @version 2.1.2
569
+ * @version 2.4.0
570
+ * @license Apache-2.0
571
+ */
572
+ /**
573
+ * Instrumentation utilities for d-ary heap performance analysis.
574
+ *
575
+ * This module provides opt-in instrumentation to count comparisons performed
576
+ * during heap operations. It is designed for:
577
+ *
578
+ * - **Educational purposes**: Understanding the theoretical vs actual cost of heap operations
579
+ * - **Benchmarking**: Measuring real comparison counts across different arities
580
+ * - **Visualization**: Powering interactive demos that show heap behavior
581
+ *
582
+ * ## Design Philosophy: Zero-Cost When Disabled
583
+ *
584
+ * Instrumentation follows these principles:
585
+ *
586
+ * 1. **Opt-in only**: No overhead when not using instrumentation
587
+ * 2. **Non-breaking**: Existing code continues to work unchanged
588
+ * 3. **Per-operation tracking**: Distinguish insert/pop/decreasePriority comparisons
589
+ *
590
+ * ## Cross-Language Consistency
591
+ *
592
+ * Currently, instrumentation is implemented in TypeScript only. The table below
593
+ * shows the idiomatic zero-cost approach for each language, planned for v2.5.0:
594
+ *
595
+ * | Language | Mechanism | Overhead When Disabled | Status |
596
+ * |------------|----------------------------------|------------------------|--------|
597
+ * | TypeScript | Optional hooks + instrumented comparator | Zero (JIT optimization) | ✅ Implemented |
598
+ * | Go | Nil stats pointer | ~1 cycle (nil check) | Planned v2.5.0 |
599
+ * | Rust | Generic over StatsCollector trait | Zero (monomorphization) | Planned v2.5.0 |
600
+ * | C++ | Template policy class | Zero (inlining) | Planned v2.5.0 |
601
+ * | Zig | Comptime bool parameter | Zero (branch elimination) | Planned v2.5.0 |
602
+ *
603
+ * ## Usage Example
604
+ *
605
+ * ```typescript
606
+ * import { PriorityQueue, minBy, instrumentComparator } from 'd-ary-heap';
607
+ *
608
+ * // 1. Wrap your comparator with instrumentation
609
+ * const comparator = instrumentComparator(minBy((v: Vertex) => v.distance));
610
+ *
611
+ * // 2. Create priority queue with operation hooks
612
+ * const pq = new PriorityQueue({
613
+ * d: 4,
614
+ * comparator,
615
+ * keyExtractor: (v) => v.id,
616
+ * onBeforeOperation: (op) => comparator.startOperation(op),
617
+ * onAfterOperation: () => comparator.endOperation(),
618
+ * });
619
+ *
620
+ * // 3. Use normally - comparisons are tracked automatically
621
+ * pq.insert({ id: 'A', distance: 0 });
622
+ * pq.insert({ id: 'B', distance: 5 });
623
+ * pq.pop();
624
+ *
625
+ * // 4. Access statistics
626
+ * console.log(comparator.stats);
627
+ * // { insert: 1, pop: 2, decreasePriority: 0, total: 3 }
628
+ *
629
+ * // 5. Reset for next measurement
630
+ * comparator.stats.reset();
631
+ * ```
632
+ *
633
+ * ## Theoretical Complexity Reference
634
+ *
635
+ * For a d-ary heap with n elements:
636
+ *
637
+ * | Operation | Comparisons (worst case) |
638
+ * |------------------|----------------------------|
639
+ * | insert | ⌊log_d(n)⌋ |
640
+ * | pop | d × ⌊log_d(n)⌋ |
641
+ * | decreasePriority | ⌊log_d(n)⌋ (upward only) |
642
+ * | increasePriority | d × ⌊log_d(n)⌋ (downward) |
643
+ *
644
+ * The demo visualization compares actual counts against these theoretical bounds.
645
+ *
646
+ * @module instrumentation
647
+ * @version 2.4.0
495
648
  * @license Apache-2.0
496
649
  */
497
650
  /**
@@ -501,13 +654,15 @@ function chain(...comparators) {
501
654
  *
502
655
  * @packageDocumentation
503
656
  * @module d-ary-heap
504
- * @version 2.1.2
657
+ * @version 2.4.0
505
658
  * @license Apache-2.0
506
659
  * @copyright 2023-2025 Eric Jacopin
507
660
  */
508
661
 
509
662
  exports.PriorityQueue = PriorityQueue;
510
663
  exports.chain = chain;
664
+ exports.createComparisonStats = createComparisonStats;
665
+ exports.instrumentComparator = instrumentComparator;
511
666
  exports.maxBy = maxBy;
512
667
  exports.maxNumber = maxNumber;
513
668
  exports.maxString = maxString;
@@ -515,5 +670,8 @@ exports.minBy = minBy;
515
670
  exports.minNumber = minNumber;
516
671
  exports.minString = minString;
517
672
  exports.reverse = reverse;
673
+ exports.theoreticalDecreasePriorityComparisons = theoreticalDecreasePriorityComparisons;
674
+ exports.theoreticalInsertComparisons = theoreticalInsertComparisons;
675
+ exports.theoreticalPopComparisons = theoreticalPopComparisons;
518
676
  //# sourceMappingURL=index.cjs.map
519
677
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/PriorityQueue.ts","../src/comparators.ts"],"names":[],"mappings":";;;AAqEO,IAAM,aAAA,GAAN,MAAM,cAAA,CAAsC;AAAA;AAAA,EAEzC,SAAA;AAAA;AAAA,EAGA,SAAA;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A,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.cjs","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.1.2\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.1.2\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;AACjC,IAAA,IAAA,CAAK,mBAAmB,OAAA,C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,GAAA,CAAI,CAAC,CAAA,GAAI,IAAA,CAAK,GAAA,CAAI,CAAC,CAAC,CAAA;AAC7C","file":"index.cjs","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"]}