queue-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 +124 -45
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +124 -44
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +124 -46
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +124 -45
- 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/queue-typed.js +122 -42
- package/dist/umd/queue-typed.js.map +1 -1
- package/dist/umd/queue-typed.min.js +1 -1
- package/dist/umd/queue-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/umd/queue-typed.js
CHANGED
|
@@ -25,11 +25,58 @@ var queueTyped = (() => {
|
|
|
25
25
|
__export(src_exports, {
|
|
26
26
|
DFSOperation: () => DFSOperation,
|
|
27
27
|
Deque: () => Deque,
|
|
28
|
+
ERR: () => ERR,
|
|
28
29
|
LinkedListQueue: () => LinkedListQueue,
|
|
29
30
|
Queue: () => Queue,
|
|
30
31
|
Range: () => Range
|
|
31
32
|
});
|
|
32
33
|
|
|
34
|
+
// src/common/error.ts
|
|
35
|
+
var ERR = {
|
|
36
|
+
// Range / index
|
|
37
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
38
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
39
|
+
// Type / argument
|
|
40
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
41
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
42
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
43
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
44
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
45
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
46
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
47
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
48
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
49
|
+
// State / operation
|
|
50
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
51
|
+
// Matrix
|
|
52
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
53
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
54
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
55
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
56
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/common/index.ts
|
|
60
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
61
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
62
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
63
|
+
return DFSOperation2;
|
|
64
|
+
})(DFSOperation || {});
|
|
65
|
+
var Range = class {
|
|
66
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
67
|
+
this.low = low;
|
|
68
|
+
this.high = high;
|
|
69
|
+
this.includeLow = includeLow;
|
|
70
|
+
this.includeHigh = includeHigh;
|
|
71
|
+
}
|
|
72
|
+
// Determine whether a key is within the range
|
|
73
|
+
isInRange(key, comparator) {
|
|
74
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
75
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
76
|
+
return lowCheck && highCheck;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
33
80
|
// src/data-structures/base/iterable-element-base.ts
|
|
34
81
|
var IterableElementBase = class {
|
|
35
82
|
/**
|
|
@@ -52,7 +99,7 @@ var queueTyped = (() => {
|
|
|
52
99
|
if (options) {
|
|
53
100
|
const { toElementFn } = options;
|
|
54
101
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
55
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
102
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
56
103
|
}
|
|
57
104
|
}
|
|
58
105
|
/**
|
|
@@ -208,7 +255,7 @@ var queueTyped = (() => {
|
|
|
208
255
|
acc = initialValue;
|
|
209
256
|
} else {
|
|
210
257
|
const first = iter.next();
|
|
211
|
-
if (first.done) throw new TypeError(
|
|
258
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
212
259
|
acc = first.value;
|
|
213
260
|
index = 1;
|
|
214
261
|
}
|
|
@@ -671,7 +718,7 @@ var queueTyped = (() => {
|
|
|
671
718
|
*/
|
|
672
719
|
constructor(elements = [], options) {
|
|
673
720
|
super(options);
|
|
674
|
-
__publicField(this, "_equals", Object.is);
|
|
721
|
+
__publicField(this, "_equals", (a, b) => Object.is(a, b));
|
|
675
722
|
__publicField(this, "_head");
|
|
676
723
|
__publicField(this, "_tail");
|
|
677
724
|
__publicField(this, "_length", 0);
|
|
@@ -759,6 +806,7 @@ var queueTyped = (() => {
|
|
|
759
806
|
* @returns Removed element or undefined.
|
|
760
807
|
*/
|
|
761
808
|
pop() {
|
|
809
|
+
var _a;
|
|
762
810
|
if (!this.head) return void 0;
|
|
763
811
|
if (this.head === this.tail) {
|
|
764
812
|
const value2 = this.head.value;
|
|
@@ -768,8 +816,8 @@ var queueTyped = (() => {
|
|
|
768
816
|
return value2;
|
|
769
817
|
}
|
|
770
818
|
let current = this.head;
|
|
771
|
-
while (current.next !== this.tail) current = current.next;
|
|
772
|
-
const value = this.tail.value;
|
|
819
|
+
while (current.next && current.next !== this.tail) current = current.next;
|
|
820
|
+
const value = (_a = this.tail) == null ? void 0 : _a.value;
|
|
773
821
|
current.next = void 0;
|
|
774
822
|
this._tail = current;
|
|
775
823
|
this._length--;
|
|
@@ -857,8 +905,8 @@ var queueTyped = (() => {
|
|
|
857
905
|
at(index) {
|
|
858
906
|
if (index < 0 || index >= this._length) return void 0;
|
|
859
907
|
let current = this.head;
|
|
860
|
-
for (let i = 0; i < index; i++) current = current.next;
|
|
861
|
-
return current.value;
|
|
908
|
+
for (let i = 0; i < index && current; i++) current = current.next;
|
|
909
|
+
return current == null ? void 0 : current.value;
|
|
862
910
|
}
|
|
863
911
|
/**
|
|
864
912
|
* Type guard: check whether the input is a SinglyLinkedListNode.
|
|
@@ -878,7 +926,7 @@ var queueTyped = (() => {
|
|
|
878
926
|
getNodeAt(index) {
|
|
879
927
|
if (index < 0 || index >= this._length) return void 0;
|
|
880
928
|
let current = this.head;
|
|
881
|
-
for (let i = 0; i < index; i++) current = current.next;
|
|
929
|
+
for (let i = 0; i < index && current; i++) current = current.next;
|
|
882
930
|
return current;
|
|
883
931
|
}
|
|
884
932
|
/**
|
|
@@ -1708,24 +1756,25 @@ var queueTyped = (() => {
|
|
|
1708
1756
|
};
|
|
1709
1757
|
|
|
1710
1758
|
// src/utils/utils.ts
|
|
1711
|
-
var rangeCheck = (index, min, max, message
|
|
1712
|
-
if (index < min || index > max)
|
|
1759
|
+
var rangeCheck = (index, min, max, message) => {
|
|
1760
|
+
if (index < min || index > max) {
|
|
1761
|
+
throw new RangeError(message != null ? message : `Index ${index} is out of range [${min}, ${max}].`);
|
|
1762
|
+
}
|
|
1713
1763
|
};
|
|
1714
1764
|
var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
1715
1765
|
|
|
1716
1766
|
// src/data-structures/queue/deque.ts
|
|
1717
1767
|
var Deque = class extends LinearBase {
|
|
1718
|
-
/**
|
|
1719
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
1720
|
-
* @remarks Time O(N), Space O(N)
|
|
1721
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
1722
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
1723
|
-
* @returns New Deque instance.
|
|
1724
|
-
*/
|
|
1725
1768
|
constructor(elements = [], options) {
|
|
1726
1769
|
super(options);
|
|
1727
|
-
__publicField(this, "_equals", Object.is);
|
|
1770
|
+
__publicField(this, "_equals", (a, b) => Object.is(a, b));
|
|
1728
1771
|
__publicField(this, "_bucketSize", 1 << 12);
|
|
1772
|
+
__publicField(this, "_autoCompactRatio", 0.5);
|
|
1773
|
+
/**
|
|
1774
|
+
* Counter for shift/pop operations since last compaction check.
|
|
1775
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
1776
|
+
*/
|
|
1777
|
+
__publicField(this, "_compactCounter", 0);
|
|
1729
1778
|
__publicField(this, "_bucketFirst", 0);
|
|
1730
1779
|
__publicField(this, "_firstInBucket", 0);
|
|
1731
1780
|
__publicField(this, "_bucketLast", 0);
|
|
@@ -1734,8 +1783,9 @@ var queueTyped = (() => {
|
|
|
1734
1783
|
__publicField(this, "_buckets", []);
|
|
1735
1784
|
__publicField(this, "_length", 0);
|
|
1736
1785
|
if (options) {
|
|
1737
|
-
const { bucketSize } = options;
|
|
1786
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
1738
1787
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
1788
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
1739
1789
|
}
|
|
1740
1790
|
let _size;
|
|
1741
1791
|
if ("length" in elements) {
|
|
@@ -1760,6 +1810,24 @@ var queueTyped = (() => {
|
|
|
1760
1810
|
get bucketSize() {
|
|
1761
1811
|
return this._bucketSize;
|
|
1762
1812
|
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Get the auto-compaction ratio.
|
|
1815
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
1816
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
1817
|
+
* @remarks Time O(1), Space O(1)
|
|
1818
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
1819
|
+
*/
|
|
1820
|
+
get autoCompactRatio() {
|
|
1821
|
+
return this._autoCompactRatio;
|
|
1822
|
+
}
|
|
1823
|
+
/**
|
|
1824
|
+
* Set the auto-compaction ratio.
|
|
1825
|
+
* @remarks Time O(1), Space O(1)
|
|
1826
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
1827
|
+
*/
|
|
1828
|
+
set autoCompactRatio(value) {
|
|
1829
|
+
this._autoCompactRatio = value;
|
|
1830
|
+
}
|
|
1763
1831
|
/**
|
|
1764
1832
|
* Get the index of the first bucket in use.
|
|
1765
1833
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1891,6 +1959,7 @@ var queueTyped = (() => {
|
|
|
1891
1959
|
}
|
|
1892
1960
|
}
|
|
1893
1961
|
this._length -= 1;
|
|
1962
|
+
this._autoCompact();
|
|
1894
1963
|
return element;
|
|
1895
1964
|
}
|
|
1896
1965
|
/**
|
|
@@ -1913,6 +1982,7 @@ var queueTyped = (() => {
|
|
|
1913
1982
|
}
|
|
1914
1983
|
}
|
|
1915
1984
|
this._length -= 1;
|
|
1985
|
+
this._autoCompact();
|
|
1916
1986
|
return element;
|
|
1917
1987
|
}
|
|
1918
1988
|
/**
|
|
@@ -2245,11 +2315,40 @@ var queueTyped = (() => {
|
|
|
2245
2315
|
* @remarks Time O(N), Space O(1)
|
|
2246
2316
|
* @returns void
|
|
2247
2317
|
*/
|
|
2318
|
+
/**
|
|
2319
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2320
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2321
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2322
|
+
*/
|
|
2323
|
+
_autoCompact() {
|
|
2324
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2325
|
+
this._compactCounter++;
|
|
2326
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2327
|
+
this._compactCounter = 0;
|
|
2328
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2329
|
+
if (utilization < this._autoCompactRatio) {
|
|
2330
|
+
this.shrinkToFit();
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
/**
|
|
2334
|
+
* Compact the deque by removing unused buckets.
|
|
2335
|
+
* @remarks Time O(N), Space O(1)
|
|
2336
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
2337
|
+
*/
|
|
2338
|
+
/**
|
|
2339
|
+
* Compact the deque by removing unused buckets.
|
|
2340
|
+
* @remarks Time O(N), Space O(1)
|
|
2341
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
2342
|
+
*/
|
|
2343
|
+
compact() {
|
|
2344
|
+
const before = this._bucketCount;
|
|
2345
|
+
this.shrinkToFit();
|
|
2346
|
+
return this._bucketCount < before;
|
|
2347
|
+
}
|
|
2248
2348
|
shrinkToFit() {
|
|
2249
2349
|
if (this._length === 0) return;
|
|
2250
2350
|
const newBuckets = [];
|
|
2251
|
-
if (this._bucketFirst
|
|
2252
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
2351
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
2253
2352
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
2254
2353
|
newBuckets.push(this._buckets[i]);
|
|
2255
2354
|
}
|
|
@@ -2264,6 +2363,8 @@ var queueTyped = (() => {
|
|
|
2264
2363
|
this._bucketFirst = 0;
|
|
2265
2364
|
this._bucketLast = newBuckets.length - 1;
|
|
2266
2365
|
this._buckets = newBuckets;
|
|
2366
|
+
this._bucketCount = newBuckets.length;
|
|
2367
|
+
this._compactCounter = 0;
|
|
2267
2368
|
}
|
|
2268
2369
|
/**
|
|
2269
2370
|
* Deep clone this deque, preserving options.
|
|
@@ -2443,27 +2544,6 @@ var queueTyped = (() => {
|
|
|
2443
2544
|
}
|
|
2444
2545
|
}
|
|
2445
2546
|
};
|
|
2446
|
-
|
|
2447
|
-
// src/common/index.ts
|
|
2448
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
2449
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
2450
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2451
|
-
return DFSOperation2;
|
|
2452
|
-
})(DFSOperation || {});
|
|
2453
|
-
var Range = class {
|
|
2454
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2455
|
-
this.low = low;
|
|
2456
|
-
this.high = high;
|
|
2457
|
-
this.includeLow = includeLow;
|
|
2458
|
-
this.includeHigh = includeHigh;
|
|
2459
|
-
}
|
|
2460
|
-
// Determine whether a key is within the range
|
|
2461
|
-
isInRange(key, comparator) {
|
|
2462
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
2463
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
2464
|
-
return lowCheck && highCheck;
|
|
2465
|
-
}
|
|
2466
|
-
};
|
|
2467
2547
|
return __toCommonJS(src_exports);
|
|
2468
2548
|
})();
|
|
2469
2549
|
/**
|