avl-tree-typed 2.0.4 → 2.1.0

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 (104) hide show
  1. package/dist/common/index.js +1 -1
  2. package/dist/constants/index.js +1 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  4. package/dist/data-structures/base/iterable-element-base.js +149 -107
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  6. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  7. package/dist/data-structures/base/linear-base.d.ts +250 -192
  8. package/dist/data-structures/base/linear-base.js +137 -274
  9. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  10. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  12. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  13. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  14. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  15. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  16. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  17. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  18. package/dist/data-structures/binary-tree/bst.js +505 -481
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  20. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  22. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  25. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  26. package/dist/data-structures/graph/abstract-graph.js +267 -237
  27. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  28. package/dist/data-structures/graph/directed-graph.js +146 -233
  29. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  30. package/dist/data-structures/graph/map-graph.js +56 -59
  31. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  32. package/dist/data-structures/graph/undirected-graph.js +129 -149
  33. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  34. package/dist/data-structures/hash/hash-map.js +270 -457
  35. package/dist/data-structures/heap/heap.d.ts +214 -289
  36. package/dist/data-structures/heap/heap.js +340 -349
  37. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  38. package/dist/data-structures/heap/max-heap.js +11 -66
  39. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  40. package/dist/data-structures/heap/min-heap.js +11 -66
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  44. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  45. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  46. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  47. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  48. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  49. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  50. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  51. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  52. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  53. package/dist/data-structures/queue/deque.d.ts +227 -254
  54. package/dist/data-structures/queue/deque.js +309 -348
  55. package/dist/data-structures/queue/queue.d.ts +180 -201
  56. package/dist/data-structures/queue/queue.js +265 -248
  57. package/dist/data-structures/stack/stack.d.ts +124 -102
  58. package/dist/data-structures/stack/stack.js +181 -125
  59. package/dist/data-structures/trie/trie.d.ts +164 -165
  60. package/dist/data-structures/trie/trie.js +189 -172
  61. package/dist/interfaces/binary-tree.d.ts +56 -6
  62. package/dist/interfaces/graph.d.ts +16 -0
  63. package/dist/types/data-structures/base/base.d.ts +1 -1
  64. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  65. package/dist/types/utils/utils.d.ts +6 -6
  66. package/dist/utils/number.js +1 -2
  67. package/dist/utils/utils.d.ts +110 -49
  68. package/dist/utils/utils.js +149 -74
  69. package/package.json +15 -15
  70. package/src/data-structures/base/iterable-element-base.ts +238 -115
  71. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  72. package/src/data-structures/base/linear-base.ts +271 -277
  73. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  74. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  75. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  76. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  77. package/src/data-structures/binary-tree/bst.ts +568 -570
  78. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  79. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  80. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  81. package/src/data-structures/graph/abstract-graph.ts +339 -264
  82. package/src/data-structures/graph/directed-graph.ts +146 -236
  83. package/src/data-structures/graph/map-graph.ts +63 -60
  84. package/src/data-structures/graph/undirected-graph.ts +129 -152
  85. package/src/data-structures/hash/hash-map.ts +274 -496
  86. package/src/data-structures/heap/heap.ts +389 -402
  87. package/src/data-structures/heap/max-heap.ts +12 -76
  88. package/src/data-structures/heap/min-heap.ts +13 -76
  89. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  90. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  91. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  92. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  93. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  94. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  95. package/src/data-structures/queue/deque.ts +381 -357
  96. package/src/data-structures/queue/queue.ts +310 -264
  97. package/src/data-structures/stack/stack.ts +217 -131
  98. package/src/data-structures/trie/trie.ts +240 -175
  99. package/src/interfaces/binary-tree.ts +240 -6
  100. package/src/interfaces/graph.ts +37 -0
  101. package/src/types/data-structures/base/base.ts +5 -5
  102. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  103. package/src/types/utils/utils.ts +9 -5
  104. package/src/utils/utils.ts +152 -86
@@ -3,7 +3,7 @@ import { LinearBase } from '../../../data-structures/base/linear-base';
3
3
  export type EntryCallback<K, V, R> = (key: K, value: V, index: number, original: IterableEntryBase<K, V>) => R;
4
4
  export type ElementCallback<E, R, RT> = (element: E, index: number, original: IterableElementBase<E, R>) => RT;
5
5
  export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, original: IterableEntryBase<K, V>) => R;
6
- export type ReduceElementCallback<E, R, RT = E> = (accumulator: RT, element: E, index: number, original: IterableElementBase<E, R>) => RT;
6
+ export type ReduceElementCallback<E, R, U = E> = (accumulator: U, value: E, index: number, self: IterableElementBase<E, R>) => U;
7
7
  export type ReduceLinearCallback<E, RT = E> = (accumulator: RT, element: E, index: number, original: LinearBase<E>) => RT;
8
8
  export type IterableElementBaseOptions<E, R> = {
9
9
  toElementFn?: (rawElement: R) => E;
@@ -8,3 +8,7 @@ export type DijkstraResult<V> = {
8
8
  minDist: number;
9
9
  minPath: V[];
10
10
  } | undefined;
11
+ export type GraphOptions<V = any> = {
12
+ vertexValueInitializer?: (key: VertexKey) => V;
13
+ defaultEdgeWeight?: number;
14
+ };
@@ -1,12 +1,7 @@
1
- export type ToThunkFn<R = any> = () => R;
2
- export type Thunk<R = any> = ToThunkFn<R> & {
3
- __THUNK__?: symbol;
4
- };
5
- export type TrlFn<A extends any[] = any[], R = any> = (...args: A) => R;
6
- export type TrlAsyncFn = (...args: any[]) => any;
7
1
  export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
8
2
  export type Any = string | number | bigint | boolean | symbol | undefined | object;
9
3
  export type Arithmetic = number | bigint;
4
+ export type ElemOf<T> = T extends (infer U)[] ? U : never;
10
5
  export type ComparablePrimitive = number | bigint | string | boolean;
11
6
  export interface BaseComparableObject {
12
7
  [key: string]: unknown;
@@ -20,3 +15,8 @@ export interface StringComparableObject extends BaseComparableObject {
20
15
  }
21
16
  export type ComparableObject = ValueComparableObject | StringComparableObject;
22
17
  export type Comparable = ComparablePrimitive | Date | ComparableObject;
18
+ export type TrampolineThunk<T> = {
19
+ readonly isThunk: true;
20
+ readonly fn: () => Trampoline<T>;
21
+ };
22
+ export type Trampoline<T> = T | TrampolineThunk<T>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toBinaryString = void 0;
3
+ exports.toBinaryString = toBinaryString;
4
4
  /**
5
5
  * The function `toBinaryString` converts a number to a binary string representation with a specified
6
6
  * number of digits.
@@ -21,4 +21,3 @@ function toBinaryString(num, digit = 32) {
21
21
  binaryString = binaryString.padStart(digit, '0');
22
22
  return binaryString;
23
23
  }
24
- exports.toBinaryString = toBinaryString;
@@ -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 { Comparable, Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
8
+ import type { Comparable, Trampoline, TrampolineThunk } from '../types';
9
9
  /**
10
10
  * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
11
11
  * @returns A randomly generated UUID (Universally Unique Identifier) in the format
@@ -23,54 +23,6 @@ export declare const uuidV4: () => string;
23
23
  * `predicate` function.
24
24
  */
25
25
  export declare const arrayRemove: <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean) => T[];
26
- export declare const THUNK_SYMBOL: unique symbol;
27
- /**
28
- * The function `isThunk` checks if a given value is a function with a specific symbol property.
29
- * @param {any} fnOrValue - The `fnOrValue` parameter in the `isThunk` function can be either a
30
- * function or a value that you want to check if it is a thunk. Thunks are functions that are wrapped
31
- * around a value or computation for lazy evaluation. The function checks if the `fnOrValue` is
32
- * @returns The function `isThunk` is checking if the input `fnOrValue` is a function and if it has a
33
- * property `__THUNK__` equal to `THUNK_SYMBOL`. The return value will be `true` if both conditions are
34
- * met, otherwise it will be `false`.
35
- */
36
- export declare const isThunk: (fnOrValue: any) => boolean;
37
- /**
38
- * The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
39
- * @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
40
- * @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
41
- * of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
42
- * function takes a function `fn` as an argument and returns a thunk function that, when called, will
43
- * execute the `fn` function provided as an argument.
44
- */
45
- export declare const toThunk: (fn: ToThunkFn) => Thunk;
46
- /**
47
- * The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
48
- * stack overflow.
49
- * @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
50
- * number of arguments and returns a value.
51
- * @returns The `trampoline` function returns an object with two properties:
52
- * 1. A function that executes the provided function `fn` and continues to execute any thunks returned
53
- * by `fn` until a non-thunk value is returned.
54
- * 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
55
- */
56
- export declare const trampoline: (fn: TrlFn) => ((...args: [...Parameters<TrlFn>]) => any) & {
57
- cont: (...args: [...Parameters<TrlFn>]) => ReturnType<TrlFn>;
58
- };
59
- /**
60
- * The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
61
- * function.
62
- * @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
63
- * function that returns a Promise. This function will be called recursively until a non-thunk value is
64
- * returned.
65
- * @returns The `trampolineAsync` function returns an object with two properties:
66
- * 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
67
- * thunks returned by the function until a non-thunk value is returned.
68
- * 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
69
- * and returns it.
70
- */
71
- export declare const trampolineAsync: (fn: TrlAsyncFn) => ((...args: [...Parameters<TrlAsyncFn>]) => Promise<any>) & {
72
- cont: (...args: [...Parameters<TrlAsyncFn>]) => ReturnType<TrlAsyncFn>;
73
- };
74
26
  /**
75
27
  * The function `getMSB` returns the most significant bit of a given number.
76
28
  * @param {number} value - The `value` parameter is a number for which we want to find the position of
@@ -146,3 +98,112 @@ export declare const roundFixed: (num: number, digit?: number) => number;
146
98
  * considered comparable or not.
147
99
  */
148
100
  export declare function isComparable(value: unknown, isForceObjectComparable?: boolean): value is Comparable;
101
+ /**
102
+ * Creates a trampoline thunk object.
103
+ *
104
+ * A "thunk" is a deferred computation — instead of performing a recursive call immediately,
105
+ * it wraps the next step of the computation in a function. This allows recursive processes
106
+ * to be executed iteratively, preventing stack overflows.
107
+ *
108
+ * @template T - The type of the final computation result.
109
+ * @param computation - A function that, when executed, returns the next trampoline step.
110
+ * @returns A TrampolineThunk object containing the deferred computation.
111
+ */
112
+ export declare const makeTrampolineThunk: <T>(computation: () => Trampoline<T>) => TrampolineThunk<T>;
113
+ /**
114
+ * Type guard to check whether a given value is a TrampolineThunk.
115
+ *
116
+ * This function is used to distinguish between a final computation result (value)
117
+ * and a deferred computation (thunk).
118
+ *
119
+ * @template T - The type of the value being checked.
120
+ * @param value - The value to test.
121
+ * @returns True if the value is a valid TrampolineThunk, false otherwise.
122
+ */
123
+ export declare const isTrampolineThunk: <T>(value: Trampoline<T>) => value is TrampolineThunk<T>;
124
+ /**
125
+ * Executes a trampoline computation until a final (non-thunk) result is obtained.
126
+ *
127
+ * The trampoline function repeatedly invokes the deferred computations (thunks)
128
+ * in an iterative loop. This avoids deep recursive calls and prevents stack overflow,
129
+ * which is particularly useful for implementing recursion in a stack-safe manner.
130
+ *
131
+ * @template T - The type of the final result.
132
+ * @param initial - The initial Trampoline value or thunk to start execution from.
133
+ * @returns The final result of the computation (a non-thunk value).
134
+ */
135
+ export declare function trampoline<T>(initial: Trampoline<T>): T;
136
+ /**
137
+ * Wraps a recursive function inside a trampoline executor.
138
+ *
139
+ * This function transforms a potentially recursive function (that returns a Trampoline<Result>)
140
+ * into a *stack-safe* function that executes iteratively using the `trampoline` runner.
141
+ *
142
+ * In other words, it allows you to write functions that look recursive,
143
+ * but actually run in constant stack space.
144
+ *
145
+ * @template Args - The tuple type representing the argument list of the original function.
146
+ * @template Result - The final return type after all trampoline steps are resolved.
147
+ *
148
+ * @param fn - A function that performs a single step of computation
149
+ * and returns a Trampoline (either a final value or a deferred thunk).
150
+ *
151
+ * @returns A new function with the same arguments, but which automatically
152
+ * runs the trampoline process and returns the *final result* instead
153
+ * of a Trampoline.
154
+ *
155
+ * @example
156
+ * // Example: Computing factorial in a stack-safe way
157
+ * const factorial = makeTrampoline(function fact(n: number, acc: number = 1): Trampoline<number> {
158
+ * return n === 0
159
+ * ? acc
160
+ * : makeTrampolineThunk(() => fact(n - 1, acc * n));
161
+ * });
162
+ *
163
+ * console.log(factorial(100000)); // Works without stack overflow
164
+ */
165
+ export declare function makeTrampoline<Args extends any[], Result>(fn: (...args: Args) => Trampoline<Result>): (...args: Args) => Result;
166
+ /**
167
+ * Executes an asynchronous trampoline computation until a final (non-thunk) result is obtained.
168
+ *
169
+ * This function repeatedly invokes asynchronous deferred computations (thunks)
170
+ * in an iterative loop. Each thunk may return either a Trampoline<T> or a Promise<Trampoline<T>>.
171
+ *
172
+ * It ensures that asynchronous recursive functions can run without growing the call stack,
173
+ * making it suitable for stack-safe async recursion.
174
+ *
175
+ * @template T - The type of the final result.
176
+ * @param initial - The initial Trampoline or Promise of Trampoline to start execution from.
177
+ * @returns A Promise that resolves to the final result (a non-thunk value).
178
+ */
179
+ export declare function asyncTrampoline<T>(initial: Trampoline<T> | Promise<Trampoline<T>>): Promise<T>;
180
+ /**
181
+ * Wraps an asynchronous recursive function inside an async trampoline executor.
182
+ *
183
+ * This helper transforms a recursive async function that returns a Trampoline<Result>
184
+ * (or Promise<Trampoline<Result>>) into a *stack-safe* async function that executes
185
+ * iteratively via the `asyncTrampoline` runner.
186
+ *
187
+ * @template Args - The tuple type representing the argument list of the original function.
188
+ * @template Result - The final return type after all async trampoline steps are resolved.
189
+ *
190
+ * @param fn - An async or sync function that performs a single step of computation
191
+ * and returns a Trampoline (either a final value or a deferred thunk).
192
+ *
193
+ * @returns An async function with the same arguments, but which automatically
194
+ * runs the trampoline process and resolves to the *final result*.
195
+ *
196
+ * @example
197
+ * // Example: Async factorial using trampoline
198
+ * const asyncFactorial = makeAsyncTrampoline(async function fact(
199
+ * n: number,
200
+ * acc: number = 1
201
+ * ): Promise<Trampoline<number>> {
202
+ * return n === 0
203
+ * ? acc
204
+ * : makeTrampolineThunk(() => fact(n - 1, acc * n));
205
+ * });
206
+ *
207
+ * asyncFactorial(100000).then(console.log); // Works without stack overflow
208
+ */
209
+ export declare function makeAsyncTrampoline<Args extends any[], Result>(fn: (...args: Args) => Trampoline<Result> | Promise<Trampoline<Result>>): (...args: Args) => Promise<Result>;
@@ -9,7 +9,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.isComparable = exports.roundFixed = exports.calcMinUnitsRequired = exports.isWeakKey = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
12
+ exports.isTrampolineThunk = exports.makeTrampolineThunk = exports.roundFixed = exports.calcMinUnitsRequired = exports.isWeakKey = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.arrayRemove = exports.uuidV4 = void 0;
13
+ exports.isComparable = isComparable;
14
+ exports.trampoline = trampoline;
15
+ exports.makeTrampoline = makeTrampoline;
16
+ exports.asyncTrampoline = asyncTrampoline;
17
+ exports.makeAsyncTrampoline = makeAsyncTrampoline;
13
18
  /**
14
19
  * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
15
20
  * @returns A randomly generated UUID (Universally Unique Identifier) in the format
@@ -46,78 +51,6 @@ const arrayRemove = function (array, predicate) {
46
51
  return result;
47
52
  };
48
53
  exports.arrayRemove = arrayRemove;
49
- exports.THUNK_SYMBOL = Symbol('thunk');
50
- /**
51
- * The function `isThunk` checks if a given value is a function with a specific symbol property.
52
- * @param {any} fnOrValue - The `fnOrValue` parameter in the `isThunk` function can be either a
53
- * function or a value that you want to check if it is a thunk. Thunks are functions that are wrapped
54
- * around a value or computation for lazy evaluation. The function checks if the `fnOrValue` is
55
- * @returns The function `isThunk` is checking if the input `fnOrValue` is a function and if it has a
56
- * property `__THUNK__` equal to `THUNK_SYMBOL`. The return value will be `true` if both conditions are
57
- * met, otherwise it will be `false`.
58
- */
59
- const isThunk = (fnOrValue) => {
60
- return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === exports.THUNK_SYMBOL;
61
- };
62
- exports.isThunk = isThunk;
63
- /**
64
- * The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
65
- * @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
66
- * @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
67
- * of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
68
- * function takes a function `fn` as an argument and returns a thunk function that, when called, will
69
- * execute the `fn` function provided as an argument.
70
- */
71
- const toThunk = (fn) => {
72
- const thunk = () => fn();
73
- thunk.__THUNK__ = exports.THUNK_SYMBOL;
74
- return thunk;
75
- };
76
- exports.toThunk = toThunk;
77
- /**
78
- * The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
79
- * stack overflow.
80
- * @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
81
- * number of arguments and returns a value.
82
- * @returns The `trampoline` function returns an object with two properties:
83
- * 1. A function that executes the provided function `fn` and continues to execute any thunks returned
84
- * by `fn` until a non-thunk value is returned.
85
- * 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
86
- */
87
- const trampoline = (fn) => {
88
- const cont = (...args) => (0, exports.toThunk)(() => fn(...args));
89
- return Object.assign((...args) => {
90
- let result = fn(...args);
91
- while ((0, exports.isThunk)(result) && typeof result === 'function') {
92
- result = result();
93
- }
94
- return result;
95
- }, { cont });
96
- };
97
- exports.trampoline = trampoline;
98
- /**
99
- * The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
100
- * function.
101
- * @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
102
- * function that returns a Promise. This function will be called recursively until a non-thunk value is
103
- * returned.
104
- * @returns The `trampolineAsync` function returns an object with two properties:
105
- * 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
106
- * thunks returned by the function until a non-thunk value is returned.
107
- * 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
108
- * and returns it.
109
- */
110
- const trampolineAsync = (fn) => {
111
- const cont = (...args) => (0, exports.toThunk)(() => fn(...args));
112
- return Object.assign((...args) => __awaiter(void 0, void 0, void 0, function* () {
113
- let result = yield fn(...args);
114
- while ((0, exports.isThunk)(result) && typeof result === 'function') {
115
- result = yield result();
116
- }
117
- return result;
118
- }), { cont });
119
- };
120
- exports.trampolineAsync = trampolineAsync;
121
54
  /**
122
55
  * The function `getMSB` returns the most significant bit of a given number.
123
56
  * @param {number} value - The `value` parameter is a number for which we want to find the position of
@@ -275,4 +208,146 @@ function isComparable(value, isForceObjectComparable = false) {
275
208
  return false;
276
209
  return isPrimitiveComparable(comparableValue);
277
210
  }
278
- exports.isComparable = isComparable;
211
+ /**
212
+ * Creates a trampoline thunk object.
213
+ *
214
+ * A "thunk" is a deferred computation — instead of performing a recursive call immediately,
215
+ * it wraps the next step of the computation in a function. This allows recursive processes
216
+ * to be executed iteratively, preventing stack overflows.
217
+ *
218
+ * @template T - The type of the final computation result.
219
+ * @param computation - A function that, when executed, returns the next trampoline step.
220
+ * @returns A TrampolineThunk object containing the deferred computation.
221
+ */
222
+ const makeTrampolineThunk = (computation) => ({
223
+ isThunk: true, // Marker indicating this is a thunk
224
+ fn: computation // The deferred computation function
225
+ });
226
+ exports.makeTrampolineThunk = makeTrampolineThunk;
227
+ /**
228
+ * Type guard to check whether a given value is a TrampolineThunk.
229
+ *
230
+ * This function is used to distinguish between a final computation result (value)
231
+ * and a deferred computation (thunk).
232
+ *
233
+ * @template T - The type of the value being checked.
234
+ * @param value - The value to test.
235
+ * @returns True if the value is a valid TrampolineThunk, false otherwise.
236
+ */
237
+ const isTrampolineThunk = (value) => typeof value === 'object' && // Must be an object
238
+ value !== null && // Must not be null
239
+ 'isThunk' in value && // Must have the 'isThunk' property
240
+ value.isThunk; // The flag must be true
241
+ exports.isTrampolineThunk = isTrampolineThunk;
242
+ /**
243
+ * Executes a trampoline computation until a final (non-thunk) result is obtained.
244
+ *
245
+ * The trampoline function repeatedly invokes the deferred computations (thunks)
246
+ * in an iterative loop. This avoids deep recursive calls and prevents stack overflow,
247
+ * which is particularly useful for implementing recursion in a stack-safe manner.
248
+ *
249
+ * @template T - The type of the final result.
250
+ * @param initial - The initial Trampoline value or thunk to start execution from.
251
+ * @returns The final result of the computation (a non-thunk value).
252
+ */
253
+ function trampoline(initial) {
254
+ let current = initial; // Start with the initial trampoline value
255
+ while ((0, exports.isTrampolineThunk)(current)) {
256
+ // Keep unwrapping while we have thunks
257
+ current = current.fn(); // Execute the deferred function to get the next step
258
+ }
259
+ return current; // Once no thunks remain, return the final result
260
+ }
261
+ /**
262
+ * Wraps a recursive function inside a trampoline executor.
263
+ *
264
+ * This function transforms a potentially recursive function (that returns a Trampoline<Result>)
265
+ * into a *stack-safe* function that executes iteratively using the `trampoline` runner.
266
+ *
267
+ * In other words, it allows you to write functions that look recursive,
268
+ * but actually run in constant stack space.
269
+ *
270
+ * @template Args - The tuple type representing the argument list of the original function.
271
+ * @template Result - The final return type after all trampoline steps are resolved.
272
+ *
273
+ * @param fn - A function that performs a single step of computation
274
+ * and returns a Trampoline (either a final value or a deferred thunk).
275
+ *
276
+ * @returns A new function with the same arguments, but which automatically
277
+ * runs the trampoline process and returns the *final result* instead
278
+ * of a Trampoline.
279
+ *
280
+ * @example
281
+ * // Example: Computing factorial in a stack-safe way
282
+ * const factorial = makeTrampoline(function fact(n: number, acc: number = 1): Trampoline<number> {
283
+ * return n === 0
284
+ * ? acc
285
+ * : makeTrampolineThunk(() => fact(n - 1, acc * n));
286
+ * });
287
+ *
288
+ * console.log(factorial(100000)); // Works without stack overflow
289
+ */
290
+ function makeTrampoline(fn // A function that returns a trampoline step
291
+ ) {
292
+ // Return a wrapped function that automatically runs the trampoline execution loop
293
+ return (...args) => trampoline(fn(...args));
294
+ }
295
+ /**
296
+ * Executes an asynchronous trampoline computation until a final (non-thunk) result is obtained.
297
+ *
298
+ * This function repeatedly invokes asynchronous deferred computations (thunks)
299
+ * in an iterative loop. Each thunk may return either a Trampoline<T> or a Promise<Trampoline<T>>.
300
+ *
301
+ * It ensures that asynchronous recursive functions can run without growing the call stack,
302
+ * making it suitable for stack-safe async recursion.
303
+ *
304
+ * @template T - The type of the final result.
305
+ * @param initial - The initial Trampoline or Promise of Trampoline to start execution from.
306
+ * @returns A Promise that resolves to the final result (a non-thunk value).
307
+ */
308
+ function asyncTrampoline(initial) {
309
+ return __awaiter(this, void 0, void 0, function* () {
310
+ let current = yield initial; // Wait for the initial step to resolve if it's a Promise
311
+ // Keep executing thunks until we reach a non-thunk (final) value
312
+ while ((0, exports.isTrampolineThunk)(current)) {
313
+ current = yield current.fn(); // Execute the thunk function (may be async)
314
+ }
315
+ // Once the final value is reached, return it
316
+ return current;
317
+ });
318
+ }
319
+ /**
320
+ * Wraps an asynchronous recursive function inside an async trampoline executor.
321
+ *
322
+ * This helper transforms a recursive async function that returns a Trampoline<Result>
323
+ * (or Promise<Trampoline<Result>>) into a *stack-safe* async function that executes
324
+ * iteratively via the `asyncTrampoline` runner.
325
+ *
326
+ * @template Args - The tuple type representing the argument list of the original function.
327
+ * @template Result - The final return type after all async trampoline steps are resolved.
328
+ *
329
+ * @param fn - An async or sync function that performs a single step of computation
330
+ * and returns a Trampoline (either a final value or a deferred thunk).
331
+ *
332
+ * @returns An async function with the same arguments, but which automatically
333
+ * runs the trampoline process and resolves to the *final result*.
334
+ *
335
+ * @example
336
+ * // Example: Async factorial using trampoline
337
+ * const asyncFactorial = makeAsyncTrampoline(async function fact(
338
+ * n: number,
339
+ * acc: number = 1
340
+ * ): Promise<Trampoline<number>> {
341
+ * return n === 0
342
+ * ? acc
343
+ * : makeTrampolineThunk(() => fact(n - 1, acc * n));
344
+ * });
345
+ *
346
+ * asyncFactorial(100000).then(console.log); // Works without stack overflow
347
+ */
348
+ function makeAsyncTrampoline(fn) {
349
+ // Return a wrapped async function that runs through the async trampoline loop
350
+ return (...args) => __awaiter(this, void 0, void 0, function* () {
351
+ return asyncTrampoline(fn(...args));
352
+ });
353
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "avl-tree-typed",
3
- "version": "2.0.4",
3
+ "version": "2.1.0",
4
4
  "description": "Standard AVL tree",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -147,22 +147,22 @@
147
147
  "homepage": "https://data-structure-typed-docs.vercel.app",
148
148
  "types": "dist/index.d.ts",
149
149
  "devDependencies": {
150
- "@types/jest": "^29.5.3",
151
- "@types/node": "^20.4.9",
152
- "@typescript-eslint/eslint-plugin": "^5.6.0",
153
- "@typescript-eslint/parser": "^5.11.0",
154
- "eslint": "^7.32.0",
155
- "eslint-config-prettier": "^8.3.0",
150
+ "@types/jest": "^30.0.0",
151
+ "@types/node": "^24.9.1",
152
+ "@typescript-eslint/eslint-plugin": "^8.46.2",
153
+ "@typescript-eslint/parser": "^8.46.2",
154
+ "eslint": "^9.38.0",
155
+ "eslint-config-prettier": "^10.1.8",
156
156
  "eslint-import-resolver-alias": "^1.1.2",
157
- "eslint-import-resolver-typescript": "^2.5.0",
158
- "eslint-plugin-import": "^2.25.4",
159
- "jest": "^29.6.2",
160
- "prettier": "^3.0.3",
161
- "ts-jest": "^29.1.1",
162
- "typedoc": "^0.25.1",
163
- "typescript": "^4.9.5"
157
+ "eslint-import-resolver-typescript": "^4.4.4",
158
+ "eslint-plugin-import": "^2.32.0",
159
+ "jest": "^30.2.0",
160
+ "prettier": "^3.6.2",
161
+ "ts-jest": "^29.4.5",
162
+ "typedoc": "^0.28.14",
163
+ "typescript": "^5.9.3"
164
164
  },
165
165
  "dependencies": {
166
- "data-structure-typed": "^2.0.4"
166
+ "data-structure-typed": "^2.1.0"
167
167
  }
168
168
  }