queue-typed 2.4.4 → 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 +117 -38
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +116 -37
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +117 -39
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +116 -38
- 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/binary-tree/binary-tree.d.ts +10 -0
- 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 +1 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +14 -0
- package/dist/types/data-structures/queue/deque.d.ts +41 -1
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/queue-typed.js +114 -35
- 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 +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
- package/src/data-structures/binary-tree/binary-tree.ts +113 -42
- package/src/data-structures/binary-tree/bst.ts +11 -3
- 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 +5 -4
- package/src/data-structures/binary-tree/tree-set.ts +7 -6
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +4 -0
- package/src/data-structures/graph/undirected-graph.ts +95 -0
- package/src/data-structures/hash/hash-map.ts +13 -2
- package/src/data-structures/heap/heap.ts +4 -3
- package/src/data-structures/heap/max-heap.ts +2 -3
- 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 +71 -3
- package/src/data-structures/trie/trie.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -5,6 +5,54 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
6
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
7
|
|
|
8
|
+
// src/common/error.ts
|
|
9
|
+
var ERR = {
|
|
10
|
+
// Range / index
|
|
11
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
12
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
13
|
+
// Type / argument
|
|
14
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
15
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
16
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
17
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
18
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
19
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
20
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
21
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
22
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
23
|
+
// State / operation
|
|
24
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
25
|
+
// Matrix
|
|
26
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
27
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
28
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
29
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
30
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/common/index.ts
|
|
34
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
35
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
36
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
37
|
+
return DFSOperation2;
|
|
38
|
+
})(DFSOperation || {});
|
|
39
|
+
var _Range = class _Range {
|
|
40
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
41
|
+
this.low = low;
|
|
42
|
+
this.high = high;
|
|
43
|
+
this.includeLow = includeLow;
|
|
44
|
+
this.includeHigh = includeHigh;
|
|
45
|
+
}
|
|
46
|
+
// Determine whether a key is within the range
|
|
47
|
+
isInRange(key, comparator) {
|
|
48
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
49
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
50
|
+
return lowCheck && highCheck;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
__name(_Range, "Range");
|
|
54
|
+
var Range = _Range;
|
|
55
|
+
|
|
8
56
|
// src/data-structures/base/iterable-element-base.ts
|
|
9
57
|
var _IterableElementBase = class _IterableElementBase {
|
|
10
58
|
/**
|
|
@@ -27,7 +75,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
27
75
|
if (options) {
|
|
28
76
|
const { toElementFn } = options;
|
|
29
77
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
30
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
78
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
31
79
|
}
|
|
32
80
|
}
|
|
33
81
|
/**
|
|
@@ -183,7 +231,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
183
231
|
acc = initialValue;
|
|
184
232
|
} else {
|
|
185
233
|
const first = iter.next();
|
|
186
|
-
if (first.done) throw new TypeError(
|
|
234
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
187
235
|
acc = first.value;
|
|
188
236
|
index = 1;
|
|
189
237
|
}
|
|
@@ -1701,24 +1749,25 @@ __name(_LinkedListQueue, "LinkedListQueue");
|
|
|
1701
1749
|
var LinkedListQueue = _LinkedListQueue;
|
|
1702
1750
|
|
|
1703
1751
|
// src/utils/utils.ts
|
|
1704
|
-
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message
|
|
1705
|
-
if (index < min || index > max)
|
|
1752
|
+
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
1753
|
+
if (index < min || index > max) {
|
|
1754
|
+
throw new RangeError(message != null ? message : `Index ${index} is out of range [${min}, ${max}].`);
|
|
1755
|
+
}
|
|
1706
1756
|
}, "rangeCheck");
|
|
1707
1757
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
1708
1758
|
|
|
1709
1759
|
// src/data-structures/queue/deque.ts
|
|
1710
1760
|
var _Deque = class _Deque extends LinearBase {
|
|
1711
|
-
/**
|
|
1712
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
1713
|
-
* @remarks Time O(N), Space O(N)
|
|
1714
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
1715
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
1716
|
-
* @returns New Deque instance.
|
|
1717
|
-
*/
|
|
1718
1761
|
constructor(elements = [], options) {
|
|
1719
1762
|
super(options);
|
|
1720
1763
|
__publicField(this, "_equals", /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals"));
|
|
1721
1764
|
__publicField(this, "_bucketSize", 1 << 12);
|
|
1765
|
+
__publicField(this, "_autoCompactRatio", 0.5);
|
|
1766
|
+
/**
|
|
1767
|
+
* Counter for shift/pop operations since last compaction check.
|
|
1768
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
1769
|
+
*/
|
|
1770
|
+
__publicField(this, "_compactCounter", 0);
|
|
1722
1771
|
__publicField(this, "_bucketFirst", 0);
|
|
1723
1772
|
__publicField(this, "_firstInBucket", 0);
|
|
1724
1773
|
__publicField(this, "_bucketLast", 0);
|
|
@@ -1727,8 +1776,9 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1727
1776
|
__publicField(this, "_buckets", []);
|
|
1728
1777
|
__publicField(this, "_length", 0);
|
|
1729
1778
|
if (options) {
|
|
1730
|
-
const { bucketSize } = options;
|
|
1779
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
1731
1780
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
1781
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
1732
1782
|
}
|
|
1733
1783
|
let _size;
|
|
1734
1784
|
if ("length" in elements) {
|
|
@@ -1753,6 +1803,24 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1753
1803
|
get bucketSize() {
|
|
1754
1804
|
return this._bucketSize;
|
|
1755
1805
|
}
|
|
1806
|
+
/**
|
|
1807
|
+
* Get the auto-compaction ratio.
|
|
1808
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
1809
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
1810
|
+
* @remarks Time O(1), Space O(1)
|
|
1811
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
1812
|
+
*/
|
|
1813
|
+
get autoCompactRatio() {
|
|
1814
|
+
return this._autoCompactRatio;
|
|
1815
|
+
}
|
|
1816
|
+
/**
|
|
1817
|
+
* Set the auto-compaction ratio.
|
|
1818
|
+
* @remarks Time O(1), Space O(1)
|
|
1819
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
1820
|
+
*/
|
|
1821
|
+
set autoCompactRatio(value) {
|
|
1822
|
+
this._autoCompactRatio = value;
|
|
1823
|
+
}
|
|
1756
1824
|
/**
|
|
1757
1825
|
* Get the index of the first bucket in use.
|
|
1758
1826
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1884,6 +1952,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1884
1952
|
}
|
|
1885
1953
|
}
|
|
1886
1954
|
this._length -= 1;
|
|
1955
|
+
this._autoCompact();
|
|
1887
1956
|
return element;
|
|
1888
1957
|
}
|
|
1889
1958
|
/**
|
|
@@ -1906,6 +1975,7 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
1906
1975
|
}
|
|
1907
1976
|
}
|
|
1908
1977
|
this._length -= 1;
|
|
1978
|
+
this._autoCompact();
|
|
1909
1979
|
return element;
|
|
1910
1980
|
}
|
|
1911
1981
|
/**
|
|
@@ -2238,11 +2308,40 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2238
2308
|
* @remarks Time O(N), Space O(1)
|
|
2239
2309
|
* @returns void
|
|
2240
2310
|
*/
|
|
2311
|
+
/**
|
|
2312
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2313
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2314
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2315
|
+
*/
|
|
2316
|
+
_autoCompact() {
|
|
2317
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2318
|
+
this._compactCounter++;
|
|
2319
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2320
|
+
this._compactCounter = 0;
|
|
2321
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2322
|
+
if (utilization < this._autoCompactRatio) {
|
|
2323
|
+
this.shrinkToFit();
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
/**
|
|
2327
|
+
* Compact the deque by removing unused buckets.
|
|
2328
|
+
* @remarks Time O(N), Space O(1)
|
|
2329
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
2330
|
+
*/
|
|
2331
|
+
/**
|
|
2332
|
+
* Compact the deque by removing unused buckets.
|
|
2333
|
+
* @remarks Time O(N), Space O(1)
|
|
2334
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
2335
|
+
*/
|
|
2336
|
+
compact() {
|
|
2337
|
+
const before = this._bucketCount;
|
|
2338
|
+
this.shrinkToFit();
|
|
2339
|
+
return this._bucketCount < before;
|
|
2340
|
+
}
|
|
2241
2341
|
shrinkToFit() {
|
|
2242
2342
|
if (this._length === 0) return;
|
|
2243
2343
|
const newBuckets = [];
|
|
2244
|
-
if (this._bucketFirst
|
|
2245
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
2344
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
2246
2345
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
2247
2346
|
newBuckets.push(this._buckets[i]);
|
|
2248
2347
|
}
|
|
@@ -2257,6 +2356,8 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2257
2356
|
this._bucketFirst = 0;
|
|
2258
2357
|
this._bucketLast = newBuckets.length - 1;
|
|
2259
2358
|
this._buckets = newBuckets;
|
|
2359
|
+
this._bucketCount = newBuckets.length;
|
|
2360
|
+
this._compactCounter = 0;
|
|
2260
2361
|
}
|
|
2261
2362
|
/**
|
|
2262
2363
|
* Deep clone this deque, preserving options.
|
|
@@ -2438,29 +2539,6 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
2438
2539
|
};
|
|
2439
2540
|
__name(_Deque, "Deque");
|
|
2440
2541
|
var Deque = _Deque;
|
|
2441
|
-
|
|
2442
|
-
// src/common/index.ts
|
|
2443
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
2444
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
2445
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2446
|
-
return DFSOperation2;
|
|
2447
|
-
})(DFSOperation || {});
|
|
2448
|
-
var _Range = class _Range {
|
|
2449
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2450
|
-
this.low = low;
|
|
2451
|
-
this.high = high;
|
|
2452
|
-
this.includeLow = includeLow;
|
|
2453
|
-
this.includeHigh = includeHigh;
|
|
2454
|
-
}
|
|
2455
|
-
// Determine whether a key is within the range
|
|
2456
|
-
isInRange(key, comparator) {
|
|
2457
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
2458
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
2459
|
-
return lowCheck && highCheck;
|
|
2460
|
-
}
|
|
2461
|
-
};
|
|
2462
|
-
__name(_Range, "Range");
|
|
2463
|
-
var Range = _Range;
|
|
2464
2542
|
/**
|
|
2465
2543
|
* data-structure-typed
|
|
2466
2544
|
*
|
|
@@ -2471,6 +2549,7 @@ var Range = _Range;
|
|
|
2471
2549
|
|
|
2472
2550
|
exports.DFSOperation = DFSOperation;
|
|
2473
2551
|
exports.Deque = Deque;
|
|
2552
|
+
exports.ERR = ERR;
|
|
2474
2553
|
exports.LinkedListQueue = LinkedListQueue;
|
|
2475
2554
|
exports.Queue = Queue;
|
|
2476
2555
|
exports.Range = Range;
|