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
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,6 +3,55 @@
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
|
|
6
|
+
// src/common/error.ts
|
|
7
|
+
var ERR = {
|
|
8
|
+
// Range / index
|
|
9
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
10
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
11
|
+
// Type / argument
|
|
12
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
13
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
14
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
15
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
16
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
17
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
18
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
19
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
20
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
21
|
+
// State / operation
|
|
22
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
23
|
+
// Matrix
|
|
24
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
25
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
26
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
27
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
28
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// src/common/index.ts
|
|
32
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
33
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
34
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
35
|
+
return DFSOperation2;
|
|
36
|
+
})(DFSOperation || {});
|
|
37
|
+
var Range = class {
|
|
38
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
39
|
+
this.low = low;
|
|
40
|
+
this.high = high;
|
|
41
|
+
this.includeLow = includeLow;
|
|
42
|
+
this.includeHigh = includeHigh;
|
|
43
|
+
}
|
|
44
|
+
static {
|
|
45
|
+
__name(this, "Range");
|
|
46
|
+
}
|
|
47
|
+
// Determine whether a key is within the range
|
|
48
|
+
isInRange(key, comparator) {
|
|
49
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
50
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
51
|
+
return lowCheck && highCheck;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
6
55
|
// src/data-structures/base/iterable-element-base.ts
|
|
7
56
|
var IterableElementBase = class {
|
|
8
57
|
static {
|
|
@@ -21,7 +70,7 @@ var IterableElementBase = class {
|
|
|
21
70
|
if (options) {
|
|
22
71
|
const { toElementFn } = options;
|
|
23
72
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
24
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
73
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
25
74
|
}
|
|
26
75
|
}
|
|
27
76
|
/**
|
|
@@ -184,7 +233,7 @@ var IterableElementBase = class {
|
|
|
184
233
|
acc = initialValue;
|
|
185
234
|
} else {
|
|
186
235
|
const first = iter.next();
|
|
187
|
-
if (first.done) throw new TypeError(
|
|
236
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
188
237
|
acc = first.value;
|
|
189
238
|
index = 1;
|
|
190
239
|
}
|
|
@@ -1702,8 +1751,10 @@ var LinkedListQueue = class extends SinglyLinkedList {
|
|
|
1702
1751
|
};
|
|
1703
1752
|
|
|
1704
1753
|
// src/utils/utils.ts
|
|
1705
|
-
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message
|
|
1706
|
-
if (index < min || index > max)
|
|
1754
|
+
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message) => {
|
|
1755
|
+
if (index < min || index > max) {
|
|
1756
|
+
throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
|
|
1757
|
+
}
|
|
1707
1758
|
}, "rangeCheck");
|
|
1708
1759
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
1709
1760
|
|
|
@@ -1713,18 +1764,12 @@ var Deque = class extends LinearBase {
|
|
|
1713
1764
|
__name(this, "Deque");
|
|
1714
1765
|
}
|
|
1715
1766
|
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
1716
|
-
/**
|
|
1717
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
1718
|
-
* @remarks Time O(N), Space O(N)
|
|
1719
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
1720
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
1721
|
-
* @returns New Deque instance.
|
|
1722
|
-
*/
|
|
1723
1767
|
constructor(elements = [], options) {
|
|
1724
1768
|
super(options);
|
|
1725
1769
|
if (options) {
|
|
1726
|
-
const { bucketSize } = options;
|
|
1770
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
1727
1771
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
1772
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
1728
1773
|
}
|
|
1729
1774
|
let _size;
|
|
1730
1775
|
if ("length" in elements) {
|
|
@@ -1750,6 +1795,30 @@ var Deque = class extends LinearBase {
|
|
|
1750
1795
|
get bucketSize() {
|
|
1751
1796
|
return this._bucketSize;
|
|
1752
1797
|
}
|
|
1798
|
+
_autoCompactRatio = 0.5;
|
|
1799
|
+
/**
|
|
1800
|
+
* Get the auto-compaction ratio.
|
|
1801
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
1802
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
1803
|
+
* @remarks Time O(1), Space O(1)
|
|
1804
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
1805
|
+
*/
|
|
1806
|
+
get autoCompactRatio() {
|
|
1807
|
+
return this._autoCompactRatio;
|
|
1808
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* Set the auto-compaction ratio.
|
|
1811
|
+
* @remarks Time O(1), Space O(1)
|
|
1812
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
1813
|
+
*/
|
|
1814
|
+
set autoCompactRatio(value) {
|
|
1815
|
+
this._autoCompactRatio = value;
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Counter for shift/pop operations since last compaction check.
|
|
1819
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
1820
|
+
*/
|
|
1821
|
+
_compactCounter = 0;
|
|
1753
1822
|
_bucketFirst = 0;
|
|
1754
1823
|
/**
|
|
1755
1824
|
* Get the index of the first bucket in use.
|
|
@@ -1888,6 +1957,7 @@ var Deque = class extends LinearBase {
|
|
|
1888
1957
|
}
|
|
1889
1958
|
}
|
|
1890
1959
|
this._length -= 1;
|
|
1960
|
+
this._autoCompact();
|
|
1891
1961
|
return element;
|
|
1892
1962
|
}
|
|
1893
1963
|
/**
|
|
@@ -1910,6 +1980,7 @@ var Deque = class extends LinearBase {
|
|
|
1910
1980
|
}
|
|
1911
1981
|
}
|
|
1912
1982
|
this._length -= 1;
|
|
1983
|
+
this._autoCompact();
|
|
1913
1984
|
return element;
|
|
1914
1985
|
}
|
|
1915
1986
|
/**
|
|
@@ -2242,11 +2313,40 @@ var Deque = class extends LinearBase {
|
|
|
2242
2313
|
* @remarks Time O(N), Space O(1)
|
|
2243
2314
|
* @returns void
|
|
2244
2315
|
*/
|
|
2316
|
+
/**
|
|
2317
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2318
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2319
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2320
|
+
*/
|
|
2321
|
+
_autoCompact() {
|
|
2322
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2323
|
+
this._compactCounter++;
|
|
2324
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2325
|
+
this._compactCounter = 0;
|
|
2326
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2327
|
+
if (utilization < this._autoCompactRatio) {
|
|
2328
|
+
this.shrinkToFit();
|
|
2329
|
+
}
|
|
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
|
+
/**
|
|
2337
|
+
* Compact the deque by removing unused buckets.
|
|
2338
|
+
* @remarks Time O(N), Space O(1)
|
|
2339
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
2340
|
+
*/
|
|
2341
|
+
compact() {
|
|
2342
|
+
const before = this._bucketCount;
|
|
2343
|
+
this.shrinkToFit();
|
|
2344
|
+
return this._bucketCount < before;
|
|
2345
|
+
}
|
|
2245
2346
|
shrinkToFit() {
|
|
2246
2347
|
if (this._length === 0) return;
|
|
2247
2348
|
const newBuckets = [];
|
|
2248
|
-
if (this._bucketFirst
|
|
2249
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
2349
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
2250
2350
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
2251
2351
|
newBuckets.push(this._buckets[i]);
|
|
2252
2352
|
}
|
|
@@ -2261,6 +2361,8 @@ var Deque = class extends LinearBase {
|
|
|
2261
2361
|
this._bucketFirst = 0;
|
|
2262
2362
|
this._bucketLast = newBuckets.length - 1;
|
|
2263
2363
|
this._buckets = newBuckets;
|
|
2364
|
+
this._bucketCount = newBuckets.length;
|
|
2365
|
+
this._compactCounter = 0;
|
|
2264
2366
|
}
|
|
2265
2367
|
/**
|
|
2266
2368
|
* Deep clone this deque, preserving options.
|
|
@@ -2440,30 +2542,6 @@ var Deque = class extends LinearBase {
|
|
|
2440
2542
|
}
|
|
2441
2543
|
}
|
|
2442
2544
|
};
|
|
2443
|
-
|
|
2444
|
-
// src/common/index.ts
|
|
2445
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
2446
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
2447
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2448
|
-
return DFSOperation2;
|
|
2449
|
-
})(DFSOperation || {});
|
|
2450
|
-
var Range = class {
|
|
2451
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2452
|
-
this.low = low;
|
|
2453
|
-
this.high = high;
|
|
2454
|
-
this.includeLow = includeLow;
|
|
2455
|
-
this.includeHigh = includeHigh;
|
|
2456
|
-
}
|
|
2457
|
-
static {
|
|
2458
|
-
__name(this, "Range");
|
|
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
2545
|
/**
|
|
2468
2546
|
* data-structure-typed
|
|
2469
2547
|
*
|
|
@@ -2474,6 +2552,7 @@ var Range = class {
|
|
|
2474
2552
|
|
|
2475
2553
|
exports.DFSOperation = DFSOperation;
|
|
2476
2554
|
exports.Deque = Deque;
|
|
2555
|
+
exports.ERR = ERR;
|
|
2477
2556
|
exports.LinkedListQueue = LinkedListQueue;
|
|
2478
2557
|
exports.Queue = Queue;
|
|
2479
2558
|
exports.Range = Range;
|