data-structure-typed 2.6.0 → 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/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 +23 -23
- 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/package.json +45 -46
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +0 -3
- package/src/data-structures/base/linear-base.ts +2 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -529
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
- package/src/data-structures/binary-tree/bst.ts +158 -1082
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
- package/src/data-structures/binary-tree/segment-tree.ts +73 -351
- package/src/data-structures/binary-tree/tree-map.ts +462 -5124
- package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
- package/src/data-structures/binary-tree/tree-multi-set.ts +284 -3972
- package/src/data-structures/binary-tree/tree-set.ts +338 -4836
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -562
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -511
- package/src/data-structures/hash/hash-map.ts +154 -582
- package/src/data-structures/heap/heap.ts +200 -795
- package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
- package/src/data-structures/matrix/matrix.ts +179 -518
- 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 +214 -882
- package/src/data-structures/queue/queue.ts +102 -625
- package/src/data-structures/stack/stack.ts +76 -505
- package/src/data-structures/trie/trie.ts +98 -628
- 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,52 +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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
* @example
|
|
208
|
-
* // Get number of elements
|
|
209
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
210
|
-
* 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;
|
|
211
166
|
*/
|
|
212
|
-
|
|
213
167
|
get size(): number {
|
|
214
168
|
return this.elements.length;
|
|
215
169
|
}
|
|
@@ -224,7 +178,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
224
178
|
* @param [options] - Options forwarded to the constructor.
|
|
225
179
|
* @returns A new Stack populated from the array.
|
|
226
180
|
*/
|
|
227
|
-
|
|
228
181
|
static fromArray<E, R = any>(
|
|
229
182
|
this: new (elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>) => any,
|
|
230
183
|
elements: E[],
|
|
@@ -237,56 +190,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
237
190
|
* Check whether the stack is empty.
|
|
238
191
|
* @remarks Time O(1), Space O(1)
|
|
239
192
|
* @returns True if size is 0.
|
|
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
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
* @example
|
|
283
|
-
* // Check if stack has elements
|
|
284
|
-
* const stack = new Stack<number>();
|
|
285
|
-
* console.log(stack.isEmpty()); // true;
|
|
286
|
-
* stack.push(1);
|
|
287
|
-
* 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;
|
|
288
199
|
*/
|
|
289
|
-
|
|
290
200
|
isEmpty(): boolean {
|
|
291
201
|
return this.elements.length === 0;
|
|
292
202
|
}
|
|
@@ -295,55 +205,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
295
205
|
* Get the top element without removing it.
|
|
296
206
|
* @remarks Time O(1), Space O(1)
|
|
297
207
|
* @returns Top element or undefined.
|
|
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
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
* @example
|
|
341
|
-
* // View the top element without removing it
|
|
342
|
-
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
343
|
-
* console.log(stack.peek()); // 'c';
|
|
344
|
-
* 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;
|
|
345
213
|
*/
|
|
346
|
-
|
|
347
214
|
peek(): E | undefined {
|
|
348
215
|
return this.isEmpty() ? undefined : this.elements[this.elements.length - 1];
|
|
349
216
|
}
|
|
@@ -353,64 +220,21 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
353
220
|
* @remarks Time O(1), Space O(1)
|
|
354
221
|
* @param element - Element to push.
|
|
355
222
|
* @returns True when pushed.
|
|
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
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
* @example
|
|
399
|
-
* // basic Stack creation and push operation
|
|
400
|
-
* // Create a simple Stack with initial values
|
|
401
|
-
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
402
|
-
*
|
|
403
|
-
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
404
|
-
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
405
|
-
*
|
|
406
|
-
* // Check length
|
|
407
|
-
* console.log(stack.size); // 5;
|
|
408
|
-
*
|
|
409
|
-
* // Push a new element to the top
|
|
410
|
-
* stack.push(6);
|
|
411
|
-
* 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;
|
|
412
237
|
*/
|
|
413
|
-
|
|
414
238
|
push(element: E): boolean {
|
|
415
239
|
this.elements.push(element);
|
|
416
240
|
return true;
|
|
@@ -420,68 +244,25 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
420
244
|
* Pop and return the top element.
|
|
421
245
|
* @remarks Time O(1), Space O(1)
|
|
422
246
|
* @returns Removed element or undefined.
|
|
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
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
* @example
|
|
466
|
-
* // Stack pop operation (LIFO - Last In First Out)
|
|
467
|
-
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
468
|
-
*
|
|
469
|
-
* // Peek at the top element without removing
|
|
470
|
-
* const top = stack.peek();
|
|
471
|
-
* console.log(top); // 50;
|
|
472
|
-
*
|
|
473
|
-
* // Pop removes from the top (LIFO order)
|
|
474
|
-
* const popped = stack.pop();
|
|
475
|
-
* console.log(popped); // 50;
|
|
476
|
-
*
|
|
477
|
-
* // Next pop gets the previous element
|
|
478
|
-
* const next = stack.pop();
|
|
479
|
-
* console.log(next); // 40;
|
|
480
|
-
*
|
|
481
|
-
* // Verify length decreased
|
|
482
|
-
* 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;
|
|
483
265
|
*/
|
|
484
|
-
|
|
485
266
|
pop(): E | undefined {
|
|
486
267
|
return this.isEmpty() ? undefined : this.elements.pop();
|
|
487
268
|
}
|
|
@@ -492,7 +273,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
492
273
|
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
493
274
|
* @returns Array of per-element success flags.
|
|
494
275
|
*/
|
|
495
|
-
|
|
496
276
|
pushMany(elements: Iterable<E> | Iterable<R>): boolean[] {
|
|
497
277
|
const ans: boolean[] = [];
|
|
498
278
|
for (const el of elements) {
|
|
@@ -507,52 +287,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
507
287
|
* @remarks Time O(N), Space O(1)
|
|
508
288
|
* @param element - Element to remove (using the configured equality).
|
|
509
289
|
* @returns True if an element was removed.
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
* @example
|
|
550
|
-
* // Remove element
|
|
551
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
552
|
-
* stack.delete(2);
|
|
553
|
-
* 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];
|
|
554
295
|
*/
|
|
555
|
-
|
|
556
296
|
delete(element: E): boolean {
|
|
557
297
|
const idx = this._indexOfByEquals(element);
|
|
558
298
|
return this.deleteAt(idx) !== undefined;
|
|
@@ -564,7 +304,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
564
304
|
* @param index - Zero-based index from the bottom.
|
|
565
305
|
* @returns The removed element, or undefined if the index is out of range.
|
|
566
306
|
*/
|
|
567
|
-
|
|
568
307
|
deleteAt(index: number): E | undefined {
|
|
569
308
|
if (index < 0 || index >= this.elements.length) return undefined;
|
|
570
309
|
const spliced = this.elements.splice(index, 1);
|
|
@@ -577,7 +316,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
577
316
|
* @param predicate - Function (value, index, stack) → boolean to decide deletion.
|
|
578
317
|
* @returns True if a match was removed.
|
|
579
318
|
*/
|
|
580
|
-
|
|
581
319
|
deleteWhere(predicate: (value: E, index: number, stack: this) => boolean): boolean {
|
|
582
320
|
for (let i = 0; i < this.elements.length; i++) {
|
|
583
321
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -592,53 +330,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
592
330
|
* Remove all elements and reset storage.
|
|
593
331
|
* @remarks Time O(1), Space O(1)
|
|
594
332
|
* @returns void
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
* @example
|
|
636
|
-
* // Remove all elements
|
|
637
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
638
|
-
* stack.clear();
|
|
639
|
-
* 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;
|
|
640
338
|
*/
|
|
641
|
-
|
|
642
339
|
clear(): void {
|
|
643
340
|
this._elements = [];
|
|
644
341
|
}
|
|
@@ -647,55 +344,14 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
647
344
|
* Deep clone this stack.
|
|
648
345
|
* @remarks Time O(N), Space O(N)
|
|
649
346
|
* @returns A new stack with the same content.
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
* @example
|
|
691
|
-
* // Create independent copy
|
|
692
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
693
|
-
* const copy = stack.clone();
|
|
694
|
-
* copy.pop();
|
|
695
|
-
* console.log(stack.size); // 3;
|
|
696
|
-
* 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;
|
|
697
354
|
*/
|
|
698
|
-
|
|
699
355
|
clone(): this {
|
|
700
356
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
701
357
|
for (const v of this) out.push(v);
|
|
@@ -708,53 +364,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
708
364
|
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
709
365
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
710
366
|
* @returns A new stack with kept values.
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
* @example
|
|
752
|
-
* // Filter elements
|
|
753
|
-
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
754
|
-
* const evens = stack.filter(x => x % 2 === 0);
|
|
755
|
-
* 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];
|
|
756
372
|
*/
|
|
757
|
-
|
|
758
373
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
759
374
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
760
375
|
let index = 0;
|
|
@@ -772,7 +387,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
772
387
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
773
388
|
* @returns A new stack with mapped values.
|
|
774
389
|
*/
|
|
775
|
-
|
|
776
390
|
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
|
|
777
391
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
778
392
|
let index = 0;
|
|
@@ -792,52 +406,12 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
792
406
|
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
793
407
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
794
408
|
* @returns A new Stack with mapped elements.
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
* @example
|
|
835
|
-
* // Transform elements
|
|
836
|
-
* const stack = new Stack<number>([1, 2, 3]);
|
|
837
|
-
* const doubled = stack.map(x => x * 2);
|
|
838
|
-
* 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];
|
|
839
414
|
*/
|
|
840
|
-
|
|
841
415
|
map<EM, RM>(
|
|
842
416
|
callback: ElementCallback<E, R, EM>,
|
|
843
417
|
options?: IterableElementBaseOptions<EM, RM>,
|
|
@@ -858,19 +432,19 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
858
432
|
* @param equals - Equality predicate (a, b) → boolean.
|
|
859
433
|
* @returns This stack.
|
|
860
434
|
*/
|
|
861
|
-
|
|
862
435
|
setEquality(equals: (a: E, b: E) => boolean): this {
|
|
863
436
|
this._equals = equals;
|
|
864
437
|
return this;
|
|
865
438
|
}
|
|
866
439
|
|
|
440
|
+
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
441
|
+
|
|
867
442
|
/**
|
|
868
443
|
* (Protected) Find the index of a target element using the equality function.
|
|
869
444
|
* @remarks Time O(N), Space O(1)
|
|
870
445
|
* @param target - Element to search for.
|
|
871
446
|
* @returns Index or -1 if not found.
|
|
872
447
|
*/
|
|
873
|
-
|
|
874
448
|
protected _indexOfByEquals(target: E): number {
|
|
875
449
|
for (let i = 0; i < this.elements.length; i++) if (this._equals(this.elements[i], target)) return i;
|
|
876
450
|
return -1;
|
|
@@ -882,7 +456,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
882
456
|
* @param [options] - Options forwarded to the constructor.
|
|
883
457
|
* @returns An empty like-kind stack instance.
|
|
884
458
|
*/
|
|
885
|
-
|
|
886
459
|
protected _createInstance(options?: StackOptions<E, R>): this {
|
|
887
460
|
const Ctor = this.constructor as new (elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>) => this;
|
|
888
461
|
return new Ctor([], options);
|
|
@@ -897,7 +470,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
897
470
|
* @param [options] - Options forwarded to the constructor.
|
|
898
471
|
* @returns A like-kind Stack instance.
|
|
899
472
|
*/
|
|
900
|
-
|
|
901
473
|
protected _createLike<T = E, RR = R>(
|
|
902
474
|
elements: Iterable<T> | Iterable<RR> = [],
|
|
903
475
|
options?: StackOptions<T, RR>
|
|
@@ -914,7 +486,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
914
486
|
* @remarks Time O(N), Space O(1)
|
|
915
487
|
* @returns Iterator of elements.
|
|
916
488
|
*/
|
|
917
|
-
|
|
918
489
|
protected *_getIterator(): IterableIterator<E> {
|
|
919
490
|
for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
|
|
920
491
|
}
|