stack-typed 2.0.5 → 2.1.1

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 (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  72. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  74. package/src/data-structures/binary-tree/bst.ts +565 -572
  75. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  76. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -5,9 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback, StackOptions } from '../../types';
8
+ import type { ElementCallback, IterableElementBaseOptions, StackOptions } from '../../types';
9
9
  import { IterableElementBase } from '../base';
10
10
  /**
11
+ * LIFO stack with array storage and optional record→element conversion.
12
+ * @remarks Time O(1), Space O(1)
13
+ * @template E
14
+ * @template R
11
15
  * 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
12
16
  * 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
13
17
  * 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
@@ -137,148 +141,166 @@ import { IterableElementBase } from '../base';
137
141
  * console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
138
142
  */
139
143
  export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
144
+ protected _equals: (a: E, b: E) => boolean;
145
+ /**
146
+ * Create a Stack and optionally bulk-push elements.
147
+ * @remarks Time O(N), Space O(N)
148
+ * @param [elements] - Iterable of elements (or raw records if toElementFn is set).
149
+ * @param [options] - Options such as toElementFn and equality function.
150
+ * @returns New Stack instance.
151
+ */
140
152
  constructor(elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>);
141
153
  protected _elements: E[];
142
154
  /**
143
- * The elements function returns the elements of this set.
144
- * @return An array of elements
155
+ * Get the backing array of elements.
156
+ * @remarks Time O(1), Space O(1)
157
+ * @returns Internal elements array.
145
158
  */
146
159
  get elements(): E[];
147
160
  /**
148
- * The size() function returns the number of elements in an array.
149
- * @returns The size of the elements array.
161
+ * Get the number of stored elements.
162
+ * @remarks Time O(1), Space O(1)
163
+ * @returns Current size.
150
164
  */
151
165
  get size(): number;
152
166
  /**
153
- * Time Complexity: O(n)
154
- * Space Complexity: O(n)
155
- *
156
- * The function "fromArray" creates a new Stack object from an array of elements.
157
- * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
158
- * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
159
- * array.
167
+ * Create a stack from an array of elements.
168
+ * @remarks Time O(N), Space O(N)
169
+ * @template E
170
+ * @template R
171
+ * @param this - The constructor (subclass) to instantiate.
172
+ * @param elements - Array of elements to push in order.
173
+ * @param [options] - Options forwarded to the constructor.
174
+ * @returns A new Stack populated from the array.
160
175
  */
161
- static fromArray<E>(elements: E[]): Stack<E>;
176
+ static fromArray<E, R = any>(this: new (elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>) => any, elements: E[], options?: StackOptions<E, R>): any;
162
177
  /**
163
- * Time Complexity: O(1)
164
- * Space Complexity: O(1)
165
- *
166
- * The function checks if an array is empty and returns a boolean value.
167
- * @returns A boolean value indicating whether the `_elements` array is empty or not.
178
+ * Check whether the stack is empty.
179
+ * @remarks Time O(1), Space O(1)
180
+ * @returns True if size is 0.
168
181
  */
169
182
  isEmpty(): boolean;
170
183
  /**
171
- * Time Complexity: O(1)
172
- * Space Complexity: O(1)
173
- *
174
- * The `peek` function returns the last element of an array, or undefined if the array is empty.
175
- * @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
184
+ * Get the top element without removing it.
185
+ * @remarks Time O(1), Space O(1)
186
+ * @returns Top element or undefined.
176
187
  */
177
188
  peek(): E | undefined;
178
189
  /**
179
- * Time Complexity: O(1)
180
- * Space Complexity: O(1)
181
- *
182
- * The push function adds an element to the stack and returns the updated stack.
183
- * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
184
- * @returns The `push` method is returning the updated `Stack<E>` object.
190
+ * Push one element onto the top.
191
+ * @remarks Time O(1), Space O(1)
192
+ * @param element - Element to push.
193
+ * @returns True when pushed.
185
194
  */
186
195
  push(element: E): boolean;
187
196
  /**
188
- * Time Complexity: O(1)
189
- * Space Complexity: O(1)
190
- *
191
- * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
192
- * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
193
- * array is empty, it returns `undefined`.
197
+ * Pop and return the top element.
198
+ * @remarks Time O(1), Space O(1)
199
+ * @returns Removed element or undefined.
194
200
  */
195
201
  pop(): E | undefined;
196
202
  /**
197
- * Time Complexity: O(k)
198
- * Space Complexity: O(1)
199
- *
200
- * The function `pushMany` iterates over elements and pushes them into an array after applying a
201
- * transformation function if provided.
202
- * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
203
- * is an iterable containing elements of type `E` or `R`. The function iterates over each element in
204
- * the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
205
- * provided, it is used to
206
- * @returns The `pushMany` function is returning an array of boolean values indicating whether each
207
- * element was successfully pushed into the data structure.
203
+ * Push many elements from an iterable.
204
+ * @remarks Time O(N), Space O(1)
205
+ * @param elements - Iterable of elements (or raw records if toElementFn is set).
206
+ * @returns Array of per-element success flags.
208
207
  */
209
208
  pushMany(elements: Iterable<E> | Iterable<R>): boolean[];
210
209
  /**
211
- * Time Complexity: O(n)
212
- * Space Complexity: O(1)
213
- *
214
- * The toArray function returns a copy of the elements in an array.
215
- * @returns An array of type E.
210
+ * Delete the first occurrence of a specific element.
211
+ * @remarks Time O(N), Space O(1)
212
+ * @param element - Element to remove (using the configured equality).
213
+ * @returns True if an element was removed.
216
214
  */
217
215
  delete(element: E): boolean;
218
216
  /**
219
- * Time Complexity: O(n)
220
- * Space Complexity: O(1)
221
- *
222
- * The toArray function returns a copy of the elements in an array.
223
- * @returns An array of type E.
217
+ * Delete the element at an index.
218
+ * @remarks Time O(N), Space O(1)
219
+ * @param index - Zero-based index from the bottom.
220
+ * @returns True if removed.
224
221
  */
225
222
  deleteAt(index: number): boolean;
226
223
  /**
227
- * Time Complexity: O(1)
228
- * Space Complexity: O(1)
229
- *
230
- * The clear function clears the elements array.
224
+ * Delete the first element that satisfies a predicate.
225
+ * @remarks Time O(N), Space O(1)
226
+ * @param predicate - Function (value, index, stack) → boolean to decide deletion.
227
+ * @returns True if a match was removed.
228
+ */
229
+ deleteWhere(predicate: (value: E, index: number, stack: this) => boolean): boolean;
230
+ /**
231
+ * Remove all elements and reset storage.
232
+ * @remarks Time O(1), Space O(1)
233
+ * @returns void
231
234
  */
232
235
  clear(): void;
233
236
  /**
234
- * Time Complexity: O(n)
235
- * Space Complexity: O(n)
236
- *
237
- * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
238
- * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
237
+ * Deep clone this stack.
238
+ * @remarks Time O(N), Space O(N)
239
+ * @returns A new stack with the same content.
240
+ */
241
+ clone(): this;
242
+ /**
243
+ * Filter elements into a new stack of the same class.
244
+ * @remarks Time O(N), Space O(N)
245
+ * @param predicate - Predicate (value, index, stack) → boolean to keep value.
246
+ * @param [thisArg] - Value for `this` inside the predicate.
247
+ * @returns A new stack with kept values.
248
+ */
249
+ filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
250
+ /**
251
+ * Map values into a new stack of the same element type.
252
+ * @remarks Time O(N), Space O(N)
253
+ * @param callback - Mapping function (value, index, stack) → newValue.
254
+ * @param [thisArg] - Value for `this` inside the callback.
255
+ * @returns A new stack with mapped values.
256
+ */
257
+ mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
258
+ /**
259
+ * Map values into a new stack (possibly different element type).
260
+ * @remarks Time O(N), Space O(N)
261
+ * @template EM
262
+ * @template RM
263
+ * @param callback - Mapping function (value, index, stack) → newElement.
264
+ * @param [options] - Options for the output stack (e.g., toElementFn).
265
+ * @param [thisArg] - Value for `this` inside the callback.
266
+ * @returns A new Stack with mapped elements.
267
+ */
268
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: IterableElementBaseOptions<EM, RM>, thisArg?: unknown): Stack<EM, RM>;
269
+ /**
270
+ * Set the equality comparator used by delete/search operations.
271
+ * @remarks Time O(1), Space O(1)
272
+ * @param equals - Equality predicate (a, b) → boolean.
273
+ * @returns This stack.
274
+ */
275
+ setEquality(equals: (a: E, b: E) => boolean): this;
276
+ /**
277
+ * (Protected) Find the index of a target element using the equality function.
278
+ * @remarks Time O(N), Space O(1)
279
+ * @param target - Element to search for.
280
+ * @returns Index or -1 if not found.
239
281
  */
240
- clone(): Stack<E, R>;
282
+ protected _indexOfByEquals(target: E): number;
241
283
  /**
242
- * Time Complexity: O(n)
243
- * Space Complexity: O(n)
244
- *
245
- * The `filter` function creates a new stack containing elements from the original stack that satisfy
246
- * a given predicate function.
247
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
248
- * the current element being iterated over, the index of the current element, and the stack itself.
249
- * It should return a boolean value indicating whether the element should be included in the filtered
250
- * stack or not.
251
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
252
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
253
- * passed as the `this` value to the `predicate` function. If `thisArg` is
254
- * @returns The `filter` method is returning a new `Stack` object that contains the elements that
255
- * satisfy the given predicate function.
284
+ * (Protected) Create an empty instance of the same concrete class.
285
+ * @remarks Time O(1), Space O(1)
286
+ * @param [options] - Options forwarded to the constructor.
287
+ * @returns An empty like-kind stack instance.
256
288
  */
257
- filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Stack<E, R>;
289
+ protected _createInstance(options?: StackOptions<E, R>): this;
258
290
  /**
259
- * Time Complexity: O(n)
260
- * Space Complexity: O(n)
261
- *
262
- * The `map` function takes a callback function and applies it to each element in the stack,
263
- * returning a new stack with the results.
264
- * @param callback - The callback parameter is a function that will be called for each element in the
265
- * stack. It takes three arguments: the current element, the index of the element, and the stack
266
- * itself. It should return a new value that will be added to the new stack.
267
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
268
- * transform the raw element (`RM`) into a new element (`EM`) before pushing it into the new stack.
269
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
270
- * specify the value of `this` within the callback function. It is used to set the context or scope
271
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
272
- * value of
273
- * @returns a new Stack object with elements of type EM and raw elements of type RM.
291
+ * (Protected) Create a like-kind stack and seed it from an iterable.
292
+ * @remarks Time O(N), Space O(N)
293
+ * @template T
294
+ * @template RR
295
+ * @param [elements] - Iterable used to seed the new stack.
296
+ * @param [options] - Options forwarded to the constructor.
297
+ * @returns A like-kind Stack instance.
274
298
  */
275
- map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Stack<EM, RM>;
299
+ protected _createLike<T = E, RR = R>(elements?: Iterable<T> | Iterable<RR>, options?: StackOptions<T, RR>): Stack<T, RR>;
276
300
  /**
277
- * Time Complexity: O(n)
278
- * Space Complexity: O(n)
279
- *
280
- * Custom iterator for the Stack class.
281
- * @returns An iterator object.
301
+ * (Protected) Iterate elements from bottom to top.
302
+ * @remarks Time O(N), Space O(1)
303
+ * @returns Iterator of elements.
282
304
  */
283
305
  protected _getIterator(): IterableIterator<E>;
284
306
  }