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/esm/binary-tree.mjs
CHANGED
|
@@ -57,6 +57,56 @@ function makeTrampoline(fn) {
|
|
|
57
57
|
}
|
|
58
58
|
__name(makeTrampoline, "makeTrampoline");
|
|
59
59
|
|
|
60
|
+
// src/common/error.ts
|
|
61
|
+
function raise(ErrorClass, message) {
|
|
62
|
+
throw new ErrorClass(message);
|
|
63
|
+
}
|
|
64
|
+
__name(raise, "raise");
|
|
65
|
+
var ERR = {
|
|
66
|
+
// Range / index
|
|
67
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
68
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
69
|
+
// Type / argument
|
|
70
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
71
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
72
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
73
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
74
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
75
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
76
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
77
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
78
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
79
|
+
// State / operation
|
|
80
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
81
|
+
// Matrix
|
|
82
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
83
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
84
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
85
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
86
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
87
|
+
// Order statistic
|
|
88
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/common/index.ts
|
|
92
|
+
var Range = class {
|
|
93
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
94
|
+
this.low = low;
|
|
95
|
+
this.high = high;
|
|
96
|
+
this.includeLow = includeLow;
|
|
97
|
+
this.includeHigh = includeHigh;
|
|
98
|
+
}
|
|
99
|
+
static {
|
|
100
|
+
__name(this, "Range");
|
|
101
|
+
}
|
|
102
|
+
// Determine whether a key is within the range
|
|
103
|
+
isInRange(key, comparator) {
|
|
104
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
105
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
106
|
+
return lowCheck && highCheck;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
60
110
|
// src/data-structures/base/iterable-element-base.ts
|
|
61
111
|
var IterableElementBase = class {
|
|
62
112
|
static {
|
|
@@ -75,7 +125,7 @@ var IterableElementBase = class {
|
|
|
75
125
|
if (options) {
|
|
76
126
|
const { toElementFn } = options;
|
|
77
127
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
78
|
-
else if (toElementFn)
|
|
128
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
79
129
|
}
|
|
80
130
|
}
|
|
81
131
|
/**
|
|
@@ -238,7 +288,7 @@ var IterableElementBase = class {
|
|
|
238
288
|
acc = initialValue;
|
|
239
289
|
} else {
|
|
240
290
|
const first = iter.next();
|
|
241
|
-
if (first.done)
|
|
291
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
242
292
|
acc = first.value;
|
|
243
293
|
index = 1;
|
|
244
294
|
}
|
|
@@ -473,50 +523,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
473
523
|
}
|
|
474
524
|
};
|
|
475
525
|
|
|
476
|
-
// src/common/error.ts
|
|
477
|
-
var ERR = {
|
|
478
|
-
// Range / index
|
|
479
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
480
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
481
|
-
// Type / argument
|
|
482
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
483
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
484
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
485
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
486
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
487
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
488
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
489
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
490
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
491
|
-
// State / operation
|
|
492
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
493
|
-
// Matrix
|
|
494
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
495
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
496
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
497
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
498
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
// src/common/index.ts
|
|
502
|
-
var Range = class {
|
|
503
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
504
|
-
this.low = low;
|
|
505
|
-
this.high = high;
|
|
506
|
-
this.includeLow = includeLow;
|
|
507
|
-
this.includeHigh = includeHigh;
|
|
508
|
-
}
|
|
509
|
-
static {
|
|
510
|
-
__name(this, "Range");
|
|
511
|
-
}
|
|
512
|
-
// Determine whether a key is within the range
|
|
513
|
-
isInRange(key, comparator) {
|
|
514
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
515
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
516
|
-
return lowCheck && highCheck;
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
|
|
520
526
|
// src/data-structures/base/iterable-entry-base.ts
|
|
521
527
|
var IterableEntryBase = class {
|
|
522
528
|
static {
|
|
@@ -785,6 +791,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
785
791
|
|
|
786
792
|
|
|
787
793
|
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
788
798
|
|
|
789
799
|
|
|
790
800
|
|
|
@@ -831,6 +841,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
831
841
|
|
|
832
842
|
|
|
833
843
|
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
834
848
|
|
|
835
849
|
|
|
836
850
|
|
|
@@ -893,6 +907,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
893
907
|
|
|
894
908
|
|
|
895
909
|
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
|
|
896
914
|
|
|
897
915
|
|
|
898
916
|
|
|
@@ -951,6 +969,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
951
969
|
|
|
952
970
|
|
|
953
971
|
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
|
|
954
976
|
|
|
955
977
|
|
|
956
978
|
|
|
@@ -1016,6 +1038,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1016
1038
|
|
|
1017
1039
|
|
|
1018
1040
|
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1019
1045
|
|
|
1020
1046
|
|
|
1021
1047
|
|
|
@@ -1071,6 +1097,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1071
1097
|
|
|
1072
1098
|
|
|
1073
1099
|
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1074
1104
|
|
|
1075
1105
|
|
|
1076
1106
|
|
|
@@ -1119,6 +1149,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1119
1149
|
|
|
1120
1150
|
|
|
1121
1151
|
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1122
1156
|
|
|
1123
1157
|
|
|
1124
1158
|
|
|
@@ -1208,6 +1242,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1208
1242
|
|
|
1209
1243
|
|
|
1210
1244
|
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1211
1249
|
|
|
1212
1250
|
|
|
1213
1251
|
|
|
@@ -1250,6 +1288,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1250
1288
|
|
|
1251
1289
|
|
|
1252
1290
|
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1253
1295
|
|
|
1254
1296
|
|
|
1255
1297
|
|
|
@@ -1315,6 +1357,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1315
1357
|
|
|
1316
1358
|
|
|
1317
1359
|
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1318
1364
|
|
|
1319
1365
|
|
|
1320
1366
|
|
|
@@ -1364,6 +1410,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1364
1410
|
|
|
1365
1411
|
|
|
1366
1412
|
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1367
1417
|
|
|
1368
1418
|
|
|
1369
1419
|
|
|
@@ -1417,6 +1467,10 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1417
1467
|
|
|
1418
1468
|
|
|
1419
1469
|
|
|
1470
|
+
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
|
|
1420
1474
|
|
|
1421
1475
|
|
|
1422
1476
|
|
|
@@ -1672,7 +1726,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1672
1726
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1673
1727
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1674
1728
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1675
|
-
else if (toEntryFn)
|
|
1729
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1676
1730
|
}
|
|
1677
1731
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1678
1732
|
}
|
|
@@ -1913,6 +1967,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1913
1967
|
|
|
1914
1968
|
|
|
1915
1969
|
|
|
1970
|
+
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
|
|
1916
1974
|
|
|
1917
1975
|
|
|
1918
1976
|
|
|
@@ -1963,6 +2021,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1963
2021
|
|
|
1964
2022
|
|
|
1965
2023
|
|
|
2024
|
+
|
|
2025
|
+
|
|
2026
|
+
|
|
2027
|
+
|
|
1966
2028
|
|
|
1967
2029
|
|
|
1968
2030
|
|
|
@@ -2065,6 +2127,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2065
2127
|
|
|
2066
2128
|
|
|
2067
2129
|
|
|
2130
|
+
|
|
2131
|
+
|
|
2132
|
+
|
|
2133
|
+
|
|
2068
2134
|
|
|
2069
2135
|
|
|
2070
2136
|
|
|
@@ -2103,6 +2169,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2103
2169
|
|
|
2104
2170
|
|
|
2105
2171
|
|
|
2172
|
+
|
|
2173
|
+
|
|
2174
|
+
|
|
2175
|
+
|
|
2106
2176
|
|
|
2107
2177
|
|
|
2108
2178
|
|
|
@@ -2162,6 +2232,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2162
2232
|
|
|
2163
2233
|
|
|
2164
2234
|
|
|
2235
|
+
|
|
2236
|
+
|
|
2237
|
+
|
|
2238
|
+
|
|
2165
2239
|
|
|
2166
2240
|
|
|
2167
2241
|
|
|
@@ -2220,6 +2294,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2220
2294
|
|
|
2221
2295
|
|
|
2222
2296
|
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
|
|
2300
|
+
|
|
2223
2301
|
|
|
2224
2302
|
|
|
2225
2303
|
|
|
@@ -2356,6 +2434,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2356
2434
|
|
|
2357
2435
|
|
|
2358
2436
|
|
|
2437
|
+
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
|
|
2359
2441
|
|
|
2360
2442
|
|
|
2361
2443
|
|
|
@@ -2410,6 +2492,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2410
2492
|
|
|
2411
2493
|
|
|
2412
2494
|
|
|
2495
|
+
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
|
|
2413
2499
|
|
|
2414
2500
|
|
|
2415
2501
|
|
|
@@ -2466,6 +2552,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2466
2552
|
|
|
2467
2553
|
|
|
2468
2554
|
|
|
2555
|
+
|
|
2556
|
+
|
|
2557
|
+
|
|
2558
|
+
|
|
2469
2559
|
|
|
2470
2560
|
|
|
2471
2561
|
|
|
@@ -2510,6 +2600,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2510
2600
|
|
|
2511
2601
|
|
|
2512
2602
|
|
|
2603
|
+
|
|
2604
|
+
|
|
2605
|
+
|
|
2606
|
+
|
|
2513
2607
|
|
|
2514
2608
|
|
|
2515
2609
|
|
|
@@ -2563,6 +2657,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2563
2657
|
|
|
2564
2658
|
|
|
2565
2659
|
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
|
|
2566
2664
|
|
|
2567
2665
|
|
|
2568
2666
|
|
|
@@ -2643,6 +2741,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2643
2741
|
|
|
2644
2742
|
|
|
2645
2743
|
|
|
2744
|
+
|
|
2745
|
+
|
|
2746
|
+
|
|
2747
|
+
|
|
2646
2748
|
|
|
2647
2749
|
|
|
2648
2750
|
|
|
@@ -2700,6 +2802,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2700
2802
|
|
|
2701
2803
|
|
|
2702
2804
|
|
|
2805
|
+
|
|
2806
|
+
|
|
2807
|
+
|
|
2808
|
+
|
|
2703
2809
|
|
|
2704
2810
|
|
|
2705
2811
|
|
|
@@ -3173,6 +3279,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3173
3279
|
|
|
3174
3280
|
|
|
3175
3281
|
|
|
3282
|
+
|
|
3283
|
+
|
|
3284
|
+
|
|
3285
|
+
|
|
3176
3286
|
|
|
3177
3287
|
|
|
3178
3288
|
|
|
@@ -3221,6 +3331,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3221
3331
|
|
|
3222
3332
|
|
|
3223
3333
|
|
|
3334
|
+
|
|
3335
|
+
|
|
3336
|
+
|
|
3337
|
+
|
|
3224
3338
|
|
|
3225
3339
|
|
|
3226
3340
|
|
|
@@ -3273,6 +3387,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3273
3387
|
|
|
3274
3388
|
|
|
3275
3389
|
|
|
3390
|
+
|
|
3391
|
+
|
|
3392
|
+
|
|
3393
|
+
|
|
3276
3394
|
|
|
3277
3395
|
|
|
3278
3396
|
|
|
@@ -3350,6 +3468,10 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
3350
3468
|
|
|
3351
3469
|
|
|
3352
3470
|
|
|
3471
|
+
|
|
3472
|
+
|
|
3473
|
+
|
|
3474
|
+
|
|
3353
3475
|
|
|
3354
3476
|
|
|
3355
3477
|
|
|
@@ -3986,12 +4108,16 @@ var BST = class extends BinaryTree {
|
|
|
3986
4108
|
} else {
|
|
3987
4109
|
this._comparator = this._createDefaultComparator();
|
|
3988
4110
|
}
|
|
4111
|
+
if (options.enableOrderStatistic) {
|
|
4112
|
+
this._enableOrderStatistic = true;
|
|
4113
|
+
}
|
|
3989
4114
|
} else {
|
|
3990
4115
|
this._comparator = this._createDefaultComparator();
|
|
3991
4116
|
}
|
|
3992
4117
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
3993
4118
|
}
|
|
3994
4119
|
_root = void 0;
|
|
4120
|
+
_enableOrderStatistic = false;
|
|
3995
4121
|
/**
|
|
3996
4122
|
* Gets the root node of the tree.
|
|
3997
4123
|
* @remarks Time O(1)
|
|
@@ -4157,6 +4283,14 @@ var BST = class extends BinaryTree {
|
|
|
4157
4283
|
|
|
4158
4284
|
|
|
4159
4285
|
|
|
4286
|
+
|
|
4287
|
+
|
|
4288
|
+
|
|
4289
|
+
|
|
4290
|
+
|
|
4291
|
+
|
|
4292
|
+
|
|
4293
|
+
|
|
4160
4294
|
|
|
4161
4295
|
|
|
4162
4296
|
|
|
@@ -4320,6 +4454,84 @@ var BST = class extends BinaryTree {
|
|
|
4320
4454
|
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
4321
4455
|
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
4322
4456
|
}
|
|
4457
|
+
getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4458
|
+
if (!this._enableOrderStatistic) {
|
|
4459
|
+
raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
|
|
4460
|
+
}
|
|
4461
|
+
if (k < 0 || k >= this._size) return void 0;
|
|
4462
|
+
let actualCallback = void 0;
|
|
4463
|
+
let actualIterationType = this.iterationType;
|
|
4464
|
+
if (typeof callback === "string") {
|
|
4465
|
+
actualIterationType = callback;
|
|
4466
|
+
} else if (callback) {
|
|
4467
|
+
actualCallback = callback;
|
|
4468
|
+
if (iterationType) {
|
|
4469
|
+
actualIterationType = iterationType;
|
|
4470
|
+
}
|
|
4471
|
+
}
|
|
4472
|
+
const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
|
|
4473
|
+
if (!node) return void 0;
|
|
4474
|
+
return actualCallback ? actualCallback(node) : node.key;
|
|
4475
|
+
}
|
|
4476
|
+
getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
4477
|
+
if (!this._enableOrderStatistic) {
|
|
4478
|
+
raise(Error, ERR.orderStatisticNotEnabled("getRank"));
|
|
4479
|
+
}
|
|
4480
|
+
if (!this._root || this._size === 0) return -1;
|
|
4481
|
+
let actualIterationType = this.iterationType;
|
|
4482
|
+
if (iterationType) actualIterationType = iterationType;
|
|
4483
|
+
let key;
|
|
4484
|
+
if (typeof keyNodeEntryOrPredicate === "function") {
|
|
4485
|
+
const results = this.search(keyNodeEntryOrPredicate, true);
|
|
4486
|
+
if (results.length === 0 || results[0] === void 0) return -1;
|
|
4487
|
+
key = results[0];
|
|
4488
|
+
} else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
4489
|
+
return -1;
|
|
4490
|
+
} else if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
4491
|
+
key = keyNodeEntryOrPredicate.key;
|
|
4492
|
+
} else if (Array.isArray(keyNodeEntryOrPredicate)) {
|
|
4493
|
+
key = keyNodeEntryOrPredicate[0] ?? void 0;
|
|
4494
|
+
if (key === void 0 || key === null) return -1;
|
|
4495
|
+
} else {
|
|
4496
|
+
key = keyNodeEntryOrPredicate;
|
|
4497
|
+
}
|
|
4498
|
+
if (key === void 0) return -1;
|
|
4499
|
+
return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
|
|
4500
|
+
}
|
|
4501
|
+
rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4502
|
+
if (!this._enableOrderStatistic) {
|
|
4503
|
+
raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
|
|
4504
|
+
}
|
|
4505
|
+
if (this._size === 0) return [];
|
|
4506
|
+
const lo = Math.max(0, start);
|
|
4507
|
+
const hi = Math.min(this._size - 1, end);
|
|
4508
|
+
if (lo > hi) return [];
|
|
4509
|
+
let actualCallback = void 0;
|
|
4510
|
+
let actualIterationType = this.iterationType;
|
|
4511
|
+
if (typeof callback === "string") {
|
|
4512
|
+
actualIterationType = callback;
|
|
4513
|
+
} else if (callback) {
|
|
4514
|
+
actualCallback = callback;
|
|
4515
|
+
if (iterationType) {
|
|
4516
|
+
actualIterationType = iterationType;
|
|
4517
|
+
}
|
|
4518
|
+
}
|
|
4519
|
+
const results = [];
|
|
4520
|
+
const count = hi - lo + 1;
|
|
4521
|
+
const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
|
|
4522
|
+
if (!startNode) return [];
|
|
4523
|
+
let collected = 0;
|
|
4524
|
+
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
|
|
4525
|
+
let current = startNode;
|
|
4526
|
+
while (current && collected < count) {
|
|
4527
|
+
results.push(cb(current));
|
|
4528
|
+
collected++;
|
|
4529
|
+
if (collected < count) {
|
|
4530
|
+
current = this._next(current);
|
|
4531
|
+
}
|
|
4532
|
+
}
|
|
4533
|
+
return results;
|
|
4534
|
+
}
|
|
4323
4535
|
/**
|
|
4324
4536
|
* Adds a new node to the BST based on key comparison.
|
|
4325
4537
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -4412,20 +4624,33 @@ var BST = class extends BinaryTree {
|
|
|
4412
4624
|
|
|
4413
4625
|
|
|
4414
4626
|
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4627
|
+
|
|
4628
|
+
|
|
4629
|
+
|
|
4630
|
+
|
|
4631
|
+
|
|
4632
|
+
|
|
4633
|
+
|
|
4634
|
+
|
|
4635
|
+
|
|
4636
|
+
|
|
4637
|
+
|
|
4638
|
+
|
|
4639
|
+
* @example
|
|
4640
|
+
* // Set a key-value pair
|
|
4641
|
+
* const bst = new BST<number, string>();
|
|
4642
|
+
* bst.set(1, 'one');
|
|
4643
|
+
* bst.set(2, 'two');
|
|
4644
|
+
* console.log(bst.get(1)); // 'one';
|
|
4645
|
+
*/
|
|
4646
|
+
set(keyNodeOrEntry, value) {
|
|
4647
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
4648
|
+
if (newNode === void 0) return false;
|
|
4425
4649
|
if (this._root === void 0) {
|
|
4426
4650
|
this._setRoot(newNode);
|
|
4427
4651
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4428
4652
|
this._size++;
|
|
4653
|
+
this._updateCount(newNode);
|
|
4429
4654
|
return true;
|
|
4430
4655
|
}
|
|
4431
4656
|
let current = this._root;
|
|
@@ -4439,6 +4664,7 @@ var BST = class extends BinaryTree {
|
|
|
4439
4664
|
current.left = newNode;
|
|
4440
4665
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4441
4666
|
this._size++;
|
|
4667
|
+
this._updateCountAlongPath(newNode);
|
|
4442
4668
|
return true;
|
|
4443
4669
|
}
|
|
4444
4670
|
if (current.left !== null) current = current.left;
|
|
@@ -4447,6 +4673,7 @@ var BST = class extends BinaryTree {
|
|
|
4447
4673
|
current.right = newNode;
|
|
4448
4674
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
4449
4675
|
this._size++;
|
|
4676
|
+
this._updateCountAlongPath(newNode);
|
|
4450
4677
|
return true;
|
|
4451
4678
|
}
|
|
4452
4679
|
if (current.right !== null) current = current.right;
|
|
@@ -4507,6 +4734,14 @@ var BST = class extends BinaryTree {
|
|
|
4507
4734
|
|
|
4508
4735
|
|
|
4509
4736
|
|
|
4737
|
+
|
|
4738
|
+
|
|
4739
|
+
|
|
4740
|
+
|
|
4741
|
+
|
|
4742
|
+
|
|
4743
|
+
|
|
4744
|
+
|
|
4510
4745
|
|
|
4511
4746
|
|
|
4512
4747
|
|
|
@@ -4794,6 +5029,10 @@ var BST = class extends BinaryTree {
|
|
|
4794
5029
|
|
|
4795
5030
|
|
|
4796
5031
|
|
|
5032
|
+
|
|
5033
|
+
|
|
5034
|
+
|
|
5035
|
+
|
|
4797
5036
|
|
|
4798
5037
|
|
|
4799
5038
|
|
|
@@ -4859,6 +5098,10 @@ var BST = class extends BinaryTree {
|
|
|
4859
5098
|
|
|
4860
5099
|
|
|
4861
5100
|
|
|
5101
|
+
|
|
5102
|
+
|
|
5103
|
+
|
|
5104
|
+
|
|
4862
5105
|
|
|
4863
5106
|
|
|
4864
5107
|
|
|
@@ -4969,6 +5212,14 @@ var BST = class extends BinaryTree {
|
|
|
4969
5212
|
|
|
4970
5213
|
|
|
4971
5214
|
|
|
5215
|
+
|
|
5216
|
+
|
|
5217
|
+
|
|
5218
|
+
|
|
5219
|
+
|
|
5220
|
+
|
|
5221
|
+
|
|
5222
|
+
|
|
4972
5223
|
|
|
4973
5224
|
|
|
4974
5225
|
|
|
@@ -5052,13 +5303,11 @@ var BST = class extends BinaryTree {
|
|
|
5052
5303
|
if (a instanceof Date && b instanceof Date) {
|
|
5053
5304
|
const ta = a.getTime();
|
|
5054
5305
|
const tb = b.getTime();
|
|
5055
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
5306
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
|
|
5056
5307
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
5057
5308
|
}
|
|
5058
5309
|
if (typeof a === "object" || typeof b === "object") {
|
|
5059
|
-
|
|
5060
|
-
ERR.comparatorRequired("BST")
|
|
5061
|
-
);
|
|
5310
|
+
raise(TypeError, ERR.comparatorRequired("BST"));
|
|
5062
5311
|
}
|
|
5063
5312
|
return 0;
|
|
5064
5313
|
};
|
|
@@ -5377,7 +5626,8 @@ var BST = class extends BinaryTree {
|
|
|
5377
5626
|
_snapshotOptions() {
|
|
5378
5627
|
return {
|
|
5379
5628
|
...super._snapshotOptions(),
|
|
5380
|
-
comparator: this._comparator
|
|
5629
|
+
comparator: this._comparator,
|
|
5630
|
+
enableOrderStatistic: this._enableOrderStatistic
|
|
5381
5631
|
};
|
|
5382
5632
|
}
|
|
5383
5633
|
/**
|
|
@@ -5399,6 +5649,113 @@ var BST = class extends BinaryTree {
|
|
|
5399
5649
|
*
|
|
5400
5650
|
* @param v - The node to set as root.
|
|
5401
5651
|
*/
|
|
5652
|
+
/**
|
|
5653
|
+
* (Protected) Recalculates the subtree count for a single node.
|
|
5654
|
+
* @remarks Time O(1). Only active when enableOrderStatistic is true.
|
|
5655
|
+
*/
|
|
5656
|
+
_updateCount(node) {
|
|
5657
|
+
if (!this._enableOrderStatistic) return;
|
|
5658
|
+
node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
5659
|
+
}
|
|
5660
|
+
/**
|
|
5661
|
+
* (Protected) Updates subtree counts from a node up to the root.
|
|
5662
|
+
* @remarks Time O(log n). Only active when enableOrderStatistic is true.
|
|
5663
|
+
*/
|
|
5664
|
+
_updateCountAlongPath(node) {
|
|
5665
|
+
if (!this._enableOrderStatistic) return;
|
|
5666
|
+
let current = node;
|
|
5667
|
+
while (current) {
|
|
5668
|
+
this._updateCount(current);
|
|
5669
|
+
current = current.parent;
|
|
5670
|
+
}
|
|
5671
|
+
}
|
|
5672
|
+
/**
|
|
5673
|
+
* (Protected) Finds the node at position k in tree order (iterative).
|
|
5674
|
+
* @remarks Time O(log n), Space O(1)
|
|
5675
|
+
*/
|
|
5676
|
+
_getByRankIterative(node, k) {
|
|
5677
|
+
let current = node;
|
|
5678
|
+
let remaining = k;
|
|
5679
|
+
while (current) {
|
|
5680
|
+
const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
|
|
5681
|
+
if (remaining < leftCount) {
|
|
5682
|
+
current = current.left;
|
|
5683
|
+
} else if (remaining === leftCount) {
|
|
5684
|
+
return current;
|
|
5685
|
+
} else {
|
|
5686
|
+
remaining = remaining - leftCount - 1;
|
|
5687
|
+
current = current.right;
|
|
5688
|
+
}
|
|
5689
|
+
}
|
|
5690
|
+
return void 0;
|
|
5691
|
+
}
|
|
5692
|
+
/**
|
|
5693
|
+
* (Protected) Finds the node at position k in tree order (recursive).
|
|
5694
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5695
|
+
*/
|
|
5696
|
+
_getByRankRecursive(node, k) {
|
|
5697
|
+
if (!node) return void 0;
|
|
5698
|
+
const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
|
|
5699
|
+
if (k < leftCount) return this._getByRankRecursive(node.left, k);
|
|
5700
|
+
if (k === leftCount) return node;
|
|
5701
|
+
return this._getByRankRecursive(node.right, k - leftCount - 1);
|
|
5702
|
+
}
|
|
5703
|
+
/**
|
|
5704
|
+
* (Protected) Computes the rank of a key iteratively.
|
|
5705
|
+
* @remarks Time O(log n), Space O(1)
|
|
5706
|
+
*/
|
|
5707
|
+
_getRankIterative(node, key) {
|
|
5708
|
+
let rank = 0;
|
|
5709
|
+
let current = node;
|
|
5710
|
+
while (this.isRealNode(current)) {
|
|
5711
|
+
const cmp = this._compare(current.key, key);
|
|
5712
|
+
if (cmp > 0) {
|
|
5713
|
+
current = current.left;
|
|
5714
|
+
} else if (cmp < 0) {
|
|
5715
|
+
rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
|
|
5716
|
+
current = current.right;
|
|
5717
|
+
} else {
|
|
5718
|
+
rank += this.isRealNode(current.left) ? current.left._count : 0;
|
|
5719
|
+
return rank;
|
|
5720
|
+
}
|
|
5721
|
+
}
|
|
5722
|
+
return rank;
|
|
5723
|
+
}
|
|
5724
|
+
/**
|
|
5725
|
+
* (Protected) Computes the rank of a key recursively.
|
|
5726
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5727
|
+
*/
|
|
5728
|
+
_getRankRecursive(node, key) {
|
|
5729
|
+
if (!node) return 0;
|
|
5730
|
+
const cmp = this._compare(node.key, key);
|
|
5731
|
+
if (cmp > 0) {
|
|
5732
|
+
return this._getRankRecursive(node.left, key);
|
|
5733
|
+
} else if (cmp < 0) {
|
|
5734
|
+
return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
|
|
5735
|
+
} else {
|
|
5736
|
+
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
5737
|
+
}
|
|
5738
|
+
}
|
|
5739
|
+
/**
|
|
5740
|
+
* (Protected) Finds the in-order successor of a node.
|
|
5741
|
+
* @remarks Time O(log n), Space O(1)
|
|
5742
|
+
*/
|
|
5743
|
+
_next(node) {
|
|
5744
|
+
if (this.isRealNode(node.right)) {
|
|
5745
|
+
let current2 = node.right;
|
|
5746
|
+
while (this.isRealNode(current2.left)) {
|
|
5747
|
+
current2 = current2.left;
|
|
5748
|
+
}
|
|
5749
|
+
return current2;
|
|
5750
|
+
}
|
|
5751
|
+
let current = node;
|
|
5752
|
+
let parent = current.parent;
|
|
5753
|
+
while (parent && current === parent.right) {
|
|
5754
|
+
current = parent;
|
|
5755
|
+
parent = parent.parent;
|
|
5756
|
+
}
|
|
5757
|
+
return parent;
|
|
5758
|
+
}
|
|
5402
5759
|
_setRoot(v) {
|
|
5403
5760
|
if (v) v.parent = void 0;
|
|
5404
5761
|
this._root = v;
|
|
@@ -5445,21 +5802,28 @@ var BST = class extends BinaryTree {
|
|
|
5445
5802
|
while (x.left !== void 0 && x.left !== null) x = x.left;
|
|
5446
5803
|
return x;
|
|
5447
5804
|
}, "minNode");
|
|
5805
|
+
let countUpdateStart;
|
|
5448
5806
|
if (node.left === void 0) {
|
|
5807
|
+
countUpdateStart = node.parent;
|
|
5449
5808
|
transplant(node, node.right);
|
|
5450
5809
|
} else if (node.right === void 0) {
|
|
5810
|
+
countUpdateStart = node.parent;
|
|
5451
5811
|
transplant(node, node.left);
|
|
5452
5812
|
} else {
|
|
5453
5813
|
const succ = minNode(node.right);
|
|
5454
5814
|
if (succ.parent !== node) {
|
|
5815
|
+
countUpdateStart = succ.parent;
|
|
5455
5816
|
transplant(succ, succ.right);
|
|
5456
5817
|
succ.right = node.right;
|
|
5457
5818
|
if (succ.right) succ.right.parent = succ;
|
|
5819
|
+
} else {
|
|
5820
|
+
countUpdateStart = succ;
|
|
5458
5821
|
}
|
|
5459
5822
|
transplant(node, succ);
|
|
5460
5823
|
succ.left = node.left;
|
|
5461
5824
|
if (succ.left) succ.left.parent = succ;
|
|
5462
5825
|
}
|
|
5826
|
+
this._updateCountAlongPath(countUpdateStart);
|
|
5463
5827
|
this._size = Math.max(0, this._size - 1);
|
|
5464
5828
|
return true;
|
|
5465
5829
|
}
|
|
@@ -5486,7 +5850,7 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5486
5850
|
}
|
|
5487
5851
|
} else {
|
|
5488
5852
|
if (!Number.isInteger(sizeOrElements) || sizeOrElements < 0) {
|
|
5489
|
-
|
|
5853
|
+
raise(RangeError, ERR.invalidArgument("size must be a non-negative integer", "BinaryIndexedTree"));
|
|
5490
5854
|
}
|
|
5491
5855
|
this._size = sizeOrElements;
|
|
5492
5856
|
this._tree = new Array(this._size + 1).fill(0);
|
|
@@ -5541,6 +5905,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5541
5905
|
|
|
5542
5906
|
|
|
5543
5907
|
|
|
5908
|
+
|
|
5909
|
+
|
|
5910
|
+
|
|
5911
|
+
|
|
5912
|
+
|
|
5913
|
+
|
|
5914
|
+
|
|
5915
|
+
|
|
5544
5916
|
|
|
5545
5917
|
|
|
5546
5918
|
|
|
@@ -5609,6 +5981,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5609
5981
|
|
|
5610
5982
|
|
|
5611
5983
|
|
|
5984
|
+
|
|
5985
|
+
|
|
5986
|
+
|
|
5987
|
+
|
|
5988
|
+
|
|
5989
|
+
|
|
5990
|
+
|
|
5991
|
+
|
|
5612
5992
|
|
|
5613
5993
|
|
|
5614
5994
|
|
|
@@ -5677,6 +6057,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5677
6057
|
|
|
5678
6058
|
|
|
5679
6059
|
|
|
6060
|
+
|
|
6061
|
+
|
|
6062
|
+
|
|
6063
|
+
|
|
6064
|
+
|
|
6065
|
+
|
|
6066
|
+
|
|
6067
|
+
|
|
5680
6068
|
|
|
5681
6069
|
|
|
5682
6070
|
|
|
@@ -5745,6 +6133,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5745
6133
|
|
|
5746
6134
|
|
|
5747
6135
|
|
|
6136
|
+
|
|
6137
|
+
|
|
6138
|
+
|
|
6139
|
+
|
|
6140
|
+
|
|
6141
|
+
|
|
6142
|
+
|
|
6143
|
+
|
|
5748
6144
|
|
|
5749
6145
|
|
|
5750
6146
|
|
|
@@ -5811,6 +6207,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5811
6207
|
|
|
5812
6208
|
|
|
5813
6209
|
|
|
6210
|
+
|
|
6211
|
+
|
|
6212
|
+
|
|
6213
|
+
|
|
6214
|
+
|
|
6215
|
+
|
|
6216
|
+
|
|
6217
|
+
|
|
5814
6218
|
|
|
5815
6219
|
|
|
5816
6220
|
|
|
@@ -5884,6 +6288,14 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5884
6288
|
|
|
5885
6289
|
|
|
5886
6290
|
|
|
6291
|
+
|
|
6292
|
+
|
|
6293
|
+
|
|
6294
|
+
|
|
6295
|
+
|
|
6296
|
+
|
|
6297
|
+
|
|
6298
|
+
|
|
5887
6299
|
|
|
5888
6300
|
|
|
5889
6301
|
|
|
@@ -5934,6 +6346,10 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5934
6346
|
|
|
5935
6347
|
|
|
5936
6348
|
|
|
6349
|
+
|
|
6350
|
+
|
|
6351
|
+
|
|
6352
|
+
|
|
5937
6353
|
|
|
5938
6354
|
|
|
5939
6355
|
|
|
@@ -5991,6 +6407,10 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
5991
6407
|
|
|
5992
6408
|
|
|
5993
6409
|
|
|
6410
|
+
|
|
6411
|
+
|
|
6412
|
+
|
|
6413
|
+
|
|
5994
6414
|
|
|
5995
6415
|
|
|
5996
6416
|
|
|
@@ -6065,10 +6485,10 @@ var BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
6065
6485
|
}
|
|
6066
6486
|
_checkIndex(index) {
|
|
6067
6487
|
if (!Number.isInteger(index)) {
|
|
6068
|
-
|
|
6488
|
+
raise(TypeError, ERR.invalidIndex("BinaryIndexedTree"));
|
|
6069
6489
|
}
|
|
6070
6490
|
if (index < 0 || index >= this._size) {
|
|
6071
|
-
|
|
6491
|
+
raise(RangeError, ERR.indexOutOfRange(index, 0, this._size - 1, "BinaryIndexedTree"));
|
|
6072
6492
|
}
|
|
6073
6493
|
}
|
|
6074
6494
|
/** Returns highest power of 2 <= n. */
|
|
@@ -6151,6 +6571,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6151
6571
|
|
|
6152
6572
|
|
|
6153
6573
|
|
|
6574
|
+
|
|
6575
|
+
|
|
6576
|
+
|
|
6577
|
+
|
|
6154
6578
|
|
|
6155
6579
|
|
|
6156
6580
|
|
|
@@ -6219,6 +6643,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6219
6643
|
|
|
6220
6644
|
|
|
6221
6645
|
|
|
6646
|
+
|
|
6647
|
+
|
|
6648
|
+
|
|
6649
|
+
|
|
6222
6650
|
|
|
6223
6651
|
|
|
6224
6652
|
|
|
@@ -6281,6 +6709,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6281
6709
|
|
|
6282
6710
|
|
|
6283
6711
|
|
|
6712
|
+
|
|
6713
|
+
|
|
6714
|
+
|
|
6715
|
+
|
|
6284
6716
|
|
|
6285
6717
|
|
|
6286
6718
|
|
|
@@ -6350,6 +6782,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6350
6782
|
|
|
6351
6783
|
|
|
6352
6784
|
|
|
6785
|
+
|
|
6786
|
+
|
|
6787
|
+
|
|
6788
|
+
|
|
6353
6789
|
|
|
6354
6790
|
|
|
6355
6791
|
|
|
@@ -6397,6 +6833,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6397
6833
|
|
|
6398
6834
|
|
|
6399
6835
|
|
|
6836
|
+
|
|
6837
|
+
|
|
6838
|
+
|
|
6839
|
+
|
|
6400
6840
|
|
|
6401
6841
|
|
|
6402
6842
|
|
|
@@ -6470,6 +6910,10 @@ var SegmentTree = class _SegmentTree {
|
|
|
6470
6910
|
|
|
6471
6911
|
|
|
6472
6912
|
|
|
6913
|
+
|
|
6914
|
+
|
|
6915
|
+
|
|
6916
|
+
|
|
6473
6917
|
|
|
6474
6918
|
|
|
6475
6919
|
|
|
@@ -6839,6 +7283,22 @@ var AVLTree = class extends BST {
|
|
|
6839
7283
|
|
|
6840
7284
|
|
|
6841
7285
|
|
|
7286
|
+
|
|
7287
|
+
|
|
7288
|
+
|
|
7289
|
+
|
|
7290
|
+
|
|
7291
|
+
|
|
7292
|
+
|
|
7293
|
+
|
|
7294
|
+
|
|
7295
|
+
|
|
7296
|
+
|
|
7297
|
+
|
|
7298
|
+
|
|
7299
|
+
|
|
7300
|
+
|
|
7301
|
+
|
|
6842
7302
|
|
|
6843
7303
|
|
|
6844
7304
|
|
|
@@ -6955,6 +7415,18 @@ var AVLTree = class extends BST {
|
|
|
6955
7415
|
|
|
6956
7416
|
|
|
6957
7417
|
|
|
7418
|
+
|
|
7419
|
+
|
|
7420
|
+
|
|
7421
|
+
|
|
7422
|
+
|
|
7423
|
+
|
|
7424
|
+
|
|
7425
|
+
|
|
7426
|
+
|
|
7427
|
+
|
|
7428
|
+
|
|
7429
|
+
|
|
6958
7430
|
|
|
6959
7431
|
|
|
6960
7432
|
|
|
@@ -7034,6 +7506,14 @@ var AVLTree = class extends BST {
|
|
|
7034
7506
|
|
|
7035
7507
|
|
|
7036
7508
|
|
|
7509
|
+
|
|
7510
|
+
|
|
7511
|
+
|
|
7512
|
+
|
|
7513
|
+
|
|
7514
|
+
|
|
7515
|
+
|
|
7516
|
+
|
|
7037
7517
|
|
|
7038
7518
|
|
|
7039
7519
|
|
|
@@ -7154,6 +7634,18 @@ var AVLTree = class extends BST {
|
|
|
7154
7634
|
|
|
7155
7635
|
|
|
7156
7636
|
|
|
7637
|
+
|
|
7638
|
+
|
|
7639
|
+
|
|
7640
|
+
|
|
7641
|
+
|
|
7642
|
+
|
|
7643
|
+
|
|
7644
|
+
|
|
7645
|
+
|
|
7646
|
+
|
|
7647
|
+
|
|
7648
|
+
|
|
7157
7649
|
|
|
7158
7650
|
|
|
7159
7651
|
|
|
@@ -7285,6 +7777,8 @@ var AVLTree = class extends BST {
|
|
|
7285
7777
|
}
|
|
7286
7778
|
this._updateHeight(A);
|
|
7287
7779
|
if (B) this._updateHeight(B);
|
|
7780
|
+
this._updateCount(A);
|
|
7781
|
+
if (B) this._updateCount(B);
|
|
7288
7782
|
}
|
|
7289
7783
|
/**
|
|
7290
7784
|
* (Protected) Performs a Left-Right (LR) double rotation.
|
|
@@ -7330,6 +7824,9 @@ var AVLTree = class extends BST {
|
|
|
7330
7824
|
this._updateHeight(A);
|
|
7331
7825
|
if (B) this._updateHeight(B);
|
|
7332
7826
|
if (C) this._updateHeight(C);
|
|
7827
|
+
this._updateCount(A);
|
|
7828
|
+
if (B) this._updateCount(B);
|
|
7829
|
+
if (C) this._updateCount(C);
|
|
7333
7830
|
}
|
|
7334
7831
|
/**
|
|
7335
7832
|
* (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
|
|
@@ -7364,6 +7861,8 @@ var AVLTree = class extends BST {
|
|
|
7364
7861
|
}
|
|
7365
7862
|
this._updateHeight(A);
|
|
7366
7863
|
if (B) this._updateHeight(B);
|
|
7864
|
+
this._updateCount(A);
|
|
7865
|
+
if (B) this._updateCount(B);
|
|
7367
7866
|
}
|
|
7368
7867
|
/**
|
|
7369
7868
|
* (Protected) Performs a Right-Left (RL) double rotation.
|
|
@@ -7407,6 +7906,9 @@ var AVLTree = class extends BST {
|
|
|
7407
7906
|
this._updateHeight(A);
|
|
7408
7907
|
if (B) this._updateHeight(B);
|
|
7409
7908
|
if (C) this._updateHeight(C);
|
|
7909
|
+
this._updateCount(A);
|
|
7910
|
+
if (B) this._updateCount(B);
|
|
7911
|
+
if (C) this._updateCount(C);
|
|
7410
7912
|
}
|
|
7411
7913
|
/**
|
|
7412
7914
|
* (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
|
|
@@ -7421,6 +7923,7 @@ var AVLTree = class extends BST {
|
|
|
7421
7923
|
const A = path[i];
|
|
7422
7924
|
if (A) {
|
|
7423
7925
|
this._updateHeight(A);
|
|
7926
|
+
this._updateCount(A);
|
|
7424
7927
|
switch (this._balanceFactor(A)) {
|
|
7425
7928
|
case -2:
|
|
7426
7929
|
if (A && A.left) {
|
|
@@ -7750,6 +8253,22 @@ var RedBlackTree = class extends BST {
|
|
|
7750
8253
|
|
|
7751
8254
|
|
|
7752
8255
|
|
|
8256
|
+
|
|
8257
|
+
|
|
8258
|
+
|
|
8259
|
+
|
|
8260
|
+
|
|
8261
|
+
|
|
8262
|
+
|
|
8263
|
+
|
|
8264
|
+
|
|
8265
|
+
|
|
8266
|
+
|
|
8267
|
+
|
|
8268
|
+
|
|
8269
|
+
|
|
8270
|
+
|
|
8271
|
+
|
|
7753
8272
|
|
|
7754
8273
|
|
|
7755
8274
|
|
|
@@ -7856,6 +8375,7 @@ var RedBlackTree = class extends BST {
|
|
|
7856
8375
|
node.left = NIL;
|
|
7857
8376
|
node.right = NIL;
|
|
7858
8377
|
node.color = "RED";
|
|
8378
|
+
this._updateCountAlongPath(node);
|
|
7859
8379
|
this._insertFixup(node);
|
|
7860
8380
|
if (this.isRealNode(this._root)) this._root.color = "BLACK";
|
|
7861
8381
|
}
|
|
@@ -7965,6 +8485,7 @@ var RedBlackTree = class extends BST {
|
|
|
7965
8485
|
newNode.left = NIL;
|
|
7966
8486
|
newNode.right = NIL;
|
|
7967
8487
|
newNode.color = "RED";
|
|
8488
|
+
this._updateCountAlongPath(newNode);
|
|
7968
8489
|
this._insertFixup(newNode);
|
|
7969
8490
|
if (this.isRealNode(this._root)) this._root.color = "BLACK";
|
|
7970
8491
|
else return void 0;
|
|
@@ -8207,6 +8728,22 @@ var RedBlackTree = class extends BST {
|
|
|
8207
8728
|
|
|
8208
8729
|
|
|
8209
8730
|
|
|
8731
|
+
|
|
8732
|
+
|
|
8733
|
+
|
|
8734
|
+
|
|
8735
|
+
|
|
8736
|
+
|
|
8737
|
+
|
|
8738
|
+
|
|
8739
|
+
|
|
8740
|
+
|
|
8741
|
+
|
|
8742
|
+
|
|
8743
|
+
|
|
8744
|
+
|
|
8745
|
+
|
|
8746
|
+
|
|
8210
8747
|
|
|
8211
8748
|
|
|
8212
8749
|
|
|
@@ -8407,17 +8944,33 @@ var RedBlackTree = class extends BST {
|
|
|
8407
8944
|
|
|
8408
8945
|
|
|
8409
8946
|
|
|
8410
|
-
|
|
8411
|
-
|
|
8412
|
-
|
|
8413
|
-
|
|
8414
|
-
|
|
8415
|
-
|
|
8416
|
-
|
|
8417
|
-
|
|
8418
|
-
|
|
8419
|
-
|
|
8420
|
-
|
|
8947
|
+
|
|
8948
|
+
|
|
8949
|
+
|
|
8950
|
+
|
|
8951
|
+
|
|
8952
|
+
|
|
8953
|
+
|
|
8954
|
+
|
|
8955
|
+
|
|
8956
|
+
|
|
8957
|
+
|
|
8958
|
+
|
|
8959
|
+
|
|
8960
|
+
|
|
8961
|
+
|
|
8962
|
+
|
|
8963
|
+
* @example
|
|
8964
|
+
* // Remove and rebalance
|
|
8965
|
+
* const rbt = new RedBlackTree<number>([10, 5, 15, 3, 7]);
|
|
8966
|
+
* rbt.delete(5);
|
|
8967
|
+
* console.log(rbt.has(5)); // false;
|
|
8968
|
+
* console.log(rbt.size); // 4;
|
|
8969
|
+
*/
|
|
8970
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
8971
|
+
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
8972
|
+
const results = [];
|
|
8973
|
+
let nodeToDelete;
|
|
8421
8974
|
if (this._isPredicate(keyNodeEntryRawOrPredicate)) nodeToDelete = this.getNode(keyNodeEntryRawOrPredicate);
|
|
8422
8975
|
else nodeToDelete = this.isRealNode(keyNodeEntryRawOrPredicate) ? keyNodeEntryRawOrPredicate : this.getNode(keyNodeEntryRawOrPredicate);
|
|
8423
8976
|
if (!nodeToDelete) {
|
|
@@ -8460,6 +9013,7 @@ var RedBlackTree = class extends BST {
|
|
|
8460
9013
|
}
|
|
8461
9014
|
if (this._isMapMode) this._store.delete(nodeToDelete.key);
|
|
8462
9015
|
this._size--;
|
|
9016
|
+
this._updateCountAlongPath(replacementNode?.parent ?? replacementNode);
|
|
8463
9017
|
if (this._size <= 0) {
|
|
8464
9018
|
this._setMinCache(void 0);
|
|
8465
9019
|
this._setMaxCache(void 0);
|
|
@@ -8566,6 +9120,18 @@ var RedBlackTree = class extends BST {
|
|
|
8566
9120
|
|
|
8567
9121
|
|
|
8568
9122
|
|
|
9123
|
+
|
|
9124
|
+
|
|
9125
|
+
|
|
9126
|
+
|
|
9127
|
+
|
|
9128
|
+
|
|
9129
|
+
|
|
9130
|
+
|
|
9131
|
+
|
|
9132
|
+
|
|
9133
|
+
|
|
9134
|
+
|
|
8569
9135
|
|
|
8570
9136
|
|
|
8571
9137
|
|
|
@@ -8698,6 +9264,22 @@ var RedBlackTree = class extends BST {
|
|
|
8698
9264
|
|
|
8699
9265
|
|
|
8700
9266
|
|
|
9267
|
+
|
|
9268
|
+
|
|
9269
|
+
|
|
9270
|
+
|
|
9271
|
+
|
|
9272
|
+
|
|
9273
|
+
|
|
9274
|
+
|
|
9275
|
+
|
|
9276
|
+
|
|
9277
|
+
|
|
9278
|
+
|
|
9279
|
+
|
|
9280
|
+
|
|
9281
|
+
|
|
9282
|
+
|
|
8701
9283
|
|
|
8702
9284
|
|
|
8703
9285
|
|
|
@@ -8800,6 +9382,7 @@ var RedBlackTree = class extends BST {
|
|
|
8800
9382
|
node.left = NIL;
|
|
8801
9383
|
node.right = NIL;
|
|
8802
9384
|
node.color = "RED";
|
|
9385
|
+
this._updateCountAlongPath(node);
|
|
8803
9386
|
this._insertFixup(node);
|
|
8804
9387
|
return "CREATED";
|
|
8805
9388
|
}
|
|
@@ -8968,6 +9551,8 @@ var RedBlackTree = class extends BST {
|
|
|
8968
9551
|
}
|
|
8969
9552
|
y.left = x;
|
|
8970
9553
|
x.parent = y;
|
|
9554
|
+
this._updateCount(x);
|
|
9555
|
+
this._updateCount(y);
|
|
8971
9556
|
}
|
|
8972
9557
|
/**
|
|
8973
9558
|
* (Protected) Perform a right rotation around y.
|
|
@@ -8994,6 +9579,8 @@ var RedBlackTree = class extends BST {
|
|
|
8994
9579
|
}
|
|
8995
9580
|
x.right = y;
|
|
8996
9581
|
y.parent = x;
|
|
9582
|
+
this._updateCount(y);
|
|
9583
|
+
this._updateCount(x);
|
|
8997
9584
|
}
|
|
8998
9585
|
};
|
|
8999
9586
|
|
|
@@ -9025,7 +9612,7 @@ var TreeSet = class _TreeSet {
|
|
|
9025
9612
|
const toElementFn = options.toElementFn;
|
|
9026
9613
|
const comparator = options.comparator ?? _TreeSet.createDefaultComparator();
|
|
9027
9614
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
9028
|
-
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode });
|
|
9615
|
+
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
9029
9616
|
for (const item of elements) {
|
|
9030
9617
|
const k = toElementFn ? toElementFn(item) : item;
|
|
9031
9618
|
this.add(k);
|
|
@@ -9044,7 +9631,7 @@ var TreeSet = class _TreeSet {
|
|
|
9044
9631
|
static createDefaultComparator() {
|
|
9045
9632
|
return (a, b) => {
|
|
9046
9633
|
if (typeof a === "number" && typeof b === "number") {
|
|
9047
|
-
if (Number.isNaN(a) || Number.isNaN(b))
|
|
9634
|
+
if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN("TreeSet"));
|
|
9048
9635
|
const aa = Object.is(a, -0) ? 0 : a;
|
|
9049
9636
|
const bb = Object.is(b, -0) ? 0 : b;
|
|
9050
9637
|
return aa > bb ? 1 : aa < bb ? -1 : 0;
|
|
@@ -9055,10 +9642,10 @@ var TreeSet = class _TreeSet {
|
|
|
9055
9642
|
if (a instanceof Date && b instanceof Date) {
|
|
9056
9643
|
const ta = a.getTime();
|
|
9057
9644
|
const tb = b.getTime();
|
|
9058
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
9645
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("TreeSet"));
|
|
9059
9646
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
9060
9647
|
}
|
|
9061
|
-
|
|
9648
|
+
raise(TypeError, ERR.comparatorRequired("TreeSet"));
|
|
9062
9649
|
};
|
|
9063
9650
|
}
|
|
9064
9651
|
/**
|
|
@@ -9186,6 +9773,26 @@ var TreeSet = class _TreeSet {
|
|
|
9186
9773
|
|
|
9187
9774
|
|
|
9188
9775
|
|
|
9776
|
+
|
|
9777
|
+
|
|
9778
|
+
|
|
9779
|
+
|
|
9780
|
+
|
|
9781
|
+
|
|
9782
|
+
|
|
9783
|
+
|
|
9784
|
+
|
|
9785
|
+
|
|
9786
|
+
|
|
9787
|
+
|
|
9788
|
+
|
|
9789
|
+
|
|
9790
|
+
|
|
9791
|
+
|
|
9792
|
+
|
|
9793
|
+
|
|
9794
|
+
|
|
9795
|
+
|
|
9189
9796
|
|
|
9190
9797
|
|
|
9191
9798
|
|
|
@@ -9217,15 +9824,15 @@ var TreeSet = class _TreeSet {
|
|
|
9217
9824
|
_validateKey(key) {
|
|
9218
9825
|
if (!this.#isDefaultComparator) return;
|
|
9219
9826
|
if (typeof key === "number") {
|
|
9220
|
-
if (Number.isNaN(key))
|
|
9827
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeSet"));
|
|
9221
9828
|
return;
|
|
9222
9829
|
}
|
|
9223
9830
|
if (typeof key === "string") return;
|
|
9224
9831
|
if (key instanceof Date) {
|
|
9225
|
-
if (Number.isNaN(key.getTime()))
|
|
9832
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeSet"));
|
|
9226
9833
|
return;
|
|
9227
9834
|
}
|
|
9228
|
-
|
|
9835
|
+
raise(TypeError, ERR.comparatorRequired("TreeSet"));
|
|
9229
9836
|
}
|
|
9230
9837
|
/**
|
|
9231
9838
|
* Add a key to the set (no-op if already present).
|
|
@@ -9350,6 +9957,26 @@ var TreeSet = class _TreeSet {
|
|
|
9350
9957
|
|
|
9351
9958
|
|
|
9352
9959
|
|
|
9960
|
+
|
|
9961
|
+
|
|
9962
|
+
|
|
9963
|
+
|
|
9964
|
+
|
|
9965
|
+
|
|
9966
|
+
|
|
9967
|
+
|
|
9968
|
+
|
|
9969
|
+
|
|
9970
|
+
|
|
9971
|
+
|
|
9972
|
+
|
|
9973
|
+
|
|
9974
|
+
|
|
9975
|
+
|
|
9976
|
+
|
|
9977
|
+
|
|
9978
|
+
|
|
9979
|
+
|
|
9353
9980
|
|
|
9354
9981
|
|
|
9355
9982
|
|
|
@@ -9519,6 +10146,26 @@ var TreeSet = class _TreeSet {
|
|
|
9519
10146
|
|
|
9520
10147
|
|
|
9521
10148
|
|
|
10149
|
+
|
|
10150
|
+
|
|
10151
|
+
|
|
10152
|
+
|
|
10153
|
+
|
|
10154
|
+
|
|
10155
|
+
|
|
10156
|
+
|
|
10157
|
+
|
|
10158
|
+
|
|
10159
|
+
|
|
10160
|
+
|
|
10161
|
+
|
|
10162
|
+
|
|
10163
|
+
|
|
10164
|
+
|
|
10165
|
+
|
|
10166
|
+
|
|
10167
|
+
|
|
10168
|
+
|
|
9522
10169
|
|
|
9523
10170
|
|
|
9524
10171
|
|
|
@@ -9683,6 +10330,26 @@ var TreeSet = class _TreeSet {
|
|
|
9683
10330
|
|
|
9684
10331
|
|
|
9685
10332
|
|
|
10333
|
+
|
|
10334
|
+
|
|
10335
|
+
|
|
10336
|
+
|
|
10337
|
+
|
|
10338
|
+
|
|
10339
|
+
|
|
10340
|
+
|
|
10341
|
+
|
|
10342
|
+
|
|
10343
|
+
|
|
10344
|
+
|
|
10345
|
+
|
|
10346
|
+
|
|
10347
|
+
|
|
10348
|
+
|
|
10349
|
+
|
|
10350
|
+
|
|
10351
|
+
|
|
10352
|
+
|
|
9686
10353
|
|
|
9687
10354
|
|
|
9688
10355
|
|
|
@@ -9836,6 +10503,26 @@ var TreeSet = class _TreeSet {
|
|
|
9836
10503
|
|
|
9837
10504
|
|
|
9838
10505
|
|
|
10506
|
+
|
|
10507
|
+
|
|
10508
|
+
|
|
10509
|
+
|
|
10510
|
+
|
|
10511
|
+
|
|
10512
|
+
|
|
10513
|
+
|
|
10514
|
+
|
|
10515
|
+
|
|
10516
|
+
|
|
10517
|
+
|
|
10518
|
+
|
|
10519
|
+
|
|
10520
|
+
|
|
10521
|
+
|
|
10522
|
+
|
|
10523
|
+
|
|
10524
|
+
|
|
10525
|
+
|
|
9839
10526
|
|
|
9840
10527
|
|
|
9841
10528
|
|
|
@@ -9985,6 +10672,26 @@ var TreeSet = class _TreeSet {
|
|
|
9985
10672
|
|
|
9986
10673
|
|
|
9987
10674
|
|
|
10675
|
+
|
|
10676
|
+
|
|
10677
|
+
|
|
10678
|
+
|
|
10679
|
+
|
|
10680
|
+
|
|
10681
|
+
|
|
10682
|
+
|
|
10683
|
+
|
|
10684
|
+
|
|
10685
|
+
|
|
10686
|
+
|
|
10687
|
+
|
|
10688
|
+
|
|
10689
|
+
|
|
10690
|
+
|
|
10691
|
+
|
|
10692
|
+
|
|
10693
|
+
|
|
10694
|
+
|
|
9988
10695
|
|
|
9989
10696
|
|
|
9990
10697
|
|
|
@@ -10135,6 +10842,26 @@ var TreeSet = class _TreeSet {
|
|
|
10135
10842
|
|
|
10136
10843
|
|
|
10137
10844
|
|
|
10845
|
+
|
|
10846
|
+
|
|
10847
|
+
|
|
10848
|
+
|
|
10849
|
+
|
|
10850
|
+
|
|
10851
|
+
|
|
10852
|
+
|
|
10853
|
+
|
|
10854
|
+
|
|
10855
|
+
|
|
10856
|
+
|
|
10857
|
+
|
|
10858
|
+
|
|
10859
|
+
|
|
10860
|
+
|
|
10861
|
+
|
|
10862
|
+
|
|
10863
|
+
|
|
10864
|
+
|
|
10138
10865
|
|
|
10139
10866
|
|
|
10140
10867
|
|
|
@@ -10285,6 +11012,26 @@ var TreeSet = class _TreeSet {
|
|
|
10285
11012
|
|
|
10286
11013
|
|
|
10287
11014
|
|
|
11015
|
+
|
|
11016
|
+
|
|
11017
|
+
|
|
11018
|
+
|
|
11019
|
+
|
|
11020
|
+
|
|
11021
|
+
|
|
11022
|
+
|
|
11023
|
+
|
|
11024
|
+
|
|
11025
|
+
|
|
11026
|
+
|
|
11027
|
+
|
|
11028
|
+
|
|
11029
|
+
|
|
11030
|
+
|
|
11031
|
+
|
|
11032
|
+
|
|
11033
|
+
|
|
11034
|
+
|
|
10288
11035
|
|
|
10289
11036
|
|
|
10290
11037
|
|
|
@@ -10438,6 +11185,26 @@ var TreeSet = class _TreeSet {
|
|
|
10438
11185
|
|
|
10439
11186
|
|
|
10440
11187
|
|
|
11188
|
+
|
|
11189
|
+
|
|
11190
|
+
|
|
11191
|
+
|
|
11192
|
+
|
|
11193
|
+
|
|
11194
|
+
|
|
11195
|
+
|
|
11196
|
+
|
|
11197
|
+
|
|
11198
|
+
|
|
11199
|
+
|
|
11200
|
+
|
|
11201
|
+
|
|
11202
|
+
|
|
11203
|
+
|
|
11204
|
+
|
|
11205
|
+
|
|
11206
|
+
|
|
11207
|
+
|
|
10441
11208
|
|
|
10442
11209
|
|
|
10443
11210
|
|
|
@@ -10591,6 +11358,26 @@ var TreeSet = class _TreeSet {
|
|
|
10591
11358
|
|
|
10592
11359
|
|
|
10593
11360
|
|
|
11361
|
+
|
|
11362
|
+
|
|
11363
|
+
|
|
11364
|
+
|
|
11365
|
+
|
|
11366
|
+
|
|
11367
|
+
|
|
11368
|
+
|
|
11369
|
+
|
|
11370
|
+
|
|
11371
|
+
|
|
11372
|
+
|
|
11373
|
+
|
|
11374
|
+
|
|
11375
|
+
|
|
11376
|
+
|
|
11377
|
+
|
|
11378
|
+
|
|
11379
|
+
|
|
11380
|
+
|
|
10594
11381
|
|
|
10595
11382
|
|
|
10596
11383
|
|
|
@@ -10768,7 +11555,27 @@ var TreeSet = class _TreeSet {
|
|
|
10768
11555
|
|
|
10769
11556
|
|
|
10770
11557
|
|
|
10771
|
-
|
|
11558
|
+
|
|
11559
|
+
|
|
11560
|
+
|
|
11561
|
+
|
|
11562
|
+
|
|
11563
|
+
|
|
11564
|
+
|
|
11565
|
+
|
|
11566
|
+
|
|
11567
|
+
|
|
11568
|
+
|
|
11569
|
+
|
|
11570
|
+
|
|
11571
|
+
|
|
11572
|
+
|
|
11573
|
+
|
|
11574
|
+
|
|
11575
|
+
|
|
11576
|
+
|
|
11577
|
+
|
|
11578
|
+
* @example
|
|
10772
11579
|
* // Filter
|
|
10773
11580
|
* const ts = new TreeSet<number>([1, 2, 3, 4, 5]);
|
|
10774
11581
|
* const evens = ts.filter(k => k % 2 === 0);
|
|
@@ -10903,6 +11710,26 @@ var TreeSet = class _TreeSet {
|
|
|
10903
11710
|
|
|
10904
11711
|
|
|
10905
11712
|
|
|
11713
|
+
|
|
11714
|
+
|
|
11715
|
+
|
|
11716
|
+
|
|
11717
|
+
|
|
11718
|
+
|
|
11719
|
+
|
|
11720
|
+
|
|
11721
|
+
|
|
11722
|
+
|
|
11723
|
+
|
|
11724
|
+
|
|
11725
|
+
|
|
11726
|
+
|
|
11727
|
+
|
|
11728
|
+
|
|
11729
|
+
|
|
11730
|
+
|
|
11731
|
+
|
|
11732
|
+
|
|
10906
11733
|
|
|
10907
11734
|
|
|
10908
11735
|
|
|
@@ -11054,6 +11881,26 @@ var TreeSet = class _TreeSet {
|
|
|
11054
11881
|
|
|
11055
11882
|
|
|
11056
11883
|
|
|
11884
|
+
|
|
11885
|
+
|
|
11886
|
+
|
|
11887
|
+
|
|
11888
|
+
|
|
11889
|
+
|
|
11890
|
+
|
|
11891
|
+
|
|
11892
|
+
|
|
11893
|
+
|
|
11894
|
+
|
|
11895
|
+
|
|
11896
|
+
|
|
11897
|
+
|
|
11898
|
+
|
|
11899
|
+
|
|
11900
|
+
|
|
11901
|
+
|
|
11902
|
+
|
|
11903
|
+
|
|
11057
11904
|
|
|
11058
11905
|
|
|
11059
11906
|
|
|
@@ -11206,6 +12053,26 @@ var TreeSet = class _TreeSet {
|
|
|
11206
12053
|
|
|
11207
12054
|
|
|
11208
12055
|
|
|
12056
|
+
|
|
12057
|
+
|
|
12058
|
+
|
|
12059
|
+
|
|
12060
|
+
|
|
12061
|
+
|
|
12062
|
+
|
|
12063
|
+
|
|
12064
|
+
|
|
12065
|
+
|
|
12066
|
+
|
|
12067
|
+
|
|
12068
|
+
|
|
12069
|
+
|
|
12070
|
+
|
|
12071
|
+
|
|
12072
|
+
|
|
12073
|
+
|
|
12074
|
+
|
|
12075
|
+
|
|
11209
12076
|
|
|
11210
12077
|
|
|
11211
12078
|
|
|
@@ -11358,6 +12225,26 @@ var TreeSet = class _TreeSet {
|
|
|
11358
12225
|
|
|
11359
12226
|
|
|
11360
12227
|
|
|
12228
|
+
|
|
12229
|
+
|
|
12230
|
+
|
|
12231
|
+
|
|
12232
|
+
|
|
12233
|
+
|
|
12234
|
+
|
|
12235
|
+
|
|
12236
|
+
|
|
12237
|
+
|
|
12238
|
+
|
|
12239
|
+
|
|
12240
|
+
|
|
12241
|
+
|
|
12242
|
+
|
|
12243
|
+
|
|
12244
|
+
|
|
12245
|
+
|
|
12246
|
+
|
|
12247
|
+
|
|
11361
12248
|
|
|
11362
12249
|
|
|
11363
12250
|
|
|
@@ -11513,6 +12400,26 @@ var TreeSet = class _TreeSet {
|
|
|
11513
12400
|
|
|
11514
12401
|
|
|
11515
12402
|
|
|
12403
|
+
|
|
12404
|
+
|
|
12405
|
+
|
|
12406
|
+
|
|
12407
|
+
|
|
12408
|
+
|
|
12409
|
+
|
|
12410
|
+
|
|
12411
|
+
|
|
12412
|
+
|
|
12413
|
+
|
|
12414
|
+
|
|
12415
|
+
|
|
12416
|
+
|
|
12417
|
+
|
|
12418
|
+
|
|
12419
|
+
|
|
12420
|
+
|
|
12421
|
+
|
|
12422
|
+
|
|
11516
12423
|
|
|
11517
12424
|
|
|
11518
12425
|
|
|
@@ -11662,6 +12569,26 @@ var TreeSet = class _TreeSet {
|
|
|
11662
12569
|
|
|
11663
12570
|
|
|
11664
12571
|
|
|
12572
|
+
|
|
12573
|
+
|
|
12574
|
+
|
|
12575
|
+
|
|
12576
|
+
|
|
12577
|
+
|
|
12578
|
+
|
|
12579
|
+
|
|
12580
|
+
|
|
12581
|
+
|
|
12582
|
+
|
|
12583
|
+
|
|
12584
|
+
|
|
12585
|
+
|
|
12586
|
+
|
|
12587
|
+
|
|
12588
|
+
|
|
12589
|
+
|
|
12590
|
+
|
|
12591
|
+
|
|
11665
12592
|
|
|
11666
12593
|
|
|
11667
12594
|
|
|
@@ -11720,6 +12647,10 @@ var TreeSet = class _TreeSet {
|
|
|
11720
12647
|
|
|
11721
12648
|
|
|
11722
12649
|
|
|
12650
|
+
|
|
12651
|
+
|
|
12652
|
+
|
|
12653
|
+
|
|
11723
12654
|
|
|
11724
12655
|
|
|
11725
12656
|
|
|
@@ -11784,6 +12715,10 @@ var TreeSet = class _TreeSet {
|
|
|
11784
12715
|
|
|
11785
12716
|
|
|
11786
12717
|
|
|
12718
|
+
|
|
12719
|
+
|
|
12720
|
+
|
|
12721
|
+
|
|
11787
12722
|
|
|
11788
12723
|
|
|
11789
12724
|
|
|
@@ -11826,6 +12761,10 @@ var TreeSet = class _TreeSet {
|
|
|
11826
12761
|
|
|
11827
12762
|
|
|
11828
12763
|
|
|
12764
|
+
|
|
12765
|
+
|
|
12766
|
+
|
|
12767
|
+
|
|
11829
12768
|
|
|
11830
12769
|
|
|
11831
12770
|
|
|
@@ -11873,6 +12812,10 @@ var TreeSet = class _TreeSet {
|
|
|
11873
12812
|
|
|
11874
12813
|
|
|
11875
12814
|
|
|
12815
|
+
|
|
12816
|
+
|
|
12817
|
+
|
|
12818
|
+
|
|
11876
12819
|
|
|
11877
12820
|
|
|
11878
12821
|
|
|
@@ -11994,6 +12937,22 @@ var TreeSet = class _TreeSet {
|
|
|
11994
12937
|
|
|
11995
12938
|
|
|
11996
12939
|
|
|
12940
|
+
|
|
12941
|
+
|
|
12942
|
+
|
|
12943
|
+
|
|
12944
|
+
|
|
12945
|
+
|
|
12946
|
+
|
|
12947
|
+
|
|
12948
|
+
|
|
12949
|
+
|
|
12950
|
+
|
|
12951
|
+
|
|
12952
|
+
|
|
12953
|
+
|
|
12954
|
+
|
|
12955
|
+
|
|
11997
12956
|
|
|
11998
12957
|
|
|
11999
12958
|
|
|
@@ -12135,6 +13094,22 @@ var TreeSet = class _TreeSet {
|
|
|
12135
13094
|
|
|
12136
13095
|
|
|
12137
13096
|
|
|
13097
|
+
|
|
13098
|
+
|
|
13099
|
+
|
|
13100
|
+
|
|
13101
|
+
|
|
13102
|
+
|
|
13103
|
+
|
|
13104
|
+
|
|
13105
|
+
|
|
13106
|
+
|
|
13107
|
+
|
|
13108
|
+
|
|
13109
|
+
|
|
13110
|
+
|
|
13111
|
+
|
|
13112
|
+
|
|
12138
13113
|
|
|
12139
13114
|
|
|
12140
13115
|
|
|
@@ -12268,6 +13243,22 @@ var TreeSet = class _TreeSet {
|
|
|
12268
13243
|
|
|
12269
13244
|
|
|
12270
13245
|
|
|
13246
|
+
|
|
13247
|
+
|
|
13248
|
+
|
|
13249
|
+
|
|
13250
|
+
|
|
13251
|
+
|
|
13252
|
+
|
|
13253
|
+
|
|
13254
|
+
|
|
13255
|
+
|
|
13256
|
+
|
|
13257
|
+
|
|
13258
|
+
|
|
13259
|
+
|
|
13260
|
+
|
|
13261
|
+
|
|
12271
13262
|
|
|
12272
13263
|
|
|
12273
13264
|
|
|
@@ -12399,6 +13390,22 @@ var TreeSet = class _TreeSet {
|
|
|
12399
13390
|
|
|
12400
13391
|
|
|
12401
13392
|
|
|
13393
|
+
|
|
13394
|
+
|
|
13395
|
+
|
|
13396
|
+
|
|
13397
|
+
|
|
13398
|
+
|
|
13399
|
+
|
|
13400
|
+
|
|
13401
|
+
|
|
13402
|
+
|
|
13403
|
+
|
|
13404
|
+
|
|
13405
|
+
|
|
13406
|
+
|
|
13407
|
+
|
|
13408
|
+
|
|
12402
13409
|
|
|
12403
13410
|
|
|
12404
13411
|
|
|
@@ -12533,6 +13540,22 @@ var TreeSet = class _TreeSet {
|
|
|
12533
13540
|
|
|
12534
13541
|
|
|
12535
13542
|
|
|
13543
|
+
|
|
13544
|
+
|
|
13545
|
+
|
|
13546
|
+
|
|
13547
|
+
|
|
13548
|
+
|
|
13549
|
+
|
|
13550
|
+
|
|
13551
|
+
|
|
13552
|
+
|
|
13553
|
+
|
|
13554
|
+
|
|
13555
|
+
|
|
13556
|
+
|
|
13557
|
+
|
|
13558
|
+
|
|
12536
13559
|
|
|
12537
13560
|
|
|
12538
13561
|
|
|
@@ -12584,6 +13607,62 @@ var TreeSet = class _TreeSet {
|
|
|
12584
13607
|
}
|
|
12585
13608
|
return out;
|
|
12586
13609
|
}
|
|
13610
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
13611
|
+
/**
|
|
13612
|
+
* Returns the element at the k-th position in tree order (0-indexed).
|
|
13613
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
13614
|
+
|
|
13615
|
+
|
|
13616
|
+
|
|
13617
|
+
* @example
|
|
13618
|
+
* // Find k-th element in a TreeSet
|
|
13619
|
+
* const set = new TreeSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
13620
|
+
* console.log(set.getByRank(0)); // 10;
|
|
13621
|
+
* console.log(set.getByRank(2)); // 30;
|
|
13622
|
+
* console.log(set.getRank(30)); // 2;
|
|
13623
|
+
*/
|
|
13624
|
+
getByRank(k) {
|
|
13625
|
+
return this.#core.getByRank(k);
|
|
13626
|
+
}
|
|
13627
|
+
/**
|
|
13628
|
+
* Returns the 0-based rank of a key (number of elements that precede it in tree order).
|
|
13629
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
13630
|
+
* @example
|
|
13631
|
+
* // Get the rank of a key in sorted order
|
|
13632
|
+
* const tree = new TreeSet<number>(
|
|
13633
|
+
* [10, 20, 30, 40, 50],
|
|
13634
|
+
* { enableOrderStatistic: true }
|
|
13635
|
+
* );
|
|
13636
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
13637
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
13638
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
13639
|
+
* console.log(tree.getRank(25)); // 2;
|
|
13640
|
+
*/
|
|
13641
|
+
getRank(key) {
|
|
13642
|
+
return this.#core.getRank(key);
|
|
13643
|
+
}
|
|
13644
|
+
/**
|
|
13645
|
+
* Returns elements by rank range (0-indexed, inclusive on both ends).
|
|
13646
|
+
* @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
|
|
13647
|
+
|
|
13648
|
+
* @example
|
|
13649
|
+
* // Pagination with rangeByRank
|
|
13650
|
+
* const tree = new TreeSet<number>(
|
|
13651
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
13652
|
+
* { enableOrderStatistic: true }
|
|
13653
|
+
* );
|
|
13654
|
+
* const pageSize = 3;
|
|
13655
|
+
*
|
|
13656
|
+
* // Page 1
|
|
13657
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
13658
|
+
* // Page 2
|
|
13659
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
13660
|
+
* // Page 3
|
|
13661
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
13662
|
+
*/
|
|
13663
|
+
rangeByRank(start, end) {
|
|
13664
|
+
return this.#core.rangeByRank(start, end).filter((k) => k !== void 0);
|
|
13665
|
+
}
|
|
12587
13666
|
/**
|
|
12588
13667
|
* Creates a shallow clone of this set.
|
|
12589
13668
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -12726,8 +13805,28 @@ var TreeSet = class _TreeSet {
|
|
|
12726
13805
|
|
|
12727
13806
|
|
|
12728
13807
|
|
|
12729
|
-
|
|
12730
|
-
|
|
13808
|
+
|
|
13809
|
+
|
|
13810
|
+
|
|
13811
|
+
|
|
13812
|
+
|
|
13813
|
+
|
|
13814
|
+
|
|
13815
|
+
|
|
13816
|
+
|
|
13817
|
+
|
|
13818
|
+
|
|
13819
|
+
|
|
13820
|
+
|
|
13821
|
+
|
|
13822
|
+
|
|
13823
|
+
|
|
13824
|
+
|
|
13825
|
+
|
|
13826
|
+
|
|
13827
|
+
|
|
13828
|
+
* @example
|
|
13829
|
+
* // Deep clone
|
|
12731
13830
|
* const ts = new TreeSet<number>([1, 2, 3]);
|
|
12732
13831
|
* const copy = ts.clone();
|
|
12733
13832
|
* copy.delete(1);
|
|
@@ -12765,7 +13864,7 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
12765
13864
|
const comparator = options.comparator ?? TreeSet.createDefaultComparator();
|
|
12766
13865
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
12767
13866
|
const toEntryFn = options.toEntryFn;
|
|
12768
|
-
this.#core = new RedBlackTree([], { ...options, comparator, isMapMode: options.isMapMode });
|
|
13867
|
+
this.#core = new RedBlackTree([], { ...options, comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
12769
13868
|
for (const x of keysNodesEntriesOrRaws) {
|
|
12770
13869
|
if (x === null || x === void 0) continue;
|
|
12771
13870
|
if (toEntryFn) {
|
|
@@ -12798,15 +13897,15 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
12798
13897
|
_validateKey(key) {
|
|
12799
13898
|
if (!this.#isDefaultComparator) return;
|
|
12800
13899
|
if (typeof key === "number") {
|
|
12801
|
-
if (Number.isNaN(key))
|
|
13900
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeMultiMap"));
|
|
12802
13901
|
return;
|
|
12803
13902
|
}
|
|
12804
13903
|
if (typeof key === "string") return;
|
|
12805
13904
|
if (key instanceof Date) {
|
|
12806
|
-
if (Number.isNaN(key.getTime()))
|
|
13905
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeMultiMap"));
|
|
12807
13906
|
return;
|
|
12808
13907
|
}
|
|
12809
|
-
|
|
13908
|
+
raise(TypeError, ERR.comparatorRequired("TreeMultiMap"));
|
|
12810
13909
|
}
|
|
12811
13910
|
/**
|
|
12812
13911
|
* Number of distinct keys.
|
|
@@ -12935,6 +14034,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
12935
14034
|
|
|
12936
14035
|
|
|
12937
14036
|
|
|
14037
|
+
|
|
14038
|
+
|
|
14039
|
+
|
|
14040
|
+
|
|
14041
|
+
|
|
14042
|
+
|
|
14043
|
+
|
|
14044
|
+
|
|
14045
|
+
|
|
14046
|
+
|
|
14047
|
+
|
|
14048
|
+
|
|
14049
|
+
|
|
14050
|
+
|
|
14051
|
+
|
|
14052
|
+
|
|
14053
|
+
|
|
14054
|
+
|
|
14055
|
+
|
|
14056
|
+
|
|
12938
14057
|
|
|
12939
14058
|
|
|
12940
14059
|
|
|
@@ -13083,6 +14202,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13083
14202
|
|
|
13084
14203
|
|
|
13085
14204
|
|
|
14205
|
+
|
|
14206
|
+
|
|
14207
|
+
|
|
14208
|
+
|
|
14209
|
+
|
|
14210
|
+
|
|
14211
|
+
|
|
14212
|
+
|
|
14213
|
+
|
|
14214
|
+
|
|
14215
|
+
|
|
14216
|
+
|
|
14217
|
+
|
|
14218
|
+
|
|
14219
|
+
|
|
14220
|
+
|
|
14221
|
+
|
|
14222
|
+
|
|
14223
|
+
|
|
14224
|
+
|
|
13086
14225
|
|
|
13087
14226
|
|
|
13088
14227
|
|
|
@@ -13134,6 +14273,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13134
14273
|
|
|
13135
14274
|
|
|
13136
14275
|
|
|
14276
|
+
|
|
14277
|
+
|
|
14278
|
+
|
|
14279
|
+
|
|
13137
14280
|
|
|
13138
14281
|
|
|
13139
14282
|
|
|
@@ -13170,6 +14313,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13170
14313
|
|
|
13171
14314
|
|
|
13172
14315
|
|
|
14316
|
+
|
|
14317
|
+
|
|
14318
|
+
|
|
14319
|
+
|
|
13173
14320
|
|
|
13174
14321
|
|
|
13175
14322
|
|
|
@@ -13342,6 +14489,30 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13342
14489
|
|
|
13343
14490
|
|
|
13344
14491
|
|
|
14492
|
+
|
|
14493
|
+
|
|
14494
|
+
|
|
14495
|
+
|
|
14496
|
+
|
|
14497
|
+
|
|
14498
|
+
|
|
14499
|
+
|
|
14500
|
+
|
|
14501
|
+
|
|
14502
|
+
|
|
14503
|
+
|
|
14504
|
+
|
|
14505
|
+
|
|
14506
|
+
|
|
14507
|
+
|
|
14508
|
+
|
|
14509
|
+
|
|
14510
|
+
|
|
14511
|
+
|
|
14512
|
+
|
|
14513
|
+
|
|
14514
|
+
|
|
14515
|
+
|
|
13345
14516
|
|
|
13346
14517
|
|
|
13347
14518
|
|
|
@@ -13528,6 +14699,30 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13528
14699
|
|
|
13529
14700
|
|
|
13530
14701
|
|
|
14702
|
+
|
|
14703
|
+
|
|
14704
|
+
|
|
14705
|
+
|
|
14706
|
+
|
|
14707
|
+
|
|
14708
|
+
|
|
14709
|
+
|
|
14710
|
+
|
|
14711
|
+
|
|
14712
|
+
|
|
14713
|
+
|
|
14714
|
+
|
|
14715
|
+
|
|
14716
|
+
|
|
14717
|
+
|
|
14718
|
+
|
|
14719
|
+
|
|
14720
|
+
|
|
14721
|
+
|
|
14722
|
+
|
|
14723
|
+
|
|
14724
|
+
|
|
14725
|
+
|
|
13531
14726
|
|
|
13532
14727
|
|
|
13533
14728
|
|
|
@@ -13674,6 +14869,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13674
14869
|
|
|
13675
14870
|
|
|
13676
14871
|
|
|
14872
|
+
|
|
14873
|
+
|
|
14874
|
+
|
|
14875
|
+
|
|
14876
|
+
|
|
14877
|
+
|
|
14878
|
+
|
|
14879
|
+
|
|
14880
|
+
|
|
14881
|
+
|
|
14882
|
+
|
|
14883
|
+
|
|
14884
|
+
|
|
14885
|
+
|
|
14886
|
+
|
|
14887
|
+
|
|
14888
|
+
|
|
14889
|
+
|
|
14890
|
+
|
|
14891
|
+
|
|
13677
14892
|
|
|
13678
14893
|
|
|
13679
14894
|
|
|
@@ -13885,6 +15100,30 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13885
15100
|
|
|
13886
15101
|
|
|
13887
15102
|
|
|
15103
|
+
|
|
15104
|
+
|
|
15105
|
+
|
|
15106
|
+
|
|
15107
|
+
|
|
15108
|
+
|
|
15109
|
+
|
|
15110
|
+
|
|
15111
|
+
|
|
15112
|
+
|
|
15113
|
+
|
|
15114
|
+
|
|
15115
|
+
|
|
15116
|
+
|
|
15117
|
+
|
|
15118
|
+
|
|
15119
|
+
|
|
15120
|
+
|
|
15121
|
+
|
|
15122
|
+
|
|
15123
|
+
|
|
15124
|
+
|
|
15125
|
+
|
|
15126
|
+
|
|
13888
15127
|
|
|
13889
15128
|
|
|
13890
15129
|
|
|
@@ -13938,6 +15177,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13938
15177
|
|
|
13939
15178
|
|
|
13940
15179
|
|
|
15180
|
+
|
|
15181
|
+
|
|
15182
|
+
|
|
15183
|
+
|
|
13941
15184
|
|
|
13942
15185
|
|
|
13943
15186
|
|
|
@@ -13975,6 +15218,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
13975
15218
|
|
|
13976
15219
|
|
|
13977
15220
|
|
|
15221
|
+
|
|
15222
|
+
|
|
15223
|
+
|
|
15224
|
+
|
|
13978
15225
|
|
|
13979
15226
|
|
|
13980
15227
|
|
|
@@ -14017,6 +15264,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14017
15264
|
|
|
14018
15265
|
|
|
14019
15266
|
|
|
15267
|
+
|
|
15268
|
+
|
|
15269
|
+
|
|
15270
|
+
|
|
14020
15271
|
|
|
14021
15272
|
|
|
14022
15273
|
|
|
@@ -14175,6 +15426,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14175
15426
|
|
|
14176
15427
|
|
|
14177
15428
|
|
|
15429
|
+
|
|
15430
|
+
|
|
15431
|
+
|
|
15432
|
+
|
|
15433
|
+
|
|
15434
|
+
|
|
15435
|
+
|
|
15436
|
+
|
|
15437
|
+
|
|
15438
|
+
|
|
15439
|
+
|
|
15440
|
+
|
|
15441
|
+
|
|
15442
|
+
|
|
15443
|
+
|
|
15444
|
+
|
|
15445
|
+
|
|
15446
|
+
|
|
15447
|
+
|
|
15448
|
+
|
|
14178
15449
|
|
|
14179
15450
|
|
|
14180
15451
|
|
|
@@ -14326,6 +15597,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14326
15597
|
|
|
14327
15598
|
|
|
14328
15599
|
|
|
15600
|
+
|
|
15601
|
+
|
|
15602
|
+
|
|
15603
|
+
|
|
15604
|
+
|
|
15605
|
+
|
|
15606
|
+
|
|
15607
|
+
|
|
15608
|
+
|
|
15609
|
+
|
|
15610
|
+
|
|
15611
|
+
|
|
15612
|
+
|
|
15613
|
+
|
|
15614
|
+
|
|
15615
|
+
|
|
15616
|
+
|
|
15617
|
+
|
|
15618
|
+
|
|
15619
|
+
|
|
14329
15620
|
|
|
14330
15621
|
|
|
14331
15622
|
|
|
@@ -14378,6 +15669,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14378
15669
|
|
|
14379
15670
|
|
|
14380
15671
|
|
|
15672
|
+
|
|
15673
|
+
|
|
15674
|
+
|
|
15675
|
+
|
|
14381
15676
|
|
|
14382
15677
|
|
|
14383
15678
|
|
|
@@ -14415,6 +15710,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14415
15710
|
|
|
14416
15711
|
|
|
14417
15712
|
|
|
15713
|
+
|
|
15714
|
+
|
|
15715
|
+
|
|
15716
|
+
|
|
14418
15717
|
|
|
14419
15718
|
|
|
14420
15719
|
|
|
@@ -14452,6 +15751,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14452
15751
|
|
|
14453
15752
|
|
|
14454
15753
|
|
|
15754
|
+
|
|
15755
|
+
|
|
15756
|
+
|
|
15757
|
+
|
|
14455
15758
|
|
|
14456
15759
|
|
|
14457
15760
|
|
|
@@ -14519,6 +15822,14 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14519
15822
|
|
|
14520
15823
|
|
|
14521
15824
|
|
|
15825
|
+
|
|
15826
|
+
|
|
15827
|
+
|
|
15828
|
+
|
|
15829
|
+
|
|
15830
|
+
|
|
15831
|
+
|
|
15832
|
+
|
|
14522
15833
|
|
|
14523
15834
|
|
|
14524
15835
|
|
|
@@ -14592,6 +15903,14 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14592
15903
|
|
|
14593
15904
|
|
|
14594
15905
|
|
|
15906
|
+
|
|
15907
|
+
|
|
15908
|
+
|
|
15909
|
+
|
|
15910
|
+
|
|
15911
|
+
|
|
15912
|
+
|
|
15913
|
+
|
|
14595
15914
|
|
|
14596
15915
|
|
|
14597
15916
|
|
|
@@ -14638,6 +15957,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14638
15957
|
|
|
14639
15958
|
|
|
14640
15959
|
|
|
15960
|
+
|
|
15961
|
+
|
|
15962
|
+
|
|
15963
|
+
|
|
14641
15964
|
|
|
14642
15965
|
|
|
14643
15966
|
|
|
@@ -14679,6 +16002,10 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14679
16002
|
|
|
14680
16003
|
|
|
14681
16004
|
|
|
16005
|
+
|
|
16006
|
+
|
|
16007
|
+
|
|
16008
|
+
|
|
14682
16009
|
|
|
14683
16010
|
|
|
14684
16011
|
|
|
@@ -14842,12 +16169,32 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14842
16169
|
|
|
14843
16170
|
|
|
14844
16171
|
|
|
14845
|
-
|
|
14846
|
-
|
|
14847
|
-
|
|
14848
|
-
|
|
14849
|
-
|
|
14850
|
-
|
|
16172
|
+
|
|
16173
|
+
|
|
16174
|
+
|
|
16175
|
+
|
|
16176
|
+
|
|
16177
|
+
|
|
16178
|
+
|
|
16179
|
+
|
|
16180
|
+
|
|
16181
|
+
|
|
16182
|
+
|
|
16183
|
+
|
|
16184
|
+
|
|
16185
|
+
|
|
16186
|
+
|
|
16187
|
+
|
|
16188
|
+
|
|
16189
|
+
|
|
16190
|
+
|
|
16191
|
+
|
|
16192
|
+
* @example
|
|
16193
|
+
* // Least key ≥ target
|
|
16194
|
+
* const mm = new TreeMultiMap<number, string>();
|
|
16195
|
+
* mm.add(10, 'a');
|
|
16196
|
+
* mm.add(20, 'b');
|
|
16197
|
+
* mm.add(30, 'c');
|
|
14851
16198
|
* console.log(mm.ceiling(15)?.[0]); // 20;
|
|
14852
16199
|
*/
|
|
14853
16200
|
ceiling(key) {
|
|
@@ -14983,6 +16330,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
14983
16330
|
|
|
14984
16331
|
|
|
14985
16332
|
|
|
16333
|
+
|
|
16334
|
+
|
|
16335
|
+
|
|
16336
|
+
|
|
16337
|
+
|
|
16338
|
+
|
|
16339
|
+
|
|
16340
|
+
|
|
16341
|
+
|
|
16342
|
+
|
|
16343
|
+
|
|
16344
|
+
|
|
16345
|
+
|
|
16346
|
+
|
|
16347
|
+
|
|
16348
|
+
|
|
16349
|
+
|
|
16350
|
+
|
|
16351
|
+
|
|
16352
|
+
|
|
14986
16353
|
|
|
14987
16354
|
|
|
14988
16355
|
|
|
@@ -15118,6 +16485,22 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15118
16485
|
|
|
15119
16486
|
|
|
15120
16487
|
|
|
16488
|
+
|
|
16489
|
+
|
|
16490
|
+
|
|
16491
|
+
|
|
16492
|
+
|
|
16493
|
+
|
|
16494
|
+
|
|
16495
|
+
|
|
16496
|
+
|
|
16497
|
+
|
|
16498
|
+
|
|
16499
|
+
|
|
16500
|
+
|
|
16501
|
+
|
|
16502
|
+
|
|
16503
|
+
|
|
15121
16504
|
|
|
15122
16505
|
|
|
15123
16506
|
|
|
@@ -15248,6 +16631,22 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15248
16631
|
|
|
15249
16632
|
|
|
15250
16633
|
|
|
16634
|
+
|
|
16635
|
+
|
|
16636
|
+
|
|
16637
|
+
|
|
16638
|
+
|
|
16639
|
+
|
|
16640
|
+
|
|
16641
|
+
|
|
16642
|
+
|
|
16643
|
+
|
|
16644
|
+
|
|
16645
|
+
|
|
16646
|
+
|
|
16647
|
+
|
|
16648
|
+
|
|
16649
|
+
|
|
15251
16650
|
|
|
15252
16651
|
|
|
15253
16652
|
|
|
@@ -15403,6 +16802,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15403
16802
|
|
|
15404
16803
|
|
|
15405
16804
|
|
|
16805
|
+
|
|
16806
|
+
|
|
16807
|
+
|
|
16808
|
+
|
|
16809
|
+
|
|
16810
|
+
|
|
16811
|
+
|
|
16812
|
+
|
|
16813
|
+
|
|
16814
|
+
|
|
16815
|
+
|
|
16816
|
+
|
|
16817
|
+
|
|
16818
|
+
|
|
16819
|
+
|
|
16820
|
+
|
|
16821
|
+
|
|
16822
|
+
|
|
16823
|
+
|
|
16824
|
+
|
|
15406
16825
|
|
|
15407
16826
|
|
|
15408
16827
|
|
|
@@ -15553,6 +16972,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15553
16972
|
|
|
15554
16973
|
|
|
15555
16974
|
|
|
16975
|
+
|
|
16976
|
+
|
|
16977
|
+
|
|
16978
|
+
|
|
16979
|
+
|
|
16980
|
+
|
|
16981
|
+
|
|
16982
|
+
|
|
16983
|
+
|
|
16984
|
+
|
|
16985
|
+
|
|
16986
|
+
|
|
16987
|
+
|
|
16988
|
+
|
|
16989
|
+
|
|
16990
|
+
|
|
16991
|
+
|
|
16992
|
+
|
|
16993
|
+
|
|
16994
|
+
|
|
15556
16995
|
|
|
15557
16996
|
|
|
15558
16997
|
|
|
@@ -15708,6 +17147,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15708
17147
|
|
|
15709
17148
|
|
|
15710
17149
|
|
|
17150
|
+
|
|
17151
|
+
|
|
17152
|
+
|
|
17153
|
+
|
|
17154
|
+
|
|
17155
|
+
|
|
17156
|
+
|
|
17157
|
+
|
|
17158
|
+
|
|
17159
|
+
|
|
17160
|
+
|
|
17161
|
+
|
|
17162
|
+
|
|
17163
|
+
|
|
17164
|
+
|
|
17165
|
+
|
|
17166
|
+
|
|
17167
|
+
|
|
17168
|
+
|
|
17169
|
+
|
|
15711
17170
|
|
|
15712
17171
|
|
|
15713
17172
|
|
|
@@ -15865,6 +17324,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
15865
17324
|
|
|
15866
17325
|
|
|
15867
17326
|
|
|
17327
|
+
|
|
17328
|
+
|
|
17329
|
+
|
|
17330
|
+
|
|
17331
|
+
|
|
17332
|
+
|
|
17333
|
+
|
|
17334
|
+
|
|
17335
|
+
|
|
17336
|
+
|
|
17337
|
+
|
|
17338
|
+
|
|
17339
|
+
|
|
17340
|
+
|
|
17341
|
+
|
|
17342
|
+
|
|
17343
|
+
|
|
17344
|
+
|
|
17345
|
+
|
|
17346
|
+
|
|
15868
17347
|
|
|
15869
17348
|
|
|
15870
17349
|
|
|
@@ -16020,6 +17499,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16020
17499
|
|
|
16021
17500
|
|
|
16022
17501
|
|
|
17502
|
+
|
|
17503
|
+
|
|
17504
|
+
|
|
17505
|
+
|
|
17506
|
+
|
|
17507
|
+
|
|
17508
|
+
|
|
17509
|
+
|
|
17510
|
+
|
|
17511
|
+
|
|
17512
|
+
|
|
17513
|
+
|
|
17514
|
+
|
|
17515
|
+
|
|
17516
|
+
|
|
17517
|
+
|
|
17518
|
+
|
|
17519
|
+
|
|
17520
|
+
|
|
17521
|
+
|
|
16023
17522
|
|
|
16024
17523
|
|
|
16025
17524
|
|
|
@@ -16168,6 +17667,26 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16168
17667
|
|
|
16169
17668
|
|
|
16170
17669
|
|
|
17670
|
+
|
|
17671
|
+
|
|
17672
|
+
|
|
17673
|
+
|
|
17674
|
+
|
|
17675
|
+
|
|
17676
|
+
|
|
17677
|
+
|
|
17678
|
+
|
|
17679
|
+
|
|
17680
|
+
|
|
17681
|
+
|
|
17682
|
+
|
|
17683
|
+
|
|
17684
|
+
|
|
17685
|
+
|
|
17686
|
+
|
|
17687
|
+
|
|
17688
|
+
|
|
17689
|
+
|
|
16171
17690
|
|
|
16172
17691
|
|
|
16173
17692
|
|
|
@@ -16298,6 +17817,22 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16298
17817
|
|
|
16299
17818
|
|
|
16300
17819
|
|
|
17820
|
+
|
|
17821
|
+
|
|
17822
|
+
|
|
17823
|
+
|
|
17824
|
+
|
|
17825
|
+
|
|
17826
|
+
|
|
17827
|
+
|
|
17828
|
+
|
|
17829
|
+
|
|
17830
|
+
|
|
17831
|
+
|
|
17832
|
+
|
|
17833
|
+
|
|
17834
|
+
|
|
17835
|
+
|
|
16301
17836
|
|
|
16302
17837
|
|
|
16303
17838
|
|
|
@@ -16465,6 +18000,81 @@ var TreeMultiMap = class _TreeMultiMap {
|
|
|
16465
18000
|
|
|
16466
18001
|
|
|
16467
18002
|
|
|
18003
|
+
|
|
18004
|
+
|
|
18005
|
+
|
|
18006
|
+
|
|
18007
|
+
|
|
18008
|
+
|
|
18009
|
+
|
|
18010
|
+
|
|
18011
|
+
|
|
18012
|
+
|
|
18013
|
+
|
|
18014
|
+
|
|
18015
|
+
|
|
18016
|
+
|
|
18017
|
+
|
|
18018
|
+
|
|
18019
|
+
|
|
18020
|
+
|
|
18021
|
+
|
|
18022
|
+
|
|
18023
|
+
|
|
18024
|
+
* @example
|
|
18025
|
+
* // Order-statistic on BST
|
|
18026
|
+
* const tree = new TreeMultiMap<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
18027
|
+
* console.log(tree.getByRank(0)); // 10;
|
|
18028
|
+
* console.log(tree.getByRank(4)); // 50;
|
|
18029
|
+
* console.log(tree.getRank(30)); // 2;
|
|
18030
|
+
*/
|
|
18031
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
18032
|
+
getByRank(k) {
|
|
18033
|
+
const key = this.#core.getByRank(k);
|
|
18034
|
+
if (key === void 0) return void 0;
|
|
18035
|
+
return [key, this.#core.get(key) ?? []];
|
|
18036
|
+
}
|
|
18037
|
+
/**
|
|
18038
|
+
* Get the rank of a key in sorted order
|
|
18039
|
+
* @example
|
|
18040
|
+
* // Get the rank of a key in sorted order
|
|
18041
|
+
* const tree = new TreeMultiMap<number>(
|
|
18042
|
+
* [10, 20, 30, 40, 50],
|
|
18043
|
+
* { enableOrderStatistic: true }
|
|
18044
|
+
* );
|
|
18045
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
18046
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
18047
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
18048
|
+
* console.log(tree.getRank(25)); // 2;
|
|
18049
|
+
*/
|
|
18050
|
+
getRank(key) {
|
|
18051
|
+
return this.#core.getRank(key);
|
|
18052
|
+
}
|
|
18053
|
+
/**
|
|
18054
|
+
* Get elements by rank range
|
|
18055
|
+
|
|
18056
|
+
* @example
|
|
18057
|
+
* // Pagination with rangeByRank
|
|
18058
|
+
* const tree = new TreeMultiMap<number>(
|
|
18059
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
18060
|
+
* { enableOrderStatistic: true }
|
|
18061
|
+
* );
|
|
18062
|
+
* const pageSize = 3;
|
|
18063
|
+
*
|
|
18064
|
+
* // Page 1
|
|
18065
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
18066
|
+
* // Page 2
|
|
18067
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
18068
|
+
* // Page 3
|
|
18069
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
18070
|
+
*/
|
|
18071
|
+
rangeByRank(start, end) {
|
|
18072
|
+
const keys = this.#core.rangeByRank(start, end);
|
|
18073
|
+
return keys.filter((k) => k !== void 0).map((k) => [k, this.#core.get(k) ?? []]);
|
|
18074
|
+
}
|
|
18075
|
+
/**
|
|
18076
|
+
* Deep copy
|
|
18077
|
+
|
|
16468
18078
|
|
|
16469
18079
|
|
|
16470
18080
|
|
|
@@ -16516,7 +18126,7 @@ var TreeMap = class _TreeMap {
|
|
|
16516
18126
|
const toEntryFn = options.toEntryFn;
|
|
16517
18127
|
const comparator = options.comparator ?? _TreeMap.createDefaultComparator();
|
|
16518
18128
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
16519
|
-
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode });
|
|
18129
|
+
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
16520
18130
|
for (const item of entries) {
|
|
16521
18131
|
let k;
|
|
16522
18132
|
let v;
|
|
@@ -16524,7 +18134,7 @@ var TreeMap = class _TreeMap {
|
|
|
16524
18134
|
[k, v] = toEntryFn(item);
|
|
16525
18135
|
} else {
|
|
16526
18136
|
if (!Array.isArray(item) || item.length < 2) {
|
|
16527
|
-
|
|
18137
|
+
raise(TypeError, ERR.invalidEntry("TreeMap"));
|
|
16528
18138
|
}
|
|
16529
18139
|
k = item[0];
|
|
16530
18140
|
v = item[1];
|
|
@@ -16545,7 +18155,7 @@ var TreeMap = class _TreeMap {
|
|
|
16545
18155
|
static createDefaultComparator() {
|
|
16546
18156
|
return (a, b) => {
|
|
16547
18157
|
if (typeof a === "number" && typeof b === "number") {
|
|
16548
|
-
if (Number.isNaN(a) || Number.isNaN(b))
|
|
18158
|
+
if (Number.isNaN(a) || Number.isNaN(b)) raise(TypeError, ERR.invalidNaN("TreeMap"));
|
|
16549
18159
|
const aa = Object.is(a, -0) ? 0 : a;
|
|
16550
18160
|
const bb = Object.is(b, -0) ? 0 : b;
|
|
16551
18161
|
return aa > bb ? 1 : aa < bb ? -1 : 0;
|
|
@@ -16556,24 +18166,24 @@ var TreeMap = class _TreeMap {
|
|
|
16556
18166
|
if (a instanceof Date && b instanceof Date) {
|
|
16557
18167
|
const ta = a.getTime();
|
|
16558
18168
|
const tb = b.getTime();
|
|
16559
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
18169
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("TreeMap"));
|
|
16560
18170
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
16561
18171
|
}
|
|
16562
|
-
|
|
18172
|
+
raise(TypeError, ERR.comparatorRequired("TreeMap"));
|
|
16563
18173
|
};
|
|
16564
18174
|
}
|
|
16565
18175
|
_validateKey(key) {
|
|
16566
18176
|
if (!this.#isDefaultComparator) return;
|
|
16567
18177
|
if (typeof key === "number") {
|
|
16568
|
-
if (Number.isNaN(key))
|
|
18178
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeMap"));
|
|
16569
18179
|
return;
|
|
16570
18180
|
}
|
|
16571
18181
|
if (typeof key === "string") return;
|
|
16572
18182
|
if (key instanceof Date) {
|
|
16573
|
-
if (Number.isNaN(key.getTime()))
|
|
18183
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeMap"));
|
|
16574
18184
|
return;
|
|
16575
18185
|
}
|
|
16576
|
-
|
|
18186
|
+
raise(TypeError, ERR.comparatorRequired("TreeMap"));
|
|
16577
18187
|
}
|
|
16578
18188
|
/**
|
|
16579
18189
|
* Number of entries in the map.
|
|
@@ -16721,14 +18331,34 @@ var TreeMap = class _TreeMap {
|
|
|
16721
18331
|
|
|
16722
18332
|
|
|
16723
18333
|
|
|
16724
|
-
|
|
16725
|
-
|
|
16726
|
-
|
|
16727
|
-
|
|
16728
|
-
|
|
16729
|
-
|
|
16730
|
-
|
|
16731
|
-
|
|
18334
|
+
|
|
18335
|
+
|
|
18336
|
+
|
|
18337
|
+
|
|
18338
|
+
|
|
18339
|
+
|
|
18340
|
+
|
|
18341
|
+
|
|
18342
|
+
|
|
18343
|
+
|
|
18344
|
+
|
|
18345
|
+
|
|
18346
|
+
|
|
18347
|
+
|
|
18348
|
+
|
|
18349
|
+
|
|
18350
|
+
|
|
18351
|
+
|
|
18352
|
+
|
|
18353
|
+
|
|
18354
|
+
* @example
|
|
18355
|
+
* // Check empty
|
|
18356
|
+
* console.log(new TreeMap().isEmpty()); // true;
|
|
18357
|
+
*/
|
|
18358
|
+
isEmpty() {
|
|
18359
|
+
return this.size === 0;
|
|
18360
|
+
}
|
|
18361
|
+
/**
|
|
16732
18362
|
* Set or overwrite a value for a key.
|
|
16733
18363
|
* @remarks Expected time O(log n)
|
|
16734
18364
|
|
|
@@ -16857,6 +18487,26 @@ var TreeMap = class _TreeMap {
|
|
|
16857
18487
|
|
|
16858
18488
|
|
|
16859
18489
|
|
|
18490
|
+
|
|
18491
|
+
|
|
18492
|
+
|
|
18493
|
+
|
|
18494
|
+
|
|
18495
|
+
|
|
18496
|
+
|
|
18497
|
+
|
|
18498
|
+
|
|
18499
|
+
|
|
18500
|
+
|
|
18501
|
+
|
|
18502
|
+
|
|
18503
|
+
|
|
18504
|
+
|
|
18505
|
+
|
|
18506
|
+
|
|
18507
|
+
|
|
18508
|
+
|
|
18509
|
+
|
|
16860
18510
|
|
|
16861
18511
|
|
|
16862
18512
|
|
|
@@ -17035,6 +18685,26 @@ var TreeMap = class _TreeMap {
|
|
|
17035
18685
|
|
|
17036
18686
|
|
|
17037
18687
|
|
|
18688
|
+
|
|
18689
|
+
|
|
18690
|
+
|
|
18691
|
+
|
|
18692
|
+
|
|
18693
|
+
|
|
18694
|
+
|
|
18695
|
+
|
|
18696
|
+
|
|
18697
|
+
|
|
18698
|
+
|
|
18699
|
+
|
|
18700
|
+
|
|
18701
|
+
|
|
18702
|
+
|
|
18703
|
+
|
|
18704
|
+
|
|
18705
|
+
|
|
18706
|
+
|
|
18707
|
+
|
|
17038
18708
|
|
|
17039
18709
|
|
|
17040
18710
|
|
|
@@ -17203,6 +18873,26 @@ var TreeMap = class _TreeMap {
|
|
|
17203
18873
|
|
|
17204
18874
|
|
|
17205
18875
|
|
|
18876
|
+
|
|
18877
|
+
|
|
18878
|
+
|
|
18879
|
+
|
|
18880
|
+
|
|
18881
|
+
|
|
18882
|
+
|
|
18883
|
+
|
|
18884
|
+
|
|
18885
|
+
|
|
18886
|
+
|
|
18887
|
+
|
|
18888
|
+
|
|
18889
|
+
|
|
18890
|
+
|
|
18891
|
+
|
|
18892
|
+
|
|
18893
|
+
|
|
18894
|
+
|
|
18895
|
+
|
|
17206
18896
|
|
|
17207
18897
|
|
|
17208
18898
|
|
|
@@ -17371,6 +19061,26 @@ var TreeMap = class _TreeMap {
|
|
|
17371
19061
|
|
|
17372
19062
|
|
|
17373
19063
|
|
|
19064
|
+
|
|
19065
|
+
|
|
19066
|
+
|
|
19067
|
+
|
|
19068
|
+
|
|
19069
|
+
|
|
19070
|
+
|
|
19071
|
+
|
|
19072
|
+
|
|
19073
|
+
|
|
19074
|
+
|
|
19075
|
+
|
|
19076
|
+
|
|
19077
|
+
|
|
19078
|
+
|
|
19079
|
+
|
|
19080
|
+
|
|
19081
|
+
|
|
19082
|
+
|
|
19083
|
+
|
|
17374
19084
|
|
|
17375
19085
|
|
|
17376
19086
|
|
|
@@ -17529,6 +19239,26 @@ var TreeMap = class _TreeMap {
|
|
|
17529
19239
|
|
|
17530
19240
|
|
|
17531
19241
|
|
|
19242
|
+
|
|
19243
|
+
|
|
19244
|
+
|
|
19245
|
+
|
|
19246
|
+
|
|
19247
|
+
|
|
19248
|
+
|
|
19249
|
+
|
|
19250
|
+
|
|
19251
|
+
|
|
19252
|
+
|
|
19253
|
+
|
|
19254
|
+
|
|
19255
|
+
|
|
19256
|
+
|
|
19257
|
+
|
|
19258
|
+
|
|
19259
|
+
|
|
19260
|
+
|
|
19261
|
+
|
|
17532
19262
|
|
|
17533
19263
|
|
|
17534
19264
|
|
|
@@ -17678,6 +19408,26 @@ var TreeMap = class _TreeMap {
|
|
|
17678
19408
|
|
|
17679
19409
|
|
|
17680
19410
|
|
|
19411
|
+
|
|
19412
|
+
|
|
19413
|
+
|
|
19414
|
+
|
|
19415
|
+
|
|
19416
|
+
|
|
19417
|
+
|
|
19418
|
+
|
|
19419
|
+
|
|
19420
|
+
|
|
19421
|
+
|
|
19422
|
+
|
|
19423
|
+
|
|
19424
|
+
|
|
19425
|
+
|
|
19426
|
+
|
|
19427
|
+
|
|
19428
|
+
|
|
19429
|
+
|
|
19430
|
+
|
|
17681
19431
|
|
|
17682
19432
|
|
|
17683
19433
|
|
|
@@ -17831,6 +19581,26 @@ var TreeMap = class _TreeMap {
|
|
|
17831
19581
|
|
|
17832
19582
|
|
|
17833
19583
|
|
|
19584
|
+
|
|
19585
|
+
|
|
19586
|
+
|
|
19587
|
+
|
|
19588
|
+
|
|
19589
|
+
|
|
19590
|
+
|
|
19591
|
+
|
|
19592
|
+
|
|
19593
|
+
|
|
19594
|
+
|
|
19595
|
+
|
|
19596
|
+
|
|
19597
|
+
|
|
19598
|
+
|
|
19599
|
+
|
|
19600
|
+
|
|
19601
|
+
|
|
19602
|
+
|
|
19603
|
+
|
|
17834
19604
|
|
|
17835
19605
|
|
|
17836
19606
|
|
|
@@ -17981,6 +19751,26 @@ var TreeMap = class _TreeMap {
|
|
|
17981
19751
|
|
|
17982
19752
|
|
|
17983
19753
|
|
|
19754
|
+
|
|
19755
|
+
|
|
19756
|
+
|
|
19757
|
+
|
|
19758
|
+
|
|
19759
|
+
|
|
19760
|
+
|
|
19761
|
+
|
|
19762
|
+
|
|
19763
|
+
|
|
19764
|
+
|
|
19765
|
+
|
|
19766
|
+
|
|
19767
|
+
|
|
19768
|
+
|
|
19769
|
+
|
|
19770
|
+
|
|
19771
|
+
|
|
19772
|
+
|
|
19773
|
+
|
|
17984
19774
|
|
|
17985
19775
|
|
|
17986
19776
|
|
|
@@ -18134,6 +19924,26 @@ var TreeMap = class _TreeMap {
|
|
|
18134
19924
|
|
|
18135
19925
|
|
|
18136
19926
|
|
|
19927
|
+
|
|
19928
|
+
|
|
19929
|
+
|
|
19930
|
+
|
|
19931
|
+
|
|
19932
|
+
|
|
19933
|
+
|
|
19934
|
+
|
|
19935
|
+
|
|
19936
|
+
|
|
19937
|
+
|
|
19938
|
+
|
|
19939
|
+
|
|
19940
|
+
|
|
19941
|
+
|
|
19942
|
+
|
|
19943
|
+
|
|
19944
|
+
|
|
19945
|
+
|
|
19946
|
+
|
|
18137
19947
|
|
|
18138
19948
|
|
|
18139
19949
|
|
|
@@ -18287,6 +20097,26 @@ var TreeMap = class _TreeMap {
|
|
|
18287
20097
|
|
|
18288
20098
|
|
|
18289
20099
|
|
|
20100
|
+
|
|
20101
|
+
|
|
20102
|
+
|
|
20103
|
+
|
|
20104
|
+
|
|
20105
|
+
|
|
20106
|
+
|
|
20107
|
+
|
|
20108
|
+
|
|
20109
|
+
|
|
20110
|
+
|
|
20111
|
+
|
|
20112
|
+
|
|
20113
|
+
|
|
20114
|
+
|
|
20115
|
+
|
|
20116
|
+
|
|
20117
|
+
|
|
20118
|
+
|
|
20119
|
+
|
|
18290
20120
|
|
|
18291
20121
|
|
|
18292
20122
|
|
|
@@ -18443,6 +20273,26 @@ var TreeMap = class _TreeMap {
|
|
|
18443
20273
|
|
|
18444
20274
|
|
|
18445
20275
|
|
|
20276
|
+
|
|
20277
|
+
|
|
20278
|
+
|
|
20279
|
+
|
|
20280
|
+
|
|
20281
|
+
|
|
20282
|
+
|
|
20283
|
+
|
|
20284
|
+
|
|
20285
|
+
|
|
20286
|
+
|
|
20287
|
+
|
|
20288
|
+
|
|
20289
|
+
|
|
20290
|
+
|
|
20291
|
+
|
|
20292
|
+
|
|
20293
|
+
|
|
20294
|
+
|
|
20295
|
+
|
|
18446
20296
|
|
|
18447
20297
|
|
|
18448
20298
|
|
|
@@ -18620,14 +20470,34 @@ var TreeMap = class _TreeMap {
|
|
|
18620
20470
|
|
|
18621
20471
|
|
|
18622
20472
|
|
|
18623
|
-
|
|
18624
|
-
|
|
18625
|
-
|
|
18626
|
-
|
|
18627
|
-
|
|
18628
|
-
|
|
18629
|
-
|
|
18630
|
-
|
|
20473
|
+
|
|
20474
|
+
|
|
20475
|
+
|
|
20476
|
+
|
|
20477
|
+
|
|
20478
|
+
|
|
20479
|
+
|
|
20480
|
+
|
|
20481
|
+
|
|
20482
|
+
|
|
20483
|
+
|
|
20484
|
+
|
|
20485
|
+
|
|
20486
|
+
|
|
20487
|
+
|
|
20488
|
+
|
|
20489
|
+
|
|
20490
|
+
|
|
20491
|
+
|
|
20492
|
+
|
|
20493
|
+
* @example
|
|
20494
|
+
* // Aggregate values
|
|
20495
|
+
* const tm = new TreeMap<number, number>([[1, 10], [2, 20]]);
|
|
20496
|
+
* console.log(tm.reduce((acc, v) => acc + (v ?? 0), 0)); // 30;
|
|
20497
|
+
*/
|
|
20498
|
+
reduce(callbackfn, initialValue) {
|
|
20499
|
+
let acc = initialValue;
|
|
20500
|
+
let index = 0;
|
|
18631
20501
|
for (const [k, v] of this) acc = callbackfn(acc, v, k, index++, this);
|
|
18632
20502
|
return acc;
|
|
18633
20503
|
}
|
|
@@ -18749,6 +20619,26 @@ var TreeMap = class _TreeMap {
|
|
|
18749
20619
|
|
|
18750
20620
|
|
|
18751
20621
|
|
|
20622
|
+
|
|
20623
|
+
|
|
20624
|
+
|
|
20625
|
+
|
|
20626
|
+
|
|
20627
|
+
|
|
20628
|
+
|
|
20629
|
+
|
|
20630
|
+
|
|
20631
|
+
|
|
20632
|
+
|
|
20633
|
+
|
|
20634
|
+
|
|
20635
|
+
|
|
20636
|
+
|
|
20637
|
+
|
|
20638
|
+
|
|
20639
|
+
|
|
20640
|
+
|
|
20641
|
+
|
|
18752
20642
|
|
|
18753
20643
|
|
|
18754
20644
|
|
|
@@ -18901,6 +20791,26 @@ var TreeMap = class _TreeMap {
|
|
|
18901
20791
|
|
|
18902
20792
|
|
|
18903
20793
|
|
|
20794
|
+
|
|
20795
|
+
|
|
20796
|
+
|
|
20797
|
+
|
|
20798
|
+
|
|
20799
|
+
|
|
20800
|
+
|
|
20801
|
+
|
|
20802
|
+
|
|
20803
|
+
|
|
20804
|
+
|
|
20805
|
+
|
|
20806
|
+
|
|
20807
|
+
|
|
20808
|
+
|
|
20809
|
+
|
|
20810
|
+
|
|
20811
|
+
|
|
20812
|
+
|
|
20813
|
+
|
|
18904
20814
|
|
|
18905
20815
|
|
|
18906
20816
|
|
|
@@ -19054,6 +20964,26 @@ var TreeMap = class _TreeMap {
|
|
|
19054
20964
|
|
|
19055
20965
|
|
|
19056
20966
|
|
|
20967
|
+
|
|
20968
|
+
|
|
20969
|
+
|
|
20970
|
+
|
|
20971
|
+
|
|
20972
|
+
|
|
20973
|
+
|
|
20974
|
+
|
|
20975
|
+
|
|
20976
|
+
|
|
20977
|
+
|
|
20978
|
+
|
|
20979
|
+
|
|
20980
|
+
|
|
20981
|
+
|
|
20982
|
+
|
|
20983
|
+
|
|
20984
|
+
|
|
20985
|
+
|
|
20986
|
+
|
|
19057
20987
|
|
|
19058
20988
|
|
|
19059
20989
|
|
|
@@ -19208,6 +21138,26 @@ var TreeMap = class _TreeMap {
|
|
|
19208
21138
|
|
|
19209
21139
|
|
|
19210
21140
|
|
|
21141
|
+
|
|
21142
|
+
|
|
21143
|
+
|
|
21144
|
+
|
|
21145
|
+
|
|
21146
|
+
|
|
21147
|
+
|
|
21148
|
+
|
|
21149
|
+
|
|
21150
|
+
|
|
21151
|
+
|
|
21152
|
+
|
|
21153
|
+
|
|
21154
|
+
|
|
21155
|
+
|
|
21156
|
+
|
|
21157
|
+
|
|
21158
|
+
|
|
21159
|
+
|
|
21160
|
+
|
|
19211
21161
|
|
|
19212
21162
|
|
|
19213
21163
|
|
|
@@ -19357,6 +21307,26 @@ var TreeMap = class _TreeMap {
|
|
|
19357
21307
|
|
|
19358
21308
|
|
|
19359
21309
|
|
|
21310
|
+
|
|
21311
|
+
|
|
21312
|
+
|
|
21313
|
+
|
|
21314
|
+
|
|
21315
|
+
|
|
21316
|
+
|
|
21317
|
+
|
|
21318
|
+
|
|
21319
|
+
|
|
21320
|
+
|
|
21321
|
+
|
|
21322
|
+
|
|
21323
|
+
|
|
21324
|
+
|
|
21325
|
+
|
|
21326
|
+
|
|
21327
|
+
|
|
21328
|
+
|
|
21329
|
+
|
|
19360
21330
|
|
|
19361
21331
|
|
|
19362
21332
|
|
|
@@ -19416,6 +21386,10 @@ var TreeMap = class _TreeMap {
|
|
|
19416
21386
|
|
|
19417
21387
|
|
|
19418
21388
|
|
|
21389
|
+
|
|
21390
|
+
|
|
21391
|
+
|
|
21392
|
+
|
|
19419
21393
|
|
|
19420
21394
|
|
|
19421
21395
|
|
|
@@ -19480,6 +21454,10 @@ var TreeMap = class _TreeMap {
|
|
|
19480
21454
|
|
|
19481
21455
|
|
|
19482
21456
|
|
|
21457
|
+
|
|
21458
|
+
|
|
21459
|
+
|
|
21460
|
+
|
|
19483
21461
|
|
|
19484
21462
|
|
|
19485
21463
|
|
|
@@ -19528,6 +21506,10 @@ var TreeMap = class _TreeMap {
|
|
|
19528
21506
|
|
|
19529
21507
|
|
|
19530
21508
|
|
|
21509
|
+
|
|
21510
|
+
|
|
21511
|
+
|
|
21512
|
+
|
|
19531
21513
|
|
|
19532
21514
|
|
|
19533
21515
|
|
|
@@ -19580,6 +21562,10 @@ var TreeMap = class _TreeMap {
|
|
|
19580
21562
|
|
|
19581
21563
|
|
|
19582
21564
|
|
|
21565
|
+
|
|
21566
|
+
|
|
21567
|
+
|
|
21568
|
+
|
|
19583
21569
|
|
|
19584
21570
|
|
|
19585
21571
|
|
|
@@ -19707,6 +21693,22 @@ var TreeMap = class _TreeMap {
|
|
|
19707
21693
|
|
|
19708
21694
|
|
|
19709
21695
|
|
|
21696
|
+
|
|
21697
|
+
|
|
21698
|
+
|
|
21699
|
+
|
|
21700
|
+
|
|
21701
|
+
|
|
21702
|
+
|
|
21703
|
+
|
|
21704
|
+
|
|
21705
|
+
|
|
21706
|
+
|
|
21707
|
+
|
|
21708
|
+
|
|
21709
|
+
|
|
21710
|
+
|
|
21711
|
+
|
|
19710
21712
|
|
|
19711
21713
|
|
|
19712
21714
|
|
|
@@ -19864,6 +21866,22 @@ var TreeMap = class _TreeMap {
|
|
|
19864
21866
|
|
|
19865
21867
|
|
|
19866
21868
|
|
|
21869
|
+
|
|
21870
|
+
|
|
21871
|
+
|
|
21872
|
+
|
|
21873
|
+
|
|
21874
|
+
|
|
21875
|
+
|
|
21876
|
+
|
|
21877
|
+
|
|
21878
|
+
|
|
21879
|
+
|
|
21880
|
+
|
|
21881
|
+
|
|
21882
|
+
|
|
21883
|
+
|
|
21884
|
+
|
|
19867
21885
|
|
|
19868
21886
|
|
|
19869
21887
|
|
|
@@ -20005,6 +22023,22 @@ var TreeMap = class _TreeMap {
|
|
|
20005
22023
|
|
|
20006
22024
|
|
|
20007
22025
|
|
|
22026
|
+
|
|
22027
|
+
|
|
22028
|
+
|
|
22029
|
+
|
|
22030
|
+
|
|
22031
|
+
|
|
22032
|
+
|
|
22033
|
+
|
|
22034
|
+
|
|
22035
|
+
|
|
22036
|
+
|
|
22037
|
+
|
|
22038
|
+
|
|
22039
|
+
|
|
22040
|
+
|
|
22041
|
+
|
|
20008
22042
|
|
|
20009
22043
|
|
|
20010
22044
|
|
|
@@ -20146,6 +22180,22 @@ var TreeMap = class _TreeMap {
|
|
|
20146
22180
|
|
|
20147
22181
|
|
|
20148
22182
|
|
|
22183
|
+
|
|
22184
|
+
|
|
22185
|
+
|
|
22186
|
+
|
|
22187
|
+
|
|
22188
|
+
|
|
22189
|
+
|
|
22190
|
+
|
|
22191
|
+
|
|
22192
|
+
|
|
22193
|
+
|
|
22194
|
+
|
|
22195
|
+
|
|
22196
|
+
|
|
22197
|
+
|
|
22198
|
+
|
|
20149
22199
|
|
|
20150
22200
|
|
|
20151
22201
|
|
|
@@ -20288,6 +22338,22 @@ var TreeMap = class _TreeMap {
|
|
|
20288
22338
|
|
|
20289
22339
|
|
|
20290
22340
|
|
|
22341
|
+
|
|
22342
|
+
|
|
22343
|
+
|
|
22344
|
+
|
|
22345
|
+
|
|
22346
|
+
|
|
22347
|
+
|
|
22348
|
+
|
|
22349
|
+
|
|
22350
|
+
|
|
22351
|
+
|
|
22352
|
+
|
|
22353
|
+
|
|
22354
|
+
|
|
22355
|
+
|
|
22356
|
+
|
|
20291
22357
|
|
|
20292
22358
|
|
|
20293
22359
|
|
|
@@ -20352,6 +22418,68 @@ var TreeMap = class _TreeMap {
|
|
|
20352
22418
|
}
|
|
20353
22419
|
return out;
|
|
20354
22420
|
}
|
|
22421
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
22422
|
+
/**
|
|
22423
|
+
* Returns the entry at the k-th position in tree order (0-indexed).
|
|
22424
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
22425
|
+
|
|
22426
|
+
|
|
22427
|
+
|
|
22428
|
+
* @example
|
|
22429
|
+
* // Find k-th entry in a TreeMap
|
|
22430
|
+
* const map = new TreeMap<string, number>(
|
|
22431
|
+
* [['alice', 95], ['bob', 87], ['charlie', 92]],
|
|
22432
|
+
* { enableOrderStatistic: true }
|
|
22433
|
+
* );
|
|
22434
|
+
* console.log(map.getByRank(0)); // 'alice';
|
|
22435
|
+
* console.log(map.getByRank(1)); // 'bob';
|
|
22436
|
+
* console.log(map.getByRank(2)); // 'charlie';
|
|
22437
|
+
*/
|
|
22438
|
+
getByRank(k) {
|
|
22439
|
+
const key = this.#core.getByRank(k);
|
|
22440
|
+
if (key === void 0) return void 0;
|
|
22441
|
+
return [key, this.#core.get(key)];
|
|
22442
|
+
}
|
|
22443
|
+
/**
|
|
22444
|
+
* Returns the 0-based rank of a key (number of elements that precede it in tree order).
|
|
22445
|
+
* @remarks Time O(log n). Requires `enableOrderStatistic: true`.
|
|
22446
|
+
* @example
|
|
22447
|
+
* // Get the rank of a key in sorted order
|
|
22448
|
+
* const tree = new TreeMap<number>(
|
|
22449
|
+
* [10, 20, 30, 40, 50],
|
|
22450
|
+
* { enableOrderStatistic: true }
|
|
22451
|
+
* );
|
|
22452
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
22453
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
22454
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
22455
|
+
* console.log(tree.getRank(25)); // 2;
|
|
22456
|
+
*/
|
|
22457
|
+
getRank(key) {
|
|
22458
|
+
return this.#core.getRank(key);
|
|
22459
|
+
}
|
|
22460
|
+
/**
|
|
22461
|
+
* Returns keys by rank range (0-indexed, inclusive on both ends).
|
|
22462
|
+
* @remarks Time O(log n + k). Requires `enableOrderStatistic: true`.
|
|
22463
|
+
|
|
22464
|
+
* @example
|
|
22465
|
+
* // Pagination with rangeByRank
|
|
22466
|
+
* const tree = new TreeMap<number>(
|
|
22467
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
22468
|
+
* { enableOrderStatistic: true }
|
|
22469
|
+
* );
|
|
22470
|
+
* const pageSize = 3;
|
|
22471
|
+
*
|
|
22472
|
+
* // Page 1
|
|
22473
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
22474
|
+
* // Page 2
|
|
22475
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
22476
|
+
* // Page 3
|
|
22477
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
22478
|
+
*/
|
|
22479
|
+
rangeByRank(start, end) {
|
|
22480
|
+
const keys = this.#core.rangeByRank(start, end);
|
|
22481
|
+
return keys.filter((k) => k !== void 0).map((k) => [k, this.#core.get(k)]);
|
|
22482
|
+
}
|
|
20355
22483
|
/**
|
|
20356
22484
|
* Creates a shallow clone of this map.
|
|
20357
22485
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -20494,20 +22622,40 @@ var TreeMap = class _TreeMap {
|
|
|
20494
22622
|
|
|
20495
22623
|
|
|
20496
22624
|
|
|
20497
|
-
|
|
20498
|
-
|
|
20499
|
-
|
|
20500
|
-
|
|
20501
|
-
|
|
20502
|
-
|
|
20503
|
-
|
|
20504
|
-
|
|
20505
|
-
|
|
20506
|
-
|
|
20507
|
-
|
|
20508
|
-
|
|
20509
|
-
|
|
20510
|
-
|
|
22625
|
+
|
|
22626
|
+
|
|
22627
|
+
|
|
22628
|
+
|
|
22629
|
+
|
|
22630
|
+
|
|
22631
|
+
|
|
22632
|
+
|
|
22633
|
+
|
|
22634
|
+
|
|
22635
|
+
|
|
22636
|
+
|
|
22637
|
+
|
|
22638
|
+
|
|
22639
|
+
|
|
22640
|
+
|
|
22641
|
+
|
|
22642
|
+
|
|
22643
|
+
|
|
22644
|
+
|
|
22645
|
+
* @example
|
|
22646
|
+
* // Deep clone
|
|
22647
|
+
* const tm = new TreeMap<number, string>([[1, 'a'], [2, 'b']]);
|
|
22648
|
+
* const copy = tm.clone();
|
|
22649
|
+
* copy.delete(1);
|
|
22650
|
+
* console.log(tm.has(1)); // true;
|
|
22651
|
+
*/
|
|
22652
|
+
clone() {
|
|
22653
|
+
return new _TreeMap(this, {
|
|
22654
|
+
comparator: this.#isDefaultComparator ? void 0 : this.#userComparator,
|
|
22655
|
+
isMapMode: this.#core.isMapMode
|
|
22656
|
+
});
|
|
22657
|
+
}
|
|
22658
|
+
};
|
|
20511
22659
|
|
|
20512
22660
|
// src/data-structures/binary-tree/tree-multi-set.ts
|
|
20513
22661
|
var TreeMultiSet = class _TreeMultiSet {
|
|
@@ -20535,7 +22683,7 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20535
22683
|
const toElementFn = options.toElementFn;
|
|
20536
22684
|
const comparator = options.comparator ?? TreeSet.createDefaultComparator();
|
|
20537
22685
|
this.#isDefaultComparator = options.comparator === void 0;
|
|
20538
|
-
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode });
|
|
22686
|
+
this.#core = new RedBlackTree([], { comparator, isMapMode: options.isMapMode, enableOrderStatistic: options.enableOrderStatistic });
|
|
20539
22687
|
for (const item of elements) {
|
|
20540
22688
|
const k = toElementFn ? toElementFn(item) : item;
|
|
20541
22689
|
this.add(k);
|
|
@@ -20548,22 +22696,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20548
22696
|
_validateKey(key) {
|
|
20549
22697
|
if (!this.#isDefaultComparator) return;
|
|
20550
22698
|
if (typeof key === "number") {
|
|
20551
|
-
if (Number.isNaN(key))
|
|
22699
|
+
if (Number.isNaN(key)) raise(TypeError, ERR.invalidNaN("TreeMultiSet"));
|
|
20552
22700
|
return;
|
|
20553
22701
|
}
|
|
20554
22702
|
if (typeof key === "string") return;
|
|
20555
22703
|
if (key instanceof Date) {
|
|
20556
|
-
if (Number.isNaN(key.getTime()))
|
|
22704
|
+
if (Number.isNaN(key.getTime())) raise(TypeError, ERR.invalidDate("TreeMultiSet"));
|
|
20557
22705
|
return;
|
|
20558
22706
|
}
|
|
20559
|
-
|
|
22707
|
+
raise(TypeError, ERR.comparatorRequired("TreeMultiSet"));
|
|
20560
22708
|
}
|
|
20561
22709
|
/**
|
|
20562
22710
|
* Validates that count is a non-negative safe integer.
|
|
20563
22711
|
* @remarks Time O(1), Space O(1)
|
|
20564
22712
|
*/
|
|
20565
22713
|
_validateCount(n) {
|
|
20566
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
22714
|
+
if (!Number.isSafeInteger(n) || n < 0) raise(RangeError, ERR.invalidArgument("count must be a safe integer >= 0.", "TreeMultiSet"));
|
|
20567
22715
|
}
|
|
20568
22716
|
/**
|
|
20569
22717
|
* Total occurrences (sumCounts).
|
|
@@ -20592,6 +22740,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20592
22740
|
|
|
20593
22741
|
|
|
20594
22742
|
|
|
22743
|
+
|
|
22744
|
+
|
|
22745
|
+
|
|
22746
|
+
|
|
20595
22747
|
|
|
20596
22748
|
|
|
20597
22749
|
|
|
@@ -20727,6 +22879,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20727
22879
|
|
|
20728
22880
|
|
|
20729
22881
|
|
|
22882
|
+
|
|
22883
|
+
|
|
22884
|
+
|
|
22885
|
+
|
|
22886
|
+
|
|
22887
|
+
|
|
22888
|
+
|
|
22889
|
+
|
|
22890
|
+
|
|
22891
|
+
|
|
22892
|
+
|
|
22893
|
+
|
|
22894
|
+
|
|
22895
|
+
|
|
22896
|
+
|
|
22897
|
+
|
|
22898
|
+
|
|
22899
|
+
|
|
22900
|
+
|
|
22901
|
+
|
|
20730
22902
|
|
|
20731
22903
|
|
|
20732
22904
|
|
|
@@ -20878,6 +23050,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20878
23050
|
|
|
20879
23051
|
|
|
20880
23052
|
|
|
23053
|
+
|
|
23054
|
+
|
|
23055
|
+
|
|
23056
|
+
|
|
23057
|
+
|
|
23058
|
+
|
|
23059
|
+
|
|
23060
|
+
|
|
23061
|
+
|
|
23062
|
+
|
|
23063
|
+
|
|
23064
|
+
|
|
23065
|
+
|
|
23066
|
+
|
|
23067
|
+
|
|
23068
|
+
|
|
23069
|
+
|
|
23070
|
+
|
|
23071
|
+
|
|
23072
|
+
|
|
20881
23073
|
|
|
20882
23074
|
|
|
20883
23075
|
|
|
@@ -20930,6 +23122,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
20930
23122
|
|
|
20931
23123
|
|
|
20932
23124
|
|
|
23125
|
+
|
|
23126
|
+
|
|
23127
|
+
|
|
23128
|
+
|
|
20933
23129
|
|
|
20934
23130
|
|
|
20935
23131
|
|
|
@@ -21060,6 +23256,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21060
23256
|
|
|
21061
23257
|
|
|
21062
23258
|
|
|
23259
|
+
|
|
23260
|
+
|
|
23261
|
+
|
|
23262
|
+
|
|
23263
|
+
|
|
23264
|
+
|
|
23265
|
+
|
|
23266
|
+
|
|
23267
|
+
|
|
23268
|
+
|
|
23269
|
+
|
|
23270
|
+
|
|
23271
|
+
|
|
23272
|
+
|
|
23273
|
+
|
|
23274
|
+
|
|
23275
|
+
|
|
23276
|
+
|
|
23277
|
+
|
|
23278
|
+
|
|
21063
23279
|
|
|
21064
23280
|
|
|
21065
23281
|
|
|
@@ -21121,6 +23337,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21121
23337
|
|
|
21122
23338
|
|
|
21123
23339
|
|
|
23340
|
+
|
|
23341
|
+
|
|
23342
|
+
|
|
23343
|
+
|
|
21124
23344
|
|
|
21125
23345
|
|
|
21126
23346
|
|
|
@@ -21269,6 +23489,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21269
23489
|
|
|
21270
23490
|
|
|
21271
23491
|
|
|
23492
|
+
|
|
23493
|
+
|
|
23494
|
+
|
|
23495
|
+
|
|
23496
|
+
|
|
23497
|
+
|
|
23498
|
+
|
|
23499
|
+
|
|
23500
|
+
|
|
23501
|
+
|
|
23502
|
+
|
|
23503
|
+
|
|
23504
|
+
|
|
23505
|
+
|
|
23506
|
+
|
|
23507
|
+
|
|
23508
|
+
|
|
23509
|
+
|
|
23510
|
+
|
|
23511
|
+
|
|
21272
23512
|
|
|
21273
23513
|
|
|
21274
23514
|
|
|
@@ -21331,6 +23571,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21331
23571
|
|
|
21332
23572
|
|
|
21333
23573
|
|
|
23574
|
+
|
|
23575
|
+
|
|
23576
|
+
|
|
23577
|
+
|
|
21334
23578
|
|
|
21335
23579
|
|
|
21336
23580
|
|
|
@@ -21371,6 +23615,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21371
23615
|
|
|
21372
23616
|
|
|
21373
23617
|
|
|
23618
|
+
|
|
23619
|
+
|
|
23620
|
+
|
|
23621
|
+
|
|
21374
23622
|
|
|
21375
23623
|
|
|
21376
23624
|
|
|
@@ -21506,6 +23754,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21506
23754
|
|
|
21507
23755
|
|
|
21508
23756
|
|
|
23757
|
+
|
|
23758
|
+
|
|
23759
|
+
|
|
23760
|
+
|
|
23761
|
+
|
|
23762
|
+
|
|
23763
|
+
|
|
23764
|
+
|
|
23765
|
+
|
|
23766
|
+
|
|
23767
|
+
|
|
23768
|
+
|
|
23769
|
+
|
|
23770
|
+
|
|
23771
|
+
|
|
23772
|
+
|
|
23773
|
+
|
|
23774
|
+
|
|
23775
|
+
|
|
23776
|
+
|
|
21509
23777
|
|
|
21510
23778
|
|
|
21511
23779
|
|
|
@@ -21667,6 +23935,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21667
23935
|
|
|
21668
23936
|
|
|
21669
23937
|
|
|
23938
|
+
|
|
23939
|
+
|
|
23940
|
+
|
|
23941
|
+
|
|
23942
|
+
|
|
23943
|
+
|
|
23944
|
+
|
|
23945
|
+
|
|
23946
|
+
|
|
23947
|
+
|
|
23948
|
+
|
|
23949
|
+
|
|
23950
|
+
|
|
23951
|
+
|
|
23952
|
+
|
|
23953
|
+
|
|
23954
|
+
|
|
23955
|
+
|
|
23956
|
+
|
|
23957
|
+
|
|
21670
23958
|
|
|
21671
23959
|
|
|
21672
23960
|
|
|
@@ -21718,6 +24006,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21718
24006
|
|
|
21719
24007
|
|
|
21720
24008
|
|
|
24009
|
+
|
|
24010
|
+
|
|
24011
|
+
|
|
24012
|
+
|
|
21721
24013
|
|
|
21722
24014
|
|
|
21723
24015
|
|
|
@@ -21753,6 +24045,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21753
24045
|
|
|
21754
24046
|
|
|
21755
24047
|
|
|
24048
|
+
|
|
24049
|
+
|
|
24050
|
+
|
|
24051
|
+
|
|
21756
24052
|
|
|
21757
24053
|
|
|
21758
24054
|
|
|
@@ -21897,6 +24193,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21897
24193
|
|
|
21898
24194
|
|
|
21899
24195
|
|
|
24196
|
+
|
|
24197
|
+
|
|
24198
|
+
|
|
24199
|
+
|
|
24200
|
+
|
|
24201
|
+
|
|
24202
|
+
|
|
24203
|
+
|
|
24204
|
+
|
|
24205
|
+
|
|
24206
|
+
|
|
24207
|
+
|
|
24208
|
+
|
|
24209
|
+
|
|
24210
|
+
|
|
24211
|
+
|
|
24212
|
+
|
|
24213
|
+
|
|
24214
|
+
|
|
24215
|
+
|
|
21900
24216
|
|
|
21901
24217
|
|
|
21902
24218
|
|
|
@@ -21951,6 +24267,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21951
24267
|
|
|
21952
24268
|
|
|
21953
24269
|
|
|
24270
|
+
|
|
24271
|
+
|
|
24272
|
+
|
|
24273
|
+
|
|
21954
24274
|
|
|
21955
24275
|
|
|
21956
24276
|
|
|
@@ -21987,6 +24307,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
21987
24307
|
|
|
21988
24308
|
|
|
21989
24309
|
|
|
24310
|
+
|
|
24311
|
+
|
|
24312
|
+
|
|
24313
|
+
|
|
21990
24314
|
|
|
21991
24315
|
|
|
21992
24316
|
|
|
@@ -22023,6 +24347,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22023
24347
|
|
|
22024
24348
|
|
|
22025
24349
|
|
|
24350
|
+
|
|
24351
|
+
|
|
24352
|
+
|
|
24353
|
+
|
|
22026
24354
|
|
|
22027
24355
|
|
|
22028
24356
|
|
|
@@ -22063,6 +24391,10 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22063
24391
|
|
|
22064
24392
|
|
|
22065
24393
|
|
|
24394
|
+
|
|
24395
|
+
|
|
24396
|
+
|
|
24397
|
+
|
|
22066
24398
|
|
|
22067
24399
|
|
|
22068
24400
|
|
|
@@ -22177,6 +24509,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22177
24509
|
|
|
22178
24510
|
|
|
22179
24511
|
|
|
24512
|
+
|
|
24513
|
+
|
|
24514
|
+
|
|
24515
|
+
|
|
24516
|
+
|
|
24517
|
+
|
|
24518
|
+
|
|
24519
|
+
|
|
24520
|
+
|
|
24521
|
+
|
|
24522
|
+
|
|
24523
|
+
|
|
24524
|
+
|
|
24525
|
+
|
|
24526
|
+
|
|
24527
|
+
|
|
22180
24528
|
|
|
22181
24529
|
|
|
22182
24530
|
|
|
@@ -22302,6 +24650,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22302
24650
|
|
|
22303
24651
|
|
|
22304
24652
|
|
|
24653
|
+
|
|
24654
|
+
|
|
24655
|
+
|
|
24656
|
+
|
|
24657
|
+
|
|
24658
|
+
|
|
24659
|
+
|
|
24660
|
+
|
|
24661
|
+
|
|
24662
|
+
|
|
24663
|
+
|
|
24664
|
+
|
|
24665
|
+
|
|
24666
|
+
|
|
24667
|
+
|
|
24668
|
+
|
|
22305
24669
|
|
|
22306
24670
|
|
|
22307
24671
|
|
|
@@ -22427,6 +24791,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22427
24791
|
|
|
22428
24792
|
|
|
22429
24793
|
|
|
24794
|
+
|
|
24795
|
+
|
|
24796
|
+
|
|
24797
|
+
|
|
24798
|
+
|
|
24799
|
+
|
|
24800
|
+
|
|
24801
|
+
|
|
24802
|
+
|
|
24803
|
+
|
|
24804
|
+
|
|
24805
|
+
|
|
24806
|
+
|
|
24807
|
+
|
|
24808
|
+
|
|
24809
|
+
|
|
22430
24810
|
|
|
22431
24811
|
|
|
22432
24812
|
|
|
@@ -22551,6 +24931,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22551
24931
|
|
|
22552
24932
|
|
|
22553
24933
|
|
|
24934
|
+
|
|
24935
|
+
|
|
24936
|
+
|
|
24937
|
+
|
|
24938
|
+
|
|
24939
|
+
|
|
24940
|
+
|
|
24941
|
+
|
|
24942
|
+
|
|
24943
|
+
|
|
24944
|
+
|
|
24945
|
+
|
|
24946
|
+
|
|
24947
|
+
|
|
24948
|
+
|
|
24949
|
+
|
|
22554
24950
|
|
|
22555
24951
|
|
|
22556
24952
|
|
|
@@ -22701,6 +25097,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22701
25097
|
|
|
22702
25098
|
|
|
22703
25099
|
|
|
25100
|
+
|
|
25101
|
+
|
|
25102
|
+
|
|
25103
|
+
|
|
25104
|
+
|
|
25105
|
+
|
|
25106
|
+
|
|
25107
|
+
|
|
25108
|
+
|
|
25109
|
+
|
|
25110
|
+
|
|
25111
|
+
|
|
25112
|
+
|
|
25113
|
+
|
|
25114
|
+
|
|
25115
|
+
|
|
25116
|
+
|
|
25117
|
+
|
|
25118
|
+
|
|
25119
|
+
|
|
22704
25120
|
|
|
22705
25121
|
|
|
22706
25122
|
|
|
@@ -22857,6 +25273,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
22857
25273
|
|
|
22858
25274
|
|
|
22859
25275
|
|
|
25276
|
+
|
|
25277
|
+
|
|
25278
|
+
|
|
25279
|
+
|
|
25280
|
+
|
|
25281
|
+
|
|
25282
|
+
|
|
25283
|
+
|
|
25284
|
+
|
|
25285
|
+
|
|
25286
|
+
|
|
25287
|
+
|
|
25288
|
+
|
|
25289
|
+
|
|
25290
|
+
|
|
25291
|
+
|
|
25292
|
+
|
|
25293
|
+
|
|
25294
|
+
|
|
25295
|
+
|
|
22860
25296
|
|
|
22861
25297
|
|
|
22862
25298
|
|
|
@@ -23020,6 +25456,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23020
25456
|
|
|
23021
25457
|
|
|
23022
25458
|
|
|
25459
|
+
|
|
25460
|
+
|
|
25461
|
+
|
|
25462
|
+
|
|
25463
|
+
|
|
25464
|
+
|
|
25465
|
+
|
|
25466
|
+
|
|
25467
|
+
|
|
25468
|
+
|
|
25469
|
+
|
|
25470
|
+
|
|
25471
|
+
|
|
25472
|
+
|
|
25473
|
+
|
|
25474
|
+
|
|
25475
|
+
|
|
25476
|
+
|
|
25477
|
+
|
|
25478
|
+
|
|
23023
25479
|
|
|
23024
25480
|
|
|
23025
25481
|
|
|
@@ -23178,6 +25634,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23178
25634
|
|
|
23179
25635
|
|
|
23180
25636
|
|
|
25637
|
+
|
|
25638
|
+
|
|
25639
|
+
|
|
25640
|
+
|
|
25641
|
+
|
|
25642
|
+
|
|
25643
|
+
|
|
25644
|
+
|
|
25645
|
+
|
|
25646
|
+
|
|
25647
|
+
|
|
25648
|
+
|
|
25649
|
+
|
|
25650
|
+
|
|
25651
|
+
|
|
25652
|
+
|
|
25653
|
+
|
|
25654
|
+
|
|
25655
|
+
|
|
25656
|
+
|
|
23181
25657
|
|
|
23182
25658
|
|
|
23183
25659
|
|
|
@@ -23357,6 +25833,78 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23357
25833
|
|
|
23358
25834
|
|
|
23359
25835
|
|
|
25836
|
+
|
|
25837
|
+
|
|
25838
|
+
|
|
25839
|
+
|
|
25840
|
+
|
|
25841
|
+
|
|
25842
|
+
|
|
25843
|
+
|
|
25844
|
+
|
|
25845
|
+
|
|
25846
|
+
|
|
25847
|
+
|
|
25848
|
+
|
|
25849
|
+
|
|
25850
|
+
|
|
25851
|
+
|
|
25852
|
+
|
|
25853
|
+
|
|
25854
|
+
|
|
25855
|
+
|
|
25856
|
+
|
|
25857
|
+
* @example
|
|
25858
|
+
* // Order-statistic on BST
|
|
25859
|
+
* const tree = new TreeMultiSet<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
25860
|
+
* console.log(tree.getByRank(0)); // 10;
|
|
25861
|
+
* console.log(tree.getByRank(4)); // 50;
|
|
25862
|
+
* console.log(tree.getRank(30)); // 2;
|
|
25863
|
+
*/
|
|
25864
|
+
// ─── Order-Statistic Methods ───────────────────────────
|
|
25865
|
+
getByRank(k) {
|
|
25866
|
+
return this.#core.getByRank(k);
|
|
25867
|
+
}
|
|
25868
|
+
/**
|
|
25869
|
+
* Get the rank of a key in sorted order
|
|
25870
|
+
* @example
|
|
25871
|
+
* // Get the rank of a key in sorted order
|
|
25872
|
+
* const tree = new TreeMultiSet<number>(
|
|
25873
|
+
* [10, 20, 30, 40, 50],
|
|
25874
|
+
* { enableOrderStatistic: true }
|
|
25875
|
+
* );
|
|
25876
|
+
* console.log(tree.getRank(10)); // 0; // smallest → rank 0
|
|
25877
|
+
* console.log(tree.getRank(30)); // 2; // 2 elements before 30 in tree order
|
|
25878
|
+
* console.log(tree.getRank(50)); // 4; // largest → rank 4
|
|
25879
|
+
* console.log(tree.getRank(25)); // 2;
|
|
25880
|
+
*/
|
|
25881
|
+
getRank(key) {
|
|
25882
|
+
return this.#core.getRank(key);
|
|
25883
|
+
}
|
|
25884
|
+
/**
|
|
25885
|
+
* Get elements by rank range
|
|
25886
|
+
|
|
25887
|
+
* @example
|
|
25888
|
+
* // Pagination with rangeByRank
|
|
25889
|
+
* const tree = new TreeMultiSet<number>(
|
|
25890
|
+
* [10, 20, 30, 40, 50, 60, 70, 80, 90],
|
|
25891
|
+
* { enableOrderStatistic: true }
|
|
25892
|
+
* );
|
|
25893
|
+
* const pageSize = 3;
|
|
25894
|
+
*
|
|
25895
|
+
* // Page 1
|
|
25896
|
+
* console.log(tree.rangeByRank(0, pageSize - 1)); // [10, 20, 30];
|
|
25897
|
+
* // Page 2
|
|
25898
|
+
* console.log(tree.rangeByRank(pageSize, 2 * pageSize - 1)); // [40, 50, 60];
|
|
25899
|
+
* // Page 3
|
|
25900
|
+
* console.log(tree.rangeByRank(2 * pageSize, 3 * pageSize - 1)); // [70, 80, 90];
|
|
25901
|
+
*/
|
|
25902
|
+
rangeByRank(start, end) {
|
|
25903
|
+
return this.#core.rangeByRank(start, end).filter((k) => k !== void 0);
|
|
25904
|
+
}
|
|
25905
|
+
/**
|
|
25906
|
+
* Deep copy
|
|
25907
|
+
|
|
23360
25908
|
|
|
23361
25909
|
|
|
23362
25910
|
|
|
@@ -23475,6 +26023,22 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23475
26023
|
|
|
23476
26024
|
|
|
23477
26025
|
|
|
26026
|
+
|
|
26027
|
+
|
|
26028
|
+
|
|
26029
|
+
|
|
26030
|
+
|
|
26031
|
+
|
|
26032
|
+
|
|
26033
|
+
|
|
26034
|
+
|
|
26035
|
+
|
|
26036
|
+
|
|
26037
|
+
|
|
26038
|
+
|
|
26039
|
+
|
|
26040
|
+
|
|
26041
|
+
|
|
23478
26042
|
|
|
23479
26043
|
|
|
23480
26044
|
|
|
@@ -23626,6 +26190,26 @@ var TreeMultiSet = class _TreeMultiSet {
|
|
|
23626
26190
|
|
|
23627
26191
|
|
|
23628
26192
|
|
|
26193
|
+
|
|
26194
|
+
|
|
26195
|
+
|
|
26196
|
+
|
|
26197
|
+
|
|
26198
|
+
|
|
26199
|
+
|
|
26200
|
+
|
|
26201
|
+
|
|
26202
|
+
|
|
26203
|
+
|
|
26204
|
+
|
|
26205
|
+
|
|
26206
|
+
|
|
26207
|
+
|
|
26208
|
+
|
|
26209
|
+
|
|
26210
|
+
|
|
26211
|
+
|
|
26212
|
+
|
|
23629
26213
|
|
|
23630
26214
|
|
|
23631
26215
|
|