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
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,6 +3,61 @@
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
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 {
|
|
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
|
+
static {
|
|
51
|
+
__name(this, "Range");
|
|
52
|
+
}
|
|
53
|
+
// Determine whether a key is within the range
|
|
54
|
+
isInRange(key, comparator) {
|
|
55
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
56
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
57
|
+
return lowCheck && highCheck;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
6
61
|
// src/data-structures/base/iterable-element-base.ts
|
|
7
62
|
var IterableElementBase = class {
|
|
8
63
|
static {
|
|
@@ -21,7 +76,7 @@ var IterableElementBase = class {
|
|
|
21
76
|
if (options) {
|
|
22
77
|
const { toElementFn } = options;
|
|
23
78
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
24
|
-
else if (toElementFn)
|
|
79
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
25
80
|
}
|
|
26
81
|
}
|
|
27
82
|
/**
|
|
@@ -184,7 +239,7 @@ var IterableElementBase = class {
|
|
|
184
239
|
acc = initialValue;
|
|
185
240
|
} else {
|
|
186
241
|
const first = iter.next();
|
|
187
|
-
if (first.done)
|
|
242
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
188
243
|
acc = first.value;
|
|
189
244
|
index = 1;
|
|
190
245
|
}
|
|
@@ -226,55 +281,6 @@ var IterableElementBase = class {
|
|
|
226
281
|
}
|
|
227
282
|
};
|
|
228
283
|
|
|
229
|
-
// src/common/error.ts
|
|
230
|
-
var ERR = {
|
|
231
|
-
// Range / index
|
|
232
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
233
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
234
|
-
// Type / argument
|
|
235
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
236
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
237
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
238
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
239
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
240
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
241
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
242
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
243
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
244
|
-
// State / operation
|
|
245
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
246
|
-
// Matrix
|
|
247
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
248
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
249
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
250
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
251
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
// src/common/index.ts
|
|
255
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
256
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
257
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
258
|
-
return DFSOperation2;
|
|
259
|
-
})(DFSOperation || {});
|
|
260
|
-
var Range = class {
|
|
261
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
262
|
-
this.low = low;
|
|
263
|
-
this.high = high;
|
|
264
|
-
this.includeLow = includeLow;
|
|
265
|
-
this.includeHigh = includeHigh;
|
|
266
|
-
}
|
|
267
|
-
static {
|
|
268
|
-
__name(this, "Range");
|
|
269
|
-
}
|
|
270
|
-
// Determine whether a key is within the range
|
|
271
|
-
isInRange(key, comparator) {
|
|
272
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
273
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
274
|
-
return lowCheck && highCheck;
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
|
|
278
284
|
// src/data-structures/heap/heap.ts
|
|
279
285
|
var Heap = class _Heap extends IterableElementBase {
|
|
280
286
|
static {
|
|
@@ -333,6 +339,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
333
339
|
|
|
334
340
|
|
|
335
341
|
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
336
349
|
|
|
337
350
|
|
|
338
351
|
|
|
@@ -389,7 +402,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
389
402
|
}
|
|
390
403
|
/**
|
|
391
404
|
* Insert an element.
|
|
392
|
-
* @remarks Time O(
|
|
405
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
393
406
|
* @param element - Element to insert.
|
|
394
407
|
* @returns True.
|
|
395
408
|
|
|
@@ -416,6 +429,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
416
429
|
|
|
417
430
|
|
|
418
431
|
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
419
439
|
|
|
420
440
|
|
|
421
441
|
|
|
@@ -470,6 +490,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
470
490
|
|
|
471
491
|
|
|
472
492
|
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
473
500
|
|
|
474
501
|
|
|
475
502
|
|
|
@@ -534,6 +561,40 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
534
561
|
|
|
535
562
|
|
|
536
563
|
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
* @example
|
|
571
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
572
|
+
* interface Task {
|
|
573
|
+
* id: number;
|
|
574
|
+
* priority: number;
|
|
575
|
+
* name: string;
|
|
576
|
+
* }
|
|
577
|
+
*
|
|
578
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
579
|
+
* const tasks: Task[] = [
|
|
580
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
581
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
582
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
583
|
+
* ];
|
|
584
|
+
*
|
|
585
|
+
* const maxHeap = new Heap(tasks, {
|
|
586
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
587
|
+
* });
|
|
588
|
+
*
|
|
589
|
+
* console.log(maxHeap.size); // 3;
|
|
590
|
+
*
|
|
591
|
+
* // Peek returns highest priority task
|
|
592
|
+
* const topTask = maxHeap.peek();
|
|
593
|
+
* console.log(topTask?.priority); // 8;
|
|
594
|
+
* console.log(topTask?.name); // 'Alert';
|
|
595
|
+
*/
|
|
596
|
+
/**
|
|
597
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
537
598
|
* @example
|
|
538
599
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
539
600
|
* interface Task {
|
|
@@ -561,6 +622,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
561
622
|
* console.log(topTask?.name); // 'Alert';
|
|
562
623
|
*/
|
|
563
624
|
poll() {
|
|
625
|
+
return this.pop();
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
629
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
630
|
+
* @returns The removed top element, or undefined if empty.
|
|
631
|
+
*/
|
|
632
|
+
pop() {
|
|
564
633
|
if (this.elements.length === 0) return;
|
|
565
634
|
const value = this.elements[0];
|
|
566
635
|
const last = this.elements.pop();
|
|
@@ -598,6 +667,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
598
667
|
|
|
599
668
|
|
|
600
669
|
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
601
677
|
|
|
602
678
|
|
|
603
679
|
|
|
@@ -695,6 +771,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
695
771
|
|
|
696
772
|
|
|
697
773
|
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
698
781
|
|
|
699
782
|
|
|
700
783
|
|
|
@@ -739,6 +822,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
739
822
|
|
|
740
823
|
|
|
741
824
|
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
742
832
|
|
|
743
833
|
|
|
744
834
|
|
|
@@ -756,16 +846,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
756
846
|
clear() {
|
|
757
847
|
this._elements = [];
|
|
758
848
|
}
|
|
759
|
-
/**
|
|
760
|
-
* Replace the backing array and rebuild the heap.
|
|
761
|
-
* @remarks Time O(N), Space O(N)
|
|
762
|
-
* @param elements - Iterable used to refill the heap.
|
|
763
|
-
* @returns Array of per-node results from fixing steps.
|
|
764
|
-
*/
|
|
765
|
-
refill(elements) {
|
|
766
|
-
this._elements = Array.from(elements);
|
|
767
|
-
return this.fix();
|
|
768
|
-
}
|
|
769
849
|
/**
|
|
770
850
|
* Check if an equal element exists in the heap.
|
|
771
851
|
* @remarks Time O(N), Space O(1)
|
|
@@ -786,6 +866,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
786
866
|
|
|
787
867
|
|
|
788
868
|
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
789
876
|
|
|
790
877
|
|
|
791
878
|
|
|
@@ -830,6 +917,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
830
917
|
|
|
831
918
|
|
|
832
919
|
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
833
927
|
|
|
834
928
|
|
|
835
929
|
|
|
@@ -854,7 +948,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
854
948
|
}
|
|
855
949
|
if (index < 0) return false;
|
|
856
950
|
if (index === 0) {
|
|
857
|
-
this.
|
|
951
|
+
this.pop();
|
|
858
952
|
} else if (index === this.elements.length - 1) {
|
|
859
953
|
this.elements.pop();
|
|
860
954
|
} else {
|
|
@@ -864,13 +958,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
864
958
|
}
|
|
865
959
|
return true;
|
|
866
960
|
}
|
|
961
|
+
/**
|
|
962
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
963
|
+
*/
|
|
964
|
+
deleteBy(predicate) {
|
|
965
|
+
return this.deleteWhere(predicate);
|
|
966
|
+
}
|
|
867
967
|
/**
|
|
868
968
|
* Delete the first element that matches a predicate.
|
|
869
969
|
* @remarks Time O(N), Space O(1)
|
|
870
970
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
871
971
|
* @returns True if an element was removed.
|
|
872
972
|
*/
|
|
873
|
-
|
|
973
|
+
deleteWhere(predicate) {
|
|
874
974
|
let idx = -1;
|
|
875
975
|
for (let i = 0; i < this.elements.length; i++) {
|
|
876
976
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -880,7 +980,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
880
980
|
}
|
|
881
981
|
if (idx < 0) return false;
|
|
882
982
|
if (idx === 0) {
|
|
883
|
-
this.
|
|
983
|
+
this.pop();
|
|
884
984
|
} else if (idx === this.elements.length - 1) {
|
|
885
985
|
this.elements.pop();
|
|
886
986
|
} else {
|
|
@@ -920,6 +1020,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
920
1020
|
|
|
921
1021
|
|
|
922
1022
|
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
923
1030
|
|
|
924
1031
|
|
|
925
1032
|
|
|
@@ -997,6 +1104,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
997
1104
|
|
|
998
1105
|
|
|
999
1106
|
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1000
1114
|
|
|
1001
1115
|
|
|
1002
1116
|
|
|
@@ -1047,6 +1161,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1047
1161
|
|
|
1048
1162
|
|
|
1049
1163
|
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1050
1171
|
|
|
1051
1172
|
|
|
1052
1173
|
|
|
@@ -1096,6 +1217,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1096
1217
|
|
|
1097
1218
|
|
|
1098
1219
|
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
|
|
1099
1227
|
|
|
1100
1228
|
|
|
1101
1229
|
|
|
@@ -1152,6 +1280,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1152
1280
|
|
|
1153
1281
|
|
|
1154
1282
|
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
|
|
1155
1290
|
|
|
1156
1291
|
|
|
1157
1292
|
|
|
@@ -1168,7 +1303,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1168
1303
|
*/
|
|
1169
1304
|
map(callback, options, thisArg) {
|
|
1170
1305
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
1171
|
-
if (!comparator)
|
|
1306
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1172
1307
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1173
1308
|
let i = 0;
|
|
1174
1309
|
for (const x of this) {
|
|
@@ -1195,7 +1330,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1195
1330
|
}
|
|
1196
1331
|
_DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
|
|
1197
1332
|
if (typeof a === "object" || typeof b === "object") {
|
|
1198
|
-
|
|
1333
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
1199
1334
|
}
|
|
1200
1335
|
if (a > b) return 1;
|
|
1201
1336
|
if (a < b) return -1;
|
|
@@ -1307,7 +1442,7 @@ var FibonacciHeap = class {
|
|
|
1307
1442
|
constructor(comparator) {
|
|
1308
1443
|
this.clear();
|
|
1309
1444
|
this._comparator = comparator || this._defaultComparator;
|
|
1310
|
-
if (typeof this.comparator !== "function")
|
|
1445
|
+
if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
1311
1446
|
}
|
|
1312
1447
|
_root;
|
|
1313
1448
|
/**
|
|
@@ -1348,7 +1483,7 @@ var FibonacciHeap = class {
|
|
|
1348
1483
|
* Push an element into the root list.
|
|
1349
1484
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1350
1485
|
* @param element - Element to insert.
|
|
1351
|
-
* @returns
|
|
1486
|
+
* @returns True when the element is added.
|
|
1352
1487
|
*/
|
|
1353
1488
|
push(element) {
|
|
1354
1489
|
const node = this.createNode(element);
|
|
@@ -1357,7 +1492,7 @@ var FibonacciHeap = class {
|
|
|
1357
1492
|
this.mergeWithRoot(node);
|
|
1358
1493
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1359
1494
|
this._size++;
|
|
1360
|
-
return
|
|
1495
|
+
return true;
|
|
1361
1496
|
}
|
|
1362
1497
|
peek() {
|
|
1363
1498
|
return this.min ? this.min.element : void 0;
|
|
@@ -1535,7 +1670,7 @@ var MaxPriorityQueue = class extends PriorityQueue {
|
|
|
1535
1670
|
super(elements, {
|
|
1536
1671
|
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
1537
1672
|
if (typeof a === "object" || typeof b === "object") {
|
|
1538
|
-
|
|
1673
|
+
raise(TypeError, ERR.comparatorRequired("MaxPriorityQueue"));
|
|
1539
1674
|
}
|
|
1540
1675
|
if (a < b) return 1;
|
|
1541
1676
|
if (a > b) return -1;
|
|
@@ -1568,5 +1703,6 @@ exports.Heap = Heap;
|
|
|
1568
1703
|
exports.MaxPriorityQueue = MaxPriorityQueue;
|
|
1569
1704
|
exports.PriorityQueue = PriorityQueue;
|
|
1570
1705
|
exports.Range = Range;
|
|
1706
|
+
exports.raise = raise;
|
|
1571
1707
|
//# sourceMappingURL=index.cjs.map
|
|
1572
1708
|
//# sourceMappingURL=index.cjs.map
|