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.
- package/README.md +40 -37
- package/llms-full.txt +743 -0
- package/llms.txt +100 -0
- package/package.json +34 -29
- package/src/cache/cache-fifo.d.ts +6 -0
- package/src/cache/cache-fifo.js +7 -4
- package/src/cache/cache-lfu.d.ts +18 -0
- package/src/cache/cache-lfu.js +18 -6
- package/src/cache/cache-lru.d.ts +74 -0
- package/src/cache/cache-lru.js +60 -5
- package/src/cache/cache-random.d.ts +20 -0
- package/src/cache/cache-random.js +17 -6
- package/src/cache/decorator.d.ts +46 -0
- package/src/cache/decorator.js +26 -2
- package/src/cache.d.ts +13 -0
- package/src/cache.js +7 -2
- package/src/ext-list.d.ts +3 -0
- package/src/ext-list.js +0 -2
- package/src/ext-slist.d.ts +3 -0
- package/src/ext-slist.js +0 -2
- package/src/ext-value-list.d.ts +3 -0
- package/src/ext-value-list.js +0 -2
- package/src/ext-value-slist.d.ts +3 -0
- package/src/ext-value-slist.js +0 -2
- package/src/heap/basics.d.ts +89 -0
- package/src/heap/basics.js +42 -5
- package/src/heap/leftist-heap.d.ts +107 -0
- package/src/heap/leftist-heap.js +54 -2
- package/src/heap/min-heap.d.ts +270 -0
- package/src/heap/min-heap.js +186 -2
- package/src/heap/skew-heap.d.ts +105 -0
- package/src/heap/skew-heap.js +54 -2
- package/src/heap.d.ts +3 -0
- package/src/heap.js +0 -2
- package/src/list/basics.d.ts +43 -0
- package/src/list/basics.js +26 -8
- package/src/list/core.d.ts +271 -0
- package/src/list/core.js +162 -7
- package/src/list/ext-value.d.ts +253 -0
- package/src/list/ext-value.js +40 -6
- package/src/list/ext.d.ts +242 -0
- package/src/list/ext.js +148 -10
- package/src/list/nodes.d.ts +336 -0
- package/src/list/nodes.js +141 -3
- package/src/list/ptr.d.ts +72 -0
- package/src/list/ptr.js +44 -2
- package/src/list/value.d.ts +292 -0
- package/src/list/value.js +47 -6
- package/src/list-helpers.d.ts +44 -0
- package/src/list-helpers.js +36 -3
- package/src/list-utils.d.ts +141 -0
- package/src/list-utils.js +89 -3
- package/src/list.d.ts +3 -0
- package/src/list.js +0 -2
- package/src/meta-utils.d.ts +212 -0
- package/src/meta-utils.js +152 -1
- package/src/nt-utils.d.ts +91 -0
- package/src/nt-utils.js +65 -4
- package/src/queue.d.ts +74 -0
- package/src/queue.js +28 -2
- package/src/slist/basics.d.ts +47 -0
- package/src/slist/basics.js +23 -8
- package/src/slist/core.d.ts +251 -0
- package/src/slist/core.js +151 -6
- package/src/slist/ext-value.d.ts +188 -0
- package/src/slist/ext-value.js +35 -6
- package/src/slist/ext.d.ts +182 -0
- package/src/slist/ext.js +114 -12
- package/src/slist/nodes.d.ts +361 -0
- package/src/slist/nodes.js +156 -3
- package/src/slist/ptr.d.ts +73 -0
- package/src/slist/ptr.js +45 -2
- package/src/slist/value.d.ts +246 -0
- package/src/slist/value.js +38 -6
- package/src/slist.d.ts +3 -0
- package/src/slist.js +0 -2
- package/src/stack.d.ts +59 -0
- package/src/stack.js +29 -3
- package/src/tree/splay-tree.d.ts +151 -0
- package/src/tree/splay-tree.js +94 -3
- package/src/value-list.d.ts +3 -0
- package/src/value-list.js +0 -2
- package/src/value-slist.d.ts +3 -0
- package/src/value-slist.js +0 -2
- package/cjs/cache/cache-fifo.js +0 -37
- package/cjs/cache/cache-lfu.js +0 -76
- package/cjs/cache/cache-lru.js +0 -100
- package/cjs/cache/cache-random.js +0 -77
- package/cjs/cache/decorator.js +0 -47
- package/cjs/cache.js +0 -27
- package/cjs/ext-list.js +0 -21
- package/cjs/ext-slist.js +0 -21
- package/cjs/ext-value-list.js +0 -21
- package/cjs/ext-value-slist.js +0 -21
- package/cjs/heap/basics.js +0 -63
- package/cjs/heap/leftist-heap.js +0 -124
- package/cjs/heap/min-heap.js +0 -294
- package/cjs/heap/skew-heap.js +0 -114
- package/cjs/heap.js +0 -21
- package/cjs/list/basics.js +0 -88
- package/cjs/list/core.js +0 -305
- package/cjs/list/ext-value.js +0 -88
- package/cjs/list/ext.js +0 -356
- package/cjs/list/nodes.js +0 -240
- package/cjs/list/ptr.js +0 -61
- package/cjs/list/value.js +0 -99
- package/cjs/list-helpers.js +0 -91
- package/cjs/list-utils.js +0 -141
- package/cjs/list.js +0 -21
- package/cjs/meta-utils.js +0 -171
- package/cjs/nt-utils.js +0 -132
- package/cjs/package.json +0 -1
- package/cjs/queue.js +0 -58
- package/cjs/slist/basics.js +0 -71
- package/cjs/slist/core.js +0 -362
- package/cjs/slist/ext-value.js +0 -82
- package/cjs/slist/ext.js +0 -336
- package/cjs/slist/nodes.js +0 -276
- package/cjs/slist/ptr.js +0 -87
- package/cjs/slist/value.js +0 -90
- package/cjs/slist.js +0 -21
- package/cjs/stack.js +0 -55
- package/cjs/tree/splay-tree.js +0 -362
- package/cjs/value-list.js +0 -21
- package/cjs/value-slist.js +0 -21
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import HeapBase, {HeapOptions} from './basics.js';
|
|
2
|
+
|
|
3
|
+
/** Array-based binary min-heap. */
|
|
4
|
+
export class MinHeap<T = unknown> extends HeapBase<T> {
|
|
5
|
+
/** The underlying array storing heap elements. */
|
|
6
|
+
array: T[];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param options - Ordering functions.
|
|
10
|
+
* @param args - Initial arrays or heaps to merge.
|
|
11
|
+
*/
|
|
12
|
+
constructor(options?: HeapOptions<T>, ...args: Array<MinHeap<T> | T[]>);
|
|
13
|
+
|
|
14
|
+
/** Number of elements in the heap. */
|
|
15
|
+
get length(): number;
|
|
16
|
+
|
|
17
|
+
/** Whether the heap has no elements. */
|
|
18
|
+
get isEmpty(): boolean;
|
|
19
|
+
|
|
20
|
+
/** The minimum element without removing it. */
|
|
21
|
+
get top(): T | undefined;
|
|
22
|
+
|
|
23
|
+
/** Alias for {@link top}. */
|
|
24
|
+
peek(): T | undefined;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Remove and return the minimum element.
|
|
28
|
+
* @returns The minimum element, or `undefined` if empty.
|
|
29
|
+
*/
|
|
30
|
+
pop(): T | undefined;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Insert a value into the heap.
|
|
34
|
+
* @param value - Value to insert.
|
|
35
|
+
* @returns `this` for chaining.
|
|
36
|
+
*/
|
|
37
|
+
push(value: T): this;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Push a value then immediately pop the minimum.
|
|
41
|
+
* @param value - Value to push.
|
|
42
|
+
* @returns The minimum element after the push.
|
|
43
|
+
*/
|
|
44
|
+
pushPop(value: T): T;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Pop the minimum then push a new value.
|
|
48
|
+
* @param value - Value to push after popping.
|
|
49
|
+
* @returns The old minimum element.
|
|
50
|
+
*/
|
|
51
|
+
replaceTop(value: T): T;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Check whether a value exists in the heap.
|
|
55
|
+
* @param value - Value to search for.
|
|
56
|
+
* @returns `true` if found.
|
|
57
|
+
*/
|
|
58
|
+
has(value: T): boolean;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Find the index of a value in the underlying array.
|
|
62
|
+
* @param value - Value to search for.
|
|
63
|
+
* @returns The index, or `-1` if not found.
|
|
64
|
+
*/
|
|
65
|
+
findIndex(value: T): number;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Remove a value from the heap.
|
|
69
|
+
* @param value - Value to remove.
|
|
70
|
+
* @returns `this` for chaining.
|
|
71
|
+
*/
|
|
72
|
+
remove(value: T): this;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Remove an element by its array index.
|
|
76
|
+
* @param index - Index to remove.
|
|
77
|
+
* @returns `this` for chaining.
|
|
78
|
+
*/
|
|
79
|
+
removeByIndex(index: number): this;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Replace a value with a new one.
|
|
83
|
+
* @param value - Value to find and replace.
|
|
84
|
+
* @param newValue - Replacement value.
|
|
85
|
+
* @returns `this` for chaining.
|
|
86
|
+
*/
|
|
87
|
+
replace(value: T, newValue: T): this;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Replace an element at a given index.
|
|
91
|
+
* @param index - Index to replace.
|
|
92
|
+
* @param newValue - Replacement value.
|
|
93
|
+
* @returns `this` for chaining.
|
|
94
|
+
*/
|
|
95
|
+
replaceByIndex(index: number, newValue: T): this;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Re-sift the top element downward after an in-place mutation.
|
|
99
|
+
* @returns `this` for chaining.
|
|
100
|
+
*/
|
|
101
|
+
updateTop(): this;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Re-sift an element at a given index after an in-place mutation.
|
|
105
|
+
* @param index - Index of the mutated element.
|
|
106
|
+
* @param isDecreased - `true` if the key decreased (sift up), `false` to sift down.
|
|
107
|
+
* @returns `this` for chaining.
|
|
108
|
+
*/
|
|
109
|
+
updateByIndex(index: number, isDecreased: boolean): this;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Remove all elements.
|
|
113
|
+
* @returns `this` for chaining.
|
|
114
|
+
*/
|
|
115
|
+
clear(): this;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Sort the array in place and release it, leaving the heap empty.
|
|
119
|
+
* @returns The sorted array.
|
|
120
|
+
*/
|
|
121
|
+
releaseSorted(): T[];
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Merge other heaps or arrays into this heap.
|
|
125
|
+
* @param args - Heaps or arrays to merge.
|
|
126
|
+
* @returns `this` for chaining.
|
|
127
|
+
*/
|
|
128
|
+
merge(...args: Array<MinHeap<T> | T[]>): this;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Create a deep copy of this heap.
|
|
132
|
+
* @returns A new MinHeap with the same elements.
|
|
133
|
+
*/
|
|
134
|
+
clone(): MinHeap<T>;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Build a heap-ordered array in place.
|
|
138
|
+
* @param array - Array to heapify.
|
|
139
|
+
* @param less - Ordering function.
|
|
140
|
+
* @returns The heapified array.
|
|
141
|
+
*/
|
|
142
|
+
static build<T = unknown>(array: T[], less?: (a: T, b: T) => boolean): T[];
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Pop the minimum from a heap array.
|
|
146
|
+
* @param heapArray - Heap-ordered array.
|
|
147
|
+
* @param less - Ordering function.
|
|
148
|
+
* @returns The minimum element, or `undefined` if empty.
|
|
149
|
+
*/
|
|
150
|
+
static pop<T = unknown>(heapArray: T[], less?: (a: T, b: T) => boolean): T | undefined;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Push a value onto a heap array.
|
|
154
|
+
* @param heapArray - Heap-ordered array.
|
|
155
|
+
* @param item - Value to push.
|
|
156
|
+
* @param less - Ordering function.
|
|
157
|
+
* @returns The array.
|
|
158
|
+
*/
|
|
159
|
+
static push<T = unknown>(heapArray: T[], item: T, less?: (a: T, b: T) => boolean): T[];
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Push then pop in one operation on a heap array.
|
|
163
|
+
* @param heapArray - Heap-ordered array.
|
|
164
|
+
* @param item - Value to push.
|
|
165
|
+
* @param less - Ordering function.
|
|
166
|
+
* @returns The minimum element.
|
|
167
|
+
*/
|
|
168
|
+
static pushPop<T = unknown>(heapArray: T[], item: T, less?: (a: T, b: T) => boolean): T;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Replace the top of a heap array.
|
|
172
|
+
* @param heapArray - Heap-ordered array.
|
|
173
|
+
* @param item - Replacement value.
|
|
174
|
+
* @param less - Ordering function.
|
|
175
|
+
* @returns The old top element.
|
|
176
|
+
*/
|
|
177
|
+
static replaceTop<T = unknown>(heapArray: T[], item: T, less?: (a: T, b: T) => boolean): T;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Check whether a value exists in a heap array.
|
|
181
|
+
* @param heapArray - Heap-ordered array.
|
|
182
|
+
* @param item - Value to search for.
|
|
183
|
+
* @param equal - Equality function.
|
|
184
|
+
* @returns `true` if found.
|
|
185
|
+
*/
|
|
186
|
+
static has<T = unknown>(heapArray: T[], item: T, equal?: (a: T, b: T) => boolean): boolean;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Find the index of a value in a heap array.
|
|
190
|
+
* @param heapArray - Heap-ordered array.
|
|
191
|
+
* @param item - Value to search for.
|
|
192
|
+
* @param equal - Equality function.
|
|
193
|
+
* @returns The index, or `-1` if not found.
|
|
194
|
+
*/
|
|
195
|
+
static findIndex<T = unknown>(heapArray: T[], item: T, equal?: (a: T, b: T) => boolean): number;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Remove an element by index from a heap array.
|
|
199
|
+
* @param heapArray - Heap-ordered array.
|
|
200
|
+
* @param index - Index to remove.
|
|
201
|
+
* @param less - Ordering function.
|
|
202
|
+
* @returns The array.
|
|
203
|
+
*/
|
|
204
|
+
static removeByIndex<T = unknown>(heapArray: T[], index: number, less?: (a: T, b: T) => boolean): T[];
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Remove a value from a heap array.
|
|
208
|
+
* @param heapArray - Heap-ordered array.
|
|
209
|
+
* @param item - Value to remove.
|
|
210
|
+
* @param less - Ordering function.
|
|
211
|
+
* @param equal - Equality function.
|
|
212
|
+
* @returns The array.
|
|
213
|
+
*/
|
|
214
|
+
static remove<T = unknown>(heapArray: T[], item: T, less?: (a: T, b: T) => boolean, equal?: (a: T, b: T) => boolean): T[];
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Replace an element at a given index in a heap array.
|
|
218
|
+
* @param heapArray - Heap-ordered array.
|
|
219
|
+
* @param index - Index to replace.
|
|
220
|
+
* @param newItem - Replacement value.
|
|
221
|
+
* @param less - Ordering function.
|
|
222
|
+
* @returns The array.
|
|
223
|
+
*/
|
|
224
|
+
static replaceByIndex<T = unknown>(heapArray: T[], index: number, newItem: T, less?: (a: T, b: T) => boolean): T[];
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Replace a value in a heap array.
|
|
228
|
+
* @param heapArray - Heap-ordered array.
|
|
229
|
+
* @param item - Value to find and replace.
|
|
230
|
+
* @param newItem - Replacement value.
|
|
231
|
+
* @param less - Ordering function.
|
|
232
|
+
* @param equal - Equality function.
|
|
233
|
+
* @returns The array.
|
|
234
|
+
*/
|
|
235
|
+
static replace<T = unknown>(
|
|
236
|
+
heapArray: T[],
|
|
237
|
+
item: T,
|
|
238
|
+
newItem: T,
|
|
239
|
+
less?: (a: T, b: T) => boolean,
|
|
240
|
+
equal?: (a: T, b: T) => boolean
|
|
241
|
+
): T[];
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Re-sift an element at a given index in a heap array.
|
|
245
|
+
* @param heapArray - Heap-ordered array.
|
|
246
|
+
* @param index - Index of the mutated element.
|
|
247
|
+
* @param isDecreased - `true` if the key decreased.
|
|
248
|
+
* @param less - Ordering function.
|
|
249
|
+
* @returns The array.
|
|
250
|
+
*/
|
|
251
|
+
static updateByIndex<T = unknown>(heapArray: T[], index: number, isDecreased: boolean, less?: (a: T, b: T) => boolean): T[];
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Heap-sort an array in place (ascending order).
|
|
255
|
+
* @param heapArray - Heap-ordered array.
|
|
256
|
+
* @param less - Ordering function.
|
|
257
|
+
* @returns The sorted array.
|
|
258
|
+
*/
|
|
259
|
+
static sort<T = unknown>(heapArray: T[], less?: (a: T, b: T) => boolean): T[];
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Build a MinHeap from an array.
|
|
263
|
+
* @param array - Array of values.
|
|
264
|
+
* @param options - Ordering functions.
|
|
265
|
+
* @returns A new MinHeap.
|
|
266
|
+
*/
|
|
267
|
+
static from<T = unknown>(array: T[], options?: HeapOptions<T>): MinHeap<T>;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export default MinHeap;
|
package/src/heap/min-heap.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
import HeapBase from './basics.js';
|
|
4
2
|
|
|
5
3
|
// the following functions are inlined:
|
|
@@ -35,7 +33,12 @@ const down = (array, i, less = defaultLess, n = array.length) => {
|
|
|
35
33
|
return array;
|
|
36
34
|
};
|
|
37
35
|
|
|
36
|
+
/** Array-based binary min-heap. */
|
|
38
37
|
export class MinHeap extends HeapBase {
|
|
38
|
+
/**
|
|
39
|
+
* @param {object} [options] - Ordering options (`less`, `equal`, `compare`).
|
|
40
|
+
* @param {...Iterable} args - Initial arrays or heaps to merge.
|
|
41
|
+
*/
|
|
39
42
|
constructor(options, ...args) {
|
|
40
43
|
super(options);
|
|
41
44
|
if (typeof this.compare == 'function') {
|
|
@@ -46,22 +49,30 @@ export class MinHeap extends HeapBase {
|
|
|
46
49
|
if (args.length) this.merge(...args);
|
|
47
50
|
}
|
|
48
51
|
|
|
52
|
+
/** The number of elements. */
|
|
49
53
|
get length() {
|
|
50
54
|
return this.array.length;
|
|
51
55
|
}
|
|
52
56
|
|
|
57
|
+
/** Whether the heap has no elements. */
|
|
53
58
|
get isEmpty() {
|
|
54
59
|
return !this.array.length;
|
|
55
60
|
}
|
|
56
61
|
|
|
62
|
+
/** The minimum element without removing it. */
|
|
57
63
|
get top() {
|
|
58
64
|
return this.array[0];
|
|
59
65
|
}
|
|
60
66
|
|
|
67
|
+
/** Alias for {@link MinHeap#top|top}. */
|
|
61
68
|
peek() {
|
|
62
69
|
return this.array[0];
|
|
63
70
|
}
|
|
64
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Remove and return the minimum element.
|
|
74
|
+
* @returns {*} The removed element, or `undefined` if empty.
|
|
75
|
+
*/
|
|
65
76
|
pop() {
|
|
66
77
|
// return MinHeap.pop(this.array, this.less); // inlined
|
|
67
78
|
switch (this.array.length) {
|
|
@@ -89,6 +100,11 @@ export class MinHeap extends HeapBase {
|
|
|
89
100
|
return top;
|
|
90
101
|
}
|
|
91
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Add an element to the heap.
|
|
105
|
+
* @param {*} value - Element to add.
|
|
106
|
+
* @returns {MinHeap} `this` for chaining.
|
|
107
|
+
*/
|
|
92
108
|
push(value) {
|
|
93
109
|
// MinHeap.push(this.array, value, this.less); // inlined
|
|
94
110
|
let i = this.array.length;
|
|
@@ -104,6 +120,11 @@ export class MinHeap extends HeapBase {
|
|
|
104
120
|
return this;
|
|
105
121
|
}
|
|
106
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Push then pop in one operation.
|
|
125
|
+
* @param {*} value - Element to push.
|
|
126
|
+
* @returns {*} The popped element.
|
|
127
|
+
*/
|
|
107
128
|
pushPop(value) {
|
|
108
129
|
// return MinHeap.pushPop(this.array, value, this.less); // inlined
|
|
109
130
|
if (!this.array.length || this.less(value, this.array[0])) return value;
|
|
@@ -126,6 +147,11 @@ export class MinHeap extends HeapBase {
|
|
|
126
147
|
return top;
|
|
127
148
|
}
|
|
128
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Pop then push in one operation.
|
|
152
|
+
* @param {*} value - Element to push.
|
|
153
|
+
* @returns {*} The previously minimum element.
|
|
154
|
+
*/
|
|
129
155
|
replaceTop(value) {
|
|
130
156
|
// return MinHeap.replaceTop(this.array, value, this.less); // inlined
|
|
131
157
|
const top = this.array[0];
|
|
@@ -147,50 +173,100 @@ export class MinHeap extends HeapBase {
|
|
|
147
173
|
return top;
|
|
148
174
|
}
|
|
149
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Check whether a value exists in the heap.
|
|
178
|
+
* @param {*} value - Value to search for.
|
|
179
|
+
* @returns {boolean} `true` if found.
|
|
180
|
+
*/
|
|
150
181
|
has(value) {
|
|
151
182
|
// return MinHeap.has(this.array, value, this.equal); // inlined
|
|
152
183
|
return this.array.findIndex(element => this.equal(element, value)) >= 0;
|
|
153
184
|
}
|
|
154
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Find the index of a value.
|
|
188
|
+
* @param {*} value - Value to search for.
|
|
189
|
+
* @returns {number} The index, or `-1` if not found.
|
|
190
|
+
*/
|
|
155
191
|
findIndex(value) {
|
|
156
192
|
return this.array.findIndex(element => this.equal(element, value));
|
|
157
193
|
}
|
|
158
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Remove a value from the heap.
|
|
197
|
+
* @param {*} value - Value to remove.
|
|
198
|
+
* @returns {MinHeap} `this` for chaining.
|
|
199
|
+
*/
|
|
159
200
|
remove(value) {
|
|
160
201
|
MinHeap.remove(this.array, value, this.less, this.equal);
|
|
161
202
|
return this;
|
|
162
203
|
}
|
|
163
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Remove an element by its array index.
|
|
207
|
+
* @param {number} index - Index to remove.
|
|
208
|
+
* @returns {MinHeap} `this` for chaining.
|
|
209
|
+
*/
|
|
164
210
|
removeByIndex(index) {
|
|
165
211
|
MinHeap.removeByIndex(this.array, index, this.less);
|
|
166
212
|
return this;
|
|
167
213
|
}
|
|
168
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Replace a value with a new one.
|
|
217
|
+
* @param {*} value - Value to find.
|
|
218
|
+
* @param {*} newValue - Replacement value.
|
|
219
|
+
* @returns {MinHeap} `this` for chaining.
|
|
220
|
+
*/
|
|
169
221
|
replace(value, newValue) {
|
|
170
222
|
MinHeap.replace(this.array, value, newValue, this.less, this.equal);
|
|
171
223
|
return this;
|
|
172
224
|
}
|
|
173
225
|
|
|
226
|
+
/**
|
|
227
|
+
* Replace an element at a given index.
|
|
228
|
+
* @param {number} index - Index to replace.
|
|
229
|
+
* @param {*} newValue - Replacement value.
|
|
230
|
+
* @returns {MinHeap} `this` for chaining.
|
|
231
|
+
*/
|
|
174
232
|
replaceByIndex(index, newValue) {
|
|
175
233
|
MinHeap.replaceByIndex(this.array, index, newValue, this.less);
|
|
176
234
|
return this;
|
|
177
235
|
}
|
|
178
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Re-heapify after the top element has been mutated.
|
|
239
|
+
* @returns {MinHeap} `this` for chaining.
|
|
240
|
+
*/
|
|
179
241
|
updateTop() {
|
|
180
242
|
down(this.array, 0, this.less);
|
|
181
243
|
return this;
|
|
182
244
|
}
|
|
183
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Re-heapify after an element at a given index has been mutated.
|
|
248
|
+
* @param {number} index - Index of the mutated element.
|
|
249
|
+
* @param {boolean} isDecreased - `true` if the key decreased.
|
|
250
|
+
* @returns {MinHeap} `this` for chaining.
|
|
251
|
+
*/
|
|
184
252
|
updateByIndex(index, isDecreased) {
|
|
185
253
|
MinHeap.updateByIndex(this.array, index, isDecreased, this.less);
|
|
186
254
|
return this;
|
|
187
255
|
}
|
|
188
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Remove all elements.
|
|
259
|
+
* @returns {MinHeap} `this` for chaining.
|
|
260
|
+
*/
|
|
189
261
|
clear() {
|
|
190
262
|
this.array = [];
|
|
191
263
|
return this;
|
|
192
264
|
}
|
|
193
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Sort the internal array in place and release it, clearing the heap.
|
|
268
|
+
* @returns {Array} The sorted array.
|
|
269
|
+
*/
|
|
194
270
|
releaseSorted() {
|
|
195
271
|
MinHeap.sort(this.array, this.less);
|
|
196
272
|
const array = this.array;
|
|
@@ -198,6 +274,11 @@ export class MinHeap extends HeapBase {
|
|
|
198
274
|
return array;
|
|
199
275
|
}
|
|
200
276
|
|
|
277
|
+
/**
|
|
278
|
+
* Merge one or more iterables or heaps into this heap.
|
|
279
|
+
* @param {...Iterable} args - Arrays or MinHeaps to merge.
|
|
280
|
+
* @returns {MinHeap} `this` for chaining.
|
|
281
|
+
*/
|
|
201
282
|
merge(...args) {
|
|
202
283
|
if (!args.length) return this;
|
|
203
284
|
this.array = MinHeap.build(
|
|
@@ -213,12 +294,22 @@ export class MinHeap extends HeapBase {
|
|
|
213
294
|
return this;
|
|
214
295
|
}
|
|
215
296
|
|
|
297
|
+
/**
|
|
298
|
+
* Create a shallow copy of this heap.
|
|
299
|
+
* @returns {MinHeap} A new MinHeap with the same elements.
|
|
300
|
+
*/
|
|
216
301
|
clone() {
|
|
217
302
|
const heap = new MinHeap(this);
|
|
218
303
|
heap.array = this.array.slice(0);
|
|
219
304
|
return heap;
|
|
220
305
|
}
|
|
221
306
|
|
|
307
|
+
/**
|
|
308
|
+
* Build a heap from an array in place.
|
|
309
|
+
* @param {Array} array - Array to heapify.
|
|
310
|
+
* @param {Function} [less] - Ordering function.
|
|
311
|
+
* @returns {Array} The heapified array.
|
|
312
|
+
*/
|
|
222
313
|
static build(array, less = MinHeap.defaults.less) {
|
|
223
314
|
if (array.length <= 1) return array;
|
|
224
315
|
for (let n = array.length, j = (n >> 1) - 1; j >= 0; --j) {
|
|
@@ -239,6 +330,12 @@ export class MinHeap extends HeapBase {
|
|
|
239
330
|
return array;
|
|
240
331
|
}
|
|
241
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Pop the minimum from a heap array.
|
|
335
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
336
|
+
* @param {Function} [less] - Ordering function.
|
|
337
|
+
* @returns {*} The removed element.
|
|
338
|
+
*/
|
|
242
339
|
static pop(heapArray, less = MinHeap.defaults.less) {
|
|
243
340
|
switch (heapArray.length) {
|
|
244
341
|
case 0:
|
|
@@ -252,17 +349,38 @@ export class MinHeap extends HeapBase {
|
|
|
252
349
|
return top;
|
|
253
350
|
}
|
|
254
351
|
|
|
352
|
+
/**
|
|
353
|
+
* Push an item onto a heap array.
|
|
354
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
355
|
+
* @param {*} item - Element to add.
|
|
356
|
+
* @param {Function} [less] - Ordering function.
|
|
357
|
+
* @returns {Array} The heap array.
|
|
358
|
+
*/
|
|
255
359
|
static push(heapArray, item, less = MinHeap.defaults.less) {
|
|
256
360
|
const i = heapArray.length;
|
|
257
361
|
heapArray.push(item);
|
|
258
362
|
return up(heapArray, i, less);
|
|
259
363
|
}
|
|
260
364
|
|
|
365
|
+
/**
|
|
366
|
+
* Push then pop on a heap array.
|
|
367
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
368
|
+
* @param {*} item - Element to push.
|
|
369
|
+
* @param {Function} [less] - Ordering function.
|
|
370
|
+
* @returns {*} The popped element.
|
|
371
|
+
*/
|
|
261
372
|
static pushPop(heapArray, item, less = MinHeap.defaults.less) {
|
|
262
373
|
if (!heapArray.length || less(item, heapArray[0])) return item;
|
|
263
374
|
return MinHeap.replaceTop(heapArray, item, less);
|
|
264
375
|
}
|
|
265
376
|
|
|
377
|
+
/**
|
|
378
|
+
* Replace the top of a heap array.
|
|
379
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
380
|
+
* @param {*} item - Replacement element.
|
|
381
|
+
* @param {Function} [less] - Ordering function.
|
|
382
|
+
* @returns {*} The previous top.
|
|
383
|
+
*/
|
|
266
384
|
static replaceTop(heapArray, item, less = MinHeap.defaults.less) {
|
|
267
385
|
const top = heapArray[0];
|
|
268
386
|
heapArray[0] = item;
|
|
@@ -270,14 +388,35 @@ export class MinHeap extends HeapBase {
|
|
|
270
388
|
return top;
|
|
271
389
|
}
|
|
272
390
|
|
|
391
|
+
/**
|
|
392
|
+
* Check whether a value exists in a heap array.
|
|
393
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
394
|
+
* @param {*} item - Value to search for.
|
|
395
|
+
* @param {Function} [equal] - Equality function.
|
|
396
|
+
* @returns {boolean} `true` if found.
|
|
397
|
+
*/
|
|
273
398
|
static has(heapArray, item, equal = MinHeap.defaults.equal) {
|
|
274
399
|
return heapArray.findIndex(element => equal(element, item)) >= 0;
|
|
275
400
|
}
|
|
276
401
|
|
|
402
|
+
/**
|
|
403
|
+
* Find the index of a value in a heap array.
|
|
404
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
405
|
+
* @param {*} item - Value to search for.
|
|
406
|
+
* @param {Function} [equal] - Equality function.
|
|
407
|
+
* @returns {number} The index, or `-1` if not found.
|
|
408
|
+
*/
|
|
277
409
|
static findIndex(heapArray, item, equal = MinHeap.defaults.equal) {
|
|
278
410
|
return heapArray.findIndex(element => equal(element, item));
|
|
279
411
|
}
|
|
280
412
|
|
|
413
|
+
/**
|
|
414
|
+
* Remove an element by index from a heap array.
|
|
415
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
416
|
+
* @param {number} index - Index to remove.
|
|
417
|
+
* @param {Function} [less] - Ordering function.
|
|
418
|
+
* @returns {Array} The heap array.
|
|
419
|
+
*/
|
|
281
420
|
static removeByIndex(heapArray, index, less = MinHeap.defaults.less) {
|
|
282
421
|
if (index < 0 || index >= heapArray.length) return this;
|
|
283
422
|
const last = heapArray.length - 1;
|
|
@@ -290,11 +429,27 @@ export class MinHeap extends HeapBase {
|
|
|
290
429
|
return heapArray;
|
|
291
430
|
}
|
|
292
431
|
|
|
432
|
+
/**
|
|
433
|
+
* Remove a value from a heap array.
|
|
434
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
435
|
+
* @param {*} item - Value to remove.
|
|
436
|
+
* @param {Function} [less] - Ordering function.
|
|
437
|
+
* @param {Function} [equal] - Equality function.
|
|
438
|
+
* @returns {Array} The heap array.
|
|
439
|
+
*/
|
|
293
440
|
static remove(heapArray, item, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
|
|
294
441
|
const index = heapArray.findIndex(element => equal(element, item));
|
|
295
442
|
return MinHeap.removeByIndex(heapArray, index, less);
|
|
296
443
|
}
|
|
297
444
|
|
|
445
|
+
/**
|
|
446
|
+
* Replace an element at a given index in a heap array.
|
|
447
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
448
|
+
* @param {number} index - Index to replace.
|
|
449
|
+
* @param {*} newItem - Replacement element.
|
|
450
|
+
* @param {Function} [less] - Ordering function.
|
|
451
|
+
* @returns {Array} The heap array.
|
|
452
|
+
*/
|
|
298
453
|
static replaceByIndex(heapArray, index, newItem, less = MinHeap.defaults.less) {
|
|
299
454
|
if (index < 0 || index >= heapArray.length) return this;
|
|
300
455
|
const item = heapArray[index];
|
|
@@ -302,16 +457,39 @@ export class MinHeap extends HeapBase {
|
|
|
302
457
|
return MinHeap.updateByIndex(heapArray, index, less(newItem, item), less);
|
|
303
458
|
}
|
|
304
459
|
|
|
460
|
+
/**
|
|
461
|
+
* Replace a value in a heap array.
|
|
462
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
463
|
+
* @param {*} item - Value to find.
|
|
464
|
+
* @param {*} newItem - Replacement value.
|
|
465
|
+
* @param {Function} [less] - Ordering function.
|
|
466
|
+
* @param {Function} [equal] - Equality function.
|
|
467
|
+
* @returns {Array} The heap array.
|
|
468
|
+
*/
|
|
305
469
|
static replace(heapArray, item, newItem, less = MinHeap.defaults.less, equal = MinHeap.defaults.equal) {
|
|
306
470
|
const index = heapArray.findIndex(element => equal(element, item));
|
|
307
471
|
return MinHeap.replaceByIndex(heapArray, index, newItem, less);
|
|
308
472
|
}
|
|
309
473
|
|
|
474
|
+
/**
|
|
475
|
+
* Re-heapify after an element at a given index has been mutated.
|
|
476
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
477
|
+
* @param {number} index - Index of the mutated element.
|
|
478
|
+
* @param {boolean} isDecreased - `true` if the key decreased.
|
|
479
|
+
* @param {Function} [less] - Ordering function.
|
|
480
|
+
* @returns {Array} The heap array.
|
|
481
|
+
*/
|
|
310
482
|
static updateByIndex(heapArray, index, isDecreased, less = MinHeap.defaults.less) {
|
|
311
483
|
if (index < 0 || index >= heapArray.length) return this;
|
|
312
484
|
return (isDecreased ? up : down)(heapArray, index, less);
|
|
313
485
|
}
|
|
314
486
|
|
|
487
|
+
/**
|
|
488
|
+
* Sort a heap array in place (heap sort).
|
|
489
|
+
* @param {Array} heapArray - Heap-ordered array.
|
|
490
|
+
* @param {Function} [less] - Ordering function.
|
|
491
|
+
* @returns {Array} The sorted array.
|
|
492
|
+
*/
|
|
315
493
|
static sort(heapArray, less = MinHeap.defaults.less) {
|
|
316
494
|
if (heapArray.length <= 1) return heapArray;
|
|
317
495
|
for (let n = heapArray.length - 1; n; --n) {
|
|
@@ -321,6 +499,12 @@ export class MinHeap extends HeapBase {
|
|
|
321
499
|
return heapArray;
|
|
322
500
|
}
|
|
323
501
|
|
|
502
|
+
/**
|
|
503
|
+
* Build a MinHeap from an existing array.
|
|
504
|
+
* @param {Array} array - Array of elements.
|
|
505
|
+
* @param {object} [options] - Ordering options.
|
|
506
|
+
* @returns {MinHeap} A new MinHeap.
|
|
507
|
+
*/
|
|
324
508
|
static from(array, options = MinHeap.defaults) {
|
|
325
509
|
const heap = new MinHeap(options);
|
|
326
510
|
heap.array = MinHeap.build(array, heap.less);
|