list-toolkit 2.2.6 → 2.3.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.
Files changed (125) hide show
  1. package/README.md +40 -37
  2. package/llms-full.txt +743 -0
  3. package/llms.txt +100 -0
  4. package/package.json +34 -29
  5. package/src/cache/cache-fifo.d.ts +6 -0
  6. package/src/cache/cache-fifo.js +7 -4
  7. package/src/cache/cache-lfu.d.ts +18 -0
  8. package/src/cache/cache-lfu.js +18 -6
  9. package/src/cache/cache-lru.d.ts +74 -0
  10. package/src/cache/cache-lru.js +60 -5
  11. package/src/cache/cache-random.d.ts +20 -0
  12. package/src/cache/cache-random.js +17 -6
  13. package/src/cache/decorator.d.ts +46 -0
  14. package/src/cache/decorator.js +26 -2
  15. package/src/cache.d.ts +13 -0
  16. package/src/cache.js +7 -2
  17. package/src/ext-list.d.ts +3 -0
  18. package/src/ext-list.js +0 -2
  19. package/src/ext-slist.d.ts +3 -0
  20. package/src/ext-slist.js +0 -2
  21. package/src/ext-value-list.d.ts +3 -0
  22. package/src/ext-value-list.js +0 -2
  23. package/src/ext-value-slist.d.ts +3 -0
  24. package/src/ext-value-slist.js +0 -2
  25. package/src/heap/basics.d.ts +89 -0
  26. package/src/heap/basics.js +42 -5
  27. package/src/heap/leftist-heap.d.ts +107 -0
  28. package/src/heap/leftist-heap.js +54 -2
  29. package/src/heap/min-heap.d.ts +270 -0
  30. package/src/heap/min-heap.js +186 -2
  31. package/src/heap/skew-heap.d.ts +105 -0
  32. package/src/heap/skew-heap.js +54 -2
  33. package/src/heap.d.ts +3 -0
  34. package/src/heap.js +0 -2
  35. package/src/list/basics.d.ts +43 -0
  36. package/src/list/basics.js +26 -8
  37. package/src/list/core.d.ts +271 -0
  38. package/src/list/core.js +162 -7
  39. package/src/list/ext-value.d.ts +253 -0
  40. package/src/list/ext-value.js +40 -6
  41. package/src/list/ext.d.ts +242 -0
  42. package/src/list/ext.js +148 -10
  43. package/src/list/nodes.d.ts +336 -0
  44. package/src/list/nodes.js +141 -3
  45. package/src/list/ptr.d.ts +72 -0
  46. package/src/list/ptr.js +44 -2
  47. package/src/list/value.d.ts +292 -0
  48. package/src/list/value.js +47 -6
  49. package/src/list-helpers.d.ts +44 -0
  50. package/src/list-helpers.js +36 -3
  51. package/src/list-utils.d.ts +141 -0
  52. package/src/list-utils.js +89 -3
  53. package/src/list.d.ts +3 -0
  54. package/src/list.js +0 -2
  55. package/src/meta-utils.d.ts +212 -0
  56. package/src/meta-utils.js +152 -1
  57. package/src/nt-utils.d.ts +91 -0
  58. package/src/nt-utils.js +65 -4
  59. package/src/queue.d.ts +74 -0
  60. package/src/queue.js +28 -2
  61. package/src/slist/basics.d.ts +47 -0
  62. package/src/slist/basics.js +23 -8
  63. package/src/slist/core.d.ts +251 -0
  64. package/src/slist/core.js +151 -6
  65. package/src/slist/ext-value.d.ts +188 -0
  66. package/src/slist/ext-value.js +35 -6
  67. package/src/slist/ext.d.ts +182 -0
  68. package/src/slist/ext.js +114 -12
  69. package/src/slist/nodes.d.ts +361 -0
  70. package/src/slist/nodes.js +156 -3
  71. package/src/slist/ptr.d.ts +73 -0
  72. package/src/slist/ptr.js +45 -2
  73. package/src/slist/value.d.ts +246 -0
  74. package/src/slist/value.js +38 -6
  75. package/src/slist.d.ts +3 -0
  76. package/src/slist.js +0 -2
  77. package/src/stack.d.ts +59 -0
  78. package/src/stack.js +29 -3
  79. package/src/tree/splay-tree.d.ts +151 -0
  80. package/src/tree/splay-tree.js +94 -3
  81. package/src/value-list.d.ts +3 -0
  82. package/src/value-list.js +0 -2
  83. package/src/value-slist.d.ts +3 -0
  84. package/src/value-slist.js +0 -2
  85. package/cjs/cache/cache-fifo.js +0 -37
  86. package/cjs/cache/cache-lfu.js +0 -76
  87. package/cjs/cache/cache-lru.js +0 -100
  88. package/cjs/cache/cache-random.js +0 -77
  89. package/cjs/cache/decorator.js +0 -47
  90. package/cjs/cache.js +0 -27
  91. package/cjs/ext-list.js +0 -21
  92. package/cjs/ext-slist.js +0 -21
  93. package/cjs/ext-value-list.js +0 -21
  94. package/cjs/ext-value-slist.js +0 -21
  95. package/cjs/heap/basics.js +0 -63
  96. package/cjs/heap/leftist-heap.js +0 -124
  97. package/cjs/heap/min-heap.js +0 -294
  98. package/cjs/heap/skew-heap.js +0 -114
  99. package/cjs/heap.js +0 -21
  100. package/cjs/list/basics.js +0 -88
  101. package/cjs/list/core.js +0 -305
  102. package/cjs/list/ext-value.js +0 -88
  103. package/cjs/list/ext.js +0 -356
  104. package/cjs/list/nodes.js +0 -240
  105. package/cjs/list/ptr.js +0 -61
  106. package/cjs/list/value.js +0 -99
  107. package/cjs/list-helpers.js +0 -91
  108. package/cjs/list-utils.js +0 -141
  109. package/cjs/list.js +0 -21
  110. package/cjs/meta-utils.js +0 -171
  111. package/cjs/nt-utils.js +0 -132
  112. package/cjs/package.json +0 -1
  113. package/cjs/queue.js +0 -58
  114. package/cjs/slist/basics.js +0 -71
  115. package/cjs/slist/core.js +0 -362
  116. package/cjs/slist/ext-value.js +0 -82
  117. package/cjs/slist/ext.js +0 -336
  118. package/cjs/slist/nodes.js +0 -276
  119. package/cjs/slist/ptr.js +0 -87
  120. package/cjs/slist/value.js +0 -90
  121. package/cjs/slist.js +0 -21
  122. package/cjs/stack.js +0 -55
  123. package/cjs/tree/splay-tree.js +0 -362
  124. package/cjs/value-list.js +0 -21
  125. package/cjs/value-slist.js +0 -21
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _exportNames = {};
7
- exports.default = void 0;
8
- var _extValue = _interopRequireWildcard(require("./slist/ext-value.js"));
9
- Object.keys(_extValue).forEach(function (key) {
10
- if (key === "default" || key === "__esModule") return;
11
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
12
- if (key in exports && exports[key] === _extValue[key]) return;
13
- Object.defineProperty(exports, key, {
14
- enumerable: true,
15
- get: function () {
16
- return _extValue[key];
17
- }
18
- });
19
- });
20
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
21
- var _default = exports.default = _extValue.default;
@@ -1,63 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.HeapBase = void 0;
7
- var _metaUtils = require("../meta-utils.js");
8
- const defaultLess = (a, b) => a < b;
9
- const defaultEqual = (a, b) => a === b;
10
- class HeapBase {
11
- constructor(options) {
12
- (0, _metaUtils.copyOptions)(this, HeapBase.defaults, options);
13
- }
14
-
15
- // main methods
16
- get isEmpty() {
17
- throw new Error('Not implemented');
18
- }
19
- get top() {
20
- throw new Error('Not implemented');
21
- }
22
- peek() {
23
- return this.top;
24
- }
25
- pop() {
26
- throw new Error('Not implemented');
27
- }
28
- push() {
29
- throw new Error('Not implemented');
30
- }
31
- clear() {
32
- throw new Error('Not implemented');
33
- }
34
-
35
- // performance methods
36
- pushPop(value) {
37
- this.push(value);
38
- return this.pop();
39
- }
40
- replaceTop(value) {
41
- const z = this.pop();
42
- this.push(value);
43
- return z;
44
- }
45
-
46
- // helper methods
47
- merge(...args) {
48
- throw new Error('Not implemented');
49
- }
50
- clone() {
51
- throw new Error('Not implemented');
52
- }
53
- make(...args) {
54
- return new this.constructor(this, ...args);
55
- }
56
- }
57
- exports.HeapBase = HeapBase;
58
- HeapBase.defaults = {
59
- less: defaultLess,
60
- equal: defaultEqual,
61
- compare: null
62
- };
63
- var _default = exports.default = HeapBase;
@@ -1,124 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.LeftistHeapNode = exports.LeftistHeap = void 0;
7
- var _basics = _interopRequireDefault(require("./basics.js"));
8
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
- const defaultLess = (a, b) => a < b;
10
- const merge = (a, b, less) => {
11
- if (!a) return b;
12
- if (!b) return a;
13
- if (less(b.value, a.value)) [a, b] = [b, a]; // swap
14
-
15
- a.right = merge(a.right, b, less);
16
- if (!a.left) {
17
- [a.left, a.right] = [a.right, a.left]; // swap
18
- a.s = 1;
19
- return a;
20
- }
21
- if (a.left.s < a.right.s) {
22
- [a.left, a.right] = [a.right, a.left]; // swap
23
- }
24
- a.s = a.right.s + 1;
25
- return a;
26
- };
27
- class LeftistHeapNode {
28
- constructor(value) {
29
- this.value = value;
30
- this.right = this.left = null;
31
- this.s = 1;
32
- }
33
- clear() {
34
- this.left = this.right = null;
35
- this.s = 1;
36
- }
37
- clone() {
38
- const node = new LeftistHeapNode(this.value);
39
- node.left = this.left && this.left.clone();
40
- node.right = this.right && this.right.clone();
41
- return node;
42
- }
43
- }
44
- exports.LeftistHeapNode = LeftistHeapNode;
45
- class LeftistHeap extends _basics.default {
46
- constructor(options, ...args) {
47
- super(options);
48
- if (typeof this.compare == 'function') {
49
- this.less = (a, b) => this.compare(a, b) < 0;
50
- }
51
- this.root = null;
52
- this.size = 0;
53
- if (args.length) this.merge(...args);
54
- }
55
- get isEmpty() {
56
- return !this.root;
57
- }
58
- get length() {
59
- return this.size;
60
- }
61
- get top() {
62
- return this.root ? this.root.value : undefined;
63
- }
64
- peek() {
65
- return this.root ? this.root.value : undefined;
66
- }
67
- push(value) {
68
- this.root = merge(this.root, new LeftistHeapNode(value), this.less);
69
- ++this.size;
70
- return this;
71
- }
72
- pop() {
73
- if (!this.root) return;
74
- const z = this.root;
75
- this.root = merge(this.root.left, this.root.right, this.less);
76
- --this.size;
77
- return z.value;
78
- }
79
- pushPop(value) {
80
- if (!this.root || this.less(value, this.root.value)) return value;
81
- const z = this.root;
82
- this.root = merge(z.left, new LeftistHeapNode(value), this.less);
83
- this.root = merge(this.root, z.right, this.less);
84
- return z.value;
85
- }
86
- replaceTop(value) {
87
- if (!this.root) {
88
- this.root = new LeftistHeapNode(value);
89
- this.size = 1;
90
- return; // undefined
91
- }
92
- const z = this.root;
93
- this.root = merge(z.left, new LeftistHeapNode(value), this.less);
94
- this.root = merge(this.root, z.right, this.less);
95
- return z.value;
96
- }
97
- clear() {
98
- this.root = null;
99
- this.size = 0;
100
- return this;
101
- }
102
- merge(...args) {
103
- for (const other of args) {
104
- this.root = merge(this.root, other.root, this.less);
105
- this.size += other.size;
106
- other.root = null;
107
- other.size = 0;
108
- }
109
- return this;
110
- }
111
- clone() {
112
- const heap = new LeftistHeap(this);
113
- heap.root = this.root && this.root.clone();
114
- heap.size = this.size;
115
- return heap;
116
- }
117
- static from(array, options = _basics.default.defaults) {
118
- const heap = new LeftistHeap(options);
119
- for (const value of array) heap.push(value);
120
- return heap;
121
- }
122
- }
123
- exports.LeftistHeap = LeftistHeap;
124
- var _default = exports.default = LeftistHeap;
@@ -1,294 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.MinHeap = void 0;
7
- var _basics = _interopRequireDefault(require("./basics.js"));
8
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
- // the following functions are inlined:
10
-
11
- // const left = i => (i << 1) + 1;
12
- // const right = i => (i + 1) << 1;
13
- // const parent = i => (i - 1) >> 1;
14
-
15
- const up = (array, i, less = defaultLess) => {
16
- for (let p = i - 1 >> 1; i > 0; i = p, p = i - 1 >> 1) {
17
- const iValue = array[i],
18
- pValue = array[p];
19
- if (!less(iValue, pValue)) break;
20
- array[i] = pValue;
21
- array[p] = iValue;
22
- }
23
- return array;
24
- };
25
- const down = (array, i, less = defaultLess, n = array.length) => {
26
- for (;;) {
27
- const l = (i << 1) + 1;
28
- if (l >= n) break;
29
- const r = l + 1,
30
- c = r < n && less(array[r], array[l]) ? r : l,
31
- iValue = array[i],
32
- cValue = array[c];
33
- if (!less(cValue, iValue)) break;
34
- array[i] = cValue;
35
- array[c] = iValue;
36
- i = c;
37
- }
38
- return array;
39
- };
40
- class MinHeap extends _basics.default {
41
- constructor(options, ...args) {
42
- super(options);
43
- if (typeof this.compare == 'function') {
44
- this.less = (a, b) => this.compare(a, b) < 0;
45
- this.equal = (a, b) => !this.compare(a, b);
46
- }
47
- this.array = [];
48
- if (args.length) this.merge(...args);
49
- }
50
- get length() {
51
- return this.array.length;
52
- }
53
- get isEmpty() {
54
- return !this.array.length;
55
- }
56
- get top() {
57
- return this.array[0];
58
- }
59
- peek() {
60
- return this.array[0];
61
- }
62
- pop() {
63
- // return MinHeap.pop(this.array, this.less); // inlined
64
- switch (this.array.length) {
65
- case 0:
66
- return;
67
- case 1:
68
- return this.array.pop();
69
- }
70
- const top = this.array[0];
71
- this.array[0] = this.array.pop();
72
- // down(this.array, 0, this.less); // inlined
73
- const n = this.array.length;
74
- for (let i = 0;;) {
75
- const l = (i << 1) + 1;
76
- if (l >= n) break;
77
- const r = l + 1,
78
- c = r < n && this.less(this.array[r], this.array[l]) ? r : l,
79
- iValue = this.array[i],
80
- cValue = this.array[c];
81
- if (!this.less(cValue, iValue)) break;
82
- this.array[i] = cValue;
83
- this.array[c] = iValue;
84
- i = c;
85
- }
86
- return top;
87
- }
88
- push(value) {
89
- // MinHeap.push(this.array, value, this.less); // inlined
90
- let i = this.array.length;
91
- this.array.push(value);
92
- // up(this.array, i, this.less); // inlined
93
- for (let p = i - 1 >> 1; i > 0; i = p, p = i - 1 >> 1) {
94
- const iValue = this.array[i],
95
- pValue = this.array[p];
96
- if (!this.less(iValue, pValue)) break;
97
- this.array[i] = pValue;
98
- this.array[p] = iValue;
99
- }
100
- return this;
101
- }
102
- pushPop(value) {
103
- // return MinHeap.pushPop(this.array, value, this.less); // inlined
104
- if (!this.array.length || this.less(value, this.array[0])) return value;
105
- const top = this.array[0];
106
- this.array[0] = value;
107
- // down(this.array, 0, this.less); // inlined
108
- const n = this.array.length;
109
- for (let i = 0;;) {
110
- const l = (i << 1) + 1;
111
- if (l >= n) break;
112
- const r = l + 1,
113
- c = r < n && this.less(this.array[r], this.array[l]) ? r : l,
114
- iValue = this.array[i],
115
- cValue = this.array[c];
116
- if (!this.less(cValue, iValue)) break;
117
- this.array[i] = cValue;
118
- this.array[c] = iValue;
119
- i = c;
120
- }
121
- return top;
122
- }
123
- replaceTop(value) {
124
- // return MinHeap.replaceTop(this.array, value, this.less); // inlined
125
- const top = this.array[0];
126
- this.array[0] = value;
127
- // down(this.array, 0, this.less); // inlined
128
- const n = this.array.length;
129
- for (let i = 0;;) {
130
- const l = (i << 1) + 1;
131
- if (l >= n) break;
132
- const r = l + 1,
133
- c = r < n && this.less(this.array[r], this.array[l]) ? r : l,
134
- iValue = this.array[i],
135
- cValue = this.array[c];
136
- if (!this.less(cValue, iValue)) break;
137
- this.array[i] = cValue;
138
- this.array[c] = iValue;
139
- i = c;
140
- }
141
- return top;
142
- }
143
- has(value) {
144
- // return MinHeap.has(this.array, value, this.equal); // inlined
145
- return this.array.findIndex(element => this.equal(element, value)) >= 0;
146
- }
147
- findIndex(value) {
148
- return this.array.findIndex(element => this.equal(element, value));
149
- }
150
- remove(value) {
151
- MinHeap.remove(this.array, value, this.less, this.equal);
152
- return this;
153
- }
154
- removeByIndex(index) {
155
- MinHeap.removeByIndex(this.array, index, this.less);
156
- return this;
157
- }
158
- replace(value, newValue) {
159
- MinHeap.replace(this.array, value, newValue, this.less, this.equal);
160
- return this;
161
- }
162
- replaceByIndex(index, newValue) {
163
- MinHeap.replaceByIndex(this.array, index, newValue, this.less);
164
- return this;
165
- }
166
- updateTop() {
167
- down(this.array, 0, this.less);
168
- return this;
169
- }
170
- updateByIndex(index, isDecreased) {
171
- MinHeap.updateByIndex(this.array, index, isDecreased, this.less);
172
- return this;
173
- }
174
- clear() {
175
- this.array = [];
176
- return this;
177
- }
178
- releaseSorted() {
179
- MinHeap.sort(this.array, this.less);
180
- const array = this.array;
181
- this.array = [];
182
- return array;
183
- }
184
- merge(...args) {
185
- if (!args.length) return this;
186
- this.array = MinHeap.build(this.array.concat(...args.map(item => {
187
- if (item instanceof MinHeap) return item.array;
188
- if (!item) return [];
189
- return item;
190
- })), this.less);
191
- return this;
192
- }
193
- clone() {
194
- const heap = new MinHeap(this);
195
- heap.array = this.array.slice(0);
196
- return heap;
197
- }
198
- static build(array, less = MinHeap.defaults.less) {
199
- if (array.length <= 1) return array;
200
- for (let n = array.length, j = (n >> 1) - 1; j >= 0; --j) {
201
- // down(array, j, less, n); // inlined
202
- for (let i = j;;) {
203
- const l = (i << 1) + 1;
204
- if (l >= n) break;
205
- const r = l + 1,
206
- c = r < n && less(array[r], array[l]) ? r : l,
207
- iValue = array[i],
208
- cValue = array[c];
209
- if (!less(cValue, iValue)) break;
210
- array[i] = cValue;
211
- array[c] = iValue;
212
- i = c;
213
- }
214
- }
215
- return array;
216
- }
217
- static pop(heapArray, less = MinHeap.defaults.less) {
218
- switch (heapArray.length) {
219
- case 0:
220
- return;
221
- case 1:
222
- return heapArray.pop();
223
- }
224
- const top = heapArray[0];
225
- heapArray[0] = heapArray.pop();
226
- down(heapArray, 0, less);
227
- return top;
228
- }
229
- static push(heapArray, item, less = MinHeap.defaults.less) {
230
- const i = heapArray.length;
231
- heapArray.push(item);
232
- return up(heapArray, i, less);
233
- }
234
- static pushPop(heapArray, item, less = MinHeap.defaults.less) {
235
- if (!heapArray.length || less(item, heapArray[0])) return item;
236
- return MinHeap.replaceTop(heapArray, item, less);
237
- }
238
- static replaceTop(heapArray, item, less = MinHeap.defaults.less) {
239
- const top = heapArray[0];
240
- heapArray[0] = item;
241
- down(heapArray, 0, less);
242
- return top;
243
- }
244
- static has(heapArray, item, equal = MinHeap.defaults.equal) {
245
- return heapArray.findIndex(element => equal(element, item)) >= 0;
246
- }
247
- static findIndex(heapArray, item, equal = MinHeap.defaults.equal) {
248
- return heapArray.findIndex(element => equal(element, item));
249
- }
250
- static removeByIndex(heapArray, index, less = MinHeap.defaults.less) {
251
- if (index < 0 || index >= heapArray.length) return this;
252
- const last = heapArray.length - 1;
253
- if (index !== last) {
254
- const item = heapArray[index],
255
- newItem = heapArray[index] = heapArray.pop();
256
- return MinHeap.updateByIndex(heapArray, index, less(newItem, item), less);
257
- }
258
- heapArray.pop();
259
- return heapArray;
260
- }
261
- static remove(heapArray, item, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
262
- const index = heapArray.findIndex(element => equal(element, item));
263
- return MinHeap.removeByIndex(heapArray, index, less);
264
- }
265
- static replaceByIndex(heapArray, index, newItem, less = MinHeap.defaults.less) {
266
- if (index < 0 || index >= heapArray.length) return this;
267
- const item = heapArray[index];
268
- heapArray[index] = newItem;
269
- return MinHeap.updateByIndex(heapArray, index, less(newItem, item), less);
270
- }
271
- static replace(heapArray, item, newItem, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
272
- const index = heapArray.findIndex(element => equal(element, item));
273
- return MinHeap.replaceByIndex(heapArray, index, newItem, less);
274
- }
275
- static updateByIndex(heapArray, index, isDecreased, less = MinHeap.defaults.less) {
276
- if (index < 0 || index >= heapArray.length) return this;
277
- return (isDecreased ? up : down)(heapArray, index, less);
278
- }
279
- static sort(heapArray, less = MinHeap.defaults.less) {
280
- if (heapArray.length <= 1) return heapArray;
281
- for (let n = heapArray.length - 1; n; --n) {
282
- [heapArray[0], heapArray[n]] = [heapArray[n], heapArray[0]];
283
- down(heapArray, 0, less, n);
284
- }
285
- return heapArray;
286
- }
287
- static from(array, options = MinHeap.defaults) {
288
- const heap = new MinHeap(options);
289
- heap.array = MinHeap.build(array, heap.less);
290
- return heap;
291
- }
292
- }
293
- exports.MinHeap = MinHeap;
294
- var _default = exports.default = MinHeap;
@@ -1,114 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.SkewHeapNode = exports.SkewHeap = void 0;
7
- var _basics = _interopRequireDefault(require("./basics.js"));
8
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
- const merge = (a, b, less) => {
10
- if (!a) return b;
11
- if (!b) return a;
12
- if (less(b.value, a.value)) [a, b] = [b, a]; // swap
13
-
14
- const temp = a.right;
15
- a.right = a.left;
16
- a.left = merge(b, temp, less);
17
- return a;
18
- };
19
- class SkewHeapNode {
20
- constructor(value) {
21
- this.value = value;
22
- this.right = this.left = null;
23
- }
24
- clear() {
25
- this.left = this.right = null;
26
- }
27
- clone() {
28
- const node = new SkewHeapNode(this.value);
29
- node.left = this.left && this.left.clone();
30
- node.right = this.right && this.right.clone();
31
- return node;
32
- }
33
- }
34
- exports.SkewHeapNode = SkewHeapNode;
35
- class SkewHeap extends _basics.default {
36
- constructor(options, ...args) {
37
- super(options);
38
- if (typeof this.compare == 'function') {
39
- this.less = (a, b) => this.compare(a, b) < 0;
40
- }
41
- this.root = null;
42
- this.size = 0;
43
- if (args.length) this.merge(...args);
44
- }
45
- get isEmpty() {
46
- return !this.root;
47
- }
48
- get length() {
49
- return this.size;
50
- }
51
- get top() {
52
- return this.root ? this.root.value : undefined;
53
- }
54
- peek() {
55
- return this.root ? this.root.value : undefined;
56
- }
57
- push(value) {
58
- this.root = merge(this.root, new SkewHeapNode(value), this.less);
59
- ++this.size;
60
- return this;
61
- }
62
- pop() {
63
- if (!this.root) return;
64
- const z = this.root;
65
- this.root = merge(this.root.left, this.root.right, this.less);
66
- --this.size;
67
- return z.value;
68
- }
69
- pushPop(value) {
70
- if (!this.root || this.less(value, this.root.value)) return value;
71
- const z = this.root;
72
- this.root = merge(z.left, new SkewHeapNode(value), this.less);
73
- this.root = merge(this.root, z.right, this.less);
74
- return z.value;
75
- }
76
- replaceTop(value) {
77
- if (!this.root) {
78
- this.root = new SkewHeapNode(value);
79
- this.size = 1;
80
- return; // undefined
81
- }
82
- const z = this.root;
83
- this.root = merge(z.left, new SkewHeapNode(value), this.less);
84
- this.root = merge(this.root, z.right, this.less);
85
- return z.value;
86
- }
87
- clear() {
88
- this.root = null;
89
- this.size = 0;
90
- return this;
91
- }
92
- merge(...args) {
93
- for (const other of args) {
94
- this.root = merge(this.root, other.root, this.less);
95
- this.size += other.size;
96
- other.root = null;
97
- other.size = 0;
98
- }
99
- return this;
100
- }
101
- clone() {
102
- const heap = new SkewHeap(this);
103
- heap.root = this.root && this.root.clone();
104
- heap.size = this.size;
105
- return heap;
106
- }
107
- static from(array, options = _basics.default.defaults) {
108
- const heap = new SkewHeap(options);
109
- for (const value of array) heap.push(value);
110
- return heap;
111
- }
112
- }
113
- exports.SkewHeap = SkewHeap;
114
- var _default = exports.default = SkewHeap;
package/cjs/heap.js DELETED
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _exportNames = {};
7
- exports.default = void 0;
8
- var _minHeap = _interopRequireWildcard(require("./heap/min-heap.js"));
9
- Object.keys(_minHeap).forEach(function (key) {
10
- if (key === "default" || key === "__esModule") return;
11
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
12
- if (key in exports && exports[key] === _minHeap[key]) return;
13
- Object.defineProperty(exports, key, {
14
- enumerable: true,
15
- get: function () {
16
- return _minHeap[key];
17
- }
18
- });
19
- });
20
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
21
- var _default = exports.default = _minHeap.default;