max-priority-queue-typed 2.4.5 → 2.5.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/README.md +63 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -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 +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -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 +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/max-priority-queue-typed.js +691 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- 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 +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -27,71 +27,6 @@ import { LinearBase } from '../base/linear-base';
|
|
|
27
27
|
* 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
|
|
28
28
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
29
29
|
* @example
|
|
30
|
-
* // basic Deque creation and push/pop operations
|
|
31
|
-
* // Create a simple Deque with initial values
|
|
32
|
-
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
33
|
-
*
|
|
34
|
-
* // Verify the deque maintains insertion order
|
|
35
|
-
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
36
|
-
*
|
|
37
|
-
* // Check length
|
|
38
|
-
* console.log(deque.length); // 5;
|
|
39
|
-
*
|
|
40
|
-
* // Push to the end
|
|
41
|
-
* deque.push(6);
|
|
42
|
-
* console.log(deque.length); // 6;
|
|
43
|
-
*
|
|
44
|
-
* // Pop from the end
|
|
45
|
-
* const last = deque.pop();
|
|
46
|
-
* console.log(last); // 6;
|
|
47
|
-
* @example
|
|
48
|
-
* // Deque shift and unshift operations
|
|
49
|
-
* const deque = new Deque<number>([20, 30, 40]);
|
|
50
|
-
*
|
|
51
|
-
* // Unshift adds to the front
|
|
52
|
-
* deque.unshift(10);
|
|
53
|
-
* console.log([...deque]); // [10, 20, 30, 40];
|
|
54
|
-
*
|
|
55
|
-
* // Shift removes from the front (O(1) complexity!)
|
|
56
|
-
* const first = deque.shift();
|
|
57
|
-
* console.log(first); // 10;
|
|
58
|
-
*
|
|
59
|
-
* // Verify remaining elements
|
|
60
|
-
* console.log([...deque]); // [20, 30, 40];
|
|
61
|
-
* console.log(deque.length); // 3;
|
|
62
|
-
* @example
|
|
63
|
-
* // Deque peek at both ends
|
|
64
|
-
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
65
|
-
*
|
|
66
|
-
* // Get first element without removing
|
|
67
|
-
* const first = deque.at(0);
|
|
68
|
-
* console.log(first); // 10;
|
|
69
|
-
*
|
|
70
|
-
* // Get last element without removing
|
|
71
|
-
* const last = deque.at(deque.length - 1);
|
|
72
|
-
* console.log(last); // 50;
|
|
73
|
-
*
|
|
74
|
-
* // Length unchanged
|
|
75
|
-
* console.log(deque.length); // 5;
|
|
76
|
-
* @example
|
|
77
|
-
* // Deque for...of iteration and reverse
|
|
78
|
-
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
79
|
-
*
|
|
80
|
-
* // Iterate forward
|
|
81
|
-
* const forward: string[] = [];
|
|
82
|
-
* for (const item of deque) {
|
|
83
|
-
* forward.push(item);
|
|
84
|
-
* }
|
|
85
|
-
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
86
|
-
*
|
|
87
|
-
* // Reverse the deque
|
|
88
|
-
* deque.reverse();
|
|
89
|
-
* const backward: string[] = [];
|
|
90
|
-
* for (const item of deque) {
|
|
91
|
-
* backward.push(item);
|
|
92
|
-
* }
|
|
93
|
-
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
94
|
-
* @example
|
|
95
30
|
* // Deque as sliding window for stream processing
|
|
96
31
|
* interface DataPoint {
|
|
97
32
|
* timestamp: number;
|
|
@@ -142,6 +77,10 @@ import { LinearBase } from '../base/linear-base';
|
|
|
142
77
|
* console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
|
|
143
78
|
* console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
|
|
144
79
|
* console.log(dataWindow.length); // 3;
|
|
80
|
+
* @example
|
|
81
|
+
* // Convert deque to array
|
|
82
|
+
* const dq = new Deque<number>([10, 20, 30]);
|
|
83
|
+
* console.log(dq.toArray()); // [10, 20, 30];
|
|
145
84
|
*/
|
|
146
85
|
export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
147
86
|
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
@@ -310,6 +249,52 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
310
249
|
* Get the first element without removing it.
|
|
311
250
|
* @remarks Time O(1), Space O(1)
|
|
312
251
|
* @returns First element or undefined.
|
|
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
|
+
|
|
283
|
+
|
|
284
|
+
* @example
|
|
285
|
+
* // Deque peek at both ends
|
|
286
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
287
|
+
*
|
|
288
|
+
* // Get first element without removing
|
|
289
|
+
* const first = deque.at(0);
|
|
290
|
+
* console.log(first); // 10;
|
|
291
|
+
*
|
|
292
|
+
* // Get last element without removing
|
|
293
|
+
* const last = deque.at(deque.length - 1);
|
|
294
|
+
* console.log(last); // 50;
|
|
295
|
+
*
|
|
296
|
+
* // Length unchanged
|
|
297
|
+
* console.log(deque.length); // 5;
|
|
313
298
|
*/
|
|
314
299
|
|
|
315
300
|
get first(): E | undefined {
|
|
@@ -321,6 +306,43 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
321
306
|
* Get the last element without removing it.
|
|
322
307
|
* @remarks Time O(1), Space O(1)
|
|
323
308
|
* @returns Last element or undefined.
|
|
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
|
+
|
|
341
|
+
* @example
|
|
342
|
+
* // Peek at the back element
|
|
343
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
344
|
+
* console.log(dq.last); // 'c';
|
|
345
|
+
* console.log(dq.first); // 'a';
|
|
324
346
|
*/
|
|
325
347
|
|
|
326
348
|
get last(): E | undefined {
|
|
@@ -355,6 +377,56 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
355
377
|
* @remarks Time O(1) amortized, Space O(1)
|
|
356
378
|
* @param element - Element to append.
|
|
357
379
|
* @returns True when appended.
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
* @example
|
|
413
|
+
* // basic Deque creation and push/pop operations
|
|
414
|
+
* // Create a simple Deque with initial values
|
|
415
|
+
* const deque = new Deque([1, 2, 3, 4, 5]);
|
|
416
|
+
*
|
|
417
|
+
* // Verify the deque maintains insertion order
|
|
418
|
+
* console.log([...deque]); // [1, 2, 3, 4, 5];
|
|
419
|
+
*
|
|
420
|
+
* // Check length
|
|
421
|
+
* console.log(deque.length); // 5;
|
|
422
|
+
*
|
|
423
|
+
* // Push to the end
|
|
424
|
+
* deque.push(6);
|
|
425
|
+
* console.log(deque.length); // 6;
|
|
426
|
+
*
|
|
427
|
+
* // Pop from the end
|
|
428
|
+
* const last = deque.pop();
|
|
429
|
+
* console.log(last); // 6;
|
|
358
430
|
*/
|
|
359
431
|
|
|
360
432
|
push(element: E): boolean {
|
|
@@ -380,6 +452,43 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
380
452
|
* Remove and return the last element.
|
|
381
453
|
* @remarks Time O(1), Space O(1)
|
|
382
454
|
* @returns Removed element or undefined.
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
* @example
|
|
488
|
+
* // Remove from the back
|
|
489
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
490
|
+
* console.log(dq.pop()); // 3;
|
|
491
|
+
* console.log(dq.length); // 2;
|
|
383
492
|
*/
|
|
384
493
|
|
|
385
494
|
pop(): E | undefined {
|
|
@@ -405,6 +514,43 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
405
514
|
* Remove and return the first element.
|
|
406
515
|
* @remarks Time O(1) amortized, Space O(1)
|
|
407
516
|
* @returns Removed element or undefined.
|
|
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 from the front
|
|
551
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
552
|
+
* console.log(dq.shift()); // 1;
|
|
553
|
+
* console.log(dq.length); // 2;
|
|
408
554
|
*/
|
|
409
555
|
|
|
410
556
|
shift(): E | undefined {
|
|
@@ -431,6 +577,53 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
431
577
|
* @remarks Time O(1) amortized, Space O(1)
|
|
432
578
|
* @param element - Element to prepend.
|
|
433
579
|
* @returns True when prepended.
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
* @example
|
|
613
|
+
* // Deque shift and unshift operations
|
|
614
|
+
* const deque = new Deque<number>([20, 30, 40]);
|
|
615
|
+
*
|
|
616
|
+
* // Unshift adds to the front
|
|
617
|
+
* deque.unshift(10);
|
|
618
|
+
* console.log([...deque]); // [10, 20, 30, 40];
|
|
619
|
+
*
|
|
620
|
+
* // Shift removes from the front (O(1) complexity!)
|
|
621
|
+
* const first = deque.shift();
|
|
622
|
+
* console.log(first); // 10;
|
|
623
|
+
*
|
|
624
|
+
* // Verify remaining elements
|
|
625
|
+
* console.log([...deque]); // [20, 30, 40];
|
|
626
|
+
* console.log(deque.length); // 3;
|
|
434
627
|
*/
|
|
435
628
|
|
|
436
629
|
unshift(element: E): boolean {
|
|
@@ -494,6 +687,40 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
494
687
|
* Check whether the deque is empty.
|
|
495
688
|
* @remarks Time O(1), Space O(1)
|
|
496
689
|
* @returns True if length is 0.
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
* @example
|
|
721
|
+
* // Check if empty
|
|
722
|
+
* const dq = new Deque();
|
|
723
|
+
* console.log(dq.isEmpty()); // true;
|
|
497
724
|
*/
|
|
498
725
|
|
|
499
726
|
isEmpty(): boolean {
|
|
@@ -504,6 +731,41 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
504
731
|
* Remove all elements and reset structure.
|
|
505
732
|
* @remarks Time O(1), Space O(1)
|
|
506
733
|
* @returns void
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
* @example
|
|
765
|
+
* // Remove all elements
|
|
766
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
767
|
+
* dq.clear();
|
|
768
|
+
* console.log(dq.length); // 0;
|
|
507
769
|
*/
|
|
508
770
|
|
|
509
771
|
clear(): void {
|
|
@@ -518,6 +780,40 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
518
780
|
* @remarks Time O(1), Space O(1)
|
|
519
781
|
* @param pos - Zero-based position from the front.
|
|
520
782
|
* @returns Element or undefined.
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
* @example
|
|
813
|
+
* // Access by index
|
|
814
|
+
* const dq = new Deque<string>(['a', 'b', 'c']);
|
|
815
|
+
* console.log(dq.at(0)); // 'a';
|
|
816
|
+
* console.log(dq.at(2)); // 'c';
|
|
521
817
|
*/
|
|
522
818
|
|
|
523
819
|
at(pos: number): E | undefined {
|
|
@@ -705,6 +1001,40 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
705
1001
|
* @remarks Time O(N), Space O(1)
|
|
706
1002
|
* @param element - Element to remove (using the configured equality).
|
|
707
1003
|
* @returns True if an element was removed.
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
* @example
|
|
1034
|
+
* // Remove element
|
|
1035
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1036
|
+
* dq.delete(2);
|
|
1037
|
+
* console.log(dq.length); // 2;
|
|
708
1038
|
*/
|
|
709
1039
|
|
|
710
1040
|
delete(element: E): boolean {
|
|
@@ -758,6 +1088,56 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
758
1088
|
* Reverse the deque by reversing buckets and pointers.
|
|
759
1089
|
* @remarks Time O(N), Space O(N)
|
|
760
1090
|
* @returns This deque.
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
* @example
|
|
1124
|
+
* // Deque for...of iteration and reverse
|
|
1125
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1126
|
+
*
|
|
1127
|
+
* // Iterate forward
|
|
1128
|
+
* const forward: string[] = [];
|
|
1129
|
+
* for (const item of deque) {
|
|
1130
|
+
* forward.push(item);
|
|
1131
|
+
* }
|
|
1132
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1133
|
+
*
|
|
1134
|
+
* // Reverse the deque
|
|
1135
|
+
* deque.reverse();
|
|
1136
|
+
* const backward: string[] = [];
|
|
1137
|
+
* for (const item of deque) {
|
|
1138
|
+
* backward.push(item);
|
|
1139
|
+
* }
|
|
1140
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
761
1141
|
*/
|
|
762
1142
|
|
|
763
1143
|
reverse(): this {
|
|
@@ -828,6 +1208,42 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
828
1208
|
* Compact the deque by removing unused buckets.
|
|
829
1209
|
* @remarks Time O(N), Space O(1)
|
|
830
1210
|
* @returns True if compaction was performed (bucket count reduced).
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
* @example
|
|
1241
|
+
* // Reclaim memory
|
|
1242
|
+
* const dq = new Deque<number>([1, 2, 3, 4, 5]);
|
|
1243
|
+
* dq.shift();
|
|
1244
|
+
* dq.shift();
|
|
1245
|
+
* dq.compact();
|
|
1246
|
+
* console.log(dq.length); // 3;
|
|
831
1247
|
*/
|
|
832
1248
|
compact(): boolean {
|
|
833
1249
|
const before = this._bucketCount;
|
|
@@ -861,6 +1277,43 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
861
1277
|
* Deep clone this deque, preserving options.
|
|
862
1278
|
* @remarks Time O(N), Space O(N)
|
|
863
1279
|
* @returns A new deque with the same content and options.
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
* @example
|
|
1311
|
+
* // Create independent copy
|
|
1312
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1313
|
+
* const copy = dq.clone();
|
|
1314
|
+
* copy.pop();
|
|
1315
|
+
* console.log(dq.length); // 3;
|
|
1316
|
+
* console.log(copy.length); // 2;
|
|
864
1317
|
*/
|
|
865
1318
|
|
|
866
1319
|
clone(): this {
|
|
@@ -877,9 +1330,44 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
877
1330
|
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
878
1331
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
879
1332
|
* @returns A new deque with kept elements.
|
|
1333
|
+
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
* @example
|
|
1364
|
+
* // Filter elements
|
|
1365
|
+
* const dq = new Deque<number>([1, 2, 3, 4]);
|
|
1366
|
+
* const result = dq.filter(x => x > 2);
|
|
1367
|
+
* console.log(result.length); // 2;
|
|
880
1368
|
*/
|
|
881
1369
|
|
|
882
|
-
filter(predicate: ElementCallback<E, R, boolean>, thisArg?:
|
|
1370
|
+
filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
883
1371
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
884
1372
|
out._setBucketSize(this._bucketSize);
|
|
885
1373
|
let index = 0;
|
|
@@ -898,7 +1386,7 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
898
1386
|
* @returns A new deque with mapped values.
|
|
899
1387
|
*/
|
|
900
1388
|
|
|
901
|
-
mapSame(callback: ElementCallback<E, R, E>, thisArg?:
|
|
1389
|
+
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
|
|
902
1390
|
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
903
1391
|
out._setBucketSize(this._bucketSize);
|
|
904
1392
|
let index = 0;
|
|
@@ -918,12 +1406,46 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
918
1406
|
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
919
1407
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
920
1408
|
* @returns A new Deque with mapped elements.
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
|
|
1438
|
+
* @example
|
|
1439
|
+
* // Transform elements
|
|
1440
|
+
* const dq = new Deque<number>([1, 2, 3]);
|
|
1441
|
+
* const result = dq.map(x => x * 10);
|
|
1442
|
+
* console.log(result.toArray()); // [10, 20, 30];
|
|
921
1443
|
*/
|
|
922
1444
|
|
|
923
1445
|
map<EM, RM>(
|
|
924
1446
|
callback: ElementCallback<E, R, EM>,
|
|
925
1447
|
options?: IterableElementBaseOptions<EM, RM>,
|
|
926
|
-
thisArg?:
|
|
1448
|
+
thisArg?: unknown
|
|
927
1449
|
): Deque<EM, RM> {
|
|
928
1450
|
const out = this._createLike<EM, RM>([], {
|
|
929
1451
|
...(options ?? {}),
|
|
@@ -1056,11 +1578,11 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
1056
1578
|
protected _createLike<T = E, RR = R>(
|
|
1057
1579
|
elements: IterableWithSizeOrLength<T> | IterableWithSizeOrLength<RR> = [],
|
|
1058
1580
|
options?: DequeOptions<T, RR>
|
|
1059
|
-
):
|
|
1581
|
+
): Deque<T, RR> {
|
|
1060
1582
|
const Ctor = this.constructor as new (
|
|
1061
1583
|
elements?: IterableWithSizeOrLength<T> | IterableWithSizeOrLength<RR>,
|
|
1062
1584
|
options?: DequeOptions<T, RR>
|
|
1063
|
-
) =>
|
|
1585
|
+
) => Deque<T, RR>;
|
|
1064
1586
|
return new Ctor(elements, options);
|
|
1065
1587
|
}
|
|
1066
1588
|
|