deque-typed 2.4.3 → 2.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +118 -39
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +117 -38
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +118 -40
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +117 -39
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +15 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +3 -2
- package/dist/types/data-structures/graph/undirected-graph.d.ts +16 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +3 -7
- package/dist/types/data-structures/queue/deque.d.ts +41 -1
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
- package/dist/umd/deque-typed.js +115 -36
- package/dist/umd/deque-typed.js.map +1 -1
- package/dist/umd/deque-typed.min.js +1 -1
- package/dist/umd/deque-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +5 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
- package/src/data-structures/binary-tree/binary-tree.ts +121 -49
- package/src/data-structures/binary-tree/bst.ts +12 -4
- package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
- package/src/data-structures/binary-tree/tree-map.ts +8 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-set.ts +10 -9
- package/src/data-structures/binary-tree/tree-set.ts +7 -6
- package/src/data-structures/graph/abstract-graph.ts +124 -19
- package/src/data-structures/graph/directed-graph.ts +8 -4
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +99 -4
- package/src/data-structures/hash/hash-map.ts +19 -6
- package/src/data-structures/heap/heap.ts +21 -17
- package/src/data-structures/heap/max-heap.ts +2 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
- package/src/data-structures/matrix/matrix.ts +9 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
- package/src/data-structures/queue/deque.ts +72 -4
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +12 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/types/data-structures/stack/stack.ts +1 -1
- package/src/utils/utils.ts +4 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -4,11 +4,62 @@ var __defProp = Object.defineProperty;
|
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
|
|
6
6
|
// src/utils/utils.ts
|
|
7
|
-
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message
|
|
8
|
-
if (index < min || index > max)
|
|
7
|
+
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
8
|
+
if (index < min || index > max) {
|
|
9
|
+
throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
|
|
10
|
+
}
|
|
9
11
|
}, "rangeCheck");
|
|
10
12
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
11
13
|
|
|
14
|
+
// src/common/error.ts
|
|
15
|
+
var ERR = {
|
|
16
|
+
// Range / index
|
|
17
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
18
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
19
|
+
// Type / argument
|
|
20
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
21
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
22
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
23
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
24
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
25
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
26
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
27
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
28
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
29
|
+
// State / operation
|
|
30
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
31
|
+
// Matrix
|
|
32
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
33
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
34
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
35
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
36
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/common/index.ts
|
|
40
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
41
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
42
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
43
|
+
return DFSOperation2;
|
|
44
|
+
})(DFSOperation || {});
|
|
45
|
+
var Range = class {
|
|
46
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
47
|
+
this.low = low;
|
|
48
|
+
this.high = high;
|
|
49
|
+
this.includeLow = includeLow;
|
|
50
|
+
this.includeHigh = includeHigh;
|
|
51
|
+
}
|
|
52
|
+
static {
|
|
53
|
+
__name(this, "Range");
|
|
54
|
+
}
|
|
55
|
+
// Determine whether a key is within the range
|
|
56
|
+
isInRange(key, comparator) {
|
|
57
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
58
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
59
|
+
return lowCheck && highCheck;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
12
63
|
// src/data-structures/base/iterable-element-base.ts
|
|
13
64
|
var IterableElementBase = class {
|
|
14
65
|
static {
|
|
@@ -27,7 +78,7 @@ var IterableElementBase = class {
|
|
|
27
78
|
if (options) {
|
|
28
79
|
const { toElementFn } = options;
|
|
29
80
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
30
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
81
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
31
82
|
}
|
|
32
83
|
}
|
|
33
84
|
/**
|
|
@@ -190,7 +241,7 @@ var IterableElementBase = class {
|
|
|
190
241
|
acc = initialValue;
|
|
191
242
|
} else {
|
|
192
243
|
const first = iter.next();
|
|
193
|
-
if (first.done) throw new TypeError(
|
|
244
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
194
245
|
acc = first.value;
|
|
195
246
|
index = 1;
|
|
196
247
|
}
|
|
@@ -430,19 +481,13 @@ var Deque = class extends LinearBase {
|
|
|
430
481
|
static {
|
|
431
482
|
__name(this, "Deque");
|
|
432
483
|
}
|
|
433
|
-
_equals = Object.is;
|
|
434
|
-
/**
|
|
435
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
436
|
-
* @remarks Time O(N), Space O(N)
|
|
437
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
438
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
439
|
-
* @returns New Deque instance.
|
|
440
|
-
*/
|
|
484
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
441
485
|
constructor(elements = [], options) {
|
|
442
486
|
super(options);
|
|
443
487
|
if (options) {
|
|
444
|
-
const { bucketSize } = options;
|
|
488
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
445
489
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
490
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
446
491
|
}
|
|
447
492
|
let _size;
|
|
448
493
|
if ("length" in elements) {
|
|
@@ -468,6 +513,30 @@ var Deque = class extends LinearBase {
|
|
|
468
513
|
get bucketSize() {
|
|
469
514
|
return this._bucketSize;
|
|
470
515
|
}
|
|
516
|
+
_autoCompactRatio = 0.5;
|
|
517
|
+
/**
|
|
518
|
+
* Get the auto-compaction ratio.
|
|
519
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
520
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
521
|
+
* @remarks Time O(1), Space O(1)
|
|
522
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
523
|
+
*/
|
|
524
|
+
get autoCompactRatio() {
|
|
525
|
+
return this._autoCompactRatio;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Set the auto-compaction ratio.
|
|
529
|
+
* @remarks Time O(1), Space O(1)
|
|
530
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
531
|
+
*/
|
|
532
|
+
set autoCompactRatio(value) {
|
|
533
|
+
this._autoCompactRatio = value;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Counter for shift/pop operations since last compaction check.
|
|
537
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
538
|
+
*/
|
|
539
|
+
_compactCounter = 0;
|
|
471
540
|
_bucketFirst = 0;
|
|
472
541
|
/**
|
|
473
542
|
* Get the index of the first bucket in use.
|
|
@@ -606,6 +675,7 @@ var Deque = class extends LinearBase {
|
|
|
606
675
|
}
|
|
607
676
|
}
|
|
608
677
|
this._length -= 1;
|
|
678
|
+
this._autoCompact();
|
|
609
679
|
return element;
|
|
610
680
|
}
|
|
611
681
|
/**
|
|
@@ -628,6 +698,7 @@ var Deque = class extends LinearBase {
|
|
|
628
698
|
}
|
|
629
699
|
}
|
|
630
700
|
this._length -= 1;
|
|
701
|
+
this._autoCompact();
|
|
631
702
|
return element;
|
|
632
703
|
}
|
|
633
704
|
/**
|
|
@@ -960,11 +1031,40 @@ var Deque = class extends LinearBase {
|
|
|
960
1031
|
* @remarks Time O(N), Space O(1)
|
|
961
1032
|
* @returns void
|
|
962
1033
|
*/
|
|
1034
|
+
/**
|
|
1035
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
1036
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
1037
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
1038
|
+
*/
|
|
1039
|
+
_autoCompact() {
|
|
1040
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
1041
|
+
this._compactCounter++;
|
|
1042
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
1043
|
+
this._compactCounter = 0;
|
|
1044
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
1045
|
+
if (utilization < this._autoCompactRatio) {
|
|
1046
|
+
this.shrinkToFit();
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Compact the deque by removing unused buckets.
|
|
1051
|
+
* @remarks Time O(N), Space O(1)
|
|
1052
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1053
|
+
*/
|
|
1054
|
+
/**
|
|
1055
|
+
* Compact the deque by removing unused buckets.
|
|
1056
|
+
* @remarks Time O(N), Space O(1)
|
|
1057
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1058
|
+
*/
|
|
1059
|
+
compact() {
|
|
1060
|
+
const before = this._bucketCount;
|
|
1061
|
+
this.shrinkToFit();
|
|
1062
|
+
return this._bucketCount < before;
|
|
1063
|
+
}
|
|
963
1064
|
shrinkToFit() {
|
|
964
1065
|
if (this._length === 0) return;
|
|
965
1066
|
const newBuckets = [];
|
|
966
|
-
if (this._bucketFirst
|
|
967
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
1067
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
968
1068
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
969
1069
|
newBuckets.push(this._buckets[i]);
|
|
970
1070
|
}
|
|
@@ -979,6 +1079,8 @@ var Deque = class extends LinearBase {
|
|
|
979
1079
|
this._bucketFirst = 0;
|
|
980
1080
|
this._bucketLast = newBuckets.length - 1;
|
|
981
1081
|
this._buckets = newBuckets;
|
|
1082
|
+
this._bucketCount = newBuckets.length;
|
|
1083
|
+
this._compactCounter = 0;
|
|
982
1084
|
}
|
|
983
1085
|
/**
|
|
984
1086
|
* Deep clone this deque, preserving options.
|
|
@@ -1158,30 +1260,6 @@ var Deque = class extends LinearBase {
|
|
|
1158
1260
|
}
|
|
1159
1261
|
}
|
|
1160
1262
|
};
|
|
1161
|
-
|
|
1162
|
-
// src/common/index.ts
|
|
1163
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1164
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1165
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1166
|
-
return DFSOperation2;
|
|
1167
|
-
})(DFSOperation || {});
|
|
1168
|
-
var Range = class {
|
|
1169
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1170
|
-
this.low = low;
|
|
1171
|
-
this.high = high;
|
|
1172
|
-
this.includeLow = includeLow;
|
|
1173
|
-
this.includeHigh = includeHigh;
|
|
1174
|
-
}
|
|
1175
|
-
static {
|
|
1176
|
-
__name(this, "Range");
|
|
1177
|
-
}
|
|
1178
|
-
// Determine whether a key is within the range
|
|
1179
|
-
isInRange(key, comparator) {
|
|
1180
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1181
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1182
|
-
return lowCheck && highCheck;
|
|
1183
|
-
}
|
|
1184
|
-
};
|
|
1185
1263
|
/**
|
|
1186
1264
|
* data-structure-typed
|
|
1187
1265
|
*
|
|
@@ -1192,6 +1270,7 @@ var Range = class {
|
|
|
1192
1270
|
|
|
1193
1271
|
exports.DFSOperation = DFSOperation;
|
|
1194
1272
|
exports.Deque = Deque;
|
|
1273
|
+
exports.ERR = ERR;
|
|
1195
1274
|
exports.Range = Range;
|
|
1196
1275
|
//# sourceMappingURL=index.cjs.map
|
|
1197
1276
|
//# sourceMappingURL=index.cjs.map
|