d-ary-heap 2.2.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -6
- package/dist/index.cjs +231 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +324 -6
- package/dist/index.d.ts +324 -6
- package/dist/index.js +225 -8
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/src/PriorityQueue.ts +128 -6
- package/src/comparators.ts +1 -1
- package/src/index.ts +18 -1
- package/src/instrumentation.ts +339 -0
package/dist/index.cjs.map
CHANGED
|
@@ -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;AAAA;AAAA,EAGA,KAAA;AAAA;AAAA,EAGS,UAAA;AAAA;AAAA,EAGA,YAAA;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;AAG5B,IAAA,MAAM,QAAA,GAAW,QAAQ,eAAA,IAAmB,CAAA;AAC5C,IAAA,IAAA,CAAK,YAAY,QAAA,GAAW,CAAA,GAAI,IAAI,KAAA,CAAS,QAAQ,IAAI,EAAC;AAC1D,IAAA,IAAI,QAAA,GAAW,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QAAA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;AChkBO,SAAS,MAAY,KAAA,EAAsC;AAChE,EAAA,OAAO,CAAC,CAAA,EAAM,CAAA,KAAS,MAAM,CAAC,CAAA,GAAI,MAAM,CAAC,CAAA;AAC3C;AAcO,SAAS,MAAY,KAAA,EAAsC;AAChE,EAAA,OAAO,CAAC,CAAA,EAAM,CAAA,KAAS,MAAM,CAAC,CAAA,GAAI,MAAM,CAAC,CAAA;AAC3C;AAMO,IAAM,SAAA,GAAgC,CAAC,CAAA,EAAG,CAAA,KAAM,CAAA,GAAI;AAMpD,IAAM,SAAA,GAAgC,CAAC,CAAA,EAAG,CAAA,KAAM,CAAA,GAAI;AAMpD,IAAM,SAAA,GAAgC,CAAC,CAAA,EAAG,CAAA,KAAM,CAAA,GAAI;AAMpD,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.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.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":";;;AAyGO,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;A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) 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 increasePriority operations\r\n * - O(d · log_d n) pop and decreasePriority operations\r\n * - O((d+1) · log_d n) updatePriority (bidirectional)\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 * - updatePriority(): O((d+1) · 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 the item at the given index.\r\n * Time complexity: O(d · 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 decreasePriority() with the item itself.\r\n * The item at the given index should already have its priority value updated\r\n * in the container before calling this method.\r\n */\r\n decreasePriorityByIndex(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.moveDown(index);\r\n }\r\n\r\n /** Alias for decreasePriorityByIndex() - snake_case for cross-language consistency */\r\n decrease_priority_by_index(index: number): void {\r\n this.decreasePriorityByIndex(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 only moves items downward. Use updatePriority() if direction is unknown.\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 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 * Update the priority of an existing item when direction is unknown.\r\n * Time complexity: O((d+1) · log_d n) - checks both directions\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 * Use this method when you don't know whether the priority increased or decreased.\r\n * If you know the direction, prefer increasePriority() or decreasePriority() for\r\n * better performance (log_d n vs (d+1) · log_d n comparisons).\r\n */\r\n updatePriority(updatedItem: T): void {\r\n this.onBeforeOperation?.('updatePriority');\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 which way priority changed\r\n this.moveUp(index);\r\n this.moveDown(index);\r\n\r\n this.onAfterOperation?.();\r\n }\r\n\r\n /** Alias for updatePriority() - snake_case for cross-language consistency */\r\n update_priority(updatedItem: T): void {\r\n this.updatePriority(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, increasePriority: 0, updatePriority: 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 * | increasePriority | ⌊log_d(n)⌋ (moveUp only) |\r\n * | decreasePriority | d × ⌊log_d(n)⌋ (moveDown only)|\r\n * | updatePriority | (d+1) × ⌊log_d(n)⌋ (both) |\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: `updatePriority` is tracked separately for when the caller doesn't know\r\n * whether priority increased or decreased (checks both directions).\r\n */\r\nexport type OperationType = 'insert' | 'pop' | 'decreasePriority' | 'increasePriority' | 'updatePriority';\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 (moveDown only) */\r\n decreasePriority: number;\r\n\r\n /** Comparisons during increasePriority operations (moveUp only) */\r\n increasePriority: number;\r\n\r\n /** Comparisons during updatePriority operations (moveUp + moveDown) */\r\n updatePriority: 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 updatePriority: 0,\r\n\r\n get total(): number {\r\n return this.insert + this.pop + this.decreasePriority + this.increasePriority + this.updatePriority;\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 this.updatePriority = 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 an increasePriority operation.\r\n *\r\n * IncreasePriority performs only moveUp (item became more important).\r\n * Worst case: ⌊log_d(n)⌋ comparisons (one per level).\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\r\n */\r\nexport function theoreticalIncreasePriorityComparisons(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 decreasePriority operation.\r\n *\r\n * DecreasePriority performs only moveDown (item became less important).\r\n * Worst case: d × ⌊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\r\n */\r\nexport function theoreticalDecreasePriorityComparisons(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 an updatePriority operation.\r\n *\r\n * UpdatePriority performs both moveUp and moveDown (direction unknown).\r\n * Worst case: (d + 1) × ⌊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\r\n */\r\nexport function theoreticalUpdatePriorityComparisons(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 + 1) * height;\r\n}\r\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instrumentation utilities for d-ary heap performance analysis.
|
|
3
|
+
*
|
|
4
|
+
* This module provides opt-in instrumentation to count comparisons performed
|
|
5
|
+
* during heap operations. It is designed for:
|
|
6
|
+
*
|
|
7
|
+
* - **Educational purposes**: Understanding the theoretical vs actual cost of heap operations
|
|
8
|
+
* - **Benchmarking**: Measuring real comparison counts across different arities
|
|
9
|
+
* - **Visualization**: Powering interactive demos that show heap behavior
|
|
10
|
+
*
|
|
11
|
+
* ## Design Philosophy: Zero-Cost When Disabled
|
|
12
|
+
*
|
|
13
|
+
* Instrumentation follows these principles:
|
|
14
|
+
*
|
|
15
|
+
* 1. **Opt-in only**: No overhead when not using instrumentation
|
|
16
|
+
* 2. **Non-breaking**: Existing code continues to work unchanged
|
|
17
|
+
* 3. **Per-operation tracking**: Distinguish insert/pop/decreasePriority comparisons
|
|
18
|
+
*
|
|
19
|
+
* ## Cross-Language Consistency
|
|
20
|
+
*
|
|
21
|
+
* Currently, instrumentation is implemented in TypeScript only. The table below
|
|
22
|
+
* shows the idiomatic zero-cost approach for each language, planned for v2.5.0:
|
|
23
|
+
*
|
|
24
|
+
* | Language | Mechanism | Overhead When Disabled | Status |
|
|
25
|
+
* |------------|----------------------------------|------------------------|--------|
|
|
26
|
+
* | TypeScript | Optional hooks + instrumented comparator | Zero (JIT optimization) | ✅ Implemented |
|
|
27
|
+
* | Go | Nil stats pointer | ~1 cycle (nil check) | Planned v2.5.0 |
|
|
28
|
+
* | Rust | Generic over StatsCollector trait | Zero (monomorphization) | Planned v2.5.0 |
|
|
29
|
+
* | C++ | Template policy class | Zero (inlining) | Planned v2.5.0 |
|
|
30
|
+
* | Zig | Comptime bool parameter | Zero (branch elimination) | Planned v2.5.0 |
|
|
31
|
+
*
|
|
32
|
+
* ## Usage Example
|
|
33
|
+
*
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { PriorityQueue, minBy, instrumentComparator } from 'd-ary-heap';
|
|
36
|
+
*
|
|
37
|
+
* // 1. Wrap your comparator with instrumentation
|
|
38
|
+
* const comparator = instrumentComparator(minBy((v: Vertex) => v.distance));
|
|
39
|
+
*
|
|
40
|
+
* // 2. Create priority queue with operation hooks
|
|
41
|
+
* const pq = new PriorityQueue({
|
|
42
|
+
* d: 4,
|
|
43
|
+
* comparator,
|
|
44
|
+
* keyExtractor: (v) => v.id,
|
|
45
|
+
* onBeforeOperation: (op) => comparator.startOperation(op),
|
|
46
|
+
* onAfterOperation: () => comparator.endOperation(),
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // 3. Use normally - comparisons are tracked automatically
|
|
50
|
+
* pq.insert({ id: 'A', distance: 0 });
|
|
51
|
+
* pq.insert({ id: 'B', distance: 5 });
|
|
52
|
+
* pq.pop();
|
|
53
|
+
*
|
|
54
|
+
* // 4. Access statistics
|
|
55
|
+
* console.log(comparator.stats);
|
|
56
|
+
* // { insert: 1, pop: 2, decreasePriority: 0, increasePriority: 0, updatePriority: 0, total: 3 }
|
|
57
|
+
*
|
|
58
|
+
* // 5. Reset for next measurement
|
|
59
|
+
* comparator.stats.reset();
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* ## Theoretical Complexity Reference
|
|
63
|
+
*
|
|
64
|
+
* For a d-ary heap with n elements:
|
|
65
|
+
*
|
|
66
|
+
* | Operation | Comparisons (worst case) |
|
|
67
|
+
* |------------------|-------------------------------|
|
|
68
|
+
* | insert | ⌊log_d(n)⌋ |
|
|
69
|
+
* | pop | d × ⌊log_d(n)⌋ |
|
|
70
|
+
* | increasePriority | ⌊log_d(n)⌋ (moveUp only) |
|
|
71
|
+
* | decreasePriority | d × ⌊log_d(n)⌋ (moveDown only)|
|
|
72
|
+
* | updatePriority | (d+1) × ⌊log_d(n)⌋ (both) |
|
|
73
|
+
*
|
|
74
|
+
* The demo visualization compares actual counts against these theoretical bounds.
|
|
75
|
+
*
|
|
76
|
+
* @module instrumentation
|
|
77
|
+
* @version 2.4.0
|
|
78
|
+
* @license Apache-2.0
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Operation types that can be tracked.
|
|
83
|
+
*
|
|
84
|
+
* Note: `updatePriority` is tracked separately for when the caller doesn't know
|
|
85
|
+
* whether priority increased or decreased (checks both directions).
|
|
86
|
+
*/
|
|
87
|
+
type OperationType = 'insert' | 'pop' | 'decreasePriority' | 'increasePriority' | 'updatePriority';
|
|
88
|
+
/**
|
|
89
|
+
* Statistics tracking comparison counts per operation type.
|
|
90
|
+
*
|
|
91
|
+
* All counts start at zero and accumulate until `reset()` is called.
|
|
92
|
+
*/
|
|
93
|
+
interface ComparisonStats {
|
|
94
|
+
/** Comparisons during insert operations (moveUp) */
|
|
95
|
+
insert: number;
|
|
96
|
+
/** Comparisons during pop operations (moveDown + bestChildPosition) */
|
|
97
|
+
pop: number;
|
|
98
|
+
/** Comparisons during decreasePriority operations (moveDown only) */
|
|
99
|
+
decreasePriority: number;
|
|
100
|
+
/** Comparisons during increasePriority operations (moveUp only) */
|
|
101
|
+
increasePriority: number;
|
|
102
|
+
/** Comparisons during updatePriority operations (moveUp + moveDown) */
|
|
103
|
+
updatePriority: number;
|
|
104
|
+
/** Total comparisons across all operation types */
|
|
105
|
+
readonly total: number;
|
|
106
|
+
/** Reset all counters to zero */
|
|
107
|
+
reset(): void;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* An instrumented comparator that tracks comparison counts.
|
|
111
|
+
*
|
|
112
|
+
* This extends a regular comparator with:
|
|
113
|
+
* - `stats`: Current comparison counts
|
|
114
|
+
* - `startOperation(type)`: Begin tracking for an operation
|
|
115
|
+
* - `endOperation()`: Stop tracking current operation
|
|
116
|
+
*
|
|
117
|
+
* The comparator itself remains a valid `Comparator<T>` and can be used
|
|
118
|
+
* anywhere a regular comparator is expected.
|
|
119
|
+
*/
|
|
120
|
+
interface InstrumentedComparator<T> extends Comparator<T> {
|
|
121
|
+
/** Current comparison statistics */
|
|
122
|
+
readonly stats: ComparisonStats;
|
|
123
|
+
/**
|
|
124
|
+
* Signal the start of a heap operation.
|
|
125
|
+
* Comparisons will be attributed to this operation type until `endOperation()`.
|
|
126
|
+
*
|
|
127
|
+
* @param type - The operation type being started
|
|
128
|
+
*/
|
|
129
|
+
startOperation(type: OperationType): void;
|
|
130
|
+
/**
|
|
131
|
+
* Signal the end of the current heap operation.
|
|
132
|
+
* Subsequent comparisons will not be counted until the next `startOperation()`.
|
|
133
|
+
*/
|
|
134
|
+
endOperation(): void;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Create comparison statistics tracker.
|
|
138
|
+
*
|
|
139
|
+
* @returns Fresh stats object with all counts at zero
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* const stats = createComparisonStats();
|
|
144
|
+
* stats.insert = 5;
|
|
145
|
+
* stats.pop = 10;
|
|
146
|
+
* console.log(stats.total); // 15
|
|
147
|
+
* stats.reset();
|
|
148
|
+
* console.log(stats.total); // 0
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function createComparisonStats(): ComparisonStats;
|
|
152
|
+
/**
|
|
153
|
+
* Wrap a comparator with instrumentation to track comparison counts.
|
|
154
|
+
*
|
|
155
|
+
* The returned comparator:
|
|
156
|
+
* - Behaves identically to the original for comparison purposes
|
|
157
|
+
* - Tracks how many times it's called, attributed to operation types
|
|
158
|
+
* - Has zero overhead when `startOperation()` hasn't been called
|
|
159
|
+
*
|
|
160
|
+
* ## How It Works
|
|
161
|
+
*
|
|
162
|
+
* 1. Call `startOperation('insert')` before `pq.insert()`
|
|
163
|
+
* 2. The comparator increments `stats.insert` for each comparison
|
|
164
|
+
* 3. Call `endOperation()` after the operation completes
|
|
165
|
+
* 4. Repeat for other operations
|
|
166
|
+
*
|
|
167
|
+
* The `PriorityQueue` class supports `onBeforeOperation` and `onAfterOperation`
|
|
168
|
+
* hooks to automate this.
|
|
169
|
+
*
|
|
170
|
+
* ## Performance Note
|
|
171
|
+
*
|
|
172
|
+
* When `currentOperation` is null (between operations), the instrumented
|
|
173
|
+
* comparator performs only a single null check before calling the original.
|
|
174
|
+
* Modern JavaScript engines optimize this extremely well.
|
|
175
|
+
*
|
|
176
|
+
* @param comparator - The original comparator to instrument
|
|
177
|
+
* @returns An instrumented comparator with stats tracking
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* import { minBy, instrumentComparator } from 'd-ary-heap';
|
|
182
|
+
*
|
|
183
|
+
* const cmp = instrumentComparator(minBy<number, number>(x => x));
|
|
184
|
+
*
|
|
185
|
+
* // Manual usage (without hooks)
|
|
186
|
+
* cmp.startOperation('insert');
|
|
187
|
+
* console.log(cmp(5, 3)); // false, and stats.insert++
|
|
188
|
+
* console.log(cmp(3, 5)); // true, and stats.insert++
|
|
189
|
+
* cmp.endOperation();
|
|
190
|
+
*
|
|
191
|
+
* console.log(cmp.stats.insert); // 2
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
declare function instrumentComparator<T>(comparator: Comparator<T>): InstrumentedComparator<T>;
|
|
195
|
+
/**
|
|
196
|
+
* Calculate theoretical comparison count for an insert operation.
|
|
197
|
+
*
|
|
198
|
+
* Insert performs at most ⌊log_d(n)⌋ comparisons (one per level during moveUp).
|
|
199
|
+
*
|
|
200
|
+
* @param n - Number of elements in heap AFTER insert
|
|
201
|
+
* @param d - Heap arity
|
|
202
|
+
* @returns Theoretical worst-case comparison count
|
|
203
|
+
*/
|
|
204
|
+
declare function theoreticalInsertComparisons(n: number, d: number): number;
|
|
205
|
+
/**
|
|
206
|
+
* Calculate theoretical comparison count for a pop operation.
|
|
207
|
+
*
|
|
208
|
+
* Pop performs at most d × ⌊log_d(n)⌋ comparisons:
|
|
209
|
+
* - At each level, find best among d children (d-1 comparisons)
|
|
210
|
+
* - Compare best child with current (1 comparison)
|
|
211
|
+
* - Total: d comparisons per level × ⌊log_d(n)⌋ levels
|
|
212
|
+
*
|
|
213
|
+
* @param n - Number of elements in heap BEFORE pop
|
|
214
|
+
* @param d - Heap arity
|
|
215
|
+
* @returns Theoretical worst-case comparison count
|
|
216
|
+
*/
|
|
217
|
+
declare function theoreticalPopComparisons(n: number, d: number): number;
|
|
218
|
+
/**
|
|
219
|
+
* Calculate theoretical comparison count for an increasePriority operation.
|
|
220
|
+
*
|
|
221
|
+
* IncreasePriority performs only moveUp (item became more important).
|
|
222
|
+
* Worst case: ⌊log_d(n)⌋ comparisons (one per level).
|
|
223
|
+
*
|
|
224
|
+
* @param n - Number of elements in heap
|
|
225
|
+
* @param d - Heap arity
|
|
226
|
+
* @returns Theoretical worst-case comparison count
|
|
227
|
+
*/
|
|
228
|
+
declare function theoreticalIncreasePriorityComparisons(n: number, d: number): number;
|
|
229
|
+
/**
|
|
230
|
+
* Calculate theoretical comparison count for a decreasePriority operation.
|
|
231
|
+
*
|
|
232
|
+
* DecreasePriority performs only moveDown (item became less important).
|
|
233
|
+
* Worst case: d × ⌊log_d(n)⌋ comparisons.
|
|
234
|
+
*
|
|
235
|
+
* @param n - Number of elements in heap
|
|
236
|
+
* @param d - Heap arity
|
|
237
|
+
* @returns Theoretical worst-case comparison count
|
|
238
|
+
*/
|
|
239
|
+
declare function theoreticalDecreasePriorityComparisons(n: number, d: number): number;
|
|
240
|
+
/**
|
|
241
|
+
* Calculate theoretical comparison count for an updatePriority operation.
|
|
242
|
+
*
|
|
243
|
+
* UpdatePriority performs both moveUp and moveDown (direction unknown).
|
|
244
|
+
* Worst case: (d + 1) × ⌊log_d(n)⌋ comparisons.
|
|
245
|
+
*
|
|
246
|
+
* @param n - Number of elements in heap
|
|
247
|
+
* @param d - Heap arity
|
|
248
|
+
* @returns Theoretical worst-case comparison count
|
|
249
|
+
*/
|
|
250
|
+
declare function theoreticalUpdatePriorityComparisons(n: number, d: number): number;
|
|
251
|
+
|
|
1
252
|
/**
|
|
2
253
|
* d-ary Heap Priority Queue - TypeScript Implementation
|
|
3
254
|
*
|
|
@@ -6,13 +257,16 @@
|
|
|
6
257
|
* - Min-heap or max-heap behavior via comparator functions
|
|
7
258
|
* - O(1) item lookup using Map for efficient priority updates
|
|
8
259
|
* - O(1) access to highest-priority item
|
|
9
|
-
* - O(log_d n) insert and
|
|
10
|
-
* - O(d · log_d n) pop and
|
|
260
|
+
* - O(log_d n) insert and increasePriority operations
|
|
261
|
+
* - O(d · log_d n) pop and decreasePriority operations
|
|
262
|
+
* - O((d+1) · log_d n) updatePriority (bidirectional)
|
|
263
|
+
* - Optional instrumentation hooks for performance analysis
|
|
11
264
|
*
|
|
12
|
-
* @version 2.
|
|
265
|
+
* @version 2.4.0
|
|
13
266
|
* @license Apache-2.0
|
|
14
267
|
* @copyright 2023-2025 Eric Jacopin
|
|
15
268
|
*/
|
|
269
|
+
|
|
16
270
|
/** Type alias for position indices (cross-language consistency) */
|
|
17
271
|
type Position = number;
|
|
18
272
|
/**
|
|
@@ -37,6 +291,35 @@ interface PriorityQueueOptions<T, K> {
|
|
|
37
291
|
keyExtractor: KeyExtractor<T, K>;
|
|
38
292
|
/** Initial capacity hint for pre-allocation */
|
|
39
293
|
initialCapacity?: number;
|
|
294
|
+
/**
|
|
295
|
+
* Optional hook called before each heap operation.
|
|
296
|
+
*
|
|
297
|
+
* This enables opt-in instrumentation for performance analysis without
|
|
298
|
+
* adding overhead when not used. Pair with an instrumented comparator
|
|
299
|
+
* to track comparison counts per operation type.
|
|
300
|
+
*
|
|
301
|
+
* @param type - The operation about to be performed
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```typescript
|
|
305
|
+
* const cmp = instrumentComparator(minBy((v) => v.priority));
|
|
306
|
+
* const pq = new PriorityQueue({
|
|
307
|
+
* comparator: cmp,
|
|
308
|
+
* keyExtractor: (v) => v.id,
|
|
309
|
+
* onBeforeOperation: (op) => cmp.startOperation(op),
|
|
310
|
+
* onAfterOperation: () => cmp.endOperation(),
|
|
311
|
+
* });
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @see instrumentComparator from './instrumentation'
|
|
315
|
+
*/
|
|
316
|
+
onBeforeOperation?: (type: OperationType) => void;
|
|
317
|
+
/**
|
|
318
|
+
* Optional hook called after each heap operation completes.
|
|
319
|
+
*
|
|
320
|
+
* @see onBeforeOperation for usage example
|
|
321
|
+
*/
|
|
322
|
+
onAfterOperation?: () => void;
|
|
40
323
|
}
|
|
41
324
|
/**
|
|
42
325
|
* Generic d-ary heap priority queue with O(1) lookup.
|
|
@@ -56,6 +339,7 @@ interface PriorityQueueOptions<T, K> {
|
|
|
56
339
|
* - pop(): O(d · log_d n)
|
|
57
340
|
* - increasePriority(): O(log_d n)
|
|
58
341
|
* - decreasePriority(): O(d · log_d n)
|
|
342
|
+
* - updatePriority(): O((d+1) · log_d n)
|
|
59
343
|
* - contains(): O(1)
|
|
60
344
|
* - len(), isEmpty(), d(): O(1)
|
|
61
345
|
*
|
|
@@ -73,6 +357,10 @@ declare class PriorityQueue<T, K = string | number> {
|
|
|
73
357
|
private readonly comparator;
|
|
74
358
|
/** Key extractor for identity-based lookup */
|
|
75
359
|
private readonly keyExtractor;
|
|
360
|
+
/** Optional hook called before operations (for instrumentation) */
|
|
361
|
+
private readonly onBeforeOperation;
|
|
362
|
+
/** Optional hook called after operations (for instrumentation) */
|
|
363
|
+
private readonly onAfterOperation;
|
|
76
364
|
/**
|
|
77
365
|
* Create a new d-ary heap priority queue.
|
|
78
366
|
*
|
|
@@ -222,6 +510,21 @@ declare class PriorityQueue<T, K = string | number> {
|
|
|
222
510
|
increasePriorityByIndex(index: number): void;
|
|
223
511
|
/** Alias for increasePriorityByIndex() - snake_case for cross-language consistency */
|
|
224
512
|
increase_priority_by_index(index: number): void;
|
|
513
|
+
/**
|
|
514
|
+
* Decrease the priority of the item at the given index.
|
|
515
|
+
* Time complexity: O(d · log_d n)
|
|
516
|
+
*
|
|
517
|
+
* @param index - Index of the item in the heap array
|
|
518
|
+
* @throws Error if index is out of bounds
|
|
519
|
+
*
|
|
520
|
+
* @remarks
|
|
521
|
+
* This is a lower-level method. Prefer decreasePriority() with the item itself.
|
|
522
|
+
* The item at the given index should already have its priority value updated
|
|
523
|
+
* in the container before calling this method.
|
|
524
|
+
*/
|
|
525
|
+
decreasePriorityByIndex(index: number): void;
|
|
526
|
+
/** Alias for decreasePriorityByIndex() - snake_case for cross-language consistency */
|
|
527
|
+
decrease_priority_by_index(index: number): void;
|
|
225
528
|
/**
|
|
226
529
|
* Decrease the priority of an existing item (move toward leaves).
|
|
227
530
|
* Time complexity: O(d · log_d n)
|
|
@@ -232,11 +535,26 @@ declare class PriorityQueue<T, K = string | number> {
|
|
|
232
535
|
* @remarks
|
|
233
536
|
* For min-heap: increasing the priority value decreases importance.
|
|
234
537
|
* For max-heap: decreasing the priority value decreases importance.
|
|
235
|
-
* This method
|
|
538
|
+
* This method only moves items downward. Use updatePriority() if direction is unknown.
|
|
236
539
|
*/
|
|
237
540
|
decreasePriority(updatedItem: T): void;
|
|
238
541
|
/** Alias for decreasePriority() - snake_case for cross-language consistency */
|
|
239
542
|
decrease_priority(updatedItem: T): void;
|
|
543
|
+
/**
|
|
544
|
+
* Update the priority of an existing item when direction is unknown.
|
|
545
|
+
* Time complexity: O((d+1) · log_d n) - checks both directions
|
|
546
|
+
*
|
|
547
|
+
* @param updatedItem - Item with same identity but updated priority
|
|
548
|
+
* @throws Error if item not found
|
|
549
|
+
*
|
|
550
|
+
* @remarks
|
|
551
|
+
* Use this method when you don't know whether the priority increased or decreased.
|
|
552
|
+
* If you know the direction, prefer increasePriority() or decreasePriority() for
|
|
553
|
+
* better performance (log_d n vs (d+1) · log_d n comparisons).
|
|
554
|
+
*/
|
|
555
|
+
updatePriority(updatedItem: T): void;
|
|
556
|
+
/** Alias for updatePriority() - snake_case for cross-language consistency */
|
|
557
|
+
update_priority(updatedItem: T): void;
|
|
240
558
|
/**
|
|
241
559
|
* Remove and return the highest-priority item.
|
|
242
560
|
* Time complexity: O(d · log_d n)
|
|
@@ -305,7 +623,7 @@ declare class PriorityQueue<T, K = string | number> {
|
|
|
305
623
|
* Pre-built comparator factories for common use cases.
|
|
306
624
|
*
|
|
307
625
|
* @module comparators
|
|
308
|
-
* @version 2.
|
|
626
|
+
* @version 2.4.0
|
|
309
627
|
* @license Apache-2.0
|
|
310
628
|
*/
|
|
311
629
|
|
|
@@ -385,4 +703,4 @@ declare function reverse<T>(cmp: Comparator<T>): Comparator<T>;
|
|
|
385
703
|
*/
|
|
386
704
|
declare function chain<T>(...comparators: Comparator<T>[]): Comparator<T>;
|
|
387
705
|
|
|
388
|
-
export { type Comparator, type KeyExtractor, type Position, PriorityQueue, type PriorityQueueOptions, chain, maxBy, maxNumber, maxString, minBy, minNumber, minString, reverse };
|
|
706
|
+
export { type Comparator, type ComparisonStats, type InstrumentedComparator, type KeyExtractor, type OperationType, type Position, PriorityQueue, type PriorityQueueOptions, chain, createComparisonStats, instrumentComparator, maxBy, maxNumber, maxString, minBy, minNumber, minString, reverse, theoreticalDecreasePriorityComparisons, theoreticalIncreasePriorityComparisons, theoreticalInsertComparisons, theoreticalPopComparisons, theoreticalUpdatePriorityComparisons };
|