data-structure-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/CHANGELOG.md +5 -1
- package/MIGRATION.md +169 -0
- package/README.md +135 -23
- package/README_CN.md +551 -143
- package/SPECIFICATION.md +20 -14
- package/SPECIFICATION.zh-CN.md +20 -14
- package/dist/cjs/binary-tree.cjs +6460 -1591
- package/dist/cjs/graph.cjs +440 -20
- package/dist/cjs/hash.cjs +125 -22
- package/dist/cjs/heap.cjs +196 -47
- package/dist/cjs/index.cjs +8486 -2429
- package/dist/cjs/linked-list.cjs +456 -31
- package/dist/cjs/matrix.cjs +79 -9
- package/dist/cjs/priority-queue.cjs +193 -44
- package/dist/cjs/queue.cjs +391 -2
- package/dist/cjs/stack.cjs +92 -6
- package/dist/cjs/trie.cjs +122 -28
- package/dist/cjs-legacy/binary-tree.cjs +6484 -1612
- package/dist/cjs-legacy/graph.cjs +440 -20
- package/dist/cjs-legacy/hash.cjs +125 -22
- package/dist/cjs-legacy/heap.cjs +196 -47
- package/dist/cjs-legacy/index.cjs +8654 -2594
- package/dist/cjs-legacy/linked-list.cjs +456 -31
- package/dist/cjs-legacy/matrix.cjs +79 -9
- package/dist/cjs-legacy/priority-queue.cjs +193 -44
- package/dist/cjs-legacy/queue.cjs +391 -2
- package/dist/cjs-legacy/stack.cjs +92 -6
- package/dist/cjs-legacy/trie.cjs +122 -28
- package/dist/esm/binary-tree.mjs +6460 -1591
- package/dist/esm/graph.mjs +440 -20
- package/dist/esm/hash.mjs +125 -22
- package/dist/esm/heap.mjs +196 -47
- package/dist/esm/index.mjs +8486 -2430
- package/dist/esm/linked-list.mjs +456 -31
- package/dist/esm/matrix.mjs +79 -9
- package/dist/esm/priority-queue.mjs +193 -44
- package/dist/esm/queue.mjs +391 -2
- package/dist/esm/stack.mjs +92 -6
- package/dist/esm/trie.mjs +122 -28
- package/dist/esm-legacy/binary-tree.mjs +6484 -1612
- package/dist/esm-legacy/graph.mjs +440 -20
- package/dist/esm-legacy/hash.mjs +125 -22
- package/dist/esm-legacy/heap.mjs +196 -47
- package/dist/esm-legacy/index.mjs +8654 -2595
- package/dist/esm-legacy/linked-list.mjs +456 -31
- package/dist/esm-legacy/matrix.mjs +79 -9
- package/dist/esm-legacy/priority-queue.mjs +193 -44
- package/dist/esm-legacy/queue.mjs +391 -2
- package/dist/esm-legacy/stack.mjs +92 -6
- package/dist/esm-legacy/trie.mjs +122 -28
- 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 +98 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +112 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +214 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +294 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +155 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +48 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1370 -323
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1329 -316
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +1116 -295
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1330 -326
- package/dist/types/data-structures/graph/directed-graph.d.ts +80 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +72 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +95 -6
- package/dist/types/data-structures/heap/heap.d.ts +154 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +143 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +144 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +64 -0
- package/dist/types/data-structures/queue/deque.d.ts +142 -0
- package/dist/types/data-structures/queue/queue.d.ts +109 -0
- package/dist/types/data-structures/stack/stack.d.ts +82 -2
- package/dist/types/data-structures/trie/trie.d.ts +96 -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/data-structure-typed.js +8623 -2564
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +696 -194
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +11 -11
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +71 -71
- package/docs-site-docusaurus/docs/api/classes/BST.md +639 -189
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +15 -15
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +148 -160
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +105 -91
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +104 -74
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +12 -12
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +51 -51
- package/docs-site-docusaurus/docs/api/classes/Heap.md +96 -85
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +25 -25
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +50 -50
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +55 -55
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +78 -78
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +31 -31
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +104 -89
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +103 -88
- package/docs-site-docusaurus/docs/api/classes/Queue.md +112 -60
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +708 -206
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +79 -79
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +44 -44
- package/docs-site-docusaurus/docs/api/classes/Stack.md +42 -42
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +236 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +162 -46
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +232 -32
- package/docs-site-docusaurus/docs/api/classes/Trie.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +81 -81
- package/docs-site-docusaurus/docs/guide/architecture.md +75 -5
- package/docs-site-docusaurus/docs/guide/concepts.md +53 -3
- package/docs-site-docusaurus/docs/guide/faq.md +233 -0
- package/docs-site-docusaurus/docs/guide/guides.md +43 -58
- package/docs-site-docusaurus/docs/guide/installation.md +2 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +75 -176
- package/docs-site-docusaurus/docs/guide/overview.md +132 -11
- package/docs-site-docusaurus/docs/guide/performance.md +2 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +31 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +1 -1
- package/docs-site-docusaurus/src/pages/index.tsx +55 -2
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/typedoc.json +1 -0
- package/llms.txt +37 -0
- package/package.json +65 -56
- 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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +0 -12984
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +0 -3
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +0 -4505
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +0 -9731
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +0 -347
|
@@ -6,6 +6,10 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
6
6
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
7
|
|
|
8
8
|
// src/common/error.ts
|
|
9
|
+
function raise(ErrorClass, message) {
|
|
10
|
+
throw new ErrorClass(message);
|
|
11
|
+
}
|
|
12
|
+
__name(raise, "raise");
|
|
9
13
|
var ERR = {
|
|
10
14
|
// Range / index
|
|
11
15
|
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
@@ -27,7 +31,9 @@ var ERR = {
|
|
|
27
31
|
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
28
32
|
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
29
33
|
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
30
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
34
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
35
|
+
// Order statistic
|
|
36
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
31
37
|
};
|
|
32
38
|
|
|
33
39
|
// src/data-structures/matrix/matrix.ts
|
|
@@ -138,6 +144,14 @@ var _Matrix = class _Matrix {
|
|
|
138
144
|
|
|
139
145
|
|
|
140
146
|
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
141
155
|
|
|
142
156
|
|
|
143
157
|
|
|
@@ -202,6 +216,14 @@ var _Matrix = class _Matrix {
|
|
|
202
216
|
|
|
203
217
|
|
|
204
218
|
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
205
227
|
|
|
206
228
|
|
|
207
229
|
|
|
@@ -262,6 +284,14 @@ var _Matrix = class _Matrix {
|
|
|
262
284
|
|
|
263
285
|
|
|
264
286
|
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
265
295
|
|
|
266
296
|
|
|
267
297
|
|
|
@@ -297,7 +327,7 @@ var _Matrix = class _Matrix {
|
|
|
297
327
|
add(matrix) {
|
|
298
328
|
var _a;
|
|
299
329
|
if (!this.isMatchForCalculate(matrix)) {
|
|
300
|
-
|
|
330
|
+
raise(Error, ERR.matrixDimensionMismatch("addition"));
|
|
301
331
|
}
|
|
302
332
|
const resultData = [];
|
|
303
333
|
for (let i = 0; i < this.rows; i++) {
|
|
@@ -345,6 +375,14 @@ var _Matrix = class _Matrix {
|
|
|
345
375
|
|
|
346
376
|
|
|
347
377
|
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
348
386
|
|
|
349
387
|
|
|
350
388
|
|
|
@@ -364,7 +402,7 @@ var _Matrix = class _Matrix {
|
|
|
364
402
|
subtract(matrix) {
|
|
365
403
|
var _a;
|
|
366
404
|
if (!this.isMatchForCalculate(matrix)) {
|
|
367
|
-
|
|
405
|
+
raise(Error, ERR.matrixDimensionMismatch("subtraction"));
|
|
368
406
|
}
|
|
369
407
|
const resultData = [];
|
|
370
408
|
for (let i = 0; i < this.rows; i++) {
|
|
@@ -411,6 +449,14 @@ var _Matrix = class _Matrix {
|
|
|
411
449
|
|
|
412
450
|
|
|
413
451
|
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
414
460
|
|
|
415
461
|
|
|
416
462
|
|
|
@@ -445,7 +491,7 @@ var _Matrix = class _Matrix {
|
|
|
445
491
|
*/
|
|
446
492
|
multiply(matrix) {
|
|
447
493
|
if (this.cols !== matrix.rows) {
|
|
448
|
-
|
|
494
|
+
raise(Error, ERR.matrixDimensionMismatch("multiplication (A.cols must equal B.rows)"));
|
|
449
495
|
}
|
|
450
496
|
const resultData = [];
|
|
451
497
|
for (let i = 0; i < this.rows; i++) {
|
|
@@ -498,6 +544,14 @@ var _Matrix = class _Matrix {
|
|
|
498
544
|
|
|
499
545
|
|
|
500
546
|
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
501
555
|
|
|
502
556
|
|
|
503
557
|
|
|
@@ -529,7 +583,7 @@ var _Matrix = class _Matrix {
|
|
|
529
583
|
*/
|
|
530
584
|
transpose() {
|
|
531
585
|
if (this.data.some((row) => row.length !== this.cols)) {
|
|
532
|
-
|
|
586
|
+
raise(Error, ERR.matrixNotRectangular());
|
|
533
587
|
}
|
|
534
588
|
const resultData = [];
|
|
535
589
|
for (let j = 0; j < this.cols; j++) {
|
|
@@ -572,6 +626,14 @@ var _Matrix = class _Matrix {
|
|
|
572
626
|
|
|
573
627
|
|
|
574
628
|
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
575
637
|
|
|
576
638
|
|
|
577
639
|
|
|
@@ -596,7 +658,7 @@ var _Matrix = class _Matrix {
|
|
|
596
658
|
inverse() {
|
|
597
659
|
var _a;
|
|
598
660
|
if (this.rows !== this.cols) {
|
|
599
|
-
|
|
661
|
+
raise(Error, ERR.matrixNotSquare());
|
|
600
662
|
}
|
|
601
663
|
const augmentedMatrixData = [];
|
|
602
664
|
for (let i = 0; i < this.rows; i++) {
|
|
@@ -618,12 +680,12 @@ var _Matrix = class _Matrix {
|
|
|
618
680
|
pivotRow++;
|
|
619
681
|
}
|
|
620
682
|
if (pivotRow === this.rows) {
|
|
621
|
-
|
|
683
|
+
raise(Error, ERR.matrixSingular());
|
|
622
684
|
}
|
|
623
685
|
augmentedMatrix._swapRows(i, pivotRow);
|
|
624
686
|
const pivotElement = (_a = augmentedMatrix.get(i, i)) != null ? _a : 1;
|
|
625
687
|
if (pivotElement === 0) {
|
|
626
|
-
|
|
688
|
+
raise(Error, ERR.matrixSingular());
|
|
627
689
|
}
|
|
628
690
|
augmentedMatrix._scaleRow(i, 1 / pivotElement);
|
|
629
691
|
for (let j = 0; j < this.rows; j++) {
|
|
@@ -672,6 +734,14 @@ var _Matrix = class _Matrix {
|
|
|
672
734
|
|
|
673
735
|
|
|
674
736
|
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
675
745
|
|
|
676
746
|
|
|
677
747
|
|
|
@@ -690,7 +760,7 @@ var _Matrix = class _Matrix {
|
|
|
690
760
|
*/
|
|
691
761
|
dot(matrix) {
|
|
692
762
|
if (this.cols !== matrix.rows) {
|
|
693
|
-
|
|
763
|
+
raise(Error, ERR.matrixDimensionMismatch("dot product (A.cols must equal B.rows)"));
|
|
694
764
|
}
|
|
695
765
|
const resultData = [];
|
|
696
766
|
for (let i = 0; i < this.rows; i++) {
|
|
@@ -5,6 +5,37 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
6
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
7
|
|
|
8
|
+
// src/common/error.ts
|
|
9
|
+
function raise(ErrorClass, message) {
|
|
10
|
+
throw new ErrorClass(message);
|
|
11
|
+
}
|
|
12
|
+
__name(raise, "raise");
|
|
13
|
+
var ERR = {
|
|
14
|
+
// Range / index
|
|
15
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
16
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
17
|
+
// Type / argument
|
|
18
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
19
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
20
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
21
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
22
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
23
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
24
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
25
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
26
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
27
|
+
// State / operation
|
|
28
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
29
|
+
// Matrix
|
|
30
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
31
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
32
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
33
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
34
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
35
|
+
// Order statistic
|
|
36
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
37
|
+
};
|
|
38
|
+
|
|
8
39
|
// src/data-structures/base/iterable-element-base.ts
|
|
9
40
|
var _IterableElementBase = class _IterableElementBase {
|
|
10
41
|
/**
|
|
@@ -27,7 +58,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
27
58
|
if (options) {
|
|
28
59
|
const { toElementFn } = options;
|
|
29
60
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
30
|
-
else if (toElementFn)
|
|
61
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
31
62
|
}
|
|
32
63
|
}
|
|
33
64
|
/**
|
|
@@ -183,7 +214,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
183
214
|
acc = initialValue;
|
|
184
215
|
} else {
|
|
185
216
|
const first = iter.next();
|
|
186
|
-
if (first.done)
|
|
217
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
187
218
|
acc = first.value;
|
|
188
219
|
index = 1;
|
|
189
220
|
}
|
|
@@ -227,31 +258,6 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
227
258
|
__name(_IterableElementBase, "IterableElementBase");
|
|
228
259
|
var IterableElementBase = _IterableElementBase;
|
|
229
260
|
|
|
230
|
-
// src/common/error.ts
|
|
231
|
-
var ERR = {
|
|
232
|
-
// Range / index
|
|
233
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
234
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
235
|
-
// Type / argument
|
|
236
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
237
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
238
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
239
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
240
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
241
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
242
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
243
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
244
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
245
|
-
// State / operation
|
|
246
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
247
|
-
// Matrix
|
|
248
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
249
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
250
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
251
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
252
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
253
|
-
};
|
|
254
|
-
|
|
255
261
|
// src/data-structures/heap/heap.ts
|
|
256
262
|
var _Heap = class _Heap extends IterableElementBase {
|
|
257
263
|
/**
|
|
@@ -267,7 +273,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
267
273
|
__publicField(this, "_elements", []);
|
|
268
274
|
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
269
275
|
if (typeof a === "object" || typeof b === "object") {
|
|
270
|
-
|
|
276
|
+
raise(TypeError, ERR.comparatorRequired("Heap"));
|
|
271
277
|
}
|
|
272
278
|
if (a > b) return 1;
|
|
273
279
|
if (a < b) return -1;
|
|
@@ -314,6 +320,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
314
320
|
|
|
315
321
|
|
|
316
322
|
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
317
331
|
|
|
318
332
|
|
|
319
333
|
|
|
@@ -372,7 +386,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
372
386
|
}
|
|
373
387
|
/**
|
|
374
388
|
* Insert an element.
|
|
375
|
-
* @remarks Time O(
|
|
389
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
376
390
|
* @param element - Element to insert.
|
|
377
391
|
* @returns True.
|
|
378
392
|
|
|
@@ -397,6 +411,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
397
411
|
|
|
398
412
|
|
|
399
413
|
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
400
422
|
|
|
401
423
|
|
|
402
424
|
|
|
@@ -450,6 +472,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
450
472
|
|
|
451
473
|
|
|
452
474
|
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
453
483
|
|
|
454
484
|
|
|
455
485
|
|
|
@@ -506,6 +536,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
506
536
|
|
|
507
537
|
|
|
508
538
|
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
509
546
|
|
|
510
547
|
|
|
511
548
|
|
|
@@ -514,6 +551,34 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
514
551
|
|
|
515
552
|
|
|
516
553
|
|
|
554
|
+
* @example
|
|
555
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
556
|
+
* interface Task {
|
|
557
|
+
* id: number;
|
|
558
|
+
* priority: number;
|
|
559
|
+
* name: string;
|
|
560
|
+
* }
|
|
561
|
+
*
|
|
562
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
563
|
+
* const tasks: Task[] = [
|
|
564
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
565
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
566
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
567
|
+
* ];
|
|
568
|
+
*
|
|
569
|
+
* const maxHeap = new Heap(tasks, {
|
|
570
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
571
|
+
* });
|
|
572
|
+
*
|
|
573
|
+
* console.log(maxHeap.size); // 3;
|
|
574
|
+
*
|
|
575
|
+
* // Peek returns highest priority task
|
|
576
|
+
* const topTask = maxHeap.peek();
|
|
577
|
+
* console.log(topTask?.priority); // 8;
|
|
578
|
+
* console.log(topTask?.name); // 'Alert';
|
|
579
|
+
*/
|
|
580
|
+
/**
|
|
581
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
517
582
|
* @example
|
|
518
583
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
519
584
|
* interface Task {
|
|
@@ -541,6 +606,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
541
606
|
* console.log(topTask?.name); // 'Alert';
|
|
542
607
|
*/
|
|
543
608
|
poll() {
|
|
609
|
+
return this.pop();
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
613
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
614
|
+
* @returns The removed top element, or undefined if empty.
|
|
615
|
+
*/
|
|
616
|
+
pop() {
|
|
544
617
|
if (this.elements.length === 0) return;
|
|
545
618
|
const value = this.elements[0];
|
|
546
619
|
const last = this.elements.pop();
|
|
@@ -576,6 +649,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
576
649
|
|
|
577
650
|
|
|
578
651
|
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
|
|
579
660
|
|
|
580
661
|
|
|
581
662
|
|
|
@@ -672,6 +753,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
672
753
|
|
|
673
754
|
|
|
674
755
|
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
675
764
|
|
|
676
765
|
|
|
677
766
|
|
|
@@ -715,6 +804,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
715
804
|
|
|
716
805
|
|
|
717
806
|
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
718
815
|
|
|
719
816
|
|
|
720
817
|
|
|
@@ -733,16 +830,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
733
830
|
clear() {
|
|
734
831
|
this._elements = [];
|
|
735
832
|
}
|
|
736
|
-
/**
|
|
737
|
-
* Replace the backing array and rebuild the heap.
|
|
738
|
-
* @remarks Time O(N), Space O(N)
|
|
739
|
-
* @param elements - Iterable used to refill the heap.
|
|
740
|
-
* @returns Array of per-node results from fixing steps.
|
|
741
|
-
*/
|
|
742
|
-
refill(elements) {
|
|
743
|
-
this._elements = Array.from(elements);
|
|
744
|
-
return this.fix();
|
|
745
|
-
}
|
|
746
833
|
/**
|
|
747
834
|
* Check if an equal element exists in the heap.
|
|
748
835
|
* @remarks Time O(N), Space O(1)
|
|
@@ -761,6 +848,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
761
848
|
|
|
762
849
|
|
|
763
850
|
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
764
859
|
|
|
765
860
|
|
|
766
861
|
|
|
@@ -804,6 +899,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
804
899
|
|
|
805
900
|
|
|
806
901
|
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
807
910
|
|
|
808
911
|
|
|
809
912
|
|
|
@@ -829,7 +932,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
829
932
|
}
|
|
830
933
|
if (index < 0) return false;
|
|
831
934
|
if (index === 0) {
|
|
832
|
-
this.
|
|
935
|
+
this.pop();
|
|
833
936
|
} else if (index === this.elements.length - 1) {
|
|
834
937
|
this.elements.pop();
|
|
835
938
|
} else {
|
|
@@ -839,13 +942,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
839
942
|
}
|
|
840
943
|
return true;
|
|
841
944
|
}
|
|
945
|
+
/**
|
|
946
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
947
|
+
*/
|
|
948
|
+
deleteBy(predicate) {
|
|
949
|
+
return this.deleteWhere(predicate);
|
|
950
|
+
}
|
|
842
951
|
/**
|
|
843
952
|
* Delete the first element that matches a predicate.
|
|
844
953
|
* @remarks Time O(N), Space O(1)
|
|
845
954
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
846
955
|
* @returns True if an element was removed.
|
|
847
956
|
*/
|
|
848
|
-
|
|
957
|
+
deleteWhere(predicate) {
|
|
849
958
|
let idx = -1;
|
|
850
959
|
for (let i = 0; i < this.elements.length; i++) {
|
|
851
960
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -855,7 +964,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
855
964
|
}
|
|
856
965
|
if (idx < 0) return false;
|
|
857
966
|
if (idx === 0) {
|
|
858
|
-
this.
|
|
967
|
+
this.pop();
|
|
859
968
|
} else if (idx === this.elements.length - 1) {
|
|
860
969
|
this.elements.pop();
|
|
861
970
|
} else {
|
|
@@ -893,6 +1002,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
893
1002
|
|
|
894
1003
|
|
|
895
1004
|
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
896
1013
|
|
|
897
1014
|
|
|
898
1015
|
|
|
@@ -969,6 +1086,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
969
1086
|
|
|
970
1087
|
|
|
971
1088
|
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
|
|
972
1097
|
|
|
973
1098
|
|
|
974
1099
|
|
|
@@ -1018,6 +1143,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1018
1143
|
|
|
1019
1144
|
|
|
1020
1145
|
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1021
1154
|
|
|
1022
1155
|
|
|
1023
1156
|
|
|
@@ -1066,6 +1199,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1066
1199
|
|
|
1067
1200
|
|
|
1068
1201
|
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1069
1210
|
|
|
1070
1211
|
|
|
1071
1212
|
|
|
@@ -1121,6 +1262,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1121
1262
|
|
|
1122
1263
|
|
|
1123
1264
|
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1124
1273
|
|
|
1125
1274
|
|
|
1126
1275
|
|
|
@@ -1138,7 +1287,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1138
1287
|
*/
|
|
1139
1288
|
map(callback, options, thisArg) {
|
|
1140
1289
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
1141
|
-
if (!comparator)
|
|
1290
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
1142
1291
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
1143
1292
|
let i = 0;
|
|
1144
1293
|
for (const x of this) {
|
|
@@ -1278,7 +1427,7 @@ var _MaxPriorityQueue = class _MaxPriorityQueue extends PriorityQueue {
|
|
|
1278
1427
|
super(elements, {
|
|
1279
1428
|
comparator: /* @__PURE__ */ __name((a, b) => {
|
|
1280
1429
|
if (typeof a === "object" || typeof b === "object") {
|
|
1281
|
-
|
|
1430
|
+
raise(TypeError, ERR.comparatorRequired("MaxPriorityQueue"));
|
|
1282
1431
|
}
|
|
1283
1432
|
if (a < b) return 1;
|
|
1284
1433
|
if (a > b) return -1;
|