@x-oasis/heap 0.0.18
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 +19 -0
- package/dist/heap.cjs.development.js +95 -0
- package/dist/heap.cjs.development.js.map +1 -0
- package/dist/heap.cjs.production.min.js +2 -0
- package/dist/heap.cjs.production.min.js.map +1 -0
- package/dist/heap.esm.js +91 -0
- package/dist/heap.esm.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +8 -0
- package/package.json +24 -0
- package/src/index.ts +144 -0
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function defaultComparator(a, b) {
|
|
6
|
+
return a < b;
|
|
7
|
+
}
|
|
8
|
+
var Heap = /*#__PURE__*/function () {
|
|
9
|
+
function Heap(items, comparator) {
|
|
10
|
+
this._items = items || [];
|
|
11
|
+
this._size = this._items.length;
|
|
12
|
+
this._comparator = comparator || defaultComparator;
|
|
13
|
+
this._heapify();
|
|
14
|
+
}
|
|
15
|
+
var _proto = Heap.prototype;
|
|
16
|
+
_proto.empty = function empty() {
|
|
17
|
+
return this._size === 0;
|
|
18
|
+
};
|
|
19
|
+
_proto.pop = function pop() {
|
|
20
|
+
if (this._size === 0) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
var elt = this._items[0];
|
|
24
|
+
var lastElt = this._items.pop();
|
|
25
|
+
this._size--;
|
|
26
|
+
if (this._size > 0) {
|
|
27
|
+
this._items[0] = lastElt;
|
|
28
|
+
this._sinkDown(0);
|
|
29
|
+
}
|
|
30
|
+
return elt;
|
|
31
|
+
};
|
|
32
|
+
_proto.push = function push(item) {
|
|
33
|
+
this._items[this._size++] = item;
|
|
34
|
+
this._bubbleUp(this._size - 1);
|
|
35
|
+
};
|
|
36
|
+
_proto.size = function size() {
|
|
37
|
+
return this._size;
|
|
38
|
+
};
|
|
39
|
+
_proto.peek = function peek() {
|
|
40
|
+
if (this._size === 0) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return this._items[0];
|
|
44
|
+
};
|
|
45
|
+
_proto._heapify = function _heapify() {
|
|
46
|
+
for (var index = Math.floor((this._size + 1) / 2); index >= 0; index--) {
|
|
47
|
+
this._sinkDown(index);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
_proto._bubbleUp = function _bubbleUp(index) {
|
|
51
|
+
var elt = this._items[index];
|
|
52
|
+
while (index > 0) {
|
|
53
|
+
var parentIndex = Math.floor((index + 1) / 2) - 1;
|
|
54
|
+
var parentElt = this._items[parentIndex];
|
|
55
|
+
if (this._comparator(parentElt, elt)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
this._items[parentIndex] = elt;
|
|
59
|
+
this._items[index] = parentElt;
|
|
60
|
+
index = parentIndex;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
_proto._sinkDown = function _sinkDown(index) {
|
|
64
|
+
var elt = this._items[index];
|
|
65
|
+
while (true) {
|
|
66
|
+
var leftChildIndex = 2 * (index + 1) - 1;
|
|
67
|
+
var rightChildIndex = 2 * (index + 1);
|
|
68
|
+
var swapIndex = -1;
|
|
69
|
+
if (leftChildIndex < this._size) {
|
|
70
|
+
var leftChild = this._items[leftChildIndex];
|
|
71
|
+
if (this._comparator(leftChild, elt)) {
|
|
72
|
+
swapIndex = leftChildIndex;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (rightChildIndex < this._size) {
|
|
76
|
+
var rightChild = this._items[rightChildIndex];
|
|
77
|
+
if (this._comparator(rightChild, elt)) {
|
|
78
|
+
if (swapIndex === -1 || this._comparator(rightChild, this._items[swapIndex])) {
|
|
79
|
+
swapIndex = rightChildIndex;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (swapIndex === -1) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this._items[index] = this._items[swapIndex];
|
|
87
|
+
this._items[swapIndex] = elt;
|
|
88
|
+
index = swapIndex;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
return Heap;
|
|
92
|
+
}();
|
|
93
|
+
|
|
94
|
+
exports.default = Heap;
|
|
95
|
+
//# sourceMappingURL=heap.cjs.development.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heap.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["function defaultComparator(a: number, b: number) {\n return a < b;\n}\n\ntype Comparator = (a: any, b: any) => boolean;\ntype HeapBasicItem = any;\n\nclass Heap<T extends HeapBasicItem = HeapBasicItem> {\n private _items: Array<T>;\n private _size: number;\n private _comparator: Comparator;\n\n constructor(items: Array<T>, comparator: Comparator) {\n this._items = items || [];\n this._size = this._items.length;\n this._comparator = comparator || defaultComparator;\n this._heapify();\n }\n\n /*\n * @return {boolean}\n */\n empty() {\n return this._size === 0;\n }\n\n /*\n * @return {*}\n */\n pop() {\n if (this._size === 0) {\n return null;\n }\n\n const elt = this._items[0];\n\n const lastElt = this._items.pop()!;\n this._size--;\n\n if (this._size > 0) {\n this._items[0] = lastElt;\n this._sinkDown(0);\n }\n\n return elt;\n }\n\n /*\n * @param {*} item\n */\n push(item: T) {\n this._items[this._size++] = item;\n this._bubbleUp(this._size - 1);\n }\n\n /*\n * @return {number}\n */\n size() {\n return this._size;\n }\n\n /*\n * @return {*}\n */\n peek() {\n if (this._size === 0) {\n return null;\n }\n\n return this._items[0];\n }\n\n _heapify() {\n for (let index = Math.floor((this._size + 1) / 2); index >= 0; index--) {\n this._sinkDown(index);\n }\n }\n\n /*\n * @parent {number} index\n */\n _bubbleUp(index: number) {\n const elt = this._items[index];\n while (index > 0) {\n const parentIndex = Math.floor((index + 1) / 2) - 1;\n const parentElt = this._items[parentIndex];\n\n // if parentElt < elt, stop\n if (this._comparator(parentElt, elt)) {\n return;\n }\n\n // swap\n this._items[parentIndex] = elt;\n this._items[index] = parentElt;\n index = parentIndex;\n }\n }\n\n /*\n * @parent {number} index\n */\n _sinkDown(index: number) {\n const elt = this._items[index];\n\n // eslint-disable-next-line\n while (true) {\n const leftChildIndex = 2 * (index + 1) - 1;\n const rightChildIndex = 2 * (index + 1);\n let swapIndex = -1;\n\n if (leftChildIndex < this._size) {\n const leftChild = this._items[leftChildIndex];\n if (this._comparator(leftChild, elt)) {\n swapIndex = leftChildIndex;\n }\n }\n\n if (rightChildIndex < this._size) {\n const rightChild = this._items[rightChildIndex];\n if (this._comparator(rightChild, elt)) {\n if (\n swapIndex === -1 ||\n this._comparator(rightChild, this._items[swapIndex])\n ) {\n swapIndex = rightChildIndex;\n }\n }\n }\n\n // if we don't have a swap, stop\n if (swapIndex === -1) {\n return;\n }\n\n this._items[index] = this._items[swapIndex];\n this._items[swapIndex] = elt;\n index = swapIndex;\n }\n }\n}\n\nexport default Heap;\n"],"names":["defaultComparator","a","b","Heap","items","comparator","_items","_size","length","_comparator","_heapify","_proto","prototype","empty","pop","elt","lastElt","_sinkDown","push","item","_bubbleUp","size","peek","index","Math","floor","parentIndex","parentElt","leftChildIndex","rightChildIndex","swapIndex","leftChild","rightChild"],"mappings":";;;;AAAA,SAASA,iBAAiBA,CAACC,CAAS,EAAEC,CAAS;EAC7C,OAAOD,CAAC,GAAGC,CAAC;AACd;AAAC,IAKKC,IAAI;EAKR,SAAAA,KAAYC,KAAe,EAAEC,UAAsB;IACjD,IAAI,CAACC,MAAM,GAAGF,KAAK,IAAI,EAAE;IACzB,IAAI,CAACG,KAAK,GAAG,IAAI,CAACD,MAAM,CAACE,MAAM;IAC/B,IAAI,CAACC,WAAW,GAAGJ,UAAU,IAAIL,iBAAiB;IAClD,IAAI,CAACU,QAAQ,EAAE;;EAChB,IAAAC,MAAA,GAAAR,IAAA,CAAAS,SAAA;EAAAD,MAAA,CAKDE,KAAK,GAAL,SAAAA;IACE,OAAO,IAAI,CAACN,KAAK,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IAAI,CAACrB,WAAW,CAACuB,UAAU,EAAE,IAAI,CAAC1B,MAAM,CAACwB,SAAS,CAAC,CAAC,EACpD;YACAA,SAAS,GAAGD,eAAe;;;;MAMjC,IAAIC,SAAS,KAAK,CAAC,CAAC,EAAE;QACpB;;MAGF,IAAI,CAACxB,MAAM,CAACiB,KAAK,CAAC,GAAG,IAAI,CAACjB,MAAM,CAACwB,SAAS,CAAC;MAC3C,IAAI,CAACxB,MAAM,CAACwB,SAAS,CAAC,GAAGf,GAAG;MAC5BQ,KAAK,GAAGO,SAAS;;GAEpB;EAAA,OAAA3B,IAAA;AAAA;;;;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";function t(t,i){return t<i}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(){function i(i,s){this._items=i||[],this._size=this._items.length,this._comparator=s||t,this._heapify()}var s=i.prototype;return s.empty=function(){return 0===this._size},s.pop=function(){if(0===this._size)return null;var t=this._items[0],i=this._items.pop();return this._size--,this._size>0&&(this._items[0]=i,this._sinkDown(0)),t},s.push=function(t){this._items[this._size++]=t,this._bubbleUp(this._size-1)},s.size=function(){return this._size},s.peek=function(){return 0===this._size?null:this._items[0]},s._heapify=function(){for(var t=Math.floor((this._size+1)/2);t>=0;t--)this._sinkDown(t)},s._bubbleUp=function(t){for(var i=this._items[t];t>0;){var s=Math.floor((t+1)/2)-1,e=this._items[s];if(this._comparator(e,i))return;this._items[s]=i,this._items[t]=e,t=s}},s._sinkDown=function(t){for(var i=this._items[t];;){var s=2*(t+1)-1,e=2*(t+1),r=-1;if(s<this._size&&this._comparator(this._items[s],i)&&(r=s),e<this._size){var h=this._items[e];this._comparator(h,i)&&(-1===r||this._comparator(h,this._items[r]))&&(r=e)}if(-1===r)return;this._items[t]=this._items[r],this._items[r]=i,t=r}},i}();
|
|
2
|
+
//# sourceMappingURL=heap.cjs.production.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heap.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["function defaultComparator(a: number, b: number) {\n return a < b;\n}\n\ntype Comparator = (a: any, b: any) => boolean;\ntype HeapBasicItem = any;\n\nclass Heap<T extends HeapBasicItem = HeapBasicItem> {\n private _items: Array<T>;\n private _size: number;\n private _comparator: Comparator;\n\n constructor(items: Array<T>, comparator: Comparator) {\n this._items = items || [];\n this._size = this._items.length;\n this._comparator = comparator || defaultComparator;\n this._heapify();\n }\n\n /*\n * @return {boolean}\n */\n empty() {\n return this._size === 0;\n }\n\n /*\n * @return {*}\n */\n pop() {\n if (this._size === 0) {\n return null;\n }\n\n const elt = this._items[0];\n\n const lastElt = this._items.pop()!;\n this._size--;\n\n if (this._size > 0) {\n this._items[0] = lastElt;\n this._sinkDown(0);\n }\n\n return elt;\n }\n\n /*\n * @param {*} item\n */\n push(item: T) {\n this._items[this._size++] = item;\n this._bubbleUp(this._size - 1);\n }\n\n /*\n * @return {number}\n */\n size() {\n return this._size;\n }\n\n /*\n * @return {*}\n */\n peek() {\n if (this._size === 0) {\n return null;\n }\n\n return this._items[0];\n }\n\n _heapify() {\n for (let index = Math.floor((this._size + 1) / 2); index >= 0; index--) {\n this._sinkDown(index);\n }\n }\n\n /*\n * @parent {number} index\n */\n _bubbleUp(index: number) {\n const elt = this._items[index];\n while (index > 0) {\n const parentIndex = Math.floor((index + 1) / 2) - 1;\n const parentElt = this._items[parentIndex];\n\n // if parentElt < elt, stop\n if (this._comparator(parentElt, elt)) {\n return;\n }\n\n // swap\n this._items[parentIndex] = elt;\n this._items[index] = parentElt;\n index = parentIndex;\n }\n }\n\n /*\n * @parent {number} index\n */\n _sinkDown(index: number) {\n const elt = this._items[index];\n\n // eslint-disable-next-line\n while (true) {\n const leftChildIndex = 2 * (index + 1) - 1;\n const rightChildIndex = 2 * (index + 1);\n let swapIndex = -1;\n\n if (leftChildIndex < this._size) {\n const leftChild = this._items[leftChildIndex];\n if (this._comparator(leftChild, elt)) {\n swapIndex = leftChildIndex;\n }\n }\n\n if (rightChildIndex < this._size) {\n const rightChild = this._items[rightChildIndex];\n if (this._comparator(rightChild, elt)) {\n if (\n swapIndex === -1 ||\n this._comparator(rightChild, this._items[swapIndex])\n ) {\n swapIndex = rightChildIndex;\n }\n }\n }\n\n // if we don't have a swap, stop\n if (swapIndex === -1) {\n return;\n }\n\n this._items[index] = this._items[swapIndex];\n this._items[swapIndex] = elt;\n index = swapIndex;\n }\n }\n}\n\nexport default Heap;\n"],"names":["defaultComparator","a","b","Heap","items","comparator","this","_items","_size","length","_comparator","_heapify","_proto","prototype","empty","pop","elt","lastElt","_sinkDown","push","item","_bubbleUp","size","peek","index","Math","floor","parentIndex","parentElt","leftChildIndex","rightChildIndex","swapIndex","rightChild"],"mappings":"aAAA,SAASA,EAAkBC,EAAWC,GACpC,OAAOD,EAAIC,oFAWX,SAAAC,EAAYC,EAAiBC,GAC3BC,KAAKC,OAASH,GAAS,GACvBE,KAAKE,MAAQF,KAAKC,OAAOE,OACzBH,KAAKI,YAAcL,GAAcL,EACjCM,KAAKK,WACN,IAAAC,EAAAT,EAAAU,UA2HA,OA3HAD,EAKDE,MAAA,WACE,OAAsB,IAAfR,KAAKE,OACbI,EAKDG,IAAA,WACE,GAAmB,IAAfT,KAAKE,MACP,OAAO,KAGT,IAAMQ,EAAMV,KAAKC,OAAO,GAElBU,EAAUX,KAAKC,OAAOQ,MAQ5B,OAPAT,KAAKE,QAEDF,KAAKE,MAAQ,IACfF,KAAKC,OAAO,GAAKU,EACjBX,KAAKY,UAAU,IAGVF,GACRJ,EAKDO,KAAA,SAAKC,GACHd,KAAKC,OAAOD,KAAKE,SAAWY,EAC5Bd,KAAKe,UAAUf,KAAKE,MAAQ,IAC7BI,EAKDU,KAAA,WACE,OAAOhB,KAAKE,OACbI,EAKDW,KAAA,WACE,OAAmB,IAAfjB,KAAKE,MACA,KAGFF,KAAKC,OAAO,IACpBK,EAEDD,SAAA,WACE,IAAK,IAAIa,EAAQC,KAAKC,OAAOpB,KAAKE,MAAQ,GAAK,GAAIgB,GAAS,EAAGA,IAC7DlB,KAAKY,UAAUM,IAElBZ,EAKDS,UAAA,SAAUG,GAER,IADA,IAAMR,EAAMV,KAAKC,OAAOiB,GACjBA,EAAQ,GAAG,CAChB,IAAMG,EAAcF,KAAKC,OAAOF,EAAQ,GAAK,GAAK,EAC5CI,EAAYtB,KAAKC,OAAOoB,GAG9B,GAAIrB,KAAKI,YAAYkB,EAAWZ,GAC9B,OAIFV,KAAKC,OAAOoB,GAAeX,EAC3BV,KAAKC,OAAOiB,GAASI,EACrBJ,EAAQG,IAEXf,EAKDM,UAAA,SAAUM,GAIR,IAHA,IAAMR,EAAMV,KAAKC,OAAOiB,KAGX,CACX,IAAMK,EAAiB,GAAKL,EAAQ,GAAK,EACnCM,EAAkB,GAAKN,EAAQ,GACjCO,GAAa,EASjB,GAPIF,EAAiBvB,KAAKE,OAEpBF,KAAKI,YADSJ,KAAKC,OAAOsB,GACEb,KAC9Be,EAAYF,GAIZC,EAAkBxB,KAAKE,MAAO,CAChC,IAAMwB,EAAa1B,KAAKC,OAAOuB,GAC3BxB,KAAKI,YAAYsB,EAAYhB,MAEd,IAAfe,GACAzB,KAAKI,YAAYsB,EAAY1B,KAAKC,OAAOwB,OAEzCA,EAAYD,GAMlB,IAAmB,IAAfC,EACF,OAGFzB,KAAKC,OAAOiB,GAASlB,KAAKC,OAAOwB,GACjCzB,KAAKC,OAAOwB,GAAaf,EACzBQ,EAAQO,IAEX5B"}
|
package/dist/heap.esm.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
function defaultComparator(a, b) {
|
|
2
|
+
return a < b;
|
|
3
|
+
}
|
|
4
|
+
var Heap = /*#__PURE__*/function () {
|
|
5
|
+
function Heap(items, comparator) {
|
|
6
|
+
this._items = items || [];
|
|
7
|
+
this._size = this._items.length;
|
|
8
|
+
this._comparator = comparator || defaultComparator;
|
|
9
|
+
this._heapify();
|
|
10
|
+
}
|
|
11
|
+
var _proto = Heap.prototype;
|
|
12
|
+
_proto.empty = function empty() {
|
|
13
|
+
return this._size === 0;
|
|
14
|
+
};
|
|
15
|
+
_proto.pop = function pop() {
|
|
16
|
+
if (this._size === 0) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
var elt = this._items[0];
|
|
20
|
+
var lastElt = this._items.pop();
|
|
21
|
+
this._size--;
|
|
22
|
+
if (this._size > 0) {
|
|
23
|
+
this._items[0] = lastElt;
|
|
24
|
+
this._sinkDown(0);
|
|
25
|
+
}
|
|
26
|
+
return elt;
|
|
27
|
+
};
|
|
28
|
+
_proto.push = function push(item) {
|
|
29
|
+
this._items[this._size++] = item;
|
|
30
|
+
this._bubbleUp(this._size - 1);
|
|
31
|
+
};
|
|
32
|
+
_proto.size = function size() {
|
|
33
|
+
return this._size;
|
|
34
|
+
};
|
|
35
|
+
_proto.peek = function peek() {
|
|
36
|
+
if (this._size === 0) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return this._items[0];
|
|
40
|
+
};
|
|
41
|
+
_proto._heapify = function _heapify() {
|
|
42
|
+
for (var index = Math.floor((this._size + 1) / 2); index >= 0; index--) {
|
|
43
|
+
this._sinkDown(index);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
_proto._bubbleUp = function _bubbleUp(index) {
|
|
47
|
+
var elt = this._items[index];
|
|
48
|
+
while (index > 0) {
|
|
49
|
+
var parentIndex = Math.floor((index + 1) / 2) - 1;
|
|
50
|
+
var parentElt = this._items[parentIndex];
|
|
51
|
+
if (this._comparator(parentElt, elt)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
this._items[parentIndex] = elt;
|
|
55
|
+
this._items[index] = parentElt;
|
|
56
|
+
index = parentIndex;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
_proto._sinkDown = function _sinkDown(index) {
|
|
60
|
+
var elt = this._items[index];
|
|
61
|
+
while (true) {
|
|
62
|
+
var leftChildIndex = 2 * (index + 1) - 1;
|
|
63
|
+
var rightChildIndex = 2 * (index + 1);
|
|
64
|
+
var swapIndex = -1;
|
|
65
|
+
if (leftChildIndex < this._size) {
|
|
66
|
+
var leftChild = this._items[leftChildIndex];
|
|
67
|
+
if (this._comparator(leftChild, elt)) {
|
|
68
|
+
swapIndex = leftChildIndex;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (rightChildIndex < this._size) {
|
|
72
|
+
var rightChild = this._items[rightChildIndex];
|
|
73
|
+
if (this._comparator(rightChild, elt)) {
|
|
74
|
+
if (swapIndex === -1 || this._comparator(rightChild, this._items[swapIndex])) {
|
|
75
|
+
swapIndex = rightChildIndex;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (swapIndex === -1) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this._items[index] = this._items[swapIndex];
|
|
83
|
+
this._items[swapIndex] = elt;
|
|
84
|
+
index = swapIndex;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return Heap;
|
|
88
|
+
}();
|
|
89
|
+
|
|
90
|
+
export default Heap;
|
|
91
|
+
//# sourceMappingURL=heap.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heap.esm.js","sources":["../src/index.ts"],"sourcesContent":["function defaultComparator(a: number, b: number) {\n return a < b;\n}\n\ntype Comparator = (a: any, b: any) => boolean;\ntype HeapBasicItem = any;\n\nclass Heap<T extends HeapBasicItem = HeapBasicItem> {\n private _items: Array<T>;\n private _size: number;\n private _comparator: Comparator;\n\n constructor(items: Array<T>, comparator: Comparator) {\n this._items = items || [];\n this._size = this._items.length;\n this._comparator = comparator || defaultComparator;\n this._heapify();\n }\n\n /*\n * @return {boolean}\n */\n empty() {\n return this._size === 0;\n }\n\n /*\n * @return {*}\n */\n pop() {\n if (this._size === 0) {\n return null;\n }\n\n const elt = this._items[0];\n\n const lastElt = this._items.pop()!;\n this._size--;\n\n if (this._size > 0) {\n this._items[0] = lastElt;\n this._sinkDown(0);\n }\n\n return elt;\n }\n\n /*\n * @param {*} item\n */\n push(item: T) {\n this._items[this._size++] = item;\n this._bubbleUp(this._size - 1);\n }\n\n /*\n * @return {number}\n */\n size() {\n return this._size;\n }\n\n /*\n * @return {*}\n */\n peek() {\n if (this._size === 0) {\n return null;\n }\n\n return this._items[0];\n }\n\n _heapify() {\n for (let index = Math.floor((this._size + 1) / 2); index >= 0; index--) {\n this._sinkDown(index);\n }\n }\n\n /*\n * @parent {number} index\n */\n _bubbleUp(index: number) {\n const elt = this._items[index];\n while (index > 0) {\n const parentIndex = Math.floor((index + 1) / 2) - 1;\n const parentElt = this._items[parentIndex];\n\n // if parentElt < elt, stop\n if (this._comparator(parentElt, elt)) {\n return;\n }\n\n // swap\n this._items[parentIndex] = elt;\n this._items[index] = parentElt;\n index = parentIndex;\n }\n }\n\n /*\n * @parent {number} index\n */\n _sinkDown(index: number) {\n const elt = this._items[index];\n\n // eslint-disable-next-line\n while (true) {\n const leftChildIndex = 2 * (index + 1) - 1;\n const rightChildIndex = 2 * (index + 1);\n let swapIndex = -1;\n\n if (leftChildIndex < this._size) {\n const leftChild = this._items[leftChildIndex];\n if (this._comparator(leftChild, elt)) {\n swapIndex = leftChildIndex;\n }\n }\n\n if (rightChildIndex < this._size) {\n const rightChild = this._items[rightChildIndex];\n if (this._comparator(rightChild, elt)) {\n if (\n swapIndex === -1 ||\n this._comparator(rightChild, this._items[swapIndex])\n ) {\n swapIndex = rightChildIndex;\n }\n }\n }\n\n // if we don't have a swap, stop\n if (swapIndex === -1) {\n return;\n }\n\n this._items[index] = this._items[swapIndex];\n this._items[swapIndex] = elt;\n index = swapIndex;\n }\n }\n}\n\nexport default Heap;\n"],"names":["defaultComparator","a","b","Heap","items","comparator","_items","_size","length","_comparator","_heapify","_proto","prototype","empty","pop","elt","lastElt","_sinkDown","push","item","_bubbleUp","size","peek","index","Math","floor","parentIndex","parentElt","leftChildIndex","rightChildIndex","swapIndex","le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O,SAAS,GAAG,CAAC,CAAC;MAElB,IAAIF,cAAc,GAAG,IAAI,CAACrB,KAAK,EAAE;QAC/B,IAAMwB,SAAS,GAAG,IAAI,CAACzB,MAAM,CAACsB,cAAc,CAAC;QAC7C,IAAI,IAAI,CAACnB,WAAW,CAACsB,SAAS,EAAEhB,GAAG,CAAC,EAAE;UACpCe,SAAS,GAAGF,cAAc;;;MAI9B,IAAIC,eAAe,GAAG,IAAI,CAACtB,KAAK,EAAE;QAChC,IAAMyB,UAAU,GAAG,IAAI,CAAC1B,MAAM,CAACuB,eAAe,CAAC;QAC/C,IAAI,IAAI,CAACpB,WAAW,CAACuB,UAAU,EAAEjB,GAAG,CAAC,EAAE;UACrC,IACEe,SAAS,KAAK,CAAC,CAAC,IAChB,IAAI,CAACrB,WAAW,CAACuB,UAAU,EAAE,IAAI,CAAC1B,MAAM,CAACwB,SAAS,CAAC,CAAC,EACpD;YACAA,SAAS,GAAGD,eAAe;;;;MAMjC,IAAIC,SAAS,KAAK,CAAC,CAAC,EAAE;QACpB;;MAGF,IAAI,CAACxB,MAAM,CAACiB,KAAK,CAAC,GAAG,IAAI,CAACjB,MAAM,CAACwB,SAAS,CAAC;MAC3C,IAAI,CAACxB,MAAM,CAACwB,SAAS,CAAC,GAAGf,GAAG;MAC5BQ,KAAK,GAAGO,SAAS;;GAEpB;EAAA,OAAA3B,IAAA;AAAA;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare type Comparator = (a: any, b: any) => boolean;
|
|
2
|
+
declare type HeapBasicItem = any;
|
|
3
|
+
declare class Heap<T extends HeapBasicItem = HeapBasicItem> {
|
|
4
|
+
private _items;
|
|
5
|
+
private _size;
|
|
6
|
+
private _comparator;
|
|
7
|
+
constructor(items: Array<T>, comparator: Comparator);
|
|
8
|
+
empty(): boolean;
|
|
9
|
+
pop(): T;
|
|
10
|
+
push(item: T): void;
|
|
11
|
+
size(): number;
|
|
12
|
+
peek(): T;
|
|
13
|
+
_heapify(): void;
|
|
14
|
+
_bubbleUp(index: number): void;
|
|
15
|
+
_sinkDown(index: number): void;
|
|
16
|
+
}
|
|
17
|
+
export default Heap;
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@x-oasis/heap",
|
|
3
|
+
"version": "0.0.18",
|
|
4
|
+
"description": "heap function",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "dist/index.d.ts",
|
|
7
|
+
"module": "dist/heap.esm.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"index.ts",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"tsdx": "^0.14.1"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsdx build --tsconfig tsconfig.build.json",
|
|
20
|
+
"clean": "rimraf ./dist",
|
|
21
|
+
"test": "vitest",
|
|
22
|
+
"compile": "tsc -p tsconfig.build.json"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
function defaultComparator(a: number, b: number) {
|
|
2
|
+
return a < b;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
type Comparator = (a: any, b: any) => boolean;
|
|
6
|
+
type HeapBasicItem = any;
|
|
7
|
+
|
|
8
|
+
class Heap<T extends HeapBasicItem = HeapBasicItem> {
|
|
9
|
+
private _items: Array<T>;
|
|
10
|
+
private _size: number;
|
|
11
|
+
private _comparator: Comparator;
|
|
12
|
+
|
|
13
|
+
constructor(items: Array<T>, comparator: Comparator) {
|
|
14
|
+
this._items = items || [];
|
|
15
|
+
this._size = this._items.length;
|
|
16
|
+
this._comparator = comparator || defaultComparator;
|
|
17
|
+
this._heapify();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* @return {boolean}
|
|
22
|
+
*/
|
|
23
|
+
empty() {
|
|
24
|
+
return this._size === 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* @return {*}
|
|
29
|
+
*/
|
|
30
|
+
pop() {
|
|
31
|
+
if (this._size === 0) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const elt = this._items[0];
|
|
36
|
+
|
|
37
|
+
const lastElt = this._items.pop()!;
|
|
38
|
+
this._size--;
|
|
39
|
+
|
|
40
|
+
if (this._size > 0) {
|
|
41
|
+
this._items[0] = lastElt;
|
|
42
|
+
this._sinkDown(0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return elt;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/*
|
|
49
|
+
* @param {*} item
|
|
50
|
+
*/
|
|
51
|
+
push(item: T) {
|
|
52
|
+
this._items[this._size++] = item;
|
|
53
|
+
this._bubbleUp(this._size - 1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* @return {number}
|
|
58
|
+
*/
|
|
59
|
+
size() {
|
|
60
|
+
return this._size;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/*
|
|
64
|
+
* @return {*}
|
|
65
|
+
*/
|
|
66
|
+
peek() {
|
|
67
|
+
if (this._size === 0) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return this._items[0];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_heapify() {
|
|
75
|
+
for (let index = Math.floor((this._size + 1) / 2); index >= 0; index--) {
|
|
76
|
+
this._sinkDown(index);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/*
|
|
81
|
+
* @parent {number} index
|
|
82
|
+
*/
|
|
83
|
+
_bubbleUp(index: number) {
|
|
84
|
+
const elt = this._items[index];
|
|
85
|
+
while (index > 0) {
|
|
86
|
+
const parentIndex = Math.floor((index + 1) / 2) - 1;
|
|
87
|
+
const parentElt = this._items[parentIndex];
|
|
88
|
+
|
|
89
|
+
// if parentElt < elt, stop
|
|
90
|
+
if (this._comparator(parentElt, elt)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// swap
|
|
95
|
+
this._items[parentIndex] = elt;
|
|
96
|
+
this._items[index] = parentElt;
|
|
97
|
+
index = parentIndex;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/*
|
|
102
|
+
* @parent {number} index
|
|
103
|
+
*/
|
|
104
|
+
_sinkDown(index: number) {
|
|
105
|
+
const elt = this._items[index];
|
|
106
|
+
|
|
107
|
+
// eslint-disable-next-line
|
|
108
|
+
while (true) {
|
|
109
|
+
const leftChildIndex = 2 * (index + 1) - 1;
|
|
110
|
+
const rightChildIndex = 2 * (index + 1);
|
|
111
|
+
let swapIndex = -1;
|
|
112
|
+
|
|
113
|
+
if (leftChildIndex < this._size) {
|
|
114
|
+
const leftChild = this._items[leftChildIndex];
|
|
115
|
+
if (this._comparator(leftChild, elt)) {
|
|
116
|
+
swapIndex = leftChildIndex;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (rightChildIndex < this._size) {
|
|
121
|
+
const rightChild = this._items[rightChildIndex];
|
|
122
|
+
if (this._comparator(rightChild, elt)) {
|
|
123
|
+
if (
|
|
124
|
+
swapIndex === -1 ||
|
|
125
|
+
this._comparator(rightChild, this._items[swapIndex])
|
|
126
|
+
) {
|
|
127
|
+
swapIndex = rightChildIndex;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// if we don't have a swap, stop
|
|
133
|
+
if (swapIndex === -1) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this._items[index] = this._items[swapIndex];
|
|
138
|
+
this._items[swapIndex] = elt;
|
|
139
|
+
index = swapIndex;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export default Heap;
|