data-structure-typed 2.4.4 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -1
- package/README.md +34 -1
- package/dist/cjs/index.cjs +10639 -2151
- package/dist/cjs-legacy/index.cjs +10694 -2195
- package/dist/esm/index.mjs +10639 -2150
- package/dist/esm-legacy/index.mjs +10694 -2194
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/data-structure-typed.js +10725 -2221
- package/dist/umd/data-structure-typed.min.js +4 -2
- package/package.json +5 -4
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
- package/src/data-structures/binary-tree/binary-tree.ts +567 -121
- package/src/data-structures/binary-tree/bst.ts +370 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1411 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
- package/src/data-structures/binary-tree/tree-set.ts +1257 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +233 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +308 -59
- package/src/data-structures/hash/hash-map.ts +254 -79
- package/src/data-structures/heap/heap.ts +305 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +433 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +358 -68
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +227 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -23,48 +23,6 @@ import { LinearBase } from '../base/linear-base';
|
|
|
23
23
|
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
24
24
|
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
25
25
|
* @example
|
|
26
|
-
* // basic Queue creation and push operation
|
|
27
|
-
* // Create a simple Queue with initial values
|
|
28
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
29
|
-
*
|
|
30
|
-
* // Verify the queue maintains insertion order
|
|
31
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
32
|
-
*
|
|
33
|
-
* // Check length
|
|
34
|
-
* console.log(queue.length); // 5;
|
|
35
|
-
* @example
|
|
36
|
-
* // Queue shift and peek operations
|
|
37
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
38
|
-
*
|
|
39
|
-
* // Peek at the front element without removing it
|
|
40
|
-
* console.log(queue.first); // 10;
|
|
41
|
-
*
|
|
42
|
-
* // Remove and get the first element (FIFO)
|
|
43
|
-
* const first = queue.shift();
|
|
44
|
-
* console.log(first); // 10;
|
|
45
|
-
*
|
|
46
|
-
* // Verify remaining elements and length decreased
|
|
47
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
48
|
-
* console.log(queue.length); // 3;
|
|
49
|
-
* @example
|
|
50
|
-
* // Queue for...of iteration and isEmpty check
|
|
51
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
52
|
-
*
|
|
53
|
-
* const elements: string[] = [];
|
|
54
|
-
* for (const item of queue) {
|
|
55
|
-
* elements.push(item);
|
|
56
|
-
* }
|
|
57
|
-
*
|
|
58
|
-
* // Verify all elements are iterated in order
|
|
59
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
60
|
-
*
|
|
61
|
-
* // Process all elements
|
|
62
|
-
* while (queue.length > 0) {
|
|
63
|
-
* queue.shift();
|
|
64
|
-
* }
|
|
65
|
-
*
|
|
66
|
-
* console.log(queue.length); // 0;
|
|
67
|
-
* @example
|
|
68
26
|
* // Queue as message broker for event processing
|
|
69
27
|
* interface Message {
|
|
70
28
|
* id: string;
|
|
@@ -125,6 +83,10 @@ import { LinearBase } from '../base/linear-base';
|
|
|
125
83
|
*
|
|
126
84
|
* // Queue should be empty after processing all messages
|
|
127
85
|
* console.log(messageQueue.length); // 0;
|
|
86
|
+
* @example
|
|
87
|
+
* // Convert queue to array
|
|
88
|
+
* const q = new Queue<number>([10, 20, 30]);
|
|
89
|
+
* console.log(q.toArray()); // [10, 20, 30];
|
|
128
90
|
*/
|
|
129
91
|
export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
130
92
|
/**
|
|
@@ -195,6 +157,25 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
195
157
|
* Get the number of elements currently in the queue.
|
|
196
158
|
* @remarks Time O(1), Space O(1)
|
|
197
159
|
* @returns Current length.
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
* @example
|
|
173
|
+
* // Track queue length
|
|
174
|
+
* const q = new Queue<number>();
|
|
175
|
+
* console.log(q.length); // 0;
|
|
176
|
+
* q.push(1);
|
|
177
|
+
* q.push(2);
|
|
178
|
+
* console.log(q.length); // 2;
|
|
198
179
|
*/
|
|
199
180
|
|
|
200
181
|
get length(): number {
|
|
@@ -205,6 +186,23 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
205
186
|
* Get the first element (front) without removing it.
|
|
206
187
|
* @remarks Time O(1), Space O(1)
|
|
207
188
|
* @returns Front element or undefined.
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
* @example
|
|
202
|
+
* // View the front element
|
|
203
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
204
|
+
* console.log(q.first); // 'first';
|
|
205
|
+
* console.log(q.length); // 3;
|
|
208
206
|
*/
|
|
209
207
|
|
|
210
208
|
get first(): E | undefined {
|
|
@@ -237,6 +235,36 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
237
235
|
* Check whether the queue is empty.
|
|
238
236
|
* @remarks Time O(1), Space O(1)
|
|
239
237
|
* @returns True if length is 0.
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
* @example
|
|
251
|
+
* // Queue for...of iteration and isEmpty check
|
|
252
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
253
|
+
*
|
|
254
|
+
* const elements: string[] = [];
|
|
255
|
+
* for (const item of queue) {
|
|
256
|
+
* elements.push(item);
|
|
257
|
+
* }
|
|
258
|
+
*
|
|
259
|
+
* // Verify all elements are iterated in order
|
|
260
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
261
|
+
*
|
|
262
|
+
* // Process all elements
|
|
263
|
+
* while (queue.length > 0) {
|
|
264
|
+
* queue.shift();
|
|
265
|
+
* }
|
|
266
|
+
*
|
|
267
|
+
* console.log(queue.length); // 0;
|
|
240
268
|
*/
|
|
241
269
|
|
|
242
270
|
isEmpty(): boolean {
|
|
@@ -248,6 +276,28 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
248
276
|
* @remarks Time O(1), Space O(1)
|
|
249
277
|
* @param element - Element to enqueue.
|
|
250
278
|
* @returns True on success.
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
* @example
|
|
292
|
+
* // basic Queue creation and push operation
|
|
293
|
+
* // Create a simple Queue with initial values
|
|
294
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
295
|
+
*
|
|
296
|
+
* // Verify the queue maintains insertion order
|
|
297
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
298
|
+
*
|
|
299
|
+
* // Check length
|
|
300
|
+
* console.log(queue.length); // 5;
|
|
251
301
|
*/
|
|
252
302
|
|
|
253
303
|
push(element: E): boolean {
|
|
@@ -276,6 +326,32 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
276
326
|
* Dequeue one element from the front (amortized via offset).
|
|
277
327
|
* @remarks Time O(1) amortized, Space O(1)
|
|
278
328
|
* @returns Removed element or undefined.
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
* @example
|
|
342
|
+
* // Queue shift and peek operations
|
|
343
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
344
|
+
*
|
|
345
|
+
* // Peek at the front element without removing it
|
|
346
|
+
* console.log(queue.first); // 10;
|
|
347
|
+
*
|
|
348
|
+
* // Remove and get the first element (FIFO)
|
|
349
|
+
* const first = queue.shift();
|
|
350
|
+
* console.log(first); // 10;
|
|
351
|
+
*
|
|
352
|
+
* // Verify remaining elements and length decreased
|
|
353
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
354
|
+
* console.log(queue.length); // 3;
|
|
279
355
|
*/
|
|
280
356
|
|
|
281
357
|
shift(): E | undefined {
|
|
@@ -291,6 +367,20 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
291
367
|
* @remarks Time O(N), Space O(1)
|
|
292
368
|
* @param element - Element to remove (strict equality via Object.is).
|
|
293
369
|
* @returns True if an element was removed.
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
* @example
|
|
380
|
+
* // Remove specific element
|
|
381
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
382
|
+
* q.delete(2);
|
|
383
|
+
* console.log(q.length); // 3;
|
|
294
384
|
*/
|
|
295
385
|
|
|
296
386
|
delete(element: E): boolean {
|
|
@@ -308,6 +398,20 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
308
398
|
* @remarks Time O(1), Space O(1)
|
|
309
399
|
* @param index - Zero-based index from the front.
|
|
310
400
|
* @returns Element or undefined.
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
* @example
|
|
411
|
+
* // Access element by index
|
|
412
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
413
|
+
* console.log(q.at(0)); // 'a';
|
|
414
|
+
* console.log(q.at(2)); // 'c';
|
|
311
415
|
*/
|
|
312
416
|
|
|
313
417
|
at(index: number): E | undefined {
|
|
@@ -373,6 +477,21 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
373
477
|
* Remove all elements and reset offset.
|
|
374
478
|
* @remarks Time O(1), Space O(1)
|
|
375
479
|
* @returns void
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
* @example
|
|
491
|
+
* // Remove all elements
|
|
492
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
493
|
+
* q.clear();
|
|
494
|
+
* console.log(q.length); // 0;
|
|
376
495
|
*/
|
|
377
496
|
|
|
378
497
|
clear(): void {
|
|
@@ -384,6 +503,22 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
384
503
|
* Compact storage by discarding consumed head elements.
|
|
385
504
|
* @remarks Time O(N), Space O(N)
|
|
386
505
|
* @returns True when compaction performed.
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
* @example
|
|
516
|
+
* // Reclaim unused memory
|
|
517
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
518
|
+
* q.shift();
|
|
519
|
+
* q.shift();
|
|
520
|
+
* q.compact();
|
|
521
|
+
* console.log(q.length); // 3;
|
|
387
522
|
*/
|
|
388
523
|
|
|
389
524
|
compact(): boolean {
|
|
@@ -421,6 +556,23 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
421
556
|
* Deep clone this queue and its parameters.
|
|
422
557
|
* @remarks Time O(N), Space O(N)
|
|
423
558
|
* @returns A new queue with the same content and options.
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
* @example
|
|
570
|
+
* // Create independent copy
|
|
571
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
572
|
+
* const copy = q.clone();
|
|
573
|
+
* copy.shift();
|
|
574
|
+
* console.log(q.length); // 3;
|
|
575
|
+
* console.log(copy.length); // 2;
|
|
424
576
|
*/
|
|
425
577
|
|
|
426
578
|
clone(): this {
|
|
@@ -436,6 +588,21 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
436
588
|
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
437
589
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
438
590
|
* @returns A new queue with kept elements.
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
* @example
|
|
602
|
+
* // Filter elements
|
|
603
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
604
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
605
|
+
* console.log(evens.length); // 2;
|
|
439
606
|
*/
|
|
440
607
|
|
|
441
608
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
@@ -458,6 +625,20 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
458
625
|
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
459
626
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
460
627
|
* @returns A new Queue with mapped elements.
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
* @example
|
|
638
|
+
* // Transform elements
|
|
639
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
640
|
+
* const doubled = q.map(x => x * 2);
|
|
641
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
461
642
|
*/
|
|
462
643
|
|
|
463
644
|
map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: QueueOptions<EM, RM>, thisArg?: unknown): Queue<EM, RM> {
|
|
@@ -21,38 +21,6 @@ import { IterableElementBase } from '../base';
|
|
|
21
21
|
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
22
22
|
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
23
23
|
* @example
|
|
24
|
-
* // basic Stack creation and push operation
|
|
25
|
-
* // Create a simple Stack with initial values
|
|
26
|
-
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
27
|
-
*
|
|
28
|
-
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
29
|
-
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
30
|
-
*
|
|
31
|
-
* // Check length
|
|
32
|
-
* console.log(stack.size); // 5;
|
|
33
|
-
*
|
|
34
|
-
* // Push a new element to the top
|
|
35
|
-
* stack.push(6);
|
|
36
|
-
* console.log(stack.size); // 6;
|
|
37
|
-
* @example
|
|
38
|
-
* // Stack pop operation (LIFO - Last In First Out)
|
|
39
|
-
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
40
|
-
*
|
|
41
|
-
* // Peek at the top element without removing
|
|
42
|
-
* const top = stack.peek();
|
|
43
|
-
* console.log(top); // 50;
|
|
44
|
-
*
|
|
45
|
-
* // Pop removes from the top (LIFO order)
|
|
46
|
-
* const popped = stack.pop();
|
|
47
|
-
* console.log(popped); // 50;
|
|
48
|
-
*
|
|
49
|
-
* // Next pop gets the previous element
|
|
50
|
-
* const next = stack.pop();
|
|
51
|
-
* console.log(next); // 40;
|
|
52
|
-
*
|
|
53
|
-
* // Verify length decreased
|
|
54
|
-
* console.log(stack.size); // 3;
|
|
55
|
-
* @example
|
|
56
24
|
* // Function Call Stack
|
|
57
25
|
* const functionStack = new Stack<string>();
|
|
58
26
|
* functionStack.push('main');
|
|
@@ -159,6 +127,10 @@ import { IterableElementBase } from '../base';
|
|
|
159
127
|
* else if (segment && segment !== '.') stack.push(segment);
|
|
160
128
|
* });
|
|
161
129
|
* console.log(stack.elements.join('/')); // 'c';
|
|
130
|
+
* @example
|
|
131
|
+
* // Convert stack to array
|
|
132
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
133
|
+
* console.log(stack.toArray()); // [1, 2, 3];
|
|
162
134
|
*/
|
|
163
135
|
export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
164
136
|
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
@@ -192,6 +164,20 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
192
164
|
* Get the number of stored elements.
|
|
193
165
|
* @remarks Time O(1), Space O(1)
|
|
194
166
|
* @returns Current size.
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
* @example
|
|
178
|
+
* // Get number of elements
|
|
179
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
180
|
+
* console.log(stack.size); // 3;
|
|
195
181
|
*/
|
|
196
182
|
|
|
197
183
|
get size(): number {
|
|
@@ -221,6 +207,24 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
221
207
|
* Check whether the stack is empty.
|
|
222
208
|
* @remarks Time O(1), Space O(1)
|
|
223
209
|
* @returns True if size is 0.
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
* @example
|
|
223
|
+
* // Check if stack has elements
|
|
224
|
+
* const stack = new Stack<number>();
|
|
225
|
+
* console.log(stack.isEmpty()); // true;
|
|
226
|
+
* stack.push(1);
|
|
227
|
+
* console.log(stack.isEmpty()); // false;
|
|
224
228
|
*/
|
|
225
229
|
|
|
226
230
|
isEmpty(): boolean {
|
|
@@ -231,6 +235,23 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
231
235
|
* Get the top element without removing it.
|
|
232
236
|
* @remarks Time O(1), Space O(1)
|
|
233
237
|
* @returns Top element or undefined.
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
* @example
|
|
251
|
+
* // View the top element without removing it
|
|
252
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
253
|
+
* console.log(stack.peek()); // 'c';
|
|
254
|
+
* console.log(stack.size); // 3;
|
|
234
255
|
*/
|
|
235
256
|
|
|
236
257
|
peek(): E | undefined {
|
|
@@ -242,6 +263,32 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
242
263
|
* @remarks Time O(1), Space O(1)
|
|
243
264
|
* @param element - Element to push.
|
|
244
265
|
* @returns True when pushed.
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
* @example
|
|
279
|
+
* // basic Stack creation and push operation
|
|
280
|
+
* // Create a simple Stack with initial values
|
|
281
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
282
|
+
*
|
|
283
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
284
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
285
|
+
*
|
|
286
|
+
* // Check length
|
|
287
|
+
* console.log(stack.size); // 5;
|
|
288
|
+
*
|
|
289
|
+
* // Push a new element to the top
|
|
290
|
+
* stack.push(6);
|
|
291
|
+
* console.log(stack.size); // 6;
|
|
245
292
|
*/
|
|
246
293
|
|
|
247
294
|
push(element: E): boolean {
|
|
@@ -253,6 +300,36 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
253
300
|
* Pop and return the top element.
|
|
254
301
|
* @remarks Time O(1), Space O(1)
|
|
255
302
|
* @returns Removed element or undefined.
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
* @example
|
|
316
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
317
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
318
|
+
*
|
|
319
|
+
* // Peek at the top element without removing
|
|
320
|
+
* const top = stack.peek();
|
|
321
|
+
* console.log(top); // 50;
|
|
322
|
+
*
|
|
323
|
+
* // Pop removes from the top (LIFO order)
|
|
324
|
+
* const popped = stack.pop();
|
|
325
|
+
* console.log(popped); // 50;
|
|
326
|
+
*
|
|
327
|
+
* // Next pop gets the previous element
|
|
328
|
+
* const next = stack.pop();
|
|
329
|
+
* console.log(next); // 40;
|
|
330
|
+
*
|
|
331
|
+
* // Verify length decreased
|
|
332
|
+
* console.log(stack.size); // 3;
|
|
256
333
|
*/
|
|
257
334
|
|
|
258
335
|
pop(): E | undefined {
|
|
@@ -280,6 +357,20 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
280
357
|
* @remarks Time O(N), Space O(1)
|
|
281
358
|
* @param element - Element to remove (using the configured equality).
|
|
282
359
|
* @returns True if an element was removed.
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
* @example
|
|
370
|
+
* // Remove element
|
|
371
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
372
|
+
* stack.delete(2);
|
|
373
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
283
374
|
*/
|
|
284
375
|
|
|
285
376
|
delete(element: E): boolean {
|
|
@@ -321,6 +412,21 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
321
412
|
* Remove all elements and reset storage.
|
|
322
413
|
* @remarks Time O(1), Space O(1)
|
|
323
414
|
* @returns void
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
* @example
|
|
426
|
+
* // Remove all elements
|
|
427
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
428
|
+
* stack.clear();
|
|
429
|
+
* console.log(stack.isEmpty()); // true;
|
|
324
430
|
*/
|
|
325
431
|
|
|
326
432
|
clear(): void {
|
|
@@ -331,6 +437,23 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
331
437
|
* Deep clone this stack.
|
|
332
438
|
* @remarks Time O(N), Space O(N)
|
|
333
439
|
* @returns A new stack with the same content.
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
* @example
|
|
451
|
+
* // Create independent copy
|
|
452
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
453
|
+
* const copy = stack.clone();
|
|
454
|
+
* copy.pop();
|
|
455
|
+
* console.log(stack.size); // 3;
|
|
456
|
+
* console.log(copy.size); // 2;
|
|
334
457
|
*/
|
|
335
458
|
|
|
336
459
|
clone(): this {
|
|
@@ -345,6 +468,21 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
345
468
|
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
346
469
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
347
470
|
* @returns A new stack with kept values.
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
* @example
|
|
482
|
+
* // Filter elements
|
|
483
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
484
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
485
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
348
486
|
*/
|
|
349
487
|
|
|
350
488
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
@@ -384,6 +522,20 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
384
522
|
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
385
523
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
386
524
|
* @returns A new Stack with mapped elements.
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
* @example
|
|
535
|
+
* // Transform elements
|
|
536
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
537
|
+
* const doubled = stack.map(x => x * 2);
|
|
538
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
387
539
|
*/
|
|
388
540
|
|
|
389
541
|
map<EM, RM>(
|