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/esm/index.mjs
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
+
// src/common/error.ts
|
|
5
|
+
var ERR = {
|
|
6
|
+
// Range / index
|
|
7
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
8
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
9
|
+
// Type / argument
|
|
10
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
11
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
12
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
13
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
14
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
15
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
16
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
17
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
18
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
19
|
+
// State / operation
|
|
20
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
21
|
+
// Matrix
|
|
22
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
23
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
24
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
25
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
26
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/common/index.ts
|
|
30
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
31
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
32
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
33
|
+
return DFSOperation2;
|
|
34
|
+
})(DFSOperation || {});
|
|
35
|
+
var Range = class {
|
|
36
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
37
|
+
this.low = low;
|
|
38
|
+
this.high = high;
|
|
39
|
+
this.includeLow = includeLow;
|
|
40
|
+
this.includeHigh = includeHigh;
|
|
41
|
+
}
|
|
42
|
+
static {
|
|
43
|
+
__name(this, "Range");
|
|
44
|
+
}
|
|
45
|
+
// Determine whether a key is within the range
|
|
46
|
+
isInRange(key, comparator) {
|
|
47
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
48
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
49
|
+
return lowCheck && highCheck;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
4
53
|
// src/data-structures/base/iterable-element-base.ts
|
|
5
54
|
var IterableElementBase = class {
|
|
6
55
|
static {
|
|
@@ -19,7 +68,7 @@ var IterableElementBase = class {
|
|
|
19
68
|
if (options) {
|
|
20
69
|
const { toElementFn } = options;
|
|
21
70
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
22
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
71
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
23
72
|
}
|
|
24
73
|
}
|
|
25
74
|
/**
|
|
@@ -182,7 +231,7 @@ var IterableElementBase = class {
|
|
|
182
231
|
acc = initialValue;
|
|
183
232
|
} else {
|
|
184
233
|
const first = iter.next();
|
|
185
|
-
if (first.done) throw new TypeError(
|
|
234
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
186
235
|
acc = first.value;
|
|
187
236
|
index = 1;
|
|
188
237
|
}
|
|
@@ -651,7 +700,7 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
651
700
|
static {
|
|
652
701
|
__name(this, "SinglyLinkedList");
|
|
653
702
|
}
|
|
654
|
-
_equals = Object.is;
|
|
703
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
655
704
|
/**
|
|
656
705
|
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
657
706
|
* @remarks Time O(N), Space O(N)
|
|
@@ -755,8 +804,8 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
755
804
|
return value2;
|
|
756
805
|
}
|
|
757
806
|
let current = this.head;
|
|
758
|
-
while (current.next !== this.tail) current = current.next;
|
|
759
|
-
const value = this.tail
|
|
807
|
+
while (current.next && current.next !== this.tail) current = current.next;
|
|
808
|
+
const value = this.tail?.value;
|
|
760
809
|
current.next = void 0;
|
|
761
810
|
this._tail = current;
|
|
762
811
|
this._length--;
|
|
@@ -844,8 +893,8 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
844
893
|
at(index) {
|
|
845
894
|
if (index < 0 || index >= this._length) return void 0;
|
|
846
895
|
let current = this.head;
|
|
847
|
-
for (let i = 0; i < index; i++) current = current.next;
|
|
848
|
-
return current
|
|
896
|
+
for (let i = 0; i < index && current; i++) current = current.next;
|
|
897
|
+
return current?.value;
|
|
849
898
|
}
|
|
850
899
|
/**
|
|
851
900
|
* Type guard: check whether the input is a SinglyLinkedListNode.
|
|
@@ -865,7 +914,7 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
865
914
|
getNodeAt(index) {
|
|
866
915
|
if (index < 0 || index >= this._length) return void 0;
|
|
867
916
|
let current = this.head;
|
|
868
|
-
for (let i = 0; i < index; i++) current = current.next;
|
|
917
|
+
for (let i = 0; i < index && current; i++) current = current.next;
|
|
869
918
|
return current;
|
|
870
919
|
}
|
|
871
920
|
/**
|
|
@@ -1700,8 +1749,10 @@ var LinkedListQueue = class extends SinglyLinkedList {
|
|
|
1700
1749
|
};
|
|
1701
1750
|
|
|
1702
1751
|
// src/utils/utils.ts
|
|
1703
|
-
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message
|
|
1704
|
-
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 ?? `Index ${index} is out of range [${min}, ${max}].`);
|
|
1755
|
+
}
|
|
1705
1756
|
}, "rangeCheck");
|
|
1706
1757
|
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
1707
1758
|
|
|
@@ -1710,19 +1761,13 @@ var Deque = class extends LinearBase {
|
|
|
1710
1761
|
static {
|
|
1711
1762
|
__name(this, "Deque");
|
|
1712
1763
|
}
|
|
1713
|
-
_equals = Object.is;
|
|
1714
|
-
/**
|
|
1715
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
1716
|
-
* @remarks Time O(N), Space O(N)
|
|
1717
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
1718
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
1719
|
-
* @returns New Deque instance.
|
|
1720
|
-
*/
|
|
1764
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
1721
1765
|
constructor(elements = [], options) {
|
|
1722
1766
|
super(options);
|
|
1723
1767
|
if (options) {
|
|
1724
|
-
const { bucketSize } = options;
|
|
1768
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
1725
1769
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
1770
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
1726
1771
|
}
|
|
1727
1772
|
let _size;
|
|
1728
1773
|
if ("length" in elements) {
|
|
@@ -1748,6 +1793,30 @@ var Deque = class extends LinearBase {
|
|
|
1748
1793
|
get bucketSize() {
|
|
1749
1794
|
return this._bucketSize;
|
|
1750
1795
|
}
|
|
1796
|
+
_autoCompactRatio = 0.5;
|
|
1797
|
+
/**
|
|
1798
|
+
* Get the auto-compaction ratio.
|
|
1799
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
1800
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
1801
|
+
* @remarks Time O(1), Space O(1)
|
|
1802
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
1803
|
+
*/
|
|
1804
|
+
get autoCompactRatio() {
|
|
1805
|
+
return this._autoCompactRatio;
|
|
1806
|
+
}
|
|
1807
|
+
/**
|
|
1808
|
+
* Set the auto-compaction ratio.
|
|
1809
|
+
* @remarks Time O(1), Space O(1)
|
|
1810
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
1811
|
+
*/
|
|
1812
|
+
set autoCompactRatio(value) {
|
|
1813
|
+
this._autoCompactRatio = value;
|
|
1814
|
+
}
|
|
1815
|
+
/**
|
|
1816
|
+
* Counter for shift/pop operations since last compaction check.
|
|
1817
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
1818
|
+
*/
|
|
1819
|
+
_compactCounter = 0;
|
|
1751
1820
|
_bucketFirst = 0;
|
|
1752
1821
|
/**
|
|
1753
1822
|
* Get the index of the first bucket in use.
|
|
@@ -1886,6 +1955,7 @@ var Deque = class extends LinearBase {
|
|
|
1886
1955
|
}
|
|
1887
1956
|
}
|
|
1888
1957
|
this._length -= 1;
|
|
1958
|
+
this._autoCompact();
|
|
1889
1959
|
return element;
|
|
1890
1960
|
}
|
|
1891
1961
|
/**
|
|
@@ -1908,6 +1978,7 @@ var Deque = class extends LinearBase {
|
|
|
1908
1978
|
}
|
|
1909
1979
|
}
|
|
1910
1980
|
this._length -= 1;
|
|
1981
|
+
this._autoCompact();
|
|
1911
1982
|
return element;
|
|
1912
1983
|
}
|
|
1913
1984
|
/**
|
|
@@ -2240,11 +2311,40 @@ var Deque = class extends LinearBase {
|
|
|
2240
2311
|
* @remarks Time O(N), Space O(1)
|
|
2241
2312
|
* @returns void
|
|
2242
2313
|
*/
|
|
2314
|
+
/**
|
|
2315
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
2316
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
2317
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
2318
|
+
*/
|
|
2319
|
+
_autoCompact() {
|
|
2320
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
2321
|
+
this._compactCounter++;
|
|
2322
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
2323
|
+
this._compactCounter = 0;
|
|
2324
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
2325
|
+
if (utilization < this._autoCompactRatio) {
|
|
2326
|
+
this.shrinkToFit();
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
/**
|
|
2330
|
+
* Compact the deque by removing unused buckets.
|
|
2331
|
+
* @remarks Time O(N), Space O(1)
|
|
2332
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
2333
|
+
*/
|
|
2334
|
+
/**
|
|
2335
|
+
* Compact the deque by removing unused buckets.
|
|
2336
|
+
* @remarks Time O(N), Space O(1)
|
|
2337
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
2338
|
+
*/
|
|
2339
|
+
compact() {
|
|
2340
|
+
const before = this._bucketCount;
|
|
2341
|
+
this.shrinkToFit();
|
|
2342
|
+
return this._bucketCount < before;
|
|
2343
|
+
}
|
|
2243
2344
|
shrinkToFit() {
|
|
2244
2345
|
if (this._length === 0) return;
|
|
2245
2346
|
const newBuckets = [];
|
|
2246
|
-
if (this._bucketFirst
|
|
2247
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
2347
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
2248
2348
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
2249
2349
|
newBuckets.push(this._buckets[i]);
|
|
2250
2350
|
}
|
|
@@ -2259,6 +2359,8 @@ var Deque = class extends LinearBase {
|
|
|
2259
2359
|
this._bucketFirst = 0;
|
|
2260
2360
|
this._bucketLast = newBuckets.length - 1;
|
|
2261
2361
|
this._buckets = newBuckets;
|
|
2362
|
+
this._bucketCount = newBuckets.length;
|
|
2363
|
+
this._compactCounter = 0;
|
|
2262
2364
|
}
|
|
2263
2365
|
/**
|
|
2264
2366
|
* Deep clone this deque, preserving options.
|
|
@@ -2438,30 +2540,6 @@ var Deque = class extends LinearBase {
|
|
|
2438
2540
|
}
|
|
2439
2541
|
}
|
|
2440
2542
|
};
|
|
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 {
|
|
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
|
-
static {
|
|
2456
|
-
__name(this, "Range");
|
|
2457
|
-
}
|
|
2458
|
-
// Determine whether a key is within the range
|
|
2459
|
-
isInRange(key, comparator) {
|
|
2460
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
2461
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
2462
|
-
return lowCheck && highCheck;
|
|
2463
|
-
}
|
|
2464
|
-
};
|
|
2465
2543
|
/**
|
|
2466
2544
|
* data-structure-typed
|
|
2467
2545
|
*
|
|
@@ -2470,6 +2548,6 @@ var Range = class {
|
|
|
2470
2548
|
* @license MIT License
|
|
2471
2549
|
*/
|
|
2472
2550
|
|
|
2473
|
-
export { DFSOperation, Deque, LinkedListQueue, Queue, Range };
|
|
2551
|
+
export { DFSOperation, Deque, ERR, LinkedListQueue, Queue, Range };
|
|
2474
2552
|
//# sourceMappingURL=index.mjs.map
|
|
2475
2553
|
//# sourceMappingURL=index.mjs.map
|