min-heap-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.
- package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +602 -873
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +2 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
- package/src/data-structures/binary-tree/avl-tree.ts +237 -206
- package/src/data-structures/binary-tree/binary-tree.ts +665 -896
- package/src/data-structures/binary-tree/bst.ts +565 -572
- package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
- package/src/data-structures/binary-tree/tree-counter.ts +195 -219
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- 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
|
-
*
|
|
144
|
-
* @
|
|
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
|
-
*
|
|
149
|
-
* @
|
|
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
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* @param
|
|
158
|
-
* @
|
|
159
|
-
*
|
|
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[]):
|
|
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
|
-
*
|
|
164
|
-
*
|
|
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
|
-
*
|
|
172
|
-
*
|
|
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
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
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
|
-
*
|
|
189
|
-
*
|
|
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
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
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
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
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
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
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
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
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
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
|
|
238
|
-
|
|
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
|
-
|
|
282
|
+
protected _indexOfByEquals(target: E): number;
|
|
241
283
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
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
|
-
|
|
289
|
+
protected _createInstance(options?: StackOptions<E, R>): this;
|
|
258
290
|
/**
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
* @param
|
|
265
|
-
*
|
|
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
|
-
|
|
299
|
+
protected _createLike<T = E, RR = R>(elements?: Iterable<T> | Iterable<RR>, options?: StackOptions<T, RR>): Stack<T, RR>;
|
|
276
300
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
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
|
}
|