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
|
@@ -21,48 +21,6 @@ import { LinearBase } from '../base/linear-base';
|
|
|
21
21
|
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
22
22
|
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
23
23
|
* @example
|
|
24
|
-
* // basic Queue creation and push operation
|
|
25
|
-
* // Create a simple Queue with initial values
|
|
26
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
27
|
-
*
|
|
28
|
-
* // Verify the queue maintains insertion order
|
|
29
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
30
|
-
*
|
|
31
|
-
* // Check length
|
|
32
|
-
* console.log(queue.length); // 5;
|
|
33
|
-
* @example
|
|
34
|
-
* // Queue shift and peek operations
|
|
35
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
36
|
-
*
|
|
37
|
-
* // Peek at the front element without removing it
|
|
38
|
-
* console.log(queue.first); // 10;
|
|
39
|
-
*
|
|
40
|
-
* // Remove and get the first element (FIFO)
|
|
41
|
-
* const first = queue.shift();
|
|
42
|
-
* console.log(first); // 10;
|
|
43
|
-
*
|
|
44
|
-
* // Verify remaining elements and length decreased
|
|
45
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
46
|
-
* console.log(queue.length); // 3;
|
|
47
|
-
* @example
|
|
48
|
-
* // Queue for...of iteration and isEmpty check
|
|
49
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
50
|
-
*
|
|
51
|
-
* const elements: string[] = [];
|
|
52
|
-
* for (const item of queue) {
|
|
53
|
-
* elements.push(item);
|
|
54
|
-
* }
|
|
55
|
-
*
|
|
56
|
-
* // Verify all elements are iterated in order
|
|
57
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
58
|
-
*
|
|
59
|
-
* // Process all elements
|
|
60
|
-
* while (queue.length > 0) {
|
|
61
|
-
* queue.shift();
|
|
62
|
-
* }
|
|
63
|
-
*
|
|
64
|
-
* console.log(queue.length); // 0;
|
|
65
|
-
* @example
|
|
66
24
|
* // Queue as message broker for event processing
|
|
67
25
|
* interface Message {
|
|
68
26
|
* id: string;
|
|
@@ -123,6 +81,10 @@ import { LinearBase } from '../base/linear-base';
|
|
|
123
81
|
*
|
|
124
82
|
* // Queue should be empty after processing all messages
|
|
125
83
|
* console.log(messageQueue.length); // 0;
|
|
84
|
+
* @example
|
|
85
|
+
* // Convert queue to array
|
|
86
|
+
* const q = new Queue<number>([10, 20, 30]);
|
|
87
|
+
* console.log(q.toArray()); // [10, 20, 30];
|
|
126
88
|
*/
|
|
127
89
|
export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
128
90
|
/**
|
|
@@ -165,12 +127,46 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
165
127
|
* Get the number of elements currently in the queue.
|
|
166
128
|
* @remarks Time O(1), Space O(1)
|
|
167
129
|
* @returns Current length.
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
* @example
|
|
142
|
+
* // Track queue length
|
|
143
|
+
* const q = new Queue<number>();
|
|
144
|
+
* console.log(q.length); // 0;
|
|
145
|
+
* q.push(1);
|
|
146
|
+
* q.push(2);
|
|
147
|
+
* console.log(q.length); // 2;
|
|
168
148
|
*/
|
|
169
149
|
get length(): number;
|
|
170
150
|
/**
|
|
171
151
|
* Get the first element (front) without removing it.
|
|
172
152
|
* @remarks Time O(1), Space O(1)
|
|
173
153
|
* @returns Front element or undefined.
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
* @example
|
|
166
|
+
* // View the front element
|
|
167
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
168
|
+
* console.log(q.first); // 'first';
|
|
169
|
+
* console.log(q.length); // 3;
|
|
174
170
|
*/
|
|
175
171
|
get first(): E | undefined;
|
|
176
172
|
/**
|
|
@@ -191,6 +187,35 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
191
187
|
* Check whether the queue is empty.
|
|
192
188
|
* @remarks Time O(1), Space O(1)
|
|
193
189
|
* @returns True if length is 0.
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
* @example
|
|
202
|
+
* // Queue for...of iteration and isEmpty check
|
|
203
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
204
|
+
*
|
|
205
|
+
* const elements: string[] = [];
|
|
206
|
+
* for (const item of queue) {
|
|
207
|
+
* elements.push(item);
|
|
208
|
+
* }
|
|
209
|
+
*
|
|
210
|
+
* // Verify all elements are iterated in order
|
|
211
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
212
|
+
*
|
|
213
|
+
* // Process all elements
|
|
214
|
+
* while (queue.length > 0) {
|
|
215
|
+
* queue.shift();
|
|
216
|
+
* }
|
|
217
|
+
*
|
|
218
|
+
* console.log(queue.length); // 0;
|
|
194
219
|
*/
|
|
195
220
|
isEmpty(): boolean;
|
|
196
221
|
/**
|
|
@@ -198,6 +223,27 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
198
223
|
* @remarks Time O(1), Space O(1)
|
|
199
224
|
* @param element - Element to enqueue.
|
|
200
225
|
* @returns True on success.
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
* @example
|
|
238
|
+
* // basic Queue creation and push operation
|
|
239
|
+
* // Create a simple Queue with initial values
|
|
240
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
241
|
+
*
|
|
242
|
+
* // Verify the queue maintains insertion order
|
|
243
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
244
|
+
*
|
|
245
|
+
* // Check length
|
|
246
|
+
* console.log(queue.length); // 5;
|
|
201
247
|
*/
|
|
202
248
|
push(element: E): boolean;
|
|
203
249
|
/**
|
|
@@ -211,6 +257,31 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
211
257
|
* Dequeue one element from the front (amortized via offset).
|
|
212
258
|
* @remarks Time O(1) amortized, Space O(1)
|
|
213
259
|
* @returns Removed element or undefined.
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
* @example
|
|
272
|
+
* // Queue shift and peek operations
|
|
273
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
274
|
+
*
|
|
275
|
+
* // Peek at the front element without removing it
|
|
276
|
+
* console.log(queue.first); // 10;
|
|
277
|
+
*
|
|
278
|
+
* // Remove and get the first element (FIFO)
|
|
279
|
+
* const first = queue.shift();
|
|
280
|
+
* console.log(first); // 10;
|
|
281
|
+
*
|
|
282
|
+
* // Verify remaining elements and length decreased
|
|
283
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
284
|
+
* console.log(queue.length); // 3;
|
|
214
285
|
*/
|
|
215
286
|
shift(): E | undefined;
|
|
216
287
|
/**
|
|
@@ -218,6 +289,19 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
218
289
|
* @remarks Time O(N), Space O(1)
|
|
219
290
|
* @param element - Element to remove (strict equality via Object.is).
|
|
220
291
|
* @returns True if an element was removed.
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
* @example
|
|
301
|
+
* // Remove specific element
|
|
302
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
303
|
+
* q.delete(2);
|
|
304
|
+
* console.log(q.length); // 3;
|
|
221
305
|
*/
|
|
222
306
|
delete(element: E): boolean;
|
|
223
307
|
/**
|
|
@@ -225,6 +309,19 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
225
309
|
* @remarks Time O(1), Space O(1)
|
|
226
310
|
* @param index - Zero-based index from the front.
|
|
227
311
|
* @returns Element or undefined.
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
* @example
|
|
321
|
+
* // Access element by index
|
|
322
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
323
|
+
* console.log(q.at(0)); // 'a';
|
|
324
|
+
* console.log(q.at(2)); // 'c';
|
|
228
325
|
*/
|
|
229
326
|
at(index: number): E | undefined;
|
|
230
327
|
/**
|
|
@@ -260,12 +357,41 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
260
357
|
* Remove all elements and reset offset.
|
|
261
358
|
* @remarks Time O(1), Space O(1)
|
|
262
359
|
* @returns void
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
* @example
|
|
370
|
+
* // Remove all elements
|
|
371
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
372
|
+
* q.clear();
|
|
373
|
+
* console.log(q.length); // 0;
|
|
263
374
|
*/
|
|
264
375
|
clear(): void;
|
|
265
376
|
/**
|
|
266
377
|
* Compact storage by discarding consumed head elements.
|
|
267
378
|
* @remarks Time O(N), Space O(N)
|
|
268
379
|
* @returns True when compaction performed.
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
* @example
|
|
389
|
+
* // Reclaim unused memory
|
|
390
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
391
|
+
* q.shift();
|
|
392
|
+
* q.shift();
|
|
393
|
+
* q.compact();
|
|
394
|
+
* console.log(q.length); // 3;
|
|
269
395
|
*/
|
|
270
396
|
compact(): boolean;
|
|
271
397
|
/**
|
|
@@ -281,6 +407,22 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
281
407
|
* Deep clone this queue and its parameters.
|
|
282
408
|
* @remarks Time O(N), Space O(N)
|
|
283
409
|
* @returns A new queue with the same content and options.
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
* @example
|
|
420
|
+
* // Create independent copy
|
|
421
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
422
|
+
* const copy = q.clone();
|
|
423
|
+
* copy.shift();
|
|
424
|
+
* console.log(q.length); // 3;
|
|
425
|
+
* console.log(copy.length); // 2;
|
|
284
426
|
*/
|
|
285
427
|
clone(): this;
|
|
286
428
|
/**
|
|
@@ -289,6 +431,20 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
289
431
|
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
290
432
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
291
433
|
* @returns A new queue with kept elements.
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
* @example
|
|
444
|
+
* // Filter elements
|
|
445
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
446
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
447
|
+
* console.log(evens.length); // 2;
|
|
292
448
|
*/
|
|
293
449
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
294
450
|
/**
|
|
@@ -300,6 +456,19 @@ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
|
|
|
300
456
|
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
301
457
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
302
458
|
* @returns A new Queue with mapped elements.
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
* @example
|
|
468
|
+
* // Transform elements
|
|
469
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
470
|
+
* const doubled = q.map(x => x * 2);
|
|
471
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
303
472
|
*/
|
|
304
473
|
map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: QueueOptions<EM, RM>, thisArg?: unknown): Queue<EM, RM>;
|
|
305
474
|
/**
|
|
@@ -19,38 +19,6 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
20
20
|
* 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.
|
|
21
21
|
* @example
|
|
22
|
-
* // basic Stack creation and push operation
|
|
23
|
-
* // Create a simple Stack with initial values
|
|
24
|
-
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
25
|
-
*
|
|
26
|
-
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
27
|
-
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
28
|
-
*
|
|
29
|
-
* // Check length
|
|
30
|
-
* console.log(stack.size); // 5;
|
|
31
|
-
*
|
|
32
|
-
* // Push a new element to the top
|
|
33
|
-
* stack.push(6);
|
|
34
|
-
* console.log(stack.size); // 6;
|
|
35
|
-
* @example
|
|
36
|
-
* // Stack pop operation (LIFO - Last In First Out)
|
|
37
|
-
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
38
|
-
*
|
|
39
|
-
* // Peek at the top element without removing
|
|
40
|
-
* const top = stack.peek();
|
|
41
|
-
* console.log(top); // 50;
|
|
42
|
-
*
|
|
43
|
-
* // Pop removes from the top (LIFO order)
|
|
44
|
-
* const popped = stack.pop();
|
|
45
|
-
* console.log(popped); // 50;
|
|
46
|
-
*
|
|
47
|
-
* // Next pop gets the previous element
|
|
48
|
-
* const next = stack.pop();
|
|
49
|
-
* console.log(next); // 40;
|
|
50
|
-
*
|
|
51
|
-
* // Verify length decreased
|
|
52
|
-
* console.log(stack.size); // 3;
|
|
53
|
-
* @example
|
|
54
22
|
* // Function Call Stack
|
|
55
23
|
* const functionStack = new Stack<string>();
|
|
56
24
|
* functionStack.push('main');
|
|
@@ -157,6 +125,10 @@ import { IterableElementBase } from '../base';
|
|
|
157
125
|
* else if (segment && segment !== '.') stack.push(segment);
|
|
158
126
|
* });
|
|
159
127
|
* console.log(stack.elements.join('/')); // 'c';
|
|
128
|
+
* @example
|
|
129
|
+
* // Convert stack to array
|
|
130
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
131
|
+
* console.log(stack.toArray()); // [1, 2, 3];
|
|
160
132
|
*/
|
|
161
133
|
export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
162
134
|
protected _equals: (a: E, b: E) => boolean;
|
|
@@ -179,6 +151,19 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
179
151
|
* Get the number of stored elements.
|
|
180
152
|
* @remarks Time O(1), Space O(1)
|
|
181
153
|
* @returns Current size.
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
* @example
|
|
164
|
+
* // Get number of elements
|
|
165
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
166
|
+
* console.log(stack.size); // 3;
|
|
182
167
|
*/
|
|
183
168
|
get size(): number;
|
|
184
169
|
/**
|
|
@@ -196,12 +181,45 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
196
181
|
* Check whether the stack is empty.
|
|
197
182
|
* @remarks Time O(1), Space O(1)
|
|
198
183
|
* @returns True if size is 0.
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
* @example
|
|
196
|
+
* // Check if stack has elements
|
|
197
|
+
* const stack = new Stack<number>();
|
|
198
|
+
* console.log(stack.isEmpty()); // true;
|
|
199
|
+
* stack.push(1);
|
|
200
|
+
* console.log(stack.isEmpty()); // false;
|
|
199
201
|
*/
|
|
200
202
|
isEmpty(): boolean;
|
|
201
203
|
/**
|
|
202
204
|
* Get the top element without removing it.
|
|
203
205
|
* @remarks Time O(1), Space O(1)
|
|
204
206
|
* @returns Top element or undefined.
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
* @example
|
|
219
|
+
* // View the top element without removing it
|
|
220
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
221
|
+
* console.log(stack.peek()); // 'c';
|
|
222
|
+
* console.log(stack.size); // 3;
|
|
205
223
|
*/
|
|
206
224
|
peek(): E | undefined;
|
|
207
225
|
/**
|
|
@@ -209,12 +227,66 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
209
227
|
* @remarks Time O(1), Space O(1)
|
|
210
228
|
* @param element - Element to push.
|
|
211
229
|
* @returns True when pushed.
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
* @example
|
|
242
|
+
* // basic Stack creation and push operation
|
|
243
|
+
* // Create a simple Stack with initial values
|
|
244
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
245
|
+
*
|
|
246
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
247
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
248
|
+
*
|
|
249
|
+
* // Check length
|
|
250
|
+
* console.log(stack.size); // 5;
|
|
251
|
+
*
|
|
252
|
+
* // Push a new element to the top
|
|
253
|
+
* stack.push(6);
|
|
254
|
+
* console.log(stack.size); // 6;
|
|
212
255
|
*/
|
|
213
256
|
push(element: E): boolean;
|
|
214
257
|
/**
|
|
215
258
|
* Pop and return the top element.
|
|
216
259
|
* @remarks Time O(1), Space O(1)
|
|
217
260
|
* @returns Removed element or undefined.
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
* @example
|
|
273
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
274
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
275
|
+
*
|
|
276
|
+
* // Peek at the top element without removing
|
|
277
|
+
* const top = stack.peek();
|
|
278
|
+
* console.log(top); // 50;
|
|
279
|
+
*
|
|
280
|
+
* // Pop removes from the top (LIFO order)
|
|
281
|
+
* const popped = stack.pop();
|
|
282
|
+
* console.log(popped); // 50;
|
|
283
|
+
*
|
|
284
|
+
* // Next pop gets the previous element
|
|
285
|
+
* const next = stack.pop();
|
|
286
|
+
* console.log(next); // 40;
|
|
287
|
+
*
|
|
288
|
+
* // Verify length decreased
|
|
289
|
+
* console.log(stack.size); // 3;
|
|
218
290
|
*/
|
|
219
291
|
pop(): E | undefined;
|
|
220
292
|
/**
|
|
@@ -229,6 +301,19 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
229
301
|
* @remarks Time O(N), Space O(1)
|
|
230
302
|
* @param element - Element to remove (using the configured equality).
|
|
231
303
|
* @returns True if an element was removed.
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
* @example
|
|
313
|
+
* // Remove element
|
|
314
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
315
|
+
* stack.delete(2);
|
|
316
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
232
317
|
*/
|
|
233
318
|
delete(element: E): boolean;
|
|
234
319
|
/**
|
|
@@ -249,12 +334,42 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
249
334
|
* Remove all elements and reset storage.
|
|
250
335
|
* @remarks Time O(1), Space O(1)
|
|
251
336
|
* @returns void
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
* @example
|
|
347
|
+
* // Remove all elements
|
|
348
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
349
|
+
* stack.clear();
|
|
350
|
+
* console.log(stack.isEmpty()); // true;
|
|
252
351
|
*/
|
|
253
352
|
clear(): void;
|
|
254
353
|
/**
|
|
255
354
|
* Deep clone this stack.
|
|
256
355
|
* @remarks Time O(N), Space O(N)
|
|
257
356
|
* @returns A new stack with the same content.
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
* @example
|
|
367
|
+
* // Create independent copy
|
|
368
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
369
|
+
* const copy = stack.clone();
|
|
370
|
+
* copy.pop();
|
|
371
|
+
* console.log(stack.size); // 3;
|
|
372
|
+
* console.log(copy.size); // 2;
|
|
258
373
|
*/
|
|
259
374
|
clone(): this;
|
|
260
375
|
/**
|
|
@@ -263,6 +378,20 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
263
378
|
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
264
379
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
265
380
|
* @returns A new stack with kept values.
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
* @example
|
|
391
|
+
* // Filter elements
|
|
392
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
393
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
394
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
266
395
|
*/
|
|
267
396
|
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
268
397
|
/**
|
|
@@ -282,6 +411,19 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
282
411
|
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
283
412
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
284
413
|
* @returns A new Stack with mapped elements.
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
* @example
|
|
423
|
+
* // Transform elements
|
|
424
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
425
|
+
* const doubled = stack.map(x => x * 2);
|
|
426
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
285
427
|
*/
|
|
286
428
|
map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: IterableElementBaseOptions<EM, RM>, thisArg?: unknown): Stack<EM, RM>;
|
|
287
429
|
/**
|