async-await-queue
Version:
async/await simple priority queues
1 lines • 6.19 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../src/heap.ts"],"sourcesContent":["/**\n * Lighter version of\n * Heap.ts from https://github.com/ignlg/heap-js/blob/master/src/Heap.ts\n * heap-js by @ignlg\n */\n\nexport type Comparator<T> = (a: T, b: T) => number;\nexport type IsEqual<T> = (e: T, o: T) => boolean;\n\nexport const toInt = (n: number): number => ~~n;\n\n/**\n * Heap\n * @type {Class}\n */\nexport class Heap<T> {\n heapArray: Array<T> = [];\n _limit = 0;\n\n /**\n * Heap instance constructor.\n * @param {Function} compare Optional comparison function, defaults to Heap.minComparator<number>\n */\n constructor(public compare: Comparator<T>) { }\n\n /*\n Static methods\n */\n\n /**\n * Gets children indices for given index.\n * @param {Number} idx Parent index\n * @return {Array(Number)} Array of children indices\n */\n static getChildrenIndexOf(idx: number): Array<number> {\n return [idx * 2 + 1, idx * 2 + 2];\n }\n\n /**\n * Gets parent index for given index.\n * @param {Number} idx Children index\n * @return {Number | undefined} Parent index, -1 if idx is 0\n */\n static getParentIndexOf(idx: number): number {\n if (idx <= 0) {\n return -1;\n }\n const whichChildren = idx % 2 ? 1 : 2;\n return Math.floor((idx - whichChildren) / 2);\n }\n\n /*\n Instance methods\n */\n\n /**\n * Adds an element to the heap. Aliases: `offer`.\n * Same as: push(element)\n * @param {any} element Element to be added\n * @return {Boolean} true\n */\n push(element: T): boolean {\n this._sortNodeUp(this.heapArray.push(element) - 1);\n return true;\n }\n\n\n\n\n\n /**\n * Length of the heap.\n * @return {Number}\n */\n length(): number {\n return this.heapArray.length;\n }\n\n /**\n * Top node. Aliases: `element`.\n * Same as: `top(1)[0]`\n * @return {any} Top node\n */\n peek(): T | undefined {\n return this.heapArray[0];\n }\n\n /**\n * Extract the top node (root). Aliases: `poll`.\n * @return {any} Extracted top node, undefined if empty\n */\n pop(): T | undefined {\n const last = this.heapArray.pop();\n if (this.length() > 0 && last !== undefined) {\n return this.replace(last);\n }\n return last;\n }\n\n /**\n * Pop the current peek value, and add the new item.\n * @param {any} element Element to replace peek\n * @return {any} Old peek\n */\n replace(element: T): T {\n const peek = this.heapArray[0];\n this.heapArray[0] = element;\n this._sortNodeDown(0);\n return peek;\n }\n\n /**\n * Size of the heap\n * @return {Number}\n */\n size(): number {\n return this.length();\n }\n\n\n /**\n * Move a node to a new index, switching places\n * @param {Number} j First node index\n * @param {Number} k Another node index\n */\n _moveNode(j: number, k: number): void {\n [this.heapArray[j], this.heapArray[k]] = [this.heapArray[k], this.heapArray[j]];\n }\n\n /**\n * Move a node down the tree (to the leaves) to find a place where the heap is sorted.\n * @param {Number} i Index of the node\n */\n _sortNodeDown(i: number): void {\n let moveIt = i < this.heapArray.length - 1;\n const self = this.heapArray[i];\n\n const getPotentialParent = (best: number, j: number) => {\n if (this.heapArray.length > j && this.compare(this.heapArray[j], this.heapArray[best]) < 0) {\n best = j;\n }\n return best;\n };\n\n while (moveIt) {\n const childrenIdx = Heap.getChildrenIndexOf(i);\n const bestChildIndex = childrenIdx.reduce(getPotentialParent, childrenIdx[0]);\n const bestChild = this.heapArray[bestChildIndex];\n if (typeof bestChild !== 'undefined' && this.compare(self, bestChild) > 0) {\n this._moveNode(i, bestChildIndex);\n i = bestChildIndex;\n } else {\n moveIt = false;\n }\n }\n }\n\n /**\n * Move a node up the tree (to the root) to find a place where the heap is sorted.\n * @param {Number} i Index of the node\n */\n _sortNodeUp(i: number): void {\n let moveIt = i > 0;\n while (moveIt) {\n const pi = Heap.getParentIndexOf(i);\n if (pi >= 0 && this.compare(this.heapArray[pi], this.heapArray[i]) > 0) {\n this._moveNode(i, pi);\n i = pi;\n } else {\n moveIt = false;\n }\n }\n }\n\n}\n"],"names":["Heap","constructor","compare","this","heapArray","_limit","static","idx","whichChildren","Math","floor","push","element","_sortNodeUp","length","peek","pop","last","undefined","replace","_sortNodeDown","size","_moveNode","j","k","i","moveIt","self","getPotentialParent","best","childrenIdx","getChildrenIndexOf","bestChildIndex","reduce","bestChild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