data-structure-typed 2.1.2 → 2.2.1
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 +1 -1
- package/CONTRIBUTING.md +4 -0
- package/README.md +66 -21
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +145 -169
- package/dist/cjs/index.cjs +1173 -583
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +13623 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +1173 -583
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +13545 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +65 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +61 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +58 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +59 -4
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +66 -3
- package/dist/types/types/data-structures/base/base.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +614 -53
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +20 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +103 -12
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
- package/src/data-structures/binary-tree/avl-tree.ts +109 -16
- package/src/data-structures/binary-tree/binary-tree.ts +3 -2
- package/src/data-structures/binary-tree/bst.ts +104 -12
- package/src/data-structures/binary-tree/red-black-tree.ts +110 -19
- package/src/data-structures/binary-tree/tree-counter.ts +102 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +124 -12
- package/src/data-structures/graph/abstract-graph.ts +8 -8
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/types/data-structures/base/base.ts +1 -1
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +39 -36
- package/test/performance/runner-config.json +4 -4
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +8 -7
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/bst.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +8 -7
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +7 -7
- package/test/unit/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/unit/data-structures/hash/hash-map.test.ts +14 -14
- package/tsup.node.config.js +40 -6
- package/test/performance/reportor.mjs +0 -505
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
5
|
|
|
8
6
|
// src/data-structures/base/iterable-entry-base.ts
|
|
9
|
-
var
|
|
7
|
+
var IterableEntryBase = class {
|
|
8
|
+
static {
|
|
9
|
+
__name(this, "IterableEntryBase");
|
|
10
|
+
}
|
|
10
11
|
/**
|
|
11
12
|
* Default iterator yielding `[key, value]` entries.
|
|
12
13
|
* @returns Iterator of `[K, V]`.
|
|
@@ -55,7 +56,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
55
56
|
every(predicate, thisArg) {
|
|
56
57
|
let index = 0;
|
|
57
58
|
for (const item of this) {
|
|
58
|
-
if (!predicate.call(thisArg, item[
|
|
59
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
59
60
|
return false;
|
|
60
61
|
}
|
|
61
62
|
}
|
|
@@ -71,7 +72,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
71
72
|
some(predicate, thisArg) {
|
|
72
73
|
let index = 0;
|
|
73
74
|
for (const item of this) {
|
|
74
|
-
if (predicate.call(thisArg, item[
|
|
75
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
75
76
|
return true;
|
|
76
77
|
}
|
|
77
78
|
}
|
|
@@ -87,7 +88,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
87
88
|
let index = 0;
|
|
88
89
|
for (const item of this) {
|
|
89
90
|
const [key, value] = item;
|
|
90
|
-
callbackfn.call(thisArg,
|
|
91
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
/**
|
|
@@ -101,7 +102,7 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
101
102
|
let index = 0;
|
|
102
103
|
for (const item of this) {
|
|
103
104
|
const [key, value] = item;
|
|
104
|
-
if (callbackfn.call(thisArg,
|
|
105
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
105
106
|
}
|
|
106
107
|
return;
|
|
107
108
|
}
|
|
@@ -175,11 +176,12 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
175
176
|
console.log(this.toVisual());
|
|
176
177
|
}
|
|
177
178
|
};
|
|
178
|
-
__name(_IterableEntryBase, "IterableEntryBase");
|
|
179
|
-
var IterableEntryBase = _IterableEntryBase;
|
|
180
179
|
|
|
181
180
|
// src/data-structures/base/iterable-element-base.ts
|
|
182
|
-
var
|
|
181
|
+
var IterableElementBase = class {
|
|
182
|
+
static {
|
|
183
|
+
__name(this, "IterableElementBase");
|
|
184
|
+
}
|
|
183
185
|
/**
|
|
184
186
|
* Create a new iterable base.
|
|
185
187
|
*
|
|
@@ -190,19 +192,19 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
190
192
|
* Time O(1), Space O(1).
|
|
191
193
|
*/
|
|
192
194
|
constructor(options) {
|
|
193
|
-
/**
|
|
194
|
-
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
195
|
-
*
|
|
196
|
-
* @remarks
|
|
197
|
-
* Time O(1), Space O(1).
|
|
198
|
-
*/
|
|
199
|
-
__publicField(this, "_toElementFn");
|
|
200
195
|
if (options) {
|
|
201
196
|
const { toElementFn } = options;
|
|
202
197
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
203
198
|
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
204
199
|
}
|
|
205
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
203
|
+
*
|
|
204
|
+
* @remarks
|
|
205
|
+
* Time O(1), Space O(1).
|
|
206
|
+
*/
|
|
207
|
+
_toElementFn;
|
|
206
208
|
/**
|
|
207
209
|
* Exposes the current `toElementFn`, if configured.
|
|
208
210
|
*
|
|
@@ -397,8 +399,6 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
397
399
|
console.log(this.toVisual());
|
|
398
400
|
}
|
|
399
401
|
};
|
|
400
|
-
__name(_IterableElementBase, "IterableElementBase");
|
|
401
|
-
var IterableElementBase = _IterableElementBase;
|
|
402
402
|
|
|
403
403
|
// src/utils/utils.ts
|
|
404
404
|
var uuidV4 = /* @__PURE__ */ __name(function() {
|
|
@@ -519,7 +519,10 @@ function toBinaryString(num, digit = 32) {
|
|
|
519
519
|
__name(toBinaryString, "toBinaryString");
|
|
520
520
|
|
|
521
521
|
// src/data-structures/hash/hash-map.ts
|
|
522
|
-
var
|
|
522
|
+
var HashMap = class extends IterableEntryBase {
|
|
523
|
+
static {
|
|
524
|
+
__name(this, "HashMap");
|
|
525
|
+
}
|
|
523
526
|
/**
|
|
524
527
|
* Create a HashMap and optionally bulk-insert entries.
|
|
525
528
|
* @remarks Time O(N), Space O(N)
|
|
@@ -529,11 +532,6 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
529
532
|
*/
|
|
530
533
|
constructor(entryOrRawElements = [], options) {
|
|
531
534
|
super();
|
|
532
|
-
__publicField(this, "_store", {});
|
|
533
|
-
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
534
|
-
__publicField(this, "_toEntryFn");
|
|
535
|
-
__publicField(this, "_size", 0);
|
|
536
|
-
__publicField(this, "_hashFn", /* @__PURE__ */ __name((key) => String(key), "_hashFn"));
|
|
537
535
|
if (options) {
|
|
538
536
|
const { hashFn, toEntryFn } = options;
|
|
539
537
|
if (hashFn) this._hashFn = hashFn;
|
|
@@ -541,6 +539,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
541
539
|
}
|
|
542
540
|
if (entryOrRawElements) this.setMany(entryOrRawElements);
|
|
543
541
|
}
|
|
542
|
+
_store = {};
|
|
544
543
|
/**
|
|
545
544
|
* Get the internal store for non-object keys.
|
|
546
545
|
* @remarks Time O(1), Space O(1)
|
|
@@ -549,6 +548,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
549
548
|
get store() {
|
|
550
549
|
return this._store;
|
|
551
550
|
}
|
|
551
|
+
_objMap = /* @__PURE__ */ new Map();
|
|
552
552
|
/**
|
|
553
553
|
* Get the internal Map used for object/function keys.
|
|
554
554
|
* @remarks Time O(1), Space O(1)
|
|
@@ -557,6 +557,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
557
557
|
get objMap() {
|
|
558
558
|
return this._objMap;
|
|
559
559
|
}
|
|
560
|
+
_toEntryFn;
|
|
560
561
|
/**
|
|
561
562
|
* Get the raw→entry converter function if present.
|
|
562
563
|
* @remarks Time O(1), Space O(1)
|
|
@@ -565,6 +566,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
565
566
|
get toEntryFn() {
|
|
566
567
|
return this._toEntryFn;
|
|
567
568
|
}
|
|
569
|
+
_size = 0;
|
|
568
570
|
/**
|
|
569
571
|
* Get the number of distinct keys stored.
|
|
570
572
|
* @remarks Time O(1), Space O(1)
|
|
@@ -573,6 +575,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
573
575
|
get size() {
|
|
574
576
|
return this._size;
|
|
575
577
|
}
|
|
578
|
+
_hashFn = /* @__PURE__ */ __name((key) => String(key), "_hashFn");
|
|
576
579
|
/**
|
|
577
580
|
* Get the current hash function for non-object keys.
|
|
578
581
|
* @remarks Time O(1), Space O(1)
|
|
@@ -648,10 +651,9 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
648
651
|
* @returns Value or undefined.
|
|
649
652
|
*/
|
|
650
653
|
get(key) {
|
|
651
|
-
var _a;
|
|
652
654
|
if (this._isObjKey(key)) return this.objMap.get(key);
|
|
653
655
|
const strKey = this._getNoObjKey(key);
|
|
654
|
-
return
|
|
656
|
+
return this._store[strKey]?.value;
|
|
655
657
|
}
|
|
656
658
|
/**
|
|
657
659
|
* Check if a key exists.
|
|
@@ -715,7 +717,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
715
717
|
map(callbackfn, thisArg) {
|
|
716
718
|
const out = this._createLike();
|
|
717
719
|
let index = 0;
|
|
718
|
-
for (const [key, value] of this) out.set(key, callbackfn.call(thisArg,
|
|
720
|
+
for (const [key, value] of this) out.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
719
721
|
return out;
|
|
720
722
|
}
|
|
721
723
|
/**
|
|
@@ -728,7 +730,7 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
728
730
|
filter(predicate, thisArg) {
|
|
729
731
|
const out = this._createLike();
|
|
730
732
|
let index = 0;
|
|
731
|
-
for (const [key, value] of this) if (predicate.call(thisArg,
|
|
733
|
+
for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
|
|
732
734
|
return out;
|
|
733
735
|
}
|
|
734
736
|
/**
|
|
@@ -776,9 +778,11 @@ var _HashMap = class _HashMap extends IterableEntryBase {
|
|
|
776
778
|
return strKey;
|
|
777
779
|
}
|
|
778
780
|
};
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
781
|
+
var LinkedHashMap = class extends IterableEntryBase {
|
|
782
|
+
static {
|
|
783
|
+
__name(this, "LinkedHashMap");
|
|
784
|
+
}
|
|
785
|
+
_sentinel;
|
|
782
786
|
/**
|
|
783
787
|
* Create a LinkedHashMap and optionally bulk-insert entries.
|
|
784
788
|
* @remarks Time O(N), Space O(N)
|
|
@@ -788,22 +792,6 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
788
792
|
*/
|
|
789
793
|
constructor(entryOrRawElements = [], options) {
|
|
790
794
|
super();
|
|
791
|
-
__publicField(this, "_sentinel");
|
|
792
|
-
__publicField(this, "_hashFn", /* @__PURE__ */ __name((key) => String(key), "_hashFn"));
|
|
793
|
-
__publicField(this, "_objHashFn", /* @__PURE__ */ __name((key) => key, "_objHashFn"));
|
|
794
|
-
__publicField(this, "_noObjMap", {});
|
|
795
|
-
__publicField(this, "_objMap", /* @__PURE__ */ new WeakMap());
|
|
796
|
-
__publicField(this, "_head");
|
|
797
|
-
__publicField(this, "_tail");
|
|
798
|
-
__publicField(this, "_toEntryFn", /* @__PURE__ */ __name((rawElement) => {
|
|
799
|
-
if (this.isEntry(rawElement)) {
|
|
800
|
-
return rawElement;
|
|
801
|
-
}
|
|
802
|
-
throw new Error(
|
|
803
|
-
"If `entryOrRawElements` does not adhere to [key,value], provide `options.toEntryFn` to transform raw records."
|
|
804
|
-
);
|
|
805
|
-
}, "_toEntryFn"));
|
|
806
|
-
__publicField(this, "_size", 0);
|
|
807
795
|
this._sentinel = {};
|
|
808
796
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
809
797
|
if (options) {
|
|
@@ -814,9 +802,11 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
814
802
|
}
|
|
815
803
|
if (entryOrRawElements) this.setMany(entryOrRawElements);
|
|
816
804
|
}
|
|
805
|
+
_hashFn = /* @__PURE__ */ __name((key) => String(key), "_hashFn");
|
|
817
806
|
get hashFn() {
|
|
818
807
|
return this._hashFn;
|
|
819
808
|
}
|
|
809
|
+
_objHashFn = /* @__PURE__ */ __name((key) => key, "_objHashFn");
|
|
820
810
|
/**
|
|
821
811
|
* Get the hash function for object/weak keys.
|
|
822
812
|
* @remarks Time O(1), Space O(1)
|
|
@@ -825,6 +815,7 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
825
815
|
get objHashFn() {
|
|
826
816
|
return this._objHashFn;
|
|
827
817
|
}
|
|
818
|
+
_noObjMap = {};
|
|
828
819
|
/**
|
|
829
820
|
* Get the internal record for non-object keys.
|
|
830
821
|
* @remarks Time O(1), Space O(1)
|
|
@@ -833,9 +824,11 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
833
824
|
get noObjMap() {
|
|
834
825
|
return this._noObjMap;
|
|
835
826
|
}
|
|
827
|
+
_objMap = /* @__PURE__ */ new WeakMap();
|
|
836
828
|
get objMap() {
|
|
837
829
|
return this._objMap;
|
|
838
830
|
}
|
|
831
|
+
_head;
|
|
839
832
|
/**
|
|
840
833
|
* Get the head node (first entry) sentinel link.
|
|
841
834
|
* @remarks Time O(1), Space O(1)
|
|
@@ -844,6 +837,7 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
844
837
|
get head() {
|
|
845
838
|
return this._head;
|
|
846
839
|
}
|
|
840
|
+
_tail;
|
|
847
841
|
/**
|
|
848
842
|
* Get the tail node (last entry) sentinel link.
|
|
849
843
|
* @remarks Time O(1), Space O(1)
|
|
@@ -852,9 +846,18 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
852
846
|
get tail() {
|
|
853
847
|
return this._tail;
|
|
854
848
|
}
|
|
849
|
+
_toEntryFn = /* @__PURE__ */ __name((rawElement) => {
|
|
850
|
+
if (this.isEntry(rawElement)) {
|
|
851
|
+
return rawElement;
|
|
852
|
+
}
|
|
853
|
+
throw new Error(
|
|
854
|
+
"If `entryOrRawElements` does not adhere to [key,value], provide `options.toEntryFn` to transform raw records."
|
|
855
|
+
);
|
|
856
|
+
}, "_toEntryFn");
|
|
855
857
|
get toEntryFn() {
|
|
856
858
|
return this._toEntryFn;
|
|
857
859
|
}
|
|
860
|
+
_size = 0;
|
|
858
861
|
get size() {
|
|
859
862
|
return this._size;
|
|
860
863
|
}
|
|
@@ -1052,7 +1055,7 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
1052
1055
|
const out = this._createLike();
|
|
1053
1056
|
let index = 0;
|
|
1054
1057
|
for (const [key, value] of this) {
|
|
1055
|
-
if (predicate.call(thisArg,
|
|
1058
|
+
if (predicate.call(thisArg, value, key, index, this)) out.set(key, value);
|
|
1056
1059
|
index++;
|
|
1057
1060
|
}
|
|
1058
1061
|
return out;
|
|
@@ -1070,7 +1073,7 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
1070
1073
|
const out = this._createLike();
|
|
1071
1074
|
let index = 0;
|
|
1072
1075
|
for (const [key, value] of this) {
|
|
1073
|
-
const [newKey, newValue] = callback.call(thisArg,
|
|
1076
|
+
const [newKey, newValue] = callback.call(thisArg, value, key, index, this);
|
|
1074
1077
|
out.set(newKey, newValue);
|
|
1075
1078
|
index++;
|
|
1076
1079
|
}
|
|
@@ -1097,22 +1100,22 @@ var _LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
|
1097
1100
|
return new Ctor(entries, options);
|
|
1098
1101
|
}
|
|
1099
1102
|
};
|
|
1100
|
-
__name(_LinkedHashMap, "LinkedHashMap");
|
|
1101
|
-
var LinkedHashMap = _LinkedHashMap;
|
|
1102
1103
|
|
|
1103
1104
|
// src/data-structures/base/linear-base.ts
|
|
1104
|
-
var
|
|
1105
|
+
var LinkedListNode = class {
|
|
1106
|
+
static {
|
|
1107
|
+
__name(this, "LinkedListNode");
|
|
1108
|
+
}
|
|
1105
1109
|
/**
|
|
1106
1110
|
* Initialize a node.
|
|
1107
1111
|
* @param value - Element value.
|
|
1108
1112
|
* @remarks Time O(1), Space O(1)
|
|
1109
1113
|
*/
|
|
1110
1114
|
constructor(value) {
|
|
1111
|
-
__publicField(this, "_value");
|
|
1112
|
-
__publicField(this, "_next");
|
|
1113
1115
|
this._value = value;
|
|
1114
1116
|
this._next = void 0;
|
|
1115
1117
|
}
|
|
1118
|
+
_value;
|
|
1116
1119
|
/**
|
|
1117
1120
|
* Element payload getter.
|
|
1118
1121
|
* @returns Element value.
|
|
@@ -1129,6 +1132,7 @@ var _LinkedListNode = class _LinkedListNode {
|
|
|
1129
1132
|
set value(value) {
|
|
1130
1133
|
this._value = value;
|
|
1131
1134
|
}
|
|
1135
|
+
_next;
|
|
1132
1136
|
/**
|
|
1133
1137
|
* Next node getter.
|
|
1134
1138
|
* @returns Next node or `undefined`.
|
|
@@ -1146,9 +1150,10 @@ var _LinkedListNode = class _LinkedListNode {
|
|
|
1146
1150
|
this._next = value;
|
|
1147
1151
|
}
|
|
1148
1152
|
};
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1153
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
1154
|
+
static {
|
|
1155
|
+
__name(this, "LinearBase");
|
|
1156
|
+
}
|
|
1152
1157
|
/**
|
|
1153
1158
|
* Construct a linear container with runtime options.
|
|
1154
1159
|
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
@@ -1156,12 +1161,12 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
1156
1161
|
*/
|
|
1157
1162
|
constructor(options) {
|
|
1158
1163
|
super(options);
|
|
1159
|
-
__publicField(this, "_maxLen", -1);
|
|
1160
1164
|
if (options) {
|
|
1161
1165
|
const { maxLen } = options;
|
|
1162
1166
|
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
1163
1167
|
}
|
|
1164
1168
|
}
|
|
1169
|
+
_maxLen = -1;
|
|
1165
1170
|
/**
|
|
1166
1171
|
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
1167
1172
|
* @returns Maximum allowed length.
|
|
@@ -1294,7 +1299,7 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
1294
1299
|
return array;
|
|
1295
1300
|
}
|
|
1296
1301
|
reduceRight(callbackfn, initialValue) {
|
|
1297
|
-
let accumulator = initialValue
|
|
1302
|
+
let accumulator = initialValue ?? 0;
|
|
1298
1303
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
1299
1304
|
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
1300
1305
|
}
|
|
@@ -1336,9 +1341,10 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
1336
1341
|
return this;
|
|
1337
1342
|
}
|
|
1338
1343
|
};
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1344
|
+
var LinearLinkedBase = class extends LinearBase {
|
|
1345
|
+
static {
|
|
1346
|
+
__name(this, "LinearLinkedBase");
|
|
1347
|
+
}
|
|
1342
1348
|
constructor(options) {
|
|
1343
1349
|
super(options);
|
|
1344
1350
|
if (options) {
|
|
@@ -1468,7 +1474,7 @@ var _LinearLinkedBase = class _LinearLinkedBase extends LinearBase {
|
|
|
1468
1474
|
return removedList;
|
|
1469
1475
|
}
|
|
1470
1476
|
reduceRight(callbackfn, initialValue) {
|
|
1471
|
-
let accumulator = initialValue
|
|
1477
|
+
let accumulator = initialValue ?? 0;
|
|
1472
1478
|
let index = this.length - 1;
|
|
1473
1479
|
for (const item of this._getReverseIterator()) {
|
|
1474
1480
|
accumulator = callbackfn(accumulator, item, index--, this);
|
|
@@ -1476,11 +1482,12 @@ var _LinearLinkedBase = class _LinearLinkedBase extends LinearBase {
|
|
|
1476
1482
|
return accumulator;
|
|
1477
1483
|
}
|
|
1478
1484
|
};
|
|
1479
|
-
__name(_LinearLinkedBase, "LinearLinkedBase");
|
|
1480
|
-
var LinearLinkedBase = _LinearLinkedBase;
|
|
1481
1485
|
|
|
1482
1486
|
// src/data-structures/linked-list/singly-linked-list.ts
|
|
1483
|
-
var
|
|
1487
|
+
var SinglyLinkedListNode = class extends LinkedListNode {
|
|
1488
|
+
static {
|
|
1489
|
+
__name(this, "SinglyLinkedListNode");
|
|
1490
|
+
}
|
|
1484
1491
|
/**
|
|
1485
1492
|
* Create a list node.
|
|
1486
1493
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1489,10 +1496,10 @@ var _SinglyLinkedListNode = class _SinglyLinkedListNode extends LinkedListNode {
|
|
|
1489
1496
|
*/
|
|
1490
1497
|
constructor(value) {
|
|
1491
1498
|
super(value);
|
|
1492
|
-
__publicField(this, "_next");
|
|
1493
1499
|
this._value = value;
|
|
1494
1500
|
this._next = void 0;
|
|
1495
1501
|
}
|
|
1502
|
+
_next;
|
|
1496
1503
|
/**
|
|
1497
1504
|
* Get the next node.
|
|
1498
1505
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1511,9 +1518,11 @@ var _SinglyLinkedListNode = class _SinglyLinkedListNode extends LinkedListNode {
|
|
|
1511
1518
|
this._next = value;
|
|
1512
1519
|
}
|
|
1513
1520
|
};
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1521
|
+
var SinglyLinkedList = class extends LinearLinkedBase {
|
|
1522
|
+
static {
|
|
1523
|
+
__name(this, "SinglyLinkedList");
|
|
1524
|
+
}
|
|
1525
|
+
_equals = Object.is;
|
|
1517
1526
|
/**
|
|
1518
1527
|
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
1519
1528
|
* @remarks Time O(N), Space O(N)
|
|
@@ -1523,12 +1532,9 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1523
1532
|
*/
|
|
1524
1533
|
constructor(elements = [], options) {
|
|
1525
1534
|
super(options);
|
|
1526
|
-
__publicField(this, "_equals", Object.is);
|
|
1527
|
-
__publicField(this, "_head");
|
|
1528
|
-
__publicField(this, "_tail");
|
|
1529
|
-
__publicField(this, "_length", 0);
|
|
1530
1535
|
this.pushMany(elements);
|
|
1531
1536
|
}
|
|
1537
|
+
_head;
|
|
1532
1538
|
/**
|
|
1533
1539
|
* Get the head node.
|
|
1534
1540
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1537,6 +1543,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1537
1543
|
get head() {
|
|
1538
1544
|
return this._head;
|
|
1539
1545
|
}
|
|
1546
|
+
_tail;
|
|
1540
1547
|
/**
|
|
1541
1548
|
* Get the tail node.
|
|
1542
1549
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1545,6 +1552,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1545
1552
|
get tail() {
|
|
1546
1553
|
return this._tail;
|
|
1547
1554
|
}
|
|
1555
|
+
_length = 0;
|
|
1548
1556
|
/**
|
|
1549
1557
|
* Get the number of elements.
|
|
1550
1558
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1559,8 +1567,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1559
1567
|
* @returns First element or undefined.
|
|
1560
1568
|
*/
|
|
1561
1569
|
get first() {
|
|
1562
|
-
|
|
1563
|
-
return (_a = this.head) == null ? void 0 : _a.value;
|
|
1570
|
+
return this.head?.value;
|
|
1564
1571
|
}
|
|
1565
1572
|
/**
|
|
1566
1573
|
* Get the last element value.
|
|
@@ -1568,8 +1575,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
1568
1575
|
* @returns Last element or undefined.
|
|
1569
1576
|
*/
|
|
1570
1577
|
get last() {
|
|
1571
|
-
|
|
1572
|
-
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
1578
|
+
return this.tail?.value;
|
|
1573
1579
|
}
|
|
1574
1580
|
/**
|
|
1575
1581
|
* Create a new list from an iterable of elements.
|
|
@@ -2048,7 +2054,7 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
2048
2054
|
* @returns A new SinglyLinkedList with mapped values.
|
|
2049
2055
|
*/
|
|
2050
2056
|
map(callback, options, thisArg) {
|
|
2051
|
-
const out = this._createLike([], { ...options
|
|
2057
|
+
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
2052
2058
|
let index = 0;
|
|
2053
2059
|
for (const value of this) out.push(callback.call(thisArg, value, index++, this));
|
|
2054
2060
|
return out;
|
|
@@ -2173,8 +2179,6 @@ var _SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
|
2173
2179
|
return this._createLike([], options);
|
|
2174
2180
|
}
|
|
2175
2181
|
};
|
|
2176
|
-
__name(_SinglyLinkedList, "SinglyLinkedList");
|
|
2177
|
-
var SinglyLinkedList = _SinglyLinkedList;
|
|
2178
2182
|
function elementOrPredicate(input, equals) {
|
|
2179
2183
|
if (input instanceof SinglyLinkedListNode) return (node) => node === input;
|
|
2180
2184
|
if (typeof input === "function") return input;
|
|
@@ -2184,7 +2188,10 @@ function elementOrPredicate(input, equals) {
|
|
|
2184
2188
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
2185
2189
|
|
|
2186
2190
|
// src/data-structures/linked-list/doubly-linked-list.ts
|
|
2187
|
-
var
|
|
2191
|
+
var DoublyLinkedListNode = class extends LinkedListNode {
|
|
2192
|
+
static {
|
|
2193
|
+
__name(this, "DoublyLinkedListNode");
|
|
2194
|
+
}
|
|
2188
2195
|
/**
|
|
2189
2196
|
* Create a node.
|
|
2190
2197
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2193,12 +2200,11 @@ var _DoublyLinkedListNode = class _DoublyLinkedListNode extends LinkedListNode {
|
|
|
2193
2200
|
*/
|
|
2194
2201
|
constructor(value) {
|
|
2195
2202
|
super(value);
|
|
2196
|
-
__publicField(this, "_next");
|
|
2197
|
-
__publicField(this, "_prev");
|
|
2198
2203
|
this._value = value;
|
|
2199
2204
|
this._next = void 0;
|
|
2200
2205
|
this._prev = void 0;
|
|
2201
2206
|
}
|
|
2207
|
+
_next;
|
|
2202
2208
|
/**
|
|
2203
2209
|
* Get the next node link.
|
|
2204
2210
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2216,6 +2222,7 @@ var _DoublyLinkedListNode = class _DoublyLinkedListNode extends LinkedListNode {
|
|
|
2216
2222
|
set next(value) {
|
|
2217
2223
|
this._next = value;
|
|
2218
2224
|
}
|
|
2225
|
+
_prev;
|
|
2219
2226
|
/**
|
|
2220
2227
|
* Get the previous node link.
|
|
2221
2228
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2234,9 +2241,11 @@ var _DoublyLinkedListNode = class _DoublyLinkedListNode extends LinkedListNode {
|
|
|
2234
2241
|
this._prev = value;
|
|
2235
2242
|
}
|
|
2236
2243
|
};
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2244
|
+
var DoublyLinkedList = class extends LinearLinkedBase {
|
|
2245
|
+
static {
|
|
2246
|
+
__name(this, "DoublyLinkedList");
|
|
2247
|
+
}
|
|
2248
|
+
_equals = Object.is;
|
|
2240
2249
|
/**
|
|
2241
2250
|
* Create a DoublyLinkedList and optionally bulk-insert elements.
|
|
2242
2251
|
* @remarks Time O(N), Space O(N)
|
|
@@ -2246,18 +2255,15 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
|
2246
2255
|
*/
|
|
2247
2256
|
constructor(elements = [], options) {
|
|
2248
2257
|
super(options);
|
|
2249
|
-
__publicField(this, "_equals", Object.is);
|
|
2250
|
-
__publicField(this, "_head");
|
|
2251
|
-
__publicField(this, "_tail");
|
|
2252
|
-
__publicField(this, "_length", 0);
|
|
2253
2258
|
this._head = void 0;
|
|
2254
2259
|
this._tail = void 0;
|
|
2255
2260
|
this._length = 0;
|
|
2256
|
-
if (
|
|
2261
|
+
if (options?.maxLen && Number.isInteger(options.maxLen) && options.maxLen > 0) {
|
|
2257
2262
|
this._maxLen = options.maxLen;
|
|
2258
2263
|
}
|
|
2259
2264
|
this.pushMany(elements);
|
|
2260
2265
|
}
|
|
2266
|
+
_head;
|
|
2261
2267
|
/**
|
|
2262
2268
|
* Get the head node.
|
|
2263
2269
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2266,6 +2272,7 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
|
2266
2272
|
get head() {
|
|
2267
2273
|
return this._head;
|
|
2268
2274
|
}
|
|
2275
|
+
_tail;
|
|
2269
2276
|
/**
|
|
2270
2277
|
* Get the tail node.
|
|
2271
2278
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2274,6 +2281,7 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
|
2274
2281
|
get tail() {
|
|
2275
2282
|
return this._tail;
|
|
2276
2283
|
}
|
|
2284
|
+
_length = 0;
|
|
2277
2285
|
/**
|
|
2278
2286
|
* Get the number of elements.
|
|
2279
2287
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2288,8 +2296,7 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
|
2288
2296
|
* @returns First element or undefined.
|
|
2289
2297
|
*/
|
|
2290
2298
|
get first() {
|
|
2291
|
-
|
|
2292
|
-
return (_a = this.head) == null ? void 0 : _a.value;
|
|
2299
|
+
return this.head?.value;
|
|
2293
2300
|
}
|
|
2294
2301
|
/**
|
|
2295
2302
|
* Get the last element value.
|
|
@@ -2297,8 +2304,7 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
|
2297
2304
|
* @returns Last element or undefined.
|
|
2298
2305
|
*/
|
|
2299
2306
|
get last() {
|
|
2300
|
-
|
|
2301
|
-
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
2307
|
+
return this.tail?.value;
|
|
2302
2308
|
}
|
|
2303
2309
|
/**
|
|
2304
2310
|
* Create a new list from an array of elements.
|
|
@@ -2713,7 +2719,7 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
|
2713
2719
|
* @returns A new DoublyLinkedList with mapped values.
|
|
2714
2720
|
*/
|
|
2715
2721
|
map(callback, options, thisArg) {
|
|
2716
|
-
const out = this._createLike([], { ...options
|
|
2722
|
+
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
2717
2723
|
let index = 0;
|
|
2718
2724
|
for (const v of this) out.push(callback.call(thisArg, v, index++, this));
|
|
2719
2725
|
return out;
|
|
@@ -2799,28 +2805,26 @@ var _DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
|
2799
2805
|
}
|
|
2800
2806
|
}
|
|
2801
2807
|
};
|
|
2802
|
-
__name(_DoublyLinkedList, "DoublyLinkedList");
|
|
2803
|
-
var DoublyLinkedList = _DoublyLinkedList;
|
|
2804
2808
|
|
|
2805
2809
|
// src/data-structures/linked-list/skip-linked-list.ts
|
|
2806
|
-
var
|
|
2810
|
+
var SkipListNode = class {
|
|
2811
|
+
static {
|
|
2812
|
+
__name(this, "SkipListNode");
|
|
2813
|
+
}
|
|
2814
|
+
key;
|
|
2815
|
+
value;
|
|
2816
|
+
forward;
|
|
2807
2817
|
constructor(key, value, level) {
|
|
2808
|
-
__publicField(this, "key");
|
|
2809
|
-
__publicField(this, "value");
|
|
2810
|
-
__publicField(this, "forward");
|
|
2811
2818
|
this.key = key;
|
|
2812
2819
|
this.value = value;
|
|
2813
2820
|
this.forward = new Array(level);
|
|
2814
2821
|
}
|
|
2815
2822
|
};
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2823
|
+
var SkipList = class {
|
|
2824
|
+
static {
|
|
2825
|
+
__name(this, "SkipList");
|
|
2826
|
+
}
|
|
2819
2827
|
constructor(elements = [], options) {
|
|
2820
|
-
__publicField(this, "_head", new SkipListNode(void 0, void 0, this.maxLevel));
|
|
2821
|
-
__publicField(this, "_level", 0);
|
|
2822
|
-
__publicField(this, "_maxLevel", 16);
|
|
2823
|
-
__publicField(this, "_probability", 0.5);
|
|
2824
2828
|
if (options) {
|
|
2825
2829
|
const { maxLevel, probability } = options;
|
|
2826
2830
|
if (typeof maxLevel === "number") this._maxLevel = maxLevel;
|
|
@@ -2830,15 +2834,19 @@ var _SkipList = class _SkipList {
|
|
|
2830
2834
|
for (const [key, value] of elements) this.add(key, value);
|
|
2831
2835
|
}
|
|
2832
2836
|
}
|
|
2837
|
+
_head = new SkipListNode(void 0, void 0, this.maxLevel);
|
|
2833
2838
|
get head() {
|
|
2834
2839
|
return this._head;
|
|
2835
2840
|
}
|
|
2841
|
+
_level = 0;
|
|
2836
2842
|
get level() {
|
|
2837
2843
|
return this._level;
|
|
2838
2844
|
}
|
|
2845
|
+
_maxLevel = 16;
|
|
2839
2846
|
get maxLevel() {
|
|
2840
2847
|
return this._maxLevel;
|
|
2841
2848
|
}
|
|
2849
|
+
_probability = 0.5;
|
|
2842
2850
|
get probability() {
|
|
2843
2851
|
return this._probability;
|
|
2844
2852
|
}
|
|
@@ -2944,11 +2952,13 @@ var _SkipList = class _SkipList {
|
|
|
2944
2952
|
return level;
|
|
2945
2953
|
}
|
|
2946
2954
|
};
|
|
2947
|
-
__name(_SkipList, "SkipList");
|
|
2948
|
-
var SkipList = _SkipList;
|
|
2949
2955
|
|
|
2950
2956
|
// src/data-structures/stack/stack.ts
|
|
2951
|
-
var
|
|
2957
|
+
var Stack = class extends IterableElementBase {
|
|
2958
|
+
static {
|
|
2959
|
+
__name(this, "Stack");
|
|
2960
|
+
}
|
|
2961
|
+
_equals = Object.is;
|
|
2952
2962
|
/**
|
|
2953
2963
|
* Create a Stack and optionally bulk-push elements.
|
|
2954
2964
|
* @remarks Time O(N), Space O(N)
|
|
@@ -2958,10 +2968,9 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
2958
2968
|
*/
|
|
2959
2969
|
constructor(elements = [], options) {
|
|
2960
2970
|
super(options);
|
|
2961
|
-
__publicField(this, "_equals", Object.is);
|
|
2962
|
-
__publicField(this, "_elements", []);
|
|
2963
2971
|
this.pushMany(elements);
|
|
2964
2972
|
}
|
|
2973
|
+
_elements = [];
|
|
2965
2974
|
/**
|
|
2966
2975
|
* Get the backing array of elements.
|
|
2967
2976
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3136,7 +3145,7 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
3136
3145
|
* @returns A new Stack with mapped elements.
|
|
3137
3146
|
*/
|
|
3138
3147
|
map(callback, options, thisArg) {
|
|
3139
|
-
const out = this._createLike([], { ...options
|
|
3148
|
+
const out = this._createLike([], { ...options ?? {} });
|
|
3140
3149
|
let index = 0;
|
|
3141
3150
|
for (const v of this) {
|
|
3142
3151
|
out.push(thisArg === void 0 ? callback(v, index, this) : callback.call(thisArg, v, index, this));
|
|
@@ -3196,11 +3205,12 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
3196
3205
|
for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
|
|
3197
3206
|
}
|
|
3198
3207
|
};
|
|
3199
|
-
__name(_Stack, "Stack");
|
|
3200
|
-
var Stack = _Stack;
|
|
3201
3208
|
|
|
3202
3209
|
// src/data-structures/queue/queue.ts
|
|
3203
|
-
var
|
|
3210
|
+
var Queue = class _Queue extends LinearBase {
|
|
3211
|
+
static {
|
|
3212
|
+
__name(this, "Queue");
|
|
3213
|
+
}
|
|
3204
3214
|
/**
|
|
3205
3215
|
* Create a Queue and optionally bulk-insert elements.
|
|
3206
3216
|
* @remarks Time O(N), Space O(N)
|
|
@@ -3210,15 +3220,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
3210
3220
|
*/
|
|
3211
3221
|
constructor(elements = [], options) {
|
|
3212
3222
|
super(options);
|
|
3213
|
-
__publicField(this, "_elements", []);
|
|
3214
|
-
__publicField(this, "_offset", 0);
|
|
3215
|
-
__publicField(this, "_autoCompactRatio", 0.5);
|
|
3216
3223
|
if (options) {
|
|
3217
3224
|
const { autoCompactRatio = 0.5 } = options;
|
|
3218
3225
|
this._autoCompactRatio = autoCompactRatio;
|
|
3219
3226
|
}
|
|
3220
3227
|
this.pushMany(elements);
|
|
3221
3228
|
}
|
|
3229
|
+
_elements = [];
|
|
3222
3230
|
/**
|
|
3223
3231
|
* Get the underlying array buffer.
|
|
3224
3232
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3227,6 +3235,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
3227
3235
|
get elements() {
|
|
3228
3236
|
return this._elements;
|
|
3229
3237
|
}
|
|
3238
|
+
_offset = 0;
|
|
3230
3239
|
/**
|
|
3231
3240
|
* Get the current start offset into the array.
|
|
3232
3241
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3235,6 +3244,7 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
3235
3244
|
get offset() {
|
|
3236
3245
|
return this._offset;
|
|
3237
3246
|
}
|
|
3247
|
+
_autoCompactRatio = 0.5;
|
|
3238
3248
|
/**
|
|
3239
3249
|
* Get the compaction threshold (offset/size).
|
|
3240
3250
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3479,11 +3489,10 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
3479
3489
|
* @returns A new Queue with mapped elements.
|
|
3480
3490
|
*/
|
|
3481
3491
|
map(callback, options, thisArg) {
|
|
3482
|
-
var _a, _b;
|
|
3483
3492
|
const out = new this.constructor([], {
|
|
3484
|
-
toElementFn: options
|
|
3485
|
-
maxLen:
|
|
3486
|
-
autoCompactRatio:
|
|
3493
|
+
toElementFn: options?.toElementFn,
|
|
3494
|
+
maxLen: options?.maxLen ?? this._maxLen,
|
|
3495
|
+
autoCompactRatio: options?.autoCompactRatio ?? this._autoCompactRatio
|
|
3487
3496
|
});
|
|
3488
3497
|
let index = 0;
|
|
3489
3498
|
for (const v of this)
|
|
@@ -3498,14 +3507,13 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
3498
3507
|
* @returns A new queue with mapped elements (same element type).
|
|
3499
3508
|
*/
|
|
3500
3509
|
mapSame(callback, thisArg) {
|
|
3501
|
-
var _a;
|
|
3502
3510
|
const Ctor = this.constructor;
|
|
3503
3511
|
const out = new Ctor([], {
|
|
3504
3512
|
toElementFn: this.toElementFn,
|
|
3505
3513
|
maxLen: this._maxLen,
|
|
3506
3514
|
autoCompactRatio: this._autoCompactRatio
|
|
3507
3515
|
});
|
|
3508
|
-
|
|
3516
|
+
out._setAutoCompactRatio?.(this._autoCompactRatio);
|
|
3509
3517
|
let index = 0;
|
|
3510
3518
|
for (const v of this) {
|
|
3511
3519
|
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
@@ -3565,9 +3573,10 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
3565
3573
|
return new Ctor(elements, options);
|
|
3566
3574
|
}
|
|
3567
3575
|
};
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3576
|
+
var LinkedListQueue = class extends SinglyLinkedList {
|
|
3577
|
+
static {
|
|
3578
|
+
__name(this, "LinkedListQueue");
|
|
3579
|
+
}
|
|
3571
3580
|
/**
|
|
3572
3581
|
* Deep clone this linked-list-based queue.
|
|
3573
3582
|
* @remarks Time O(N), Space O(N)
|
|
@@ -3579,11 +3588,13 @@ var _LinkedListQueue = class _LinkedListQueue extends SinglyLinkedList {
|
|
|
3579
3588
|
return out;
|
|
3580
3589
|
}
|
|
3581
3590
|
};
|
|
3582
|
-
__name(_LinkedListQueue, "LinkedListQueue");
|
|
3583
|
-
var LinkedListQueue = _LinkedListQueue;
|
|
3584
3591
|
|
|
3585
3592
|
// src/data-structures/queue/deque.ts
|
|
3586
|
-
var
|
|
3593
|
+
var Deque = class extends LinearBase {
|
|
3594
|
+
static {
|
|
3595
|
+
__name(this, "Deque");
|
|
3596
|
+
}
|
|
3597
|
+
_equals = Object.is;
|
|
3587
3598
|
/**
|
|
3588
3599
|
* Create a Deque and optionally bulk-insert elements.
|
|
3589
3600
|
* @remarks Time O(N), Space O(N)
|
|
@@ -3593,15 +3604,6 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3593
3604
|
*/
|
|
3594
3605
|
constructor(elements = [], options) {
|
|
3595
3606
|
super(options);
|
|
3596
|
-
__publicField(this, "_equals", Object.is);
|
|
3597
|
-
__publicField(this, "_bucketSize", 1 << 12);
|
|
3598
|
-
__publicField(this, "_bucketFirst", 0);
|
|
3599
|
-
__publicField(this, "_firstInBucket", 0);
|
|
3600
|
-
__publicField(this, "_bucketLast", 0);
|
|
3601
|
-
__publicField(this, "_lastInBucket", 0);
|
|
3602
|
-
__publicField(this, "_bucketCount", 0);
|
|
3603
|
-
__publicField(this, "_buckets", []);
|
|
3604
|
-
__publicField(this, "_length", 0);
|
|
3605
3607
|
if (options) {
|
|
3606
3608
|
const { bucketSize } = options;
|
|
3607
3609
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
@@ -3621,6 +3623,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3621
3623
|
this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
|
|
3622
3624
|
this.pushMany(elements);
|
|
3623
3625
|
}
|
|
3626
|
+
_bucketSize = 1 << 12;
|
|
3624
3627
|
/**
|
|
3625
3628
|
* Get the current bucket size.
|
|
3626
3629
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3629,6 +3632,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3629
3632
|
get bucketSize() {
|
|
3630
3633
|
return this._bucketSize;
|
|
3631
3634
|
}
|
|
3635
|
+
_bucketFirst = 0;
|
|
3632
3636
|
/**
|
|
3633
3637
|
* Get the index of the first bucket in use.
|
|
3634
3638
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3637,6 +3641,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3637
3641
|
get bucketFirst() {
|
|
3638
3642
|
return this._bucketFirst;
|
|
3639
3643
|
}
|
|
3644
|
+
_firstInBucket = 0;
|
|
3640
3645
|
/**
|
|
3641
3646
|
* Get the index inside the first bucket.
|
|
3642
3647
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3645,6 +3650,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3645
3650
|
get firstInBucket() {
|
|
3646
3651
|
return this._firstInBucket;
|
|
3647
3652
|
}
|
|
3653
|
+
_bucketLast = 0;
|
|
3648
3654
|
/**
|
|
3649
3655
|
* Get the index of the last bucket in use.
|
|
3650
3656
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3653,6 +3659,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3653
3659
|
get bucketLast() {
|
|
3654
3660
|
return this._bucketLast;
|
|
3655
3661
|
}
|
|
3662
|
+
_lastInBucket = 0;
|
|
3656
3663
|
/**
|
|
3657
3664
|
* Get the index inside the last bucket.
|
|
3658
3665
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3661,6 +3668,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3661
3668
|
get lastInBucket() {
|
|
3662
3669
|
return this._lastInBucket;
|
|
3663
3670
|
}
|
|
3671
|
+
_bucketCount = 0;
|
|
3664
3672
|
/**
|
|
3665
3673
|
* Get the number of buckets allocated.
|
|
3666
3674
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3669,6 +3677,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3669
3677
|
get bucketCount() {
|
|
3670
3678
|
return this._bucketCount;
|
|
3671
3679
|
}
|
|
3680
|
+
_buckets = [];
|
|
3672
3681
|
/**
|
|
3673
3682
|
* Get the internal buckets array.
|
|
3674
3683
|
* @remarks Time O(1), Space O(1)
|
|
@@ -3677,6 +3686,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
3677
3686
|
get buckets() {
|
|
3678
3687
|
return this._buckets;
|
|
3679
3688
|
}
|
|
3689
|
+
_length = 0;
|
|
3680
3690
|
/**
|
|
3681
3691
|
* Get the number of elements in the deque.
|
|
3682
3692
|
* @remarks Time O(1), Space O(1)
|
|
@@ -4192,7 +4202,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4192
4202
|
*/
|
|
4193
4203
|
map(callback, options, thisArg) {
|
|
4194
4204
|
const out = this._createLike([], {
|
|
4195
|
-
...options
|
|
4205
|
+
...options ?? {},
|
|
4196
4206
|
bucketSize: this._bucketSize,
|
|
4197
4207
|
maxLen: this._maxLen
|
|
4198
4208
|
});
|
|
@@ -4306,11 +4316,13 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4306
4316
|
}
|
|
4307
4317
|
}
|
|
4308
4318
|
};
|
|
4309
|
-
__name(_Deque, "Deque");
|
|
4310
|
-
var Deque = _Deque;
|
|
4311
4319
|
|
|
4312
4320
|
// src/data-structures/heap/heap.ts
|
|
4313
|
-
var
|
|
4321
|
+
var Heap = class _Heap extends IterableElementBase {
|
|
4322
|
+
static {
|
|
4323
|
+
__name(this, "Heap");
|
|
4324
|
+
}
|
|
4325
|
+
_equals = Object.is;
|
|
4314
4326
|
/**
|
|
4315
4327
|
* Create a Heap and optionally bulk-insert elements.
|
|
4316
4328
|
* @remarks Time O(N), Space O(N)
|
|
@@ -4320,23 +4332,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
4320
4332
|
*/
|
|
4321
4333
|
constructor(elements = [], options) {
|
|
4322
4334
|
super(options);
|
|
4323
|
-
__publicField(this, "_equals", Object.is);
|
|
4324
|
-
__publicField(this, "_elements", []);
|
|
4325
|
-
__publicField(this, "_DEFAULT_COMPARATOR", /* @__PURE__ */ __name((a, b) => {
|
|
4326
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
4327
|
-
throw TypeError("When comparing object types, define a custom comparator in options.");
|
|
4328
|
-
}
|
|
4329
|
-
if (a > b) return 1;
|
|
4330
|
-
if (a < b) return -1;
|
|
4331
|
-
return 0;
|
|
4332
|
-
}, "_DEFAULT_COMPARATOR"));
|
|
4333
|
-
__publicField(this, "_comparator", this._DEFAULT_COMPARATOR);
|
|
4334
4335
|
if (options) {
|
|
4335
4336
|
const { comparator } = options;
|
|
4336
4337
|
if (comparator) this._comparator = comparator;
|
|
4337
4338
|
}
|
|
4338
4339
|
this.addMany(elements);
|
|
4339
4340
|
}
|
|
4341
|
+
_elements = [];
|
|
4340
4342
|
/**
|
|
4341
4343
|
* Get the backing array of the heap.
|
|
4342
4344
|
* @remarks Time O(1), Space O(1)
|
|
@@ -4359,8 +4361,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
4359
4361
|
* @returns Last element or undefined.
|
|
4360
4362
|
*/
|
|
4361
4363
|
get leaf() {
|
|
4362
|
-
|
|
4363
|
-
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
4364
|
+
return this.elements[this.size - 1] ?? void 0;
|
|
4364
4365
|
}
|
|
4365
4366
|
/**
|
|
4366
4367
|
* Create a heap of the same class from an iterable.
|
|
@@ -4633,7 +4634,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
4633
4634
|
* @returns A new heap with mapped elements.
|
|
4634
4635
|
*/
|
|
4635
4636
|
map(callback, options, thisArg) {
|
|
4636
|
-
const { comparator, toElementFn, ...rest } = options
|
|
4637
|
+
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
4637
4638
|
if (!comparator) throw new TypeError("Heap.map requires options.comparator for EM");
|
|
4638
4639
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
4639
4640
|
let i = 0;
|
|
@@ -4659,6 +4660,15 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
4659
4660
|
}
|
|
4660
4661
|
return out;
|
|
4661
4662
|
}
|
|
4663
|
+
_DEFAULT_COMPARATOR = /* @__PURE__ */ __name((a, b) => {
|
|
4664
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
4665
|
+
throw TypeError("When comparing object types, define a custom comparator in options.");
|
|
4666
|
+
}
|
|
4667
|
+
if (a > b) return 1;
|
|
4668
|
+
if (a < b) return -1;
|
|
4669
|
+
return 0;
|
|
4670
|
+
}, "_DEFAULT_COMPARATOR");
|
|
4671
|
+
_comparator = this._DEFAULT_COMPARATOR;
|
|
4662
4672
|
/**
|
|
4663
4673
|
* Get the comparator used to order elements.
|
|
4664
4674
|
* @remarks Time O(1), Space O(1)
|
|
@@ -4712,7 +4722,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
4712
4722
|
*/
|
|
4713
4723
|
_createInstance(options) {
|
|
4714
4724
|
const Ctor = this.constructor;
|
|
4715
|
-
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options
|
|
4725
|
+
const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options ?? {} });
|
|
4716
4726
|
return next;
|
|
4717
4727
|
}
|
|
4718
4728
|
/**
|
|
@@ -4740,25 +4750,27 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
4740
4750
|
return this._createLike([], options);
|
|
4741
4751
|
}
|
|
4742
4752
|
};
|
|
4743
|
-
|
|
4744
|
-
|
|
4745
|
-
|
|
4753
|
+
var FibonacciHeapNode = class {
|
|
4754
|
+
static {
|
|
4755
|
+
__name(this, "FibonacciHeapNode");
|
|
4756
|
+
}
|
|
4757
|
+
element;
|
|
4758
|
+
degree;
|
|
4759
|
+
left;
|
|
4760
|
+
right;
|
|
4761
|
+
child;
|
|
4762
|
+
parent;
|
|
4763
|
+
marked;
|
|
4746
4764
|
constructor(element, degree = 0) {
|
|
4747
|
-
__publicField(this, "element");
|
|
4748
|
-
__publicField(this, "degree");
|
|
4749
|
-
__publicField(this, "left");
|
|
4750
|
-
__publicField(this, "right");
|
|
4751
|
-
__publicField(this, "child");
|
|
4752
|
-
__publicField(this, "parent");
|
|
4753
|
-
__publicField(this, "marked");
|
|
4754
4765
|
this.element = element;
|
|
4755
4766
|
this.degree = degree;
|
|
4756
4767
|
this.marked = false;
|
|
4757
4768
|
}
|
|
4758
4769
|
};
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4770
|
+
var FibonacciHeap = class {
|
|
4771
|
+
static {
|
|
4772
|
+
__name(this, "FibonacciHeap");
|
|
4773
|
+
}
|
|
4762
4774
|
/**
|
|
4763
4775
|
* Create a FibonacciHeap.
|
|
4764
4776
|
* @remarks Time O(1), Space O(1)
|
|
@@ -4766,14 +4778,11 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
4766
4778
|
* @returns New FibonacciHeap instance.
|
|
4767
4779
|
*/
|
|
4768
4780
|
constructor(comparator) {
|
|
4769
|
-
__publicField(this, "_root");
|
|
4770
|
-
__publicField(this, "_size", 0);
|
|
4771
|
-
__publicField(this, "_min");
|
|
4772
|
-
__publicField(this, "_comparator");
|
|
4773
4781
|
this.clear();
|
|
4774
4782
|
this._comparator = comparator || this._defaultComparator;
|
|
4775
4783
|
if (typeof this.comparator !== "function") throw new Error("FibonacciHeap: comparator must be a function.");
|
|
4776
4784
|
}
|
|
4785
|
+
_root;
|
|
4777
4786
|
/**
|
|
4778
4787
|
* Get the circular root list head.
|
|
4779
4788
|
* @remarks Time O(1), Space O(1)
|
|
@@ -4782,9 +4791,11 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
4782
4791
|
get root() {
|
|
4783
4792
|
return this._root;
|
|
4784
4793
|
}
|
|
4794
|
+
_size = 0;
|
|
4785
4795
|
get size() {
|
|
4786
4796
|
return this._size;
|
|
4787
4797
|
}
|
|
4798
|
+
_min;
|
|
4788
4799
|
/**
|
|
4789
4800
|
* Get the current minimum node.
|
|
4790
4801
|
* @remarks Time O(1), Space O(1)
|
|
@@ -4793,6 +4804,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
4793
4804
|
get min() {
|
|
4794
4805
|
return this._min;
|
|
4795
4806
|
}
|
|
4807
|
+
_comparator;
|
|
4796
4808
|
get comparator() {
|
|
4797
4809
|
return this._comparator;
|
|
4798
4810
|
}
|
|
@@ -4969,11 +4981,12 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
4969
4981
|
}
|
|
4970
4982
|
}
|
|
4971
4983
|
};
|
|
4972
|
-
__name(_FibonacciHeap, "FibonacciHeap");
|
|
4973
|
-
var FibonacciHeap = _FibonacciHeap;
|
|
4974
4984
|
|
|
4975
4985
|
// src/data-structures/heap/max-heap.ts
|
|
4976
|
-
var
|
|
4986
|
+
var MaxHeap = class extends Heap {
|
|
4987
|
+
static {
|
|
4988
|
+
__name(this, "MaxHeap");
|
|
4989
|
+
}
|
|
4977
4990
|
/**
|
|
4978
4991
|
* Create a max-heap. For objects, supply a custom comparator.
|
|
4979
4992
|
* @param elements Optional initial elements.
|
|
@@ -4995,11 +5008,12 @@ var _MaxHeap = class _MaxHeap extends Heap {
|
|
|
4995
5008
|
});
|
|
4996
5009
|
}
|
|
4997
5010
|
};
|
|
4998
|
-
__name(_MaxHeap, "MaxHeap");
|
|
4999
|
-
var MaxHeap = _MaxHeap;
|
|
5000
5011
|
|
|
5001
5012
|
// src/data-structures/heap/min-heap.ts
|
|
5002
|
-
var
|
|
5013
|
+
var MinHeap = class extends Heap {
|
|
5014
|
+
static {
|
|
5015
|
+
__name(this, "MinHeap");
|
|
5016
|
+
}
|
|
5003
5017
|
/**
|
|
5004
5018
|
* Create a min-heap.
|
|
5005
5019
|
* @param elements Optional initial elements.
|
|
@@ -5009,36 +5023,39 @@ var _MinHeap = class _MinHeap extends Heap {
|
|
|
5009
5023
|
super(elements, options);
|
|
5010
5024
|
}
|
|
5011
5025
|
};
|
|
5012
|
-
__name(_MinHeap, "MinHeap");
|
|
5013
|
-
var MinHeap = _MinHeap;
|
|
5014
5026
|
|
|
5015
5027
|
// src/data-structures/graph/abstract-graph.ts
|
|
5016
|
-
var
|
|
5028
|
+
var AbstractVertex = class {
|
|
5029
|
+
static {
|
|
5030
|
+
__name(this, "AbstractVertex");
|
|
5031
|
+
}
|
|
5032
|
+
key;
|
|
5033
|
+
value;
|
|
5017
5034
|
constructor(key, value) {
|
|
5018
|
-
__publicField(this, "key");
|
|
5019
|
-
__publicField(this, "value");
|
|
5020
5035
|
this.key = key;
|
|
5021
5036
|
this.value = value;
|
|
5022
5037
|
}
|
|
5023
5038
|
};
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
|
|
5039
|
+
var AbstractEdge = class {
|
|
5040
|
+
static {
|
|
5041
|
+
__name(this, "AbstractEdge");
|
|
5042
|
+
}
|
|
5043
|
+
value;
|
|
5044
|
+
weight;
|
|
5027
5045
|
constructor(weight, value) {
|
|
5028
|
-
__publicField(this, "value");
|
|
5029
|
-
__publicField(this, "weight");
|
|
5030
|
-
__publicField(this, "_hashCode");
|
|
5031
5046
|
this.weight = weight !== void 0 ? weight : 1;
|
|
5032
5047
|
this.value = value;
|
|
5033
5048
|
this._hashCode = uuidV4();
|
|
5034
5049
|
}
|
|
5050
|
+
_hashCode;
|
|
5035
5051
|
get hashCode() {
|
|
5036
5052
|
return this._hashCode;
|
|
5037
5053
|
}
|
|
5038
5054
|
};
|
|
5039
|
-
|
|
5040
|
-
|
|
5041
|
-
|
|
5055
|
+
var AbstractGraph = class extends IterableEntryBase {
|
|
5056
|
+
static {
|
|
5057
|
+
__name(this, "AbstractGraph");
|
|
5058
|
+
}
|
|
5042
5059
|
/**
|
|
5043
5060
|
* Construct a graph with runtime defaults.
|
|
5044
5061
|
* @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -5046,14 +5063,14 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5046
5063
|
*/
|
|
5047
5064
|
constructor(options) {
|
|
5048
5065
|
super();
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
const graph = options == null ? void 0 : options.graph;
|
|
5052
|
-
this._options = { defaultEdgeWeight: 1, ...graph != null ? graph : {} };
|
|
5066
|
+
const graph = options?.graph;
|
|
5067
|
+
this._options = { defaultEdgeWeight: 1, ...graph ?? {} };
|
|
5053
5068
|
}
|
|
5069
|
+
_options = { defaultEdgeWeight: 1 };
|
|
5054
5070
|
get options() {
|
|
5055
5071
|
return this._options;
|
|
5056
5072
|
}
|
|
5073
|
+
_vertexMap = /* @__PURE__ */ new Map();
|
|
5057
5074
|
get vertexMap() {
|
|
5058
5075
|
return this._vertexMap;
|
|
5059
5076
|
}
|
|
@@ -5211,10 +5228,9 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5211
5228
|
* @remarks Time O(L), Space O(1) where L is path length
|
|
5212
5229
|
*/
|
|
5213
5230
|
getPathSumWeight(path) {
|
|
5214
|
-
var _a;
|
|
5215
5231
|
let sum = 0;
|
|
5216
5232
|
for (let i = 0; i < path.length; i++) {
|
|
5217
|
-
sum +=
|
|
5233
|
+
sum += this.getEdge(path[i], path[i + 1])?.weight || 0;
|
|
5218
5234
|
}
|
|
5219
5235
|
return sum;
|
|
5220
5236
|
}
|
|
@@ -5276,7 +5292,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5276
5292
|
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
5277
5293
|
*/
|
|
5278
5294
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
5279
|
-
var _a, _b;
|
|
5280
5295
|
if (isWeight === void 0) isWeight = false;
|
|
5281
5296
|
if (isWeight) {
|
|
5282
5297
|
if (isDFS) {
|
|
@@ -5294,7 +5309,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5294
5309
|
}
|
|
5295
5310
|
return allPaths[minIndex] || void 0;
|
|
5296
5311
|
} else {
|
|
5297
|
-
return
|
|
5312
|
+
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
5298
5313
|
}
|
|
5299
5314
|
} else {
|
|
5300
5315
|
let minPath = [];
|
|
@@ -5423,7 +5438,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5423
5438
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
5424
5439
|
}
|
|
5425
5440
|
dijkstra(src, dest = void 0, getMinDist = false, genPaths = false) {
|
|
5426
|
-
var _a;
|
|
5427
5441
|
let minDist = Number.MAX_SAFE_INTEGER;
|
|
5428
5442
|
let minDest = void 0;
|
|
5429
5443
|
let minPath = [];
|
|
@@ -5461,8 +5475,8 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5461
5475
|
}, "getPaths");
|
|
5462
5476
|
while (heap.size > 0) {
|
|
5463
5477
|
const curHeapNode = heap.poll();
|
|
5464
|
-
const dist = curHeapNode
|
|
5465
|
-
const cur = curHeapNode
|
|
5478
|
+
const dist = curHeapNode?.key;
|
|
5479
|
+
const cur = curHeapNode?.value;
|
|
5466
5480
|
if (dist !== void 0) {
|
|
5467
5481
|
if (cur) {
|
|
5468
5482
|
seen.add(cur);
|
|
@@ -5478,7 +5492,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5478
5492
|
const neighbors = this.getNeighbors(cur);
|
|
5479
5493
|
for (const neighbor of neighbors) {
|
|
5480
5494
|
if (!seen.has(neighbor)) {
|
|
5481
|
-
const weight =
|
|
5495
|
+
const weight = this.getEdge(cur, neighbor)?.weight;
|
|
5482
5496
|
if (typeof weight === "number") {
|
|
5483
5497
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
5484
5498
|
if (distSrcToNeighbor !== void 0) {
|
|
@@ -5601,7 +5615,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5601
5615
|
* @remarks Time O(V^3), Space O(V^2)
|
|
5602
5616
|
*/
|
|
5603
5617
|
floydWarshall() {
|
|
5604
|
-
var _a;
|
|
5605
5618
|
const idAndVertices = [...this._vertexMap];
|
|
5606
5619
|
const n = idAndVertices.length;
|
|
5607
5620
|
const costs = [];
|
|
@@ -5615,7 +5628,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5615
5628
|
}
|
|
5616
5629
|
for (let i = 0; i < n; i++) {
|
|
5617
5630
|
for (let j = 0; j < n; j++) {
|
|
5618
|
-
costs[i][j] =
|
|
5631
|
+
costs[i][j] = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])?.weight || Number.MAX_SAFE_INTEGER;
|
|
5619
5632
|
}
|
|
5620
5633
|
}
|
|
5621
5634
|
for (let k = 0; k < n; k++) {
|
|
@@ -5679,7 +5692,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5679
5692
|
const filtered = [];
|
|
5680
5693
|
let index = 0;
|
|
5681
5694
|
for (const [key, value] of this) {
|
|
5682
|
-
if (predicate.call(thisArg,
|
|
5695
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
5683
5696
|
filtered.push([key, value]);
|
|
5684
5697
|
}
|
|
5685
5698
|
index++;
|
|
@@ -5694,7 +5707,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5694
5707
|
const filtered = [];
|
|
5695
5708
|
let index = 0;
|
|
5696
5709
|
for (const [key, value] of this) {
|
|
5697
|
-
if (predicate.call(thisArg,
|
|
5710
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
5698
5711
|
filtered.push([key, value]);
|
|
5699
5712
|
}
|
|
5700
5713
|
index++;
|
|
@@ -5705,7 +5718,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5705
5718
|
const mapped = [];
|
|
5706
5719
|
let index = 0;
|
|
5707
5720
|
for (const [key, value] of this) {
|
|
5708
|
-
mapped.push(callback.call(thisArg,
|
|
5721
|
+
mapped.push(callback.call(thisArg, value, key, index, this));
|
|
5709
5722
|
index++;
|
|
5710
5723
|
}
|
|
5711
5724
|
return mapped;
|
|
@@ -5758,7 +5771,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5758
5771
|
_createInstance(_options) {
|
|
5759
5772
|
const Ctor = this.constructor;
|
|
5760
5773
|
const instance = new Ctor();
|
|
5761
|
-
const graph = _options
|
|
5774
|
+
const graph = _options?.graph;
|
|
5762
5775
|
if (graph) instance._options = { ...instance._options, ...graph };
|
|
5763
5776
|
else instance._options = { ...instance._options, ...this._options };
|
|
5764
5777
|
return instance;
|
|
@@ -5837,29 +5850,32 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
5837
5850
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
5838
5851
|
}
|
|
5839
5852
|
};
|
|
5840
|
-
__name(_AbstractGraph, "AbstractGraph");
|
|
5841
|
-
var AbstractGraph = _AbstractGraph;
|
|
5842
5853
|
|
|
5843
5854
|
// src/data-structures/graph/directed-graph.ts
|
|
5844
|
-
var
|
|
5855
|
+
var DirectedVertex = class extends AbstractVertex {
|
|
5856
|
+
static {
|
|
5857
|
+
__name(this, "DirectedVertex");
|
|
5858
|
+
}
|
|
5845
5859
|
constructor(key, value) {
|
|
5846
5860
|
super(key, value);
|
|
5847
5861
|
}
|
|
5848
5862
|
};
|
|
5849
|
-
|
|
5850
|
-
|
|
5851
|
-
|
|
5863
|
+
var DirectedEdge = class extends AbstractEdge {
|
|
5864
|
+
static {
|
|
5865
|
+
__name(this, "DirectedEdge");
|
|
5866
|
+
}
|
|
5867
|
+
src;
|
|
5868
|
+
dest;
|
|
5852
5869
|
constructor(src, dest, weight, value) {
|
|
5853
5870
|
super(weight, value);
|
|
5854
|
-
__publicField(this, "src");
|
|
5855
|
-
__publicField(this, "dest");
|
|
5856
5871
|
this.src = src;
|
|
5857
5872
|
this.dest = dest;
|
|
5858
5873
|
}
|
|
5859
5874
|
};
|
|
5860
|
-
|
|
5861
|
-
|
|
5862
|
-
|
|
5875
|
+
var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
5876
|
+
static {
|
|
5877
|
+
__name(this, "DirectedGraph");
|
|
5878
|
+
}
|
|
5863
5879
|
/**
|
|
5864
5880
|
* Construct a directed graph with runtime defaults.
|
|
5865
5881
|
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -5867,15 +5883,15 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
5867
5883
|
*/
|
|
5868
5884
|
constructor(options) {
|
|
5869
5885
|
super(options);
|
|
5870
|
-
__publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
|
|
5871
|
-
__publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
|
|
5872
5886
|
}
|
|
5887
|
+
_outEdgeMap = /* @__PURE__ */ new Map();
|
|
5873
5888
|
get outEdgeMap() {
|
|
5874
5889
|
return this._outEdgeMap;
|
|
5875
5890
|
}
|
|
5876
5891
|
set outEdgeMap(v) {
|
|
5877
5892
|
this._outEdgeMap = v;
|
|
5878
5893
|
}
|
|
5894
|
+
_inEdgeMap = /* @__PURE__ */ new Map();
|
|
5879
5895
|
get inEdgeMap() {
|
|
5880
5896
|
return this._inEdgeMap;
|
|
5881
5897
|
}
|
|
@@ -5928,8 +5944,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
5928
5944
|
* @remarks Time O(1), Space O(1)
|
|
5929
5945
|
*/
|
|
5930
5946
|
createEdge(src, dest, weight, value) {
|
|
5931
|
-
|
|
5932
|
-
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
5947
|
+
return new DirectedEdge(src, dest, weight ?? this.options.defaultEdgeWeight ?? 1, value);
|
|
5933
5948
|
}
|
|
5934
5949
|
/**
|
|
5935
5950
|
* Get the unique edge from `src` to `dest`, if present.
|
|
@@ -6000,7 +6015,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
6000
6015
|
if (src && dest) {
|
|
6001
6016
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
6002
6017
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
6003
|
-
arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest ===
|
|
6018
|
+
arrayRemove(srcOutEdges, (edge) => edge.src === src.key && edge.dest === dest?.key);
|
|
6004
6019
|
}
|
|
6005
6020
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
6006
6021
|
if (destInEdges && destInEdges.length > 0) {
|
|
@@ -6122,7 +6137,7 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
6122
6137
|
* @remarks Time O(V + E), Space O(V)
|
|
6123
6138
|
*/
|
|
6124
6139
|
topologicalSort(propertyName) {
|
|
6125
|
-
propertyName = propertyName
|
|
6140
|
+
propertyName = propertyName ?? "key";
|
|
6126
6141
|
const statusMap = /* @__PURE__ */ new Map();
|
|
6127
6142
|
for (const entry of this.vertexMap) {
|
|
6128
6143
|
statusMap.set(entry[1], 0);
|
|
@@ -6315,27 +6330,30 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
6315
6330
|
}
|
|
6316
6331
|
}
|
|
6317
6332
|
};
|
|
6318
|
-
__name(_DirectedGraph, "DirectedGraph");
|
|
6319
|
-
var DirectedGraph = _DirectedGraph;
|
|
6320
6333
|
|
|
6321
6334
|
// src/data-structures/graph/undirected-graph.ts
|
|
6322
|
-
var
|
|
6335
|
+
var UndirectedVertex = class extends AbstractVertex {
|
|
6336
|
+
static {
|
|
6337
|
+
__name(this, "UndirectedVertex");
|
|
6338
|
+
}
|
|
6323
6339
|
constructor(key, value) {
|
|
6324
6340
|
super(key, value);
|
|
6325
6341
|
}
|
|
6326
6342
|
};
|
|
6327
|
-
|
|
6328
|
-
|
|
6329
|
-
|
|
6343
|
+
var UndirectedEdge = class extends AbstractEdge {
|
|
6344
|
+
static {
|
|
6345
|
+
__name(this, "UndirectedEdge");
|
|
6346
|
+
}
|
|
6347
|
+
endpoints;
|
|
6330
6348
|
constructor(v1, v2, weight, value) {
|
|
6331
6349
|
super(weight, value);
|
|
6332
|
-
__publicField(this, "endpoints");
|
|
6333
6350
|
this.endpoints = [v1, v2];
|
|
6334
6351
|
}
|
|
6335
6352
|
};
|
|
6336
|
-
|
|
6337
|
-
|
|
6338
|
-
|
|
6353
|
+
var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
6354
|
+
static {
|
|
6355
|
+
__name(this, "UndirectedGraph");
|
|
6356
|
+
}
|
|
6339
6357
|
/**
|
|
6340
6358
|
* Construct an undirected graph with runtime defaults.
|
|
6341
6359
|
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
@@ -6343,9 +6361,9 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
6343
6361
|
*/
|
|
6344
6362
|
constructor(options) {
|
|
6345
6363
|
super(options);
|
|
6346
|
-
__publicField(this, "_edgeMap");
|
|
6347
6364
|
this._edgeMap = /* @__PURE__ */ new Map();
|
|
6348
6365
|
}
|
|
6366
|
+
_edgeMap;
|
|
6349
6367
|
get edgeMap() {
|
|
6350
6368
|
return this._edgeMap;
|
|
6351
6369
|
}
|
|
@@ -6398,8 +6416,7 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
6398
6416
|
* @remarks Time O(1), Space O(1)
|
|
6399
6417
|
*/
|
|
6400
6418
|
createEdge(v1, v2, weight, value) {
|
|
6401
|
-
|
|
6402
|
-
return new UndirectedEdge(v1, v2, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
6419
|
+
return new UndirectedEdge(v1, v2, weight ?? this.options.defaultEdgeWeight ?? 1, value);
|
|
6403
6420
|
}
|
|
6404
6421
|
/**
|
|
6405
6422
|
* Get an undirected edge between two vertices, if present.
|
|
@@ -6409,13 +6426,12 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
6409
6426
|
* @remarks Time O(1) avg, Space O(1)
|
|
6410
6427
|
*/
|
|
6411
6428
|
getEdge(v1, v2) {
|
|
6412
|
-
var _a;
|
|
6413
6429
|
let edgeMap = [];
|
|
6414
6430
|
if (v1 !== void 0 && v2 !== void 0) {
|
|
6415
6431
|
const vertex1 = this._getVertex(v1);
|
|
6416
6432
|
const vertex2 = this._getVertex(v2);
|
|
6417
6433
|
if (vertex1 && vertex2) {
|
|
6418
|
-
edgeMap =
|
|
6434
|
+
edgeMap = this._edgeMap.get(vertex1)?.filter((e) => e.endpoints.includes(vertex2.key));
|
|
6419
6435
|
}
|
|
6420
6436
|
}
|
|
6421
6437
|
return edgeMap ? edgeMap[0] || void 0 : void 0;
|
|
@@ -6508,10 +6524,9 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
6508
6524
|
* @remarks Time O(1) avg, Space O(1)
|
|
6509
6525
|
*/
|
|
6510
6526
|
degreeOf(vertexOrKey) {
|
|
6511
|
-
var _a;
|
|
6512
6527
|
const vertex = this._getVertex(vertexOrKey);
|
|
6513
6528
|
if (vertex) {
|
|
6514
|
-
return
|
|
6529
|
+
return this._edgeMap.get(vertex)?.length || 0;
|
|
6515
6530
|
} else {
|
|
6516
6531
|
return 0;
|
|
6517
6532
|
}
|
|
@@ -6704,29 +6719,32 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
6704
6719
|
return true;
|
|
6705
6720
|
}
|
|
6706
6721
|
};
|
|
6707
|
-
__name(_UndirectedGraph, "UndirectedGraph");
|
|
6708
|
-
var UndirectedGraph = _UndirectedGraph;
|
|
6709
6722
|
|
|
6710
6723
|
// src/data-structures/graph/map-graph.ts
|
|
6711
|
-
var
|
|
6724
|
+
var MapVertex = class extends DirectedVertex {
|
|
6725
|
+
static {
|
|
6726
|
+
__name(this, "MapVertex");
|
|
6727
|
+
}
|
|
6728
|
+
lat;
|
|
6729
|
+
long;
|
|
6712
6730
|
constructor(key, value, lat, long) {
|
|
6713
6731
|
super(key, value);
|
|
6714
|
-
__publicField(this, "lat");
|
|
6715
|
-
__publicField(this, "long");
|
|
6716
6732
|
this.lat = lat;
|
|
6717
6733
|
this.long = long;
|
|
6718
6734
|
}
|
|
6719
6735
|
};
|
|
6720
|
-
|
|
6721
|
-
|
|
6722
|
-
|
|
6736
|
+
var MapEdge = class extends DirectedEdge {
|
|
6737
|
+
static {
|
|
6738
|
+
__name(this, "MapEdge");
|
|
6739
|
+
}
|
|
6723
6740
|
constructor(src, dest, weight, value) {
|
|
6724
6741
|
super(src, dest, weight, value);
|
|
6725
6742
|
}
|
|
6726
6743
|
};
|
|
6727
|
-
|
|
6728
|
-
|
|
6729
|
-
|
|
6744
|
+
var MapGraph = class _MapGraph extends DirectedGraph {
|
|
6745
|
+
static {
|
|
6746
|
+
__name(this, "MapGraph");
|
|
6747
|
+
}
|
|
6730
6748
|
/**
|
|
6731
6749
|
* Construct a MapGraph.
|
|
6732
6750
|
* @param originCoord - Origin coordinate `[lat, long]` used as default.
|
|
@@ -6735,14 +6753,14 @@ var _MapGraph = class _MapGraph extends DirectedGraph {
|
|
|
6735
6753
|
*/
|
|
6736
6754
|
constructor(originCoord, bottomRight) {
|
|
6737
6755
|
super();
|
|
6738
|
-
__publicField(this, "_originCoord", [0, 0]);
|
|
6739
|
-
__publicField(this, "_bottomRight");
|
|
6740
6756
|
this._originCoord = originCoord;
|
|
6741
6757
|
this._bottomRight = bottomRight;
|
|
6742
6758
|
}
|
|
6759
|
+
_originCoord = [0, 0];
|
|
6743
6760
|
get originCoord() {
|
|
6744
6761
|
return this._originCoord;
|
|
6745
6762
|
}
|
|
6763
|
+
_bottomRight;
|
|
6746
6764
|
get bottomRight() {
|
|
6747
6765
|
return this._bottomRight;
|
|
6748
6766
|
}
|
|
@@ -6794,13 +6812,11 @@ var _MapGraph = class _MapGraph extends DirectedGraph {
|
|
|
6794
6812
|
*/
|
|
6795
6813
|
_createInstance(options) {
|
|
6796
6814
|
const { originCoord, bottomRight } = options || {};
|
|
6797
|
-
const oc = originCoord
|
|
6798
|
-
const br = bottomRight
|
|
6815
|
+
const oc = originCoord ?? this.originCoord;
|
|
6816
|
+
const br = bottomRight ?? this.bottomRight;
|
|
6799
6817
|
return new _MapGraph(oc, br);
|
|
6800
6818
|
}
|
|
6801
6819
|
};
|
|
6802
|
-
__name(_MapGraph, "MapGraph");
|
|
6803
|
-
var MapGraph = _MapGraph;
|
|
6804
6820
|
|
|
6805
6821
|
// src/common/index.ts
|
|
6806
6822
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
@@ -6808,7 +6824,7 @@ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
|
6808
6824
|
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
6809
6825
|
return DFSOperation2;
|
|
6810
6826
|
})(DFSOperation || {});
|
|
6811
|
-
var
|
|
6827
|
+
var Range = class {
|
|
6812
6828
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
6813
6829
|
this.low = low;
|
|
6814
6830
|
this.high = high;
|
|
@@ -6817,6 +6833,9 @@ var _Range = class _Range {
|
|
|
6817
6833
|
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
6818
6834
|
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
6819
6835
|
}
|
|
6836
|
+
static {
|
|
6837
|
+
__name(this, "Range");
|
|
6838
|
+
}
|
|
6820
6839
|
// Determine whether a key is within the range
|
|
6821
6840
|
isInRange(key, comparator) {
|
|
6822
6841
|
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
@@ -6824,11 +6843,15 @@ var _Range = class _Range {
|
|
|
6824
6843
|
return lowCheck && highCheck;
|
|
6825
6844
|
}
|
|
6826
6845
|
};
|
|
6827
|
-
__name(_Range, "Range");
|
|
6828
|
-
var Range = _Range;
|
|
6829
6846
|
|
|
6830
6847
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
6831
|
-
var
|
|
6848
|
+
var BinaryTreeNode = class {
|
|
6849
|
+
static {
|
|
6850
|
+
__name(this, "BinaryTreeNode");
|
|
6851
|
+
}
|
|
6852
|
+
key;
|
|
6853
|
+
value;
|
|
6854
|
+
parent = void 0;
|
|
6832
6855
|
/**
|
|
6833
6856
|
* Creates an instance of BinaryTreeNode.
|
|
6834
6857
|
* @remarks Time O(1), Space O(1)
|
|
@@ -6837,17 +6860,10 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
6837
6860
|
* @param [value] - The value associated with the key.
|
|
6838
6861
|
*/
|
|
6839
6862
|
constructor(key, value) {
|
|
6840
|
-
__publicField(this, "key");
|
|
6841
|
-
__publicField(this, "value");
|
|
6842
|
-
__publicField(this, "parent");
|
|
6843
|
-
__publicField(this, "_left");
|
|
6844
|
-
__publicField(this, "_right");
|
|
6845
|
-
__publicField(this, "_height", 0);
|
|
6846
|
-
__publicField(this, "_color", "BLACK");
|
|
6847
|
-
__publicField(this, "_count", 1);
|
|
6848
6863
|
this.key = key;
|
|
6849
6864
|
this.value = value;
|
|
6850
6865
|
}
|
|
6866
|
+
_left = void 0;
|
|
6851
6867
|
/**
|
|
6852
6868
|
* Gets the left child of the node.
|
|
6853
6869
|
* @remarks Time O(1), Space O(1)
|
|
@@ -6869,6 +6885,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
6869
6885
|
}
|
|
6870
6886
|
this._left = v;
|
|
6871
6887
|
}
|
|
6888
|
+
_right = void 0;
|
|
6872
6889
|
/**
|
|
6873
6890
|
* Gets the right child of the node.
|
|
6874
6891
|
* @remarks Time O(1), Space O(1)
|
|
@@ -6890,6 +6907,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
6890
6907
|
}
|
|
6891
6908
|
this._right = v;
|
|
6892
6909
|
}
|
|
6910
|
+
_height = 0;
|
|
6893
6911
|
/**
|
|
6894
6912
|
* Gets the height of the node (used in self-balancing trees).
|
|
6895
6913
|
* @remarks Time O(1), Space O(1)
|
|
@@ -6908,6 +6926,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
6908
6926
|
set height(value) {
|
|
6909
6927
|
this._height = value;
|
|
6910
6928
|
}
|
|
6929
|
+
_color = "BLACK";
|
|
6911
6930
|
/**
|
|
6912
6931
|
* Gets the color of the node (used in Red-Black trees).
|
|
6913
6932
|
* @remarks Time O(1), Space O(1)
|
|
@@ -6926,6 +6945,7 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
6926
6945
|
set color(value) {
|
|
6927
6946
|
this._color = value;
|
|
6928
6947
|
}
|
|
6948
|
+
_count = 1;
|
|
6929
6949
|
/**
|
|
6930
6950
|
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
6931
6951
|
* @remarks Time O(1), Space O(1)
|
|
@@ -6962,9 +6982,11 @@ var _BinaryTreeNode = class _BinaryTreeNode {
|
|
|
6962
6982
|
return "MAL_NODE";
|
|
6963
6983
|
}
|
|
6964
6984
|
};
|
|
6965
|
-
|
|
6966
|
-
|
|
6967
|
-
|
|
6985
|
+
var BinaryTree = class extends IterableEntryBase {
|
|
6986
|
+
static {
|
|
6987
|
+
__name(this, "BinaryTree");
|
|
6988
|
+
}
|
|
6989
|
+
iterationType = "ITERATIVE";
|
|
6968
6990
|
/**
|
|
6969
6991
|
* Creates an instance of BinaryTree.
|
|
6970
6992
|
* @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
|
|
@@ -6974,22 +6996,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
6974
6996
|
*/
|
|
6975
6997
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
6976
6998
|
super();
|
|
6977
|
-
__publicField(this, "iterationType", "ITERATIVE");
|
|
6978
|
-
__publicField(this, "_isMapMode", true);
|
|
6979
|
-
__publicField(this, "_isDuplicate", false);
|
|
6980
|
-
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
6981
|
-
__publicField(this, "_root");
|
|
6982
|
-
__publicField(this, "_size", 0);
|
|
6983
|
-
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
6984
|
-
__publicField(this, "_toEntryFn");
|
|
6985
|
-
/**
|
|
6986
|
-
* (Protected) Default callback function, returns the node's key.
|
|
6987
|
-
* @remarks Time O(1)
|
|
6988
|
-
*
|
|
6989
|
-
* @param node - The node.
|
|
6990
|
-
* @returns The node's key or undefined.
|
|
6991
|
-
*/
|
|
6992
|
-
__publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK"));
|
|
6993
6999
|
if (options) {
|
|
6994
7000
|
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
6995
7001
|
if (iterationType) this.iterationType = iterationType;
|
|
@@ -7000,6 +7006,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7000
7006
|
}
|
|
7001
7007
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
7002
7008
|
}
|
|
7009
|
+
_isMapMode = true;
|
|
7003
7010
|
/**
|
|
7004
7011
|
* Gets whether the tree is in Map mode.
|
|
7005
7012
|
* @remarks In Map mode (default), values are stored in an external Map, and nodes only hold keys. If false, values are stored directly on the nodes. Time O(1)
|
|
@@ -7009,6 +7016,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7009
7016
|
get isMapMode() {
|
|
7010
7017
|
return this._isMapMode;
|
|
7011
7018
|
}
|
|
7019
|
+
_isDuplicate = false;
|
|
7012
7020
|
/**
|
|
7013
7021
|
* Gets whether the tree allows duplicate keys.
|
|
7014
7022
|
* @remarks Time O(1)
|
|
@@ -7018,6 +7026,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7018
7026
|
get isDuplicate() {
|
|
7019
7027
|
return this._isDuplicate;
|
|
7020
7028
|
}
|
|
7029
|
+
_store = /* @__PURE__ */ new Map();
|
|
7021
7030
|
/**
|
|
7022
7031
|
* Gets the external value store (used in Map mode).
|
|
7023
7032
|
* @remarks Time O(1)
|
|
@@ -7027,6 +7036,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7027
7036
|
get store() {
|
|
7028
7037
|
return this._store;
|
|
7029
7038
|
}
|
|
7039
|
+
_root;
|
|
7030
7040
|
/**
|
|
7031
7041
|
* Gets the root node of the tree.
|
|
7032
7042
|
* @remarks Time O(1)
|
|
@@ -7036,6 +7046,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7036
7046
|
get root() {
|
|
7037
7047
|
return this._root;
|
|
7038
7048
|
}
|
|
7049
|
+
_size = 0;
|
|
7039
7050
|
/**
|
|
7040
7051
|
* Gets the number of nodes in the tree.
|
|
7041
7052
|
* @remarks Time O(1)
|
|
@@ -7045,6 +7056,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7045
7056
|
get size() {
|
|
7046
7057
|
return this._size;
|
|
7047
7058
|
}
|
|
7059
|
+
_NIL = new BinaryTreeNode(NaN);
|
|
7048
7060
|
/**
|
|
7049
7061
|
* Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
|
|
7050
7062
|
* @remarks Time O(1)
|
|
@@ -7054,6 +7066,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7054
7066
|
get NIL() {
|
|
7055
7067
|
return this._NIL;
|
|
7056
7068
|
}
|
|
7069
|
+
_toEntryFn;
|
|
7057
7070
|
/**
|
|
7058
7071
|
* Gets the function used to convert raw data objects (R) into [key, value] entries.
|
|
7059
7072
|
* @remarks Time O(1)
|
|
@@ -7213,7 +7226,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7213
7226
|
if (newNode === void 0) return false;
|
|
7214
7227
|
if (!this._root) {
|
|
7215
7228
|
this._setRoot(newNode);
|
|
7216
|
-
if (this._isMapMode) this._setValue(newNode
|
|
7229
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
7217
7230
|
this._size = 1;
|
|
7218
7231
|
return true;
|
|
7219
7232
|
}
|
|
@@ -7245,7 +7258,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7245
7258
|
} else if (potentialParent.right === void 0) {
|
|
7246
7259
|
potentialParent.right = newNode;
|
|
7247
7260
|
}
|
|
7248
|
-
if (this._isMapMode) this._setValue(newNode
|
|
7261
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
7249
7262
|
this._size++;
|
|
7250
7263
|
return true;
|
|
7251
7264
|
}
|
|
@@ -7310,7 +7323,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7310
7323
|
if (!this._root) return deletedResult;
|
|
7311
7324
|
const curr = this.getNode(keyNodeOrEntry);
|
|
7312
7325
|
if (!curr) return deletedResult;
|
|
7313
|
-
const parent = curr
|
|
7326
|
+
const parent = curr?.parent;
|
|
7314
7327
|
let needBalanced;
|
|
7315
7328
|
let orgCurrent = curr;
|
|
7316
7329
|
if (!curr.left && !curr.right && !parent) {
|
|
@@ -7415,13 +7428,12 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7415
7428
|
* @returns The associated value, or undefined.
|
|
7416
7429
|
*/
|
|
7417
7430
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7418
|
-
var _a;
|
|
7419
7431
|
if (this._isMapMode) {
|
|
7420
7432
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
7421
7433
|
if (key === null || key === void 0) return;
|
|
7422
7434
|
return this._store.get(key);
|
|
7423
7435
|
}
|
|
7424
|
-
return
|
|
7436
|
+
return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
|
|
7425
7437
|
}
|
|
7426
7438
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7427
7439
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
@@ -7509,7 +7521,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7509
7521
|
let distEnsured = this.ensureNode(dist);
|
|
7510
7522
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
7511
7523
|
let depth = 0;
|
|
7512
|
-
while (distEnsured
|
|
7524
|
+
while (distEnsured?.parent) {
|
|
7513
7525
|
if (distEnsured === beginRootEnsured) {
|
|
7514
7526
|
return depth;
|
|
7515
7527
|
}
|
|
@@ -7982,7 +7994,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7982
7994
|
filter(predicate, thisArg) {
|
|
7983
7995
|
const out = this._createInstance();
|
|
7984
7996
|
let i = 0;
|
|
7985
|
-
for (const [k, v] of this) if (predicate.call(thisArg,
|
|
7997
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
|
|
7986
7998
|
return out;
|
|
7987
7999
|
}
|
|
7988
8000
|
/**
|
|
@@ -8000,7 +8012,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8000
8012
|
map(cb, options, thisArg) {
|
|
8001
8013
|
const out = this._createLike([], options);
|
|
8002
8014
|
let i = 0;
|
|
8003
|
-
for (const [k, v] of this) out.add(cb.call(thisArg,
|
|
8015
|
+
for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
|
|
8004
8016
|
return out;
|
|
8005
8017
|
}
|
|
8006
8018
|
/**
|
|
@@ -8071,10 +8083,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8071
8083
|
const dfs = /* @__PURE__ */ __name((node) => {
|
|
8072
8084
|
if (!shouldVisitRoot(node)) return;
|
|
8073
8085
|
const visitLeft = /* @__PURE__ */ __name(() => {
|
|
8074
|
-
if (shouldVisitLeft(node) &&
|
|
8086
|
+
if (shouldVisitLeft(node) && node?.left !== void 0) dfs(node?.left);
|
|
8075
8087
|
}, "visitLeft");
|
|
8076
8088
|
const visitRight = /* @__PURE__ */ __name(() => {
|
|
8077
|
-
if (shouldVisitRight(node) &&
|
|
8089
|
+
if (shouldVisitRight(node) && node?.right !== void 0) dfs(node?.right);
|
|
8078
8090
|
}, "visitRight");
|
|
8079
8091
|
switch (pattern) {
|
|
8080
8092
|
case "IN":
|
|
@@ -8107,12 +8119,10 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8107
8119
|
} else {
|
|
8108
8120
|
const stack = [{ opt: 0 /* VISIT */, node: startNode }];
|
|
8109
8121
|
const pushLeft = /* @__PURE__ */ __name((cur) => {
|
|
8110
|
-
|
|
8111
|
-
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.left });
|
|
8122
|
+
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.left });
|
|
8112
8123
|
}, "pushLeft");
|
|
8113
8124
|
const pushRight = /* @__PURE__ */ __name((cur) => {
|
|
8114
|
-
|
|
8115
|
-
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.right });
|
|
8125
|
+
if (shouldVisitRight(cur.node)) stack.push({ opt: 0 /* VISIT */, node: cur.node?.right });
|
|
8116
8126
|
}, "pushRight");
|
|
8117
8127
|
const pushRoot = /* @__PURE__ */ __name((cur) => {
|
|
8118
8128
|
if (shouldVisitRoot(cur.node)) stack.push({ opt: 1 /* PROCESS */, node: cur.node });
|
|
@@ -8184,6 +8194,14 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8184
8194
|
}
|
|
8185
8195
|
}
|
|
8186
8196
|
}
|
|
8197
|
+
/**
|
|
8198
|
+
* (Protected) Default callback function, returns the node's key.
|
|
8199
|
+
* @remarks Time O(1)
|
|
8200
|
+
*
|
|
8201
|
+
* @param node - The node.
|
|
8202
|
+
* @returns The node's key or undefined.
|
|
8203
|
+
*/
|
|
8204
|
+
_DEFAULT_NODE_CALLBACK = /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK");
|
|
8187
8205
|
/**
|
|
8188
8206
|
* (Protected) Snapshots the current tree's configuration options.
|
|
8189
8207
|
* @remarks Time O(1)
|
|
@@ -8209,7 +8227,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8209
8227
|
*/
|
|
8210
8228
|
_createInstance(options) {
|
|
8211
8229
|
const Ctor = this.constructor;
|
|
8212
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
8230
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
8213
8231
|
}
|
|
8214
8232
|
/**
|
|
8215
8233
|
* (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
|
|
@@ -8222,7 +8240,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8222
8240
|
*/
|
|
8223
8241
|
_createLike(iter = [], options) {
|
|
8224
8242
|
const Ctor = this.constructor;
|
|
8225
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
8243
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
8226
8244
|
}
|
|
8227
8245
|
/**
|
|
8228
8246
|
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
@@ -8240,7 +8258,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8240
8258
|
const [key, entryValue] = keyNodeOrEntry;
|
|
8241
8259
|
if (key === void 0) return [void 0, void 0];
|
|
8242
8260
|
else if (key === null) return [null, void 0];
|
|
8243
|
-
const finalValue = value
|
|
8261
|
+
const finalValue = value ?? entryValue;
|
|
8244
8262
|
return [this.createNode(key, finalValue), finalValue];
|
|
8245
8263
|
}
|
|
8246
8264
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
@@ -8447,11 +8465,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8447
8465
|
this._store.clear();
|
|
8448
8466
|
}
|
|
8449
8467
|
};
|
|
8450
|
-
__name(_BinaryTree, "BinaryTree");
|
|
8451
|
-
var BinaryTree = _BinaryTree;
|
|
8452
8468
|
|
|
8453
8469
|
// src/data-structures/binary-tree/bst.ts
|
|
8454
|
-
var
|
|
8470
|
+
var BSTNode = class {
|
|
8471
|
+
static {
|
|
8472
|
+
__name(this, "BSTNode");
|
|
8473
|
+
}
|
|
8474
|
+
key;
|
|
8475
|
+
value;
|
|
8476
|
+
parent = void 0;
|
|
8455
8477
|
/**
|
|
8456
8478
|
* Creates an instance of BSTNode.
|
|
8457
8479
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8460,11 +8482,10 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
8460
8482
|
* @param [value] - The value associated with the key.
|
|
8461
8483
|
*/
|
|
8462
8484
|
constructor(key, value) {
|
|
8463
|
-
|
|
8464
|
-
|
|
8465
|
-
__publicField(this, "_left");
|
|
8466
|
-
__publicField(this, "_right");
|
|
8485
|
+
this.key = key;
|
|
8486
|
+
this.value = value;
|
|
8467
8487
|
}
|
|
8488
|
+
_left = void 0;
|
|
8468
8489
|
/**
|
|
8469
8490
|
* Gets the left child of the node.
|
|
8470
8491
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8484,6 +8505,7 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
8484
8505
|
if (v) v.parent = this;
|
|
8485
8506
|
this._left = v;
|
|
8486
8507
|
}
|
|
8508
|
+
_right = void 0;
|
|
8487
8509
|
/**
|
|
8488
8510
|
* Gets the right child of the node.
|
|
8489
8511
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8503,10 +8525,85 @@ var _BSTNode = class _BSTNode extends BinaryTreeNode {
|
|
|
8503
8525
|
if (v) v.parent = this;
|
|
8504
8526
|
this._right = v;
|
|
8505
8527
|
}
|
|
8528
|
+
_height = 0;
|
|
8529
|
+
/**
|
|
8530
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
8531
|
+
* @remarks Time O(1), Space O(1)
|
|
8532
|
+
*
|
|
8533
|
+
* @returns The height.
|
|
8534
|
+
*/
|
|
8535
|
+
get height() {
|
|
8536
|
+
return this._height;
|
|
8537
|
+
}
|
|
8538
|
+
/**
|
|
8539
|
+
* Sets the height of the node.
|
|
8540
|
+
* @remarks Time O(1), Space O(1)
|
|
8541
|
+
*
|
|
8542
|
+
* @param value - The new height.
|
|
8543
|
+
*/
|
|
8544
|
+
set height(value) {
|
|
8545
|
+
this._height = value;
|
|
8546
|
+
}
|
|
8547
|
+
_color = "BLACK";
|
|
8548
|
+
/**
|
|
8549
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
8550
|
+
* @remarks Time O(1), Space O(1)
|
|
8551
|
+
*
|
|
8552
|
+
* @returns The node's color.
|
|
8553
|
+
*/
|
|
8554
|
+
get color() {
|
|
8555
|
+
return this._color;
|
|
8556
|
+
}
|
|
8557
|
+
/**
|
|
8558
|
+
* Sets the color of the node.
|
|
8559
|
+
* @remarks Time O(1), Space O(1)
|
|
8560
|
+
*
|
|
8561
|
+
* @param value - The new color.
|
|
8562
|
+
*/
|
|
8563
|
+
set color(value) {
|
|
8564
|
+
this._color = value;
|
|
8565
|
+
}
|
|
8566
|
+
_count = 1;
|
|
8567
|
+
/**
|
|
8568
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
8569
|
+
* @remarks Time O(1), Space O(1)
|
|
8570
|
+
*
|
|
8571
|
+
* @returns The subtree node count.
|
|
8572
|
+
*/
|
|
8573
|
+
get count() {
|
|
8574
|
+
return this._count;
|
|
8575
|
+
}
|
|
8576
|
+
/**
|
|
8577
|
+
* Sets the count of nodes in the subtree.
|
|
8578
|
+
* @remarks Time O(1), Space O(1)
|
|
8579
|
+
*
|
|
8580
|
+
* @param value - The new count.
|
|
8581
|
+
*/
|
|
8582
|
+
set count(value) {
|
|
8583
|
+
this._count = value;
|
|
8584
|
+
}
|
|
8585
|
+
/**
|
|
8586
|
+
* Gets the position of the node relative to its parent.
|
|
8587
|
+
* @remarks Time O(1), Space O(1)
|
|
8588
|
+
*
|
|
8589
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
8590
|
+
*/
|
|
8591
|
+
get familyPosition() {
|
|
8592
|
+
if (!this.parent) {
|
|
8593
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
8594
|
+
}
|
|
8595
|
+
if (this.parent.left === this) {
|
|
8596
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
8597
|
+
} else if (this.parent.right === this) {
|
|
8598
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
8599
|
+
}
|
|
8600
|
+
return "MAL_NODE";
|
|
8601
|
+
}
|
|
8506
8602
|
};
|
|
8507
|
-
|
|
8508
|
-
|
|
8509
|
-
|
|
8603
|
+
var BST = class extends BinaryTree {
|
|
8604
|
+
static {
|
|
8605
|
+
__name(this, "BST");
|
|
8606
|
+
}
|
|
8510
8607
|
/**
|
|
8511
8608
|
* Creates an instance of BST.
|
|
8512
8609
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -8516,33 +8613,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8516
8613
|
*/
|
|
8517
8614
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8518
8615
|
super([], options);
|
|
8519
|
-
__publicField(this, "_root");
|
|
8520
|
-
__publicField(this, "_isReverse", false);
|
|
8521
|
-
/**
|
|
8522
|
-
* The default comparator function.
|
|
8523
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
8524
|
-
*/
|
|
8525
|
-
__publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
|
|
8526
|
-
if (isComparable(a) && isComparable(b)) {
|
|
8527
|
-
if (a > b) return 1;
|
|
8528
|
-
if (a < b) return -1;
|
|
8529
|
-
return 0;
|
|
8530
|
-
}
|
|
8531
|
-
if (this._specifyComparable) {
|
|
8532
|
-
const va = this._specifyComparable(a);
|
|
8533
|
-
const vb = this._specifyComparable(b);
|
|
8534
|
-
if (va > vb) return 1;
|
|
8535
|
-
if (va < vb) return -1;
|
|
8536
|
-
return 0;
|
|
8537
|
-
}
|
|
8538
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8539
|
-
throw TypeError(
|
|
8540
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
8541
|
-
);
|
|
8542
|
-
}
|
|
8543
|
-
return 0;
|
|
8544
|
-
}, "_comparator"));
|
|
8545
|
-
__publicField(this, "_specifyComparable");
|
|
8546
8616
|
if (options) {
|
|
8547
8617
|
const { specifyComparable, isReverse } = options;
|
|
8548
8618
|
if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
|
|
@@ -8550,6 +8620,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8550
8620
|
}
|
|
8551
8621
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
8552
8622
|
}
|
|
8623
|
+
_root = void 0;
|
|
8553
8624
|
/**
|
|
8554
8625
|
* Gets the root node of the tree.
|
|
8555
8626
|
* @remarks Time O(1)
|
|
@@ -8559,6 +8630,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8559
8630
|
get root() {
|
|
8560
8631
|
return this._root;
|
|
8561
8632
|
}
|
|
8633
|
+
_isReverse = false;
|
|
8562
8634
|
/**
|
|
8563
8635
|
* Gets whether the tree's comparison logic is reversed.
|
|
8564
8636
|
* @remarks Time O(1)
|
|
@@ -8568,6 +8640,30 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8568
8640
|
get isReverse() {
|
|
8569
8641
|
return this._isReverse;
|
|
8570
8642
|
}
|
|
8643
|
+
/**
|
|
8644
|
+
* The default comparator function.
|
|
8645
|
+
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
8646
|
+
*/
|
|
8647
|
+
_comparator = /* @__PURE__ */ __name((a, b) => {
|
|
8648
|
+
if (isComparable(a) && isComparable(b)) {
|
|
8649
|
+
if (a > b) return 1;
|
|
8650
|
+
if (a < b) return -1;
|
|
8651
|
+
return 0;
|
|
8652
|
+
}
|
|
8653
|
+
if (this._specifyComparable) {
|
|
8654
|
+
const va = this._specifyComparable(a);
|
|
8655
|
+
const vb = this._specifyComparable(b);
|
|
8656
|
+
if (va > vb) return 1;
|
|
8657
|
+
if (va < vb) return -1;
|
|
8658
|
+
return 0;
|
|
8659
|
+
}
|
|
8660
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
8661
|
+
throw TypeError(
|
|
8662
|
+
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
8663
|
+
);
|
|
8664
|
+
}
|
|
8665
|
+
return 0;
|
|
8666
|
+
}, "_comparator");
|
|
8571
8667
|
/**
|
|
8572
8668
|
* Gets the comparator function used by the tree.
|
|
8573
8669
|
* @remarks Time O(1)
|
|
@@ -8577,6 +8673,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8577
8673
|
get comparator() {
|
|
8578
8674
|
return this._comparator;
|
|
8579
8675
|
}
|
|
8676
|
+
_specifyComparable;
|
|
8580
8677
|
/**
|
|
8581
8678
|
* Gets the function used to extract a comparable value from a complex key.
|
|
8582
8679
|
* @remarks Time O(1)
|
|
@@ -8606,8 +8703,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8606
8703
|
* @returns The resolved node, or undefined if not found.
|
|
8607
8704
|
*/
|
|
8608
8705
|
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
8609
|
-
|
|
8610
|
-
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) != null ? _a : void 0;
|
|
8706
|
+
return super.ensureNode(keyNodeOrEntry, iterationType) ?? void 0;
|
|
8611
8707
|
}
|
|
8612
8708
|
/**
|
|
8613
8709
|
* Checks if the given item is a `BSTNode` instance.
|
|
@@ -8680,8 +8776,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8680
8776
|
* @returns The first matching node, or undefined if not found.
|
|
8681
8777
|
*/
|
|
8682
8778
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
8683
|
-
|
|
8684
|
-
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
8779
|
+
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? void 0;
|
|
8685
8780
|
}
|
|
8686
8781
|
/**
|
|
8687
8782
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -8786,7 +8881,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8786
8881
|
if (newNode === void 0) return false;
|
|
8787
8882
|
if (this._root === void 0) {
|
|
8788
8883
|
this._setRoot(newNode);
|
|
8789
|
-
if (this._isMapMode) this._setValue(newNode
|
|
8884
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
8790
8885
|
this._size++;
|
|
8791
8886
|
return true;
|
|
8792
8887
|
}
|
|
@@ -8799,7 +8894,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8799
8894
|
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
8800
8895
|
if (current.left === void 0) {
|
|
8801
8896
|
current.left = newNode;
|
|
8802
|
-
if (this._isMapMode) this._setValue(newNode
|
|
8897
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
8803
8898
|
this._size++;
|
|
8804
8899
|
return true;
|
|
8805
8900
|
}
|
|
@@ -8807,7 +8902,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8807
8902
|
} else {
|
|
8808
8903
|
if (current.right === void 0) {
|
|
8809
8904
|
current.right = newNode;
|
|
8810
|
-
if (this._isMapMode) this._setValue(newNode
|
|
8905
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
8811
8906
|
this._size++;
|
|
8812
8907
|
return true;
|
|
8813
8908
|
}
|
|
@@ -8830,10 +8925,10 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8830
8925
|
*/
|
|
8831
8926
|
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8832
8927
|
const inserted = [];
|
|
8833
|
-
const valuesIterator = values
|
|
8928
|
+
const valuesIterator = values?.[Symbol.iterator]();
|
|
8834
8929
|
if (!isBalanceAdd) {
|
|
8835
8930
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
8836
|
-
const val = valuesIterator
|
|
8931
|
+
const val = valuesIterator?.next().value;
|
|
8837
8932
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
8838
8933
|
inserted.push(this.add(kve, val));
|
|
8839
8934
|
}
|
|
@@ -8842,7 +8937,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8842
8937
|
const realBTNExemplars = [];
|
|
8843
8938
|
let i = 0;
|
|
8844
8939
|
for (const kve of keysNodesEntriesOrRaws) {
|
|
8845
|
-
realBTNExemplars.push({ key: kve, value: valuesIterator
|
|
8940
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i++ });
|
|
8846
8941
|
}
|
|
8847
8942
|
const sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
8848
8943
|
let keyA, keyB;
|
|
@@ -9023,7 +9118,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9023
9118
|
const out = this._createLike([], options);
|
|
9024
9119
|
let index = 0;
|
|
9025
9120
|
for (const [key, value] of this) {
|
|
9026
|
-
out.add(callback.call(thisArg,
|
|
9121
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
9027
9122
|
}
|
|
9028
9123
|
return out;
|
|
9029
9124
|
}
|
|
@@ -9064,7 +9159,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9064
9159
|
*/
|
|
9065
9160
|
_createInstance(options) {
|
|
9066
9161
|
const Ctor = this.constructor;
|
|
9067
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
9162
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
9068
9163
|
}
|
|
9069
9164
|
/**
|
|
9070
9165
|
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
@@ -9077,7 +9172,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9077
9172
|
*/
|
|
9078
9173
|
_createLike(iter = [], options) {
|
|
9079
9174
|
const Ctor = this.constructor;
|
|
9080
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
9175
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
9081
9176
|
}
|
|
9082
9177
|
/**
|
|
9083
9178
|
* (Protected) Snapshots the current BST's configuration options.
|
|
@@ -9104,7 +9199,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9104
9199
|
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
9105
9200
|
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
9106
9201
|
if (node === null) return [void 0, void 0];
|
|
9107
|
-
return [node, value
|
|
9202
|
+
return [node, value ?? entryValue];
|
|
9108
9203
|
}
|
|
9109
9204
|
/**
|
|
9110
9205
|
* (Protected) Sets the root node and clears its parent reference.
|
|
@@ -9135,7 +9230,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9135
9230
|
* @returns True if the node was found and deleted, false otherwise.
|
|
9136
9231
|
*/
|
|
9137
9232
|
_deleteByKey(key) {
|
|
9138
|
-
var _a;
|
|
9139
9233
|
let node = this._root;
|
|
9140
9234
|
while (node) {
|
|
9141
9235
|
const cmp = this._compare(node.key, key);
|
|
@@ -9144,7 +9238,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9144
9238
|
}
|
|
9145
9239
|
if (!node) return false;
|
|
9146
9240
|
const transplant = /* @__PURE__ */ __name((u, v) => {
|
|
9147
|
-
const p = u
|
|
9241
|
+
const p = u?.parent;
|
|
9148
9242
|
if (!p) {
|
|
9149
9243
|
this._setRoot(v);
|
|
9150
9244
|
} else if (p.left === u) {
|
|
@@ -9174,15 +9268,18 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9174
9268
|
succ.left = node.left;
|
|
9175
9269
|
if (succ.left) succ.left.parent = succ;
|
|
9176
9270
|
}
|
|
9177
|
-
this._size = Math.max(0, (
|
|
9271
|
+
this._size = Math.max(0, (this._size ?? 0) - 1);
|
|
9178
9272
|
return true;
|
|
9179
9273
|
}
|
|
9180
9274
|
};
|
|
9181
|
-
__name(_BST, "BST");
|
|
9182
|
-
var BST = _BST;
|
|
9183
9275
|
|
|
9184
9276
|
// src/data-structures/binary-tree/binary-indexed-tree.ts
|
|
9185
|
-
var
|
|
9277
|
+
var BinaryIndexedTree = class {
|
|
9278
|
+
static {
|
|
9279
|
+
__name(this, "BinaryIndexedTree");
|
|
9280
|
+
}
|
|
9281
|
+
_freq;
|
|
9282
|
+
_max;
|
|
9186
9283
|
/**
|
|
9187
9284
|
* The constructor initializes the properties of an object, including default frequency, maximum
|
|
9188
9285
|
* value, a freqMap data structure, the most significant bit, and the count of negative frequencies.
|
|
@@ -9190,17 +9287,13 @@ var _BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
9190
9287
|
* value of 0.
|
|
9191
9288
|
*/
|
|
9192
9289
|
constructor({ frequency = 0, max }) {
|
|
9193
|
-
__publicField(this, "_freq");
|
|
9194
|
-
__publicField(this, "_max");
|
|
9195
|
-
__publicField(this, "_freqMap");
|
|
9196
|
-
__publicField(this, "_msb");
|
|
9197
|
-
__publicField(this, "_negativeCount");
|
|
9198
9290
|
this._freq = frequency;
|
|
9199
9291
|
this._max = max;
|
|
9200
9292
|
this._freqMap = { 0: 0 };
|
|
9201
9293
|
this._msb = getMSB(max);
|
|
9202
9294
|
this._negativeCount = frequency < 0 ? max : 0;
|
|
9203
9295
|
}
|
|
9296
|
+
_freqMap;
|
|
9204
9297
|
/**
|
|
9205
9298
|
* The function returns the frequency map of numbers.
|
|
9206
9299
|
* @returns The `_freqMap` property, which is a record with number keys and number values, is being
|
|
@@ -9209,6 +9302,7 @@ var _BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
9209
9302
|
get freqMap() {
|
|
9210
9303
|
return this._freqMap;
|
|
9211
9304
|
}
|
|
9305
|
+
_msb;
|
|
9212
9306
|
/**
|
|
9213
9307
|
* The function returns the value of the _msb property.
|
|
9214
9308
|
* @returns The `_msb` property of the object.
|
|
@@ -9216,6 +9310,7 @@ var _BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
9216
9310
|
get msb() {
|
|
9217
9311
|
return this._msb;
|
|
9218
9312
|
}
|
|
9313
|
+
_negativeCount;
|
|
9219
9314
|
/**
|
|
9220
9315
|
* The function returns the value of the _negativeCount property.
|
|
9221
9316
|
* @returns The method is returning the value of the variable `_negativeCount`, which is of type
|
|
@@ -9464,11 +9559,12 @@ var _BinaryIndexedTree = class _BinaryIndexedTree {
|
|
|
9464
9559
|
return left;
|
|
9465
9560
|
}
|
|
9466
9561
|
};
|
|
9467
|
-
__name(_BinaryIndexedTree, "BinaryIndexedTree");
|
|
9468
|
-
var BinaryIndexedTree = _BinaryIndexedTree;
|
|
9469
9562
|
|
|
9470
9563
|
// src/data-structures/binary-tree/segment-tree.ts
|
|
9471
|
-
var
|
|
9564
|
+
var SegmentTreeNode = class {
|
|
9565
|
+
static {
|
|
9566
|
+
__name(this, "SegmentTreeNode");
|
|
9567
|
+
}
|
|
9472
9568
|
/**
|
|
9473
9569
|
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
9474
9570
|
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
@@ -9481,17 +9577,12 @@ var _SegmentTreeNode = class _SegmentTreeNode {
|
|
|
9481
9577
|
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
9482
9578
|
*/
|
|
9483
9579
|
constructor(start, end, sum, value) {
|
|
9484
|
-
__publicField(this, "_start", 0);
|
|
9485
|
-
__publicField(this, "_end", 0);
|
|
9486
|
-
__publicField(this, "_value");
|
|
9487
|
-
__publicField(this, "_sum", 0);
|
|
9488
|
-
__publicField(this, "_left");
|
|
9489
|
-
__publicField(this, "_right");
|
|
9490
9580
|
this._start = start;
|
|
9491
9581
|
this._end = end;
|
|
9492
9582
|
this._sum = sum;
|
|
9493
9583
|
this._value = value || void 0;
|
|
9494
9584
|
}
|
|
9585
|
+
_start = 0;
|
|
9495
9586
|
/**
|
|
9496
9587
|
* The function returns the value of the protected variable _start.
|
|
9497
9588
|
* @returns The start value, which is of type number.
|
|
@@ -9506,6 +9597,7 @@ var _SegmentTreeNode = class _SegmentTreeNode {
|
|
|
9506
9597
|
set start(value) {
|
|
9507
9598
|
this._start = value;
|
|
9508
9599
|
}
|
|
9600
|
+
_end = 0;
|
|
9509
9601
|
/**
|
|
9510
9602
|
* The function returns the value of the protected variable `_end`.
|
|
9511
9603
|
* @returns The value of the protected property `_end`.
|
|
@@ -9521,6 +9613,7 @@ var _SegmentTreeNode = class _SegmentTreeNode {
|
|
|
9521
9613
|
set end(value) {
|
|
9522
9614
|
this._end = value;
|
|
9523
9615
|
}
|
|
9616
|
+
_value = void 0;
|
|
9524
9617
|
/**
|
|
9525
9618
|
* The function returns the value of a segment tree node.
|
|
9526
9619
|
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
@@ -9536,6 +9629,7 @@ var _SegmentTreeNode = class _SegmentTreeNode {
|
|
|
9536
9629
|
set value(value) {
|
|
9537
9630
|
this._value = value;
|
|
9538
9631
|
}
|
|
9632
|
+
_sum = 0;
|
|
9539
9633
|
/**
|
|
9540
9634
|
* The function returns the value of the sum property.
|
|
9541
9635
|
* @returns The method is returning the value of the variable `_sum`.
|
|
@@ -9550,6 +9644,7 @@ var _SegmentTreeNode = class _SegmentTreeNode {
|
|
|
9550
9644
|
set sum(value) {
|
|
9551
9645
|
this._sum = value;
|
|
9552
9646
|
}
|
|
9647
|
+
_left = void 0;
|
|
9553
9648
|
/**
|
|
9554
9649
|
* The function returns the left child of a segment tree node.
|
|
9555
9650
|
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
@@ -9566,6 +9661,7 @@ var _SegmentTreeNode = class _SegmentTreeNode {
|
|
|
9566
9661
|
set left(value) {
|
|
9567
9662
|
this._left = value;
|
|
9568
9663
|
}
|
|
9664
|
+
_right = void 0;
|
|
9569
9665
|
/**
|
|
9570
9666
|
* The function returns the right child of a segment tree node.
|
|
9571
9667
|
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
@@ -9583,9 +9679,10 @@ var _SegmentTreeNode = class _SegmentTreeNode {
|
|
|
9583
9679
|
this._right = value;
|
|
9584
9680
|
}
|
|
9585
9681
|
};
|
|
9586
|
-
|
|
9587
|
-
|
|
9588
|
-
|
|
9682
|
+
var SegmentTree = class {
|
|
9683
|
+
static {
|
|
9684
|
+
__name(this, "SegmentTree");
|
|
9685
|
+
}
|
|
9589
9686
|
/**
|
|
9590
9687
|
* The constructor initializes the values, start, end, and root properties of an object.
|
|
9591
9688
|
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
@@ -9596,10 +9693,6 @@ var _SegmentTree = class _SegmentTree {
|
|
|
9596
9693
|
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
9597
9694
|
*/
|
|
9598
9695
|
constructor(values, start, end) {
|
|
9599
|
-
__publicField(this, "_values", []);
|
|
9600
|
-
__publicField(this, "_start", 0);
|
|
9601
|
-
__publicField(this, "_end");
|
|
9602
|
-
__publicField(this, "_root");
|
|
9603
9696
|
start = start || 0;
|
|
9604
9697
|
end = end || values.length - 1;
|
|
9605
9698
|
this._values = values;
|
|
@@ -9612,6 +9705,7 @@ var _SegmentTree = class _SegmentTree {
|
|
|
9612
9705
|
this._values = [];
|
|
9613
9706
|
}
|
|
9614
9707
|
}
|
|
9708
|
+
_values = [];
|
|
9615
9709
|
/**
|
|
9616
9710
|
* The function returns an array of numbers.
|
|
9617
9711
|
* @returns An array of numbers is being returned.
|
|
@@ -9619,6 +9713,7 @@ var _SegmentTree = class _SegmentTree {
|
|
|
9619
9713
|
get values() {
|
|
9620
9714
|
return this._values;
|
|
9621
9715
|
}
|
|
9716
|
+
_start = 0;
|
|
9622
9717
|
/**
|
|
9623
9718
|
* The function returns the value of the protected variable _start.
|
|
9624
9719
|
* @returns The start value, which is of type number.
|
|
@@ -9626,6 +9721,7 @@ var _SegmentTree = class _SegmentTree {
|
|
|
9626
9721
|
get start() {
|
|
9627
9722
|
return this._start;
|
|
9628
9723
|
}
|
|
9724
|
+
_end;
|
|
9629
9725
|
/**
|
|
9630
9726
|
* The function returns the value of the protected variable `_end`.
|
|
9631
9727
|
* @returns The value of the protected property `_end`.
|
|
@@ -9633,6 +9729,7 @@ var _SegmentTree = class _SegmentTree {
|
|
|
9633
9729
|
get end() {
|
|
9634
9730
|
return this._end;
|
|
9635
9731
|
}
|
|
9732
|
+
_root;
|
|
9636
9733
|
/**
|
|
9637
9734
|
* The function returns the root node of a segment tree.
|
|
9638
9735
|
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
@@ -9747,11 +9844,15 @@ var _SegmentTree = class _SegmentTree {
|
|
|
9747
9844
|
return dfs(root, indexA, indexB);
|
|
9748
9845
|
}
|
|
9749
9846
|
};
|
|
9750
|
-
__name(_SegmentTree, "SegmentTree");
|
|
9751
|
-
var SegmentTree = _SegmentTree;
|
|
9752
9847
|
|
|
9753
9848
|
// src/data-structures/binary-tree/avl-tree.ts
|
|
9754
|
-
var
|
|
9849
|
+
var AVLTreeNode = class {
|
|
9850
|
+
static {
|
|
9851
|
+
__name(this, "AVLTreeNode");
|
|
9852
|
+
}
|
|
9853
|
+
key;
|
|
9854
|
+
value;
|
|
9855
|
+
parent = void 0;
|
|
9755
9856
|
/**
|
|
9756
9857
|
* Creates an instance of AVLTreeNode.
|
|
9757
9858
|
* @remarks Time O(1), Space O(1)
|
|
@@ -9760,11 +9861,10 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
9760
9861
|
* @param [value] - The value associated with the key.
|
|
9761
9862
|
*/
|
|
9762
9863
|
constructor(key, value) {
|
|
9763
|
-
|
|
9764
|
-
|
|
9765
|
-
__publicField(this, "_left");
|
|
9766
|
-
__publicField(this, "_right");
|
|
9864
|
+
this.key = key;
|
|
9865
|
+
this.value = value;
|
|
9767
9866
|
}
|
|
9867
|
+
_left = void 0;
|
|
9768
9868
|
/**
|
|
9769
9869
|
* Gets the left child of the node.
|
|
9770
9870
|
* @remarks Time O(1), Space O(1)
|
|
@@ -9786,6 +9886,7 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
9786
9886
|
}
|
|
9787
9887
|
this._left = v;
|
|
9788
9888
|
}
|
|
9889
|
+
_right = void 0;
|
|
9789
9890
|
/**
|
|
9790
9891
|
* Gets the right child of the node.
|
|
9791
9892
|
* @remarks Time O(1), Space O(1)
|
|
@@ -9807,10 +9908,85 @@ var _AVLTreeNode = class _AVLTreeNode extends BSTNode {
|
|
|
9807
9908
|
}
|
|
9808
9909
|
this._right = v;
|
|
9809
9910
|
}
|
|
9911
|
+
_height = 0;
|
|
9912
|
+
/**
|
|
9913
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
9914
|
+
* @remarks Time O(1), Space O(1)
|
|
9915
|
+
*
|
|
9916
|
+
* @returns The height.
|
|
9917
|
+
*/
|
|
9918
|
+
get height() {
|
|
9919
|
+
return this._height;
|
|
9920
|
+
}
|
|
9921
|
+
/**
|
|
9922
|
+
* Sets the height of the node.
|
|
9923
|
+
* @remarks Time O(1), Space O(1)
|
|
9924
|
+
*
|
|
9925
|
+
* @param value - The new height.
|
|
9926
|
+
*/
|
|
9927
|
+
set height(value) {
|
|
9928
|
+
this._height = value;
|
|
9929
|
+
}
|
|
9930
|
+
_color = "BLACK";
|
|
9931
|
+
/**
|
|
9932
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
9933
|
+
* @remarks Time O(1), Space O(1)
|
|
9934
|
+
*
|
|
9935
|
+
* @returns The node's color.
|
|
9936
|
+
*/
|
|
9937
|
+
get color() {
|
|
9938
|
+
return this._color;
|
|
9939
|
+
}
|
|
9940
|
+
/**
|
|
9941
|
+
* Sets the color of the node.
|
|
9942
|
+
* @remarks Time O(1), Space O(1)
|
|
9943
|
+
*
|
|
9944
|
+
* @param value - The new color.
|
|
9945
|
+
*/
|
|
9946
|
+
set color(value) {
|
|
9947
|
+
this._color = value;
|
|
9948
|
+
}
|
|
9949
|
+
_count = 1;
|
|
9950
|
+
/**
|
|
9951
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
9952
|
+
* @remarks Time O(1), Space O(1)
|
|
9953
|
+
*
|
|
9954
|
+
* @returns The subtree node count.
|
|
9955
|
+
*/
|
|
9956
|
+
get count() {
|
|
9957
|
+
return this._count;
|
|
9958
|
+
}
|
|
9959
|
+
/**
|
|
9960
|
+
* Sets the count of nodes in the subtree.
|
|
9961
|
+
* @remarks Time O(1), Space O(1)
|
|
9962
|
+
*
|
|
9963
|
+
* @param value - The new count.
|
|
9964
|
+
*/
|
|
9965
|
+
set count(value) {
|
|
9966
|
+
this._count = value;
|
|
9967
|
+
}
|
|
9968
|
+
/**
|
|
9969
|
+
* Gets the position of the node relative to its parent.
|
|
9970
|
+
* @remarks Time O(1), Space O(1)
|
|
9971
|
+
*
|
|
9972
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
9973
|
+
*/
|
|
9974
|
+
get familyPosition() {
|
|
9975
|
+
if (!this.parent) {
|
|
9976
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
9977
|
+
}
|
|
9978
|
+
if (this.parent.left === this) {
|
|
9979
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
9980
|
+
} else if (this.parent.right === this) {
|
|
9981
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
9982
|
+
}
|
|
9983
|
+
return "MAL_NODE";
|
|
9984
|
+
}
|
|
9810
9985
|
};
|
|
9811
|
-
|
|
9812
|
-
|
|
9813
|
-
|
|
9986
|
+
var AVLTree = class extends BST {
|
|
9987
|
+
static {
|
|
9988
|
+
__name(this, "AVLTree");
|
|
9989
|
+
}
|
|
9814
9990
|
/**
|
|
9815
9991
|
* Creates an instance of AVLTree.
|
|
9816
9992
|
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
@@ -9919,7 +10095,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
9919
10095
|
const out = this._createLike([], options);
|
|
9920
10096
|
let index = 0;
|
|
9921
10097
|
for (const [key, value] of this) {
|
|
9922
|
-
out.add(callback.call(thisArg,
|
|
10098
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
9923
10099
|
}
|
|
9924
10100
|
return out;
|
|
9925
10101
|
}
|
|
@@ -9933,7 +10109,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
9933
10109
|
*/
|
|
9934
10110
|
_createInstance(options) {
|
|
9935
10111
|
const Ctor = this.constructor;
|
|
9936
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
10112
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
9937
10113
|
}
|
|
9938
10114
|
/**
|
|
9939
10115
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
@@ -9946,7 +10122,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
9946
10122
|
*/
|
|
9947
10123
|
_createLike(iter = [], options) {
|
|
9948
10124
|
const Ctor = this.constructor;
|
|
9949
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
10125
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
9950
10126
|
}
|
|
9951
10127
|
/**
|
|
9952
10128
|
* (Protected) Swaps properties of two nodes, including height.
|
|
@@ -10015,7 +10191,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
10015
10191
|
if (A === this.root) {
|
|
10016
10192
|
if (B) this._setRoot(B);
|
|
10017
10193
|
} else {
|
|
10018
|
-
if (
|
|
10194
|
+
if (parentOfA?.left === A) {
|
|
10019
10195
|
parentOfA.left = B;
|
|
10020
10196
|
} else {
|
|
10021
10197
|
if (parentOfA) parentOfA.right = B;
|
|
@@ -10198,11 +10374,15 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
10198
10374
|
return super._replaceNode(oldNode, newNode);
|
|
10199
10375
|
}
|
|
10200
10376
|
};
|
|
10201
|
-
__name(_AVLTree, "AVLTree");
|
|
10202
|
-
var AVLTree = _AVLTree;
|
|
10203
10377
|
|
|
10204
10378
|
// src/data-structures/binary-tree/red-black-tree.ts
|
|
10205
|
-
var
|
|
10379
|
+
var RedBlackTreeNode = class {
|
|
10380
|
+
static {
|
|
10381
|
+
__name(this, "RedBlackTreeNode");
|
|
10382
|
+
}
|
|
10383
|
+
key;
|
|
10384
|
+
value;
|
|
10385
|
+
parent = void 0;
|
|
10206
10386
|
/**
|
|
10207
10387
|
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
10208
10388
|
* @remarks Time O(n log n), Space O(n)
|
|
@@ -10212,12 +10392,11 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
10212
10392
|
* @returns New RedBlackTree instance.
|
|
10213
10393
|
*/
|
|
10214
10394
|
constructor(key, value, color = "BLACK") {
|
|
10215
|
-
|
|
10216
|
-
|
|
10217
|
-
|
|
10218
|
-
__publicField(this, "_right");
|
|
10219
|
-
this._color = color;
|
|
10395
|
+
this.key = key;
|
|
10396
|
+
this.value = value;
|
|
10397
|
+
this.color = color;
|
|
10220
10398
|
}
|
|
10399
|
+
_left = void 0;
|
|
10221
10400
|
/**
|
|
10222
10401
|
* Get the left child pointer.
|
|
10223
10402
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10238,6 +10417,7 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
10238
10417
|
}
|
|
10239
10418
|
this._left = v;
|
|
10240
10419
|
}
|
|
10420
|
+
_right = void 0;
|
|
10241
10421
|
/**
|
|
10242
10422
|
* Get the right child pointer.
|
|
10243
10423
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10258,32 +10438,107 @@ var _RedBlackTreeNode = class _RedBlackTreeNode extends BSTNode {
|
|
|
10258
10438
|
}
|
|
10259
10439
|
this._right = v;
|
|
10260
10440
|
}
|
|
10261
|
-
|
|
10262
|
-
__name(_RedBlackTreeNode, "RedBlackTreeNode");
|
|
10263
|
-
var RedBlackTreeNode = _RedBlackTreeNode;
|
|
10264
|
-
var _RedBlackTree = class _RedBlackTree extends BST {
|
|
10265
|
-
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
10266
|
-
super([], options);
|
|
10267
|
-
__publicField(this, "_root");
|
|
10268
|
-
this._root = this.NIL;
|
|
10269
|
-
if (keysNodesEntriesOrRaws) {
|
|
10270
|
-
this.addMany(keysNodesEntriesOrRaws);
|
|
10271
|
-
}
|
|
10272
|
-
}
|
|
10441
|
+
_height = 0;
|
|
10273
10442
|
/**
|
|
10274
|
-
*
|
|
10443
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
10275
10444
|
* @remarks Time O(1), Space O(1)
|
|
10276
|
-
*
|
|
10445
|
+
*
|
|
10446
|
+
* @returns The height.
|
|
10277
10447
|
*/
|
|
10278
|
-
get
|
|
10279
|
-
return this.
|
|
10448
|
+
get height() {
|
|
10449
|
+
return this._height;
|
|
10280
10450
|
}
|
|
10281
10451
|
/**
|
|
10282
|
-
*
|
|
10452
|
+
* Sets the height of the node.
|
|
10283
10453
|
* @remarks Time O(1), Space O(1)
|
|
10284
|
-
*
|
|
10285
|
-
* @param
|
|
10286
|
-
|
|
10454
|
+
*
|
|
10455
|
+
* @param value - The new height.
|
|
10456
|
+
*/
|
|
10457
|
+
set height(value) {
|
|
10458
|
+
this._height = value;
|
|
10459
|
+
}
|
|
10460
|
+
_color = "BLACK";
|
|
10461
|
+
/**
|
|
10462
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
10463
|
+
* @remarks Time O(1), Space O(1)
|
|
10464
|
+
*
|
|
10465
|
+
* @returns The node's color.
|
|
10466
|
+
*/
|
|
10467
|
+
get color() {
|
|
10468
|
+
return this._color;
|
|
10469
|
+
}
|
|
10470
|
+
/**
|
|
10471
|
+
* Sets the color of the node.
|
|
10472
|
+
* @remarks Time O(1), Space O(1)
|
|
10473
|
+
*
|
|
10474
|
+
* @param value - The new color.
|
|
10475
|
+
*/
|
|
10476
|
+
set color(value) {
|
|
10477
|
+
this._color = value;
|
|
10478
|
+
}
|
|
10479
|
+
_count = 1;
|
|
10480
|
+
/**
|
|
10481
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
10482
|
+
* @remarks Time O(1), Space O(1)
|
|
10483
|
+
*
|
|
10484
|
+
* @returns The subtree node count.
|
|
10485
|
+
*/
|
|
10486
|
+
get count() {
|
|
10487
|
+
return this._count;
|
|
10488
|
+
}
|
|
10489
|
+
/**
|
|
10490
|
+
* Sets the count of nodes in the subtree.
|
|
10491
|
+
* @remarks Time O(1), Space O(1)
|
|
10492
|
+
*
|
|
10493
|
+
* @param value - The new count.
|
|
10494
|
+
*/
|
|
10495
|
+
set count(value) {
|
|
10496
|
+
this._count = value;
|
|
10497
|
+
}
|
|
10498
|
+
/**
|
|
10499
|
+
* Gets the position of the node relative to its parent.
|
|
10500
|
+
* @remarks Time O(1), Space O(1)
|
|
10501
|
+
*
|
|
10502
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
10503
|
+
*/
|
|
10504
|
+
get familyPosition() {
|
|
10505
|
+
if (!this.parent) {
|
|
10506
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
10507
|
+
}
|
|
10508
|
+
if (this.parent.left === this) {
|
|
10509
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
10510
|
+
} else if (this.parent.right === this) {
|
|
10511
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
10512
|
+
}
|
|
10513
|
+
return "MAL_NODE";
|
|
10514
|
+
}
|
|
10515
|
+
};
|
|
10516
|
+
var RedBlackTree = class extends BST {
|
|
10517
|
+
static {
|
|
10518
|
+
__name(this, "RedBlackTree");
|
|
10519
|
+
}
|
|
10520
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
10521
|
+
super([], options);
|
|
10522
|
+
this._root = this.NIL;
|
|
10523
|
+
if (keysNodesEntriesOrRaws) {
|
|
10524
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
10525
|
+
}
|
|
10526
|
+
}
|
|
10527
|
+
_root;
|
|
10528
|
+
/**
|
|
10529
|
+
* Get the current root node.
|
|
10530
|
+
* @remarks Time O(1), Space O(1)
|
|
10531
|
+
* @returns Root node, or undefined.
|
|
10532
|
+
*/
|
|
10533
|
+
get root() {
|
|
10534
|
+
return this._root;
|
|
10535
|
+
}
|
|
10536
|
+
/**
|
|
10537
|
+
* Create a red-black node for the given key/value (value ignored in map mode).
|
|
10538
|
+
* @remarks Time O(1), Space O(1)
|
|
10539
|
+
* @param key - See parameter type for details.
|
|
10540
|
+
* @param [value] - See parameter type for details.
|
|
10541
|
+
* @param color - See parameter type for details.
|
|
10287
10542
|
* @returns A new RedBlackTreeNode instance.
|
|
10288
10543
|
*/
|
|
10289
10544
|
createNode(key, value, color = "BLACK") {
|
|
@@ -10408,17 +10663,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10408
10663
|
const out = this._createLike([], options);
|
|
10409
10664
|
let index = 0;
|
|
10410
10665
|
for (const [key, value] of this) {
|
|
10411
|
-
out.add(callback.call(thisArg,
|
|
10666
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
10412
10667
|
}
|
|
10413
10668
|
return out;
|
|
10414
10669
|
}
|
|
10415
10670
|
_createInstance(options) {
|
|
10416
10671
|
const Ctor = this.constructor;
|
|
10417
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
10672
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
10418
10673
|
}
|
|
10419
10674
|
_createLike(iter = [], options) {
|
|
10420
10675
|
const Ctor = this.constructor;
|
|
10421
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
10676
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
10422
10677
|
}
|
|
10423
10678
|
_setRoot(v) {
|
|
10424
10679
|
if (v) {
|
|
@@ -10437,16 +10692,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10437
10692
|
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
10438
10693
|
*/
|
|
10439
10694
|
_insert(node) {
|
|
10440
|
-
|
|
10441
|
-
let current = this.root;
|
|
10695
|
+
let current = this.root ?? this.NIL;
|
|
10442
10696
|
let parent = void 0;
|
|
10443
|
-
while (this.
|
|
10697
|
+
while (current !== this.NIL) {
|
|
10444
10698
|
parent = current;
|
|
10445
10699
|
const compared = this._compare(node.key, current.key);
|
|
10446
10700
|
if (compared < 0) {
|
|
10447
|
-
current =
|
|
10701
|
+
current = current.left ?? this.NIL;
|
|
10448
10702
|
} else if (compared > 0) {
|
|
10449
|
-
current =
|
|
10703
|
+
current = current.right ?? this.NIL;
|
|
10450
10704
|
} else {
|
|
10451
10705
|
this._replaceNode(current, node);
|
|
10452
10706
|
return "UPDATED";
|
|
@@ -10492,11 +10746,10 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10492
10746
|
* @returns void
|
|
10493
10747
|
*/
|
|
10494
10748
|
_insertFixup(z) {
|
|
10495
|
-
|
|
10496
|
-
|
|
10497
|
-
if (z.parent === ((_b = z.parent.parent) == null ? void 0 : _b.left)) {
|
|
10749
|
+
while (z?.parent?.color === "RED") {
|
|
10750
|
+
if (z.parent === z.parent.parent?.left) {
|
|
10498
10751
|
const y = z.parent.parent.right;
|
|
10499
|
-
if (
|
|
10752
|
+
if (y?.color === "RED") {
|
|
10500
10753
|
z.parent.color = "BLACK";
|
|
10501
10754
|
y.color = "BLACK";
|
|
10502
10755
|
z.parent.parent.color = "RED";
|
|
@@ -10506,15 +10759,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10506
10759
|
z = z.parent;
|
|
10507
10760
|
this._leftRotate(z);
|
|
10508
10761
|
}
|
|
10509
|
-
if (z &&
|
|
10762
|
+
if (z && z.parent && z.parent.parent) {
|
|
10510
10763
|
z.parent.color = "BLACK";
|
|
10511
10764
|
z.parent.parent.color = "RED";
|
|
10512
10765
|
this._rightRotate(z.parent.parent);
|
|
10513
10766
|
}
|
|
10514
10767
|
}
|
|
10515
10768
|
} else {
|
|
10516
|
-
const y =
|
|
10517
|
-
if (
|
|
10769
|
+
const y = z?.parent?.parent?.left ?? void 0;
|
|
10770
|
+
if (y?.color === "RED") {
|
|
10518
10771
|
z.parent.color = "BLACK";
|
|
10519
10772
|
y.color = "BLACK";
|
|
10520
10773
|
z.parent.parent.color = "RED";
|
|
@@ -10524,7 +10777,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10524
10777
|
z = z.parent;
|
|
10525
10778
|
this._rightRotate(z);
|
|
10526
10779
|
}
|
|
10527
|
-
if (z &&
|
|
10780
|
+
if (z && z.parent && z.parent.parent) {
|
|
10528
10781
|
z.parent.color = "BLACK";
|
|
10529
10782
|
z.parent.parent.color = "RED";
|
|
10530
10783
|
this._leftRotate(z.parent.parent);
|
|
@@ -10541,7 +10794,6 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10541
10794
|
* @returns void
|
|
10542
10795
|
*/
|
|
10543
10796
|
_deleteFixup(node) {
|
|
10544
|
-
var _a, _b, _c, _d;
|
|
10545
10797
|
if (!node || node === this.root || node.color === "BLACK") {
|
|
10546
10798
|
if (node) {
|
|
10547
10799
|
node.color = "BLACK";
|
|
@@ -10555,17 +10807,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10555
10807
|
}
|
|
10556
10808
|
if (node === parent.left) {
|
|
10557
10809
|
let sibling = parent.right;
|
|
10558
|
-
if (
|
|
10810
|
+
if (sibling?.color === "RED") {
|
|
10559
10811
|
sibling.color = "BLACK";
|
|
10560
10812
|
parent.color = "RED";
|
|
10561
10813
|
this._leftRotate(parent);
|
|
10562
10814
|
sibling = parent.right;
|
|
10563
10815
|
}
|
|
10564
|
-
if ((
|
|
10816
|
+
if ((sibling?.left?.color ?? "BLACK") === "BLACK") {
|
|
10565
10817
|
if (sibling) sibling.color = "RED";
|
|
10566
10818
|
node = parent;
|
|
10567
10819
|
} else {
|
|
10568
|
-
if (sibling
|
|
10820
|
+
if (sibling?.left) sibling.left.color = "BLACK";
|
|
10569
10821
|
if (sibling) sibling.color = parent.color;
|
|
10570
10822
|
parent.color = "BLACK";
|
|
10571
10823
|
this._rightRotate(parent);
|
|
@@ -10573,17 +10825,17 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10573
10825
|
}
|
|
10574
10826
|
} else {
|
|
10575
10827
|
let sibling = parent.left;
|
|
10576
|
-
if (
|
|
10828
|
+
if (sibling?.color === "RED") {
|
|
10577
10829
|
sibling.color = "BLACK";
|
|
10578
10830
|
if (parent) parent.color = "RED";
|
|
10579
10831
|
this._rightRotate(parent);
|
|
10580
10832
|
if (parent) sibling = parent.left;
|
|
10581
10833
|
}
|
|
10582
|
-
if ((
|
|
10834
|
+
if ((sibling?.right?.color ?? "BLACK") === "BLACK") {
|
|
10583
10835
|
if (sibling) sibling.color = "RED";
|
|
10584
10836
|
node = parent;
|
|
10585
10837
|
} else {
|
|
10586
|
-
if (sibling
|
|
10838
|
+
if (sibling?.right) sibling.right.color = "BLACK";
|
|
10587
10839
|
if (sibling) sibling.color = parent.color;
|
|
10588
10840
|
if (parent) parent.color = "BLACK";
|
|
10589
10841
|
this._leftRotate(parent);
|
|
@@ -10607,7 +10859,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10607
10859
|
}
|
|
10608
10860
|
const y = x.right;
|
|
10609
10861
|
x.right = y.left;
|
|
10610
|
-
if (
|
|
10862
|
+
if (y.left && y.left !== this.NIL) {
|
|
10611
10863
|
y.left.parent = x;
|
|
10612
10864
|
}
|
|
10613
10865
|
y.parent = x.parent;
|
|
@@ -10633,7 +10885,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10633
10885
|
}
|
|
10634
10886
|
const x = y.left;
|
|
10635
10887
|
y.left = x.right;
|
|
10636
|
-
if (
|
|
10888
|
+
if (x.right && x.right !== this.NIL) {
|
|
10637
10889
|
x.right.parent = y;
|
|
10638
10890
|
}
|
|
10639
10891
|
x.parent = y.parent;
|
|
@@ -10648,11 +10900,15 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10648
10900
|
y.parent = x;
|
|
10649
10901
|
}
|
|
10650
10902
|
};
|
|
10651
|
-
__name(_RedBlackTree, "RedBlackTree");
|
|
10652
|
-
var RedBlackTree = _RedBlackTree;
|
|
10653
10903
|
|
|
10654
10904
|
// src/data-structures/binary-tree/avl-tree-multi-map.ts
|
|
10655
|
-
var
|
|
10905
|
+
var AVLTreeMultiMapNode = class {
|
|
10906
|
+
static {
|
|
10907
|
+
__name(this, "AVLTreeMultiMapNode");
|
|
10908
|
+
}
|
|
10909
|
+
key;
|
|
10910
|
+
value;
|
|
10911
|
+
parent = void 0;
|
|
10656
10912
|
/**
|
|
10657
10913
|
* Create an AVLTreeMultiMap node with a value bucket.
|
|
10658
10914
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10660,12 +10916,11 @@ var _AVLTreeMultiMapNode = class _AVLTreeMultiMapNode extends AVLTreeNode {
|
|
|
10660
10916
|
* @param value - Initial array of values.
|
|
10661
10917
|
* @returns New AVLTreeMultiMapNode instance.
|
|
10662
10918
|
*/
|
|
10663
|
-
constructor(key, value) {
|
|
10664
|
-
|
|
10665
|
-
|
|
10666
|
-
__publicField(this, "_left");
|
|
10667
|
-
__publicField(this, "_right");
|
|
10919
|
+
constructor(key, value = []) {
|
|
10920
|
+
this.key = key;
|
|
10921
|
+
this.value = value;
|
|
10668
10922
|
}
|
|
10923
|
+
_left = void 0;
|
|
10669
10924
|
/**
|
|
10670
10925
|
* Get the left child pointer.
|
|
10671
10926
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10686,6 +10941,7 @@ var _AVLTreeMultiMapNode = class _AVLTreeMultiMapNode extends AVLTreeNode {
|
|
|
10686
10941
|
}
|
|
10687
10942
|
this._left = v;
|
|
10688
10943
|
}
|
|
10944
|
+
_right = void 0;
|
|
10689
10945
|
/**
|
|
10690
10946
|
* Get the right child pointer.
|
|
10691
10947
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10706,10 +10962,85 @@ var _AVLTreeMultiMapNode = class _AVLTreeMultiMapNode extends AVLTreeNode {
|
|
|
10706
10962
|
}
|
|
10707
10963
|
this._right = v;
|
|
10708
10964
|
}
|
|
10965
|
+
_height = 0;
|
|
10966
|
+
/**
|
|
10967
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
10968
|
+
* @remarks Time O(1), Space O(1)
|
|
10969
|
+
*
|
|
10970
|
+
* @returns The height.
|
|
10971
|
+
*/
|
|
10972
|
+
get height() {
|
|
10973
|
+
return this._height;
|
|
10974
|
+
}
|
|
10975
|
+
/**
|
|
10976
|
+
* Sets the height of the node.
|
|
10977
|
+
* @remarks Time O(1), Space O(1)
|
|
10978
|
+
*
|
|
10979
|
+
* @param value - The new height.
|
|
10980
|
+
*/
|
|
10981
|
+
set height(value) {
|
|
10982
|
+
this._height = value;
|
|
10983
|
+
}
|
|
10984
|
+
_color = "BLACK";
|
|
10985
|
+
/**
|
|
10986
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
10987
|
+
* @remarks Time O(1), Space O(1)
|
|
10988
|
+
*
|
|
10989
|
+
* @returns The node's color.
|
|
10990
|
+
*/
|
|
10991
|
+
get color() {
|
|
10992
|
+
return this._color;
|
|
10993
|
+
}
|
|
10994
|
+
/**
|
|
10995
|
+
* Sets the color of the node.
|
|
10996
|
+
* @remarks Time O(1), Space O(1)
|
|
10997
|
+
*
|
|
10998
|
+
* @param value - The new color.
|
|
10999
|
+
*/
|
|
11000
|
+
set color(value) {
|
|
11001
|
+
this._color = value;
|
|
11002
|
+
}
|
|
11003
|
+
_count = 1;
|
|
11004
|
+
/**
|
|
11005
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
11006
|
+
* @remarks Time O(1), Space O(1)
|
|
11007
|
+
*
|
|
11008
|
+
* @returns The subtree node count.
|
|
11009
|
+
*/
|
|
11010
|
+
get count() {
|
|
11011
|
+
return this._count;
|
|
11012
|
+
}
|
|
11013
|
+
/**
|
|
11014
|
+
* Sets the count of nodes in the subtree.
|
|
11015
|
+
* @remarks Time O(1), Space O(1)
|
|
11016
|
+
*
|
|
11017
|
+
* @param value - The new count.
|
|
11018
|
+
*/
|
|
11019
|
+
set count(value) {
|
|
11020
|
+
this._count = value;
|
|
11021
|
+
}
|
|
11022
|
+
/**
|
|
11023
|
+
* Gets the position of the node relative to its parent.
|
|
11024
|
+
* @remarks Time O(1), Space O(1)
|
|
11025
|
+
*
|
|
11026
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
11027
|
+
*/
|
|
11028
|
+
get familyPosition() {
|
|
11029
|
+
if (!this.parent) {
|
|
11030
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
11031
|
+
}
|
|
11032
|
+
if (this.parent.left === this) {
|
|
11033
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
11034
|
+
} else if (this.parent.right === this) {
|
|
11035
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
11036
|
+
}
|
|
11037
|
+
return "MAL_NODE";
|
|
11038
|
+
}
|
|
10709
11039
|
};
|
|
10710
|
-
|
|
10711
|
-
|
|
10712
|
-
|
|
11040
|
+
var AVLTreeMultiMap = class extends AVLTree {
|
|
11041
|
+
static {
|
|
11042
|
+
__name(this, "AVLTreeMultiMap");
|
|
11043
|
+
}
|
|
10713
11044
|
/**
|
|
10714
11045
|
* Create an AVLTreeMultiMap and optionally bulk-insert items.
|
|
10715
11046
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -10726,6 +11057,16 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
10726
11057
|
createNode(key, value = []) {
|
|
10727
11058
|
return new AVLTreeMultiMapNode(key, this._isMapMode ? [] : value);
|
|
10728
11059
|
}
|
|
11060
|
+
/**
|
|
11061
|
+
* Checks if the given item is a `AVLTreeMultiMapNode` instance.
|
|
11062
|
+
* @remarks Time O(1), Space O(1)
|
|
11063
|
+
*
|
|
11064
|
+
* @param keyNodeOrEntry - The item to check.
|
|
11065
|
+
* @returns True if it's a AVLTreeMultiMapNode, false otherwise.
|
|
11066
|
+
*/
|
|
11067
|
+
isNode(keyNodeOrEntry) {
|
|
11068
|
+
return keyNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
11069
|
+
}
|
|
10729
11070
|
/**
|
|
10730
11071
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
10731
11072
|
* @remarks Time O(log N + M), Space O(1)
|
|
@@ -10834,7 +11175,7 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
10834
11175
|
map(callback, options, thisArg) {
|
|
10835
11176
|
const out = this._createLike([], options);
|
|
10836
11177
|
let i = 0;
|
|
10837
|
-
for (const [k, v] of this) out.add(callback.call(thisArg,
|
|
11178
|
+
for (const [k, v] of this) out.add(callback.call(thisArg, v, k, i++, this));
|
|
10838
11179
|
return out;
|
|
10839
11180
|
}
|
|
10840
11181
|
/**
|
|
@@ -10847,9 +11188,8 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
10847
11188
|
* @returns An empty like-kind instance.
|
|
10848
11189
|
*/
|
|
10849
11190
|
_createInstance(options) {
|
|
10850
|
-
var _a, _b;
|
|
10851
11191
|
const Ctor = this.constructor;
|
|
10852
|
-
return new Ctor([], { ...
|
|
11192
|
+
return new Ctor([], { ...this._snapshotOptions?.() ?? {}, ...options ?? {} });
|
|
10853
11193
|
}
|
|
10854
11194
|
/**
|
|
10855
11195
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
@@ -10862,16 +11202,19 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
10862
11202
|
* @returns A like-kind AVLTree built from the iterable.
|
|
10863
11203
|
*/
|
|
10864
11204
|
_createLike(iter = [], options) {
|
|
10865
|
-
var _a, _b;
|
|
10866
11205
|
const Ctor = this.constructor;
|
|
10867
|
-
return new Ctor(iter, { ...
|
|
11206
|
+
return new Ctor(iter, { ...this._snapshotOptions?.() ?? {}, ...options ?? {} });
|
|
10868
11207
|
}
|
|
10869
11208
|
};
|
|
10870
|
-
__name(_AVLTreeMultiMap, "AVLTreeMultiMap");
|
|
10871
|
-
var AVLTreeMultiMap = _AVLTreeMultiMap;
|
|
10872
11209
|
|
|
10873
11210
|
// src/data-structures/binary-tree/tree-multi-map.ts
|
|
10874
|
-
var
|
|
11211
|
+
var TreeMultiMapNode = class {
|
|
11212
|
+
static {
|
|
11213
|
+
__name(this, "TreeMultiMapNode");
|
|
11214
|
+
}
|
|
11215
|
+
key;
|
|
11216
|
+
value;
|
|
11217
|
+
parent = void 0;
|
|
10875
11218
|
/**
|
|
10876
11219
|
* Create a TreeMultiMap node with an optional value bucket.
|
|
10877
11220
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10879,12 +11222,12 @@ var _TreeMultiMapNode = class _TreeMultiMapNode extends RedBlackTreeNode {
|
|
|
10879
11222
|
* @param [value] - Initial array of values.
|
|
10880
11223
|
* @returns New TreeMultiMapNode instance.
|
|
10881
11224
|
*/
|
|
10882
|
-
constructor(key, value) {
|
|
10883
|
-
|
|
10884
|
-
|
|
10885
|
-
|
|
10886
|
-
__publicField(this, "_right");
|
|
11225
|
+
constructor(key, value = [], color = "BLACK") {
|
|
11226
|
+
this.key = key;
|
|
11227
|
+
this.value = value;
|
|
11228
|
+
this.color = color;
|
|
10887
11229
|
}
|
|
11230
|
+
_left = void 0;
|
|
10888
11231
|
/**
|
|
10889
11232
|
* Get the left child pointer.
|
|
10890
11233
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10905,6 +11248,7 @@ var _TreeMultiMapNode = class _TreeMultiMapNode extends RedBlackTreeNode {
|
|
|
10905
11248
|
}
|
|
10906
11249
|
this._left = v;
|
|
10907
11250
|
}
|
|
11251
|
+
_right = void 0;
|
|
10908
11252
|
/**
|
|
10909
11253
|
* Get the right child pointer.
|
|
10910
11254
|
* @remarks Time O(1), Space O(1)
|
|
@@ -10925,10 +11269,85 @@ var _TreeMultiMapNode = class _TreeMultiMapNode extends RedBlackTreeNode {
|
|
|
10925
11269
|
}
|
|
10926
11270
|
this._right = v;
|
|
10927
11271
|
}
|
|
11272
|
+
_height = 0;
|
|
11273
|
+
/**
|
|
11274
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
11275
|
+
* @remarks Time O(1), Space O(1)
|
|
11276
|
+
*
|
|
11277
|
+
* @returns The height.
|
|
11278
|
+
*/
|
|
11279
|
+
get height() {
|
|
11280
|
+
return this._height;
|
|
11281
|
+
}
|
|
11282
|
+
/**
|
|
11283
|
+
* Sets the height of the node.
|
|
11284
|
+
* @remarks Time O(1), Space O(1)
|
|
11285
|
+
*
|
|
11286
|
+
* @param value - The new height.
|
|
11287
|
+
*/
|
|
11288
|
+
set height(value) {
|
|
11289
|
+
this._height = value;
|
|
11290
|
+
}
|
|
11291
|
+
_color = "BLACK";
|
|
11292
|
+
/**
|
|
11293
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
11294
|
+
* @remarks Time O(1), Space O(1)
|
|
11295
|
+
*
|
|
11296
|
+
* @returns The node's color.
|
|
11297
|
+
*/
|
|
11298
|
+
get color() {
|
|
11299
|
+
return this._color;
|
|
11300
|
+
}
|
|
11301
|
+
/**
|
|
11302
|
+
* Sets the color of the node.
|
|
11303
|
+
* @remarks Time O(1), Space O(1)
|
|
11304
|
+
*
|
|
11305
|
+
* @param value - The new color.
|
|
11306
|
+
*/
|
|
11307
|
+
set color(value) {
|
|
11308
|
+
this._color = value;
|
|
11309
|
+
}
|
|
11310
|
+
_count = 1;
|
|
11311
|
+
/**
|
|
11312
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
11313
|
+
* @remarks Time O(1), Space O(1)
|
|
11314
|
+
*
|
|
11315
|
+
* @returns The subtree node count.
|
|
11316
|
+
*/
|
|
11317
|
+
get count() {
|
|
11318
|
+
return this._count;
|
|
11319
|
+
}
|
|
11320
|
+
/**
|
|
11321
|
+
* Sets the count of nodes in the subtree.
|
|
11322
|
+
* @remarks Time O(1), Space O(1)
|
|
11323
|
+
*
|
|
11324
|
+
* @param value - The new count.
|
|
11325
|
+
*/
|
|
11326
|
+
set count(value) {
|
|
11327
|
+
this._count = value;
|
|
11328
|
+
}
|
|
11329
|
+
/**
|
|
11330
|
+
* Gets the position of the node relative to its parent.
|
|
11331
|
+
* @remarks Time O(1), Space O(1)
|
|
11332
|
+
*
|
|
11333
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
11334
|
+
*/
|
|
11335
|
+
get familyPosition() {
|
|
11336
|
+
if (!this.parent) {
|
|
11337
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
11338
|
+
}
|
|
11339
|
+
if (this.parent.left === this) {
|
|
11340
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
11341
|
+
} else if (this.parent.right === this) {
|
|
11342
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
11343
|
+
}
|
|
11344
|
+
return "MAL_NODE";
|
|
11345
|
+
}
|
|
10928
11346
|
};
|
|
10929
|
-
|
|
10930
|
-
|
|
10931
|
-
|
|
11347
|
+
var TreeMultiMap = class extends RedBlackTree {
|
|
11348
|
+
static {
|
|
11349
|
+
__name(this, "TreeMultiMap");
|
|
11350
|
+
}
|
|
10932
11351
|
/**
|
|
10933
11352
|
* Create a TreeMultiMap and optionally bulk-insert items.
|
|
10934
11353
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -10945,6 +11364,16 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
10945
11364
|
createNode(key, value = []) {
|
|
10946
11365
|
return new TreeMultiMapNode(key, this._isMapMode ? [] : value);
|
|
10947
11366
|
}
|
|
11367
|
+
/**
|
|
11368
|
+
* Checks if the given item is a `TreeMultiMapNode` instance.
|
|
11369
|
+
* @remarks Time O(1), Space O(1)
|
|
11370
|
+
*
|
|
11371
|
+
* @param keyNodeOrEntry - The item to check.
|
|
11372
|
+
* @returns True if it's a TreeMultiMapNode, false otherwise.
|
|
11373
|
+
*/
|
|
11374
|
+
isNode(keyNodeOrEntry) {
|
|
11375
|
+
return keyNodeOrEntry instanceof TreeMultiMapNode;
|
|
11376
|
+
}
|
|
10948
11377
|
/**
|
|
10949
11378
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
10950
11379
|
* @remarks Time O(log N + M), Space O(1)
|
|
@@ -11025,7 +11454,7 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
11025
11454
|
map(callback, options, thisArg) {
|
|
11026
11455
|
const out = this._createLike([], options);
|
|
11027
11456
|
let i = 0;
|
|
11028
|
-
for (const [k, v] of this) out.add(callback.call(thisArg,
|
|
11457
|
+
for (const [k, v] of this) out.add(callback.call(thisArg, v, k, i++, this));
|
|
11029
11458
|
return out;
|
|
11030
11459
|
}
|
|
11031
11460
|
/**
|
|
@@ -11038,9 +11467,8 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
11038
11467
|
* @returns An empty like-kind instance.
|
|
11039
11468
|
*/
|
|
11040
11469
|
_createInstance(options) {
|
|
11041
|
-
var _a, _b;
|
|
11042
11470
|
const Ctor = this.constructor;
|
|
11043
|
-
return new Ctor([], { ...
|
|
11471
|
+
return new Ctor([], { ...this._snapshotOptions?.() ?? {}, ...options ?? {} });
|
|
11044
11472
|
}
|
|
11045
11473
|
/**
|
|
11046
11474
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
@@ -11053,16 +11481,19 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
11053
11481
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
11054
11482
|
*/
|
|
11055
11483
|
_createLike(iter = [], options) {
|
|
11056
|
-
var _a, _b;
|
|
11057
11484
|
const Ctor = this.constructor;
|
|
11058
|
-
return new Ctor(iter, { ...
|
|
11485
|
+
return new Ctor(iter, { ...this._snapshotOptions?.() ?? {}, ...options ?? {} });
|
|
11059
11486
|
}
|
|
11060
11487
|
};
|
|
11061
|
-
__name(_TreeMultiMap, "TreeMultiMap");
|
|
11062
|
-
var TreeMultiMap = _TreeMultiMap;
|
|
11063
11488
|
|
|
11064
11489
|
// src/data-structures/binary-tree/tree-counter.ts
|
|
11065
|
-
var
|
|
11490
|
+
var TreeCounterNode = class {
|
|
11491
|
+
static {
|
|
11492
|
+
__name(this, "TreeCounterNode");
|
|
11493
|
+
}
|
|
11494
|
+
key;
|
|
11495
|
+
value;
|
|
11496
|
+
parent = void 0;
|
|
11066
11497
|
/**
|
|
11067
11498
|
* Create a tree counter node.
|
|
11068
11499
|
* @remarks Time O(1), Space O(1)
|
|
@@ -11073,12 +11504,12 @@ var _TreeCounterNode = class _TreeCounterNode extends RedBlackTreeNode {
|
|
|
11073
11504
|
* @returns New TreeCounterNode instance.
|
|
11074
11505
|
*/
|
|
11075
11506
|
constructor(key, value, count = 1, color = "BLACK") {
|
|
11076
|
-
|
|
11077
|
-
|
|
11078
|
-
|
|
11079
|
-
__publicField(this, "_right");
|
|
11507
|
+
this.key = key;
|
|
11508
|
+
this.value = value;
|
|
11509
|
+
this.color = color;
|
|
11080
11510
|
this.count = count;
|
|
11081
11511
|
}
|
|
11512
|
+
_left = void 0;
|
|
11082
11513
|
/**
|
|
11083
11514
|
* Get the left child pointer.
|
|
11084
11515
|
* @remarks Time O(1), Space O(1)
|
|
@@ -11099,6 +11530,7 @@ var _TreeCounterNode = class _TreeCounterNode extends RedBlackTreeNode {
|
|
|
11099
11530
|
}
|
|
11100
11531
|
this._left = v;
|
|
11101
11532
|
}
|
|
11533
|
+
_right = void 0;
|
|
11102
11534
|
/**
|
|
11103
11535
|
* Get the right child pointer.
|
|
11104
11536
|
* @remarks Time O(1), Space O(1)
|
|
@@ -11119,10 +11551,85 @@ var _TreeCounterNode = class _TreeCounterNode extends RedBlackTreeNode {
|
|
|
11119
11551
|
}
|
|
11120
11552
|
this._right = v;
|
|
11121
11553
|
}
|
|
11554
|
+
_height = 0;
|
|
11555
|
+
/**
|
|
11556
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
11557
|
+
* @remarks Time O(1), Space O(1)
|
|
11558
|
+
*
|
|
11559
|
+
* @returns The height.
|
|
11560
|
+
*/
|
|
11561
|
+
get height() {
|
|
11562
|
+
return this._height;
|
|
11563
|
+
}
|
|
11564
|
+
/**
|
|
11565
|
+
* Sets the height of the node.
|
|
11566
|
+
* @remarks Time O(1), Space O(1)
|
|
11567
|
+
*
|
|
11568
|
+
* @param value - The new height.
|
|
11569
|
+
*/
|
|
11570
|
+
set height(value) {
|
|
11571
|
+
this._height = value;
|
|
11572
|
+
}
|
|
11573
|
+
_color = "BLACK";
|
|
11574
|
+
/**
|
|
11575
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
11576
|
+
* @remarks Time O(1), Space O(1)
|
|
11577
|
+
*
|
|
11578
|
+
* @returns The node's color.
|
|
11579
|
+
*/
|
|
11580
|
+
get color() {
|
|
11581
|
+
return this._color;
|
|
11582
|
+
}
|
|
11583
|
+
/**
|
|
11584
|
+
* Sets the color of the node.
|
|
11585
|
+
* @remarks Time O(1), Space O(1)
|
|
11586
|
+
*
|
|
11587
|
+
* @param value - The new color.
|
|
11588
|
+
*/
|
|
11589
|
+
set color(value) {
|
|
11590
|
+
this._color = value;
|
|
11591
|
+
}
|
|
11592
|
+
_count = 1;
|
|
11593
|
+
/**
|
|
11594
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
11595
|
+
* @remarks Time O(1), Space O(1)
|
|
11596
|
+
*
|
|
11597
|
+
* @returns The subtree node count.
|
|
11598
|
+
*/
|
|
11599
|
+
get count() {
|
|
11600
|
+
return this._count;
|
|
11601
|
+
}
|
|
11602
|
+
/**
|
|
11603
|
+
* Sets the count of nodes in the subtree.
|
|
11604
|
+
* @remarks Time O(1), Space O(1)
|
|
11605
|
+
*
|
|
11606
|
+
* @param value - The new count.
|
|
11607
|
+
*/
|
|
11608
|
+
set count(value) {
|
|
11609
|
+
this._count = value;
|
|
11610
|
+
}
|
|
11611
|
+
/**
|
|
11612
|
+
* Gets the position of the node relative to its parent.
|
|
11613
|
+
* @remarks Time O(1), Space O(1)
|
|
11614
|
+
*
|
|
11615
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
11616
|
+
*/
|
|
11617
|
+
get familyPosition() {
|
|
11618
|
+
if (!this.parent) {
|
|
11619
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
11620
|
+
}
|
|
11621
|
+
if (this.parent.left === this) {
|
|
11622
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
11623
|
+
} else if (this.parent.right === this) {
|
|
11624
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
11625
|
+
}
|
|
11626
|
+
return "MAL_NODE";
|
|
11627
|
+
}
|
|
11122
11628
|
};
|
|
11123
|
-
|
|
11124
|
-
|
|
11125
|
-
|
|
11629
|
+
var TreeCounter = class extends RedBlackTree {
|
|
11630
|
+
static {
|
|
11631
|
+
__name(this, "TreeCounter");
|
|
11632
|
+
}
|
|
11126
11633
|
/**
|
|
11127
11634
|
* Create a TreeCounter and optionally bulk-insert items.
|
|
11128
11635
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -11132,9 +11639,9 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
11132
11639
|
*/
|
|
11133
11640
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11134
11641
|
super([], options);
|
|
11135
|
-
__publicField(this, "_count", 0);
|
|
11136
11642
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
11137
11643
|
}
|
|
11644
|
+
_count = 0;
|
|
11138
11645
|
/**
|
|
11139
11646
|
* Get the total aggregate count across all nodes.
|
|
11140
11647
|
* @remarks Time O(1), Space O(1)
|
|
@@ -11174,7 +11681,7 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
11174
11681
|
*/
|
|
11175
11682
|
add(keyNodeOrEntry, value, count = 1) {
|
|
11176
11683
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
11177
|
-
const orgCount =
|
|
11684
|
+
const orgCount = newNode?.count || 0;
|
|
11178
11685
|
const isSuccessAdded = super.add(newNode, newValue);
|
|
11179
11686
|
if (isSuccessAdded) {
|
|
11180
11687
|
this._count += orgCount;
|
|
@@ -11328,7 +11835,7 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
11328
11835
|
const out = this._createLike([], options);
|
|
11329
11836
|
let index = 0;
|
|
11330
11837
|
for (const [key, value] of this) {
|
|
11331
|
-
out.add(callback.call(thisArg,
|
|
11838
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
11332
11839
|
}
|
|
11333
11840
|
return out;
|
|
11334
11841
|
}
|
|
@@ -11354,7 +11861,7 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
11354
11861
|
*/
|
|
11355
11862
|
_createInstance(options) {
|
|
11356
11863
|
const Ctor = this.constructor;
|
|
11357
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
11864
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
11358
11865
|
}
|
|
11359
11866
|
/**
|
|
11360
11867
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
@@ -11370,7 +11877,7 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
11370
11877
|
const Ctor = this.constructor;
|
|
11371
11878
|
return new Ctor(iter, {
|
|
11372
11879
|
...this._snapshotOptions(),
|
|
11373
|
-
...options
|
|
11880
|
+
...options ?? {}
|
|
11374
11881
|
});
|
|
11375
11882
|
}
|
|
11376
11883
|
/**
|
|
@@ -11387,7 +11894,7 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
11387
11894
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11388
11895
|
const [key, entryValue] = keyNodeOrEntry;
|
|
11389
11896
|
if (key === void 0 || key === null) return [void 0, void 0];
|
|
11390
|
-
const finalValue = value
|
|
11897
|
+
const finalValue = value ?? entryValue;
|
|
11391
11898
|
return [this.createNode(key, finalValue, "BLACK", count), finalValue];
|
|
11392
11899
|
}
|
|
11393
11900
|
return [this.createNode(keyNodeOrEntry, value, "BLACK", count), value];
|
|
@@ -11432,11 +11939,15 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
11432
11939
|
return super._replaceNode(oldNode, newNode);
|
|
11433
11940
|
}
|
|
11434
11941
|
};
|
|
11435
|
-
__name(_TreeCounter, "TreeCounter");
|
|
11436
|
-
var TreeCounter = _TreeCounter;
|
|
11437
11942
|
|
|
11438
11943
|
// src/data-structures/binary-tree/avl-tree-counter.ts
|
|
11439
|
-
var
|
|
11944
|
+
var AVLTreeCounterNode = class {
|
|
11945
|
+
static {
|
|
11946
|
+
__name(this, "AVLTreeCounterNode");
|
|
11947
|
+
}
|
|
11948
|
+
key;
|
|
11949
|
+
value;
|
|
11950
|
+
parent = void 0;
|
|
11440
11951
|
/**
|
|
11441
11952
|
* Create an AVL counter node.
|
|
11442
11953
|
* @remarks Time O(1), Space O(1)
|
|
@@ -11446,12 +11957,11 @@ var _AVLTreeCounterNode = class _AVLTreeCounterNode extends AVLTreeNode {
|
|
|
11446
11957
|
* @returns New AVLTreeCounterNode instance.
|
|
11447
11958
|
*/
|
|
11448
11959
|
constructor(key, value, count = 1) {
|
|
11449
|
-
|
|
11450
|
-
|
|
11451
|
-
__publicField(this, "_left");
|
|
11452
|
-
__publicField(this, "_right");
|
|
11960
|
+
this.key = key;
|
|
11961
|
+
this.value = value;
|
|
11453
11962
|
this.count = count;
|
|
11454
11963
|
}
|
|
11964
|
+
_left = void 0;
|
|
11455
11965
|
/**
|
|
11456
11966
|
* Get the left child pointer.
|
|
11457
11967
|
* @remarks Time O(1), Space O(1)
|
|
@@ -11472,6 +11982,7 @@ var _AVLTreeCounterNode = class _AVLTreeCounterNode extends AVLTreeNode {
|
|
|
11472
11982
|
}
|
|
11473
11983
|
this._left = v;
|
|
11474
11984
|
}
|
|
11985
|
+
_right = void 0;
|
|
11475
11986
|
/**
|
|
11476
11987
|
* Get the right child pointer.
|
|
11477
11988
|
* @remarks Time O(1), Space O(1)
|
|
@@ -11492,10 +12003,85 @@ var _AVLTreeCounterNode = class _AVLTreeCounterNode extends AVLTreeNode {
|
|
|
11492
12003
|
}
|
|
11493
12004
|
this._right = v;
|
|
11494
12005
|
}
|
|
12006
|
+
_height = 0;
|
|
12007
|
+
/**
|
|
12008
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
12009
|
+
* @remarks Time O(1), Space O(1)
|
|
12010
|
+
*
|
|
12011
|
+
* @returns The height.
|
|
12012
|
+
*/
|
|
12013
|
+
get height() {
|
|
12014
|
+
return this._height;
|
|
12015
|
+
}
|
|
12016
|
+
/**
|
|
12017
|
+
* Sets the height of the node.
|
|
12018
|
+
* @remarks Time O(1), Space O(1)
|
|
12019
|
+
*
|
|
12020
|
+
* @param value - The new height.
|
|
12021
|
+
*/
|
|
12022
|
+
set height(value) {
|
|
12023
|
+
this._height = value;
|
|
12024
|
+
}
|
|
12025
|
+
_color = "BLACK";
|
|
12026
|
+
/**
|
|
12027
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
12028
|
+
* @remarks Time O(1), Space O(1)
|
|
12029
|
+
*
|
|
12030
|
+
* @returns The node's color.
|
|
12031
|
+
*/
|
|
12032
|
+
get color() {
|
|
12033
|
+
return this._color;
|
|
12034
|
+
}
|
|
12035
|
+
/**
|
|
12036
|
+
* Sets the color of the node.
|
|
12037
|
+
* @remarks Time O(1), Space O(1)
|
|
12038
|
+
*
|
|
12039
|
+
* @param value - The new color.
|
|
12040
|
+
*/
|
|
12041
|
+
set color(value) {
|
|
12042
|
+
this._color = value;
|
|
12043
|
+
}
|
|
12044
|
+
_count = 1;
|
|
12045
|
+
/**
|
|
12046
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
12047
|
+
* @remarks Time O(1), Space O(1)
|
|
12048
|
+
*
|
|
12049
|
+
* @returns The subtree node count.
|
|
12050
|
+
*/
|
|
12051
|
+
get count() {
|
|
12052
|
+
return this._count;
|
|
12053
|
+
}
|
|
12054
|
+
/**
|
|
12055
|
+
* Sets the count of nodes in the subtree.
|
|
12056
|
+
* @remarks Time O(1), Space O(1)
|
|
12057
|
+
*
|
|
12058
|
+
* @param value - The new count.
|
|
12059
|
+
*/
|
|
12060
|
+
set count(value) {
|
|
12061
|
+
this._count = value;
|
|
12062
|
+
}
|
|
12063
|
+
/**
|
|
12064
|
+
* Gets the position of the node relative to its parent.
|
|
12065
|
+
* @remarks Time O(1), Space O(1)
|
|
12066
|
+
*
|
|
12067
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
12068
|
+
*/
|
|
12069
|
+
get familyPosition() {
|
|
12070
|
+
if (!this.parent) {
|
|
12071
|
+
return this.left || this.right ? "ROOT" : "ISOLATED";
|
|
12072
|
+
}
|
|
12073
|
+
if (this.parent.left === this) {
|
|
12074
|
+
return this.left || this.right ? "ROOT_LEFT" : "LEFT";
|
|
12075
|
+
} else if (this.parent.right === this) {
|
|
12076
|
+
return this.left || this.right ? "ROOT_RIGHT" : "RIGHT";
|
|
12077
|
+
}
|
|
12078
|
+
return "MAL_NODE";
|
|
12079
|
+
}
|
|
11495
12080
|
};
|
|
11496
|
-
|
|
11497
|
-
|
|
11498
|
-
|
|
12081
|
+
var AVLTreeCounter = class extends AVLTree {
|
|
12082
|
+
static {
|
|
12083
|
+
__name(this, "AVLTreeCounter");
|
|
12084
|
+
}
|
|
11499
12085
|
/**
|
|
11500
12086
|
* Create a AVLTreeCounter instance
|
|
11501
12087
|
* @remarks Time O(n), Space O(n)
|
|
@@ -11505,9 +12091,9 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11505
12091
|
*/
|
|
11506
12092
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11507
12093
|
super([], options);
|
|
11508
|
-
__publicField(this, "_count", 0);
|
|
11509
12094
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
11510
12095
|
}
|
|
12096
|
+
_count = 0;
|
|
11511
12097
|
get count() {
|
|
11512
12098
|
return this._count;
|
|
11513
12099
|
}
|
|
@@ -11543,7 +12129,7 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11543
12129
|
add(keyNodeOrEntry, value, count = 1) {
|
|
11544
12130
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
11545
12131
|
if (newNode === void 0) return false;
|
|
11546
|
-
const orgNodeCount =
|
|
12132
|
+
const orgNodeCount = newNode?.count || 0;
|
|
11547
12133
|
const inserted = super.add(newNode, newValue);
|
|
11548
12134
|
if (inserted) {
|
|
11549
12135
|
this._count += orgNodeCount;
|
|
@@ -11558,12 +12144,11 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11558
12144
|
* @returns Array of deletion results including deleted node and a rebalance hint when present.
|
|
11559
12145
|
*/
|
|
11560
12146
|
delete(keyNodeOrEntry, ignoreCount = false) {
|
|
11561
|
-
var _a;
|
|
11562
12147
|
const deletedResult = [];
|
|
11563
12148
|
if (!this.root) return deletedResult;
|
|
11564
|
-
const curr =
|
|
12149
|
+
const curr = this.getNode(keyNodeOrEntry) ?? void 0;
|
|
11565
12150
|
if (!curr) return deletedResult;
|
|
11566
|
-
const parent =
|
|
12151
|
+
const parent = curr?.parent ? curr.parent : void 0;
|
|
11567
12152
|
let needBalanced = void 0, orgCurrent = curr;
|
|
11568
12153
|
if (curr.count > 1 && !ignoreCount) {
|
|
11569
12154
|
curr.count--;
|
|
@@ -11675,7 +12260,7 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11675
12260
|
const out = this._createLike([], options);
|
|
11676
12261
|
let index = 0;
|
|
11677
12262
|
for (const [key, value] of this) {
|
|
11678
|
-
out.add(callback.call(thisArg,
|
|
12263
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
11679
12264
|
}
|
|
11680
12265
|
return out;
|
|
11681
12266
|
}
|
|
@@ -11690,7 +12275,7 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11690
12275
|
*/
|
|
11691
12276
|
_createInstance(options) {
|
|
11692
12277
|
const Ctor = this.constructor;
|
|
11693
|
-
return new Ctor([], { ...this._snapshotOptions(), ...options
|
|
12278
|
+
return new Ctor([], { ...this._snapshotOptions(), ...options ?? {} });
|
|
11694
12279
|
}
|
|
11695
12280
|
/**
|
|
11696
12281
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
@@ -11704,7 +12289,7 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11704
12289
|
*/
|
|
11705
12290
|
_createLike(iter = [], options) {
|
|
11706
12291
|
const Ctor = this.constructor;
|
|
11707
|
-
return new Ctor(iter, { ...this._snapshotOptions(), ...options
|
|
12292
|
+
return new Ctor(iter, { ...this._snapshotOptions(), ...options ?? {} });
|
|
11708
12293
|
}
|
|
11709
12294
|
/**
|
|
11710
12295
|
* (Protected) Normalize input into a node plus its effective value and count.
|
|
@@ -11720,7 +12305,7 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11720
12305
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11721
12306
|
const [key, entryValue] = keyNodeOrEntry;
|
|
11722
12307
|
if (key === void 0 || key === null) return [void 0, void 0];
|
|
11723
|
-
const finalValue = value
|
|
12308
|
+
const finalValue = value ?? entryValue;
|
|
11724
12309
|
return [this.createNode(key, finalValue, count), finalValue];
|
|
11725
12310
|
}
|
|
11726
12311
|
return [this.createNode(keyNodeOrEntry, value, count), value];
|
|
@@ -11765,20 +12350,22 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
11765
12350
|
return super._replaceNode(oldNode, newNode);
|
|
11766
12351
|
}
|
|
11767
12352
|
};
|
|
11768
|
-
__name(_AVLTreeCounter, "AVLTreeCounter");
|
|
11769
|
-
var AVLTreeCounter = _AVLTreeCounter;
|
|
11770
12353
|
|
|
11771
12354
|
// src/data-structures/priority-queue/priority-queue.ts
|
|
11772
|
-
var
|
|
12355
|
+
var PriorityQueue = class extends Heap {
|
|
12356
|
+
static {
|
|
12357
|
+
__name(this, "PriorityQueue");
|
|
12358
|
+
}
|
|
11773
12359
|
constructor(elements = [], options) {
|
|
11774
12360
|
super(elements, options);
|
|
11775
12361
|
}
|
|
11776
12362
|
};
|
|
11777
|
-
__name(_PriorityQueue, "PriorityQueue");
|
|
11778
|
-
var PriorityQueue = _PriorityQueue;
|
|
11779
12363
|
|
|
11780
12364
|
// src/data-structures/priority-queue/min-priority-queue.ts
|
|
11781
|
-
var
|
|
12365
|
+
var MinPriorityQueue = class extends PriorityQueue {
|
|
12366
|
+
static {
|
|
12367
|
+
__name(this, "MinPriorityQueue");
|
|
12368
|
+
}
|
|
11782
12369
|
/**
|
|
11783
12370
|
* Creates a min-priority queue.
|
|
11784
12371
|
* @param elements Optional initial elements to insert.
|
|
@@ -11789,11 +12376,12 @@ var _MinPriorityQueue = class _MinPriorityQueue extends PriorityQueue {
|
|
|
11789
12376
|
super(elements, options);
|
|
11790
12377
|
}
|
|
11791
12378
|
};
|
|
11792
|
-
__name(_MinPriorityQueue, "MinPriorityQueue");
|
|
11793
|
-
var MinPriorityQueue = _MinPriorityQueue;
|
|
11794
12379
|
|
|
11795
12380
|
// src/data-structures/priority-queue/max-priority-queue.ts
|
|
11796
|
-
var
|
|
12381
|
+
var MaxPriorityQueue = class extends PriorityQueue {
|
|
12382
|
+
static {
|
|
12383
|
+
__name(this, "MaxPriorityQueue");
|
|
12384
|
+
}
|
|
11797
12385
|
/**
|
|
11798
12386
|
* Creates a max-priority queue.
|
|
11799
12387
|
* @param elements Optional initial elements to insert.
|
|
@@ -11817,11 +12405,12 @@ var _MaxPriorityQueue = class _MaxPriorityQueue extends PriorityQueue {
|
|
|
11817
12405
|
});
|
|
11818
12406
|
}
|
|
11819
12407
|
};
|
|
11820
|
-
__name(_MaxPriorityQueue, "MaxPriorityQueue");
|
|
11821
|
-
var MaxPriorityQueue = _MaxPriorityQueue;
|
|
11822
12408
|
|
|
11823
12409
|
// src/data-structures/matrix/matrix.ts
|
|
11824
|
-
var
|
|
12410
|
+
var Matrix = class _Matrix {
|
|
12411
|
+
static {
|
|
12412
|
+
__name(this, "Matrix");
|
|
12413
|
+
}
|
|
11825
12414
|
/**
|
|
11826
12415
|
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
11827
12416
|
* default values if no options are provided.
|
|
@@ -11830,22 +12419,18 @@ var _Matrix = class _Matrix {
|
|
|
11830
12419
|
* properties:
|
|
11831
12420
|
*/
|
|
11832
12421
|
constructor(data, options) {
|
|
11833
|
-
__publicField(this, "_rows", 0);
|
|
11834
|
-
__publicField(this, "_cols", 0);
|
|
11835
|
-
__publicField(this, "_data");
|
|
11836
|
-
var _a, _b, _c;
|
|
11837
12422
|
if (options) {
|
|
11838
12423
|
const { rows, cols, addFn, subtractFn, multiplyFn } = options;
|
|
11839
12424
|
if (typeof rows === "number" && rows > 0) this._rows = rows;
|
|
11840
12425
|
else this._rows = data.length;
|
|
11841
12426
|
if (typeof cols === "number" && cols > 0) this._cols = cols;
|
|
11842
|
-
else this._cols =
|
|
12427
|
+
else this._cols = data[0]?.length || 0;
|
|
11843
12428
|
if (addFn) this._addFn = addFn;
|
|
11844
12429
|
if (subtractFn) this._subtractFn = subtractFn;
|
|
11845
12430
|
if (multiplyFn) this._multiplyFn = multiplyFn;
|
|
11846
12431
|
} else {
|
|
11847
12432
|
this._rows = data.length;
|
|
11848
|
-
this._cols =
|
|
12433
|
+
this._cols = data[0]?.length ?? 0;
|
|
11849
12434
|
}
|
|
11850
12435
|
if (data.length > 0) {
|
|
11851
12436
|
this._data = data;
|
|
@@ -11856,6 +12441,7 @@ var _Matrix = class _Matrix {
|
|
|
11856
12441
|
}
|
|
11857
12442
|
}
|
|
11858
12443
|
}
|
|
12444
|
+
_rows = 0;
|
|
11859
12445
|
/**
|
|
11860
12446
|
* The function returns the number of rows.
|
|
11861
12447
|
* @returns The number of rows.
|
|
@@ -11863,6 +12449,7 @@ var _Matrix = class _Matrix {
|
|
|
11863
12449
|
get rows() {
|
|
11864
12450
|
return this._rows;
|
|
11865
12451
|
}
|
|
12452
|
+
_cols = 0;
|
|
11866
12453
|
/**
|
|
11867
12454
|
* The function returns the value of the protected variable _cols.
|
|
11868
12455
|
* @returns The number of columns.
|
|
@@ -11870,6 +12457,7 @@ var _Matrix = class _Matrix {
|
|
|
11870
12457
|
get cols() {
|
|
11871
12458
|
return this._cols;
|
|
11872
12459
|
}
|
|
12460
|
+
_data;
|
|
11873
12461
|
/**
|
|
11874
12462
|
* The function returns a two-dimensional array of numbers.
|
|
11875
12463
|
* @returns The data property, which is a two-dimensional array of numbers.
|
|
@@ -12068,7 +12656,6 @@ var _Matrix = class _Matrix {
|
|
|
12068
12656
|
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
12069
12657
|
*/
|
|
12070
12658
|
inverse() {
|
|
12071
|
-
var _a;
|
|
12072
12659
|
if (this.rows !== this.cols) {
|
|
12073
12660
|
throw new Error("Matrix must be square for inversion.");
|
|
12074
12661
|
}
|
|
@@ -12095,7 +12682,7 @@ var _Matrix = class _Matrix {
|
|
|
12095
12682
|
throw new Error("Matrix is singular, and its inverse does not exist.");
|
|
12096
12683
|
}
|
|
12097
12684
|
augmentedMatrix._swapRows(i, pivotRow);
|
|
12098
|
-
const pivotElement =
|
|
12685
|
+
const pivotElement = augmentedMatrix.get(i, i) ?? 1;
|
|
12099
12686
|
if (pivotElement === 0) {
|
|
12100
12687
|
throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");
|
|
12101
12688
|
}
|
|
@@ -12238,11 +12825,14 @@ var _Matrix = class _Matrix {
|
|
|
12238
12825
|
}
|
|
12239
12826
|
}
|
|
12240
12827
|
};
|
|
12241
|
-
__name(_Matrix, "Matrix");
|
|
12242
|
-
var Matrix = _Matrix;
|
|
12243
12828
|
|
|
12244
12829
|
// src/data-structures/matrix/navigator.ts
|
|
12245
|
-
var
|
|
12830
|
+
var Character = class _Character {
|
|
12831
|
+
static {
|
|
12832
|
+
__name(this, "Character");
|
|
12833
|
+
}
|
|
12834
|
+
direction;
|
|
12835
|
+
turn;
|
|
12246
12836
|
/**
|
|
12247
12837
|
* The constructor function takes in a direction and turning object and sets the direction and turn properties of the
|
|
12248
12838
|
* Character class.
|
|
@@ -12252,26 +12842,25 @@ var _Character = class _Character {
|
|
|
12252
12842
|
* turning direction. It is used to determine the new direction when the character turns.
|
|
12253
12843
|
*/
|
|
12254
12844
|
constructor(direction, turning) {
|
|
12255
|
-
__publicField(this, "direction");
|
|
12256
|
-
__publicField(this, "turn");
|
|
12257
12845
|
this.direction = direction;
|
|
12258
12846
|
this.turn = () => new _Character(turning[direction], turning);
|
|
12259
12847
|
}
|
|
12260
12848
|
};
|
|
12261
|
-
|
|
12262
|
-
|
|
12263
|
-
|
|
12849
|
+
var Navigator = class {
|
|
12850
|
+
static {
|
|
12851
|
+
__name(this, "Navigator");
|
|
12852
|
+
}
|
|
12853
|
+
onMove;
|
|
12854
|
+
_matrix;
|
|
12855
|
+
_cur;
|
|
12856
|
+
_character;
|
|
12857
|
+
_VISITED;
|
|
12264
12858
|
/**
|
|
12265
12859
|
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
12266
12860
|
* in the matrix.
|
|
12267
12861
|
* @param - - `matrix`: a 2D array representing the grid or map
|
|
12268
12862
|
*/
|
|
12269
12863
|
constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }) {
|
|
12270
|
-
__publicField(this, "onMove");
|
|
12271
|
-
__publicField(this, "_matrix");
|
|
12272
|
-
__publicField(this, "_cur");
|
|
12273
|
-
__publicField(this, "_character");
|
|
12274
|
-
__publicField(this, "_VISITED");
|
|
12275
12864
|
this._matrix = matrix;
|
|
12276
12865
|
this._cur = cur;
|
|
12277
12866
|
this._character = new Character(charDir, turning);
|
|
@@ -12349,24 +12938,23 @@ var _Navigator = class _Navigator {
|
|
|
12349
12938
|
if (this.onMove) this.onMove(this._cur);
|
|
12350
12939
|
}
|
|
12351
12940
|
};
|
|
12352
|
-
__name(_Navigator, "Navigator");
|
|
12353
|
-
var Navigator = _Navigator;
|
|
12354
12941
|
|
|
12355
12942
|
// src/data-structures/trie/trie.ts
|
|
12356
|
-
var
|
|
12943
|
+
var TrieNode = class {
|
|
12944
|
+
static {
|
|
12945
|
+
__name(this, "TrieNode");
|
|
12946
|
+
}
|
|
12357
12947
|
/**
|
|
12358
12948
|
* Create a Trie node with a character key.
|
|
12359
12949
|
* @remarks Time O(1), Space O(1)
|
|
12360
12950
|
* @returns New TrieNode instance.
|
|
12361
12951
|
*/
|
|
12362
12952
|
constructor(key) {
|
|
12363
|
-
__publicField(this, "_key");
|
|
12364
|
-
__publicField(this, "_children");
|
|
12365
|
-
__publicField(this, "_isEnd");
|
|
12366
12953
|
this._key = key;
|
|
12367
12954
|
this._isEnd = false;
|
|
12368
12955
|
this._children = /* @__PURE__ */ new Map();
|
|
12369
12956
|
}
|
|
12957
|
+
_key;
|
|
12370
12958
|
/**
|
|
12371
12959
|
* Get the character key of this node.
|
|
12372
12960
|
* @remarks Time O(1), Space O(1)
|
|
@@ -12384,6 +12972,7 @@ var _TrieNode = class _TrieNode {
|
|
|
12384
12972
|
set key(value) {
|
|
12385
12973
|
this._key = value;
|
|
12386
12974
|
}
|
|
12975
|
+
_children;
|
|
12387
12976
|
/**
|
|
12388
12977
|
* Get the child map of this node.
|
|
12389
12978
|
* @remarks Time O(1), Space O(1)
|
|
@@ -12401,6 +12990,7 @@ var _TrieNode = class _TrieNode {
|
|
|
12401
12990
|
set children(value) {
|
|
12402
12991
|
this._children = value;
|
|
12403
12992
|
}
|
|
12993
|
+
_isEnd;
|
|
12404
12994
|
/**
|
|
12405
12995
|
* Check whether this node marks the end of a word.
|
|
12406
12996
|
* @remarks Time O(1), Space O(1)
|
|
@@ -12419,9 +13009,10 @@ var _TrieNode = class _TrieNode {
|
|
|
12419
13009
|
this._isEnd = value;
|
|
12420
13010
|
}
|
|
12421
13011
|
};
|
|
12422
|
-
|
|
12423
|
-
|
|
12424
|
-
|
|
13012
|
+
var Trie = class extends IterableElementBase {
|
|
13013
|
+
static {
|
|
13014
|
+
__name(this, "Trie");
|
|
13015
|
+
}
|
|
12425
13016
|
/**
|
|
12426
13017
|
* Create a Trie and optionally bulk-insert words.
|
|
12427
13018
|
* @remarks Time O(totalChars), Space O(totalChars)
|
|
@@ -12431,9 +13022,6 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
12431
13022
|
*/
|
|
12432
13023
|
constructor(words = [], options) {
|
|
12433
13024
|
super(options);
|
|
12434
|
-
__publicField(this, "_size", 0);
|
|
12435
|
-
__publicField(this, "_caseSensitive", true);
|
|
12436
|
-
__publicField(this, "_root", new TrieNode(""));
|
|
12437
13025
|
if (options) {
|
|
12438
13026
|
const { caseSensitive } = options;
|
|
12439
13027
|
if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
|
|
@@ -12442,6 +13030,7 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
12442
13030
|
this.addMany(words);
|
|
12443
13031
|
}
|
|
12444
13032
|
}
|
|
13033
|
+
_size = 0;
|
|
12445
13034
|
/**
|
|
12446
13035
|
* Get the number of stored words.
|
|
12447
13036
|
* @remarks Time O(1), Space O(1)
|
|
@@ -12450,6 +13039,7 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
12450
13039
|
get size() {
|
|
12451
13040
|
return this._size;
|
|
12452
13041
|
}
|
|
13042
|
+
_caseSensitive = true;
|
|
12453
13043
|
/**
|
|
12454
13044
|
* Get whether comparisons are case-sensitive.
|
|
12455
13045
|
* @remarks Time O(1), Space O(1)
|
|
@@ -12458,6 +13048,7 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
12458
13048
|
get caseSensitive() {
|
|
12459
13049
|
return this._caseSensitive;
|
|
12460
13050
|
}
|
|
13051
|
+
_root = new TrieNode("");
|
|
12461
13052
|
/**
|
|
12462
13053
|
* Get the root node.
|
|
12463
13054
|
* @remarks Time O(1), Space O(1)
|
|
@@ -12787,7 +13378,7 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
12787
13378
|
const next = new Ctor([], {
|
|
12788
13379
|
toElementFn: this.toElementFn,
|
|
12789
13380
|
caseSensitive: this.caseSensitive,
|
|
12790
|
-
...options
|
|
13381
|
+
...options ?? {}
|
|
12791
13382
|
});
|
|
12792
13383
|
return next;
|
|
12793
13384
|
}
|
|
@@ -12843,11 +13434,12 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
12843
13434
|
return str;
|
|
12844
13435
|
}
|
|
12845
13436
|
};
|
|
12846
|
-
__name(_Trie, "Trie");
|
|
12847
|
-
var Trie = _Trie;
|
|
12848
13437
|
|
|
12849
13438
|
// src/data-structures/tree/tree.ts
|
|
12850
|
-
var
|
|
13439
|
+
var TreeNode = class _TreeNode {
|
|
13440
|
+
static {
|
|
13441
|
+
__name(this, "TreeNode");
|
|
13442
|
+
}
|
|
12851
13443
|
/**
|
|
12852
13444
|
* The constructor function initializes a TreeNode object with a key, optional value, and optional
|
|
12853
13445
|
* children.
|
|
@@ -12859,13 +13451,11 @@ var _TreeNode = class _TreeNode {
|
|
|
12859
13451
|
* default value is an empty array.
|
|
12860
13452
|
*/
|
|
12861
13453
|
constructor(key, value, children) {
|
|
12862
|
-
__publicField(this, "_key");
|
|
12863
|
-
__publicField(this, "_value");
|
|
12864
|
-
__publicField(this, "_children");
|
|
12865
13454
|
this._key = key;
|
|
12866
13455
|
this._value = value || void 0;
|
|
12867
13456
|
if (children) this._children = children;
|
|
12868
13457
|
}
|
|
13458
|
+
_key;
|
|
12869
13459
|
/**
|
|
12870
13460
|
* The function returns the value of the protected variable _key.
|
|
12871
13461
|
* @returns The value of the `_key` property, which is a string.
|
|
@@ -12881,6 +13471,7 @@ var _TreeNode = class _TreeNode {
|
|
|
12881
13471
|
set key(value) {
|
|
12882
13472
|
this._key = value;
|
|
12883
13473
|
}
|
|
13474
|
+
_value;
|
|
12884
13475
|
/**
|
|
12885
13476
|
* The function returns the value stored in a variable, or undefined if the variable is empty.
|
|
12886
13477
|
* @returns The value of the variable `_value` is being returned.
|
|
@@ -12896,6 +13487,7 @@ var _TreeNode = class _TreeNode {
|
|
|
12896
13487
|
set value(value) {
|
|
12897
13488
|
this._value = value;
|
|
12898
13489
|
}
|
|
13490
|
+
_children;
|
|
12899
13491
|
/**
|
|
12900
13492
|
* The function returns an array of TreeNode objects or undefined.
|
|
12901
13493
|
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
|
|
@@ -12951,8 +13543,6 @@ var _TreeNode = class _TreeNode {
|
|
|
12951
13543
|
return maxDepth;
|
|
12952
13544
|
}
|
|
12953
13545
|
};
|
|
12954
|
-
__name(_TreeNode, "TreeNode");
|
|
12955
|
-
var TreeNode = _TreeNode;
|
|
12956
13546
|
/**
|
|
12957
13547
|
* data-structure-typed
|
|
12958
13548
|
*
|