max-priority-queue-typed 2.5.1 → 2.5.3
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/dist/cjs/index.cjs +207 -71
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +206 -70
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +207 -72
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +206 -71
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +189 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +270 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1089 -161
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1243 -350
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +980 -255
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1174 -284
- package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
- package/dist/types/data-structures/heap/heap.d.ts +140 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +126 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
- package/dist/types/data-structures/queue/deque.d.ts +127 -0
- package/dist/types/data-structures/queue/queue.d.ts +97 -0
- package/dist/types/data-structures/stack/stack.d.ts +72 -2
- package/dist/types/data-structures/trie/trie.d.ts +84 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/umd/max-priority-queue-typed.js +204 -69
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +99 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
- package/src/data-structures/binary-tree/binary-tree.ts +239 -78
- package/src/data-structures/binary-tree/bst.ts +542 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +1223 -261
- package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
- package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
- package/src/data-structures/binary-tree/tree-set.ts +1018 -99
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +102 -16
- package/src/data-structures/heap/heap.ts +153 -23
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
- package/src/data-structures/matrix/matrix.ts +65 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +130 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +86 -2
- package/src/interfaces/binary-tree.ts +1 -9
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
|
@@ -3,6 +3,60 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
5
|
|
|
6
|
+
// src/common/error.ts
|
|
7
|
+
function raise(ErrorClass, message) {
|
|
8
|
+
throw new ErrorClass(message);
|
|
9
|
+
}
|
|
10
|
+
__name(raise, "raise");
|
|
11
|
+
var ERR = {
|
|
12
|
+
// Range / index
|
|
13
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
14
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
15
|
+
// Type / argument
|
|
16
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
17
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
18
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
19
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
20
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
21
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
22
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
23
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
24
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
25
|
+
// State / operation
|
|
26
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
27
|
+
// Matrix
|
|
28
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
29
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
30
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
31
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
32
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
33
|
+
// Order statistic
|
|
34
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/common/index.ts
|
|
38
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
39
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
40
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
41
|
+
return DFSOperation2;
|
|
42
|
+
})(DFSOperation || {});
|
|
43
|
+
var _Range = class _Range {
|
|
44
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
45
|
+
this.low = low;
|
|
46
|
+
this.high = high;
|
|
47
|
+
this.includeLow = includeLow;
|
|
48
|
+
this.includeHigh = includeHigh;
|
|
49
|
+
}
|
|
50
|
+
// Determine whether a key is within the range
|
|
51
|
+
isInRange(key, comparator) {
|
|
52
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
53
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
54
|
+
return lowCheck && highCheck;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
__name(_Range, "Range");
|
|
58
|
+
var Range = _Range;
|
|
59
|
+
|
|
6
60
|
// src/data-structures/base/iterable-element-base.ts
|
|
7
61
|
var _IterableElementBase = class _IterableElementBase {
|
|
8
62
|
/**
|
|
@@ -25,7 +79,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
25
79
|
if (options) {
|
|
26
80
|
const { toElementFn } = options;
|
|
27
81
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
28
|
-
else if (toElementFn)
|
|
82
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
29
83
|
}
|
|
30
84
|
}
|
|
31
85
|
/**
|
|
@@ -181,7 +235,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
181
235
|
acc = initialValue;
|
|
182
236
|
} else {
|
|
183
237
|
const first = iter.next();
|
|
184
|
-
if (first.done)
|
|
238
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
185
239
|
acc = first.value;
|
|
186
240
|
index = 1;
|
|
187
241
|
}
|
|
@@ -225,54 +279,6 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
225
279
|
__name(_IterableElementBase, "IterableElementBase");
|
|
226
280
|
var IterableElementBase = _IterableElementBase;
|
|
227
281
|
|
|
228
|
-
// src/common/error.ts
|
|
229
|
-
var ERR = {
|
|
230
|
-
// Range / index
|
|
231
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
232
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
233
|
-
// Type / argument
|
|
234
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
235
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
236
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
237
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
238
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
239
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
240
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
241
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
242
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
243
|
-
// State / operation
|
|
244
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
245
|
-
// Matrix
|
|
246
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
247
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
248
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
249
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
250
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
// src/common/index.ts
|
|
254
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
255
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
256
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
257
|
-
return DFSOperation2;
|
|
258
|
-
})(DFSOperation || {});
|
|
259
|
-
var _Range = class _Range {
|
|
260
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
261
|
-
this.low = low;
|
|
262
|
-
this.high = high;
|
|
263
|
-
this.includeLow = includeLow;
|
|
264
|
-
this.includeHigh = includeHigh;
|
|
265
|
-
}
|
|
266
|
-
// Determine whether a key is within the range
|
|
267
|
-
isInRange(key, comparator) {
|
|
268
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
269
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
270
|
-
return lowCheck && highCheck;
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
__name(_Range, "Range");
|
|
274
|
-
var Range = _Range;
|
|
275
|
-
|
|
276
282
|
// src/data-structures/heap/heap.ts
|
|
277
283
|
var _Heap = class _Heap extends IterableElementBase {
|
|
278
284
|
/**
|
|
@@ -288,7 +294,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
288
294
|
__publicField(this, "_elements", []);
|
|
289
295
|
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
290
296
|
if (typeof a === "object" || typeof b === "object") {
|
|
291
|
-
|
|
297
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
292
298
|
}
|
|
293
299
|
if (a > b) return 1;
|
|
294
300
|
if (a < b) return -1;
|
|
@@ -337,6 +343,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
337
343
|
|
|
338
344
|
|
|
339
345
|
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
340
353
|
|
|
341
354
|
|
|
342
355
|
|
|
@@ -394,7 +407,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
394
407
|
}
|
|
395
408
|
/**
|
|
396
409
|
* Insert an element.
|
|
397
|
-
* @remarks Time O(
|
|
410
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
398
411
|
* @param element - Element to insert.
|
|
399
412
|
* @returns True.
|
|
400
413
|
|
|
@@ -421,6 +434,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
421
434
|
|
|
422
435
|
|
|
423
436
|
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
424
444
|
|
|
425
445
|
|
|
426
446
|
|
|
@@ -475,6 +495,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
475
495
|
|
|
476
496
|
|
|
477
497
|
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
478
505
|
|
|
479
506
|
|
|
480
507
|
|
|
@@ -539,6 +566,40 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
539
566
|
|
|
540
567
|
|
|
541
568
|
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
* @example
|
|
576
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
577
|
+
* interface Task {
|
|
578
|
+
* id: number;
|
|
579
|
+
* priority: number;
|
|
580
|
+
* name: string;
|
|
581
|
+
* }
|
|
582
|
+
*
|
|
583
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
584
|
+
* const tasks: Task[] = [
|
|
585
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
586
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
587
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
588
|
+
* ];
|
|
589
|
+
*
|
|
590
|
+
* const maxHeap = new Heap(tasks, {
|
|
591
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
592
|
+
* });
|
|
593
|
+
*
|
|
594
|
+
* console.log(maxHeap.size); // 3;
|
|
595
|
+
*
|
|
596
|
+
* // Peek returns highest priority task
|
|
597
|
+
* const topTask = maxHeap.peek();
|
|
598
|
+
* console.log(topTask?.priority); // 8;
|
|
599
|
+
* console.log(topTask?.name); // 'Alert';
|
|
600
|
+
*/
|
|
601
|
+
/**
|
|
602
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
542
603
|
* @example
|
|
543
604
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
544
605
|
* interface Task {
|
|
@@ -566,6 +627,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
566
627
|
* console.log(topTask?.name); // 'Alert';
|
|
567
628
|
*/
|
|
568
629
|
poll() {
|
|
630
|
+
return this.pop();
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
634
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
635
|
+
* @returns The removed top element, or undefined if empty.
|
|
636
|
+
*/
|
|
637
|
+
pop() {
|
|
569
638
|
if (this.elements.length === 0) return;
|
|
570
639
|
const value = this.elements[0];
|
|
571
640
|
const last = this.elements.pop();
|
|
@@ -603,6 +672,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
603
672
|
|
|
604
673
|
|
|
605
674
|
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
606
682
|
|
|
607
683
|
|
|
608
684
|
|
|
@@ -700,6 +776,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
700
776
|
|
|
701
777
|
|
|
702
778
|
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
703
786
|
|
|
704
787
|
|
|
705
788
|
|
|
@@ -744,6 +827,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
744
827
|
|
|
745
828
|
|
|
746
829
|
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
747
837
|
|
|
748
838
|
|
|
749
839
|
|
|
@@ -761,16 +851,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
761
851
|
clear() {
|
|
762
852
|
this._elements = [];
|
|
763
853
|
}
|
|
764
|
-
/**
|
|
765
|
-
* Replace the backing array and rebuild the heap.
|
|
766
|
-
* @remarks Time O(N), Space O(N)
|
|
767
|
-
* @param elements - Iterable used to refill the heap.
|
|
768
|
-
* @returns Array of per-node results from fixing steps.
|
|
769
|
-
*/
|
|
770
|
-
refill(elements) {
|
|
771
|
-
this._elements = Array.from(elements);
|
|
772
|
-
return this.fix();
|
|
773
|
-
}
|
|
774
854
|
/**
|
|
775
855
|
* Check if an equal element exists in the heap.
|
|
776
856
|
* @remarks Time O(N), Space O(1)
|
|
@@ -791,6 +871,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
791
871
|
|
|
792
872
|
|
|
793
873
|
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
794
881
|
|
|
795
882
|
|
|
796
883
|
|
|
@@ -835,6 +922,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
835
922
|
|
|
836
923
|
|
|
837
924
|
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
838
932
|
|
|
839
933
|
|
|
840
934
|
|
|
@@ -859,7 +953,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
859
953
|
}
|
|
860
954
|
if (index < 0) return false;
|
|
861
955
|
if (index === 0) {
|
|
862
|
-
this.
|
|
956
|
+
this.pop();
|
|
863
957
|
} else if (index === this.elements.length - 1) {
|
|
864
958
|
this.elements.pop();
|
|
865
959
|
} else {
|
|
@@ -869,13 +963,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
869
963
|
}
|
|
870
964
|
return true;
|
|
871
965
|
}
|
|
966
|
+
/**
|
|
967
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
968
|
+
*/
|
|
969
|
+
deleteBy(predicate) {
|
|
970
|
+
return this.deleteWhere(predicate);
|
|
971
|
+
}
|
|
872
972
|
/**
|
|
873
973
|
* Delete the first element that matches a predicate.
|
|
874
974
|
* @remarks Time O(N), Space O(1)
|
|
875
975
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
876
976
|
* @returns True if an element was removed.
|
|
877
977
|
*/
|
|
878
|
-
|
|
978
|
+
deleteWhere(predicate) {
|
|
879
979
|
let idx = -1;
|
|
880
980
|
for (let i = 0; i < this.elements.length; i++) {
|
|
881
981
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -885,7 +985,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
885
985
|
}
|
|
886
986
|
if (idx < 0) return false;
|
|
887
987
|
if (idx === 0) {
|
|
888
|
-
this.
|
|
988
|
+
this.pop();
|
|
889
989
|
} else if (idx === this.elements.length - 1) {
|
|
890
990
|
this.elements.pop();
|
|
891
991
|
} else {
|
|
@@ -925,6 +1025,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
925
1025
|
|
|
926
1026
|
|
|
927
1027
|
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
928
1035
|
|
|
929
1036
|
|
|
930
1037
|
|
|
@@ -1002,6 +1109,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1002
1109
|
|
|
1003
1110
|
|
|
1004
1111
|
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1005
1119
|
|
|
1006
1120
|
|
|
1007
1121
|
|
|
@@ -1052,6 +1166,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1052
1166
|
|
|
1053
1167
|
|
|
1054
1168
|
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1055
1176
|
|
|
1056
1177
|
|
|
1057
1178
|
|
|
@@ -1101,6 +1222,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1101
1222
|
|
|
1102
1223
|
|
|
1103
1224
|
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
|
|
1104
1232
|
|
|
1105
1233
|
|
|
1106
1234
|
|
|
@@ -1157,6 +1285,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1157
1285
|
|
|
1158
1286
|
|
|
1159
1287
|
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1160
1295
|
|
|
1161
1296
|
|
|
1162
1297
|
|
|
@@ -1173,7 +1308,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1173
1308
|
*/
|
|
1174
1309
|
map(callback, options, thisArg) {
|
|
1175
1310
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
1176
|
-
if (!comparator)
|
|
1311
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1177
1312
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1178
1313
|
let i = 0;
|
|
1179
1314
|
for (const x of this) {
|
|
@@ -1305,7 +1440,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1305
1440
|
__publicField(this, "_comparator");
|
|
1306
1441
|
this.clear();
|
|
1307
1442
|
this._comparator = comparator || this._defaultComparator;
|
|
1308
|
-
if (typeof this.comparator !== "function")
|
|
1443
|
+
if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
1309
1444
|
}
|
|
1310
1445
|
/**
|
|
1311
1446
|
* Get the circular root list head.
|
|
@@ -1342,7 +1477,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1342
1477
|
* Push an element into the root list.
|
|
1343
1478
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1344
1479
|
* @param element - Element to insert.
|
|
1345
|
-
* @returns
|
|
1480
|
+
* @returns True when the element is added.
|
|
1346
1481
|
*/
|
|
1347
1482
|
push(element) {
|
|
1348
1483
|
const node = this.createNode(element);
|
|
@@ -1351,7 +1486,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1351
1486
|
this.mergeWithRoot(node);
|
|
1352
1487
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1353
1488
|
this._size++;
|
|
1354
|
-
return
|
|
1489
|
+
return true;
|
|
1355
1490
|
}
|
|
1356
1491
|
peek() {
|
|
1357
1492
|
return this.min ? this.min.element : void 0;
|
|
@@ -1527,7 +1662,7 @@ var _MaxPriorityQueue = class _MaxPriorityQueue extends PriorityQueue {
|
|
|
1527
1662
|
super(elements, {
|
|
1528
1663
|
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
1529
1664
|
if (typeof a === "object" || typeof b === "object") {
|
|
1530
|
-
|
|
1665
|
+
raise(TypeError, ERR.comparatorRequired("MaxPriorityQueue"));
|
|
1531
1666
|
}
|
|
1532
1667
|
if (a < b) return 1;
|
|
1533
1668
|
if (a > b) return -1;
|
|
@@ -1554,6 +1689,6 @@ var MaxPriorityQueue = _MaxPriorityQueue;
|
|
|
1554
1689
|
* @license MIT License
|
|
1555
1690
|
*/
|
|
1556
1691
|
|
|
1557
|
-
export { DFSOperation, ERR, FibonacciHeap, FibonacciHeapNode, Heap, MaxPriorityQueue, PriorityQueue, Range };
|
|
1692
|
+
export { DFSOperation, ERR, FibonacciHeap, FibonacciHeapNode, Heap, MaxPriorityQueue, PriorityQueue, Range, raise };
|
|
1558
1693
|
//# sourceMappingURL=index.mjs.map
|
|
1559
1694
|
//# sourceMappingURL=index.mjs.map
|