linked-list-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.
Files changed (65) hide show
  1. package/dist/cjs/index.cjs +62 -36
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +62 -35
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +62 -37
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +62 -36
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +23 -0
  10. package/dist/types/common/index.d.ts +1 -0
  11. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +15 -5
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
  15. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  16. package/dist/types/data-structures/graph/directed-graph.d.ts +3 -2
  17. package/dist/types/data-structures/graph/undirected-graph.d.ts +16 -2
  18. package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
  19. package/dist/types/data-structures/heap/heap.d.ts +3 -7
  20. package/dist/types/data-structures/queue/deque.d.ts +41 -1
  21. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
  22. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  23. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  24. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  25. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  26. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  27. package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
  28. package/dist/umd/linked-list-typed.js +60 -33
  29. package/dist/umd/linked-list-typed.js.map +1 -1
  30. package/dist/umd/linked-list-typed.min.js +1 -1
  31. package/dist/umd/linked-list-typed.min.js.map +1 -1
  32. package/package.json +2 -2
  33. package/src/common/error.ts +60 -0
  34. package/src/common/index.ts +2 -0
  35. package/src/data-structures/base/iterable-element-base.ts +5 -4
  36. package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
  37. package/src/data-structures/binary-tree/binary-tree.ts +121 -49
  38. package/src/data-structures/binary-tree/bst.ts +12 -4
  39. package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
  40. package/src/data-structures/binary-tree/tree-map.ts +8 -7
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
  42. package/src/data-structures/binary-tree/tree-multi-set.ts +10 -9
  43. package/src/data-structures/binary-tree/tree-set.ts +7 -6
  44. package/src/data-structures/graph/abstract-graph.ts +124 -19
  45. package/src/data-structures/graph/directed-graph.ts +8 -4
  46. package/src/data-structures/graph/map-graph.ts +1 -1
  47. package/src/data-structures/graph/undirected-graph.ts +99 -4
  48. package/src/data-structures/hash/hash-map.ts +19 -6
  49. package/src/data-structures/heap/heap.ts +21 -17
  50. package/src/data-structures/heap/max-heap.ts +2 -3
  51. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  52. package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
  53. package/src/data-structures/matrix/matrix.ts +9 -10
  54. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
  55. package/src/data-structures/queue/deque.ts +72 -4
  56. package/src/data-structures/stack/stack.ts +1 -1
  57. package/src/data-structures/trie/trie.ts +12 -6
  58. package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
  59. package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
  60. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
  61. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
  62. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  63. package/src/types/data-structures/queue/deque.ts +7 -0
  64. package/src/types/data-structures/stack/stack.ts +1 -1
  65. package/src/utils/utils.ts +4 -2
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Centralized error message templates.
3
+ * Keep using native Error/TypeError/RangeError — this only standardizes messages.
4
+ */
5
+ export declare const ERR: {
6
+ readonly indexOutOfRange: (index: number, min: number, max: number, ctx?: string) => string;
7
+ readonly invalidIndex: (ctx?: string) => string;
8
+ readonly invalidArgument: (reason: string, ctx?: string) => string;
9
+ readonly comparatorRequired: (ctx?: string) => string;
10
+ readonly invalidKey: (reason: string, ctx?: string) => string;
11
+ readonly notAFunction: (name: string, ctx?: string) => string;
12
+ readonly invalidEntry: (ctx?: string) => string;
13
+ readonly invalidNaN: (ctx?: string) => string;
14
+ readonly invalidDate: (ctx?: string) => string;
15
+ readonly reduceEmpty: (ctx?: string) => string;
16
+ readonly callbackReturnType: (expected: string, got: string, ctx?: string) => string;
17
+ readonly invalidOperation: (reason: string, ctx?: string) => string;
18
+ readonly matrixDimensionMismatch: (op: string) => string;
19
+ readonly matrixSingular: () => string;
20
+ readonly matrixNotSquare: () => string;
21
+ readonly matrixNotRectangular: () => string;
22
+ readonly matrixRowMismatch: (expected: number, got: number) => string;
23
+ };
@@ -1,3 +1,4 @@
1
+ export { ERR } from './error';
1
2
  export declare enum DFSOperation {
2
3
  VISIT = 0,
3
4
  PROCESS = 1
@@ -27,7 +27,7 @@ export declare abstract class IterableElementBase<E, R> implements Iterable<E> {
27
27
  * @remarks
28
28
  * Time O(1), Space O(1).
29
29
  */
30
- protected _toElementFn?: (rawElement: R) => E;
30
+ protected readonly _toElementFn?: (rawElement: R) => E;
31
31
  /**
32
32
  * Exposes the current `toElementFn`, if configured.
33
33
  *
@@ -275,7 +275,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
275
275
  * @param [options] - Configuration options for the tree.
276
276
  */
277
277
  constructor(keysNodesEntriesOrRaws?: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BinaryTreeOptions<K, V, R>);
278
- protected _isMapMode: boolean;
278
+ protected readonly _isMapMode: boolean;
279
279
  /**
280
280
  * Gets whether the tree is in Map mode.
281
281
  * @remarks In Map mode (default), values are stored in an external Map, and nodes only hold keys. If false, values are stored directly on the nodes. Time O(1)
@@ -283,7 +283,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
283
283
  * @returns True if in Map mode, false otherwise.
284
284
  */
285
285
  get isMapMode(): boolean;
286
- protected _isDuplicate: boolean;
286
+ protected readonly _isDuplicate: boolean;
287
287
  /**
288
288
  * Gets whether the tree allows duplicate keys.
289
289
  * @remarks Time O(1)
@@ -315,7 +315,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
315
315
  * @returns The size of the tree.
316
316
  */
317
317
  get size(): number;
318
- protected _NIL: BinaryTreeNode<K, V>;
318
+ protected readonly _NIL: BinaryTreeNode<K, V>;
319
319
  /**
320
320
  * Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
321
321
  * @remarks Time O(1)
@@ -323,7 +323,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
323
323
  * @returns The NIL node.
324
324
  */
325
325
  get NIL(): BinaryTreeNode<K, V>;
326
- protected _toEntryFn?: ToEntryFn<K, V, R>;
326
+ protected readonly _toEntryFn?: ToEntryFn<K, V, R>;
327
327
  /**
328
328
  * Gets the function used to convert raw data objects (R) into [key, value] entries.
329
329
  * @remarks Time O(1)
@@ -682,7 +682,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
682
682
  * @param node - The node.
683
683
  * @returns The node's key or undefined.
684
684
  */
685
- protected _DEFAULT_NODE_CALLBACK: (node: BinaryTreeNode<K, V> | null | undefined) => K | undefined;
685
+ protected readonly _DEFAULT_NODE_CALLBACK: NodeCallback<BinaryTreeNode<K, V> | null | undefined, K | undefined>;
686
686
  /**
687
687
  * (Protected) Snapshots the current tree's configuration options.
688
688
  * @remarks Time O(1)
@@ -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)
@@ -294,7 +294,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
294
294
 
295
295
  * @remarks Time O(1) Space O(1)
296
296
  */
297
- protected _comparator: Comparator<K>;
297
+ protected readonly _comparator: Comparator<K>;
298
298
  /**
299
299
  * Gets the comparator function used by the tree.
300
300
  * @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[]>);
@@ -170,7 +171,7 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
170
171
  * @returns DirectedGraph with all keys added.
171
172
  * @remarks Time O(V), Space O(V)
172
173
  */
173
- static fromKeys<K extends VertexKey>(keys: Iterable<K>): DirectedGraph<K, any, DirectedVertex<K>, DirectedEdge<any>>;
174
+ static fromKeys<K extends VertexKey>(keys: Iterable<K>): DirectedGraph<K, undefined, DirectedVertex<K>, DirectedEdge<undefined>>;
174
175
  /**
175
176
  * Construct a directed graph from `[key, value]` entries.
176
177
  * @template V - Vertex value type.
@@ -178,7 +179,7 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
178
179
  * @returns DirectedGraph with all vertices added.
179
180
  * @remarks Time O(V), Space O(V)
180
181
  */
181
- static fromEntries<V>(entries: Iterable<[VertexKey, V]>): DirectedGraph<V, any, DirectedVertex<V>, DirectedEdge<any>>;
182
+ static fromEntries<V>(entries: Iterable<[VertexKey, V]>): DirectedGraph<V, undefined, DirectedVertex<V>, DirectedEdge<undefined>>;
182
183
  /**
183
184
  * Create a directed vertex instance. Does not insert into the graph.
184
185
  * @param key - Vertex identifier.
@@ -200,7 +200,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
200
200
  * @returns UndirectedGraph with all keys added.
201
201
  * @remarks Time O(V), Space O(V)
202
202
  */
203
- static fromKeys<K extends VertexKey>(keys: Iterable<K>): UndirectedGraph<K, any, UndirectedVertex<K>, UndirectedEdge<any>>;
203
+ static fromKeys<K extends VertexKey>(keys: Iterable<K>): UndirectedGraph<K, undefined, UndirectedVertex<K>, UndirectedEdge<undefined>>;
204
204
  /**
205
205
  * Construct an undirected graph from `[key, value]` entries.
206
206
  * @template V - Vertex value type.
@@ -208,7 +208,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
208
208
  * @returns UndirectedGraph with all vertices added.
209
209
  * @remarks Time O(V), Space O(V)
210
210
  */
211
- static fromEntries<V>(entries: Iterable<[VertexKey, V]>): UndirectedGraph<V, any, UndirectedVertex<V>, UndirectedEdge<any>>;
211
+ static fromEntries<V>(entries: Iterable<[VertexKey, V]>): UndirectedGraph<V, undefined, UndirectedVertex<V>, UndirectedEdge<undefined>>;
212
212
  /**
213
213
  * Create an undirected vertex instance. Does not insert into the graph.
214
214
  * @param key - Vertex identifier.
@@ -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.
@@ -175,7 +175,7 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
175
175
  * @returns Map of object→value.
176
176
  */
177
177
  get objMap(): Map<object, V>;
178
- protected _toEntryFn?: (rawElement: R) => [K, V];
178
+ protected readonly _toEntryFn?: (rawElement: R) => [K, V];
179
179
  /**
180
180
  * Get the raw→entry converter function if present.
181
181
  * @remarks Time O(1), Space O(1)
@@ -346,7 +346,7 @@ export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends Iterabl
346
346
  * @returns Tail node or sentinel.
347
347
  */
348
348
  get tail(): HashMapLinkedNode<K, V | undefined>;
349
- protected _toEntryFn?: (rawElement: R) => [K, V];
349
+ protected readonly _toEntryFn?: (rawElement: R) => [K, V];
350
350
  get toEntryFn(): ((rawElement: R) => [K, V]) | undefined;
351
351
  protected _size: number;
352
352
  get size(): number;
@@ -414,12 +414,8 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
414
414
  * @returns A new heap with mapped elements.
415
415
  */
416
416
  mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
417
- protected _DEFAULT_COMPARATOR: (a: E, b: E) => number;
418
- protected _comparator: Comparator<E>; /**
419
- * Get the comparator used to order elements.
420
- * @remarks Time O(1), Space O(1)
421
- * @returns Comparator function.
422
- */
417
+ protected readonly _DEFAULT_COMPARATOR: Comparator<E>;
418
+ protected readonly _comparator: Comparator<E>;
423
419
  /**
424
420
  * Get the comparator used to order elements.
425
421
  * @remarks Time O(1), Space O(1)
@@ -501,7 +497,7 @@ export declare class FibonacciHeap<E> {
501
497
  * @returns Min node or undefined.
502
498
  */
503
499
  get min(): FibonacciHeapNode<E> | undefined;
504
- protected _comparator: Comparator<E>;
500
+ protected readonly _comparator: Comparator<E>;
505
501
  get comparator(): Comparator<E>;
506
502
  clear(): void;
507
503
  add(element: E): boolean;
@@ -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> | IterableWithSizeOrLength<R>, options?: DequeOptions<E, R>);
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,2 +1,2 @@
1
1
  import { BSTOptions } from './bst';
2
- export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
2
+ export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R>;
@@ -1,3 +1,3 @@
1
1
  import type { BSTOptions } from './bst';
2
2
  export type RBTNColor = 'RED' | 'BLACK';
3
- export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
3
+ export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R>;
@@ -1,2 +1,2 @@
1
1
  import { LinearBaseOptions } from '../base';
2
- export type DoublyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
2
+ export type DoublyLinkedListOptions<E, R> = LinearBaseOptions<E, R>;
@@ -1,2 +1,2 @@
1
1
  import { LinearBaseOptions } from '../base';
2
- export type SinglyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
2
+ export type SinglyLinkedListOptions<E, R> = LinearBaseOptions<E, R>;
@@ -1,2 +1,2 @@
1
1
  import { HeapOptions } from '../heap';
2
- export type PriorityQueueOptions<E, R> = HeapOptions<E, R> & {};
2
+ export type PriorityQueueOptions<E, R> = HeapOptions<E, R>;
@@ -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>;
@@ -1,2 +1,2 @@
1
1
  import { IterableElementBaseOptions } from '../base';
2
- export type StackOptions<E, R> = IterableElementBaseOptions<E, R> & {};
2
+ export type StackOptions<E, R> = IterableElementBaseOptions<E, R>;
@@ -26,6 +26,7 @@ var linkedListTyped = (() => {
26
26
  DFSOperation: () => DFSOperation,
27
27
  DoublyLinkedList: () => DoublyLinkedList,
28
28
  DoublyLinkedListNode: () => DoublyLinkedListNode,
29
+ ERR: () => ERR,
29
30
  Range: () => Range,
30
31
  SinglyLinkedList: () => SinglyLinkedList,
31
32
  SinglyLinkedListNode: () => SinglyLinkedListNode,
@@ -33,6 +34,52 @@ var linkedListTyped = (() => {
33
34
  SkipListNode: () => SkipListNode
34
35
  });
35
36
 
37
+ // src/common/error.ts
38
+ var ERR = {
39
+ // Range / index
40
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
41
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
42
+ // Type / argument
43
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
44
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
45
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
46
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
47
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
48
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
49
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
50
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
51
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
52
+ // State / operation
53
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
54
+ // Matrix
55
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
56
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
57
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
58
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
59
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
60
+ };
61
+
62
+ // src/common/index.ts
63
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
64
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
65
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
66
+ return DFSOperation2;
67
+ })(DFSOperation || {});
68
+ var Range = class {
69
+ constructor(low, high, includeLow = true, includeHigh = true) {
70
+ this.low = low;
71
+ this.high = high;
72
+ this.includeLow = includeLow;
73
+ this.includeHigh = includeHigh;
74
+ }
75
+ // Determine whether a key is within the range
76
+ isInRange(key, comparator) {
77
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
78
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
79
+ return lowCheck && highCheck;
80
+ }
81
+ };
82
+
36
83
  // src/data-structures/base/iterable-element-base.ts
37
84
  var IterableElementBase = class {
38
85
  /**
@@ -55,7 +102,7 @@ var linkedListTyped = (() => {
55
102
  if (options) {
56
103
  const { toElementFn } = options;
57
104
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
58
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
105
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
59
106
  }
60
107
  }
61
108
  /**
@@ -211,7 +258,7 @@ var linkedListTyped = (() => {
211
258
  acc = initialValue;
212
259
  } else {
213
260
  const first = iter.next();
214
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
261
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
215
262
  acc = first.value;
216
263
  index = 1;
217
264
  }
@@ -674,7 +721,7 @@ var linkedListTyped = (() => {
674
721
  */
675
722
  constructor(elements = [], options) {
676
723
  super(options);
677
- __publicField(this, "_equals", Object.is);
724
+ __publicField(this, "_equals", (a, b) => Object.is(a, b));
678
725
  __publicField(this, "_head");
679
726
  __publicField(this, "_tail");
680
727
  __publicField(this, "_length", 0);
@@ -762,6 +809,7 @@ var linkedListTyped = (() => {
762
809
  * @returns Removed element or undefined.
763
810
  */
764
811
  pop() {
812
+ var _a;
765
813
  if (!this.head) return void 0;
766
814
  if (this.head === this.tail) {
767
815
  const value2 = this.head.value;
@@ -771,8 +819,8 @@ var linkedListTyped = (() => {
771
819
  return value2;
772
820
  }
773
821
  let current = this.head;
774
- while (current.next !== this.tail) current = current.next;
775
- const value = this.tail.value;
822
+ while (current.next && current.next !== this.tail) current = current.next;
823
+ const value = (_a = this.tail) == null ? void 0 : _a.value;
776
824
  current.next = void 0;
777
825
  this._tail = current;
778
826
  this._length--;
@@ -860,8 +908,8 @@ var linkedListTyped = (() => {
860
908
  at(index) {
861
909
  if (index < 0 || index >= this._length) return void 0;
862
910
  let current = this.head;
863
- for (let i = 0; i < index; i++) current = current.next;
864
- return current.value;
911
+ for (let i = 0; i < index && current; i++) current = current.next;
912
+ return current == null ? void 0 : current.value;
865
913
  }
866
914
  /**
867
915
  * Type guard: check whether the input is a SinglyLinkedListNode.
@@ -881,7 +929,7 @@ var linkedListTyped = (() => {
881
929
  getNodeAt(index) {
882
930
  if (index < 0 || index >= this._length) return void 0;
883
931
  let current = this.head;
884
- for (let i = 0; i < index; i++) current = current.next;
932
+ for (let i = 0; i < index && current; i++) current = current.next;
885
933
  return current;
886
934
  }
887
935
  /**
@@ -1392,7 +1440,7 @@ var linkedListTyped = (() => {
1392
1440
  */
1393
1441
  constructor(elements = [], options) {
1394
1442
  super(options);
1395
- __publicField(this, "_equals", Object.is);
1443
+ __publicField(this, "_equals", (a, b) => Object.is(a, b));
1396
1444
  __publicField(this, "_head");
1397
1445
  __publicField(this, "_tail");
1398
1446
  __publicField(this, "_length", 0);
@@ -1580,8 +1628,8 @@ var linkedListTyped = (() => {
1580
1628
  at(index) {
1581
1629
  if (index < 0 || index >= this._length) return void 0;
1582
1630
  let current = this.head;
1583
- for (let i = 0; i < index; i++) current = current.next;
1584
- return current.value;
1631
+ for (let i = 0; i < index && current; i++) current = current.next;
1632
+ return current == null ? void 0 : current.value;
1585
1633
  }
1586
1634
  /**
1587
1635
  * Get the node reference at a given index.
@@ -1592,7 +1640,7 @@ var linkedListTyped = (() => {
1592
1640
  getNodeAt(index) {
1593
1641
  if (index < 0 || index >= this._length) return void 0;
1594
1642
  let current = this.head;
1595
- for (let i = 0; i < index; i++) current = current.next;
1643
+ for (let i = 0; i < index && current; i++) current = current.next;
1596
1644
  return current;
1597
1645
  }
1598
1646
  /**
@@ -2086,27 +2134,6 @@ var linkedListTyped = (() => {
2086
2134
  return level;
2087
2135
  }
2088
2136
  };
2089
-
2090
- // src/common/index.ts
2091
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
2092
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
2093
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
2094
- return DFSOperation2;
2095
- })(DFSOperation || {});
2096
- var Range = class {
2097
- constructor(low, high, includeLow = true, includeHigh = true) {
2098
- this.low = low;
2099
- this.high = high;
2100
- this.includeLow = includeLow;
2101
- this.includeHigh = includeHigh;
2102
- }
2103
- // Determine whether a key is within the range
2104
- isInRange(key, comparator) {
2105
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
2106
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
2107
- return lowCheck && highCheck;
2108
- }
2109
- };
2110
2137
  return __toCommonJS(src_exports);
2111
2138
  })();
2112
2139
  /**