min-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 +206 -70
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +205 -69
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +206 -71
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +205 -70
- 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 +203 -68
- 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
|
@@ -242,6 +242,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
242
242
|
|
|
243
243
|
|
|
244
244
|
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
245
252
|
|
|
246
253
|
|
|
247
254
|
|
|
@@ -293,6 +300,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
293
300
|
|
|
294
301
|
|
|
295
302
|
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
296
310
|
|
|
297
311
|
|
|
298
312
|
|
|
@@ -339,6 +353,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
339
353
|
|
|
340
354
|
|
|
341
355
|
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
342
363
|
|
|
343
364
|
|
|
344
365
|
|
|
@@ -381,6 +402,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
381
402
|
|
|
382
403
|
|
|
383
404
|
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
384
412
|
|
|
385
413
|
|
|
386
414
|
|
|
@@ -422,6 +450,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
422
450
|
|
|
423
451
|
|
|
424
452
|
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
425
460
|
|
|
426
461
|
|
|
427
462
|
|
|
@@ -466,6 +501,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
466
501
|
|
|
467
502
|
|
|
468
503
|
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
469
511
|
|
|
470
512
|
|
|
471
513
|
|
|
@@ -535,6 +577,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
535
577
|
|
|
536
578
|
|
|
537
579
|
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
538
587
|
|
|
539
588
|
|
|
540
589
|
|
|
@@ -587,6 +636,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
587
636
|
|
|
588
637
|
|
|
589
638
|
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
590
646
|
|
|
591
647
|
|
|
592
648
|
|
|
@@ -633,6 +689,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
633
689
|
|
|
634
690
|
|
|
635
691
|
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
636
699
|
|
|
637
700
|
|
|
638
701
|
|
|
@@ -679,6 +742,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
679
742
|
|
|
680
743
|
|
|
681
744
|
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
682
752
|
|
|
683
753
|
|
|
684
754
|
|
|
@@ -722,6 +792,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
722
792
|
|
|
723
793
|
|
|
724
794
|
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
725
802
|
|
|
726
803
|
|
|
727
804
|
|
|
@@ -760,6 +837,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
760
837
|
|
|
761
838
|
|
|
762
839
|
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
763
847
|
|
|
764
848
|
|
|
765
849
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import type {
|
|
2
|
+
import type { BinaryTreeOptions, BTNRep, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNodeOrNull, ReduceEntryCallback, ToEntryFn } from '../types';
|
|
3
3
|
/**
|
|
4
4
|
* Public, implementation-agnostic binary tree API.
|
|
5
5
|
* K = key, V = value, R = raw/record used with toEntryFn (optional).
|
|
@@ -19,7 +19,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
19
19
|
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
20
20
|
set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
21
21
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
22
|
-
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>):
|
|
22
|
+
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): boolean;
|
|
23
23
|
clear(): void;
|
|
24
24
|
isEmpty(): boolean;
|
|
25
25
|
get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
|
|
@@ -56,5 +56,4 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
56
56
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
|
|
57
57
|
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): IBinaryTree<MK, MV, MR>;
|
|
58
58
|
merge(anotherTree: IBinaryTree<K, V, R>): void;
|
|
59
|
-
refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void;
|
|
60
59
|
}
|
|
@@ -3,6 +3,7 @@ import type { Comparator, OptValue } from '../../common';
|
|
|
3
3
|
type BSTBaseOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'>;
|
|
4
4
|
export type BSTOptions<K, V, R> = BSTBaseOptions<K, V, R> & {
|
|
5
5
|
comparator?: Comparator<K>;
|
|
6
|
+
enableOrderStatistic?: boolean;
|
|
6
7
|
};
|
|
7
8
|
export type BSTNOptKey<K> = K | undefined;
|
|
8
9
|
export type OptNode<NODE> = NODE | undefined;
|
|
@@ -8,6 +8,11 @@ export interface TreeMapOptions<K, V, R = [K, V]> {
|
|
|
8
8
|
* - `false`: store values on tree nodes (Node Mode).
|
|
9
9
|
*/
|
|
10
10
|
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Enable order-statistic operations (select, rank, rangeByRank).
|
|
13
|
+
* When true, subtree counts are maintained on every node.
|
|
14
|
+
*/
|
|
15
|
+
enableOrderStatistic?: boolean;
|
|
11
16
|
/**
|
|
12
17
|
* Transform raw elements into `[key, value]` entries.
|
|
13
18
|
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<[K, V]>`.
|
|
@@ -8,6 +8,10 @@ export interface TreeMultiSetOptions<K, R = K> {
|
|
|
8
8
|
* - `false`: Node Mode.
|
|
9
9
|
*/
|
|
10
10
|
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Enable order-statistic operations (select, rank, rangeByRank).
|
|
13
|
+
*/
|
|
14
|
+
enableOrderStatistic?: boolean;
|
|
11
15
|
/**
|
|
12
16
|
* Transform raw elements into keys.
|
|
13
17
|
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
|
|
@@ -8,6 +8,10 @@ export interface TreeSetOptions<K, R = K> {
|
|
|
8
8
|
* - `false`: store values on tree nodes (Node Mode).
|
|
9
9
|
*/
|
|
10
10
|
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Enable order-statistic operations (select, rank, rangeByRank).
|
|
13
|
+
*/
|
|
14
|
+
enableOrderStatistic?: boolean;
|
|
11
15
|
/**
|
|
12
16
|
* Transform raw elements into keys.
|
|
13
17
|
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
|
|
@@ -30,9 +30,61 @@ var minPriorityQueueTyped = (() => {
|
|
|
30
30
|
Heap: () => Heap,
|
|
31
31
|
MinPriorityQueue: () => MinPriorityQueue,
|
|
32
32
|
PriorityQueue: () => PriorityQueue,
|
|
33
|
-
Range: () => Range
|
|
33
|
+
Range: () => Range,
|
|
34
|
+
raise: () => raise
|
|
34
35
|
});
|
|
35
36
|
|
|
37
|
+
// src/common/error.ts
|
|
38
|
+
function raise(ErrorClass, message) {
|
|
39
|
+
throw new ErrorClass(message);
|
|
40
|
+
}
|
|
41
|
+
var ERR = {
|
|
42
|
+
// Range / index
|
|
43
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
44
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
45
|
+
// Type / argument
|
|
46
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
47
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
48
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
49
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
50
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
51
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
52
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
53
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
54
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
55
|
+
// State / operation
|
|
56
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
57
|
+
// Matrix
|
|
58
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
59
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
60
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
61
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
62
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`,
|
|
63
|
+
// Order statistic
|
|
64
|
+
orderStatisticNotEnabled: (method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/common/index.ts
|
|
68
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
69
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
70
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
71
|
+
return DFSOperation2;
|
|
72
|
+
})(DFSOperation || {});
|
|
73
|
+
var Range = class {
|
|
74
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
75
|
+
this.low = low;
|
|
76
|
+
this.high = high;
|
|
77
|
+
this.includeLow = includeLow;
|
|
78
|
+
this.includeHigh = includeHigh;
|
|
79
|
+
}
|
|
80
|
+
// Determine whether a key is within the range
|
|
81
|
+
isInRange(key, comparator) {
|
|
82
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
83
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
84
|
+
return lowCheck && highCheck;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
36
88
|
// src/data-structures/base/iterable-element-base.ts
|
|
37
89
|
var IterableElementBase = class {
|
|
38
90
|
/**
|
|
@@ -55,7 +107,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
55
107
|
if (options) {
|
|
56
108
|
const { toElementFn } = options;
|
|
57
109
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
58
|
-
else if (toElementFn)
|
|
110
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
59
111
|
}
|
|
60
112
|
}
|
|
61
113
|
/**
|
|
@@ -211,7 +263,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
211
263
|
acc = initialValue;
|
|
212
264
|
} else {
|
|
213
265
|
const first = iter.next();
|
|
214
|
-
if (first.done)
|
|
266
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
215
267
|
acc = first.value;
|
|
216
268
|
index = 1;
|
|
217
269
|
}
|
|
@@ -253,52 +305,6 @@ var minPriorityQueueTyped = (() => {
|
|
|
253
305
|
}
|
|
254
306
|
};
|
|
255
307
|
|
|
256
|
-
// src/common/error.ts
|
|
257
|
-
var ERR = {
|
|
258
|
-
// Range / index
|
|
259
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
260
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
261
|
-
// Type / argument
|
|
262
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
263
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
264
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
265
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
266
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
267
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
268
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
269
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
270
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
271
|
-
// State / operation
|
|
272
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
273
|
-
// Matrix
|
|
274
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
275
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
276
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
277
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
278
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
// src/common/index.ts
|
|
282
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
283
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
284
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
285
|
-
return DFSOperation2;
|
|
286
|
-
})(DFSOperation || {});
|
|
287
|
-
var Range = class {
|
|
288
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
289
|
-
this.low = low;
|
|
290
|
-
this.high = high;
|
|
291
|
-
this.includeLow = includeLow;
|
|
292
|
-
this.includeHigh = includeHigh;
|
|
293
|
-
}
|
|
294
|
-
// Determine whether a key is within the range
|
|
295
|
-
isInRange(key, comparator) {
|
|
296
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
297
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
298
|
-
return lowCheck && highCheck;
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
|
|
302
308
|
// src/data-structures/heap/heap.ts
|
|
303
309
|
var Heap = class _Heap extends IterableElementBase {
|
|
304
310
|
/**
|
|
@@ -314,7 +320,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
314
320
|
__publicField(this, "_elements", []);
|
|
315
321
|
__publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
|
|
316
322
|
if (typeof a === "object" || typeof b === "object") {
|
|
317
|
-
|
|
323
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
318
324
|
}
|
|
319
325
|
if (a > b) return 1;
|
|
320
326
|
if (a < b) return -1;
|
|
@@ -363,6 +369,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
363
369
|
|
|
364
370
|
|
|
365
371
|
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
366
379
|
|
|
367
380
|
|
|
368
381
|
|
|
@@ -420,7 +433,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
420
433
|
}
|
|
421
434
|
/**
|
|
422
435
|
* Insert an element.
|
|
423
|
-
* @remarks Time O(
|
|
436
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
424
437
|
* @param element - Element to insert.
|
|
425
438
|
* @returns True.
|
|
426
439
|
|
|
@@ -447,6 +460,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
447
460
|
|
|
448
461
|
|
|
449
462
|
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
450
470
|
|
|
451
471
|
|
|
452
472
|
|
|
@@ -501,6 +521,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
501
521
|
|
|
502
522
|
|
|
503
523
|
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
504
531
|
|
|
505
532
|
|
|
506
533
|
|
|
@@ -558,6 +585,12 @@ var minPriorityQueueTyped = (() => {
|
|
|
558
585
|
|
|
559
586
|
|
|
560
587
|
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
561
594
|
|
|
562
595
|
|
|
563
596
|
|
|
@@ -565,6 +598,34 @@ var minPriorityQueueTyped = (() => {
|
|
|
565
598
|
|
|
566
599
|
|
|
567
600
|
|
|
601
|
+
* @example
|
|
602
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
603
|
+
* interface Task {
|
|
604
|
+
* id: number;
|
|
605
|
+
* priority: number;
|
|
606
|
+
* name: string;
|
|
607
|
+
* }
|
|
608
|
+
*
|
|
609
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
610
|
+
* const tasks: Task[] = [
|
|
611
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
612
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
613
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
614
|
+
* ];
|
|
615
|
+
*
|
|
616
|
+
* const maxHeap = new Heap(tasks, {
|
|
617
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
618
|
+
* });
|
|
619
|
+
*
|
|
620
|
+
* console.log(maxHeap.size); // 3;
|
|
621
|
+
*
|
|
622
|
+
* // Peek returns highest priority task
|
|
623
|
+
* const topTask = maxHeap.peek();
|
|
624
|
+
* console.log(topTask?.priority); // 8;
|
|
625
|
+
* console.log(topTask?.name); // 'Alert';
|
|
626
|
+
*/
|
|
627
|
+
/**
|
|
628
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
568
629
|
* @example
|
|
569
630
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
570
631
|
* interface Task {
|
|
@@ -592,6 +653,14 @@ var minPriorityQueueTyped = (() => {
|
|
|
592
653
|
* console.log(topTask?.name); // 'Alert';
|
|
593
654
|
*/
|
|
594
655
|
poll() {
|
|
656
|
+
return this.pop();
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
660
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
661
|
+
* @returns The removed top element, or undefined if empty.
|
|
662
|
+
*/
|
|
663
|
+
pop() {
|
|
595
664
|
if (this.elements.length === 0) return;
|
|
596
665
|
const value = this.elements[0];
|
|
597
666
|
const last = this.elements.pop();
|
|
@@ -629,6 +698,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
629
698
|
|
|
630
699
|
|
|
631
700
|
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
632
708
|
|
|
633
709
|
|
|
634
710
|
|
|
@@ -726,6 +802,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
726
802
|
|
|
727
803
|
|
|
728
804
|
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
729
812
|
|
|
730
813
|
|
|
731
814
|
|
|
@@ -770,6 +853,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
770
853
|
|
|
771
854
|
|
|
772
855
|
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
773
863
|
|
|
774
864
|
|
|
775
865
|
|
|
@@ -787,16 +877,6 @@ var minPriorityQueueTyped = (() => {
|
|
|
787
877
|
clear() {
|
|
788
878
|
this._elements = [];
|
|
789
879
|
}
|
|
790
|
-
/**
|
|
791
|
-
* Replace the backing array and rebuild the heap.
|
|
792
|
-
* @remarks Time O(N), Space O(N)
|
|
793
|
-
* @param elements - Iterable used to refill the heap.
|
|
794
|
-
* @returns Array of per-node results from fixing steps.
|
|
795
|
-
*/
|
|
796
|
-
refill(elements) {
|
|
797
|
-
this._elements = Array.from(elements);
|
|
798
|
-
return this.fix();
|
|
799
|
-
}
|
|
800
880
|
/**
|
|
801
881
|
* Check if an equal element exists in the heap.
|
|
802
882
|
* @remarks Time O(N), Space O(1)
|
|
@@ -817,6 +897,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
817
897
|
|
|
818
898
|
|
|
819
899
|
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
820
907
|
|
|
821
908
|
|
|
822
909
|
|
|
@@ -861,6 +948,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
861
948
|
|
|
862
949
|
|
|
863
950
|
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
864
958
|
|
|
865
959
|
|
|
866
960
|
|
|
@@ -885,7 +979,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
885
979
|
}
|
|
886
980
|
if (index < 0) return false;
|
|
887
981
|
if (index === 0) {
|
|
888
|
-
this.
|
|
982
|
+
this.pop();
|
|
889
983
|
} else if (index === this.elements.length - 1) {
|
|
890
984
|
this.elements.pop();
|
|
891
985
|
} else {
|
|
@@ -895,13 +989,19 @@ var minPriorityQueueTyped = (() => {
|
|
|
895
989
|
}
|
|
896
990
|
return true;
|
|
897
991
|
}
|
|
992
|
+
/**
|
|
993
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
994
|
+
*/
|
|
995
|
+
deleteBy(predicate) {
|
|
996
|
+
return this.deleteWhere(predicate);
|
|
997
|
+
}
|
|
898
998
|
/**
|
|
899
999
|
* Delete the first element that matches a predicate.
|
|
900
1000
|
* @remarks Time O(N), Space O(1)
|
|
901
1001
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
902
1002
|
* @returns True if an element was removed.
|
|
903
1003
|
*/
|
|
904
|
-
|
|
1004
|
+
deleteWhere(predicate) {
|
|
905
1005
|
let idx = -1;
|
|
906
1006
|
for (let i = 0; i < this.elements.length; i++) {
|
|
907
1007
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -911,7 +1011,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
911
1011
|
}
|
|
912
1012
|
if (idx < 0) return false;
|
|
913
1013
|
if (idx === 0) {
|
|
914
|
-
this.
|
|
1014
|
+
this.pop();
|
|
915
1015
|
} else if (idx === this.elements.length - 1) {
|
|
916
1016
|
this.elements.pop();
|
|
917
1017
|
} else {
|
|
@@ -951,6 +1051,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
951
1051
|
|
|
952
1052
|
|
|
953
1053
|
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
954
1061
|
|
|
955
1062
|
|
|
956
1063
|
|
|
@@ -1028,6 +1135,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
1028
1135
|
|
|
1029
1136
|
|
|
1030
1137
|
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1031
1145
|
|
|
1032
1146
|
|
|
1033
1147
|
|
|
@@ -1078,6 +1192,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
1078
1192
|
|
|
1079
1193
|
|
|
1080
1194
|
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1081
1202
|
|
|
1082
1203
|
|
|
1083
1204
|
|
|
@@ -1127,6 +1248,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
1127
1248
|
|
|
1128
1249
|
|
|
1129
1250
|
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1130
1258
|
|
|
1131
1259
|
|
|
1132
1260
|
|
|
@@ -1183,6 +1311,13 @@ var minPriorityQueueTyped = (() => {
|
|
|
1183
1311
|
|
|
1184
1312
|
|
|
1185
1313
|
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1186
1321
|
|
|
1187
1322
|
|
|
1188
1323
|
|
|
@@ -1199,7 +1334,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
1199
1334
|
*/
|
|
1200
1335
|
map(callback, options, thisArg) {
|
|
1201
1336
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
1202
|
-
if (!comparator)
|
|
1337
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1203
1338
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1204
1339
|
let i = 0;
|
|
1205
1340
|
for (const x of this) {
|
|
@@ -1327,7 +1462,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
1327
1462
|
__publicField(this, "_comparator");
|
|
1328
1463
|
this.clear();
|
|
1329
1464
|
this._comparator = comparator || this._defaultComparator;
|
|
1330
|
-
if (typeof this.comparator !== "function")
|
|
1465
|
+
if (typeof this.comparator !== "function") raise(TypeError, ERR.notAFunction("comparator", "FibonacciHeap"));
|
|
1331
1466
|
}
|
|
1332
1467
|
/**
|
|
1333
1468
|
* Get the circular root list head.
|
|
@@ -1364,7 +1499,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
1364
1499
|
* Push an element into the root list.
|
|
1365
1500
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1366
1501
|
* @param element - Element to insert.
|
|
1367
|
-
* @returns
|
|
1502
|
+
* @returns True when the element is added.
|
|
1368
1503
|
*/
|
|
1369
1504
|
push(element) {
|
|
1370
1505
|
const node = this.createNode(element);
|
|
@@ -1373,7 +1508,7 @@ var minPriorityQueueTyped = (() => {
|
|
|
1373
1508
|
this.mergeWithRoot(node);
|
|
1374
1509
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1375
1510
|
this._size++;
|
|
1376
|
-
return
|
|
1511
|
+
return true;
|
|
1377
1512
|
}
|
|
1378
1513
|
peek() {
|
|
1379
1514
|
return this.min ? this.min.element : void 0;
|