deque-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/deque-typed.js +114 -35
- 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 +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
|
@@ -735,6 +735,16 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
735
735
|
* @returns Layout information for this subtree.
|
|
736
736
|
*/
|
|
737
737
|
protected _displayAux(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
738
|
+
protected static _buildNodeDisplay(line: string, width: number, left: NodeDisplayLayout, right: NodeDisplayLayout): NodeDisplayLayout;
|
|
739
|
+
/**
|
|
740
|
+
* Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
|
|
741
|
+
*/
|
|
742
|
+
protected _isDisplayLeaf(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): boolean;
|
|
743
|
+
protected _hasDisplayableChild(child: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): boolean;
|
|
744
|
+
/**
|
|
745
|
+
* Resolve a display leaf node to its layout.
|
|
746
|
+
*/
|
|
747
|
+
protected _resolveDisplayLeaf(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions, emptyDisplayLayout: NodeDisplayLayout): NodeDisplayLayout;
|
|
738
748
|
/**
|
|
739
749
|
* (Protected) Swaps the key/value properties of two nodes.
|
|
740
750
|
* @remarks Time O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, FamilyPosition, NodePredicate, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, FamilyPosition, NodePredicate, RBTNColor, IterationType, RedBlackTreeOptions } from '../../types';
|
|
9
9
|
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
@@ -375,6 +375,12 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
375
375
|
* @param [thisArg] - See parameter type for details.
|
|
376
376
|
* @returns A new RedBlackTree with mapped entries.
|
|
377
377
|
*/
|
|
378
|
+
/**
|
|
379
|
+
* Red-Black trees are self-balancing — `perfectlyBalance` rebuilds via
|
|
380
|
+
* sorted bulk insert, which naturally produces a balanced RBT.
|
|
381
|
+
* @remarks Time O(N), Space O(N)
|
|
382
|
+
*/
|
|
383
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
378
384
|
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
379
385
|
/**
|
|
380
386
|
* (Internal) Create an empty instance of the same concrete tree type.
|
|
@@ -337,4 +337,48 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
337
337
|
* @remarks Time O(1), Space O(1)
|
|
338
338
|
*/
|
|
339
339
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
340
|
+
/**
|
|
341
|
+
* The edge connector string used in visual output.
|
|
342
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
343
|
+
*/
|
|
344
|
+
protected get _edgeConnector(): string;
|
|
345
|
+
/**
|
|
346
|
+
* Generate a text-based visual representation of the graph.
|
|
347
|
+
*
|
|
348
|
+
* **Adjacency list format:**
|
|
349
|
+
* ```
|
|
350
|
+
* Graph (5 vertices, 6 edges):
|
|
351
|
+
* A -> B (1), C (2)
|
|
352
|
+
* B -> D (3)
|
|
353
|
+
* C -> (no outgoing edges)
|
|
354
|
+
* D -> A (1)
|
|
355
|
+
* E (isolated)
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @param options - Optional display settings.
|
|
359
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
360
|
+
* @returns The visual string.
|
|
361
|
+
*/
|
|
362
|
+
toVisual(options?: {
|
|
363
|
+
showWeight?: boolean;
|
|
364
|
+
}): string;
|
|
365
|
+
/**
|
|
366
|
+
* Generate DOT language representation for Graphviz.
|
|
367
|
+
*
|
|
368
|
+
* @param options - Optional display settings.
|
|
369
|
+
* @param options.name - Graph name (default: 'G').
|
|
370
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
371
|
+
* @returns DOT format string.
|
|
372
|
+
*/
|
|
373
|
+
toDot(options?: {
|
|
374
|
+
name?: string;
|
|
375
|
+
showWeight?: boolean;
|
|
376
|
+
}): string;
|
|
377
|
+
/**
|
|
378
|
+
* Print the graph to console.
|
|
379
|
+
* @param options - Display settings passed to `toVisual`.
|
|
380
|
+
*/
|
|
381
|
+
print(options?: {
|
|
382
|
+
showWeight?: boolean;
|
|
383
|
+
}): void;
|
|
340
384
|
}
|
|
@@ -157,6 +157,7 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
157
157
|
* @remarks Time O(1), Space O(1)
|
|
158
158
|
*/
|
|
159
159
|
constructor(options?: Partial<GraphOptions<V>>);
|
|
160
|
+
protected get _edgeConnector(): string;
|
|
160
161
|
protected _outEdgeMap: Map<VO, EO[]>;
|
|
161
162
|
get outEdgeMap(): Map<VO, EO[]>;
|
|
162
163
|
set outEdgeMap(v: Map<VO, EO[]>);
|
|
@@ -313,6 +313,20 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
313
313
|
bridges: EO[];
|
|
314
314
|
cutVertices: VO[];
|
|
315
315
|
};
|
|
316
|
+
/**
|
|
317
|
+
* Find biconnected components using edge-stack Tarjan variant.
|
|
318
|
+
* A biconnected component is a maximal biconnected subgraph.
|
|
319
|
+
* @returns Array of edge arrays, each representing a biconnected component.
|
|
320
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
321
|
+
*/
|
|
322
|
+
getBiconnectedComponents(): EO[][];
|
|
323
|
+
/**
|
|
324
|
+
* Detect whether the graph contains a cycle.
|
|
325
|
+
* Uses DFS with parent tracking.
|
|
326
|
+
* @returns `true` if a cycle exists, `false` otherwise.
|
|
327
|
+
* @remarks Time O(V + E), Space O(V)
|
|
328
|
+
*/
|
|
329
|
+
hasCycle(): boolean;
|
|
316
330
|
/**
|
|
317
331
|
* Get bridges discovered by `tarjan()`.
|
|
318
332
|
* @returns Array of edges that are bridges.
|
|
@@ -143,7 +143,10 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
143
143
|
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
144
144
|
* @returns New Deque instance.
|
|
145
145
|
*/
|
|
146
|
-
constructor(elements?: IterableWithSizeOrLength<E
|
|
146
|
+
constructor(elements?: IterableWithSizeOrLength<E>, options?: DequeOptions<E, R>);
|
|
147
|
+
constructor(elements: IterableWithSizeOrLength<R>, options: DequeOptions<E, R> & {
|
|
148
|
+
toElementFn: (rawElement: R) => E;
|
|
149
|
+
});
|
|
147
150
|
protected _bucketSize: number;
|
|
148
151
|
/**
|
|
149
152
|
* Get the current bucket size.
|
|
@@ -151,6 +154,26 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
151
154
|
* @returns Bucket capacity per bucket.
|
|
152
155
|
*/
|
|
153
156
|
get bucketSize(): number;
|
|
157
|
+
protected _autoCompactRatio: number;
|
|
158
|
+
/**
|
|
159
|
+
* Get the auto-compaction ratio.
|
|
160
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
161
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
162
|
+
* @remarks Time O(1), Space O(1)
|
|
163
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
164
|
+
*/
|
|
165
|
+
get autoCompactRatio(): number;
|
|
166
|
+
/**
|
|
167
|
+
* Set the auto-compaction ratio.
|
|
168
|
+
* @remarks Time O(1), Space O(1)
|
|
169
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
170
|
+
*/
|
|
171
|
+
set autoCompactRatio(value: number);
|
|
172
|
+
/**
|
|
173
|
+
* Counter for shift/pop operations since last compaction check.
|
|
174
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
175
|
+
*/
|
|
176
|
+
protected _compactCounter: number;
|
|
154
177
|
protected _bucketFirst: number;
|
|
155
178
|
/**
|
|
156
179
|
* Get the index of the first bucket in use.
|
|
@@ -369,6 +392,23 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
369
392
|
* @remarks Time O(N), Space O(1)
|
|
370
393
|
* @returns void
|
|
371
394
|
*/
|
|
395
|
+
/**
|
|
396
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
397
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
398
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
399
|
+
*/
|
|
400
|
+
protected _autoCompact(): void;
|
|
401
|
+
/**
|
|
402
|
+
* Compact the deque by removing unused buckets.
|
|
403
|
+
* @remarks Time O(N), Space O(1)
|
|
404
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
405
|
+
*/
|
|
406
|
+
/**
|
|
407
|
+
* Compact the deque by removing unused buckets.
|
|
408
|
+
* @remarks Time O(N), Space O(1)
|
|
409
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
410
|
+
*/
|
|
411
|
+
compact(): boolean;
|
|
372
412
|
shrinkToFit(): void;
|
|
373
413
|
/**
|
|
374
414
|
* Deep clone this deque, preserving options.
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { LinearBaseOptions } from '../base';
|
|
2
2
|
export type DequeOptions<E, R> = {
|
|
3
3
|
bucketSize?: number;
|
|
4
|
+
/**
|
|
5
|
+
* When the ratio of used buckets to total buckets falls below this threshold
|
|
6
|
+
* after a shift/pop, auto-compact is triggered. Set to 0 to disable.
|
|
7
|
+
* Default: 0.5 (compact when less than half the buckets are in use).
|
|
8
|
+
*/
|
|
9
|
+
autoCompactRatio?: number;
|
|
4
10
|
} & LinearBaseOptions<E, R>;
|
package/dist/umd/deque-typed.js
CHANGED
|
@@ -25,15 +25,64 @@ var dequeTyped = (() => {
|
|
|
25
25
|
__export(src_exports, {
|
|
26
26
|
DFSOperation: () => DFSOperation,
|
|
27
27
|
Deque: () => Deque,
|
|
28
|
+
ERR: () => ERR,
|
|
28
29
|
Range: () => Range
|
|
29
30
|
});
|
|
30
31
|
|
|
31
32
|
// src/utils/utils.ts
|
|
32
|
-
var rangeCheck = (index, min, max, message
|
|
33
|
-
if (index < min || index > max)
|
|
33
|
+
var rangeCheck = (index, min, max, message) => {
|
|
34
|
+
if (index < min || index > max) {
|
|
35
|
+
throw new RangeError(message != null ? message : `Index ${index} is out of range [${min}, ${max}].`);
|
|
36
|
+
}
|
|
34
37
|
};
|
|
35
38
|
var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
36
39
|
|
|
40
|
+
// src/common/error.ts
|
|
41
|
+
var ERR = {
|
|
42
|
+
// Range / index
|
|
43
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
44
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
45
|
+
// Type / argument
|
|
46
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
47
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
48
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
49
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
50
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
51
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
52
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
53
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
54
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
55
|
+
// State / operation
|
|
56
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
57
|
+
// Matrix
|
|
58
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
59
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
60
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
61
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
62
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/common/index.ts
|
|
66
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
67
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
68
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
69
|
+
return DFSOperation2;
|
|
70
|
+
})(DFSOperation || {});
|
|
71
|
+
var Range = class {
|
|
72
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
73
|
+
this.low = low;
|
|
74
|
+
this.high = high;
|
|
75
|
+
this.includeLow = includeLow;
|
|
76
|
+
this.includeHigh = includeHigh;
|
|
77
|
+
}
|
|
78
|
+
// Determine whether a key is within the range
|
|
79
|
+
isInRange(key, comparator) {
|
|
80
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
81
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
82
|
+
return lowCheck && highCheck;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
37
86
|
// src/data-structures/base/iterable-element-base.ts
|
|
38
87
|
var IterableElementBase = class {
|
|
39
88
|
/**
|
|
@@ -56,7 +105,7 @@ var dequeTyped = (() => {
|
|
|
56
105
|
if (options) {
|
|
57
106
|
const { toElementFn } = options;
|
|
58
107
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
59
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
108
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
60
109
|
}
|
|
61
110
|
}
|
|
62
111
|
/**
|
|
@@ -212,7 +261,7 @@ var dequeTyped = (() => {
|
|
|
212
261
|
acc = initialValue;
|
|
213
262
|
} else {
|
|
214
263
|
const first = iter.next();
|
|
215
|
-
if (first.done) throw new TypeError(
|
|
264
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
216
265
|
acc = first.value;
|
|
217
266
|
index = 1;
|
|
218
267
|
}
|
|
@@ -446,17 +495,16 @@ var dequeTyped = (() => {
|
|
|
446
495
|
|
|
447
496
|
// src/data-structures/queue/deque.ts
|
|
448
497
|
var Deque = class extends LinearBase {
|
|
449
|
-
/**
|
|
450
|
-
* Create a Deque and optionally bulk-insert elements.
|
|
451
|
-
* @remarks Time O(N), Space O(N)
|
|
452
|
-
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
453
|
-
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
454
|
-
* @returns New Deque instance.
|
|
455
|
-
*/
|
|
456
498
|
constructor(elements = [], options) {
|
|
457
499
|
super(options);
|
|
458
500
|
__publicField(this, "_equals", (a, b) => Object.is(a, b));
|
|
459
501
|
__publicField(this, "_bucketSize", 1 << 12);
|
|
502
|
+
__publicField(this, "_autoCompactRatio", 0.5);
|
|
503
|
+
/**
|
|
504
|
+
* Counter for shift/pop operations since last compaction check.
|
|
505
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
506
|
+
*/
|
|
507
|
+
__publicField(this, "_compactCounter", 0);
|
|
460
508
|
__publicField(this, "_bucketFirst", 0);
|
|
461
509
|
__publicField(this, "_firstInBucket", 0);
|
|
462
510
|
__publicField(this, "_bucketLast", 0);
|
|
@@ -465,8 +513,9 @@ var dequeTyped = (() => {
|
|
|
465
513
|
__publicField(this, "_buckets", []);
|
|
466
514
|
__publicField(this, "_length", 0);
|
|
467
515
|
if (options) {
|
|
468
|
-
const { bucketSize } = options;
|
|
516
|
+
const { bucketSize, autoCompactRatio } = options;
|
|
469
517
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
518
|
+
if (typeof autoCompactRatio === "number") this._autoCompactRatio = autoCompactRatio;
|
|
470
519
|
}
|
|
471
520
|
let _size;
|
|
472
521
|
if ("length" in elements) {
|
|
@@ -491,6 +540,24 @@ var dequeTyped = (() => {
|
|
|
491
540
|
get bucketSize() {
|
|
492
541
|
return this._bucketSize;
|
|
493
542
|
}
|
|
543
|
+
/**
|
|
544
|
+
* Get the auto-compaction ratio.
|
|
545
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
546
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
547
|
+
* @remarks Time O(1), Space O(1)
|
|
548
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
549
|
+
*/
|
|
550
|
+
get autoCompactRatio() {
|
|
551
|
+
return this._autoCompactRatio;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Set the auto-compaction ratio.
|
|
555
|
+
* @remarks Time O(1), Space O(1)
|
|
556
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
557
|
+
*/
|
|
558
|
+
set autoCompactRatio(value) {
|
|
559
|
+
this._autoCompactRatio = value;
|
|
560
|
+
}
|
|
494
561
|
/**
|
|
495
562
|
* Get the index of the first bucket in use.
|
|
496
563
|
* @remarks Time O(1), Space O(1)
|
|
@@ -622,6 +689,7 @@ var dequeTyped = (() => {
|
|
|
622
689
|
}
|
|
623
690
|
}
|
|
624
691
|
this._length -= 1;
|
|
692
|
+
this._autoCompact();
|
|
625
693
|
return element;
|
|
626
694
|
}
|
|
627
695
|
/**
|
|
@@ -644,6 +712,7 @@ var dequeTyped = (() => {
|
|
|
644
712
|
}
|
|
645
713
|
}
|
|
646
714
|
this._length -= 1;
|
|
715
|
+
this._autoCompact();
|
|
647
716
|
return element;
|
|
648
717
|
}
|
|
649
718
|
/**
|
|
@@ -976,11 +1045,40 @@ var dequeTyped = (() => {
|
|
|
976
1045
|
* @remarks Time O(N), Space O(1)
|
|
977
1046
|
* @returns void
|
|
978
1047
|
*/
|
|
1048
|
+
/**
|
|
1049
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
1050
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
1051
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
1052
|
+
*/
|
|
1053
|
+
_autoCompact() {
|
|
1054
|
+
if (this._autoCompactRatio <= 0 || this._bucketCount <= 1) return;
|
|
1055
|
+
this._compactCounter++;
|
|
1056
|
+
if (this._compactCounter < this._bucketSize) return;
|
|
1057
|
+
this._compactCounter = 0;
|
|
1058
|
+
const utilization = this._length / (this._bucketCount * this._bucketSize);
|
|
1059
|
+
if (utilization < this._autoCompactRatio) {
|
|
1060
|
+
this.shrinkToFit();
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Compact the deque by removing unused buckets.
|
|
1065
|
+
* @remarks Time O(N), Space O(1)
|
|
1066
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1067
|
+
*/
|
|
1068
|
+
/**
|
|
1069
|
+
* Compact the deque by removing unused buckets.
|
|
1070
|
+
* @remarks Time O(N), Space O(1)
|
|
1071
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
1072
|
+
*/
|
|
1073
|
+
compact() {
|
|
1074
|
+
const before = this._bucketCount;
|
|
1075
|
+
this.shrinkToFit();
|
|
1076
|
+
return this._bucketCount < before;
|
|
1077
|
+
}
|
|
979
1078
|
shrinkToFit() {
|
|
980
1079
|
if (this._length === 0) return;
|
|
981
1080
|
const newBuckets = [];
|
|
982
|
-
if (this._bucketFirst
|
|
983
|
-
else if (this._bucketFirst < this._bucketLast) {
|
|
1081
|
+
if (this._bucketFirst <= this._bucketLast) {
|
|
984
1082
|
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
985
1083
|
newBuckets.push(this._buckets[i]);
|
|
986
1084
|
}
|
|
@@ -995,6 +1093,8 @@ var dequeTyped = (() => {
|
|
|
995
1093
|
this._bucketFirst = 0;
|
|
996
1094
|
this._bucketLast = newBuckets.length - 1;
|
|
997
1095
|
this._buckets = newBuckets;
|
|
1096
|
+
this._bucketCount = newBuckets.length;
|
|
1097
|
+
this._compactCounter = 0;
|
|
998
1098
|
}
|
|
999
1099
|
/**
|
|
1000
1100
|
* Deep clone this deque, preserving options.
|
|
@@ -1174,27 +1274,6 @@ var dequeTyped = (() => {
|
|
|
1174
1274
|
}
|
|
1175
1275
|
}
|
|
1176
1276
|
};
|
|
1177
|
-
|
|
1178
|
-
// src/common/index.ts
|
|
1179
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1180
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1181
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1182
|
-
return DFSOperation2;
|
|
1183
|
-
})(DFSOperation || {});
|
|
1184
|
-
var Range = class {
|
|
1185
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1186
|
-
this.low = low;
|
|
1187
|
-
this.high = high;
|
|
1188
|
-
this.includeLow = includeLow;
|
|
1189
|
-
this.includeHigh = includeHigh;
|
|
1190
|
-
}
|
|
1191
|
-
// Determine whether a key is within the range
|
|
1192
|
-
isInRange(key, comparator) {
|
|
1193
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1194
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1195
|
-
return lowCheck && highCheck;
|
|
1196
|
-
}
|
|
1197
|
-
};
|
|
1198
1277
|
return __toCommonJS(src_exports);
|
|
1199
1278
|
})();
|
|
1200
1279
|
/**
|