data-structure-typed 2.5.3 → 2.6.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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +1 -1
- package/MIGRATION.md +48 -0
- package/README.md +20 -2
- package/README_CN.md +20 -2
- package/SPECIFICATION.md +24 -0
- package/SPECIFICATION.zh-CN.md +24 -0
- package/dist/cjs/binary-tree.cjs +1897 -19
- package/dist/cjs/graph.cjs +174 -0
- package/dist/cjs/hash.cjs +33 -0
- package/dist/cjs/heap.cjs +71 -0
- package/dist/cjs/index.cjs +2383 -3
- package/dist/cjs/linked-list.cjs +224 -2
- package/dist/cjs/matrix.cjs +24 -0
- package/dist/cjs/priority-queue.cjs +71 -0
- package/dist/cjs/queue.cjs +221 -1
- package/dist/cjs/stack.cjs +59 -0
- package/dist/cjs/trie.cjs +62 -0
- package/dist/cjs-legacy/binary-tree.cjs +1897 -19
- package/dist/cjs-legacy/graph.cjs +174 -0
- package/dist/cjs-legacy/hash.cjs +33 -0
- package/dist/cjs-legacy/heap.cjs +71 -0
- package/dist/cjs-legacy/index.cjs +2383 -3
- package/dist/cjs-legacy/linked-list.cjs +224 -2
- package/dist/cjs-legacy/matrix.cjs +24 -0
- package/dist/cjs-legacy/priority-queue.cjs +71 -0
- package/dist/cjs-legacy/queue.cjs +221 -1
- package/dist/cjs-legacy/stack.cjs +59 -0
- package/dist/cjs-legacy/trie.cjs +62 -0
- package/dist/esm/binary-tree.mjs +1897 -19
- package/dist/esm/graph.mjs +174 -0
- package/dist/esm/hash.mjs +33 -0
- package/dist/esm/heap.mjs +71 -0
- package/dist/esm/index.mjs +2383 -3
- package/dist/esm/linked-list.mjs +224 -2
- package/dist/esm/matrix.mjs +24 -0
- package/dist/esm/priority-queue.mjs +71 -0
- package/dist/esm/queue.mjs +221 -1
- package/dist/esm/stack.mjs +59 -0
- package/dist/esm/trie.mjs +62 -0
- package/dist/esm-legacy/binary-tree.mjs +1897 -19
- package/dist/esm-legacy/graph.mjs +174 -0
- package/dist/esm-legacy/hash.mjs +33 -0
- package/dist/esm-legacy/heap.mjs +71 -0
- package/dist/esm-legacy/index.mjs +2383 -3
- package/dist/esm-legacy/linked-list.mjs +224 -2
- package/dist/esm-legacy/matrix.mjs +24 -0
- package/dist/esm-legacy/priority-queue.mjs +71 -0
- package/dist/esm-legacy/queue.mjs +221 -1
- package/dist/esm-legacy/stack.mjs +59 -0
- package/dist/esm-legacy/trie.mjs +62 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +90 -1
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/umd/data-structure-typed.js +2383 -3
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/jest.integration.config.js +1 -2
- package/package.json +51 -50
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +32 -3
- package/src/data-structures/base/linear-base.ts +13 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -493
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
- package/src/data-structures/binary-tree/bst.ts +158 -1010
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
- package/src/data-structures/binary-tree/segment-tree.ts +73 -333
- package/src/data-structures/binary-tree/tree-map.ts +462 -4749
- package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
- package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
- package/src/data-structures/binary-tree/tree-set.ts +437 -4443
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -532
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -484
- package/src/data-structures/hash/hash-map.ts +154 -549
- package/src/data-structures/heap/heap.ts +200 -753
- package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
- package/src/data-structures/matrix/matrix.ts +179 -494
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +241 -807
- package/src/data-structures/queue/queue.ts +102 -589
- package/src/data-structures/stack/stack.ts +76 -475
- package/src/data-structures/trie/trie.ts +98 -592
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
import type { ElementCallback, IterableElementBaseOptions, StackOptions } from '../../types';
|
|
10
9
|
import { IterableElementBase } from '../base';
|
|
11
10
|
|
|
@@ -133,8 +132,6 @@ import { IterableElementBase } from '../base';
|
|
|
133
132
|
* console.log(stack.toArray()); // [1, 2, 3];
|
|
134
133
|
*/
|
|
135
134
|
export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
136
|
-
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
137
|
-
|
|
138
135
|
/**
|
|
139
136
|
* Create a Stack and optionally bulk-push elements.
|
|
140
137
|
* @remarks Time O(N), Space O(N)
|
|
@@ -142,7 +139,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
142
139
|
* @param [options] - Options such as toElementFn and equality function.
|
|
143
140
|
* @returns New Stack instance.
|
|
144
141
|
*/
|
|
145
|
-
|
|
146
142
|
constructor(elements: Iterable<E> | Iterable<R> = [], options?: StackOptions<E, R>) {
|
|
147
143
|
super(options);
|
|
148
144
|
this.pushMany(elements);
|
|
@@ -155,7 +151,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
155
151
|
* @remarks Time O(1), Space O(1)
|
|
156
152
|
* @returns Internal elements array.
|
|
157
153
|
*/
|
|
158
|
-
|
|
159
154
|
get elements(): E[] {
|
|
160
155
|
return this._elements;
|
|
161
156
|
}
|
|
@@ -164,49 +159,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
164
159
|
* Get the number of stored elements.
|
|
165
160
|
* @remarks Time O(1), Space O(1)
|
|
166
161
|
* @returns Current size.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
* @example
|
|
205
|
-
* // Get number of elements
|
|
206
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
207
|
-
* console.log(stack.size); // 3;
|
|
162
|
+
* @example
|
|
163
|
+
* // Get number of elements
|
|
164
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
165
|
+
* console.log(stack.size); // 3;
|
|
208
166
|
*/
|
|
209
|
-
|
|
210
167
|
get size(): number {
|
|
211
168
|
return this.elements.length;
|
|
212
169
|
}
|
|
@@ -221,7 +178,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
221
178
|
* @param [options] - Options forwarded to the constructor.
|
|
222
179
|
* @returns A new Stack populated from the array.
|
|
223
180
|
*/
|
|
224
|
-
|
|
225
181
|
static fromArray<E, R = any>(
|
|
226
182
|
this: new (elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>) => any,
|
|
227
183
|
elements: E[],
|
|
@@ -234,53 +190,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
234
190
|
* Check whether the stack is empty.
|
|
235
191
|
* @remarks Time O(1), Space O(1)
|
|
236
192
|
* @returns True if size is 0.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
* @example
|
|
277
|
-
* // Check if stack has elements
|
|
278
|
-
* const stack = new Stack<number>();
|
|
279
|
-
* console.log(stack.isEmpty()); // true;
|
|
280
|
-
* stack.push(1);
|
|
281
|
-
* console.log(stack.isEmpty()); // false;
|
|
193
|
+
* @example
|
|
194
|
+
* // Check if stack has elements
|
|
195
|
+
* const stack = new Stack<number>();
|
|
196
|
+
* console.log(stack.isEmpty()); // true;
|
|
197
|
+
* stack.push(1);
|
|
198
|
+
* console.log(stack.isEmpty()); // false;
|
|
282
199
|
*/
|
|
283
|
-
|
|
284
200
|
isEmpty(): boolean {
|
|
285
201
|
return this.elements.length === 0;
|
|
286
202
|
}
|
|
@@ -289,52 +205,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
289
205
|
* Get the top element without removing it.
|
|
290
206
|
* @remarks Time O(1), Space O(1)
|
|
291
207
|
* @returns Top element or undefined.
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
* @example
|
|
332
|
-
* // View the top element without removing it
|
|
333
|
-
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
334
|
-
* console.log(stack.peek()); // 'c';
|
|
335
|
-
* console.log(stack.size); // 3;
|
|
208
|
+
* @example
|
|
209
|
+
* // View the top element without removing it
|
|
210
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
211
|
+
* console.log(stack.peek()); // 'c';
|
|
212
|
+
* console.log(stack.size); // 3;
|
|
336
213
|
*/
|
|
337
|
-
|
|
338
214
|
peek(): E | undefined {
|
|
339
215
|
return this.isEmpty() ? undefined : this.elements[this.elements.length - 1];
|
|
340
216
|
}
|
|
@@ -344,61 +220,21 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
344
220
|
* @remarks Time O(1), Space O(1)
|
|
345
221
|
* @param element - Element to push.
|
|
346
222
|
* @returns True when pushed.
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
* @example
|
|
387
|
-
* // basic Stack creation and push operation
|
|
388
|
-
* // Create a simple Stack with initial values
|
|
389
|
-
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
390
|
-
*
|
|
391
|
-
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
392
|
-
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
393
|
-
*
|
|
394
|
-
* // Check length
|
|
395
|
-
* console.log(stack.size); // 5;
|
|
396
|
-
*
|
|
397
|
-
* // Push a new element to the top
|
|
398
|
-
* stack.push(6);
|
|
399
|
-
* console.log(stack.size); // 6;
|
|
223
|
+
* @example
|
|
224
|
+
* // basic Stack creation and push operation
|
|
225
|
+
* // Create a simple Stack with initial values
|
|
226
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
227
|
+
*
|
|
228
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
229
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
230
|
+
*
|
|
231
|
+
* // Check length
|
|
232
|
+
* console.log(stack.size); // 5;
|
|
233
|
+
*
|
|
234
|
+
* // Push a new element to the top
|
|
235
|
+
* stack.push(6);
|
|
236
|
+
* console.log(stack.size); // 6;
|
|
400
237
|
*/
|
|
401
|
-
|
|
402
238
|
push(element: E): boolean {
|
|
403
239
|
this.elements.push(element);
|
|
404
240
|
return true;
|
|
@@ -408,65 +244,25 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
408
244
|
* Pop and return the top element.
|
|
409
245
|
* @remarks Time O(1), Space O(1)
|
|
410
246
|
* @returns Removed element or undefined.
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
* @example
|
|
451
|
-
* // Stack pop operation (LIFO - Last In First Out)
|
|
452
|
-
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
453
|
-
*
|
|
454
|
-
* // Peek at the top element without removing
|
|
455
|
-
* const top = stack.peek();
|
|
456
|
-
* console.log(top); // 50;
|
|
457
|
-
*
|
|
458
|
-
* // Pop removes from the top (LIFO order)
|
|
459
|
-
* const popped = stack.pop();
|
|
460
|
-
* console.log(popped); // 50;
|
|
461
|
-
*
|
|
462
|
-
* // Next pop gets the previous element
|
|
463
|
-
* const next = stack.pop();
|
|
464
|
-
* console.log(next); // 40;
|
|
465
|
-
*
|
|
466
|
-
* // Verify length decreased
|
|
467
|
-
* console.log(stack.size); // 3;
|
|
247
|
+
* @example
|
|
248
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
249
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
250
|
+
*
|
|
251
|
+
* // Peek at the top element without removing
|
|
252
|
+
* const top = stack.peek();
|
|
253
|
+
* console.log(top); // 50;
|
|
254
|
+
*
|
|
255
|
+
* // Pop removes from the top (LIFO order)
|
|
256
|
+
* const popped = stack.pop();
|
|
257
|
+
* console.log(popped); // 50;
|
|
258
|
+
*
|
|
259
|
+
* // Next pop gets the previous element
|
|
260
|
+
* const next = stack.pop();
|
|
261
|
+
* console.log(next); // 40;
|
|
262
|
+
*
|
|
263
|
+
* // Verify length decreased
|
|
264
|
+
* console.log(stack.size); // 3;
|
|
468
265
|
*/
|
|
469
|
-
|
|
470
266
|
pop(): E | undefined {
|
|
471
267
|
return this.isEmpty() ? undefined : this.elements.pop();
|
|
472
268
|
}
|
|
@@ -477,7 +273,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
477
273
|
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
478
274
|
* @returns Array of per-element success flags.
|
|
479
275
|
*/
|
|
480
|
-
|
|
481
276
|
pushMany(elements: Iterable<E> | Iterable<R>): boolean[] {
|
|
482
277
|
const ans: boolean[] = [];
|
|
483
278
|
for (const el of elements) {
|
|
@@ -492,49 +287,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
492
287
|
* @remarks Time O(N), Space O(1)
|
|
493
288
|
* @param element - Element to remove (using the configured equality).
|
|
494
289
|
* @returns True if an element was removed.
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
* @example
|
|
532
|
-
* // Remove element
|
|
533
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
534
|
-
* stack.delete(2);
|
|
535
|
-
* console.log(stack.toArray()); // [1, 3];
|
|
290
|
+
* @example
|
|
291
|
+
* // Remove element
|
|
292
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
293
|
+
* stack.delete(2);
|
|
294
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
536
295
|
*/
|
|
537
|
-
|
|
538
296
|
delete(element: E): boolean {
|
|
539
297
|
const idx = this._indexOfByEquals(element);
|
|
540
298
|
return this.deleteAt(idx) !== undefined;
|
|
@@ -546,7 +304,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
546
304
|
* @param index - Zero-based index from the bottom.
|
|
547
305
|
* @returns The removed element, or undefined if the index is out of range.
|
|
548
306
|
*/
|
|
549
|
-
|
|
550
307
|
deleteAt(index: number): E | undefined {
|
|
551
308
|
if (index < 0 || index >= this.elements.length) return undefined;
|
|
552
309
|
const spliced = this.elements.splice(index, 1);
|
|
@@ -559,7 +316,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
559
316
|
* @param predicate - Function (value, index, stack) → boolean to decide deletion.
|
|
560
317
|
* @returns True if a match was removed.
|
|
561
318
|
*/
|
|
562
|
-
|
|
563
319
|
deleteWhere(predicate: (value: E, index: number, stack: this) => boolean): boolean {
|
|
564
320
|
for (let i = 0; i < this.elements.length; i++) {
|
|
565
321
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -574,50 +330,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
574
330
|
* Remove all elements and reset storage.
|
|
575
331
|
* @remarks Time O(1), Space O(1)
|
|
576
332
|
* @returns void
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
* @example
|
|
615
|
-
* // Remove all elements
|
|
616
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
617
|
-
* stack.clear();
|
|
618
|
-
* console.log(stack.isEmpty()); // true;
|
|
333
|
+
* @example
|
|
334
|
+
* // Remove all elements
|
|
335
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
336
|
+
* stack.clear();
|
|
337
|
+
* console.log(stack.isEmpty()); // true;
|
|
619
338
|
*/
|
|
620
|
-
|
|
621
339
|
clear(): void {
|
|
622
340
|
this._elements = [];
|
|
623
341
|
}
|
|
@@ -626,52 +344,14 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
626
344
|
* Deep clone this stack.
|
|
627
345
|
* @remarks Time O(N), Space O(N)
|
|
628
346
|
* @returns A new stack with the same content.
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
* @example
|
|
667
|
-
* // Create independent copy
|
|
668
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
669
|
-
* const copy = stack.clone();
|
|
670
|
-
* copy.pop();
|
|
671
|
-
* console.log(stack.size); // 3;
|
|
672
|
-
* console.log(copy.size); // 2;
|
|
347
|
+
* @example
|
|
348
|
+
* // Create independent copy
|
|
349
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
350
|
+
* const copy = stack.clone();
|
|
351
|
+
* copy.pop();
|
|
352
|
+
* console.log(stack.size); // 3;
|
|
353
|
+
* console.log(copy.size); // 2;
|
|
673
354
|
*/
|
|
674
|
-
|
|
675
355
|
clone(): this {
|
|
676
356
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
677
357
|
for (const v of this) out.push(v);
|
|
@@ -684,50 +364,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
684
364
|
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
685
365
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
686
366
|
* @returns A new stack with kept values.
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
* @example
|
|
725
|
-
* // Filter elements
|
|
726
|
-
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
727
|
-
* const evens = stack.filter(x => x % 2 === 0);
|
|
728
|
-
* console.log(evens.toArray()); // [2, 4];
|
|
367
|
+
* @example
|
|
368
|
+
* // Filter elements
|
|
369
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
370
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
371
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
729
372
|
*/
|
|
730
|
-
|
|
731
373
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
732
374
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
733
375
|
let index = 0;
|
|
@@ -745,7 +387,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
745
387
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
746
388
|
* @returns A new stack with mapped values.
|
|
747
389
|
*/
|
|
748
|
-
|
|
749
390
|
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
|
|
750
391
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
751
392
|
let index = 0;
|
|
@@ -765,49 +406,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
765
406
|
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
766
407
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
767
408
|
* @returns A new Stack with mapped elements.
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
* @example
|
|
805
|
-
* // Transform elements
|
|
806
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
807
|
-
* const doubled = stack.map(x => x * 2);
|
|
808
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
409
|
+
* @example
|
|
410
|
+
* // Transform elements
|
|
411
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
412
|
+
* const doubled = stack.map(x => x * 2);
|
|
413
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
809
414
|
*/
|
|
810
|
-
|
|
811
415
|
map<EM, RM>(
|
|
812
416
|
callback: ElementCallback<E, R, EM>,
|
|
813
417
|
options?: IterableElementBaseOptions<EM, RM>,
|
|
@@ -828,19 +432,19 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
828
432
|
* @param equals - Equality predicate (a, b) → boolean.
|
|
829
433
|
* @returns This stack.
|
|
830
434
|
*/
|
|
831
|
-
|
|
832
435
|
setEquality(equals: (a: E, b: E) => boolean): this {
|
|
833
436
|
this._equals = equals;
|
|
834
437
|
return this;
|
|
835
438
|
}
|
|
836
439
|
|
|
440
|
+
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
441
|
+
|
|
837
442
|
/**
|
|
838
443
|
* (Protected) Find the index of a target element using the equality function.
|
|
839
444
|
* @remarks Time O(N), Space O(1)
|
|
840
445
|
* @param target - Element to search for.
|
|
841
446
|
* @returns Index or -1 if not found.
|
|
842
447
|
*/
|
|
843
|
-
|
|
844
448
|
protected _indexOfByEquals(target: E): number {
|
|
845
449
|
for (let i = 0; i < this.elements.length; i++) if (this._equals(this.elements[i], target)) return i;
|
|
846
450
|
return -1;
|
|
@@ -852,7 +456,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
852
456
|
* @param [options] - Options forwarded to the constructor.
|
|
853
457
|
* @returns An empty like-kind stack instance.
|
|
854
458
|
*/
|
|
855
|
-
|
|
856
459
|
protected _createInstance(options?: StackOptions<E, R>): this {
|
|
857
460
|
const Ctor = this.constructor as new (elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>) => this;
|
|
858
461
|
return new Ctor([], options);
|
|
@@ -867,7 +470,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
867
470
|
* @param [options] - Options forwarded to the constructor.
|
|
868
471
|
* @returns A like-kind Stack instance.
|
|
869
472
|
*/
|
|
870
|
-
|
|
871
473
|
protected _createLike<T = E, RR = R>(
|
|
872
474
|
elements: Iterable<T> | Iterable<RR> = [],
|
|
873
475
|
options?: StackOptions<T, RR>
|
|
@@ -884,7 +486,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
884
486
|
* @remarks Time O(N), Space O(1)
|
|
885
487
|
* @returns Iterator of elements.
|
|
886
488
|
*/
|
|
887
|
-
|
|
888
489
|
protected *_getIterator(): IterableIterator<E> {
|
|
889
490
|
for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
|
|
890
491
|
}
|