data-structure-typed 2.5.1 → 2.5.2
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 +3 -1
- package/README.md +75 -17
- package/dist/cjs/binary-tree.cjs +2723 -139
- package/dist/cjs/graph.cjs +192 -6
- package/dist/cjs/hash.cjs +63 -15
- package/dist/cjs/heap.cjs +93 -31
- package/dist/cjs/index.cjs +3514 -379
- package/dist/cjs/linked-list.cjs +237 -31
- package/dist/cjs/matrix.cjs +47 -9
- package/dist/cjs/priority-queue.cjs +92 -30
- package/dist/cjs/queue.cjs +176 -2
- package/dist/cjs/stack.cjs +48 -2
- package/dist/cjs/trie.cjs +78 -28
- package/dist/cjs-legacy/binary-tree.cjs +2725 -136
- package/dist/cjs-legacy/graph.cjs +192 -6
- package/dist/cjs-legacy/hash.cjs +63 -15
- package/dist/cjs-legacy/heap.cjs +93 -31
- package/dist/cjs-legacy/index.cjs +3389 -249
- package/dist/cjs-legacy/linked-list.cjs +237 -31
- package/dist/cjs-legacy/matrix.cjs +47 -9
- package/dist/cjs-legacy/priority-queue.cjs +92 -30
- package/dist/cjs-legacy/queue.cjs +176 -2
- package/dist/cjs-legacy/stack.cjs +48 -2
- package/dist/cjs-legacy/trie.cjs +78 -28
- package/dist/esm/binary-tree.mjs +2723 -139
- package/dist/esm/graph.mjs +192 -6
- package/dist/esm/hash.mjs +63 -15
- package/dist/esm/heap.mjs +93 -31
- package/dist/esm/index.mjs +3514 -380
- package/dist/esm/linked-list.mjs +237 -31
- package/dist/esm/matrix.mjs +47 -9
- package/dist/esm/priority-queue.mjs +92 -30
- package/dist/esm/queue.mjs +176 -2
- package/dist/esm/stack.mjs +48 -2
- package/dist/esm/trie.mjs +78 -28
- package/dist/esm-legacy/binary-tree.mjs +2725 -136
- package/dist/esm-legacy/graph.mjs +192 -6
- package/dist/esm-legacy/hash.mjs +63 -15
- package/dist/esm-legacy/heap.mjs +93 -31
- package/dist/esm-legacy/index.mjs +3389 -250
- package/dist/esm-legacy/linked-list.mjs +237 -31
- package/dist/esm-legacy/matrix.mjs +47 -9
- package/dist/esm-legacy/priority-queue.mjs +92 -30
- package/dist/esm-legacy/queue.mjs +176 -2
- package/dist/esm-legacy/stack.mjs +48 -2
- package/dist/esm-legacy/trie.mjs +78 -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 +48 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +56 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +102 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +195 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +76 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +24 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +528 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +531 -6
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +435 -6
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +505 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +40 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +36 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +44 -0
- package/dist/types/data-structures/heap/heap.d.ts +56 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +68 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +60 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +72 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +32 -0
- package/dist/types/data-structures/queue/deque.d.ts +60 -0
- package/dist/types/data-structures/queue/queue.d.ts +48 -0
- package/dist/types/data-structures/stack/stack.d.ts +40 -0
- package/dist/types/data-structures/trie/trie.d.ts +48 -0
- 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 +3404 -265
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +650 -136
- 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 +591 -129
- 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 +107 -107
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +82 -82
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +74 -74
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/Heap.md +45 -45
- 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 +49 -49
- 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 +45 -45
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +5 -5
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +44 -44
- package/docs-site-docusaurus/docs/api/classes/Queue.md +60 -60
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +660 -146
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -10
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +78 -78
- 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 +39 -39
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +165 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +162 -46
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +161 -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 +2 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +32 -1
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +40 -54
- package/docs-site-docusaurus/docs/guide/installation.md +2 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +2 -0
- package/docs-site-docusaurus/docs/guide/overview.md +7 -0
- 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 +51 -2
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/llms.txt +37 -0
- package/package.json +64 -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 +47 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
- package/src/data-structures/binary-tree/binary-tree.ts +79 -4
- package/src/data-structures/binary-tree/bst.ts +441 -6
- package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
- package/src/data-structures/binary-tree/segment-tree.ts +18 -0
- package/src/data-structures/binary-tree/tree-map.ts +434 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
- package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
- package/src/data-structures/binary-tree/tree-set.ts +410 -8
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/undirected-graph.ts +27 -0
- package/src/data-structures/hash/hash-map.ts +35 -4
- package/src/data-structures/heap/heap.ts +46 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
- package/src/data-structures/matrix/matrix.ts +33 -9
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +45 -0
- package/src/data-structures/queue/queue.ts +36 -0
- package/src/data-structures/stack/stack.ts +30 -0
- package/src/data-structures/trie/trie.ts +38 -2
- 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/binary-tree.cjs
CHANGED
|
@@ -59,6 +59,56 @@ function makeTrampoline(fn) {
|
|
|
59
59
|
}
|
|
60
60
|
__name(makeTrampoline, "makeTrampoline");
|
|
61
61
|
|
|
62
|
+
// src/common/error.ts
|
|
63
|
+
function raise(ErrorClass, message) {
|
|
64
|
+
throw new ErrorClass(message);
|
|
65
|
+
}
|
|
66
|
+
__name(raise, "raise");
|
|
67
|
+
var ERR = {
|
|
68
|
+
// Range / index
|
|
69
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
70
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
71
|
+
// Type / argument
|
|
72
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
73
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
74
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
75
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
76
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
77
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
78
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
79
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
80
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
81
|
+
// State / operation
|
|
82
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
83
|
+
// Matrix
|
|
84
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
85
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
86
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
87
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
88
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
89
|
+
// Order statistic
|
|
90
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/common/index.ts
|
|
94
|
+
var Range = class {
|
|
95
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
96
|
+
this.low = low;
|
|
97
|
+
this.high = high;
|
|
98
|
+
this.includeLow = includeLow;
|
|
99
|
+
this.includeHigh = includeHigh;
|
|
100
|
+
}
|
|
101
|
+
static {
|
|
102
|
+
__name(this, "Range");
|
|
103
|
+
}
|
|
104
|
+
// Determine whether a key is within the range
|
|
105
|
+
isInRange(key, comparator) {
|
|
106
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
107
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
108
|
+
return lowCheck && highCheck;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
62
112
|
// src/data-structures/base/iterable-element-base.ts
|
|
63
113
|
var IterableElementBase = class {
|
|
64
114
|
static {
|
|
@@ -77,7 +127,7 @@ var IterableElementBase = class {
|
|
|
77
127
|
if (options) {
|
|
78
128
|
const { toElementFn } = options;
|
|
79
129
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
80
|
-
else if (toElementFn)
|
|
130
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
81
131
|
}
|
|
82
132
|
}
|
|
83
133
|
/**
|
|
@@ -240,7 +290,7 @@ var IterableElementBase = class {
|
|
|
240
290
|
acc = initialValue;
|
|
241
291
|
} else {
|
|
242
292
|
const first = iter.next();
|
|
243
|
-
if (first.done)
|
|
293
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
244
294
|
acc = first.value;
|
|
245
295
|
index = 1;
|
|
246
296
|
}
|
|
@@ -475,50 +525,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
475
525
|
}
|
|
476
526
|
};
|
|
477
527
|
|
|
478
|
-
// src/common/error.ts
|
|
479
|
-
var ERR = {
|
|
480
|
-
// Range / index
|
|
481
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
482
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
483
|
-
// Type / argument
|
|
484
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
485
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
486
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
487
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
488
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
489
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
490
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
491
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
492
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
493
|
-
// State / operation
|
|
494
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
495
|
-
// Matrix
|
|
496
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
497
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
498
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
499
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
500
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
// src/common/index.ts
|
|
504
|
-
var Range = class {
|
|
505
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
506
|
-
this.low = low;
|
|
507
|
-
this.high = high;
|
|
508
|
-
this.includeLow = includeLow;
|
|
509
|
-
this.includeHigh = includeHigh;
|
|
510
|
-
}
|
|
511
|
-
static {
|
|
512
|
-
__name(this, "Range");
|
|
513
|
-
}
|
|
514
|
-
// Determine whether a key is within the range
|
|
515
|
-
isInRange(key, comparator) {
|
|
516
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
517
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
518
|
-
return lowCheck && highCheck;
|
|
519
|
-
}
|
|
520
|
-
};
|
|
521
|
-
|
|
522
528
|
// src/data-structures/base/iterable-entry-base.ts
|
|
523
529
|
var IterableEntryBase = class {
|
|
524
530
|
static {
|
|
@@ -787,6 +793,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
787
793
|
|
|
788
794
|
|
|
789
795
|
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
790
800
|
|
|
791
801
|
|
|
792
802
|
|
|
@@ -833,6 +843,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
833
843
|
|
|
834
844
|
|
|
835
845
|
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
836
850
|
|
|
837
851
|
|
|
838
852
|
|
|
@@ -895,6 +909,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
895
909
|
|
|
896
910
|
|
|
897
911
|
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
|
|
898
916
|
|
|
899
917
|
|
|
900
918
|
|
|
@@ -953,6 +971,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
953
971
|
|
|
954
972
|
|
|
955
973
|
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
956
978
|
|
|
957
979
|
|
|
958
980
|
|
|
@@ -1018,6 +1040,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1018
1040
|
|
|
1019
1041
|
|
|
1020
1042
|
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1021
1047
|
|
|
1022
1048
|
|
|
1023
1049
|
|
|
@@ -1073,6 +1099,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1073
1099
|
|
|
1074
1100
|
|
|
1075
1101
|
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1076
1106
|
|
|
1077
1107
|
|
|
1078
1108
|
|
|
@@ -1121,6 +1151,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1121
1151
|
|
|
1122
1152
|
|
|
1123
1153
|
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1124
1158
|
|
|
1125
1159
|
|
|
1126
1160
|
|
|
@@ -1210,6 +1244,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1210
1244
|
|
|
1211
1245
|
|
|
1212
1246
|
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1213
1251
|
|
|
1214
1252
|
|
|
1215
1253
|
|
|
@@ -1252,6 +1290,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1252
1290
|
|
|
1253
1291
|
|
|
1254
1292
|
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1255
1297
|
|
|
1256
1298
|
|
|
1257
1299
|
|
|
@@ -1317,6 +1359,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1317
1359
|
|
|
1318
1360
|
|
|
1319
1361
|
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1320
1366
|
|
|
1321
1367
|
|
|
1322
1368
|
|
|
@@ -1366,6 +1412,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1366
1412
|
|
|
1367
1413
|
|
|
1368
1414
|
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1369
1419
|
|
|
1370
1420
|
|
|
1371
1421
|
|
|
@@ -1419,6 +1469,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1419
1469
|
|
|
1420
1470
|
|
|
1421
1471
|
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
|
|
1475
|
+
|
|
1422
1476
|
|
|
1423
1477
|
|
|
1424
1478
|
|
|
@@ -1674,7 +1728,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1674
1728
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1675
1729
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1676
1730
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1677
|
-
else if (toEntryFn)
|
|
1731
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1678
1732
|
}
|
|
1679
1733
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1680
1734
|
}
|
|
@@ -1915,6 +1969,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1915
1969
|
|
|
1916
1970
|
|
|
1917
1971
|
|
|
1972
|
+
|
|
1973
|
+
|
|
1974
|
+
|
|
1975
|
+
|
|
1918
1976
|
|
|
1919
1977
|
|
|
1920
1978
|
|
|
@@ -1965,6 +2023,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1965
2023
|
|
|
1966
2024
|
|
|
1967
2025
|
|
|
2026
|
+
|
|
2027
|
+
|
|
2028
|
+
|
|
2029
|
+
|
|
1968
2030
|
|
|
1969
2031
|
|
|
1970
2032
|
|
|
@@ -2067,6 +2129,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2067
2129
|
|
|
2068
2130
|
|
|
2069
2131
|
|
|
2132
|
+
|
|
2133
|
+
|
|
2134
|
+
|
|
2135
|
+
|
|
2070
2136
|
|
|
2071
2137
|
|
|
2072
2138
|
|
|
@@ -2105,6 +2171,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2105
2171
|
|
|
2106
2172
|
|
|
2107
2173
|
|
|
2174
|
+
|
|
2175
|
+
|
|
2176
|
+
|
|
2177
|
+
|
|
2108
2178
|
|
|
2109
2179
|
|
|
2110
2180
|
|
|
@@ -2164,6 +2234,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2164
2234
|
|
|
2165
2235
|
|
|
2166
2236
|
|
|
2237
|
+
|
|
2238
|
+
|
|
2239
|
+
|
|
2240
|
+
|
|
2167
2241
|
|
|
2168
2242
|
|
|
2169
2243
|
|
|
@@ -2222,6 +2296,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2222
2296
|
|
|
2223
2297
|
|
|
2224
2298
|
|
|
2299
|
+
|
|
2300
|
+
|
|
2301
|
+
|
|
2302
|
+
|
|
2225
2303
|
|
|
2226
2304
|
|
|
2227
2305
|
|
|
@@ -2358,6 +2436,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2358
2436
|
|
|
2359
2437
|
|
|
2360
2438
|
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
2442
|
+
|
|
2361
2443
|
|
|
2362
2444
|
|
|
2363
2445
|
|
|
@@ -2412,6 +2494,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2412
2494
|
|
|
2413
2495
|
|
|
2414
2496
|
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2415
2501
|
|
|
2416
2502
|
|
|
2417
2503
|
|
|
@@ -2468,6 +2554,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2468
2554
|
|
|
2469
2555
|
|
|
2470
2556
|
|
|
2557
|
+
|
|
2558
|
+
|
|
2559
|
+
|
|
2560
|
+
|
|
2471
2561
|
|
|
2472
2562
|
|
|
2473
2563
|
|
|
@@ -2512,6 +2602,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2512
2602
|
|
|
2513
2603
|
|
|
2514
2604
|
|
|
2605
|
+
|
|
2606
|
+
|
|
2607
|
+
|
|
2608
|
+
|
|
2515
2609
|
|
|
2516
2610
|
|
|
2517
2611
|
|
|
@@ -2565,6 +2659,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2565
2659
|
|
|
2566
2660
|
|
|
2567
2661
|
|
|
2662
|
+
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2568
2666
|
|
|
2569
2667
|
|
|
2570
2668
|
|
|
@@ -2645,6 +2743,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2645
2743
|
|
|
2646
2744
|
|
|
2647
2745
|
|
|
2746
|
+
|
|
2747
|
+
|
|
2748
|
+
|
|
2749
|
+
|
|
2648
2750
|
|
|
2649
2751
|
|
|
2650
2752
|
|
|
@@ -2702,6 +2804,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2702
2804
|
|
|
2703
2805
|
|
|
2704
2806
|
|
|
2807
|
+
|
|
2808
|
+
|
|
2809
|
+
|
|
2810
|
+
|
|
2705
2811
|
|
|
2706
2812
|
|
|
2707
2813
|
|
|
@@ -3175,6 +3281,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3175
3281
|
|
|
3176
3282
|
|
|
3177
3283
|
|
|
3284
|
+
|
|
3285
|
+
|
|
3286
|
+
|
|
3287
|
+
|
|
3178
3288
|
|
|
3179
3289
|
|
|
3180
3290
|
|
|
@@ -3223,6 +3333,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3223
3333
|
|
|
3224
3334
|
|
|
3225
3335
|
|
|
3336
|
+
|
|
3337
|
+
|
|
3338
|
+
|
|
3339
|
+
|
|
3226
3340
|
|
|
3227
3341
|
|
|
3228
3342
|
|
|
@@ -3275,6 +3389,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3275
3389
|
|
|
3276
3390
|
|
|
3277
3391
|
|
|
3392
|
+
|
|
3393
|
+
|
|
3394
|
+
|
|
3395
|
+
|
|
3278
3396
|
|
|
3279
3397
|
|
|
3280
3398
|
|
|
@@ -3352,6 +3470,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3352
3470
|
|
|
3353
3471
|
|
|
3354
3472
|
|
|
3473
|
+
|
|
3474
|
+
|
|
3475
|
+
|
|
3476
|
+
|
|
3355
3477
|
|
|
3356
3478
|
|
|
3357
3479
|
|
|
@@ -3988,12 +4110,16 @@ var BST = class extends BinaryTree {
|
|
|
3988
4110
|
} else {
|
|
3989
4111
|
this._comparator = this._createDefaultComparator();
|
|
3990
4112
|
}
|
|
4113
|
+
if (options.enableOrderStatistic) {
|
|
4114
|
+
this._enableOrderStatistic = true;
|
|
4115
|
+
}
|
|
3991
4116
|
} else {
|
|
3992
4117
|
this._comparator = this._createDefaultComparator();
|
|
3993
4118
|
}
|
|
3994
4119
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
3995
4120
|
}
|
|
3996
4121
|
_root = void 0;
|
|
4122
|
+
_enableOrderStatistic = false;
|
|
3997
4123
|
/**
|
|
3998
4124
|
* Gets the root node of the tree.
|
|
3999
4125
|
* @remarks Time O(1)
|
|
@@ -4159,6 +4285,14 @@ var BST = class extends BinaryTree {
|
|
|
4159
4285
|
|
|
4160
4286
|
|
|
4161
4287
|
|
|
4288
|
+
|
|
4289
|
+
|
|
4290
|
+
|
|
4291
|
+
|
|
4292
|
+
|
|
4293
|
+
|
|
4294
|
+
|
|
4295
|
+
|
|
4162
4296
|
|
|
4163
4297
|
|
|
4164
4298
|
|
|
@@ -4322,6 +4456,84 @@ var BST = class extends BinaryTree {
|
|
|
4322
4456
|
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
4323
4457
|
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
4324
4458
|
}
|
|
4459
|
+
getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4460
|
+
if (!this._enableOrderStatistic) {
|
|
4461
|
+
raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
|
|
4462
|
+
}
|
|
4463
|
+
if (k < 0 || k >= this._size) return void 0;
|
|
4464
|
+
let actualCallback = void 0;
|
|
4465
|
+
let actualIterationType = this.iterationType;
|
|
4466
|
+
if (typeof callback === "string") {
|
|
4467
|
+
actualIterationType = callback;
|
|
4468
|
+
} else if (callback) {
|
|
4469
|
+
actualCallback = callback;
|
|
4470
|
+
if (iterationType) {
|
|
4471
|
+
actualIterationType = iterationType;
|
|
4472
|
+
}
|
|
4473
|
+
}
|
|
4474
|
+
const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
|
|
4475
|
+
if (!node) return void 0;
|
|
4476
|
+
return actualCallback ? actualCallback(node) : node.key;
|
|
4477
|
+
}
|
|
4478
|
+
getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
4479
|
+
if (!this._enableOrderStatistic) {
|
|
4480
|
+
raise(Error, ERR.orderStatisticNotEnabled("getRank"));
|
|
4481
|
+
}
|
|
4482
|
+
if (!this._root || this._size === 0) return -1;
|
|
4483
|
+
let actualIterationType = this.iterationType;
|
|
4484
|
+
if (iterationType) actualIterationType = iterationType;
|
|
4485
|
+
let key;
|
|
4486
|
+
if (typeof keyNodeEntryOrPredicate === "function") {
|
|
4487
|
+
const results = this.search(keyNodeEntryOrPredicate, true);
|
|
4488
|
+
if (results.length === 0 || results[0] === void 0) return -1;
|
|
4489
|
+
key = results[0];
|
|
4490
|
+
} else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
4491
|
+
return -1;
|
|
4492
|
+
} else if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
4493
|
+
key = keyNodeEntryOrPredicate.key;
|
|
4494
|
+
} else if (Array.isArray(keyNodeEntryOrPredicate)) {
|
|
4495
|
+
key = keyNodeEntryOrPredicate[0] ?? void 0;
|
|
4496
|
+
if (key === void 0 || key === null) return -1;
|
|
4497
|
+
} else {
|
|
4498
|
+
key = keyNodeEntryOrPredicate;
|
|
4499
|
+
}
|
|
4500
|
+
if (key === void 0) return -1;
|
|
4501
|
+
return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
|
|
4502
|
+
}
|
|
4503
|
+
rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4504
|
+
if (!this._enableOrderStatistic) {
|
|
4505
|
+
raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
|
|
4506
|
+
}
|
|
4507
|
+
if (this._size === 0) return [];
|
|
4508
|
+
const lo = Math.max(0, start);
|
|
4509
|
+
const hi = Math.min(this._size - 1, end);
|
|
4510
|
+
if (lo > hi) return [];
|
|
4511
|
+
let actualCallback = void 0;
|
|
4512
|
+
let actualIterationType = this.iterationType;
|
|
4513
|
+
if (typeof callback === "string") {
|
|
4514
|
+
actualIterationType = callback;
|
|
4515
|
+
} else if (callback) {
|
|
4516
|
+
actualCallback = callback;
|
|
4517
|
+
if (iterationType) {
|
|
4518
|
+
actualIterationType = iterationType;
|
|
4519
|
+
}
|
|
4520
|
+
}
|
|
4521
|
+
const results = [];
|
|
4522
|
+
const count = hi - lo + 1;
|
|
4523
|
+
const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
|
|
4524
|
+
if (!startNode) return [];
|
|
4525
|
+
let collected = 0;
|
|
4526
|
+
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
|
|
4527
|
+
let current = startNode;
|
|
4528
|
+
while (current && collected < count) {
|
|
4529
|
+
results.push(cb(current));
|
|
4530
|
+
collected++;
|
|
4531
|
+
if (collected < count) {
|
|
4532
|
+
current = this._next(current);
|
|
4533
|
+
}
|
|
4534
|
+
}
|
|
4535
|
+
return results;
|
|
4536
|
+
}
|
|
4325
4537
|
/**
|
|
4326
4538
|
* Adds a new node to the BST based on key comparison.
|
|
4327
4539
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -4414,20 +4626,33 @@ var BST = class extends BinaryTree {
|
|
|
4414
4626
|
|
|
4415
4627
|
|
|
4416
4628
|
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4629
|
+
|
|
4630
|
+
|
|
4631
|
+
|
|
4632
|
+
|
|
4633
|
+
|
|
4634
|
+
|
|
4635
|
+
|
|
4636
|
+
|
|
4637
|
+
|
|
4638
|
+
|
|
4639
|
+
|
|
4640
|
+
|
|
4641
|
+
* @example
|
|
4642
|
+
* // Set a key-value pair
|
|
4643
|
+
* const bst = new BST<number, string>();
|
|
4644
|
+
* bst.set(1, 'one');
|
|
4645
|
+
* bst.set(2, 'two');
|
|
4646
|
+
* console.log(bst.get(1)); // 'one';
|
|
4647
|
+
*/
|
|
4648
|
+
set(keyNodeOrEntry, value) {
|
|
4649
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
4650
|
+
if (newNode === void 0) return false;
|
|
4427
4651
|
if (this._root === void 0) {
|
|
4428
4652
|
this._setRoot(newNode);
|
|
4429
4653
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4430
4654
|
this._size++;
|
|
4655
|
+
this._updateCount(newNode);
|
|
4431
4656
|
return true;
|
|
4432
4657
|
}
|
|
4433
4658
|
let current = this._root;
|
|
@@ -4441,6 +4666,7 @@ var BST = class extends BinaryTree {
|
|
|
4441
4666
|
current.left = newNode;
|
|
4442
4667
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4443
4668
|
this._size++;
|
|
4669
|
+
this._updateCountAlongPath(newNode);
|
|
4444
4670
|
return true;
|
|
4445
4671
|
}
|
|
4446
4672
|
if (current.left !== null) current = current.left;
|
|
@@ -4449,6 +4675,7 @@ var BST = class extends BinaryTree {
|
|
|
4449
4675
|
current.right = newNode;
|
|
4450
4676
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4451
4677
|
this._size++;
|
|
4678
|
+
this._updateCountAlongPath(newNode);
|
|
4452
4679
|
return true;
|
|
4453
4680
|
}
|
|
4454
4681
|
if (current.right !== null) current = current.right;
|
|
@@ -4509,6 +4736,14 @@ var BST = class extends BinaryTree {
|
|
|
4509
4736
|
|
|
4510
4737
|
|
|
4511
4738
|
|
|
4739
|
+
|
|
4740
|
+
|
|
4741
|
+
|
|
4742
|
+
|
|
4743
|
+
|
|
4744
|
+
|
|
4745
|
+
|
|
4746
|
+
|
|
4512
4747
|
|
|
4513
4748
|
|
|
4514
4749
|
|
|
@@ -4796,6 +5031,10 @@ var BST = class extends BinaryTree {
|
|
|
4796
5031
|
|
|
4797
5032
|
|
|
4798
5033
|
|
|
5034
|
+
|
|
5035
|
+
|
|
5036
|
+
|
|
5037
|
+
|
|
4799
5038
|
|
|
4800
5039
|
|
|
4801
5040
|
|
|
@@ -4861,6 +5100,10 @@ var BST = class extends BinaryTree {
|
|
|
4861
5100
|
|
|
4862
5101
|
|
|
4863
5102
|
|
|
5103
|
+
|
|
5104
|
+
|
|
5105
|
+
|
|
5106
|
+
|
|
4864
5107
|
|
|
4865
5108
|
|
|
4866
5109
|
|
|
@@ -4971,6 +5214,14 @@ var BST = class extends BinaryTree {
|
|
|
4971
5214
|
|
|
4972
5215
|
|
|
4973
5216
|
|
|
5217
|
+
|
|
5218
|
+
|
|
5219
|
+
|
|
5220
|
+
|
|
5221
|
+
|
|
5222
|
+
|
|
5223
|
+
|
|
5224
|
+
|
|
4974
5225
|
|
|
4975
5226
|
|
|
4976
5227
|
|
|
@@ -5054,13 +5305,11 @@ var BST = class extends BinaryTree {
|
|
|
5054
5305
|
if (a instanceof Date && b instanceof Date) {
|
|
5055
5306
|
const ta = a.getTime();
|
|
5056
5307
|
const tb = b.getTime();
|
|
5057
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
5308
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
|
|
5058
5309
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
5059
5310
|
}
|
|
5060
5311
|
if (typeof a === "object" || typeof b === "object") {
|
|
5061
|
-
|
|
5062
|
-
ERR.comparatorRequired("BST")
|
|
5063
|
-
);
|
|
5312
|
+
raise(TypeError, ERR.comparatorRequired("BST"));
|
|
5064
5313
|
}
|
|
5065
5314
|
return 0;
|
|
5066
5315
|
};
|
|
@@ -5379,7 +5628,8 @@ var BST = class extends BinaryTree {
|
|
|
5379
5628
|
_snapshotOptions() {
|
|
5380
5629
|
return {
|
|
5381
5630
|
...super._snapshotOptions(),
|
|
5382
|
-
comparator: this._comparator
|
|
5631
|
+
comparator: this._comparator,
|
|
5632
|
+
enableOrderStatistic: this._enableOrderStatistic
|
|
5383
5633
|
};
|
|
5384
5634
|
}
|
|
5385
5635
|
/**
|
|
@@ -5401,6 +5651,113 @@ var BST = class extends BinaryTree {
|
|
|
5401
5651
|
*
|
|
5402
5652
|
* @param v - The node to set as root.
|
|
5403
5653
|
*/
|
|
5654
|
+
/**
|
|
5655
|
+
* (Protected) Recalculates the subtree count for a single node.
|
|
5656
|
+
* @remarks Time O(1). Only active when enableOrderStatistic is true.
|
|
5657
|
+
*/
|
|
5658
|
+
_updateCount(node) {
|
|
5659
|
+
if (!this._enableOrderStatistic) return;
|
|
5660
|
+
node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
5661
|
+
}
|
|
5662
|
+
/**
|
|
5663
|
+
* (Protected) Updates subtree counts from a node up to the root.
|
|
5664
|
+
* @remarks Time O(log n). Only active when enableOrderStatistic is true.
|
|
5665
|
+
*/
|
|
5666
|
+
_updateCountAlongPath(node) {
|
|
5667
|
+
if (!this._enableOrderStatistic) return;
|
|
5668
|
+
let current = node;
|
|
5669
|
+
while (current) {
|
|
5670
|
+
this._updateCount(current);
|
|
5671
|
+
current = current.parent;
|
|
5672
|
+
}
|
|
5673
|
+
}
|
|
5674
|
+
/**
|
|
5675
|
+
* (Protected) Finds the node at position k in tree order (iterative).
|
|
5676
|
+
* @remarks Time O(log n), Space O(1)
|
|
5677
|
+
*/
|
|
5678
|
+
_getByRankIterative(node, k) {
|
|
5679
|
+
let current = node;
|
|
5680
|
+
let remaining = k;
|
|
5681
|
+
while (current) {
|
|
5682
|
+
const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
|
|
5683
|
+
if (remaining < leftCount) {
|
|
5684
|
+
current = current.left;
|
|
5685
|
+
} else if (remaining === leftCount) {
|
|
5686
|
+
return current;
|
|
5687
|
+
} else {
|
|
5688
|
+
remaining = remaining - leftCount - 1;
|
|
5689
|
+
current = current.right;
|
|
5690
|
+
}
|
|
5691
|
+
}
|
|
5692
|
+
return void 0;
|
|
5693
|
+
}
|
|
5694
|
+
/**
|
|
5695
|
+
* (Protected) Finds the node at position k in tree order (recursive).
|
|
5696
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5697
|
+
*/
|
|
5698
|
+
_getByRankRecursive(node, k) {
|
|
5699
|
+
if (!node) return void 0;
|
|
5700
|
+
const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
|
|
5701
|
+
if (k < leftCount) return this._getByRankRecursive(node.left, k);
|
|
5702
|
+
if (k === leftCount) return node;
|
|
5703
|
+
return this._getByRankRecursive(node.right, k - leftCount - 1);
|
|
5704
|
+
}
|
|
5705
|
+
/**
|
|
5706
|
+
* (Protected) Computes the rank of a key iteratively.
|
|
5707
|
+
* @remarks Time O(log n), Space O(1)
|
|
5708
|
+
*/
|
|
5709
|
+
_getRankIterative(node, key) {
|
|
5710
|
+
let rank = 0;
|
|
5711
|
+
let current = node;
|
|
5712
|
+
while (this.isRealNode(current)) {
|
|
5713
|
+
const cmp = this._compare(current.key, key);
|
|
5714
|
+
if (cmp > 0) {
|
|
5715
|
+
current = current.left;
|
|
5716
|
+
} else if (cmp < 0) {
|
|
5717
|
+
rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
|
|
5718
|
+
current = current.right;
|
|
5719
|
+
} else {
|
|
5720
|
+
rank += this.isRealNode(current.left) ? current.left._count : 0;
|
|
5721
|
+
return rank;
|
|
5722
|
+
}
|
|
5723
|
+
}
|
|
5724
|
+
return rank;
|
|
5725
|
+
}
|
|
5726
|
+
/**
|
|
5727
|
+
* (Protected) Computes the rank of a key recursively.
|
|
5728
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5729
|
+
*/
|
|
5730
|
+
_getRankRecursive(node, key) {
|
|
5731
|
+
if (!node) return 0;
|
|
5732
|
+
const cmp = this._compare(node.key, key);
|
|
5733
|
+
if (cmp > 0) {
|
|
5734
|
+
return this._getRankRecursive(node.left, key);
|
|
5735
|
+
} else if (cmp < 0) {
|
|
5736
|
+
return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
|
|
5737
|
+
} else {
|
|
5738
|
+
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
5739
|
+
}
|
|
5740
|
+
}
|
|
5741
|
+
/**
|
|
5742
|
+
* (Protected) Finds the in-order successor of a node.
|
|
5743
|
+
* @remarks Time O(log n), Space O(1)
|
|
5744
|
+
*/
|
|
5745
|
+
_next(node) {
|
|
5746
|
+
if (this.isRealNode(node.right)) {
|
|
5747
|
+
let current2 = node.right;
|
|
5748
|
+
while (this.isRealNode(current2.left)) {
|
|
5749
|
+
current2 = current2.left;
|
|
5750
|
+
}
|
|
5751
|
+
return current2;
|
|
5752
|
+
}
|
|
5753
|
+
let current = node;
|
|
5754
|
+
let parent = current.parent;
|
|
5755
|
+
while (parent && current === parent.right) {
|
|
5756
|
+
current = parent;
|
|
5757
|
+
parent = parent.parent;
|
|
5758
|
+
}
|
|
5759
|
+
return parent;
|
|
5760
|
+
}
|
|
5404
5761
|
_setRoot(v) {
|
|
5405
5762
|
if (v) v.parent = void 0;
|
|
5406
5763
|
this._root = v;
|
|
@@ -5447,21 +5804,28 @@ var BST = class extends BinaryTree {
|
|
|
5447
5804
|
while (x.left !== void 0 && x.left !== null) x = x.left;
|
|
5448
5805
|
return x;
|
|
5449
5806
|
}, "minNode");
|
|
5807
|
+
let countUpdateStart;
|
|
5450
5808
|
if (node.left === void 0) {
|
|
5809
|
+
countUpdateStart = node.parent;
|
|
5451
5810
|
transplant(node, node.right);
|
|
5452
5811
|
} else if (node.right === void 0) {
|
|
5812
|
+
countUpdateStart = node.parent;
|
|
5453
5813
|
transplant(node, node.left);
|
|
5454
5814
|
} else {
|
|
5455
5815
|
const succ = minNode(node.right);
|
|
5456
5816
|
if (succ.parent !== node) {
|
|
5817
|
+
countUpdateStart = succ.parent;
|
|
5457
5818
|
transplant(succ, succ.right);
|
|
5458
5819
|
succ.right = node.right;
|
|
5459
5820
|
if (succ.right) succ.right.parent = succ;
|
|
5821
|
+
} else {
|
|
5822
|
+
countUpdateStart = succ;
|
|
5460
5823
|
}
|
|
5461
5824
|
transplant(node, succ);
|
|
5462
5825
|
succ.left = node.left;
|
|
5463
5826
|
if (succ.left) succ.left.parent = succ;
|
|
5464
5827
|
}
|
|
5828
|
+
this._updateCountAlongPath(countUpdateStart);
|
|
5465
5829
|
this._size = Math.max(0, this._size - 1);
|
|
5466
5830
|
return true;
|
|
5467
5831
|
}
|
|
@@ -5488,7 +5852,7 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5488
5852
|
}
|
|
5489
5853
|
} else {
|
|
5490
5854
|
if (!Number.isInteger(sizeOrElements) || sizeOrElements < 0) {
|
|
5491
|
-
|
|
5855
|
+
raise(RangeError, ERR.invalidArgument("size must be a non-negative integer", "BinaryIndexedTree"));
|
|
5492
5856
|
}
|
|
5493
5857
|
this._size = sizeOrElements;
|
|
5494
5858
|
this._tree = new Array(this._size + 1).fill(0);
|
|
@@ -5543,6 +5907,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5543
5907
|
|
|
5544
5908
|
|
|
5545
5909
|
|
|
5910
|
+
|
|
5911
|
+
|
|
5912
|
+
|
|
5913
|
+
|
|
5914
|
+
|
|
5915
|
+
|
|
5916
|
+
|
|
5917
|
+
|
|
5546
5918
|
|
|
5547
5919
|
|
|
5548
5920
|
|
|
@@ -5611,6 +5983,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5611
5983
|
|
|
5612
5984
|
|
|
5613
5985
|
|
|
5986
|
+
|
|
5987
|
+
|
|
5988
|
+
|
|
5989
|
+
|
|
5990
|
+
|
|
5991
|
+
|
|
5992
|
+
|
|
5993
|
+
|
|
5614
5994
|
|
|
5615
5995
|
|
|
5616
5996
|
|
|
@@ -5679,6 +6059,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5679
6059
|
|
|
5680
6060
|
|
|
5681
6061
|
|
|
6062
|
+
|
|
6063
|
+
|
|
6064
|
+
|
|
6065
|
+
|
|
6066
|
+
|
|
6067
|
+
|
|
6068
|
+
|
|
6069
|
+
|
|
5682
6070
|
|
|
5683
6071
|
|
|
5684
6072
|
|
|
@@ -5747,6 +6135,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5747
6135
|
|
|
5748
6136
|
|
|
5749
6137
|
|
|
6138
|
+
|
|
6139
|
+
|
|
6140
|
+
|
|
6141
|
+
|
|
6142
|
+
|
|
6143
|
+
|
|
6144
|
+
|
|
6145
|
+
|
|
5750
6146
|
|
|
5751
6147
|
|
|
5752
6148
|
|
|
@@ -5813,6 +6209,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5813
6209
|
|
|
5814
6210
|
|
|
5815
6211
|
|
|
6212
|
+
|
|
6213
|
+
|
|
6214
|
+
|
|
6215
|
+
|
|
6216
|
+
|
|
6217
|
+
|
|
6218
|
+
|
|
6219
|
+
|
|
5816
6220
|
|
|
5817
6221
|
|
|
5818
6222
|
|
|
@@ -5886,6 +6290,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5886
6290
|
|
|
5887
6291
|
|
|
5888
6292
|
|
|
6293
|
+
|
|
6294
|
+
|
|
6295
|
+
|
|
6296
|
+
|
|
6297
|
+
|
|
6298
|
+
|
|
6299
|
+
|
|
6300
|
+
|
|
5889
6301
|
|
|
5890
6302
|
|
|
5891
6303
|
|
|
@@ -5936,6 +6348,10 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5936
6348
|
|
|
5937
6349
|
|
|
5938
6350
|
|
|
6351
|
+
|
|
6352
|
+
|
|
6353
|
+
|
|
6354
|
+
|
|
5939
6355
|
|
|
5940
6356
|
|
|
5941
6357
|
|
|
@@ -5993,6 +6409,10 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5993
6409
|
|
|
5994
6410
|
|
|
5995
6411
|
|
|
6412
|
+
|
|
6413
|
+
|
|
6414
|
+
|
|
6415
|
+
|
|
5996
6416
|
|
|
5997
6417
|
|
|
5998
6418
|
|
|
@@ -6067,10 +6487,10 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
6067
6487
|
}
|
|
6068
6488
|
_checkIndex(index) {
|
|
6069
6489
|
if (!Number.isInteger(index)) {
|
|
6070
|
-
|
|
6490
|
+
raise(TypeError, ERR.invalidIndex("BinaryIndexedTree"));
|
|
6071
6491
|
}
|
|
6072
6492
|
if (index < 0 || index >= this._size) {
|
|
6073
|
-
|
|
6493
|
+
raise(RangeError, ERR.indexOutOfRange(index, 0, this._size - 1, "BinaryIndexedTree"));
|
|
6074
6494
|
}
|
|
6075
6495
|
}
|
|
6076
6496
|
/** Returns highest power of 2 <= n. */
|
|
@@ -6153,6 +6573,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6153
6573
|
|
|
6154
6574
|
|
|
6155
6575
|
|
|
6576
|
+
|
|
6577
|
+
|
|
6578
|
+
|
|
6579
|
+
|
|
6156
6580
|
|
|
6157
6581
|
|
|
6158
6582
|
|
|
@@ -6221,6 +6645,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6221
6645
|
|
|
6222
6646
|
|
|
6223
6647
|
|
|
6648
|
+
|
|
6649
|
+
|
|
6650
|
+
|
|
6651
|
+
|
|
6224
6652
|
|
|
6225
6653
|
|
|
6226
6654
|
|
|
@@ -6283,6 +6711,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6283
6711
|
|
|
6284
6712
|
|
|
6285
6713
|
|
|
6714
|
+
|
|
6715
|
+
|
|
6716
|
+
|
|
6717
|
+
|
|
6286
6718
|
|
|
6287
6719
|
|
|
6288
6720
|
|
|
@@ -6352,6 +6784,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6352
6784
|
|
|
6353
6785
|
|
|
6354
6786
|
|
|
6787
|
+
|
|
6788
|
+
|
|
6789
|
+
|
|
6790
|
+
|
|
6355
6791
|
|
|
6356
6792
|
|
|
6357
6793
|
|
|
@@ -6399,6 +6835,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6399
6835
|
|
|
6400
6836
|
|
|
6401
6837
|
|
|
6838
|
+
|
|
6839
|
+
|
|
6840
|
+
|
|
6841
|
+
|
|
6402
6842
|
|
|
6403
6843
|
|
|
6404
6844
|
|
|
@@ -6472,6 +6912,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6472
6912
|
|
|
6473
6913
|
|
|
6474
6914
|
|
|
6915
|
+
|
|
6916
|
+
|
|
6917
|
+
|
|
6918
|
+
|
|
6475
6919
|
|
|
6476
6920
|
|
|
6477
6921
|
|
|
@@ -6841,6 +7285,22 @@ var AVLTree = class extends BST {
|
|
|
6841
7285
|
|
|
6842
7286
|
|
|
6843
7287
|
|
|
7288
|
+
|
|
7289
|
+
|
|
7290
|
+
|
|
7291
|
+
|
|
7292
|
+
|
|
7293
|
+
|
|
7294
|
+
|
|
7295
|
+
|
|
7296
|
+
|
|
7297
|
+
|
|
7298
|
+
|
|
7299
|
+
|
|
7300
|
+
|
|
7301
|
+
|
|
7302
|
+
|
|
7303
|
+
|
|
6844
7304
|
|
|
6845
7305
|
|
|
6846
7306
|
|
|
@@ -6957,6 +7417,18 @@ var AVLTree = class extends BST {
|
|
|
6957
7417
|
|
|
6958
7418
|
|
|
6959
7419
|
|
|
7420
|
+
|
|
7421
|
+
|
|
7422
|
+
|
|
7423
|
+
|
|
7424
|
+
|
|
7425
|
+
|
|
7426
|
+
|
|
7427
|
+
|
|
7428
|
+
|
|
7429
|
+
|
|
7430
|
+
|
|
7431
|
+
|
|
6960
7432
|
|
|
6961
7433
|
|
|
6962
7434
|
|
|
@@ -7036,6 +7508,14 @@ var AVLTree = class extends BST {
|
|
|
7036
7508
|
|
|
7037
7509
|
|
|
7038
7510
|
|
|
7511
|
+
|
|
7512
|
+
|
|
7513
|
+
|
|
7514
|
+
|
|
7515
|
+
|
|
7516
|
+
|
|
7517
|
+
|
|
7518
|
+
|
|
7039
7519
|
|
|
7040
7520
|
|
|
7041
7521
|
|
|
@@ -7156,6 +7636,18 @@ var AVLTree = class extends BST {
|
|
|
7156
7636
|
|
|
7157
7637
|
|
|
7158
7638
|
|
|
7639
|
+
|
|
7640
|
+
|
|
7641
|
+
|
|
7642
|
+
|
|
7643
|
+
|
|
7644
|
+
|
|
7645
|
+
|
|
7646
|
+
|
|
7647
|
+
|
|
7648
|
+
|
|
7649
|
+
|
|
7650
|
+
|
|
7159
7651
|
|
|
7160
7652
|
|
|
7161
7653
|
|
|
@@ -7287,6 +7779,8 @@ var AVLTree = class extends BST {
|
|
|
7287
7779
|
}
|
|
7288
7780
|
this._updateHeight(A);
|
|
7289
7781
|
if (B) this._updateHeight(B);
|
|
7782
|
+
this._updateCount(A);
|
|
7783
|
+
if (B) this._updateCount(B);
|
|
7290
7784
|
}
|
|
7291
7785
|
/**
|
|
7292
7786
|
* (Protected) Performs a Left-Right (LR) double rotation.
|
|
@@ -7332,6 +7826,9 @@ var AVLTree = class extends BST {
|
|
|
7332
7826
|
this._updateHeight(A);
|
|
7333
7827
|
if (B) this._updateHeight(B);
|
|
7334
7828
|
if (C) this._updateHeight(C);
|
|
7829
|
+
this._updateCount(A);
|
|
7830
|
+
if (B) this._updateCount(B);
|
|
7831
|
+
if (C) this._updateCount(C);
|
|
7335
7832
|
}
|
|
7336
7833
|
/**
|
|
7337
7834
|
* (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
|
|
@@ -7366,6 +7863,8 @@ var AVLTree = class extends BST {
|
|
|
7366
7863
|
}
|
|
7367
7864
|
this._updateHeight(A);
|
|
7368
7865
|
if (B) this._updateHeight(B);
|
|
7866
|
+
this._updateCount(A);
|
|
7867
|
+
if (B) this._updateCount(B);
|
|
7369
7868
|
}
|
|
7370
7869
|
/**
|
|
7371
7870
|
* (Protected) Performs a Right-Left (RL) double rotation.
|
|
@@ -7409,6 +7908,9 @@ var AVLTree = class extends BST {
|
|
|
7409
7908
|
this._updateHeight(A);
|
|
7410
7909
|
if (B) this._updateHeight(B);
|
|
7411
7910
|
if (C) this._updateHeight(C);
|
|
7911
|
+
this._updateCount(A);
|
|
7912
|
+
if (B) this._updateCount(B);
|
|
7913
|
+
if (C) this._updateCount(C);
|
|
7412
7914
|
}
|
|
7413
7915
|
/**
|
|
7414
7916
|
* (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
|
|
@@ -7423,6 +7925,7 @@ var AVLTree = class extends BST {
|
|
|
7423
7925
|
const A = path[i];
|
|
7424
7926
|
if (A) {
|
|
7425
7927
|
this._updateHeight(A);
|
|
7928
|
+
this._updateCount(A);
|
|
7426
7929
|
switch (this._balanceFactor(A)) {
|
|
7427
7930
|
case -2:
|
|
7428
7931
|
if (A && A.left) {
|
|
@@ -7752,6 +8255,22 @@ var RedBlackTree = class extends BST {
|
|
|
7752
8255
|
|
|
7753
8256
|
|
|
7754
8257
|
|
|
8258
|
+
|
|
8259
|
+
|
|
8260
|
+
|
|
8261
|
+
|
|
8262
|
+
|
|
8263
|
+
|
|
8264
|
+
|
|
8265
|
+
|
|
8266
|
+
|
|
8267
|
+
|
|
8268
|
+
|
|
8269
|
+
|
|
8270
|
+
|
|
8271
|
+
|
|
8272
|
+
|
|
8273
|
+
|
|
7755
8274
|
|
|
7756
8275
|
|
|
7757
8276
|
|
|
@@ -7858,6 +8377,7 @@ var RedBlackTree = class extends BST {
|
|
|
7858
8377
|
node.left = NIL;
|
|
7859
8378
|
node.right = NIL;
|
|
7860
8379
|
node.color = "RED";
|
|
8380
|
+
this._updateCountAlongPath(node);
|
|
7861
8381
|
this._insertFixup(node);
|
|
7862
8382
|
if (this.isRealNode(this._root)) this._root.color = "BLACK";
|
|
7863
8383
|
}
|
|
@@ -7967,6 +8487,7 @@ var RedBlackTree = class extends BST {
|
|
|
7967
8487
|
newNode.left = NIL;
|
|
7968
8488
|
newNode.right = NIL;
|
|
7969
8489
|
newNode.color = "RED";
|
|
8490
|
+
this._updateCountAlongPath(newNode);
|
|
7970
8491
|
this._insertFixup(newNode);
|
|
7971
8492
|
if (this.isRealNode(this._root)) this._root.color = "BLACK";
|
|
7972
8493
|
else return void 0;
|
|
@@ -8209,6 +8730,22 @@ var RedBlackTree = class extends BST {
|
|
|
8209
8730
|
|
|
8210
8731
|
|
|
8211
8732
|
|
|
8733
|
+
|
|
8734
|
+
|
|
8735
|
+
|
|
8736
|
+
|
|
8737
|
+
|
|
8738
|
+
|
|
8739
|
+
|
|
8740
|
+
|
|
8741
|
+
|
|
8742
|
+
|
|
8743
|
+
|
|
8744
|
+
|
|
8745
|
+
|
|
8746
|
+
|
|
8747
|
+
|
|
8748
|
+
|
|
8212
8749
|
|
|
8213
8750
|
|
|
8214
8751
|
|
|
@@ -8409,17 +8946,33 @@ var RedBlackTree = class extends BST {
|
|
|
8409
8946
|
|
|
8410
8947
|
|
|
8411
8948
|
|
|
8412
|
-
|
|
8413
|
-
|
|
8414
|
-
|
|
8415
|
-
|
|
8416
|
-
|
|
8417
|
-
|
|
8418
|
-
|
|
8419
|
-
|
|
8420
|
-
|
|
8421
|
-
|
|
8422
|
-
|
|
8949
|
+
|
|
8950
|
+
|
|
8951
|
+
|
|
8952
|
+
|
|
8953
|
+
|
|
8954
|
+
|
|
8955
|
+
|
|
8956
|
+
|
|
8957
|
+
|
|
8958
|
+
|
|
8959
|
+
|
|
8960
|
+
|
|
8961
|
+
|
|
8962
|
+
|
|
8963
|
+
|
|
8964
|
+
|
|
8965
|
+
* @example
|
|
8966
|
+
* // Remove and rebalance
|
|
8967
|
+
* const rbt = new RedBlackTree<number>([10, 5, 15, 3, 7]);
|
|
8968
|
+
* rbt.delete(5);
|
|
8969
|
+
* console.log(rbt.has(5)); // false;
|
|
8970
|
+
* console.log(rbt.size); // 4;
|
|
8971
|
+
*/
|
|
8972
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
8973
|
+
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
8974
|
+
const results = [];
|
|
8975
|
+
let nodeToDelete;
|
|
8423
8976
|
if (this._isPredicate(keyNodeEntryRawOrPredicate)) nodeToDelete = this.getNode(keyNodeEntryRawOrPredicate);
|
|
8424
8977
|
else nodeToDelete = this.isRealNode(keyNodeEntryRawOrPredicate) ? keyNodeEntryRawOrPredicate : this.getNode(keyNodeEntryRawOrPredicate);
|
|
8425
8978
|
if (!nodeToDelete) {
|
|
@@ -8462,6 +9015,7 @@ var RedBlackTree = class extends BST {
|
|
|
8462
9015
|
}
|
|
8463
9016
|
if (this._isMapMode) this._store.delete(nodeToDelete.key);
|
|
8464
9017
|
this._size--;
|
|
9018
|
+
this._updateCountAlongPath(replacementNode?.parent ?? replacementNode);
|
|
8465
9019
|
if (this._size <= 0) {
|
|
8466
9020
|
this._setMinCache(void 0);
|
|
8467
9021
|
this._setMaxCache(void 0);
|
|
@@ -8568,6 +9122,18 @@ var RedBlackTree = class extends BST {
|
|
|
8568
9122
|
|
|
8569
9123
|
|
|
8570
9124
|
|
|
9125
|
+
|
|
9126
|
+
|
|
9127
|
+
|
|
9128
|
+
|
|
9129
|
+
|
|
9130
|
+
|
|
9131
|
+
|
|
9132
|
+
|
|
9133
|
+
|
|
9134
|
+
|
|
9135
|
+
|
|
9136
|
+
|
|
8571
9137
|
|
|
8572
9138
|
|
|
8573
9139
|
|
|
@@ -8700,6 +9266,22 @@ var RedBlackTree = class extends BST {
|
|
|
8700
9266
|
|
|
8701
9267
|
|
|
8702
9268
|
|
|
9269
|
+
|
|
9270
|
+
|
|
9271
|
+
|
|
9272
|
+
|
|
9273
|
+
|
|
9274
|
+
|
|
9275
|
+
|
|
9276
|
+
|
|
9277
|
+
|
|
9278
|
+
|
|
9279
|
+
|
|
9280
|
+
|
|
9281
|
+
|
|
9282
|
+
|
|
9283
|
+
|
|
9284
|
+
|
|
8703
9285
|
|
|
8704
9286
|
|
|
8705
9287
|
|
|
@@ -8802,6 +9384,7 @@ var RedBlackTree = class extends BST {
|
|
|
8802
9384
|
node.left = NIL;
|
|
8803
9385
|
node.right = NIL;
|
|
8804
9386
|
node.color = "RED";
|
|
9387
|
+
this._updateCountAlongPath(node);
|
|
8805
9388
|
this._insertFixup(node);
|
|
8806
9389
|
return "CREATED";
|
|
8807
9390
|
}
|
|
@@ -8970,6 +9553,8 @@ var RedBlackTree = class extends BST {
|
|
|
8970
9553
|
}
|
|
8971
9554
|
y.left = x;
|
|
8972
9555
|
x.parent = y;
|
|
9556
|
+
this._updateCount(x);
|
|
9557
|
+
this._updateCount(y);
|
|
8973
9558
|
}
|
|
8974
9559
|
/**
|
|
8975
9560
|
* (Protected) Perform a right rotation around y.
|
|
@@ -8996,6 +9581,8 @@ var RedBlackTree = class extends BST {
|
|
|
8996
9581
|
}
|
|
8997
9582
|
x.right = y;
|
|
8998
9583
|
y.parent = x;
|
|
9584
|
+
this._updateCount(y);
|
|
9585
|
+
this._updateCount(x);
|
|
8999
9586
|
}
|
|
9000
9587
|
};
|
|
9001
9588
|
|
|
@@ -9027,7 +9614,7 @@ var TreeSet = class _TreeSet {
|
|
|
9027
9614
|
const toElementFn = options.toElementFn;
|
|
9028
9615
|
const comparator = options.comparator ?? _TreeSet.createDefaultComparator();
|
|
9029
9616
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
9030
|
-
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode });
|
|
9617
|
+
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
9031
9618
|
for (const item of elements) {
|
|
9032
9619
|
const k = toElementFn ? toElementFn(item) : item;
|
|
9033
9620
|
this.add(k);
|
|
@@ -9046,7 +9633,7 @@ var TreeSet = class _TreeSet {
|
|
|
9046
9633
|
static createDefaultComparator() {
|
|
9047
9634
|
return (a, b) => {
|
|
9048
9635
|
if (typeof a === "number" && typeof b === "number") {
|
|
9049
|
-
if (Number.isNaN(a) || Number.isNaN(b))
|
|
9636
|
+
if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN("TreeSet"));
|
|
9050
9637
|
const aa = Object.is(a, -0) ? 0 : a;
|
|
9051
9638
|
const bb = Object.is(b, -0) ? 0 : b;
|
|
9052
9639
|
return aa > bb ? 1 : aa < bb ? -1 : 0;
|
|
@@ -9057,10 +9644,10 @@ var TreeSet = class _TreeSet {
|
|
|
9057
9644
|
if (a instanceof Date && b instanceof Date) {
|
|
9058
9645
|
const ta = a.getTime();
|
|
9059
9646
|
const tb = b.getTime();
|
|
9060
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
9647
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("TreeSet"));
|
|
9061
9648
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
9062
9649
|
}
|
|
9063
|
-
|
|
9650
|
+
raise(TypeError, ERR.comparatorRequired("TreeSet"));
|
|
9064
9651
|
};
|
|
9065
9652
|
}
|
|
9066
9653
|
/**
|
|
@@ -9188,6 +9775,26 @@ var TreeSet = class _TreeSet {
|
|
|
9188
9775
|
|
|
9189
9776
|
|
|
9190
9777
|
|
|
9778
|
+
|
|
9779
|
+
|
|
9780
|
+
|
|
9781
|
+
|
|
9782
|
+
|
|
9783
|
+
|
|
9784
|
+
|
|
9785
|
+
|
|
9786
|
+
|
|
9787
|
+
|
|
9788
|
+
|
|
9789
|
+
|
|
9790
|
+
|
|
9791
|
+
|
|
9792
|
+
|
|
9793
|
+
|
|
9794
|
+
|
|
9795
|
+
|
|
9796
|
+
|
|
9797
|
+
|
|
9191
9798
|
|
|
9192
9799
|
|
|
9193
9800
|
|
|
@@ -9219,15 +9826,15 @@ var TreeSet = class _TreeSet {
|
|
|
9219
9826
|
_validateKey(key) {
|
|
9220
9827
|
if (!this.#isDefaultComparator) return;
|
|
9221
9828
|
if (typeof key === "number") {
|
|
9222
|
-
if (Number.isNaN(key))
|
|
9829
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeSet"));
|
|
9223
9830
|
return;
|
|
9224
9831
|
}
|
|
9225
9832
|
if (typeof key === "string") return;
|
|
9226
9833
|
if (key instanceof Date) {
|
|
9227
|
-
if (Number.isNaN(key.getTime()))
|
|
9834
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeSet"));
|
|
9228
9835
|
return;
|
|
9229
9836
|
}
|
|
9230
|
-
|
|
9837
|
+
raise(TypeError, ERR.comparatorRequired("TreeSet"));
|
|
9231
9838
|
}
|
|
9232
9839
|
/**
|
|
9233
9840
|
* Add a key to the set (no-op if already present).
|
|
@@ -9352,6 +9959,26 @@ var TreeSet = class _TreeSet {
|
|
|
9352
9959
|
|
|
9353
9960
|
|
|
9354
9961
|
|
|
9962
|
+
|
|
9963
|
+
|
|
9964
|
+
|
|
9965
|
+
|
|
9966
|
+
|
|
9967
|
+
|
|
9968
|
+
|
|
9969
|
+
|
|
9970
|
+
|
|
9971
|
+
|
|
9972
|
+
|
|
9973
|
+
|
|
9974
|
+
|
|
9975
|
+
|
|
9976
|
+
|
|
9977
|
+
|
|
9978
|
+
|
|
9979
|
+
|
|
9980
|
+
|
|
9981
|
+
|
|
9355
9982
|
|
|
9356
9983
|
|
|
9357
9984
|
|
|
@@ -9521,6 +10148,26 @@ var TreeSet = class _TreeSet {
|
|
|
9521
10148
|
|
|
9522
10149
|
|
|
9523
10150
|
|
|
10151
|
+
|
|
10152
|
+
|
|
10153
|
+
|
|
10154
|
+
|
|
10155
|
+
|
|
10156
|
+
|
|
10157
|
+
|
|
10158
|
+
|
|
10159
|
+
|
|
10160
|
+
|
|
10161
|
+
|
|
10162
|
+
|
|
10163
|
+
|
|
10164
|
+
|
|
10165
|
+
|
|
10166
|
+
|
|
10167
|
+
|
|
10168
|
+
|
|
10169
|
+
|
|
10170
|
+
|
|
9524
10171
|
|
|
9525
10172
|
|
|
9526
10173
|
|
|
@@ -9685,6 +10332,26 @@ var TreeSet = class _TreeSet {
|
|
|
9685
10332
|
|
|
9686
10333
|
|
|
9687
10334
|
|
|
10335
|
+
|
|
10336
|
+
|
|
10337
|
+
|
|
10338
|
+
|
|
10339
|
+
|
|
10340
|
+
|
|
10341
|
+
|
|
10342
|
+
|
|
10343
|
+
|
|
10344
|
+
|
|
10345
|
+
|
|
10346
|
+
|
|
10347
|
+
|
|
10348
|
+
|
|
10349
|
+
|
|
10350
|
+
|
|
10351
|
+
|
|
10352
|
+
|
|
10353
|
+
|
|
10354
|
+
|
|
9688
10355
|
|
|
9689
10356
|
|
|
9690
10357
|
|
|
@@ -9838,6 +10505,26 @@ var TreeSet = class _TreeSet {
|
|
|
9838
10505
|
|
|
9839
10506
|
|
|
9840
10507
|
|
|
10508
|
+
|
|
10509
|
+
|
|
10510
|
+
|
|
10511
|
+
|
|
10512
|
+
|
|
10513
|
+
|
|
10514
|
+
|
|
10515
|
+
|
|
10516
|
+
|
|
10517
|
+
|
|
10518
|
+
|
|
10519
|
+
|
|
10520
|
+
|
|
10521
|
+
|
|
10522
|
+
|
|
10523
|
+
|
|
10524
|
+
|
|
10525
|
+
|
|
10526
|
+
|
|
10527
|
+
|
|
9841
10528
|
|
|
9842
10529
|
|
|
9843
10530
|
|
|
@@ -9987,6 +10674,26 @@ var TreeSet = class _TreeSet {
|
|
|
9987
10674
|
|
|
9988
10675
|
|
|
9989
10676
|
|
|
10677
|
+
|
|
10678
|
+
|
|
10679
|
+
|
|
10680
|
+
|
|
10681
|
+
|
|
10682
|
+
|
|
10683
|
+
|
|
10684
|
+
|
|
10685
|
+
|
|
10686
|
+
|
|
10687
|
+
|
|
10688
|
+
|
|
10689
|
+
|
|
10690
|
+
|
|
10691
|
+
|
|
10692
|
+
|
|
10693
|
+
|
|
10694
|
+
|
|
10695
|
+
|
|
10696
|
+
|
|
9990
10697
|
|
|
9991
10698
|
|
|
9992
10699
|
|
|
@@ -10137,6 +10844,26 @@ var TreeSet = class _TreeSet {
|
|
|
10137
10844
|
|
|
10138
10845
|
|
|
10139
10846
|
|
|
10847
|
+
|
|
10848
|
+
|
|
10849
|
+
|
|
10850
|
+
|
|
10851
|
+
|
|
10852
|
+
|
|
10853
|
+
|
|
10854
|
+
|
|
10855
|
+
|
|
10856
|
+
|
|
10857
|
+
|
|
10858
|
+
|
|
10859
|
+
|
|
10860
|
+
|
|
10861
|
+
|
|
10862
|
+
|
|
10863
|
+
|
|
10864
|
+
|
|
10865
|
+
|
|
10866
|
+
|
|
10140
10867
|
|
|
10141
10868
|
|
|
10142
10869
|
|
|
@@ -10287,6 +11014,26 @@ var TreeSet = class _TreeSet {
|
|
|
10287
11014
|
|
|
10288
11015
|
|
|
10289
11016
|
|
|
11017
|
+
|
|
11018
|
+
|
|
11019
|
+
|
|
11020
|
+
|
|
11021
|
+
|
|
11022
|
+
|
|
11023
|
+
|
|
11024
|
+
|
|
11025
|
+
|
|
11026
|
+
|
|
11027
|
+
|
|
11028
|
+
|
|
11029
|
+
|
|
11030
|
+
|
|
11031
|
+
|
|
11032
|
+
|
|
11033
|
+
|
|
11034
|
+
|
|
11035
|
+
|
|
11036
|
+
|
|
10290
11037
|
|
|
10291
11038
|
|
|
10292
11039
|
|
|
@@ -10440,6 +11187,26 @@ var TreeSet = class _TreeSet {
|
|
|
10440
11187
|
|
|
10441
11188
|
|
|
10442
11189
|
|
|
11190
|
+
|
|
11191
|
+
|
|
11192
|
+
|
|
11193
|
+
|
|
11194
|
+
|
|
11195
|
+
|
|
11196
|
+
|
|
11197
|
+
|
|
11198
|
+
|
|
11199
|
+
|
|
11200
|
+
|
|
11201
|
+
|
|
11202
|
+
|
|
11203
|
+
|
|
11204
|
+
|
|
11205
|
+
|
|
11206
|
+
|
|
11207
|
+
|
|
11208
|
+
|
|
11209
|
+
|
|
10443
11210
|
|
|
10444
11211
|
|
|
10445
11212
|
|
|
@@ -10593,6 +11360,26 @@ var TreeSet = class _TreeSet {
|
|
|
10593
11360
|
|
|
10594
11361
|
|
|
10595
11362
|
|
|
11363
|
+
|
|
11364
|
+
|
|
11365
|
+
|
|
11366
|
+
|
|
11367
|
+
|
|
11368
|
+
|
|
11369
|
+
|
|
11370
|
+
|
|
11371
|
+
|
|
11372
|
+
|
|
11373
|
+
|
|
11374
|
+
|
|
11375
|
+
|
|
11376
|
+
|
|
11377
|
+
|
|
11378
|
+
|
|
11379
|
+
|
|
11380
|
+
|
|
11381
|
+
|
|
11382
|
+
|
|
10596
11383
|
|
|
10597
11384
|
|
|
10598
11385
|
|
|
@@ -10770,7 +11557,27 @@ var TreeSet = class _TreeSet {
|
|
|
10770
11557
|
|
|
10771
11558
|
|
|
10772
11559
|
|
|
10773
|
-
|
|
11560
|
+
|
|
11561
|
+
|
|
11562
|
+
|
|
11563
|
+
|
|
11564
|
+
|
|
11565
|
+
|
|
11566
|
+
|
|
11567
|
+
|
|
11568
|
+
|
|
11569
|
+
|
|
11570
|
+
|
|
11571
|
+
|
|
11572
|
+
|
|
11573
|
+
|
|
11574
|
+
|
|
11575
|
+
|
|
11576
|
+
|
|
11577
|
+
|
|
11578
|
+
|
|
11579
|
+
|
|
11580
|
+
* @example
|
|
10774
11581
|
* // Filter
|
|
10775
11582
|
* const ts = new TreeSet<number>([1, 2, 3, 4, 5]);
|
|
10776
11583
|
* const evens = ts.filter(k => k % 2 === 0);
|
|
@@ -10905,6 +11712,26 @@ var TreeSet = class _TreeSet {
|
|
|
10905
11712
|
|
|
10906
11713
|
|
|
10907
11714
|
|
|
11715
|
+
|
|
11716
|
+
|
|
11717
|
+
|
|
11718
|
+
|
|
11719
|
+
|
|
11720
|
+
|
|
11721
|
+
|
|
11722
|
+
|
|
11723
|
+
|
|
11724
|
+
|
|
11725
|
+
|
|
11726
|
+
|
|
11727
|
+
|
|
11728
|
+
|
|
11729
|
+
|
|
11730
|
+
|
|
11731
|
+
|
|
11732
|
+
|
|
11733
|
+
|
|
11734
|
+
|
|
10908
11735
|
|
|
10909
11736
|
|
|
10910
11737
|
|
|
@@ -11056,6 +11883,26 @@ var TreeSet = class _TreeSet {
|
|
|
11056
11883
|
|
|
11057
11884
|
|
|
11058
11885
|
|
|
11886
|
+
|
|
11887
|
+
|
|
11888
|
+
|
|
11889
|
+
|
|
11890
|
+
|
|
11891
|
+
|
|
11892
|
+
|
|
11893
|
+
|
|
11894
|
+
|
|
11895
|
+
|
|
11896
|
+
|
|
11897
|
+
|
|
11898
|
+
|
|
11899
|
+
|
|
11900
|
+
|
|
11901
|
+
|
|
11902
|
+
|
|
11903
|
+
|
|
11904
|
+
|
|
11905
|
+
|
|
11059
11906
|
|
|
11060
11907
|
|
|
11061
11908
|
|
|
@@ -11208,6 +12055,26 @@ var TreeSet = class _TreeSet {
|
|
|
11208
12055
|
|
|
11209
12056
|
|
|
11210
12057
|
|
|
12058
|
+
|
|
12059
|
+
|
|
12060
|
+
|
|
12061
|
+
|
|
12062
|
+
|
|
12063
|
+
|
|
12064
|
+
|
|
12065
|
+
|
|
12066
|
+
|
|
12067
|
+
|
|
12068
|
+
|
|
12069
|
+
|
|
12070
|
+
|
|
12071
|
+
|
|
12072
|
+
|
|
12073
|
+
|
|
12074
|
+
|
|
12075
|
+
|
|
12076
|
+
|
|
12077
|
+
|
|
11211
12078
|
|
|
11212
12079
|
|
|
11213
12080
|
|
|
@@ -11360,6 +12227,26 @@ var TreeSet = class _TreeSet {
|
|
|
11360
12227
|
|
|
11361
12228
|
|
|
11362
12229
|
|
|
12230
|
+
|
|
12231
|
+
|
|
12232
|
+
|
|
12233
|
+
|
|
12234
|
+
|
|
12235
|
+
|
|
12236
|
+
|
|
12237
|
+
|
|
12238
|
+
|
|
12239
|
+
|
|
12240
|
+
|
|
12241
|
+
|
|
12242
|
+
|
|
12243
|
+
|
|
12244
|
+
|
|
12245
|
+
|
|
12246
|
+
|
|
12247
|
+
|
|
12248
|
+
|
|
12249
|
+
|
|
11363
12250
|
|
|
11364
12251
|
|
|
11365
12252
|
|
|
@@ -11515,6 +12402,26 @@ var TreeSet = class _TreeSet {
|
|
|
11515
12402
|
|
|
11516
12403
|
|
|
11517
12404
|
|
|
12405
|
+
|
|
12406
|
+
|
|
12407
|
+
|
|
12408
|
+
|
|
12409
|
+
|
|
12410
|
+
|
|
12411
|
+
|
|
12412
|
+
|
|
12413
|
+
|
|
12414
|
+
|
|
12415
|
+
|
|
12416
|
+
|
|
12417
|
+
|
|
12418
|
+
|
|
12419
|
+
|
|
12420
|
+
|
|
12421
|
+
|
|
12422
|
+
|
|
12423
|
+
|
|
12424
|
+
|
|
11518
12425
|
|
|
11519
12426
|
|
|
11520
12427
|
|
|
@@ -11664,6 +12571,26 @@ var TreeSet = class _TreeSet {
|
|
|
11664
12571
|
|
|
11665
12572
|
|
|
11666
12573
|
|
|
12574
|
+
|
|
12575
|
+
|
|
12576
|
+
|
|
12577
|
+
|
|
12578
|
+
|
|
12579
|
+
|
|
12580
|
+
|
|
12581
|
+
|
|
12582
|
+
|
|
12583
|
+
|
|
12584
|
+
|
|
12585
|
+
|
|
12586
|
+
|
|
12587
|
+
|
|
12588
|
+
|
|
12589
|
+
|
|
12590
|
+
|
|
12591
|
+
|
|
12592
|
+
|
|
12593
|
+
|
|
11667
12594
|
|
|
11668
12595
|
|
|
11669
12596
|
|
|
@@ -11722,6 +12649,10 @@ var TreeSet = class _TreeSet {
|
|
|
11722
12649
|
|
|
11723
12650
|
|
|
11724
12651
|
|
|
12652
|
+
|
|
12653
|
+
|
|
12654
|
+
|
|
12655
|
+
|
|
11725
12656
|
|
|
11726
12657
|
|
|
11727
12658
|
|
|
@@ -11786,6 +12717,10 @@ var TreeSet = class _TreeSet {
|
|
|
11786
12717
|
|
|
11787
12718
|
|
|
11788
12719
|
|
|
12720
|
+
|
|
12721
|
+
|
|
12722
|
+
|
|
12723
|
+
|
|
11789
12724
|
|
|
11790
12725
|
|
|
11791
12726
|
|
|
@@ -11828,6 +12763,10 @@ var TreeSet = class _TreeSet {
|
|
|
11828
12763
|
|
|
11829
12764
|
|
|
11830
12765
|
|
|
12766
|
+
|
|
12767
|
+
|
|
12768
|
+
|
|
12769
|
+
|
|
11831
12770
|
|
|
11832
12771
|
|
|
11833
12772
|
|
|
@@ -11875,6 +12814,10 @@ var TreeSet = class _TreeSet {
|
|
|
11875
12814
|
|
|
11876
12815
|
|
|
11877
12816
|
|
|
12817
|
+
|
|
12818
|
+
|
|
12819
|
+
|
|
12820
|
+
|
|
11878
12821
|
|
|
11879
12822
|
|
|
11880
12823
|
|
|
@@ -11996,6 +12939,22 @@ var TreeSet = class _TreeSet {
|
|
|
11996
12939
|
|
|
11997
12940
|
|
|
11998
12941
|
|
|
12942
|
+
|
|
12943
|
+
|
|
12944
|
+
|
|
12945
|
+
|
|
12946
|
+
|
|
12947
|
+
|
|
12948
|
+
|
|
12949
|
+
|
|
12950
|
+
|
|
12951
|
+
|
|
12952
|
+
|
|
12953
|
+
|
|
12954
|
+
|
|
12955
|
+
|
|
12956
|
+
|
|
12957
|
+
|
|
11999
12958
|
|
|
12000
12959
|
|
|
12001
12960
|
|
|
@@ -12137,6 +13096,22 @@ var TreeSet = class _TreeSet {
|
|
|
12137
13096
|
|
|
12138
13097
|
|
|
12139
13098
|
|
|
13099
|
+
|
|
13100
|
+
|
|
13101
|
+
|
|
13102
|
+
|
|
13103
|
+
|
|
13104
|
+
|
|
13105
|
+
|
|
13106
|
+
|
|
13107
|
+
|
|
13108
|
+
|
|
13109
|
+
|
|
13110
|
+
|
|
13111
|
+
|
|
13112
|
+
|
|
13113
|
+
|
|
13114
|
+
|
|
12140
13115
|
|
|
12141
13116
|
|
|
12142
13117
|
|
|
@@ -12270,6 +13245,22 @@ var TreeSet = class _TreeSet {
|
|
|
12270
13245
|
|
|
12271
13246
|
|
|
12272
13247
|
|
|
13248
|
+
|
|
13249
|
+
|
|
13250
|
+
|
|
13251
|
+
|
|
13252
|
+
|
|
13253
|
+
|
|
13254
|
+
|
|
13255
|
+
|
|
13256
|
+
|
|
13257
|
+
|
|
13258
|
+
|
|
13259
|
+
|
|
13260
|
+
|
|
13261
|
+
|
|
13262
|
+
|
|
13263
|
+
|
|
12273
13264
|
|
|
12274
13265
|
|
|
12275
13266
|
|
|
@@ -12401,6 +13392,22 @@ var TreeSet = class _TreeSet {
|
|
|
12401
13392
|
|
|
12402
13393
|
|
|
12403
13394
|
|
|
13395
|
+
|
|
13396
|
+
|
|
13397
|
+
|
|
13398
|
+
|
|
13399
|
+
|
|
13400
|
+
|
|
13401
|
+
|
|
13402
|
+
|
|
13403
|
+
|
|
13404
|
+
|
|
13405
|
+
|
|
13406
|
+
|
|
13407
|
+
|
|
13408
|
+
|
|
13409
|
+
|
|
13410
|
+
|
|
12404
13411
|
|
|
12405
13412
|
|
|
12406
13413
|
|
|
@@ -12535,6 +13542,22 @@ var TreeSet = class _TreeSet {
|
|
|
12535
13542
|
|
|
12536
13543
|
|
|
12537
13544
|
|
|
13545
|
+
|
|
13546
|
+
|
|
13547
|
+
|
|
13548
|
+
|
|
13549
|
+
|
|
13550
|
+
|
|
13551
|
+
|
|
13552
|
+
|
|
13553
|
+
|
|
13554
|
+
|
|
13555
|
+
|
|
13556
|
+
|
|
13557
|
+
|
|
13558
|
+
|
|
13559
|
+
|
|
13560
|
+
|
|
12538
13561
|
|
|
12539
13562
|
|
|
12540
13563
|
|
|
@@ -12586,6 +13609,62 @@ var TreeSet = class _TreeSet {
|
|
|
12586
13609
|
}
|
|
12587
13610
|
return out;
|
|
12588
13611
|
}
|
|
13612
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
13613
|
+
/**
|
|
13614
|
+
* Returns the element at the k-th position in tree order (0-indexed).
|
|
13615
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
13616
|
+
|
|
13617
|
+
|
|
13618
|
+
|
|
13619
|
+
* @example
|
|
13620
|
+
* // Find k-th element in a TreeSet
|
|
13621
|
+
* const set = new TreeSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
13622
|
+
* console.log(set.getByRank(0)); // 10;
|
|
13623
|
+
* console.log(set.getByRank(2)); // 30;
|
|
13624
|
+
* console.log(set.getRank(30)); // 2;
|
|
13625
|
+
*/
|
|
13626
|
+
getByRank(k) {
|
|
13627
|
+
return this.#core.getByRank(k);
|
|
13628
|
+
}
|
|
13629
|
+
/**
|
|
13630
|
+
* Returns the 0-based rank of a key (number of elements that precede it in tree order).
|
|
13631
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
13632
|
+
* @example
|
|
13633
|
+
* // Get the rank of a key in sorted order
|
|
13634
|
+
* const tree = new TreeSet<number>(
|
|
13635
|
+
* [10, 20, 30, 40, 50],
|
|
13636
|
+
* { enableOrderStatistic: true }
|
|
13637
|
+
* );
|
|
13638
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
13639
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
13640
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
13641
|
+
* console.log(tree.getRank(25)); // 2;
|
|
13642
|
+
*/
|
|
13643
|
+
getRank(key) {
|
|
13644
|
+
return this.#core.getRank(key);
|
|
13645
|
+
}
|
|
13646
|
+
/**
|
|
13647
|
+
* Returns elements by rank range (0-indexed, inclusive on both ends).
|
|
13648
|
+
* @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
|
|
13649
|
+
|
|
13650
|
+
* @example
|
|
13651
|
+
* // Pagination with rangeByRank
|
|
13652
|
+
* const tree = new TreeSet<number>(
|
|
13653
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
13654
|
+
* { enableOrderStatistic: true }
|
|
13655
|
+
* );
|
|
13656
|
+
* const pageSize = 3;
|
|
13657
|
+
*
|
|
13658
|
+
* // Page 1
|
|
13659
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
13660
|
+
* // Page 2
|
|
13661
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
13662
|
+
* // Page 3
|
|
13663
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
13664
|
+
*/
|
|
13665
|
+
rangeByRank(start, end) {
|
|
13666
|
+
return this.#core.rangeByRank(start, end).filter((k) => k !== void 0);
|
|
13667
|
+
}
|
|
12589
13668
|
/**
|
|
12590
13669
|
* Creates a shallow clone of this set.
|
|
12591
13670
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -12728,8 +13807,28 @@ var TreeSet = class _TreeSet {
|
|
|
12728
13807
|
|
|
12729
13808
|
|
|
12730
13809
|
|
|
12731
|
-
|
|
12732
|
-
|
|
13810
|
+
|
|
13811
|
+
|
|
13812
|
+
|
|
13813
|
+
|
|
13814
|
+
|
|
13815
|
+
|
|
13816
|
+
|
|
13817
|
+
|
|
13818
|
+
|
|
13819
|
+
|
|
13820
|
+
|
|
13821
|
+
|
|
13822
|
+
|
|
13823
|
+
|
|
13824
|
+
|
|
13825
|
+
|
|
13826
|
+
|
|
13827
|
+
|
|
13828
|
+
|
|
13829
|
+
|
|
13830
|
+
* @example
|
|
13831
|
+
* // Deep clone
|
|
12733
13832
|
* const ts = new TreeSet<number>([1, 2, 3]);
|
|
12734
13833
|
* const copy = ts.clone();
|
|
12735
13834
|
* copy.delete(1);
|
|
@@ -12767,7 +13866,7 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
12767
13866
|
const comparator = options.comparator ?? TreeSet.createDefaultComparator();
|
|
12768
13867
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
12769
13868
|
const toEntryFn = options.toEntryFn;
|
|
12770
|
-
this.#core = new RedBlackTree([], { ...options, comparator, isMapMode: options.isMapMode });
|
|
13869
|
+
this.#core = new RedBlackTree([], { ...options, comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
12771
13870
|
for (const x of keysNodesEntriesOrRaws) {
|
|
12772
13871
|
if (x === null || x === void 0) continue;
|
|
12773
13872
|
if (toEntryFn) {
|
|
@@ -12800,15 +13899,15 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
12800
13899
|
_validateKey(key) {
|
|
12801
13900
|
if (!this.#isDefaultComparator) return;
|
|
12802
13901
|
if (typeof key === "number") {
|
|
12803
|
-
if (Number.isNaN(key))
|
|
13902
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeMultiMap"));
|
|
12804
13903
|
return;
|
|
12805
13904
|
}
|
|
12806
13905
|
if (typeof key === "string") return;
|
|
12807
13906
|
if (key instanceof Date) {
|
|
12808
|
-
if (Number.isNaN(key.getTime()))
|
|
13907
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeMultiMap"));
|
|
12809
13908
|
return;
|
|
12810
13909
|
}
|
|
12811
|
-
|
|
13910
|
+
raise(TypeError, ERR.comparatorRequired("TreeMultiMap"));
|
|
12812
13911
|
}
|
|
12813
13912
|
/**
|
|
12814
13913
|
* Number of distinct keys.
|
|
@@ -12937,6 +14036,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
12937
14036
|
|
|
12938
14037
|
|
|
12939
14038
|
|
|
14039
|
+
|
|
14040
|
+
|
|
14041
|
+
|
|
14042
|
+
|
|
14043
|
+
|
|
14044
|
+
|
|
14045
|
+
|
|
14046
|
+
|
|
14047
|
+
|
|
14048
|
+
|
|
14049
|
+
|
|
14050
|
+
|
|
14051
|
+
|
|
14052
|
+
|
|
14053
|
+
|
|
14054
|
+
|
|
14055
|
+
|
|
14056
|
+
|
|
14057
|
+
|
|
14058
|
+
|
|
12940
14059
|
|
|
12941
14060
|
|
|
12942
14061
|
|
|
@@ -13085,6 +14204,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13085
14204
|
|
|
13086
14205
|
|
|
13087
14206
|
|
|
14207
|
+
|
|
14208
|
+
|
|
14209
|
+
|
|
14210
|
+
|
|
14211
|
+
|
|
14212
|
+
|
|
14213
|
+
|
|
14214
|
+
|
|
14215
|
+
|
|
14216
|
+
|
|
14217
|
+
|
|
14218
|
+
|
|
14219
|
+
|
|
14220
|
+
|
|
14221
|
+
|
|
14222
|
+
|
|
14223
|
+
|
|
14224
|
+
|
|
14225
|
+
|
|
14226
|
+
|
|
13088
14227
|
|
|
13089
14228
|
|
|
13090
14229
|
|
|
@@ -13136,6 +14275,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13136
14275
|
|
|
13137
14276
|
|
|
13138
14277
|
|
|
14278
|
+
|
|
14279
|
+
|
|
14280
|
+
|
|
14281
|
+
|
|
13139
14282
|
|
|
13140
14283
|
|
|
13141
14284
|
|
|
@@ -13172,6 +14315,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13172
14315
|
|
|
13173
14316
|
|
|
13174
14317
|
|
|
14318
|
+
|
|
14319
|
+
|
|
14320
|
+
|
|
14321
|
+
|
|
13175
14322
|
|
|
13176
14323
|
|
|
13177
14324
|
|
|
@@ -13344,6 +14491,30 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13344
14491
|
|
|
13345
14492
|
|
|
13346
14493
|
|
|
14494
|
+
|
|
14495
|
+
|
|
14496
|
+
|
|
14497
|
+
|
|
14498
|
+
|
|
14499
|
+
|
|
14500
|
+
|
|
14501
|
+
|
|
14502
|
+
|
|
14503
|
+
|
|
14504
|
+
|
|
14505
|
+
|
|
14506
|
+
|
|
14507
|
+
|
|
14508
|
+
|
|
14509
|
+
|
|
14510
|
+
|
|
14511
|
+
|
|
14512
|
+
|
|
14513
|
+
|
|
14514
|
+
|
|
14515
|
+
|
|
14516
|
+
|
|
14517
|
+
|
|
13347
14518
|
|
|
13348
14519
|
|
|
13349
14520
|
|
|
@@ -13530,6 +14701,30 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13530
14701
|
|
|
13531
14702
|
|
|
13532
14703
|
|
|
14704
|
+
|
|
14705
|
+
|
|
14706
|
+
|
|
14707
|
+
|
|
14708
|
+
|
|
14709
|
+
|
|
14710
|
+
|
|
14711
|
+
|
|
14712
|
+
|
|
14713
|
+
|
|
14714
|
+
|
|
14715
|
+
|
|
14716
|
+
|
|
14717
|
+
|
|
14718
|
+
|
|
14719
|
+
|
|
14720
|
+
|
|
14721
|
+
|
|
14722
|
+
|
|
14723
|
+
|
|
14724
|
+
|
|
14725
|
+
|
|
14726
|
+
|
|
14727
|
+
|
|
13533
14728
|
|
|
13534
14729
|
|
|
13535
14730
|
|
|
@@ -13676,6 +14871,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13676
14871
|
|
|
13677
14872
|
|
|
13678
14873
|
|
|
14874
|
+
|
|
14875
|
+
|
|
14876
|
+
|
|
14877
|
+
|
|
14878
|
+
|
|
14879
|
+
|
|
14880
|
+
|
|
14881
|
+
|
|
14882
|
+
|
|
14883
|
+
|
|
14884
|
+
|
|
14885
|
+
|
|
14886
|
+
|
|
14887
|
+
|
|
14888
|
+
|
|
14889
|
+
|
|
14890
|
+
|
|
14891
|
+
|
|
14892
|
+
|
|
14893
|
+
|
|
13679
14894
|
|
|
13680
14895
|
|
|
13681
14896
|
|
|
@@ -13887,6 +15102,30 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13887
15102
|
|
|
13888
15103
|
|
|
13889
15104
|
|
|
15105
|
+
|
|
15106
|
+
|
|
15107
|
+
|
|
15108
|
+
|
|
15109
|
+
|
|
15110
|
+
|
|
15111
|
+
|
|
15112
|
+
|
|
15113
|
+
|
|
15114
|
+
|
|
15115
|
+
|
|
15116
|
+
|
|
15117
|
+
|
|
15118
|
+
|
|
15119
|
+
|
|
15120
|
+
|
|
15121
|
+
|
|
15122
|
+
|
|
15123
|
+
|
|
15124
|
+
|
|
15125
|
+
|
|
15126
|
+
|
|
15127
|
+
|
|
15128
|
+
|
|
13890
15129
|
|
|
13891
15130
|
|
|
13892
15131
|
|
|
@@ -13940,6 +15179,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13940
15179
|
|
|
13941
15180
|
|
|
13942
15181
|
|
|
15182
|
+
|
|
15183
|
+
|
|
15184
|
+
|
|
15185
|
+
|
|
13943
15186
|
|
|
13944
15187
|
|
|
13945
15188
|
|
|
@@ -13977,6 +15220,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13977
15220
|
|
|
13978
15221
|
|
|
13979
15222
|
|
|
15223
|
+
|
|
15224
|
+
|
|
15225
|
+
|
|
15226
|
+
|
|
13980
15227
|
|
|
13981
15228
|
|
|
13982
15229
|
|
|
@@ -14019,6 +15266,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14019
15266
|
|
|
14020
15267
|
|
|
14021
15268
|
|
|
15269
|
+
|
|
15270
|
+
|
|
15271
|
+
|
|
15272
|
+
|
|
14022
15273
|
|
|
14023
15274
|
|
|
14024
15275
|
|
|
@@ -14177,6 +15428,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14177
15428
|
|
|
14178
15429
|
|
|
14179
15430
|
|
|
15431
|
+
|
|
15432
|
+
|
|
15433
|
+
|
|
15434
|
+
|
|
15435
|
+
|
|
15436
|
+
|
|
15437
|
+
|
|
15438
|
+
|
|
15439
|
+
|
|
15440
|
+
|
|
15441
|
+
|
|
15442
|
+
|
|
15443
|
+
|
|
15444
|
+
|
|
15445
|
+
|
|
15446
|
+
|
|
15447
|
+
|
|
15448
|
+
|
|
15449
|
+
|
|
15450
|
+
|
|
14180
15451
|
|
|
14181
15452
|
|
|
14182
15453
|
|
|
@@ -14328,6 +15599,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14328
15599
|
|
|
14329
15600
|
|
|
14330
15601
|
|
|
15602
|
+
|
|
15603
|
+
|
|
15604
|
+
|
|
15605
|
+
|
|
15606
|
+
|
|
15607
|
+
|
|
15608
|
+
|
|
15609
|
+
|
|
15610
|
+
|
|
15611
|
+
|
|
15612
|
+
|
|
15613
|
+
|
|
15614
|
+
|
|
15615
|
+
|
|
15616
|
+
|
|
15617
|
+
|
|
15618
|
+
|
|
15619
|
+
|
|
15620
|
+
|
|
15621
|
+
|
|
14331
15622
|
|
|
14332
15623
|
|
|
14333
15624
|
|
|
@@ -14380,6 +15671,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14380
15671
|
|
|
14381
15672
|
|
|
14382
15673
|
|
|
15674
|
+
|
|
15675
|
+
|
|
15676
|
+
|
|
15677
|
+
|
|
14383
15678
|
|
|
14384
15679
|
|
|
14385
15680
|
|
|
@@ -14417,6 +15712,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14417
15712
|
|
|
14418
15713
|
|
|
14419
15714
|
|
|
15715
|
+
|
|
15716
|
+
|
|
15717
|
+
|
|
15718
|
+
|
|
14420
15719
|
|
|
14421
15720
|
|
|
14422
15721
|
|
|
@@ -14454,6 +15753,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14454
15753
|
|
|
14455
15754
|
|
|
14456
15755
|
|
|
15756
|
+
|
|
15757
|
+
|
|
15758
|
+
|
|
15759
|
+
|
|
14457
15760
|
|
|
14458
15761
|
|
|
14459
15762
|
|
|
@@ -14521,6 +15824,14 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14521
15824
|
|
|
14522
15825
|
|
|
14523
15826
|
|
|
15827
|
+
|
|
15828
|
+
|
|
15829
|
+
|
|
15830
|
+
|
|
15831
|
+
|
|
15832
|
+
|
|
15833
|
+
|
|
15834
|
+
|
|
14524
15835
|
|
|
14525
15836
|
|
|
14526
15837
|
|
|
@@ -14594,6 +15905,14 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14594
15905
|
|
|
14595
15906
|
|
|
14596
15907
|
|
|
15908
|
+
|
|
15909
|
+
|
|
15910
|
+
|
|
15911
|
+
|
|
15912
|
+
|
|
15913
|
+
|
|
15914
|
+
|
|
15915
|
+
|
|
14597
15916
|
|
|
14598
15917
|
|
|
14599
15918
|
|
|
@@ -14640,6 +15959,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14640
15959
|
|
|
14641
15960
|
|
|
14642
15961
|
|
|
15962
|
+
|
|
15963
|
+
|
|
15964
|
+
|
|
15965
|
+
|
|
14643
15966
|
|
|
14644
15967
|
|
|
14645
15968
|
|
|
@@ -14681,6 +16004,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14681
16004
|
|
|
14682
16005
|
|
|
14683
16006
|
|
|
16007
|
+
|
|
16008
|
+
|
|
16009
|
+
|
|
16010
|
+
|
|
14684
16011
|
|
|
14685
16012
|
|
|
14686
16013
|
|
|
@@ -14844,12 +16171,32 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14844
16171
|
|
|
14845
16172
|
|
|
14846
16173
|
|
|
14847
|
-
|
|
14848
|
-
|
|
14849
|
-
|
|
14850
|
-
|
|
14851
|
-
|
|
14852
|
-
|
|
16174
|
+
|
|
16175
|
+
|
|
16176
|
+
|
|
16177
|
+
|
|
16178
|
+
|
|
16179
|
+
|
|
16180
|
+
|
|
16181
|
+
|
|
16182
|
+
|
|
16183
|
+
|
|
16184
|
+
|
|
16185
|
+
|
|
16186
|
+
|
|
16187
|
+
|
|
16188
|
+
|
|
16189
|
+
|
|
16190
|
+
|
|
16191
|
+
|
|
16192
|
+
|
|
16193
|
+
|
|
16194
|
+
* @example
|
|
16195
|
+
* // Least key ≥ target
|
|
16196
|
+
* const mm = new TreeMultiMap<number, string>();
|
|
16197
|
+
* mm.add(10, 'a');
|
|
16198
|
+
* mm.add(20, 'b');
|
|
16199
|
+
* mm.add(30, 'c');
|
|
14853
16200
|
* console.log(mm.ceiling(15)?.[0]); // 20;
|
|
14854
16201
|
*/
|
|
14855
16202
|
ceiling(key) {
|
|
@@ -14985,6 +16332,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14985
16332
|
|
|
14986
16333
|
|
|
14987
16334
|
|
|
16335
|
+
|
|
16336
|
+
|
|
16337
|
+
|
|
16338
|
+
|
|
16339
|
+
|
|
16340
|
+
|
|
16341
|
+
|
|
16342
|
+
|
|
16343
|
+
|
|
16344
|
+
|
|
16345
|
+
|
|
16346
|
+
|
|
16347
|
+
|
|
16348
|
+
|
|
16349
|
+
|
|
16350
|
+
|
|
16351
|
+
|
|
16352
|
+
|
|
16353
|
+
|
|
16354
|
+
|
|
14988
16355
|
|
|
14989
16356
|
|
|
14990
16357
|
|
|
@@ -15120,6 +16487,22 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15120
16487
|
|
|
15121
16488
|
|
|
15122
16489
|
|
|
16490
|
+
|
|
16491
|
+
|
|
16492
|
+
|
|
16493
|
+
|
|
16494
|
+
|
|
16495
|
+
|
|
16496
|
+
|
|
16497
|
+
|
|
16498
|
+
|
|
16499
|
+
|
|
16500
|
+
|
|
16501
|
+
|
|
16502
|
+
|
|
16503
|
+
|
|
16504
|
+
|
|
16505
|
+
|
|
15123
16506
|
|
|
15124
16507
|
|
|
15125
16508
|
|
|
@@ -15250,6 +16633,22 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15250
16633
|
|
|
15251
16634
|
|
|
15252
16635
|
|
|
16636
|
+
|
|
16637
|
+
|
|
16638
|
+
|
|
16639
|
+
|
|
16640
|
+
|
|
16641
|
+
|
|
16642
|
+
|
|
16643
|
+
|
|
16644
|
+
|
|
16645
|
+
|
|
16646
|
+
|
|
16647
|
+
|
|
16648
|
+
|
|
16649
|
+
|
|
16650
|
+
|
|
16651
|
+
|
|
15253
16652
|
|
|
15254
16653
|
|
|
15255
16654
|
|
|
@@ -15405,6 +16804,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15405
16804
|
|
|
15406
16805
|
|
|
15407
16806
|
|
|
16807
|
+
|
|
16808
|
+
|
|
16809
|
+
|
|
16810
|
+
|
|
16811
|
+
|
|
16812
|
+
|
|
16813
|
+
|
|
16814
|
+
|
|
16815
|
+
|
|
16816
|
+
|
|
16817
|
+
|
|
16818
|
+
|
|
16819
|
+
|
|
16820
|
+
|
|
16821
|
+
|
|
16822
|
+
|
|
16823
|
+
|
|
16824
|
+
|
|
16825
|
+
|
|
16826
|
+
|
|
15408
16827
|
|
|
15409
16828
|
|
|
15410
16829
|
|
|
@@ -15555,6 +16974,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15555
16974
|
|
|
15556
16975
|
|
|
15557
16976
|
|
|
16977
|
+
|
|
16978
|
+
|
|
16979
|
+
|
|
16980
|
+
|
|
16981
|
+
|
|
16982
|
+
|
|
16983
|
+
|
|
16984
|
+
|
|
16985
|
+
|
|
16986
|
+
|
|
16987
|
+
|
|
16988
|
+
|
|
16989
|
+
|
|
16990
|
+
|
|
16991
|
+
|
|
16992
|
+
|
|
16993
|
+
|
|
16994
|
+
|
|
16995
|
+
|
|
16996
|
+
|
|
15558
16997
|
|
|
15559
16998
|
|
|
15560
16999
|
|
|
@@ -15710,6 +17149,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15710
17149
|
|
|
15711
17150
|
|
|
15712
17151
|
|
|
17152
|
+
|
|
17153
|
+
|
|
17154
|
+
|
|
17155
|
+
|
|
17156
|
+
|
|
17157
|
+
|
|
17158
|
+
|
|
17159
|
+
|
|
17160
|
+
|
|
17161
|
+
|
|
17162
|
+
|
|
17163
|
+
|
|
17164
|
+
|
|
17165
|
+
|
|
17166
|
+
|
|
17167
|
+
|
|
17168
|
+
|
|
17169
|
+
|
|
17170
|
+
|
|
17171
|
+
|
|
15713
17172
|
|
|
15714
17173
|
|
|
15715
17174
|
|
|
@@ -15867,6 +17326,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15867
17326
|
|
|
15868
17327
|
|
|
15869
17328
|
|
|
17329
|
+
|
|
17330
|
+
|
|
17331
|
+
|
|
17332
|
+
|
|
17333
|
+
|
|
17334
|
+
|
|
17335
|
+
|
|
17336
|
+
|
|
17337
|
+
|
|
17338
|
+
|
|
17339
|
+
|
|
17340
|
+
|
|
17341
|
+
|
|
17342
|
+
|
|
17343
|
+
|
|
17344
|
+
|
|
17345
|
+
|
|
17346
|
+
|
|
17347
|
+
|
|
17348
|
+
|
|
15870
17349
|
|
|
15871
17350
|
|
|
15872
17351
|
|
|
@@ -16022,6 +17501,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16022
17501
|
|
|
16023
17502
|
|
|
16024
17503
|
|
|
17504
|
+
|
|
17505
|
+
|
|
17506
|
+
|
|
17507
|
+
|
|
17508
|
+
|
|
17509
|
+
|
|
17510
|
+
|
|
17511
|
+
|
|
17512
|
+
|
|
17513
|
+
|
|
17514
|
+
|
|
17515
|
+
|
|
17516
|
+
|
|
17517
|
+
|
|
17518
|
+
|
|
17519
|
+
|
|
17520
|
+
|
|
17521
|
+
|
|
17522
|
+
|
|
17523
|
+
|
|
16025
17524
|
|
|
16026
17525
|
|
|
16027
17526
|
|
|
@@ -16170,6 +17669,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16170
17669
|
|
|
16171
17670
|
|
|
16172
17671
|
|
|
17672
|
+
|
|
17673
|
+
|
|
17674
|
+
|
|
17675
|
+
|
|
17676
|
+
|
|
17677
|
+
|
|
17678
|
+
|
|
17679
|
+
|
|
17680
|
+
|
|
17681
|
+
|
|
17682
|
+
|
|
17683
|
+
|
|
17684
|
+
|
|
17685
|
+
|
|
17686
|
+
|
|
17687
|
+
|
|
17688
|
+
|
|
17689
|
+
|
|
17690
|
+
|
|
17691
|
+
|
|
16173
17692
|
|
|
16174
17693
|
|
|
16175
17694
|
|
|
@@ -16300,6 +17819,22 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16300
17819
|
|
|
16301
17820
|
|
|
16302
17821
|
|
|
17822
|
+
|
|
17823
|
+
|
|
17824
|
+
|
|
17825
|
+
|
|
17826
|
+
|
|
17827
|
+
|
|
17828
|
+
|
|
17829
|
+
|
|
17830
|
+
|
|
17831
|
+
|
|
17832
|
+
|
|
17833
|
+
|
|
17834
|
+
|
|
17835
|
+
|
|
17836
|
+
|
|
17837
|
+
|
|
16303
17838
|
|
|
16304
17839
|
|
|
16305
17840
|
|
|
@@ -16467,6 +18002,81 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16467
18002
|
|
|
16468
18003
|
|
|
16469
18004
|
|
|
18005
|
+
|
|
18006
|
+
|
|
18007
|
+
|
|
18008
|
+
|
|
18009
|
+
|
|
18010
|
+
|
|
18011
|
+
|
|
18012
|
+
|
|
18013
|
+
|
|
18014
|
+
|
|
18015
|
+
|
|
18016
|
+
|
|
18017
|
+
|
|
18018
|
+
|
|
18019
|
+
|
|
18020
|
+
|
|
18021
|
+
|
|
18022
|
+
|
|
18023
|
+
|
|
18024
|
+
|
|
18025
|
+
|
|
18026
|
+
* @example
|
|
18027
|
+
* // Order-statistic on BST
|
|
18028
|
+
* const tree = new TreeMultiMap<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
18029
|
+
* console.log(tree.getByRank(0)); // 10;
|
|
18030
|
+
* console.log(tree.getByRank(4)); // 50;
|
|
18031
|
+
* console.log(tree.getRank(30)); // 2;
|
|
18032
|
+
*/
|
|
18033
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
18034
|
+
getByRank(k) {
|
|
18035
|
+
const key = this.#core.getByRank(k);
|
|
18036
|
+
if (key === void 0) return void 0;
|
|
18037
|
+
return [key, this.#core.get(key) ?? []];
|
|
18038
|
+
}
|
|
18039
|
+
/**
|
|
18040
|
+
* Get the rank of a key in sorted order
|
|
18041
|
+
* @example
|
|
18042
|
+
* // Get the rank of a key in sorted order
|
|
18043
|
+
* const tree = new TreeMultiMap<number>(
|
|
18044
|
+
* [10, 20, 30, 40, 50],
|
|
18045
|
+
* { enableOrderStatistic: true }
|
|
18046
|
+
* );
|
|
18047
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
18048
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
18049
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
18050
|
+
* console.log(tree.getRank(25)); // 2;
|
|
18051
|
+
*/
|
|
18052
|
+
getRank(key) {
|
|
18053
|
+
return this.#core.getRank(key);
|
|
18054
|
+
}
|
|
18055
|
+
/**
|
|
18056
|
+
* Get elements by rank range
|
|
18057
|
+
|
|
18058
|
+
* @example
|
|
18059
|
+
* // Pagination with rangeByRank
|
|
18060
|
+
* const tree = new TreeMultiMap<number>(
|
|
18061
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
18062
|
+
* { enableOrderStatistic: true }
|
|
18063
|
+
* );
|
|
18064
|
+
* const pageSize = 3;
|
|
18065
|
+
*
|
|
18066
|
+
* // Page 1
|
|
18067
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
18068
|
+
* // Page 2
|
|
18069
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
18070
|
+
* // Page 3
|
|
18071
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
18072
|
+
*/
|
|
18073
|
+
rangeByRank(start, end) {
|
|
18074
|
+
const keys = this.#core.rangeByRank(start, end);
|
|
18075
|
+
return keys.filter((k) => k !== void 0).map((k) => [k, this.#core.get(k) ?? []]);
|
|
18076
|
+
}
|
|
18077
|
+
/**
|
|
18078
|
+
* Deep copy
|
|
18079
|
+
|
|
16470
18080
|
|
|
16471
18081
|
|
|
16472
18082
|
|
|
@@ -16518,7 +18128,7 @@ var TreeMap = class _TreeMap {
|
|
|
16518
18128
|
const toEntryFn = options.toEntryFn;
|
|
16519
18129
|
const comparator = options.comparator ?? _TreeMap.createDefaultComparator();
|
|
16520
18130
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
16521
|
-
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode });
|
|
18131
|
+
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
16522
18132
|
for (const item of entries) {
|
|
16523
18133
|
let k;
|
|
16524
18134
|
let v;
|
|
@@ -16526,7 +18136,7 @@ var TreeMap = class _TreeMap {
|
|
|
16526
18136
|
[k, v] = toEntryFn(item);
|
|
16527
18137
|
} else {
|
|
16528
18138
|
if (!Array.isArray(item) || item.length < 2) {
|
|
16529
|
-
|
|
18139
|
+
raise(TypeError, ERR.invalidEntry("TreeMap"));
|
|
16530
18140
|
}
|
|
16531
18141
|
k = item[0];
|
|
16532
18142
|
v = item[1];
|
|
@@ -16547,7 +18157,7 @@ var TreeMap = class _TreeMap {
|
|
|
16547
18157
|
static createDefaultComparator() {
|
|
16548
18158
|
return (a, b) => {
|
|
16549
18159
|
if (typeof a === "number" && typeof b === "number") {
|
|
16550
|
-
if (Number.isNaN(a) || Number.isNaN(b))
|
|
18160
|
+
if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN("TreeMap"));
|
|
16551
18161
|
const aa = Object.is(a, -0) ? 0 : a;
|
|
16552
18162
|
const bb = Object.is(b, -0) ? 0 : b;
|
|
16553
18163
|
return aa > bb ? 1 : aa < bb ? -1 : 0;
|
|
@@ -16558,24 +18168,24 @@ var TreeMap = class _TreeMap {
|
|
|
16558
18168
|
if (a instanceof Date && b instanceof Date) {
|
|
16559
18169
|
const ta = a.getTime();
|
|
16560
18170
|
const tb = b.getTime();
|
|
16561
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
18171
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("TreeMap"));
|
|
16562
18172
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
16563
18173
|
}
|
|
16564
|
-
|
|
18174
|
+
raise(TypeError, ERR.comparatorRequired("TreeMap"));
|
|
16565
18175
|
};
|
|
16566
18176
|
}
|
|
16567
18177
|
_validateKey(key) {
|
|
16568
18178
|
if (!this.#isDefaultComparator) return;
|
|
16569
18179
|
if (typeof key === "number") {
|
|
16570
|
-
if (Number.isNaN(key))
|
|
18180
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeMap"));
|
|
16571
18181
|
return;
|
|
16572
18182
|
}
|
|
16573
18183
|
if (typeof key === "string") return;
|
|
16574
18184
|
if (key instanceof Date) {
|
|
16575
|
-
if (Number.isNaN(key.getTime()))
|
|
18185
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeMap"));
|
|
16576
18186
|
return;
|
|
16577
18187
|
}
|
|
16578
|
-
|
|
18188
|
+
raise(TypeError, ERR.comparatorRequired("TreeMap"));
|
|
16579
18189
|
}
|
|
16580
18190
|
/**
|
|
16581
18191
|
* Number of entries in the map.
|
|
@@ -16723,14 +18333,34 @@ var TreeMap = class _TreeMap {
|
|
|
16723
18333
|
|
|
16724
18334
|
|
|
16725
18335
|
|
|
16726
|
-
|
|
16727
|
-
|
|
16728
|
-
|
|
16729
|
-
|
|
16730
|
-
|
|
16731
|
-
|
|
16732
|
-
|
|
16733
|
-
|
|
18336
|
+
|
|
18337
|
+
|
|
18338
|
+
|
|
18339
|
+
|
|
18340
|
+
|
|
18341
|
+
|
|
18342
|
+
|
|
18343
|
+
|
|
18344
|
+
|
|
18345
|
+
|
|
18346
|
+
|
|
18347
|
+
|
|
18348
|
+
|
|
18349
|
+
|
|
18350
|
+
|
|
18351
|
+
|
|
18352
|
+
|
|
18353
|
+
|
|
18354
|
+
|
|
18355
|
+
|
|
18356
|
+
* @example
|
|
18357
|
+
* // Check empty
|
|
18358
|
+
* console.log(new TreeMap().isEmpty()); // true;
|
|
18359
|
+
*/
|
|
18360
|
+
isEmpty() {
|
|
18361
|
+
return this.size === 0;
|
|
18362
|
+
}
|
|
18363
|
+
/**
|
|
16734
18364
|
* Set or overwrite a value for a key.
|
|
16735
18365
|
* @remarks Expected time O(log n)
|
|
16736
18366
|
|
|
@@ -16859,6 +18489,26 @@ var TreeMap = class _TreeMap {
|
|
|
16859
18489
|
|
|
16860
18490
|
|
|
16861
18491
|
|
|
18492
|
+
|
|
18493
|
+
|
|
18494
|
+
|
|
18495
|
+
|
|
18496
|
+
|
|
18497
|
+
|
|
18498
|
+
|
|
18499
|
+
|
|
18500
|
+
|
|
18501
|
+
|
|
18502
|
+
|
|
18503
|
+
|
|
18504
|
+
|
|
18505
|
+
|
|
18506
|
+
|
|
18507
|
+
|
|
18508
|
+
|
|
18509
|
+
|
|
18510
|
+
|
|
18511
|
+
|
|
16862
18512
|
|
|
16863
18513
|
|
|
16864
18514
|
|
|
@@ -17037,6 +18687,26 @@ var TreeMap = class _TreeMap {
|
|
|
17037
18687
|
|
|
17038
18688
|
|
|
17039
18689
|
|
|
18690
|
+
|
|
18691
|
+
|
|
18692
|
+
|
|
18693
|
+
|
|
18694
|
+
|
|
18695
|
+
|
|
18696
|
+
|
|
18697
|
+
|
|
18698
|
+
|
|
18699
|
+
|
|
18700
|
+
|
|
18701
|
+
|
|
18702
|
+
|
|
18703
|
+
|
|
18704
|
+
|
|
18705
|
+
|
|
18706
|
+
|
|
18707
|
+
|
|
18708
|
+
|
|
18709
|
+
|
|
17040
18710
|
|
|
17041
18711
|
|
|
17042
18712
|
|
|
@@ -17205,6 +18875,26 @@ var TreeMap = class _TreeMap {
|
|
|
17205
18875
|
|
|
17206
18876
|
|
|
17207
18877
|
|
|
18878
|
+
|
|
18879
|
+
|
|
18880
|
+
|
|
18881
|
+
|
|
18882
|
+
|
|
18883
|
+
|
|
18884
|
+
|
|
18885
|
+
|
|
18886
|
+
|
|
18887
|
+
|
|
18888
|
+
|
|
18889
|
+
|
|
18890
|
+
|
|
18891
|
+
|
|
18892
|
+
|
|
18893
|
+
|
|
18894
|
+
|
|
18895
|
+
|
|
18896
|
+
|
|
18897
|
+
|
|
17208
18898
|
|
|
17209
18899
|
|
|
17210
18900
|
|
|
@@ -17373,6 +19063,26 @@ var TreeMap = class _TreeMap {
|
|
|
17373
19063
|
|
|
17374
19064
|
|
|
17375
19065
|
|
|
19066
|
+
|
|
19067
|
+
|
|
19068
|
+
|
|
19069
|
+
|
|
19070
|
+
|
|
19071
|
+
|
|
19072
|
+
|
|
19073
|
+
|
|
19074
|
+
|
|
19075
|
+
|
|
19076
|
+
|
|
19077
|
+
|
|
19078
|
+
|
|
19079
|
+
|
|
19080
|
+
|
|
19081
|
+
|
|
19082
|
+
|
|
19083
|
+
|
|
19084
|
+
|
|
19085
|
+
|
|
17376
19086
|
|
|
17377
19087
|
|
|
17378
19088
|
|
|
@@ -17531,6 +19241,26 @@ var TreeMap = class _TreeMap {
|
|
|
17531
19241
|
|
|
17532
19242
|
|
|
17533
19243
|
|
|
19244
|
+
|
|
19245
|
+
|
|
19246
|
+
|
|
19247
|
+
|
|
19248
|
+
|
|
19249
|
+
|
|
19250
|
+
|
|
19251
|
+
|
|
19252
|
+
|
|
19253
|
+
|
|
19254
|
+
|
|
19255
|
+
|
|
19256
|
+
|
|
19257
|
+
|
|
19258
|
+
|
|
19259
|
+
|
|
19260
|
+
|
|
19261
|
+
|
|
19262
|
+
|
|
19263
|
+
|
|
17534
19264
|
|
|
17535
19265
|
|
|
17536
19266
|
|
|
@@ -17680,6 +19410,26 @@ var TreeMap = class _TreeMap {
|
|
|
17680
19410
|
|
|
17681
19411
|
|
|
17682
19412
|
|
|
19413
|
+
|
|
19414
|
+
|
|
19415
|
+
|
|
19416
|
+
|
|
19417
|
+
|
|
19418
|
+
|
|
19419
|
+
|
|
19420
|
+
|
|
19421
|
+
|
|
19422
|
+
|
|
19423
|
+
|
|
19424
|
+
|
|
19425
|
+
|
|
19426
|
+
|
|
19427
|
+
|
|
19428
|
+
|
|
19429
|
+
|
|
19430
|
+
|
|
19431
|
+
|
|
19432
|
+
|
|
17683
19433
|
|
|
17684
19434
|
|
|
17685
19435
|
|
|
@@ -17833,6 +19583,26 @@ var TreeMap = class _TreeMap {
|
|
|
17833
19583
|
|
|
17834
19584
|
|
|
17835
19585
|
|
|
19586
|
+
|
|
19587
|
+
|
|
19588
|
+
|
|
19589
|
+
|
|
19590
|
+
|
|
19591
|
+
|
|
19592
|
+
|
|
19593
|
+
|
|
19594
|
+
|
|
19595
|
+
|
|
19596
|
+
|
|
19597
|
+
|
|
19598
|
+
|
|
19599
|
+
|
|
19600
|
+
|
|
19601
|
+
|
|
19602
|
+
|
|
19603
|
+
|
|
19604
|
+
|
|
19605
|
+
|
|
17836
19606
|
|
|
17837
19607
|
|
|
17838
19608
|
|
|
@@ -17983,6 +19753,26 @@ var TreeMap = class _TreeMap {
|
|
|
17983
19753
|
|
|
17984
19754
|
|
|
17985
19755
|
|
|
19756
|
+
|
|
19757
|
+
|
|
19758
|
+
|
|
19759
|
+
|
|
19760
|
+
|
|
19761
|
+
|
|
19762
|
+
|
|
19763
|
+
|
|
19764
|
+
|
|
19765
|
+
|
|
19766
|
+
|
|
19767
|
+
|
|
19768
|
+
|
|
19769
|
+
|
|
19770
|
+
|
|
19771
|
+
|
|
19772
|
+
|
|
19773
|
+
|
|
19774
|
+
|
|
19775
|
+
|
|
17986
19776
|
|
|
17987
19777
|
|
|
17988
19778
|
|
|
@@ -18136,6 +19926,26 @@ var TreeMap = class _TreeMap {
|
|
|
18136
19926
|
|
|
18137
19927
|
|
|
18138
19928
|
|
|
19929
|
+
|
|
19930
|
+
|
|
19931
|
+
|
|
19932
|
+
|
|
19933
|
+
|
|
19934
|
+
|
|
19935
|
+
|
|
19936
|
+
|
|
19937
|
+
|
|
19938
|
+
|
|
19939
|
+
|
|
19940
|
+
|
|
19941
|
+
|
|
19942
|
+
|
|
19943
|
+
|
|
19944
|
+
|
|
19945
|
+
|
|
19946
|
+
|
|
19947
|
+
|
|
19948
|
+
|
|
18139
19949
|
|
|
18140
19950
|
|
|
18141
19951
|
|
|
@@ -18289,6 +20099,26 @@ var TreeMap = class _TreeMap {
|
|
|
18289
20099
|
|
|
18290
20100
|
|
|
18291
20101
|
|
|
20102
|
+
|
|
20103
|
+
|
|
20104
|
+
|
|
20105
|
+
|
|
20106
|
+
|
|
20107
|
+
|
|
20108
|
+
|
|
20109
|
+
|
|
20110
|
+
|
|
20111
|
+
|
|
20112
|
+
|
|
20113
|
+
|
|
20114
|
+
|
|
20115
|
+
|
|
20116
|
+
|
|
20117
|
+
|
|
20118
|
+
|
|
20119
|
+
|
|
20120
|
+
|
|
20121
|
+
|
|
18292
20122
|
|
|
18293
20123
|
|
|
18294
20124
|
|
|
@@ -18445,6 +20275,26 @@ var TreeMap = class _TreeMap {
|
|
|
18445
20275
|
|
|
18446
20276
|
|
|
18447
20277
|
|
|
20278
|
+
|
|
20279
|
+
|
|
20280
|
+
|
|
20281
|
+
|
|
20282
|
+
|
|
20283
|
+
|
|
20284
|
+
|
|
20285
|
+
|
|
20286
|
+
|
|
20287
|
+
|
|
20288
|
+
|
|
20289
|
+
|
|
20290
|
+
|
|
20291
|
+
|
|
20292
|
+
|
|
20293
|
+
|
|
20294
|
+
|
|
20295
|
+
|
|
20296
|
+
|
|
20297
|
+
|
|
18448
20298
|
|
|
18449
20299
|
|
|
18450
20300
|
|
|
@@ -18622,14 +20472,34 @@ var TreeMap = class _TreeMap {
|
|
|
18622
20472
|
|
|
18623
20473
|
|
|
18624
20474
|
|
|
18625
|
-
|
|
18626
|
-
|
|
18627
|
-
|
|
18628
|
-
|
|
18629
|
-
|
|
18630
|
-
|
|
18631
|
-
|
|
18632
|
-
|
|
20475
|
+
|
|
20476
|
+
|
|
20477
|
+
|
|
20478
|
+
|
|
20479
|
+
|
|
20480
|
+
|
|
20481
|
+
|
|
20482
|
+
|
|
20483
|
+
|
|
20484
|
+
|
|
20485
|
+
|
|
20486
|
+
|
|
20487
|
+
|
|
20488
|
+
|
|
20489
|
+
|
|
20490
|
+
|
|
20491
|
+
|
|
20492
|
+
|
|
20493
|
+
|
|
20494
|
+
|
|
20495
|
+
* @example
|
|
20496
|
+
* // Aggregate values
|
|
20497
|
+
* const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
|
|
20498
|
+
* console.log(tm.reduce((acc, v) => acc + (v ?? 0), 0)); // 30;
|
|
20499
|
+
*/
|
|
20500
|
+
reduce(callbackfn, initialValue) {
|
|
20501
|
+
let acc = initialValue;
|
|
20502
|
+
let index = 0;
|
|
18633
20503
|
for (const [k, v] of this) acc = callbackfn(acc, v, k, index++, this);
|
|
18634
20504
|
return acc;
|
|
18635
20505
|
}
|
|
@@ -18751,6 +20621,26 @@ var TreeMap = class _TreeMap {
|
|
|
18751
20621
|
|
|
18752
20622
|
|
|
18753
20623
|
|
|
20624
|
+
|
|
20625
|
+
|
|
20626
|
+
|
|
20627
|
+
|
|
20628
|
+
|
|
20629
|
+
|
|
20630
|
+
|
|
20631
|
+
|
|
20632
|
+
|
|
20633
|
+
|
|
20634
|
+
|
|
20635
|
+
|
|
20636
|
+
|
|
20637
|
+
|
|
20638
|
+
|
|
20639
|
+
|
|
20640
|
+
|
|
20641
|
+
|
|
20642
|
+
|
|
20643
|
+
|
|
18754
20644
|
|
|
18755
20645
|
|
|
18756
20646
|
|
|
@@ -18903,6 +20793,26 @@ var TreeMap = class _TreeMap {
|
|
|
18903
20793
|
|
|
18904
20794
|
|
|
18905
20795
|
|
|
20796
|
+
|
|
20797
|
+
|
|
20798
|
+
|
|
20799
|
+
|
|
20800
|
+
|
|
20801
|
+
|
|
20802
|
+
|
|
20803
|
+
|
|
20804
|
+
|
|
20805
|
+
|
|
20806
|
+
|
|
20807
|
+
|
|
20808
|
+
|
|
20809
|
+
|
|
20810
|
+
|
|
20811
|
+
|
|
20812
|
+
|
|
20813
|
+
|
|
20814
|
+
|
|
20815
|
+
|
|
18906
20816
|
|
|
18907
20817
|
|
|
18908
20818
|
|
|
@@ -19056,6 +20966,26 @@ var TreeMap = class _TreeMap {
|
|
|
19056
20966
|
|
|
19057
20967
|
|
|
19058
20968
|
|
|
20969
|
+
|
|
20970
|
+
|
|
20971
|
+
|
|
20972
|
+
|
|
20973
|
+
|
|
20974
|
+
|
|
20975
|
+
|
|
20976
|
+
|
|
20977
|
+
|
|
20978
|
+
|
|
20979
|
+
|
|
20980
|
+
|
|
20981
|
+
|
|
20982
|
+
|
|
20983
|
+
|
|
20984
|
+
|
|
20985
|
+
|
|
20986
|
+
|
|
20987
|
+
|
|
20988
|
+
|
|
19059
20989
|
|
|
19060
20990
|
|
|
19061
20991
|
|
|
@@ -19210,6 +21140,26 @@ var TreeMap = class _TreeMap {
|
|
|
19210
21140
|
|
|
19211
21141
|
|
|
19212
21142
|
|
|
21143
|
+
|
|
21144
|
+
|
|
21145
|
+
|
|
21146
|
+
|
|
21147
|
+
|
|
21148
|
+
|
|
21149
|
+
|
|
21150
|
+
|
|
21151
|
+
|
|
21152
|
+
|
|
21153
|
+
|
|
21154
|
+
|
|
21155
|
+
|
|
21156
|
+
|
|
21157
|
+
|
|
21158
|
+
|
|
21159
|
+
|
|
21160
|
+
|
|
21161
|
+
|
|
21162
|
+
|
|
19213
21163
|
|
|
19214
21164
|
|
|
19215
21165
|
|
|
@@ -19359,6 +21309,26 @@ var TreeMap = class _TreeMap {
|
|
|
19359
21309
|
|
|
19360
21310
|
|
|
19361
21311
|
|
|
21312
|
+
|
|
21313
|
+
|
|
21314
|
+
|
|
21315
|
+
|
|
21316
|
+
|
|
21317
|
+
|
|
21318
|
+
|
|
21319
|
+
|
|
21320
|
+
|
|
21321
|
+
|
|
21322
|
+
|
|
21323
|
+
|
|
21324
|
+
|
|
21325
|
+
|
|
21326
|
+
|
|
21327
|
+
|
|
21328
|
+
|
|
21329
|
+
|
|
21330
|
+
|
|
21331
|
+
|
|
19362
21332
|
|
|
19363
21333
|
|
|
19364
21334
|
|
|
@@ -19418,6 +21388,10 @@ var TreeMap = class _TreeMap {
|
|
|
19418
21388
|
|
|
19419
21389
|
|
|
19420
21390
|
|
|
21391
|
+
|
|
21392
|
+
|
|
21393
|
+
|
|
21394
|
+
|
|
19421
21395
|
|
|
19422
21396
|
|
|
19423
21397
|
|
|
@@ -19482,6 +21456,10 @@ var TreeMap = class _TreeMap {
|
|
|
19482
21456
|
|
|
19483
21457
|
|
|
19484
21458
|
|
|
21459
|
+
|
|
21460
|
+
|
|
21461
|
+
|
|
21462
|
+
|
|
19485
21463
|
|
|
19486
21464
|
|
|
19487
21465
|
|
|
@@ -19530,6 +21508,10 @@ var TreeMap = class _TreeMap {
|
|
|
19530
21508
|
|
|
19531
21509
|
|
|
19532
21510
|
|
|
21511
|
+
|
|
21512
|
+
|
|
21513
|
+
|
|
21514
|
+
|
|
19533
21515
|
|
|
19534
21516
|
|
|
19535
21517
|
|
|
@@ -19582,6 +21564,10 @@ var TreeMap = class _TreeMap {
|
|
|
19582
21564
|
|
|
19583
21565
|
|
|
19584
21566
|
|
|
21567
|
+
|
|
21568
|
+
|
|
21569
|
+
|
|
21570
|
+
|
|
19585
21571
|
|
|
19586
21572
|
|
|
19587
21573
|
|
|
@@ -19709,6 +21695,22 @@ var TreeMap = class _TreeMap {
|
|
|
19709
21695
|
|
|
19710
21696
|
|
|
19711
21697
|
|
|
21698
|
+
|
|
21699
|
+
|
|
21700
|
+
|
|
21701
|
+
|
|
21702
|
+
|
|
21703
|
+
|
|
21704
|
+
|
|
21705
|
+
|
|
21706
|
+
|
|
21707
|
+
|
|
21708
|
+
|
|
21709
|
+
|
|
21710
|
+
|
|
21711
|
+
|
|
21712
|
+
|
|
21713
|
+
|
|
19712
21714
|
|
|
19713
21715
|
|
|
19714
21716
|
|
|
@@ -19866,6 +21868,22 @@ var TreeMap = class _TreeMap {
|
|
|
19866
21868
|
|
|
19867
21869
|
|
|
19868
21870
|
|
|
21871
|
+
|
|
21872
|
+
|
|
21873
|
+
|
|
21874
|
+
|
|
21875
|
+
|
|
21876
|
+
|
|
21877
|
+
|
|
21878
|
+
|
|
21879
|
+
|
|
21880
|
+
|
|
21881
|
+
|
|
21882
|
+
|
|
21883
|
+
|
|
21884
|
+
|
|
21885
|
+
|
|
21886
|
+
|
|
19869
21887
|
|
|
19870
21888
|
|
|
19871
21889
|
|
|
@@ -20007,6 +22025,22 @@ var TreeMap = class _TreeMap {
|
|
|
20007
22025
|
|
|
20008
22026
|
|
|
20009
22027
|
|
|
22028
|
+
|
|
22029
|
+
|
|
22030
|
+
|
|
22031
|
+
|
|
22032
|
+
|
|
22033
|
+
|
|
22034
|
+
|
|
22035
|
+
|
|
22036
|
+
|
|
22037
|
+
|
|
22038
|
+
|
|
22039
|
+
|
|
22040
|
+
|
|
22041
|
+
|
|
22042
|
+
|
|
22043
|
+
|
|
20010
22044
|
|
|
20011
22045
|
|
|
20012
22046
|
|
|
@@ -20148,6 +22182,22 @@ var TreeMap = class _TreeMap {
|
|
|
20148
22182
|
|
|
20149
22183
|
|
|
20150
22184
|
|
|
22185
|
+
|
|
22186
|
+
|
|
22187
|
+
|
|
22188
|
+
|
|
22189
|
+
|
|
22190
|
+
|
|
22191
|
+
|
|
22192
|
+
|
|
22193
|
+
|
|
22194
|
+
|
|
22195
|
+
|
|
22196
|
+
|
|
22197
|
+
|
|
22198
|
+
|
|
22199
|
+
|
|
22200
|
+
|
|
20151
22201
|
|
|
20152
22202
|
|
|
20153
22203
|
|
|
@@ -20290,6 +22340,22 @@ var TreeMap = class _TreeMap {
|
|
|
20290
22340
|
|
|
20291
22341
|
|
|
20292
22342
|
|
|
22343
|
+
|
|
22344
|
+
|
|
22345
|
+
|
|
22346
|
+
|
|
22347
|
+
|
|
22348
|
+
|
|
22349
|
+
|
|
22350
|
+
|
|
22351
|
+
|
|
22352
|
+
|
|
22353
|
+
|
|
22354
|
+
|
|
22355
|
+
|
|
22356
|
+
|
|
22357
|
+
|
|
22358
|
+
|
|
20293
22359
|
|
|
20294
22360
|
|
|
20295
22361
|
|
|
@@ -20354,6 +22420,68 @@ var TreeMap = class _TreeMap {
|
|
|
20354
22420
|
}
|
|
20355
22421
|
return out;
|
|
20356
22422
|
}
|
|
22423
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
22424
|
+
/**
|
|
22425
|
+
* Returns the entry at the k-th position in tree order (0-indexed).
|
|
22426
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
22427
|
+
|
|
22428
|
+
|
|
22429
|
+
|
|
22430
|
+
* @example
|
|
22431
|
+
* // Find k-th entry in a TreeMap
|
|
22432
|
+
* const map = new TreeMap<string, number>(
|
|
22433
|
+
* [['alice', 95], ['bob', 87], ['charlie', 92]],
|
|
22434
|
+
* { enableOrderStatistic: true }
|
|
22435
|
+
* );
|
|
22436
|
+
* console.log(map.getByRank(0)); // 'alice';
|
|
22437
|
+
* console.log(map.getByRank(1)); // 'bob';
|
|
22438
|
+
* console.log(map.getByRank(2)); // 'charlie';
|
|
22439
|
+
*/
|
|
22440
|
+
getByRank(k) {
|
|
22441
|
+
const key = this.#core.getByRank(k);
|
|
22442
|
+
if (key === void 0) return void 0;
|
|
22443
|
+
return [key, this.#core.get(key)];
|
|
22444
|
+
}
|
|
22445
|
+
/**
|
|
22446
|
+
* Returns the 0-based rank of a key (number of elements that precede it in tree order).
|
|
22447
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
22448
|
+
* @example
|
|
22449
|
+
* // Get the rank of a key in sorted order
|
|
22450
|
+
* const tree = new TreeMap<number>(
|
|
22451
|
+
* [10, 20, 30, 40, 50],
|
|
22452
|
+
* { enableOrderStatistic: true }
|
|
22453
|
+
* );
|
|
22454
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
22455
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
22456
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
22457
|
+
* console.log(tree.getRank(25)); // 2;
|
|
22458
|
+
*/
|
|
22459
|
+
getRank(key) {
|
|
22460
|
+
return this.#core.getRank(key);
|
|
22461
|
+
}
|
|
22462
|
+
/**
|
|
22463
|
+
* Returns keys by rank range (0-indexed, inclusive on both ends).
|
|
22464
|
+
* @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
|
|
22465
|
+
|
|
22466
|
+
* @example
|
|
22467
|
+
* // Pagination with rangeByRank
|
|
22468
|
+
* const tree = new TreeMap<number>(
|
|
22469
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
22470
|
+
* { enableOrderStatistic: true }
|
|
22471
|
+
* );
|
|
22472
|
+
* const pageSize = 3;
|
|
22473
|
+
*
|
|
22474
|
+
* // Page 1
|
|
22475
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
22476
|
+
* // Page 2
|
|
22477
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
22478
|
+
* // Page 3
|
|
22479
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
22480
|
+
*/
|
|
22481
|
+
rangeByRank(start, end) {
|
|
22482
|
+
const keys = this.#core.rangeByRank(start, end);
|
|
22483
|
+
return keys.filter((k) => k !== void 0).map((k) => [k, this.#core.get(k)]);
|
|
22484
|
+
}
|
|
20357
22485
|
/**
|
|
20358
22486
|
* Creates a shallow clone of this map.
|
|
20359
22487
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -20496,20 +22624,40 @@ var TreeMap = class _TreeMap {
|
|
|
20496
22624
|
|
|
20497
22625
|
|
|
20498
22626
|
|
|
20499
|
-
|
|
20500
|
-
|
|
20501
|
-
|
|
20502
|
-
|
|
20503
|
-
|
|
20504
|
-
|
|
20505
|
-
|
|
20506
|
-
|
|
20507
|
-
|
|
20508
|
-
|
|
20509
|
-
|
|
20510
|
-
|
|
20511
|
-
|
|
20512
|
-
|
|
22627
|
+
|
|
22628
|
+
|
|
22629
|
+
|
|
22630
|
+
|
|
22631
|
+
|
|
22632
|
+
|
|
22633
|
+
|
|
22634
|
+
|
|
22635
|
+
|
|
22636
|
+
|
|
22637
|
+
|
|
22638
|
+
|
|
22639
|
+
|
|
22640
|
+
|
|
22641
|
+
|
|
22642
|
+
|
|
22643
|
+
|
|
22644
|
+
|
|
22645
|
+
|
|
22646
|
+
|
|
22647
|
+
* @example
|
|
22648
|
+
* // Deep clone
|
|
22649
|
+
* const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
|
|
22650
|
+
* const copy = tm.clone();
|
|
22651
|
+
* copy.delete(1);
|
|
22652
|
+
* console.log(tm.has(1)); // true;
|
|
22653
|
+
*/
|
|
22654
|
+
clone() {
|
|
22655
|
+
return new _TreeMap(this, {
|
|
22656
|
+
comparator: this.#isDefaultComparator ? void 0 : this.#userComparator,
|
|
22657
|
+
isMapMode: this.#core.isMapMode
|
|
22658
|
+
});
|
|
22659
|
+
}
|
|
22660
|
+
};
|
|
20513
22661
|
|
|
20514
22662
|
// src/data-structures/binary-tree/tree-multi-set.ts
|
|
20515
22663
|
var TreeMultiSet = class _TreeMultiSet {
|
|
@@ -20537,7 +22685,7 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20537
22685
|
const toElementFn = options.toElementFn;
|
|
20538
22686
|
const comparator = options.comparator ?? TreeSet.createDefaultComparator();
|
|
20539
22687
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
20540
|
-
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode });
|
|
22688
|
+
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
20541
22689
|
for (const item of elements) {
|
|
20542
22690
|
const k = toElementFn ? toElementFn(item) : item;
|
|
20543
22691
|
this.add(k);
|
|
@@ -20550,22 +22698,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20550
22698
|
_validateKey(key) {
|
|
20551
22699
|
if (!this.#isDefaultComparator) return;
|
|
20552
22700
|
if (typeof key === "number") {
|
|
20553
|
-
if (Number.isNaN(key))
|
|
22701
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeMultiSet"));
|
|
20554
22702
|
return;
|
|
20555
22703
|
}
|
|
20556
22704
|
if (typeof key === "string") return;
|
|
20557
22705
|
if (key instanceof Date) {
|
|
20558
|
-
if (Number.isNaN(key.getTime()))
|
|
22706
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeMultiSet"));
|
|
20559
22707
|
return;
|
|
20560
22708
|
}
|
|
20561
|
-
|
|
22709
|
+
raise(TypeError, ERR.comparatorRequired("TreeMultiSet"));
|
|
20562
22710
|
}
|
|
20563
22711
|
/**
|
|
20564
22712
|
* Validates that count is a non-negative safe integer.
|
|
20565
22713
|
* @remarks Time O(1), Space O(1)
|
|
20566
22714
|
*/
|
|
20567
22715
|
_validateCount(n) {
|
|
20568
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
22716
|
+
if (!Number.isSafeInteger(n) || n < 0) raise(RangeError, ERR.invalidArgument("count must be a safe integer >= 0.", "TreeMultiSet"));
|
|
20569
22717
|
}
|
|
20570
22718
|
/**
|
|
20571
22719
|
* Total occurrences (sumCounts).
|
|
@@ -20594,6 +22742,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20594
22742
|
|
|
20595
22743
|
|
|
20596
22744
|
|
|
22745
|
+
|
|
22746
|
+
|
|
22747
|
+
|
|
22748
|
+
|
|
20597
22749
|
|
|
20598
22750
|
|
|
20599
22751
|
|
|
@@ -20729,6 +22881,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20729
22881
|
|
|
20730
22882
|
|
|
20731
22883
|
|
|
22884
|
+
|
|
22885
|
+
|
|
22886
|
+
|
|
22887
|
+
|
|
22888
|
+
|
|
22889
|
+
|
|
22890
|
+
|
|
22891
|
+
|
|
22892
|
+
|
|
22893
|
+
|
|
22894
|
+
|
|
22895
|
+
|
|
22896
|
+
|
|
22897
|
+
|
|
22898
|
+
|
|
22899
|
+
|
|
22900
|
+
|
|
22901
|
+
|
|
22902
|
+
|
|
22903
|
+
|
|
20732
22904
|
|
|
20733
22905
|
|
|
20734
22906
|
|
|
@@ -20880,6 +23052,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20880
23052
|
|
|
20881
23053
|
|
|
20882
23054
|
|
|
23055
|
+
|
|
23056
|
+
|
|
23057
|
+
|
|
23058
|
+
|
|
23059
|
+
|
|
23060
|
+
|
|
23061
|
+
|
|
23062
|
+
|
|
23063
|
+
|
|
23064
|
+
|
|
23065
|
+
|
|
23066
|
+
|
|
23067
|
+
|
|
23068
|
+
|
|
23069
|
+
|
|
23070
|
+
|
|
23071
|
+
|
|
23072
|
+
|
|
23073
|
+
|
|
23074
|
+
|
|
20883
23075
|
|
|
20884
23076
|
|
|
20885
23077
|
|
|
@@ -20932,6 +23124,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20932
23124
|
|
|
20933
23125
|
|
|
20934
23126
|
|
|
23127
|
+
|
|
23128
|
+
|
|
23129
|
+
|
|
23130
|
+
|
|
20935
23131
|
|
|
20936
23132
|
|
|
20937
23133
|
|
|
@@ -21062,6 +23258,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21062
23258
|
|
|
21063
23259
|
|
|
21064
23260
|
|
|
23261
|
+
|
|
23262
|
+
|
|
23263
|
+
|
|
23264
|
+
|
|
23265
|
+
|
|
23266
|
+
|
|
23267
|
+
|
|
23268
|
+
|
|
23269
|
+
|
|
23270
|
+
|
|
23271
|
+
|
|
23272
|
+
|
|
23273
|
+
|
|
23274
|
+
|
|
23275
|
+
|
|
23276
|
+
|
|
23277
|
+
|
|
23278
|
+
|
|
23279
|
+
|
|
23280
|
+
|
|
21065
23281
|
|
|
21066
23282
|
|
|
21067
23283
|
|
|
@@ -21123,6 +23339,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21123
23339
|
|
|
21124
23340
|
|
|
21125
23341
|
|
|
23342
|
+
|
|
23343
|
+
|
|
23344
|
+
|
|
23345
|
+
|
|
21126
23346
|
|
|
21127
23347
|
|
|
21128
23348
|
|
|
@@ -21271,6 +23491,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21271
23491
|
|
|
21272
23492
|
|
|
21273
23493
|
|
|
23494
|
+
|
|
23495
|
+
|
|
23496
|
+
|
|
23497
|
+
|
|
23498
|
+
|
|
23499
|
+
|
|
23500
|
+
|
|
23501
|
+
|
|
23502
|
+
|
|
23503
|
+
|
|
23504
|
+
|
|
23505
|
+
|
|
23506
|
+
|
|
23507
|
+
|
|
23508
|
+
|
|
23509
|
+
|
|
23510
|
+
|
|
23511
|
+
|
|
23512
|
+
|
|
23513
|
+
|
|
21274
23514
|
|
|
21275
23515
|
|
|
21276
23516
|
|
|
@@ -21333,6 +23573,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21333
23573
|
|
|
21334
23574
|
|
|
21335
23575
|
|
|
23576
|
+
|
|
23577
|
+
|
|
23578
|
+
|
|
23579
|
+
|
|
21336
23580
|
|
|
21337
23581
|
|
|
21338
23582
|
|
|
@@ -21373,6 +23617,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21373
23617
|
|
|
21374
23618
|
|
|
21375
23619
|
|
|
23620
|
+
|
|
23621
|
+
|
|
23622
|
+
|
|
23623
|
+
|
|
21376
23624
|
|
|
21377
23625
|
|
|
21378
23626
|
|
|
@@ -21508,6 +23756,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21508
23756
|
|
|
21509
23757
|
|
|
21510
23758
|
|
|
23759
|
+
|
|
23760
|
+
|
|
23761
|
+
|
|
23762
|
+
|
|
23763
|
+
|
|
23764
|
+
|
|
23765
|
+
|
|
23766
|
+
|
|
23767
|
+
|
|
23768
|
+
|
|
23769
|
+
|
|
23770
|
+
|
|
23771
|
+
|
|
23772
|
+
|
|
23773
|
+
|
|
23774
|
+
|
|
23775
|
+
|
|
23776
|
+
|
|
23777
|
+
|
|
23778
|
+
|
|
21511
23779
|
|
|
21512
23780
|
|
|
21513
23781
|
|
|
@@ -21669,6 +23937,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21669
23937
|
|
|
21670
23938
|
|
|
21671
23939
|
|
|
23940
|
+
|
|
23941
|
+
|
|
23942
|
+
|
|
23943
|
+
|
|
23944
|
+
|
|
23945
|
+
|
|
23946
|
+
|
|
23947
|
+
|
|
23948
|
+
|
|
23949
|
+
|
|
23950
|
+
|
|
23951
|
+
|
|
23952
|
+
|
|
23953
|
+
|
|
23954
|
+
|
|
23955
|
+
|
|
23956
|
+
|
|
23957
|
+
|
|
23958
|
+
|
|
23959
|
+
|
|
21672
23960
|
|
|
21673
23961
|
|
|
21674
23962
|
|
|
@@ -21720,6 +24008,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21720
24008
|
|
|
21721
24009
|
|
|
21722
24010
|
|
|
24011
|
+
|
|
24012
|
+
|
|
24013
|
+
|
|
24014
|
+
|
|
21723
24015
|
|
|
21724
24016
|
|
|
21725
24017
|
|
|
@@ -21755,6 +24047,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21755
24047
|
|
|
21756
24048
|
|
|
21757
24049
|
|
|
24050
|
+
|
|
24051
|
+
|
|
24052
|
+
|
|
24053
|
+
|
|
21758
24054
|
|
|
21759
24055
|
|
|
21760
24056
|
|
|
@@ -21899,6 +24195,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21899
24195
|
|
|
21900
24196
|
|
|
21901
24197
|
|
|
24198
|
+
|
|
24199
|
+
|
|
24200
|
+
|
|
24201
|
+
|
|
24202
|
+
|
|
24203
|
+
|
|
24204
|
+
|
|
24205
|
+
|
|
24206
|
+
|
|
24207
|
+
|
|
24208
|
+
|
|
24209
|
+
|
|
24210
|
+
|
|
24211
|
+
|
|
24212
|
+
|
|
24213
|
+
|
|
24214
|
+
|
|
24215
|
+
|
|
24216
|
+
|
|
24217
|
+
|
|
21902
24218
|
|
|
21903
24219
|
|
|
21904
24220
|
|
|
@@ -21953,6 +24269,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21953
24269
|
|
|
21954
24270
|
|
|
21955
24271
|
|
|
24272
|
+
|
|
24273
|
+
|
|
24274
|
+
|
|
24275
|
+
|
|
21956
24276
|
|
|
21957
24277
|
|
|
21958
24278
|
|
|
@@ -21989,6 +24309,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21989
24309
|
|
|
21990
24310
|
|
|
21991
24311
|
|
|
24312
|
+
|
|
24313
|
+
|
|
24314
|
+
|
|
24315
|
+
|
|
21992
24316
|
|
|
21993
24317
|
|
|
21994
24318
|
|
|
@@ -22025,6 +24349,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22025
24349
|
|
|
22026
24350
|
|
|
22027
24351
|
|
|
24352
|
+
|
|
24353
|
+
|
|
24354
|
+
|
|
24355
|
+
|
|
22028
24356
|
|
|
22029
24357
|
|
|
22030
24358
|
|
|
@@ -22065,6 +24393,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22065
24393
|
|
|
22066
24394
|
|
|
22067
24395
|
|
|
24396
|
+
|
|
24397
|
+
|
|
24398
|
+
|
|
24399
|
+
|
|
22068
24400
|
|
|
22069
24401
|
|
|
22070
24402
|
|
|
@@ -22179,6 +24511,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22179
24511
|
|
|
22180
24512
|
|
|
22181
24513
|
|
|
24514
|
+
|
|
24515
|
+
|
|
24516
|
+
|
|
24517
|
+
|
|
24518
|
+
|
|
24519
|
+
|
|
24520
|
+
|
|
24521
|
+
|
|
24522
|
+
|
|
24523
|
+
|
|
24524
|
+
|
|
24525
|
+
|
|
24526
|
+
|
|
24527
|
+
|
|
24528
|
+
|
|
24529
|
+
|
|
22182
24530
|
|
|
22183
24531
|
|
|
22184
24532
|
|
|
@@ -22304,6 +24652,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22304
24652
|
|
|
22305
24653
|
|
|
22306
24654
|
|
|
24655
|
+
|
|
24656
|
+
|
|
24657
|
+
|
|
24658
|
+
|
|
24659
|
+
|
|
24660
|
+
|
|
24661
|
+
|
|
24662
|
+
|
|
24663
|
+
|
|
24664
|
+
|
|
24665
|
+
|
|
24666
|
+
|
|
24667
|
+
|
|
24668
|
+
|
|
24669
|
+
|
|
24670
|
+
|
|
22307
24671
|
|
|
22308
24672
|
|
|
22309
24673
|
|
|
@@ -22429,6 +24793,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22429
24793
|
|
|
22430
24794
|
|
|
22431
24795
|
|
|
24796
|
+
|
|
24797
|
+
|
|
24798
|
+
|
|
24799
|
+
|
|
24800
|
+
|
|
24801
|
+
|
|
24802
|
+
|
|
24803
|
+
|
|
24804
|
+
|
|
24805
|
+
|
|
24806
|
+
|
|
24807
|
+
|
|
24808
|
+
|
|
24809
|
+
|
|
24810
|
+
|
|
24811
|
+
|
|
22432
24812
|
|
|
22433
24813
|
|
|
22434
24814
|
|
|
@@ -22553,6 +24933,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22553
24933
|
|
|
22554
24934
|
|
|
22555
24935
|
|
|
24936
|
+
|
|
24937
|
+
|
|
24938
|
+
|
|
24939
|
+
|
|
24940
|
+
|
|
24941
|
+
|
|
24942
|
+
|
|
24943
|
+
|
|
24944
|
+
|
|
24945
|
+
|
|
24946
|
+
|
|
24947
|
+
|
|
24948
|
+
|
|
24949
|
+
|
|
24950
|
+
|
|
24951
|
+
|
|
22556
24952
|
|
|
22557
24953
|
|
|
22558
24954
|
|
|
@@ -22703,6 +25099,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22703
25099
|
|
|
22704
25100
|
|
|
22705
25101
|
|
|
25102
|
+
|
|
25103
|
+
|
|
25104
|
+
|
|
25105
|
+
|
|
25106
|
+
|
|
25107
|
+
|
|
25108
|
+
|
|
25109
|
+
|
|
25110
|
+
|
|
25111
|
+
|
|
25112
|
+
|
|
25113
|
+
|
|
25114
|
+
|
|
25115
|
+
|
|
25116
|
+
|
|
25117
|
+
|
|
25118
|
+
|
|
25119
|
+
|
|
25120
|
+
|
|
25121
|
+
|
|
22706
25122
|
|
|
22707
25123
|
|
|
22708
25124
|
|
|
@@ -22859,6 +25275,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22859
25275
|
|
|
22860
25276
|
|
|
22861
25277
|
|
|
25278
|
+
|
|
25279
|
+
|
|
25280
|
+
|
|
25281
|
+
|
|
25282
|
+
|
|
25283
|
+
|
|
25284
|
+
|
|
25285
|
+
|
|
25286
|
+
|
|
25287
|
+
|
|
25288
|
+
|
|
25289
|
+
|
|
25290
|
+
|
|
25291
|
+
|
|
25292
|
+
|
|
25293
|
+
|
|
25294
|
+
|
|
25295
|
+
|
|
25296
|
+
|
|
25297
|
+
|
|
22862
25298
|
|
|
22863
25299
|
|
|
22864
25300
|
|
|
@@ -23022,6 +25458,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23022
25458
|
|
|
23023
25459
|
|
|
23024
25460
|
|
|
25461
|
+
|
|
25462
|
+
|
|
25463
|
+
|
|
25464
|
+
|
|
25465
|
+
|
|
25466
|
+
|
|
25467
|
+
|
|
25468
|
+
|
|
25469
|
+
|
|
25470
|
+
|
|
25471
|
+
|
|
25472
|
+
|
|
25473
|
+
|
|
25474
|
+
|
|
25475
|
+
|
|
25476
|
+
|
|
25477
|
+
|
|
25478
|
+
|
|
25479
|
+
|
|
25480
|
+
|
|
23025
25481
|
|
|
23026
25482
|
|
|
23027
25483
|
|
|
@@ -23180,6 +25636,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23180
25636
|
|
|
23181
25637
|
|
|
23182
25638
|
|
|
25639
|
+
|
|
25640
|
+
|
|
25641
|
+
|
|
25642
|
+
|
|
25643
|
+
|
|
25644
|
+
|
|
25645
|
+
|
|
25646
|
+
|
|
25647
|
+
|
|
25648
|
+
|
|
25649
|
+
|
|
25650
|
+
|
|
25651
|
+
|
|
25652
|
+
|
|
25653
|
+
|
|
25654
|
+
|
|
25655
|
+
|
|
25656
|
+
|
|
25657
|
+
|
|
25658
|
+
|
|
23183
25659
|
|
|
23184
25660
|
|
|
23185
25661
|
|
|
@@ -23359,6 +25835,78 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23359
25835
|
|
|
23360
25836
|
|
|
23361
25837
|
|
|
25838
|
+
|
|
25839
|
+
|
|
25840
|
+
|
|
25841
|
+
|
|
25842
|
+
|
|
25843
|
+
|
|
25844
|
+
|
|
25845
|
+
|
|
25846
|
+
|
|
25847
|
+
|
|
25848
|
+
|
|
25849
|
+
|
|
25850
|
+
|
|
25851
|
+
|
|
25852
|
+
|
|
25853
|
+
|
|
25854
|
+
|
|
25855
|
+
|
|
25856
|
+
|
|
25857
|
+
|
|
25858
|
+
|
|
25859
|
+
* @example
|
|
25860
|
+
* // Order-statistic on BST
|
|
25861
|
+
* const tree = new TreeMultiSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
25862
|
+
* console.log(tree.getByRank(0)); // 10;
|
|
25863
|
+
* console.log(tree.getByRank(4)); // 50;
|
|
25864
|
+
* console.log(tree.getRank(30)); // 2;
|
|
25865
|
+
*/
|
|
25866
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
25867
|
+
getByRank(k) {
|
|
25868
|
+
return this.#core.getByRank(k);
|
|
25869
|
+
}
|
|
25870
|
+
/**
|
|
25871
|
+
* Get the rank of a key in sorted order
|
|
25872
|
+
* @example
|
|
25873
|
+
* // Get the rank of a key in sorted order
|
|
25874
|
+
* const tree = new TreeMultiSet<number>(
|
|
25875
|
+
* [10, 20, 30, 40, 50],
|
|
25876
|
+
* { enableOrderStatistic: true }
|
|
25877
|
+
* );
|
|
25878
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
25879
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
25880
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
25881
|
+
* console.log(tree.getRank(25)); // 2;
|
|
25882
|
+
*/
|
|
25883
|
+
getRank(key) {
|
|
25884
|
+
return this.#core.getRank(key);
|
|
25885
|
+
}
|
|
25886
|
+
/**
|
|
25887
|
+
* Get elements by rank range
|
|
25888
|
+
|
|
25889
|
+
* @example
|
|
25890
|
+
* // Pagination with rangeByRank
|
|
25891
|
+
* const tree = new TreeMultiSet<number>(
|
|
25892
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
25893
|
+
* { enableOrderStatistic: true }
|
|
25894
|
+
* );
|
|
25895
|
+
* const pageSize = 3;
|
|
25896
|
+
*
|
|
25897
|
+
* // Page 1
|
|
25898
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
25899
|
+
* // Page 2
|
|
25900
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
25901
|
+
* // Page 3
|
|
25902
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
25903
|
+
*/
|
|
25904
|
+
rangeByRank(start, end) {
|
|
25905
|
+
return this.#core.rangeByRank(start, end).filter((k) => k !== void 0);
|
|
25906
|
+
}
|
|
25907
|
+
/**
|
|
25908
|
+
* Deep copy
|
|
25909
|
+
|
|
23362
25910
|
|
|
23363
25911
|
|
|
23364
25912
|
|
|
@@ -23477,6 +26025,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23477
26025
|
|
|
23478
26026
|
|
|
23479
26027
|
|
|
26028
|
+
|
|
26029
|
+
|
|
26030
|
+
|
|
26031
|
+
|
|
26032
|
+
|
|
26033
|
+
|
|
26034
|
+
|
|
26035
|
+
|
|
26036
|
+
|
|
26037
|
+
|
|
26038
|
+
|
|
26039
|
+
|
|
26040
|
+
|
|
26041
|
+
|
|
26042
|
+
|
|
26043
|
+
|
|
23480
26044
|
|
|
23481
26045
|
|
|
23482
26046
|
|
|
@@ -23628,6 +26192,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23628
26192
|
|
|
23629
26193
|
|
|
23630
26194
|
|
|
26195
|
+
|
|
26196
|
+
|
|
26197
|
+
|
|
26198
|
+
|
|
26199
|
+
|
|
26200
|
+
|
|
26201
|
+
|
|
26202
|
+
|
|
26203
|
+
|
|
26204
|
+
|
|
26205
|
+
|
|
26206
|
+
|
|
26207
|
+
|
|
26208
|
+
|
|
26209
|
+
|
|
26210
|
+
|
|
26211
|
+
|
|
26212
|
+
|
|
26213
|
+
|
|
26214
|
+
|
|
23631
26215
|
|
|
23632
26216
|
|
|
23633
26217
|
|