binary-tree-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/README.md +0 -84
- package/dist/cjs/index.cjs +965 -420
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +962 -417
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +965 -421
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +962 -418
- package/dist/esm-legacy/index.mjs.map +1 -1
- 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/binary-tree-typed.js +959 -414
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +3 -3
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- 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 +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -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 +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +425 -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 +343 -68
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +215 -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,24 @@ 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
|
+
* @example
|
|
172
|
+
* // Track queue length
|
|
173
|
+
* const q = new Queue<number>();
|
|
174
|
+
* console.log(q.length); // 0;
|
|
175
|
+
* q.push(1);
|
|
176
|
+
* q.push(2);
|
|
177
|
+
* console.log(q.length); // 2;
|
|
198
178
|
*/
|
|
199
179
|
|
|
200
180
|
get length(): number {
|
|
@@ -205,6 +185,22 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
205
185
|
* Get the first element (front) without removing it.
|
|
206
186
|
* @remarks Time O(1), Space O(1)
|
|
207
187
|
* @returns Front element or undefined.
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
* @example
|
|
200
|
+
* // View the front element
|
|
201
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
202
|
+
* console.log(q.first); // 'first';
|
|
203
|
+
* console.log(q.length); // 3;
|
|
208
204
|
*/
|
|
209
205
|
|
|
210
206
|
get first(): E | undefined {
|
|
@@ -237,6 +233,35 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
237
233
|
* Check whether the queue is empty.
|
|
238
234
|
* @remarks Time O(1), Space O(1)
|
|
239
235
|
* @returns True if length is 0.
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
* @example
|
|
248
|
+
* // Queue for...of iteration and isEmpty check
|
|
249
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
250
|
+
*
|
|
251
|
+
* const elements: string[] = [];
|
|
252
|
+
* for (const item of queue) {
|
|
253
|
+
* elements.push(item);
|
|
254
|
+
* }
|
|
255
|
+
*
|
|
256
|
+
* // Verify all elements are iterated in order
|
|
257
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
258
|
+
*
|
|
259
|
+
* // Process all elements
|
|
260
|
+
* while (queue.length > 0) {
|
|
261
|
+
* queue.shift();
|
|
262
|
+
* }
|
|
263
|
+
*
|
|
264
|
+
* console.log(queue.length); // 0;
|
|
240
265
|
*/
|
|
241
266
|
|
|
242
267
|
isEmpty(): boolean {
|
|
@@ -248,6 +273,27 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
248
273
|
* @remarks Time O(1), Space O(1)
|
|
249
274
|
* @param element - Element to enqueue.
|
|
250
275
|
* @returns True on success.
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
* @example
|
|
288
|
+
* // basic Queue creation and push operation
|
|
289
|
+
* // Create a simple Queue with initial values
|
|
290
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
291
|
+
*
|
|
292
|
+
* // Verify the queue maintains insertion order
|
|
293
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
294
|
+
*
|
|
295
|
+
* // Check length
|
|
296
|
+
* console.log(queue.length); // 5;
|
|
251
297
|
*/
|
|
252
298
|
|
|
253
299
|
push(element: E): boolean {
|
|
@@ -276,6 +322,31 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
276
322
|
* Dequeue one element from the front (amortized via offset).
|
|
277
323
|
* @remarks Time O(1) amortized, Space O(1)
|
|
278
324
|
* @returns Removed element or undefined.
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
* @example
|
|
337
|
+
* // Queue shift and peek operations
|
|
338
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
339
|
+
*
|
|
340
|
+
* // Peek at the front element without removing it
|
|
341
|
+
* console.log(queue.first); // 10;
|
|
342
|
+
*
|
|
343
|
+
* // Remove and get the first element (FIFO)
|
|
344
|
+
* const first = queue.shift();
|
|
345
|
+
* console.log(first); // 10;
|
|
346
|
+
*
|
|
347
|
+
* // Verify remaining elements and length decreased
|
|
348
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
349
|
+
* console.log(queue.length); // 3;
|
|
279
350
|
*/
|
|
280
351
|
|
|
281
352
|
shift(): E | undefined {
|
|
@@ -291,6 +362,19 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
291
362
|
* @remarks Time O(N), Space O(1)
|
|
292
363
|
* @param element - Element to remove (strict equality via Object.is).
|
|
293
364
|
* @returns True if an element was removed.
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
* @example
|
|
374
|
+
* // Remove specific element
|
|
375
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
376
|
+
* q.delete(2);
|
|
377
|
+
* console.log(q.length); // 3;
|
|
294
378
|
*/
|
|
295
379
|
|
|
296
380
|
delete(element: E): boolean {
|
|
@@ -308,6 +392,19 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
308
392
|
* @remarks Time O(1), Space O(1)
|
|
309
393
|
* @param index - Zero-based index from the front.
|
|
310
394
|
* @returns Element or undefined.
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
* @example
|
|
404
|
+
* // Access element by index
|
|
405
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
406
|
+
* console.log(q.at(0)); // 'a';
|
|
407
|
+
* console.log(q.at(2)); // 'c';
|
|
311
408
|
*/
|
|
312
409
|
|
|
313
410
|
at(index: number): E | undefined {
|
|
@@ -373,6 +470,20 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
373
470
|
* Remove all elements and reset offset.
|
|
374
471
|
* @remarks Time O(1), Space O(1)
|
|
375
472
|
* @returns void
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
* @example
|
|
483
|
+
* // Remove all elements
|
|
484
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
485
|
+
* q.clear();
|
|
486
|
+
* console.log(q.length); // 0;
|
|
376
487
|
*/
|
|
377
488
|
|
|
378
489
|
clear(): void {
|
|
@@ -384,6 +495,21 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
384
495
|
* Compact storage by discarding consumed head elements.
|
|
385
496
|
* @remarks Time O(N), Space O(N)
|
|
386
497
|
* @returns True when compaction performed.
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
* @example
|
|
507
|
+
* // Reclaim unused memory
|
|
508
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
509
|
+
* q.shift();
|
|
510
|
+
* q.shift();
|
|
511
|
+
* q.compact();
|
|
512
|
+
* console.log(q.length); // 3;
|
|
387
513
|
*/
|
|
388
514
|
|
|
389
515
|
compact(): boolean {
|
|
@@ -421,6 +547,22 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
421
547
|
* Deep clone this queue and its parameters.
|
|
422
548
|
* @remarks Time O(N), Space O(N)
|
|
423
549
|
* @returns A new queue with the same content and options.
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
* @example
|
|
560
|
+
* // Create independent copy
|
|
561
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
562
|
+
* const copy = q.clone();
|
|
563
|
+
* copy.shift();
|
|
564
|
+
* console.log(q.length); // 3;
|
|
565
|
+
* console.log(copy.length); // 2;
|
|
424
566
|
*/
|
|
425
567
|
|
|
426
568
|
clone(): this {
|
|
@@ -436,6 +578,20 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
436
578
|
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
437
579
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
438
580
|
* @returns A new queue with kept elements.
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
* @example
|
|
591
|
+
* // Filter elements
|
|
592
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
593
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
594
|
+
* console.log(evens.length); // 2;
|
|
439
595
|
*/
|
|
440
596
|
|
|
441
597
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
@@ -458,6 +614,19 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
458
614
|
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
459
615
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
460
616
|
* @returns A new Queue with mapped elements.
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
* @example
|
|
626
|
+
* // Transform elements
|
|
627
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
628
|
+
* const doubled = q.map(x => x * 2);
|
|
629
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
461
630
|
*/
|
|
462
631
|
|
|
463
632
|
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,19 @@ 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
|
+
* @example
|
|
177
|
+
* // Get number of elements
|
|
178
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
179
|
+
* console.log(stack.size); // 3;
|
|
195
180
|
*/
|
|
196
181
|
|
|
197
182
|
get size(): number {
|
|
@@ -221,6 +206,23 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
221
206
|
* Check whether the stack is empty.
|
|
222
207
|
* @remarks Time O(1), Space O(1)
|
|
223
208
|
* @returns True if size is 0.
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
* @example
|
|
221
|
+
* // Check if stack has elements
|
|
222
|
+
* const stack = new Stack<number>();
|
|
223
|
+
* console.log(stack.isEmpty()); // true;
|
|
224
|
+
* stack.push(1);
|
|
225
|
+
* console.log(stack.isEmpty()); // false;
|
|
224
226
|
*/
|
|
225
227
|
|
|
226
228
|
isEmpty(): boolean {
|
|
@@ -231,6 +233,22 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
231
233
|
* Get the top element without removing it.
|
|
232
234
|
* @remarks Time O(1), Space O(1)
|
|
233
235
|
* @returns Top element or undefined.
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
* @example
|
|
248
|
+
* // View the top element without removing it
|
|
249
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
250
|
+
* console.log(stack.peek()); // 'c';
|
|
251
|
+
* console.log(stack.size); // 3;
|
|
234
252
|
*/
|
|
235
253
|
|
|
236
254
|
peek(): E | undefined {
|
|
@@ -242,6 +260,31 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
242
260
|
* @remarks Time O(1), Space O(1)
|
|
243
261
|
* @param element - Element to push.
|
|
244
262
|
* @returns True when pushed.
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
* @example
|
|
275
|
+
* // basic Stack creation and push operation
|
|
276
|
+
* // Create a simple Stack with initial values
|
|
277
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
278
|
+
*
|
|
279
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
280
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
281
|
+
*
|
|
282
|
+
* // Check length
|
|
283
|
+
* console.log(stack.size); // 5;
|
|
284
|
+
*
|
|
285
|
+
* // Push a new element to the top
|
|
286
|
+
* stack.push(6);
|
|
287
|
+
* console.log(stack.size); // 6;
|
|
245
288
|
*/
|
|
246
289
|
|
|
247
290
|
push(element: E): boolean {
|
|
@@ -253,6 +296,35 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
253
296
|
* Pop and return the top element.
|
|
254
297
|
* @remarks Time O(1), Space O(1)
|
|
255
298
|
* @returns Removed element or undefined.
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
* @example
|
|
311
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
312
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
313
|
+
*
|
|
314
|
+
* // Peek at the top element without removing
|
|
315
|
+
* const top = stack.peek();
|
|
316
|
+
* console.log(top); // 50;
|
|
317
|
+
*
|
|
318
|
+
* // Pop removes from the top (LIFO order)
|
|
319
|
+
* const popped = stack.pop();
|
|
320
|
+
* console.log(popped); // 50;
|
|
321
|
+
*
|
|
322
|
+
* // Next pop gets the previous element
|
|
323
|
+
* const next = stack.pop();
|
|
324
|
+
* console.log(next); // 40;
|
|
325
|
+
*
|
|
326
|
+
* // Verify length decreased
|
|
327
|
+
* console.log(stack.size); // 3;
|
|
256
328
|
*/
|
|
257
329
|
|
|
258
330
|
pop(): E | undefined {
|
|
@@ -280,6 +352,19 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
280
352
|
* @remarks Time O(N), Space O(1)
|
|
281
353
|
* @param element - Element to remove (using the configured equality).
|
|
282
354
|
* @returns True if an element was removed.
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
* @example
|
|
364
|
+
* // Remove element
|
|
365
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
366
|
+
* stack.delete(2);
|
|
367
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
283
368
|
*/
|
|
284
369
|
|
|
285
370
|
delete(element: E): boolean {
|
|
@@ -321,6 +406,20 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
321
406
|
* Remove all elements and reset storage.
|
|
322
407
|
* @remarks Time O(1), Space O(1)
|
|
323
408
|
* @returns void
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
* @example
|
|
419
|
+
* // Remove all elements
|
|
420
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
421
|
+
* stack.clear();
|
|
422
|
+
* console.log(stack.isEmpty()); // true;
|
|
324
423
|
*/
|
|
325
424
|
|
|
326
425
|
clear(): void {
|
|
@@ -331,6 +430,22 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
331
430
|
* Deep clone this stack.
|
|
332
431
|
* @remarks Time O(N), Space O(N)
|
|
333
432
|
* @returns A new stack with the same content.
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
* @example
|
|
443
|
+
* // Create independent copy
|
|
444
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
445
|
+
* const copy = stack.clone();
|
|
446
|
+
* copy.pop();
|
|
447
|
+
* console.log(stack.size); // 3;
|
|
448
|
+
* console.log(copy.size); // 2;
|
|
334
449
|
*/
|
|
335
450
|
|
|
336
451
|
clone(): this {
|
|
@@ -345,6 +460,20 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
345
460
|
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
346
461
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
347
462
|
* @returns A new stack with kept values.
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
* @example
|
|
473
|
+
* // Filter elements
|
|
474
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
475
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
476
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
348
477
|
*/
|
|
349
478
|
|
|
350
479
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
@@ -384,6 +513,19 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
384
513
|
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
385
514
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
386
515
|
* @returns A new Stack with mapped elements.
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
* @example
|
|
525
|
+
* // Transform elements
|
|
526
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
527
|
+
* const doubled = stack.map(x => x * 2);
|
|
528
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
387
529
|
*/
|
|
388
530
|
|
|
389
531
|
map<EM, RM>(
|