data-structure-typed 1.48.0 → 1.48.2
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 +1 -1
- package/README.md +24 -18
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +188 -308
- package/dist/cjs/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/data-structures/base/index.js +18 -0
- package/dist/cjs/data-structures/base/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/cjs/data-structures/base/iterable-base.js +313 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/cjs/data-structures/hash/hash-map.js +314 -82
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/cjs/data-structures/heap/heap.js +60 -30
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/index.d.ts +1 -0
- package/dist/cjs/data-structures/index.js +1 -0
- package/dist/cjs/data-structures/index.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/cjs/data-structures/queue/deque.js +36 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/cjs/data-structures/queue/queue.js +69 -82
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/cjs/data-structures/stack/stack.js +50 -31
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/cjs/data-structures/trie/trie.js +53 -32
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/cjs/types/data-structures/base/base.js +3 -0
- package/dist/cjs/types/data-structures/base/base.js.map +1 -0
- package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/base/index.js +18 -0
- package/dist/cjs/types/data-structures/base/index.js.map +1 -0
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/cjs/types/data-structures/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/index.js +1 -0
- package/dist/cjs/types/data-structures/index.js.map +1 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/data-structures/base/index.js +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/mjs/data-structures/base/iterable-base.js +307 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
- package/dist/mjs/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/mjs/data-structures/hash/hash-map.js +310 -80
- package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/mjs/data-structures/heap/heap.js +60 -30
- package/dist/mjs/data-structures/index.d.ts +1 -0
- package/dist/mjs/data-structures/index.js +1 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/mjs/data-structures/queue/deque.js +36 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/mjs/data-structures/queue/queue.js +66 -79
- package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/mjs/data-structures/stack/stack.js +50 -31
- package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/mjs/data-structures/trie/trie.js +53 -32
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/mjs/types/data-structures/base/base.js +1 -0
- package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/base/index.js +1 -0
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/mjs/types/data-structures/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/index.js +1 -0
- package/dist/umd/data-structure-typed.js +1104 -575
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-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-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +334 -83
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/index.ts +1 -0
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +21 -25
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -8
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +24 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +24 -8
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
- package/test/unit/data-structures/queue/deque.test.ts +25 -0
|
@@ -126,7 +126,10 @@ var dataStructureTyped = (() => {
|
|
|
126
126
|
HashTable: () => HashTable,
|
|
127
127
|
HashTableNode: () => HashTableNode,
|
|
128
128
|
Heap: () => Heap,
|
|
129
|
+
IterableElementBase: () => IterableElementBase,
|
|
130
|
+
IterablePairBase: () => IterablePairBase,
|
|
129
131
|
IterationType: () => IterationType,
|
|
132
|
+
LinkedHashMap: () => LinkedHashMap,
|
|
130
133
|
LinkedListQueue: () => LinkedListQueue,
|
|
131
134
|
MapEdge: () => MapEdge,
|
|
132
135
|
MapGraph: () => MapGraph,
|
|
@@ -515,12 +518,534 @@ var dataStructureTyped = (() => {
|
|
|
515
518
|
};
|
|
516
519
|
var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
517
520
|
|
|
521
|
+
// src/data-structures/base/iterable-base.ts
|
|
522
|
+
var IterablePairBase = class {
|
|
523
|
+
/**
|
|
524
|
+
* Time Complexity: O(n)
|
|
525
|
+
* Space Complexity: O(1)
|
|
526
|
+
*/
|
|
527
|
+
/**
|
|
528
|
+
* Time Complexity: O(n)
|
|
529
|
+
* Space Complexity: O(1)
|
|
530
|
+
*
|
|
531
|
+
* The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
|
|
532
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
533
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
534
|
+
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
535
|
+
*/
|
|
536
|
+
*[Symbol.iterator](...args) {
|
|
537
|
+
yield* __yieldStar(this._getIterator(...args));
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Time Complexity: O(n)
|
|
541
|
+
* Space Complexity: O(n)
|
|
542
|
+
*/
|
|
543
|
+
/**
|
|
544
|
+
* Time Complexity: O(n)
|
|
545
|
+
* Space Complexity: O(n)
|
|
546
|
+
*
|
|
547
|
+
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
548
|
+
* be undefined.
|
|
549
|
+
*/
|
|
550
|
+
*entries() {
|
|
551
|
+
for (const item of this) {
|
|
552
|
+
yield item;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Time Complexity: O(n)
|
|
557
|
+
* Space Complexity: O(n)
|
|
558
|
+
*/
|
|
559
|
+
/**
|
|
560
|
+
* Time Complexity: O(n)
|
|
561
|
+
* Space Complexity: O(n)
|
|
562
|
+
*
|
|
563
|
+
* The function returns an iterator that yields the keys of a data structure.
|
|
564
|
+
*/
|
|
565
|
+
*keys() {
|
|
566
|
+
for (const item of this) {
|
|
567
|
+
yield item[0];
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Time Complexity: O(n)
|
|
572
|
+
* Space Complexity: O(n)
|
|
573
|
+
*/
|
|
574
|
+
/**
|
|
575
|
+
* Time Complexity: O(n)
|
|
576
|
+
* Space Complexity: O(n)
|
|
577
|
+
*
|
|
578
|
+
* The function returns an iterator that yields the values of a collection.
|
|
579
|
+
*/
|
|
580
|
+
*values() {
|
|
581
|
+
for (const item of this) {
|
|
582
|
+
yield item[1];
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Time Complexity: O(n)
|
|
587
|
+
* Space Complexity: O(1)
|
|
588
|
+
*/
|
|
589
|
+
/**
|
|
590
|
+
* Time Complexity: O(n)
|
|
591
|
+
* Space Complexity: O(1)
|
|
592
|
+
*
|
|
593
|
+
* The `every` function checks if every element in a collection satisfies a given condition.
|
|
594
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
595
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
596
|
+
* met for the current element in the iteration.
|
|
597
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
598
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
599
|
+
* passed as the first argument to the `predicate` function. If `thisArg` is not provided
|
|
600
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
601
|
+
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
602
|
+
*/
|
|
603
|
+
every(predicate, thisArg) {
|
|
604
|
+
let index = 0;
|
|
605
|
+
for (const item of this) {
|
|
606
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Time Complexity: O(n)
|
|
614
|
+
* Space Complexity: O(1)
|
|
615
|
+
*/
|
|
616
|
+
/**
|
|
617
|
+
* Time Complexity: O(n)
|
|
618
|
+
* Space Complexity: O(1)
|
|
619
|
+
*
|
|
620
|
+
* The "some" function iterates over a collection and returns true if at least one element satisfies
|
|
621
|
+
* a given predicate.
|
|
622
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
623
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
624
|
+
* met for the current element in the iteration.
|
|
625
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
626
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
627
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
628
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
629
|
+
* the collection, and false otherwise.
|
|
630
|
+
*/
|
|
631
|
+
some(predicate, thisArg) {
|
|
632
|
+
let index = 0;
|
|
633
|
+
for (const item of this) {
|
|
634
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
635
|
+
return true;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Time Complexity: O(n)
|
|
642
|
+
* Space Complexity: O(1)
|
|
643
|
+
*/
|
|
644
|
+
/**
|
|
645
|
+
* Time Complexity: O(n)
|
|
646
|
+
* Space Complexity: O(1)
|
|
647
|
+
*
|
|
648
|
+
* The `forEach` function iterates over each key-value pair in a collection and executes a callback
|
|
649
|
+
* function for each pair.
|
|
650
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
651
|
+
* It takes four parameters: the value of the current element, the key of the current element, the
|
|
652
|
+
* index of the current element, and the collection itself.
|
|
653
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
654
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
655
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
656
|
+
*/
|
|
657
|
+
forEach(callbackfn, thisArg) {
|
|
658
|
+
let index = 0;
|
|
659
|
+
for (const item of this) {
|
|
660
|
+
const [key, value] = item;
|
|
661
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Time Complexity: O(n)
|
|
666
|
+
* Space Complexity: O(1)
|
|
667
|
+
*/
|
|
668
|
+
/**
|
|
669
|
+
* Time Complexity: O(n)
|
|
670
|
+
* Space Complexity: O(1)
|
|
671
|
+
*
|
|
672
|
+
* The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
|
|
673
|
+
* accumulating a single value.
|
|
674
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
675
|
+
* It takes four arguments: the current accumulator value, the current value of the element, the key
|
|
676
|
+
* of the element, and the index of the element in the collection. It should return the updated
|
|
677
|
+
* accumulator value.
|
|
678
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
679
|
+
* is the value that will be used as the first argument to the `callbackfn` function when reducing
|
|
680
|
+
* the elements of the collection.
|
|
681
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
682
|
+
* all the elements in the collection.
|
|
683
|
+
*/
|
|
684
|
+
reduce(callbackfn, initialValue) {
|
|
685
|
+
let accumulator = initialValue;
|
|
686
|
+
let index = 0;
|
|
687
|
+
for (const item of this) {
|
|
688
|
+
const [key, value] = item;
|
|
689
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
690
|
+
}
|
|
691
|
+
return accumulator;
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
var IterableElementBase = class {
|
|
695
|
+
/**
|
|
696
|
+
* Time Complexity: O(n)
|
|
697
|
+
* Space Complexity: O(1)
|
|
698
|
+
*/
|
|
699
|
+
/**
|
|
700
|
+
* Time Complexity: O(n)
|
|
701
|
+
* Space Complexity: O(1)
|
|
702
|
+
*
|
|
703
|
+
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
704
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
705
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
706
|
+
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
707
|
+
*/
|
|
708
|
+
*[Symbol.iterator](...args) {
|
|
709
|
+
yield* __yieldStar(this._getIterator(...args));
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Time Complexity: O(n)
|
|
713
|
+
* Space Complexity: O(n)
|
|
714
|
+
*/
|
|
715
|
+
/**
|
|
716
|
+
* Time Complexity: O(n)
|
|
717
|
+
* Space Complexity: O(n)
|
|
718
|
+
*
|
|
719
|
+
* The function returns an iterator that yields all the values in the object.
|
|
720
|
+
*/
|
|
721
|
+
*values() {
|
|
722
|
+
for (const item of this) {
|
|
723
|
+
yield item;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Time Complexity: O(n)
|
|
728
|
+
* Space Complexity: O(1)
|
|
729
|
+
*/
|
|
730
|
+
/**
|
|
731
|
+
* Time Complexity: O(n)
|
|
732
|
+
* Space Complexity: O(1)
|
|
733
|
+
*
|
|
734
|
+
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
735
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
736
|
+
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
737
|
+
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
738
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
739
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
740
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
741
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
742
|
+
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
743
|
+
*/
|
|
744
|
+
every(predicate, thisArg) {
|
|
745
|
+
let index = 0;
|
|
746
|
+
for (const item of this) {
|
|
747
|
+
if (!predicate.call(thisArg, item, index++, this)) {
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Time Complexity: O(n)
|
|
755
|
+
* Space Complexity: O(1)
|
|
756
|
+
*/
|
|
757
|
+
/**
|
|
758
|
+
* Time Complexity: O(n)
|
|
759
|
+
* Space Complexity: O(1)
|
|
760
|
+
*
|
|
761
|
+
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
762
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
763
|
+
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
764
|
+
* element satisfies the condition.
|
|
765
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
766
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
767
|
+
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
768
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
769
|
+
* in the collection, and false otherwise.
|
|
770
|
+
*/
|
|
771
|
+
some(predicate, thisArg) {
|
|
772
|
+
let index = 0;
|
|
773
|
+
for (const item of this) {
|
|
774
|
+
if (predicate.call(thisArg, item, index++, this)) {
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return false;
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Time Complexity: O(n)
|
|
782
|
+
* Space Complexity: O(1)
|
|
783
|
+
*/
|
|
784
|
+
/**
|
|
785
|
+
* Time Complexity: O(n)
|
|
786
|
+
* Space Complexity: O(1)
|
|
787
|
+
*
|
|
788
|
+
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
789
|
+
* function for each element.
|
|
790
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
791
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
792
|
+
* element, and the array that forEach was called upon.
|
|
793
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
794
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
795
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
796
|
+
*/
|
|
797
|
+
forEach(callbackfn, thisArg) {
|
|
798
|
+
let index = 0;
|
|
799
|
+
for (const item of this) {
|
|
800
|
+
callbackfn.call(thisArg, item, index++, this);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Time Complexity: O(n)
|
|
805
|
+
* Space Complexity: O(1)
|
|
806
|
+
*/
|
|
807
|
+
/**
|
|
808
|
+
* Time Complexity: O(n)
|
|
809
|
+
* Space Complexity: O(1)
|
|
810
|
+
*
|
|
811
|
+
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
812
|
+
* function to reduce them into a single value.
|
|
813
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
814
|
+
* the array. It takes four arguments:
|
|
815
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
816
|
+
* is the value that the accumulator starts with before the reduction operation begins.
|
|
817
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
818
|
+
* all the elements in the array and applying the callback function to each element.
|
|
819
|
+
*/
|
|
820
|
+
reduce(callbackfn, initialValue) {
|
|
821
|
+
let accumulator = initialValue;
|
|
822
|
+
let index = 0;
|
|
823
|
+
for (const item of this) {
|
|
824
|
+
accumulator = callbackfn(accumulator, item, index++, this);
|
|
825
|
+
}
|
|
826
|
+
return accumulator;
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
|
|
518
830
|
// src/data-structures/hash/hash-map.ts
|
|
519
|
-
var HashMap = class _HashMap {
|
|
831
|
+
var HashMap = class _HashMap extends IterablePairBase {
|
|
832
|
+
/**
|
|
833
|
+
* The constructor function initializes a new instance of a class with optional elements and options.
|
|
834
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
835
|
+
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
836
|
+
* key-value pairs.
|
|
837
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
838
|
+
* configuration options for the constructor. In this case, it has one property:
|
|
839
|
+
*/
|
|
840
|
+
constructor(elements = [], options) {
|
|
841
|
+
super();
|
|
842
|
+
__publicField(this, "_store", {});
|
|
843
|
+
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
844
|
+
__publicField(this, "_size", 0);
|
|
845
|
+
__publicField(this, "_hashFn", (key) => String(key));
|
|
846
|
+
if (options) {
|
|
847
|
+
const { hashFn } = options;
|
|
848
|
+
if (hashFn) {
|
|
849
|
+
this._hashFn = hashFn;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
if (elements) {
|
|
853
|
+
this.setMany(elements);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
get size() {
|
|
857
|
+
return this._size;
|
|
858
|
+
}
|
|
859
|
+
isEmpty() {
|
|
860
|
+
return this.size === 0;
|
|
861
|
+
}
|
|
862
|
+
clear() {
|
|
863
|
+
this._store = {};
|
|
864
|
+
this._objMap.clear();
|
|
865
|
+
this._size = 0;
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
869
|
+
* the key is not already present.
|
|
870
|
+
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
871
|
+
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be
|
|
872
|
+
* stored in a regular JavaScript object.
|
|
873
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
874
|
+
* key in the data structure.
|
|
875
|
+
*/
|
|
876
|
+
set(key, value) {
|
|
877
|
+
if (this._isObjKey(key)) {
|
|
878
|
+
if (!this._objMap.has(key)) {
|
|
879
|
+
this._size++;
|
|
880
|
+
}
|
|
881
|
+
this._objMap.set(key, value);
|
|
882
|
+
} else {
|
|
883
|
+
const strKey = this._getNoObjKey(key);
|
|
884
|
+
if (this._store[strKey] === void 0) {
|
|
885
|
+
this._size++;
|
|
886
|
+
}
|
|
887
|
+
this._store[strKey] = { key, value };
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* The function "setMany" sets multiple key-value pairs in a map.
|
|
892
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
893
|
+
* key-value pair is represented as an array with two elements: the key and the value.
|
|
894
|
+
*/
|
|
895
|
+
setMany(elements) {
|
|
896
|
+
for (const [key, value] of elements)
|
|
897
|
+
this.set(key, value);
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
901
|
+
* a string map.
|
|
902
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
903
|
+
* of any type, but it should be compatible with the key type used when the map was created.
|
|
904
|
+
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
|
|
905
|
+
* or `_store`, otherwise it returns `undefined`.
|
|
906
|
+
*/
|
|
907
|
+
get(key) {
|
|
908
|
+
var _a;
|
|
909
|
+
if (this._isObjKey(key)) {
|
|
910
|
+
return this._objMap.get(key);
|
|
911
|
+
} else {
|
|
912
|
+
const strKey = this._getNoObjKey(key);
|
|
913
|
+
return (_a = this._store[strKey]) == null ? void 0 : _a.value;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
918
|
+
* is an object key or not.
|
|
919
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
920
|
+
* @returns The `has` method is returning a boolean value.
|
|
921
|
+
*/
|
|
922
|
+
has(key) {
|
|
923
|
+
if (this._isObjKey(key)) {
|
|
924
|
+
return this._objMap.has(key);
|
|
925
|
+
} else {
|
|
926
|
+
const strKey = this._getNoObjKey(key);
|
|
927
|
+
return strKey in this._store;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
932
|
+
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
933
|
+
* data structure.
|
|
934
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was
|
|
935
|
+
* successfully deleted from the map, and `false` if the key was not found in the map.
|
|
936
|
+
*/
|
|
937
|
+
delete(key) {
|
|
938
|
+
if (this._isObjKey(key)) {
|
|
939
|
+
if (this._objMap.has(key)) {
|
|
940
|
+
this._size--;
|
|
941
|
+
}
|
|
942
|
+
return this._objMap.delete(key);
|
|
943
|
+
} else {
|
|
944
|
+
const strKey = this._getNoObjKey(key);
|
|
945
|
+
if (strKey in this._store) {
|
|
946
|
+
delete this._store[strKey];
|
|
947
|
+
this._size--;
|
|
948
|
+
return true;
|
|
949
|
+
}
|
|
950
|
+
return false;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Time Complexity: O(n)
|
|
955
|
+
* Space Complexity: O(n)
|
|
956
|
+
*/
|
|
957
|
+
/**
|
|
958
|
+
* Time Complexity: O(n)
|
|
959
|
+
* Space Complexity: O(n)
|
|
960
|
+
*
|
|
961
|
+
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each
|
|
962
|
+
* key-value pair in the original HashMap.
|
|
963
|
+
* @param callbackfn - The callback function that will be called for each key-value pair in the
|
|
964
|
+
* HashMap. It takes four parameters:
|
|
965
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
966
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
967
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
968
|
+
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
969
|
+
* the provided callback function.
|
|
970
|
+
*/
|
|
971
|
+
map(callbackfn, thisArg) {
|
|
972
|
+
const resultMap = new _HashMap();
|
|
973
|
+
let index = 0;
|
|
974
|
+
for (const [key, value] of this) {
|
|
975
|
+
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
976
|
+
}
|
|
977
|
+
return resultMap;
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Time Complexity: O(n)
|
|
981
|
+
* Space Complexity: O(n)
|
|
982
|
+
*/
|
|
983
|
+
/**
|
|
984
|
+
* Time Complexity: O(n)
|
|
985
|
+
* Space Complexity: O(n)
|
|
986
|
+
*
|
|
987
|
+
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
|
|
988
|
+
* that satisfy a given predicate function.
|
|
989
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
990
|
+
* index, and map. It is used to determine whether an element should be included in the filtered map
|
|
991
|
+
* or not. The function should return a boolean value - true if the element should be included, and
|
|
992
|
+
* false otherwise.
|
|
993
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
994
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
995
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
996
|
+
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
997
|
+
* from the original `HashMap` that pass the provided `predicate` function.
|
|
998
|
+
*/
|
|
999
|
+
filter(predicate, thisArg) {
|
|
1000
|
+
const filteredMap = new _HashMap();
|
|
1001
|
+
let index = 0;
|
|
1002
|
+
for (const [key, value] of this) {
|
|
1003
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
1004
|
+
filteredMap.set(key, value);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
return filteredMap;
|
|
1008
|
+
}
|
|
1009
|
+
print() {
|
|
1010
|
+
console.log([...this.entries()]);
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
1014
|
+
* object map.
|
|
1015
|
+
*/
|
|
1016
|
+
*_getIterator() {
|
|
1017
|
+
for (const node of Object.values(this._store)) {
|
|
1018
|
+
yield [node.key, node.value];
|
|
1019
|
+
}
|
|
1020
|
+
for (const node of this._objMap) {
|
|
1021
|
+
yield node;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
_isObjKey(key) {
|
|
1025
|
+
const keyType = typeof key;
|
|
1026
|
+
return (keyType === "object" || keyType === "function") && key !== null;
|
|
1027
|
+
}
|
|
1028
|
+
_getNoObjKey(key) {
|
|
1029
|
+
const keyType = typeof key;
|
|
1030
|
+
let strKey;
|
|
1031
|
+
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
|
|
1032
|
+
strKey = this._hashFn(key);
|
|
1033
|
+
} else {
|
|
1034
|
+
if (keyType === "number") {
|
|
1035
|
+
strKey = key;
|
|
1036
|
+
} else {
|
|
1037
|
+
strKey = key;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
return strKey;
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
var LinkedHashMap = class _LinkedHashMap extends IterablePairBase {
|
|
520
1044
|
constructor(elements, options = {
|
|
521
1045
|
hashFn: (key) => String(key),
|
|
522
1046
|
objHashFn: (key) => key
|
|
523
1047
|
}) {
|
|
1048
|
+
super();
|
|
524
1049
|
__publicField(this, "_noObjMap", {});
|
|
525
1050
|
__publicField(this, "_objMap", /* @__PURE__ */ new WeakMap());
|
|
526
1051
|
__publicField(this, "_head");
|
|
@@ -604,39 +1129,53 @@ var dataStructureTyped = (() => {
|
|
|
604
1129
|
*/
|
|
605
1130
|
set(key, value) {
|
|
606
1131
|
let node;
|
|
1132
|
+
const isNewKey = !this.has(key);
|
|
607
1133
|
if (isWeakKey(key)) {
|
|
608
1134
|
const hash = this._objHashFn(key);
|
|
609
1135
|
node = this._objMap.get(hash);
|
|
610
|
-
if (node) {
|
|
611
|
-
node.value = value;
|
|
612
|
-
} else {
|
|
1136
|
+
if (!node && isNewKey) {
|
|
613
1137
|
node = { key: hash, value, prev: this._tail, next: this._sentinel };
|
|
614
1138
|
this._objMap.set(hash, node);
|
|
1139
|
+
} else if (node) {
|
|
1140
|
+
node.value = value;
|
|
615
1141
|
}
|
|
616
1142
|
} else {
|
|
617
1143
|
const hash = this._hashFn(key);
|
|
618
1144
|
node = this._noObjMap[hash];
|
|
619
|
-
if (node) {
|
|
1145
|
+
if (!node && isNewKey) {
|
|
1146
|
+
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel };
|
|
1147
|
+
} else if (node) {
|
|
620
1148
|
node.value = value;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
if (node && isNewKey) {
|
|
1152
|
+
if (this._size === 0) {
|
|
1153
|
+
this._head = node;
|
|
1154
|
+
this._sentinel.next = node;
|
|
621
1155
|
} else {
|
|
622
|
-
this.
|
|
623
|
-
|
|
624
|
-
value,
|
|
625
|
-
prev: this._tail,
|
|
626
|
-
next: this._sentinel
|
|
627
|
-
};
|
|
1156
|
+
this._tail.next = node;
|
|
1157
|
+
node.prev = this._tail;
|
|
628
1158
|
}
|
|
1159
|
+
this._tail = node;
|
|
1160
|
+
this._sentinel.prev = node;
|
|
1161
|
+
this._size++;
|
|
629
1162
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
1163
|
+
return this._size;
|
|
1164
|
+
}
|
|
1165
|
+
has(key) {
|
|
1166
|
+
if (isWeakKey(key)) {
|
|
1167
|
+
const hash = this._objHashFn(key);
|
|
1168
|
+
return this._objMap.has(hash);
|
|
633
1169
|
} else {
|
|
634
|
-
|
|
1170
|
+
const hash = this._hashFn(key);
|
|
1171
|
+
return hash in this._noObjMap;
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
setMany(entries) {
|
|
1175
|
+
for (const entry of entries) {
|
|
1176
|
+
const [key, value] = entry;
|
|
1177
|
+
this.set(key, value);
|
|
635
1178
|
}
|
|
636
|
-
this._tail = node;
|
|
637
|
-
this._sentinel.prev = node;
|
|
638
|
-
this._size++;
|
|
639
|
-
return this._size;
|
|
640
1179
|
}
|
|
641
1180
|
/**
|
|
642
1181
|
* Time Complexity: O(1)
|
|
@@ -751,36 +1290,38 @@ var dataStructureTyped = (() => {
|
|
|
751
1290
|
this._size = 0;
|
|
752
1291
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
753
1292
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
* each element.
|
|
760
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
761
|
-
* HashMap. It takes three arguments:
|
|
762
|
-
*/
|
|
763
|
-
forEach(callback) {
|
|
764
|
-
let index = 0;
|
|
765
|
-
let node = this._head;
|
|
766
|
-
while (node !== this._sentinel) {
|
|
767
|
-
callback([node.key, node.value], index++, this);
|
|
768
|
-
node = node.next;
|
|
1293
|
+
clone() {
|
|
1294
|
+
const cloned = new _LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
|
1295
|
+
for (const entry of this) {
|
|
1296
|
+
const [key, value] = entry;
|
|
1297
|
+
cloned.set(key, value);
|
|
769
1298
|
}
|
|
1299
|
+
return cloned;
|
|
770
1300
|
}
|
|
771
1301
|
/**
|
|
772
|
-
*
|
|
773
|
-
*
|
|
774
|
-
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
|
|
775
|
-
* `map`.
|
|
776
|
-
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that
|
|
777
|
-
* satisfy the given predicate function.
|
|
1302
|
+
* Time Complexity: O(n)
|
|
1303
|
+
* Space Complexity: O(n)
|
|
778
1304
|
*/
|
|
779
|
-
|
|
780
|
-
|
|
1305
|
+
/**
|
|
1306
|
+
* Time Complexity: O(n)
|
|
1307
|
+
* Space Complexity: O(n)
|
|
1308
|
+
*
|
|
1309
|
+
* The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
|
|
1310
|
+
* map that satisfy a given predicate function.
|
|
1311
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
1312
|
+
* `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
|
|
1313
|
+
* current element should be included in the filtered map or not.
|
|
1314
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1315
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
1316
|
+
* specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
|
|
1317
|
+
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
|
|
1318
|
+
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
1319
|
+
*/
|
|
1320
|
+
filter(predicate, thisArg) {
|
|
1321
|
+
const filteredMap = new _LinkedHashMap();
|
|
781
1322
|
let index = 0;
|
|
782
1323
|
for (const [key, value] of this) {
|
|
783
|
-
if (predicate(
|
|
1324
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
784
1325
|
filteredMap.set(key, value);
|
|
785
1326
|
}
|
|
786
1327
|
index++;
|
|
@@ -788,59 +1329,52 @@ var dataStructureTyped = (() => {
|
|
|
788
1329
|
return filteredMap;
|
|
789
1330
|
}
|
|
790
1331
|
/**
|
|
791
|
-
*
|
|
792
|
-
*
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
*
|
|
1332
|
+
* Time Complexity: O(n)
|
|
1333
|
+
* Space Complexity: O(n)
|
|
1334
|
+
*/
|
|
1335
|
+
/**
|
|
1336
|
+
* Time Complexity: O(n)
|
|
1337
|
+
* Space Complexity: O(n)
|
|
1338
|
+
*
|
|
1339
|
+
* The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
|
|
1340
|
+
* each key-value pair in the original map.
|
|
1341
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
1342
|
+
* in the map. It takes four arguments: the value of the current key-value pair, the key of the
|
|
1343
|
+
* current key-value pair, the index of the current key-value pair, and the map itself. The callback
|
|
1344
|
+
* function should
|
|
1345
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1346
|
+
* specify the value of `this` within the callback function. If provided, the callback function will
|
|
1347
|
+
* be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
|
|
1348
|
+
* map
|
|
1349
|
+
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
1350
|
+
* function.
|
|
796
1351
|
*/
|
|
797
|
-
map(callback) {
|
|
798
|
-
const mappedMap = new
|
|
1352
|
+
map(callback, thisArg) {
|
|
1353
|
+
const mappedMap = new _LinkedHashMap();
|
|
799
1354
|
let index = 0;
|
|
800
1355
|
for (const [key, value] of this) {
|
|
801
|
-
const newValue = callback(
|
|
1356
|
+
const newValue = callback.call(thisArg, value, key, index, this);
|
|
802
1357
|
mappedMap.set(key, newValue);
|
|
803
1358
|
index++;
|
|
804
1359
|
}
|
|
805
1360
|
return mappedMap;
|
|
806
1361
|
}
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
* each element, accumulating a single value.
|
|
810
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
811
|
-
* element, and map. It is called for each element in the HashMap and is used to accumulate a single
|
|
812
|
-
* result.
|
|
813
|
-
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
814
|
-
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
815
|
-
* the elements of the map.
|
|
816
|
-
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
817
|
-
* over all the elements in the HashMap and applying the callback function to each element.
|
|
818
|
-
*/
|
|
819
|
-
reduce(callback, initialValue) {
|
|
820
|
-
let accumulator = initialValue;
|
|
821
|
-
let index = 0;
|
|
822
|
-
for (const entry of this) {
|
|
823
|
-
accumulator = callback(accumulator, entry, index, this);
|
|
824
|
-
index++;
|
|
825
|
-
}
|
|
826
|
-
return accumulator;
|
|
1362
|
+
print() {
|
|
1363
|
+
console.log([...this]);
|
|
827
1364
|
}
|
|
828
1365
|
/**
|
|
829
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
1366
|
+
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
830
1367
|
* Space Complexity: O(1)
|
|
831
1368
|
*
|
|
832
1369
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
833
1370
|
*/
|
|
834
|
-
*
|
|
1371
|
+
*_getIterator() {
|
|
835
1372
|
let node = this._head;
|
|
836
1373
|
while (node !== this._sentinel) {
|
|
837
1374
|
yield [node.key, node.value];
|
|
838
1375
|
node = node.next;
|
|
839
1376
|
}
|
|
840
1377
|
}
|
|
841
|
-
print() {
|
|
842
|
-
console.log([...this]);
|
|
843
|
-
}
|
|
844
1378
|
/**
|
|
845
1379
|
* Time Complexity: O(1)
|
|
846
1380
|
* Space Complexity: O(1)
|
|
@@ -879,11 +1413,12 @@ var dataStructureTyped = (() => {
|
|
|
879
1413
|
this.next = void 0;
|
|
880
1414
|
}
|
|
881
1415
|
};
|
|
882
|
-
var SinglyLinkedList = class _SinglyLinkedList {
|
|
1416
|
+
var SinglyLinkedList = class _SinglyLinkedList extends IterableElementBase {
|
|
883
1417
|
/**
|
|
884
1418
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
885
1419
|
*/
|
|
886
1420
|
constructor(elements) {
|
|
1421
|
+
super();
|
|
887
1422
|
__publicField(this, "_head");
|
|
888
1423
|
__publicField(this, "_tail");
|
|
889
1424
|
__publicField(this, "_length");
|
|
@@ -1465,54 +2000,31 @@ var dataStructureTyped = (() => {
|
|
|
1465
2000
|
return count;
|
|
1466
2001
|
}
|
|
1467
2002
|
/**
|
|
1468
|
-
*
|
|
1469
|
-
*/
|
|
1470
|
-
*[Symbol.iterator]() {
|
|
1471
|
-
let current = this.head;
|
|
1472
|
-
while (current) {
|
|
1473
|
-
yield current.value;
|
|
1474
|
-
current = current.next;
|
|
1475
|
-
}
|
|
1476
|
-
}
|
|
1477
|
-
/**
|
|
1478
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1479
|
-
* Space Complexity: O(1)
|
|
1480
|
-
*/
|
|
1481
|
-
/**
|
|
1482
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1483
|
-
* Space Complexity: O(1)
|
|
1484
|
-
*
|
|
1485
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
1486
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
1487
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
1488
|
-
* current node in the linked list.
|
|
1489
|
-
*/
|
|
1490
|
-
forEach(callback) {
|
|
1491
|
-
let index = 0;
|
|
1492
|
-
for (const el of this) {
|
|
1493
|
-
callback(el, index, this);
|
|
1494
|
-
index++;
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1497
|
-
/**
|
|
1498
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2003
|
+
* Time Complexity: O(n)
|
|
1499
2004
|
* Space Complexity: O(n)
|
|
1500
2005
|
*/
|
|
1501
2006
|
/**
|
|
1502
|
-
* Time Complexity: O(n)
|
|
2007
|
+
* Time Complexity: O(n)
|
|
1503
2008
|
* Space Complexity: O(n)
|
|
1504
2009
|
*
|
|
1505
|
-
* The `filter` function
|
|
1506
|
-
*
|
|
1507
|
-
*
|
|
1508
|
-
*
|
|
1509
|
-
*
|
|
1510
|
-
|
|
1511
|
-
|
|
2010
|
+
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
|
|
2011
|
+
* list and applying a callback function to each element to determine if it should be included in the
|
|
2012
|
+
* filtered list.
|
|
2013
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
2014
|
+
* list. It takes three arguments: the current element, the index of the current element, and the
|
|
2015
|
+
* list itself. The callback function should return a boolean value indicating whether the current
|
|
2016
|
+
* element should be included in the filtered list or not
|
|
2017
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
2018
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
2019
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
2020
|
+
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
|
|
2021
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
2022
|
+
*/
|
|
2023
|
+
filter(callback, thisArg) {
|
|
1512
2024
|
const filteredList = new _SinglyLinkedList();
|
|
1513
2025
|
let index = 0;
|
|
1514
2026
|
for (const current of this) {
|
|
1515
|
-
if (callback(current, index, this)) {
|
|
2027
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
1516
2028
|
filteredList.push(current);
|
|
1517
2029
|
}
|
|
1518
2030
|
index++;
|
|
@@ -1524,21 +2036,24 @@ var dataStructureTyped = (() => {
|
|
|
1524
2036
|
* Space Complexity: O(n)
|
|
1525
2037
|
*/
|
|
1526
2038
|
/**
|
|
1527
|
-
* Time Complexity: O(n)
|
|
2039
|
+
* Time Complexity: O(n)
|
|
1528
2040
|
* Space Complexity: O(n)
|
|
1529
2041
|
*
|
|
1530
|
-
* The `map` function
|
|
1531
|
-
*
|
|
1532
|
-
* @param callback - The callback parameter is a function that
|
|
1533
|
-
* the
|
|
1534
|
-
*
|
|
1535
|
-
*
|
|
1536
|
-
|
|
1537
|
-
|
|
2042
|
+
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element
|
|
2043
|
+
* of the original list.
|
|
2044
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
2045
|
+
* the linked list. It takes three arguments:
|
|
2046
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
2047
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
2048
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
2049
|
+
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
|
|
2050
|
+
* of applying the provided `callback` function to each element in the original list.
|
|
2051
|
+
*/
|
|
2052
|
+
map(callback, thisArg) {
|
|
1538
2053
|
const mappedList = new _SinglyLinkedList();
|
|
1539
2054
|
let index = 0;
|
|
1540
2055
|
for (const current of this) {
|
|
1541
|
-
mappedList.push(callback(current, index, this));
|
|
2056
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
1542
2057
|
index++;
|
|
1543
2058
|
}
|
|
1544
2059
|
return mappedList;
|
|
@@ -1547,30 +2062,15 @@ var dataStructureTyped = (() => {
|
|
|
1547
2062
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1548
2063
|
* Space Complexity: O(n)
|
|
1549
2064
|
*/
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
1559
|
-
* point for the reduction operation.
|
|
1560
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
1561
|
-
* elements in the linked list.
|
|
1562
|
-
*/
|
|
1563
|
-
reduce(callback, initialValue) {
|
|
1564
|
-
let accumulator = initialValue;
|
|
1565
|
-
let index = 0;
|
|
1566
|
-
for (const current of this) {
|
|
1567
|
-
accumulator = callback(accumulator, current, index, this);
|
|
1568
|
-
index++;
|
|
2065
|
+
print() {
|
|
2066
|
+
console.log([...this]);
|
|
2067
|
+
}
|
|
2068
|
+
*_getIterator() {
|
|
2069
|
+
let current = this.head;
|
|
2070
|
+
while (current) {
|
|
2071
|
+
yield current.value;
|
|
2072
|
+
current = current.next;
|
|
1569
2073
|
}
|
|
1570
|
-
return accumulator;
|
|
1571
|
-
}
|
|
1572
|
-
print() {
|
|
1573
|
-
console.log([...this]);
|
|
1574
2074
|
}
|
|
1575
2075
|
};
|
|
1576
2076
|
|
|
@@ -1590,11 +2090,12 @@ var dataStructureTyped = (() => {
|
|
|
1590
2090
|
this.prev = void 0;
|
|
1591
2091
|
}
|
|
1592
2092
|
};
|
|
1593
|
-
var DoublyLinkedList = class _DoublyLinkedList {
|
|
2093
|
+
var DoublyLinkedList = class _DoublyLinkedList extends IterableElementBase {
|
|
1594
2094
|
/**
|
|
1595
2095
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
1596
2096
|
*/
|
|
1597
2097
|
constructor(elements) {
|
|
2098
|
+
super();
|
|
1598
2099
|
__publicField(this, "_head");
|
|
1599
2100
|
__publicField(this, "_tail");
|
|
1600
2101
|
__publicField(this, "_length");
|
|
@@ -2230,54 +2731,31 @@ var dataStructureTyped = (() => {
|
|
|
2230
2731
|
return array;
|
|
2231
2732
|
}
|
|
2232
2733
|
/**
|
|
2233
|
-
*
|
|
2234
|
-
*/
|
|
2235
|
-
*[Symbol.iterator]() {
|
|
2236
|
-
let current = this.head;
|
|
2237
|
-
while (current) {
|
|
2238
|
-
yield current.value;
|
|
2239
|
-
current = current.next;
|
|
2240
|
-
}
|
|
2241
|
-
}
|
|
2242
|
-
/**
|
|
2243
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2244
|
-
* Space Complexity: O(1)
|
|
2245
|
-
*/
|
|
2246
|
-
/**
|
|
2247
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2248
|
-
* Space Complexity: O(1)
|
|
2249
|
-
*
|
|
2250
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
2251
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
2252
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
2253
|
-
* current node in the linked list.
|
|
2254
|
-
*/
|
|
2255
|
-
forEach(callback) {
|
|
2256
|
-
let index = 0;
|
|
2257
|
-
for (const el of this) {
|
|
2258
|
-
callback(el, index, this);
|
|
2259
|
-
index++;
|
|
2260
|
-
}
|
|
2261
|
-
}
|
|
2262
|
-
/**
|
|
2263
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2734
|
+
* Time Complexity: O(n)
|
|
2264
2735
|
* Space Complexity: O(n)
|
|
2265
2736
|
*/
|
|
2266
2737
|
/**
|
|
2267
|
-
* Time Complexity: O(n)
|
|
2738
|
+
* Time Complexity: O(n)
|
|
2268
2739
|
* Space Complexity: O(n)
|
|
2269
2740
|
*
|
|
2270
|
-
* The `filter` function
|
|
2271
|
-
*
|
|
2272
|
-
*
|
|
2273
|
-
*
|
|
2274
|
-
*
|
|
2275
|
-
|
|
2276
|
-
|
|
2741
|
+
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
|
|
2742
|
+
* list and applying a callback function to each element, returning only the elements for which the
|
|
2743
|
+
* callback function returns true.
|
|
2744
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
2745
|
+
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current
|
|
2746
|
+
* element, and the DoublyLinkedList itself. The callback function should return a boolean value
|
|
2747
|
+
* indicating whether the current element should be included
|
|
2748
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
2749
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
2750
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
2751
|
+
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
|
|
2752
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
2753
|
+
*/
|
|
2754
|
+
filter(callback, thisArg) {
|
|
2277
2755
|
const filteredList = new _DoublyLinkedList();
|
|
2278
2756
|
let index = 0;
|
|
2279
2757
|
for (const current of this) {
|
|
2280
|
-
if (callback(current, index, this)) {
|
|
2758
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
2281
2759
|
filteredList.push(current);
|
|
2282
2760
|
}
|
|
2283
2761
|
index++;
|
|
@@ -2289,21 +2767,27 @@ var dataStructureTyped = (() => {
|
|
|
2289
2767
|
* Space Complexity: O(n)
|
|
2290
2768
|
*/
|
|
2291
2769
|
/**
|
|
2292
|
-
* Time Complexity: O(n)
|
|
2770
|
+
* Time Complexity: O(n)
|
|
2293
2771
|
* Space Complexity: O(n)
|
|
2294
2772
|
*
|
|
2295
|
-
* The `map` function
|
|
2296
|
-
*
|
|
2297
|
-
* @param callback - The callback parameter is a function that
|
|
2298
|
-
*
|
|
2299
|
-
* DoublyLinkedList
|
|
2300
|
-
*
|
|
2773
|
+
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element
|
|
2774
|
+
* in the original list.
|
|
2775
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
2776
|
+
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
|
|
2777
|
+
* and the DoublyLinkedList itself. The callback function should return a value that will be added to
|
|
2778
|
+
* the new DoublyLinkedList that
|
|
2779
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
2780
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
2781
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
2782
|
+
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
|
|
2783
|
+
* of applying the provided `callback` function to each element in the original `DoublyLinkedList`
|
|
2784
|
+
* object.
|
|
2301
2785
|
*/
|
|
2302
|
-
map(callback) {
|
|
2786
|
+
map(callback, thisArg) {
|
|
2303
2787
|
const mappedList = new _DoublyLinkedList();
|
|
2304
2788
|
let index = 0;
|
|
2305
2789
|
for (const current of this) {
|
|
2306
|
-
mappedList.push(callback(current, index, this));
|
|
2790
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
2307
2791
|
index++;
|
|
2308
2792
|
}
|
|
2309
2793
|
return mappedList;
|
|
@@ -2312,30 +2796,18 @@ var dataStructureTyped = (() => {
|
|
|
2312
2796
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2313
2797
|
* Space Complexity: O(n)
|
|
2314
2798
|
*/
|
|
2799
|
+
print() {
|
|
2800
|
+
console.log([...this]);
|
|
2801
|
+
}
|
|
2315
2802
|
/**
|
|
2316
|
-
*
|
|
2317
|
-
* Space Complexity: O(n)
|
|
2318
|
-
*
|
|
2319
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
2320
|
-
* single value.
|
|
2321
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
2322
|
-
* used to perform a specific operation on each element of the linked list.
|
|
2323
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
2324
|
-
* point for the reduction operation.
|
|
2325
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
2326
|
-
* elements in the linked list.
|
|
2803
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
2327
2804
|
*/
|
|
2328
|
-
|
|
2329
|
-
let
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
index++;
|
|
2805
|
+
*_getIterator() {
|
|
2806
|
+
let current = this.head;
|
|
2807
|
+
while (current) {
|
|
2808
|
+
yield current.value;
|
|
2809
|
+
current = current.next;
|
|
2334
2810
|
}
|
|
2335
|
-
return accumulator;
|
|
2336
|
-
}
|
|
2337
|
-
print() {
|
|
2338
|
-
console.log([...this]);
|
|
2339
2811
|
}
|
|
2340
2812
|
};
|
|
2341
2813
|
|
|
@@ -2588,7 +3060,7 @@ var dataStructureTyped = (() => {
|
|
|
2588
3060
|
};
|
|
2589
3061
|
|
|
2590
3062
|
// src/data-structures/stack/stack.ts
|
|
2591
|
-
var Stack = class _Stack {
|
|
3063
|
+
var Stack = class _Stack extends IterableElementBase {
|
|
2592
3064
|
/**
|
|
2593
3065
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
2594
3066
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -2596,6 +3068,7 @@ var dataStructureTyped = (() => {
|
|
|
2596
3068
|
* is provided and is an array, it is assigned to the `_elements
|
|
2597
3069
|
*/
|
|
2598
3070
|
constructor(elements) {
|
|
3071
|
+
super();
|
|
2599
3072
|
__publicField(this, "_elements");
|
|
2600
3073
|
this._elements = [];
|
|
2601
3074
|
if (elements) {
|
|
@@ -2721,92 +3194,78 @@ var dataStructureTyped = (() => {
|
|
|
2721
3194
|
return new _Stack(this.elements.slice());
|
|
2722
3195
|
}
|
|
2723
3196
|
/**
|
|
2724
|
-
*
|
|
2725
|
-
*
|
|
3197
|
+
* Time Complexity: O(n)
|
|
3198
|
+
* Space Complexity: O(n)
|
|
2726
3199
|
*/
|
|
2727
|
-
*[Symbol.iterator]() {
|
|
2728
|
-
for (let i = 0; i < this.elements.length; i++) {
|
|
2729
|
-
yield this.elements[i];
|
|
2730
|
-
}
|
|
2731
|
-
}
|
|
2732
3200
|
/**
|
|
2733
|
-
*
|
|
2734
|
-
*
|
|
3201
|
+
* Time Complexity: O(n)
|
|
3202
|
+
* Space Complexity: O(n)
|
|
3203
|
+
*
|
|
3204
|
+
* The `filter` function creates a new stack containing elements from the original stack that satisfy
|
|
3205
|
+
* a given predicate function.
|
|
3206
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
3207
|
+
* the current element being iterated over, the index of the current element, and the stack itself.
|
|
3208
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
3209
|
+
* stack or not.
|
|
3210
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
3211
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
3212
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
3213
|
+
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
3214
|
+
* satisfy the given predicate function.
|
|
2735
3215
|
*/
|
|
2736
|
-
|
|
2737
|
-
let index = 0;
|
|
2738
|
-
for (const el of this) {
|
|
2739
|
-
callback(el, index, this);
|
|
2740
|
-
index++;
|
|
2741
|
-
}
|
|
2742
|
-
}
|
|
2743
|
-
filter(predicate) {
|
|
3216
|
+
filter(predicate, thisArg) {
|
|
2744
3217
|
const newStack = new _Stack();
|
|
2745
3218
|
let index = 0;
|
|
2746
3219
|
for (const el of this) {
|
|
2747
|
-
if (predicate(el, index, this)) {
|
|
3220
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
2748
3221
|
newStack.push(el);
|
|
2749
3222
|
}
|
|
2750
3223
|
index++;
|
|
2751
3224
|
}
|
|
2752
3225
|
return newStack;
|
|
2753
3226
|
}
|
|
2754
|
-
|
|
3227
|
+
/**
|
|
3228
|
+
* Time Complexity: O(n)
|
|
3229
|
+
* Space Complexity: O(n)
|
|
3230
|
+
*/
|
|
3231
|
+
/**
|
|
3232
|
+
* Time Complexity: O(n)
|
|
3233
|
+
* Space Complexity: O(n)
|
|
3234
|
+
*
|
|
3235
|
+
* The `map` function takes a callback function and applies it to each element in the stack,
|
|
3236
|
+
* returning a new stack with the results.
|
|
3237
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
3238
|
+
* the stack. It takes three arguments:
|
|
3239
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
3240
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
3241
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
3242
|
+
* @returns The `map` method is returning a new `Stack` object.
|
|
3243
|
+
*/
|
|
3244
|
+
map(callback, thisArg) {
|
|
2755
3245
|
const newStack = new _Stack();
|
|
2756
3246
|
let index = 0;
|
|
2757
3247
|
for (const el of this) {
|
|
2758
|
-
newStack.push(callback(el, index, this));
|
|
3248
|
+
newStack.push(callback.call(thisArg, el, index, this));
|
|
2759
3249
|
index++;
|
|
2760
3250
|
}
|
|
2761
3251
|
return newStack;
|
|
2762
3252
|
}
|
|
2763
|
-
reduce(callback, initialValue) {
|
|
2764
|
-
let accumulator = initialValue;
|
|
2765
|
-
let index = 0;
|
|
2766
|
-
for (const el of this) {
|
|
2767
|
-
accumulator = callback(accumulator, el, index, this);
|
|
2768
|
-
index++;
|
|
2769
|
-
}
|
|
2770
|
-
return accumulator;
|
|
2771
|
-
}
|
|
2772
3253
|
print() {
|
|
2773
3254
|
console.log([...this]);
|
|
2774
3255
|
}
|
|
2775
|
-
};
|
|
2776
|
-
|
|
2777
|
-
// src/data-structures/queue/queue.ts
|
|
2778
|
-
var LinkedListQueue = class extends SinglyLinkedList {
|
|
2779
|
-
/**
|
|
2780
|
-
* The enqueue function adds a value to the end of an array.
|
|
2781
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
2782
|
-
*/
|
|
2783
|
-
enqueue(value) {
|
|
2784
|
-
this.push(value);
|
|
2785
|
-
}
|
|
2786
|
-
/**
|
|
2787
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
2788
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
2789
|
-
*/
|
|
2790
|
-
dequeue() {
|
|
2791
|
-
return this.shift();
|
|
2792
|
-
}
|
|
2793
|
-
/**
|
|
2794
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
2795
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
2796
|
-
*/
|
|
2797
|
-
getFirst() {
|
|
2798
|
-
var _a;
|
|
2799
|
-
return (_a = this.head) == null ? void 0 : _a.value;
|
|
2800
|
-
}
|
|
2801
3256
|
/**
|
|
2802
|
-
*
|
|
2803
|
-
* @returns
|
|
3257
|
+
* Custom iterator for the Stack class.
|
|
3258
|
+
* @returns An iterator object.
|
|
2804
3259
|
*/
|
|
2805
|
-
|
|
2806
|
-
|
|
3260
|
+
*_getIterator() {
|
|
3261
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
3262
|
+
yield this.elements[i];
|
|
3263
|
+
}
|
|
2807
3264
|
}
|
|
2808
3265
|
};
|
|
2809
|
-
|
|
3266
|
+
|
|
3267
|
+
// src/data-structures/queue/queue.ts
|
|
3268
|
+
var Queue = class _Queue extends IterableElementBase {
|
|
2810
3269
|
/**
|
|
2811
3270
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
2812
3271
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
@@ -2814,6 +3273,7 @@ var dataStructureTyped = (() => {
|
|
|
2814
3273
|
* initialized as an empty array.
|
|
2815
3274
|
*/
|
|
2816
3275
|
constructor(elements) {
|
|
3276
|
+
super();
|
|
2817
3277
|
__publicField(this, "_nodes");
|
|
2818
3278
|
__publicField(this, "_offset");
|
|
2819
3279
|
this._nodes = elements || [];
|
|
@@ -3035,31 +3495,6 @@ var dataStructureTyped = (() => {
|
|
|
3035
3495
|
print() {
|
|
3036
3496
|
console.log([...this]);
|
|
3037
3497
|
}
|
|
3038
|
-
*[Symbol.iterator]() {
|
|
3039
|
-
for (const item of this.nodes) {
|
|
3040
|
-
yield item;
|
|
3041
|
-
}
|
|
3042
|
-
}
|
|
3043
|
-
/**
|
|
3044
|
-
* Time Complexity: O(n)
|
|
3045
|
-
* Space Complexity: O(1)
|
|
3046
|
-
*/
|
|
3047
|
-
/**
|
|
3048
|
-
* Time Complexity: O(n)
|
|
3049
|
-
* Space Complexity: O(1)
|
|
3050
|
-
*
|
|
3051
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
3052
|
-
* each element.
|
|
3053
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
3054
|
-
* deque. It takes three parameters:
|
|
3055
|
-
*/
|
|
3056
|
-
forEach(callback) {
|
|
3057
|
-
let index = 0;
|
|
3058
|
-
for (const el of this) {
|
|
3059
|
-
callback(el, index, this);
|
|
3060
|
-
index++;
|
|
3061
|
-
}
|
|
3062
|
-
}
|
|
3063
3498
|
/**
|
|
3064
3499
|
* Time Complexity: O(n)
|
|
3065
3500
|
* Space Complexity: O(n)
|
|
@@ -3068,18 +3503,23 @@ var dataStructureTyped = (() => {
|
|
|
3068
3503
|
* Time Complexity: O(n)
|
|
3069
3504
|
* Space Complexity: O(n)
|
|
3070
3505
|
*
|
|
3071
|
-
* The `filter` function creates a new
|
|
3072
|
-
* predicate function.
|
|
3073
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
3074
|
-
*
|
|
3075
|
-
*
|
|
3076
|
-
*
|
|
3506
|
+
* The `filter` function creates a new `Queue` object containing elements from the original `Queue`
|
|
3507
|
+
* that satisfy a given predicate function.
|
|
3508
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
3509
|
+
* the current element being iterated over, the index of the current element, and the queue itself.
|
|
3510
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
3511
|
+
* queue or not.
|
|
3512
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
3513
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
3514
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
3515
|
+
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
3516
|
+
* satisfy the given predicate function.
|
|
3077
3517
|
*/
|
|
3078
|
-
filter(predicate) {
|
|
3518
|
+
filter(predicate, thisArg) {
|
|
3079
3519
|
const newDeque = new _Queue([]);
|
|
3080
3520
|
let index = 0;
|
|
3081
3521
|
for (const el of this) {
|
|
3082
|
-
if (predicate(el, index, this)) {
|
|
3522
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
3083
3523
|
newDeque.push(el);
|
|
3084
3524
|
}
|
|
3085
3525
|
index++;
|
|
@@ -3094,33 +3534,69 @@ var dataStructureTyped = (() => {
|
|
|
3094
3534
|
* Time Complexity: O(n)
|
|
3095
3535
|
* Space Complexity: O(n)
|
|
3096
3536
|
*
|
|
3097
|
-
* The `map` function takes a callback function and applies it to each element in the
|
|
3098
|
-
* returning a new
|
|
3099
|
-
* @param callback - The
|
|
3100
|
-
*
|
|
3101
|
-
|
|
3102
|
-
|
|
3537
|
+
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
3538
|
+
* returning a new queue with the results.
|
|
3539
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
3540
|
+
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
3541
|
+
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
3542
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
3543
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
3544
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
3545
|
+
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
3546
|
+
*/
|
|
3547
|
+
map(callback, thisArg) {
|
|
3103
3548
|
const newDeque = new _Queue([]);
|
|
3104
3549
|
let index = 0;
|
|
3105
3550
|
for (const el of this) {
|
|
3106
|
-
newDeque.push(callback(el, index, this));
|
|
3551
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
3107
3552
|
index++;
|
|
3108
3553
|
}
|
|
3109
3554
|
return newDeque;
|
|
3110
3555
|
}
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3556
|
+
/**
|
|
3557
|
+
* Time Complexity: O(n)
|
|
3558
|
+
* Space Complexity: O(n)
|
|
3559
|
+
*/
|
|
3560
|
+
*_getIterator() {
|
|
3561
|
+
for (const item of this.nodes) {
|
|
3562
|
+
yield item;
|
|
3117
3563
|
}
|
|
3118
|
-
|
|
3564
|
+
}
|
|
3565
|
+
};
|
|
3566
|
+
var LinkedListQueue = class extends SinglyLinkedList {
|
|
3567
|
+
/**
|
|
3568
|
+
* The enqueue function adds a value to the end of an array.
|
|
3569
|
+
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
3570
|
+
*/
|
|
3571
|
+
enqueue(value) {
|
|
3572
|
+
this.push(value);
|
|
3573
|
+
}
|
|
3574
|
+
/**
|
|
3575
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
3576
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
3577
|
+
*/
|
|
3578
|
+
dequeue() {
|
|
3579
|
+
return this.shift();
|
|
3580
|
+
}
|
|
3581
|
+
/**
|
|
3582
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
3583
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
3584
|
+
*/
|
|
3585
|
+
getFirst() {
|
|
3586
|
+
var _a;
|
|
3587
|
+
return (_a = this.head) == null ? void 0 : _a.value;
|
|
3588
|
+
}
|
|
3589
|
+
/**
|
|
3590
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
3591
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
3592
|
+
*/
|
|
3593
|
+
peek() {
|
|
3594
|
+
return this.getFirst();
|
|
3119
3595
|
}
|
|
3120
3596
|
};
|
|
3121
3597
|
|
|
3122
3598
|
// src/data-structures/queue/deque.ts
|
|
3123
|
-
var Deque = class _Deque {
|
|
3599
|
+
var Deque = class _Deque extends IterableElementBase {
|
|
3124
3600
|
/**
|
|
3125
3601
|
* The constructor initializes a data structure with a specified bucket size and populates it with
|
|
3126
3602
|
* elements from an iterable.
|
|
@@ -3131,6 +3607,7 @@ var dataStructureTyped = (() => {
|
|
|
3131
3607
|
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
3132
3608
|
*/
|
|
3133
3609
|
constructor(elements = [], bucketSize = 1 << 12) {
|
|
3610
|
+
super();
|
|
3134
3611
|
__publicField(this, "_bucketFirst", 0);
|
|
3135
3612
|
__publicField(this, "_firstInBucket", 0);
|
|
3136
3613
|
__publicField(this, "_bucketLast", 0);
|
|
@@ -3758,42 +4235,6 @@ var dataStructureTyped = (() => {
|
|
|
3758
4235
|
}
|
|
3759
4236
|
return arr;
|
|
3760
4237
|
}
|
|
3761
|
-
/**
|
|
3762
|
-
* Time Complexity: O(n)
|
|
3763
|
-
* Space Complexity: O(1)
|
|
3764
|
-
*/
|
|
3765
|
-
/**
|
|
3766
|
-
* Time Complexity: O(n)
|
|
3767
|
-
* Space Complexity: O(1)
|
|
3768
|
-
*
|
|
3769
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
3770
|
-
* object to be iterated over using a for...of loop.
|
|
3771
|
-
*/
|
|
3772
|
-
*[Symbol.iterator]() {
|
|
3773
|
-
for (let i = 0; i < this.size; ++i) {
|
|
3774
|
-
yield this.getAt(i);
|
|
3775
|
-
}
|
|
3776
|
-
}
|
|
3777
|
-
/**
|
|
3778
|
-
* Time Complexity: O(n)
|
|
3779
|
-
* Space Complexity: O(1)
|
|
3780
|
-
*/
|
|
3781
|
-
/**
|
|
3782
|
-
* Time Complexity: O(n)
|
|
3783
|
-
* Space Complexity: O(1)
|
|
3784
|
-
*
|
|
3785
|
-
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
3786
|
-
* each element.
|
|
3787
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
3788
|
-
* deque. It takes three parameters:
|
|
3789
|
-
*/
|
|
3790
|
-
forEach(callback) {
|
|
3791
|
-
let index = 0;
|
|
3792
|
-
for (const el of this) {
|
|
3793
|
-
callback(el, index, this);
|
|
3794
|
-
index++;
|
|
3795
|
-
}
|
|
3796
|
-
}
|
|
3797
4238
|
/**
|
|
3798
4239
|
* Time Complexity: O(n)
|
|
3799
4240
|
* Space Complexity: O(n)
|
|
@@ -3802,18 +4243,23 @@ var dataStructureTyped = (() => {
|
|
|
3802
4243
|
* Time Complexity: O(n)
|
|
3803
4244
|
* Space Complexity: O(n)
|
|
3804
4245
|
*
|
|
3805
|
-
* The `filter` function creates a new deque containing
|
|
3806
|
-
* predicate function.
|
|
3807
|
-
* @param predicate - The `predicate` parameter is a function that takes three arguments:
|
|
3808
|
-
*
|
|
3809
|
-
*
|
|
3810
|
-
*
|
|
4246
|
+
* The `filter` function creates a new deque containing elements from the original deque that satisfy
|
|
4247
|
+
* a given predicate function.
|
|
4248
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
4249
|
+
* the current element being iterated over, the index of the current element, and the deque itself.
|
|
4250
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
4251
|
+
* deque or not.
|
|
4252
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
4253
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
4254
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
4255
|
+
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
4256
|
+
* satisfy the given predicate function.
|
|
3811
4257
|
*/
|
|
3812
|
-
filter(predicate) {
|
|
4258
|
+
filter(predicate, thisArg) {
|
|
3813
4259
|
const newDeque = new _Deque([], this._bucketSize);
|
|
3814
4260
|
let index = 0;
|
|
3815
4261
|
for (const el of this) {
|
|
3816
|
-
if (predicate(el, index, this)) {
|
|
4262
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
3817
4263
|
newDeque.push(el);
|
|
3818
4264
|
}
|
|
3819
4265
|
index++;
|
|
@@ -3828,48 +4274,42 @@ var dataStructureTyped = (() => {
|
|
|
3828
4274
|
* Time Complexity: O(n)
|
|
3829
4275
|
* Space Complexity: O(n)
|
|
3830
4276
|
*
|
|
3831
|
-
* The `map` function
|
|
3832
|
-
*
|
|
3833
|
-
* @param callback - The `callback` parameter is a function that
|
|
3834
|
-
*
|
|
4277
|
+
* The `map` function creates a new Deque by applying a callback function to each element of the
|
|
4278
|
+
* original Deque.
|
|
4279
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
4280
|
+
* the deque. It takes three arguments:
|
|
4281
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
4282
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
4283
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
4284
|
+
* @returns a new Deque object with the mapped values.
|
|
3835
4285
|
*/
|
|
3836
|
-
map(callback) {
|
|
4286
|
+
map(callback, thisArg) {
|
|
3837
4287
|
const newDeque = new _Deque([], this._bucketSize);
|
|
3838
4288
|
let index = 0;
|
|
3839
4289
|
for (const el of this) {
|
|
3840
|
-
newDeque.push(callback(el, index, this));
|
|
4290
|
+
newDeque.push(callback.call(thisArg, el, index, this));
|
|
3841
4291
|
index++;
|
|
3842
4292
|
}
|
|
3843
4293
|
return newDeque;
|
|
3844
4294
|
}
|
|
3845
4295
|
/**
|
|
3846
4296
|
* Time Complexity: O(n)
|
|
3847
|
-
* Space Complexity: O(
|
|
4297
|
+
* Space Complexity: O(n)
|
|
3848
4298
|
*/
|
|
4299
|
+
print() {
|
|
4300
|
+
console.log([...this]);
|
|
4301
|
+
}
|
|
3849
4302
|
/**
|
|
3850
4303
|
* Time Complexity: O(n)
|
|
3851
4304
|
* Space Complexity: O(1)
|
|
3852
4305
|
*
|
|
3853
|
-
* The
|
|
3854
|
-
*
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
3860
|
-
* applying the callback function to each element.
|
|
3861
|
-
*/
|
|
3862
|
-
reduce(callback, initialValue) {
|
|
3863
|
-
let accumulator = initialValue;
|
|
3864
|
-
let index = 0;
|
|
3865
|
-
for (const el of this) {
|
|
3866
|
-
accumulator = callback(accumulator, el, index, this);
|
|
3867
|
-
index++;
|
|
4306
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
4307
|
+
* object to be iterated over using a for...of loop.
|
|
4308
|
+
*/
|
|
4309
|
+
*_getIterator() {
|
|
4310
|
+
for (let i = 0; i < this.size; ++i) {
|
|
4311
|
+
yield this.getAt(i);
|
|
3868
4312
|
}
|
|
3869
|
-
return accumulator;
|
|
3870
|
-
}
|
|
3871
|
-
print() {
|
|
3872
|
-
console.log([...this]);
|
|
3873
4313
|
}
|
|
3874
4314
|
/**
|
|
3875
4315
|
* Time Complexity: O(n)
|
|
@@ -4100,8 +4540,9 @@ var dataStructureTyped = (() => {
|
|
|
4100
4540
|
};
|
|
4101
4541
|
|
|
4102
4542
|
// src/data-structures/heap/heap.ts
|
|
4103
|
-
var Heap = class _Heap {
|
|
4543
|
+
var Heap = class _Heap extends IterableElementBase {
|
|
4104
4544
|
constructor(elements, options) {
|
|
4545
|
+
super();
|
|
4105
4546
|
__publicField(this, "options");
|
|
4106
4547
|
__publicField(this, "_elements", []);
|
|
4107
4548
|
const defaultComparator = (a, b) => {
|
|
@@ -4396,47 +4837,70 @@ var dataStructureTyped = (() => {
|
|
|
4396
4837
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
4397
4838
|
this._sinkDown(i, this.elements.length >> 1);
|
|
4398
4839
|
}
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4840
|
+
/**
|
|
4841
|
+
* Time Complexity: O(n)
|
|
4842
|
+
* Space Complexity: O(n)
|
|
4843
|
+
*/
|
|
4844
|
+
/**
|
|
4845
|
+
* Time Complexity: O(n)
|
|
4846
|
+
* Space Complexity: O(n)
|
|
4847
|
+
*
|
|
4848
|
+
* The `filter` function creates a new Heap object containing elements that pass a given callback
|
|
4849
|
+
* function.
|
|
4850
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
4851
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
4852
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
4853
|
+
* element should be included in the filtered list
|
|
4854
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
4855
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
4856
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
4857
|
+
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
|
|
4858
|
+
* the filter condition specified by the `callback` function.
|
|
4859
|
+
*/
|
|
4860
|
+
filter(callback, thisArg) {
|
|
4861
|
+
const filteredList = new _Heap();
|
|
4413
4862
|
let index = 0;
|
|
4414
|
-
for (const
|
|
4415
|
-
if (
|
|
4416
|
-
|
|
4863
|
+
for (const current of this) {
|
|
4864
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
4865
|
+
filteredList.push(current);
|
|
4417
4866
|
}
|
|
4418
4867
|
index++;
|
|
4419
4868
|
}
|
|
4420
|
-
return
|
|
4869
|
+
return filteredList;
|
|
4421
4870
|
}
|
|
4422
|
-
|
|
4871
|
+
/**
|
|
4872
|
+
* Time Complexity: O(n)
|
|
4873
|
+
* Space Complexity: O(n)
|
|
4874
|
+
*/
|
|
4875
|
+
/**
|
|
4876
|
+
* Time Complexity: O(n)
|
|
4877
|
+
* Space Complexity: O(n)
|
|
4878
|
+
*
|
|
4879
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
4880
|
+
* original heap.
|
|
4881
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
4882
|
+
* original heap. It takes three arguments: the current element, the index of the current element,
|
|
4883
|
+
* and the original heap itself. The callback function should return a value of type T, which will be
|
|
4884
|
+
* added to the mapped heap.
|
|
4885
|
+
* @param comparator - The `comparator` parameter is a function that is used to compare elements in
|
|
4886
|
+
* the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
|
|
4887
|
+
* `b`, a positive number if `a` is greater than `b`, or
|
|
4888
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
4889
|
+
* specify the value of `this` within the callback function. It is used when you want to bind a
|
|
4890
|
+
* specific object as the context for the callback function. If `thisArg` is not provided,
|
|
4891
|
+
* `undefined` is used as
|
|
4892
|
+
* @returns a new instance of the Heap class, which is created using the mapped elements from the
|
|
4893
|
+
* original Heap.
|
|
4894
|
+
*/
|
|
4895
|
+
map(callback, comparator, thisArg) {
|
|
4423
4896
|
const mappedHeap = new _Heap([], { comparator });
|
|
4424
4897
|
let index = 0;
|
|
4425
4898
|
for (const el of this) {
|
|
4426
|
-
mappedHeap.add(callback(el, index, this));
|
|
4899
|
+
mappedHeap.add(callback.call(thisArg, el, index, this));
|
|
4427
4900
|
index++;
|
|
4428
4901
|
}
|
|
4429
4902
|
return mappedHeap;
|
|
4430
4903
|
}
|
|
4431
|
-
reduce(callback, initialValue) {
|
|
4432
|
-
let accumulator = initialValue;
|
|
4433
|
-
let index = 0;
|
|
4434
|
-
for (const el of this) {
|
|
4435
|
-
accumulator = callback(accumulator, el, index, this);
|
|
4436
|
-
index++;
|
|
4437
|
-
}
|
|
4438
|
-
return accumulator;
|
|
4439
|
-
}
|
|
4440
4904
|
/**
|
|
4441
4905
|
* Time Complexity: O(log n)
|
|
4442
4906
|
* Space Complexity: O(1)
|
|
@@ -4444,6 +4908,11 @@ var dataStructureTyped = (() => {
|
|
|
4444
4908
|
print() {
|
|
4445
4909
|
console.log([...this]);
|
|
4446
4910
|
}
|
|
4911
|
+
*_getIterator() {
|
|
4912
|
+
for (const element of this.elements) {
|
|
4913
|
+
yield element;
|
|
4914
|
+
}
|
|
4915
|
+
}
|
|
4447
4916
|
/**
|
|
4448
4917
|
* Time Complexity: O(n)
|
|
4449
4918
|
* Space Complexity: O(1)
|
|
@@ -4950,8 +5419,9 @@ var dataStructureTyped = (() => {
|
|
|
4950
5419
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
4951
5420
|
*/
|
|
4952
5421
|
};
|
|
4953
|
-
var AbstractGraph = class {
|
|
5422
|
+
var AbstractGraph = class extends IterablePairBase {
|
|
4954
5423
|
constructor() {
|
|
5424
|
+
super();
|
|
4955
5425
|
__publicField(this, "_vertices", /* @__PURE__ */ new Map());
|
|
4956
5426
|
}
|
|
4957
5427
|
get vertices() {
|
|
@@ -5908,46 +6378,67 @@ var dataStructureTyped = (() => {
|
|
|
5908
6378
|
getBridges() {
|
|
5909
6379
|
return this.tarjan(false, true, false, false).bridges;
|
|
5910
6380
|
}
|
|
5911
|
-
|
|
5912
|
-
|
|
5913
|
-
|
|
5914
|
-
|
|
5915
|
-
|
|
5916
|
-
|
|
5917
|
-
|
|
5918
|
-
|
|
5919
|
-
|
|
5920
|
-
|
|
5921
|
-
|
|
5922
|
-
|
|
5923
|
-
|
|
6381
|
+
/**
|
|
6382
|
+
* Time Complexity: O(n)
|
|
6383
|
+
* Space Complexity: O(n)
|
|
6384
|
+
*/
|
|
6385
|
+
/**
|
|
6386
|
+
* Time Complexity: O(n)
|
|
6387
|
+
* Space Complexity: O(n)
|
|
6388
|
+
*
|
|
6389
|
+
* The `filter` function iterates over key-value pairs in a data structure and returns an array of
|
|
6390
|
+
* pairs that satisfy a given predicate.
|
|
6391
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
6392
|
+
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
|
|
6393
|
+
* in the filtered array. The callback function should return `true` if the element should be
|
|
6394
|
+
* included, and `
|
|
6395
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
6396
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
6397
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
6398
|
+
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
|
|
6399
|
+
* that satisfy the given predicate function.
|
|
6400
|
+
*/
|
|
6401
|
+
filter(predicate, thisArg) {
|
|
5924
6402
|
const filtered = [];
|
|
5925
6403
|
let index = 0;
|
|
5926
|
-
for (const
|
|
5927
|
-
if (predicate(
|
|
5928
|
-
filtered.push(
|
|
6404
|
+
for (const [key, value] of this) {
|
|
6405
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
6406
|
+
filtered.push([key, value]);
|
|
5929
6407
|
}
|
|
5930
6408
|
index++;
|
|
5931
6409
|
}
|
|
5932
6410
|
return filtered;
|
|
5933
6411
|
}
|
|
5934
|
-
|
|
6412
|
+
/**
|
|
6413
|
+
* Time Complexity: O(n)
|
|
6414
|
+
* Space Complexity: O(n)
|
|
6415
|
+
*/
|
|
6416
|
+
/**
|
|
6417
|
+
* Time Complexity: O(n)
|
|
6418
|
+
* Space Complexity: O(n)
|
|
6419
|
+
*
|
|
6420
|
+
* The `map` function iterates over the elements of a collection and applies a callback function to
|
|
6421
|
+
* each element, returning an array of the results.
|
|
6422
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
6423
|
+
* map. It takes four arguments:
|
|
6424
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
6425
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
6426
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
6427
|
+
* @returns The `map` function is returning an array of type `T[]`.
|
|
6428
|
+
*/
|
|
6429
|
+
map(callback, thisArg) {
|
|
5935
6430
|
const mapped = [];
|
|
5936
6431
|
let index = 0;
|
|
5937
|
-
for (const
|
|
5938
|
-
mapped.push(callback(
|
|
6432
|
+
for (const [key, value] of this) {
|
|
6433
|
+
mapped.push(callback.call(thisArg, value, key, index, this));
|
|
5939
6434
|
index++;
|
|
5940
6435
|
}
|
|
5941
6436
|
return mapped;
|
|
5942
6437
|
}
|
|
5943
|
-
|
|
5944
|
-
|
|
5945
|
-
|
|
5946
|
-
for (const entry of this) {
|
|
5947
|
-
accumulator = callback(accumulator, entry, index, this._vertices);
|
|
5948
|
-
index++;
|
|
6438
|
+
*_getIterator() {
|
|
6439
|
+
for (const vertex of this._vertices.values()) {
|
|
6440
|
+
yield [vertex.key, vertex.value];
|
|
5949
6441
|
}
|
|
5950
|
-
return accumulator;
|
|
5951
6442
|
}
|
|
5952
6443
|
_addVertexOnly(newVertex) {
|
|
5953
6444
|
if (this.hasVertex(newVertex)) {
|
|
@@ -6931,7 +7422,7 @@ var dataStructureTyped = (() => {
|
|
|
6931
7422
|
return "MAL_NODE" /* MAL_NODE */;
|
|
6932
7423
|
}
|
|
6933
7424
|
};
|
|
6934
|
-
var BinaryTree = class _BinaryTree {
|
|
7425
|
+
var BinaryTree = class _BinaryTree extends IterablePairBase {
|
|
6935
7426
|
/**
|
|
6936
7427
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
6937
7428
|
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
@@ -6942,6 +7433,7 @@ var dataStructureTyped = (() => {
|
|
|
6942
7433
|
* required.
|
|
6943
7434
|
*/
|
|
6944
7435
|
constructor(elements, options) {
|
|
7436
|
+
super();
|
|
6945
7437
|
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
6946
7438
|
__publicField(this, "_root");
|
|
6947
7439
|
__publicField(this, "_size");
|
|
@@ -8220,112 +8712,86 @@ var dataStructureTyped = (() => {
|
|
|
8220
8712
|
}
|
|
8221
8713
|
/**
|
|
8222
8714
|
* Time complexity: O(n)
|
|
8223
|
-
* Space complexity: O(
|
|
8715
|
+
* Space complexity: O(n)
|
|
8224
8716
|
*/
|
|
8225
8717
|
/**
|
|
8226
|
-
*
|
|
8227
|
-
*
|
|
8228
|
-
*
|
|
8229
|
-
*
|
|
8718
|
+
* Time complexity: O(n)
|
|
8719
|
+
* Space complexity: O(n)
|
|
8720
|
+
*
|
|
8721
|
+
* The `clone` function creates a new tree object and copies all the nodes from the original tree to
|
|
8722
|
+
* the new tree.
|
|
8723
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
8230
8724
|
*/
|
|
8231
|
-
|
|
8232
|
-
|
|
8233
|
-
|
|
8234
|
-
|
|
8725
|
+
clone() {
|
|
8726
|
+
const cloned = this.createTree();
|
|
8727
|
+
this.bfs((node) => cloned.add([node.key, node.value]));
|
|
8728
|
+
return cloned;
|
|
8235
8729
|
}
|
|
8236
8730
|
/**
|
|
8237
|
-
*
|
|
8238
|
-
*
|
|
8239
|
-
* @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
|
|
8240
|
-
* `tree`.
|
|
8241
|
-
* @returns The `filter` method is returning a new tree object that contains only the entries that
|
|
8242
|
-
* satisfy the given predicate function.
|
|
8731
|
+
* Time Complexity: O(n)
|
|
8732
|
+
* Space Complexity: O(n)
|
|
8243
8733
|
*/
|
|
8244
|
-
|
|
8734
|
+
/**
|
|
8735
|
+
* Time Complexity: O(n)
|
|
8736
|
+
* Space Complexity: O(n)
|
|
8737
|
+
*
|
|
8738
|
+
* The `filter` function creates a new tree by iterating over the elements of the current tree and
|
|
8739
|
+
* adding only the elements that satisfy the given predicate function.
|
|
8740
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
8741
|
+
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
8742
|
+
* included in the filtered tree or not.
|
|
8743
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
8744
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
8745
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
8746
|
+
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
8747
|
+
* pass the given predicate function.
|
|
8748
|
+
*/
|
|
8749
|
+
filter(predicate, thisArg) {
|
|
8245
8750
|
const newTree = this.createTree();
|
|
8751
|
+
let index = 0;
|
|
8246
8752
|
for (const [key, value] of this) {
|
|
8247
|
-
if (predicate(
|
|
8753
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
8248
8754
|
newTree.add([key, value]);
|
|
8249
8755
|
}
|
|
8250
8756
|
}
|
|
8251
8757
|
return newTree;
|
|
8252
8758
|
}
|
|
8253
8759
|
/**
|
|
8254
|
-
*
|
|
8255
|
-
*
|
|
8256
|
-
|
|
8760
|
+
* Time Complexity: O(n)
|
|
8761
|
+
* Space Complexity: O(n)
|
|
8762
|
+
*/
|
|
8763
|
+
/**
|
|
8764
|
+
* Time Complexity: O(n)
|
|
8765
|
+
* Space Complexity: O(n)
|
|
8766
|
+
*
|
|
8767
|
+
* The `map` function creates a new tree by applying a callback function to each key-value pair in
|
|
8768
|
+
* the original tree.
|
|
8769
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
8770
|
+
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
|
|
8771
|
+
* the index of the current pair, and a reference to the tree itself. The callback function should
|
|
8772
|
+
* return a new
|
|
8773
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
8774
|
+
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
|
|
8775
|
+
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
8257
8776
|
* @returns The `map` method is returning a new tree object.
|
|
8258
8777
|
*/
|
|
8259
|
-
map(callback) {
|
|
8778
|
+
map(callback, thisArg) {
|
|
8260
8779
|
const newTree = this.createTree();
|
|
8780
|
+
let index = 0;
|
|
8261
8781
|
for (const [key, value] of this) {
|
|
8262
|
-
newTree.add([key, callback(
|
|
8782
|
+
newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
|
|
8263
8783
|
}
|
|
8264
8784
|
return newTree;
|
|
8265
8785
|
}
|
|
8266
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
8267
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
8268
|
-
// const newTree = this.createTree();
|
|
8269
|
-
// for (const [key, value] of this) {
|
|
8270
|
-
// newTree.add(key, callback([key, value], this));
|
|
8271
|
-
// }
|
|
8272
|
-
// return newTree;
|
|
8273
|
-
// }
|
|
8274
|
-
|
|
8275
|
-
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
8276
|
-
* entry, accumulating a single value.
|
|
8277
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
8278
|
-
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
8279
|
-
* based on the logic defined in the callback function.
|
|
8280
|
-
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
8281
|
-
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
8282
|
-
* elements of the tree.
|
|
8283
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
8284
|
-
* all the entries in the tree and applying the callback function to each entry.
|
|
8285
|
-
*/
|
|
8286
|
-
reduce(callback, initialValue) {
|
|
8287
|
-
let accumulator = initialValue;
|
|
8288
|
-
for (const [key, value] of this) {
|
|
8289
|
-
accumulator = callback(accumulator, [key, value], this);
|
|
8290
|
-
}
|
|
8291
|
-
return accumulator;
|
|
8292
|
-
}
|
|
8293
|
-
/**
|
|
8294
|
-
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
8295
|
-
* either an iterative or recursive manner.
|
|
8296
|
-
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
8297
|
-
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
8298
|
-
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
8299
|
-
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
8300
|
-
* binary tree nodes in a specific order.
|
|
8301
|
-
*/
|
|
8302
|
-
*[Symbol.iterator](node = this.root) {
|
|
8303
|
-
if (!node)
|
|
8304
|
-
return;
|
|
8305
|
-
if (this.iterationType === "ITERATIVE" /* ITERATIVE */) {
|
|
8306
|
-
const stack = [];
|
|
8307
|
-
let current = node;
|
|
8308
|
-
while (current || stack.length > 0) {
|
|
8309
|
-
while (current && !isNaN(current.key)) {
|
|
8310
|
-
stack.push(current);
|
|
8311
|
-
current = current.left;
|
|
8312
|
-
}
|
|
8313
|
-
current = stack.pop();
|
|
8314
|
-
if (current && !isNaN(current.key)) {
|
|
8315
|
-
yield [current.key, current.value];
|
|
8316
|
-
current = current.right;
|
|
8317
|
-
}
|
|
8318
|
-
}
|
|
8319
|
-
} else {
|
|
8320
|
-
if (node.left && !isNaN(node.key)) {
|
|
8321
|
-
yield* __yieldStar(this[Symbol.iterator](node.left));
|
|
8322
|
-
}
|
|
8323
|
-
yield [node.key, node.value];
|
|
8324
|
-
if (node.right && !isNaN(node.key)) {
|
|
8325
|
-
yield* __yieldStar(this[Symbol.iterator](node.right));
|
|
8326
|
-
}
|
|
8327
|
-
}
|
|
8328
|
-
}
|
|
8786
|
+
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
8787
|
+
// // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
8788
|
+
// // const newTree = this.createTree();
|
|
8789
|
+
// // for (const [key, value] of this) {
|
|
8790
|
+
// // newTree.add(key, callback([key, value], this));
|
|
8791
|
+
// // }
|
|
8792
|
+
// // return newTree;
|
|
8793
|
+
// // }
|
|
8794
|
+
//
|
|
8329
8795
|
/**
|
|
8330
8796
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
8331
8797
|
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
@@ -8355,6 +8821,33 @@ var dataStructureTyped = (() => {
|
|
|
8355
8821
|
};
|
|
8356
8822
|
display(beginRoot);
|
|
8357
8823
|
}
|
|
8824
|
+
*_getIterator(node = this.root) {
|
|
8825
|
+
if (!node)
|
|
8826
|
+
return;
|
|
8827
|
+
if (this.iterationType === "ITERATIVE" /* ITERATIVE */) {
|
|
8828
|
+
const stack = [];
|
|
8829
|
+
let current = node;
|
|
8830
|
+
while (current || stack.length > 0) {
|
|
8831
|
+
while (current && !isNaN(current.key)) {
|
|
8832
|
+
stack.push(current);
|
|
8833
|
+
current = current.left;
|
|
8834
|
+
}
|
|
8835
|
+
current = stack.pop();
|
|
8836
|
+
if (current && !isNaN(current.key)) {
|
|
8837
|
+
yield [current.key, current.value];
|
|
8838
|
+
current = current.right;
|
|
8839
|
+
}
|
|
8840
|
+
}
|
|
8841
|
+
} else {
|
|
8842
|
+
if (node.left && !isNaN(node.key)) {
|
|
8843
|
+
yield* __yieldStar(this[Symbol.iterator](node.left));
|
|
8844
|
+
}
|
|
8845
|
+
yield [node.key, node.value];
|
|
8846
|
+
if (node.right && !isNaN(node.key)) {
|
|
8847
|
+
yield* __yieldStar(this[Symbol.iterator](node.right));
|
|
8848
|
+
}
|
|
8849
|
+
}
|
|
8850
|
+
}
|
|
8358
8851
|
_displayAux(node, options) {
|
|
8359
8852
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
8360
8853
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
@@ -10771,6 +11264,22 @@ var dataStructureTyped = (() => {
|
|
|
10771
11264
|
super.clear();
|
|
10772
11265
|
this._count = 0;
|
|
10773
11266
|
}
|
|
11267
|
+
/**
|
|
11268
|
+
* Time complexity: O(n)
|
|
11269
|
+
* Space complexity: O(n)
|
|
11270
|
+
*/
|
|
11271
|
+
/**
|
|
11272
|
+
* Time complexity: O(n)
|
|
11273
|
+
* Space complexity: O(n)
|
|
11274
|
+
*
|
|
11275
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
11276
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
11277
|
+
*/
|
|
11278
|
+
clone() {
|
|
11279
|
+
const cloned = this.createTree();
|
|
11280
|
+
this.bfs((node) => cloned.add([node.key, node.value], node.count));
|
|
11281
|
+
return cloned;
|
|
11282
|
+
}
|
|
10774
11283
|
/**
|
|
10775
11284
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
10776
11285
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -11492,8 +12001,9 @@ var dataStructureTyped = (() => {
|
|
|
11492
12001
|
this.children = /* @__PURE__ */ new Map();
|
|
11493
12002
|
}
|
|
11494
12003
|
};
|
|
11495
|
-
var Trie = class _Trie {
|
|
12004
|
+
var Trie = class _Trie extends IterableElementBase {
|
|
11496
12005
|
constructor(words, caseSensitive = true) {
|
|
12006
|
+
super();
|
|
11497
12007
|
__publicField(this, "_size");
|
|
11498
12008
|
__publicField(this, "_caseSensitive");
|
|
11499
12009
|
__publicField(this, "_root");
|
|
@@ -11788,56 +12298,75 @@ var dataStructureTyped = (() => {
|
|
|
11788
12298
|
dfs(startNode, prefix);
|
|
11789
12299
|
return words;
|
|
11790
12300
|
}
|
|
11791
|
-
|
|
11792
|
-
|
|
11793
|
-
|
|
11794
|
-
|
|
11795
|
-
|
|
11796
|
-
|
|
11797
|
-
|
|
11798
|
-
|
|
11799
|
-
|
|
11800
|
-
|
|
11801
|
-
|
|
11802
|
-
|
|
11803
|
-
|
|
11804
|
-
|
|
11805
|
-
|
|
11806
|
-
|
|
11807
|
-
|
|
11808
|
-
|
|
11809
|
-
filter(predicate) {
|
|
12301
|
+
/**
|
|
12302
|
+
* Time Complexity: O(n)
|
|
12303
|
+
* Space Complexity: O(n)
|
|
12304
|
+
*/
|
|
12305
|
+
/**
|
|
12306
|
+
* Time Complexity: O(n)
|
|
12307
|
+
* Space Complexity: O(n)
|
|
12308
|
+
*
|
|
12309
|
+
* The `filter` function takes a predicate function and returns a new array containing all the
|
|
12310
|
+
* elements for which the predicate function returns true.
|
|
12311
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
12312
|
+
* `word`, `index`, and `this`. It should return a boolean value indicating whether the current
|
|
12313
|
+
* element should be included in the filtered results or not.
|
|
12314
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
12315
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
12316
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
12317
|
+
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
12318
|
+
*/
|
|
12319
|
+
filter(predicate, thisArg) {
|
|
11810
12320
|
const results = [];
|
|
11811
12321
|
let index = 0;
|
|
11812
12322
|
for (const word of this) {
|
|
11813
|
-
if (predicate(word, index, this)) {
|
|
12323
|
+
if (predicate.call(thisArg, word, index, this)) {
|
|
11814
12324
|
results.push(word);
|
|
11815
12325
|
}
|
|
11816
12326
|
index++;
|
|
11817
12327
|
}
|
|
11818
12328
|
return results;
|
|
11819
12329
|
}
|
|
11820
|
-
|
|
12330
|
+
/**
|
|
12331
|
+
* Time Complexity: O(n)
|
|
12332
|
+
* Space Complexity: O(n)
|
|
12333
|
+
*/
|
|
12334
|
+
/**
|
|
12335
|
+
* Time Complexity: O(n)
|
|
12336
|
+
* Space Complexity: O(n)
|
|
12337
|
+
*
|
|
12338
|
+
* The `map` function creates a new Trie by applying a callback function to each element in the Trie.
|
|
12339
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
12340
|
+
* Trie. It takes three arguments: the current element in the Trie, the index of the current element,
|
|
12341
|
+
* and the Trie itself. The callback function should return a new value for the element.
|
|
12342
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
12343
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
12344
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
12345
|
+
* @returns The `map` function is returning a new Trie object.
|
|
12346
|
+
*/
|
|
12347
|
+
map(callback, thisArg) {
|
|
11821
12348
|
const newTrie = new _Trie();
|
|
11822
12349
|
let index = 0;
|
|
11823
12350
|
for (const word of this) {
|
|
11824
|
-
newTrie.add(callback(word, index, this));
|
|
12351
|
+
newTrie.add(callback.call(thisArg, word, index, this));
|
|
11825
12352
|
index++;
|
|
11826
12353
|
}
|
|
11827
12354
|
return newTrie;
|
|
11828
12355
|
}
|
|
11829
|
-
reduce(callback, initialValue) {
|
|
11830
|
-
let accumulator = initialValue;
|
|
11831
|
-
let index = 0;
|
|
11832
|
-
for (const word of this) {
|
|
11833
|
-
accumulator = callback(accumulator, word, index, this);
|
|
11834
|
-
index++;
|
|
11835
|
-
}
|
|
11836
|
-
return accumulator;
|
|
11837
|
-
}
|
|
11838
12356
|
print() {
|
|
11839
12357
|
console.log([...this]);
|
|
11840
12358
|
}
|
|
12359
|
+
*_getIterator() {
|
|
12360
|
+
function* _dfs(node, path) {
|
|
12361
|
+
if (node.isEnd) {
|
|
12362
|
+
yield path;
|
|
12363
|
+
}
|
|
12364
|
+
for (const [char, childNode] of node.children) {
|
|
12365
|
+
yield* __yieldStar(_dfs(childNode, path + char));
|
|
12366
|
+
}
|
|
12367
|
+
}
|
|
12368
|
+
yield* __yieldStar(_dfs(this.root, ""));
|
|
12369
|
+
}
|
|
11841
12370
|
/**
|
|
11842
12371
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
11843
12372
|
* Space Complexity: O(1) - Constant space.
|