deque-typed 1.48.1 → 1.48.3
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/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -16
- package/dist/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
- package/dist/data-structures/binary-tree/binary-tree.js +140 -182
- package/dist/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/data-structures/binary-tree/bst.js +54 -57
- package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/data-structures/hash/hash-map.js +69 -173
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- 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/avl-tree.ts +20 -21
- package/src/data-structures/binary-tree/binary-tree.ts +222 -267
- package/src/data-structures/binary-tree/bst.ts +86 -82
- package/src/data-structures/binary-tree/rb-tree.ts +25 -26
- package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- 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/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- 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/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/index.ts +1 -0
|
@@ -5,13 +5,15 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
8
|
+
import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types';
|
|
9
9
|
import { HeapOptions } from "../../types";
|
|
10
|
+
import { IterableElementBase } from "../base";
|
|
10
11
|
|
|
11
|
-
export class Heap<E = any> {
|
|
12
|
+
export class Heap<E = any> extends IterableElementBase<E> {
|
|
12
13
|
options: HeapOptions<E>;
|
|
13
14
|
|
|
14
15
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>) {
|
|
16
|
+
super();
|
|
15
17
|
const defaultComparator = (a: E, b: E) => {
|
|
16
18
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
19
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -339,56 +341,75 @@ export class Heap<E = any> {
|
|
|
339
341
|
for (let i = Math.floor(this.size / 2); i >= 0; i--) this._sinkDown(i, this.elements.length >> 1);
|
|
340
342
|
}
|
|
341
343
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
forEach(callback: (element: E, index: number, heap: this) => void): void {
|
|
349
|
-
let index = 0;
|
|
350
|
-
for (const el of this) {
|
|
351
|
-
callback(el, index, this);
|
|
352
|
-
index++;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
344
|
+
/**
|
|
345
|
+
* Time Complexity: O(n)
|
|
346
|
+
* Space Complexity: O(n)
|
|
347
|
+
*/
|
|
355
348
|
|
|
356
|
-
|
|
357
|
-
|
|
349
|
+
/**
|
|
350
|
+
* Time Complexity: O(n)
|
|
351
|
+
* Space Complexity: O(n)
|
|
352
|
+
*
|
|
353
|
+
* The `filter` function creates a new Heap object containing elements that pass a given callback
|
|
354
|
+
* function.
|
|
355
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
356
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
357
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
358
|
+
* element should be included in the filtered list
|
|
359
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
360
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
361
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
362
|
+
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
|
|
363
|
+
* the filter condition specified by the `callback` function.
|
|
364
|
+
*/
|
|
365
|
+
filter(callback: ElementCallback<E, boolean>, thisArg?: any): Heap<E> {
|
|
366
|
+
const filteredList = new Heap<E>();
|
|
358
367
|
let index = 0;
|
|
359
|
-
for (const
|
|
360
|
-
if (
|
|
361
|
-
|
|
368
|
+
for (const current of this) {
|
|
369
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
370
|
+
filteredList.push(current);
|
|
362
371
|
}
|
|
363
372
|
index++;
|
|
364
373
|
}
|
|
365
|
-
return
|
|
374
|
+
return filteredList;
|
|
366
375
|
}
|
|
367
376
|
|
|
368
|
-
|
|
377
|
+
/**
|
|
378
|
+
* Time Complexity: O(n)
|
|
379
|
+
* Space Complexity: O(n)
|
|
380
|
+
*/
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Time Complexity: O(n)
|
|
384
|
+
* Space Complexity: O(n)
|
|
385
|
+
*
|
|
386
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
387
|
+
* original heap.
|
|
388
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
389
|
+
* original heap. It takes three arguments: the current element, the index of the current element,
|
|
390
|
+
* and the original heap itself. The callback function should return a value of type T, which will be
|
|
391
|
+
* added to the mapped heap.
|
|
392
|
+
* @param comparator - The `comparator` parameter is a function that is used to compare elements in
|
|
393
|
+
* the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
|
|
394
|
+
* `b`, a positive number if `a` is greater than `b`, or
|
|
395
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
396
|
+
* specify the value of `this` within the callback function. It is used when you want to bind a
|
|
397
|
+
* specific object as the context for the callback function. If `thisArg` is not provided,
|
|
398
|
+
* `undefined` is used as
|
|
399
|
+
* @returns a new instance of the Heap class, which is created using the mapped elements from the
|
|
400
|
+
* original Heap.
|
|
401
|
+
*/
|
|
402
|
+
map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T> {
|
|
369
403
|
|
|
370
404
|
const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
|
|
371
405
|
let index = 0;
|
|
372
406
|
for (const el of this) {
|
|
373
|
-
mappedHeap.add(callback(el, index, this));
|
|
407
|
+
mappedHeap.add(callback.call(thisArg, el, index, this));
|
|
374
408
|
index++;
|
|
375
409
|
}
|
|
376
410
|
return mappedHeap;
|
|
377
411
|
}
|
|
378
412
|
|
|
379
|
-
reduce<T>(
|
|
380
|
-
callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T,
|
|
381
|
-
initialValue: T
|
|
382
|
-
): T {
|
|
383
|
-
let accumulator: T = initialValue;
|
|
384
|
-
let index = 0;
|
|
385
|
-
for (const el of this) {
|
|
386
|
-
accumulator = callback(accumulator, el, index, this);
|
|
387
|
-
index++;
|
|
388
|
-
}
|
|
389
|
-
return accumulator;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
413
|
/**
|
|
393
414
|
* Time Complexity: O(log n)
|
|
394
415
|
* Space Complexity: O(1)
|
|
@@ -398,6 +419,12 @@ export class Heap<E = any> {
|
|
|
398
419
|
console.log([...this]);
|
|
399
420
|
}
|
|
400
421
|
|
|
422
|
+
protected* _getIterator() {
|
|
423
|
+
for (const element of this.elements) {
|
|
424
|
+
yield element;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
401
428
|
/**
|
|
402
429
|
* Time Complexity: O(n)
|
|
403
430
|
* Space Complexity: O(1)
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* data-structure-typed
|
|
3
6
|
*
|
|
@@ -22,11 +25,12 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
22
25
|
}
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
export class DoublyLinkedList<E = any> {
|
|
28
|
+
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
26
29
|
/**
|
|
27
30
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
31
|
*/
|
|
29
32
|
constructor(elements?: Iterable<E>) {
|
|
33
|
+
super();
|
|
30
34
|
this._head = undefined;
|
|
31
35
|
this._tail = undefined;
|
|
32
36
|
this._length = 0;
|
|
@@ -724,59 +728,32 @@ export class DoublyLinkedList<E = any> {
|
|
|
724
728
|
}
|
|
725
729
|
|
|
726
730
|
/**
|
|
727
|
-
*
|
|
728
|
-
*/
|
|
729
|
-
* [Symbol.iterator]() {
|
|
730
|
-
let current = this.head;
|
|
731
|
-
|
|
732
|
-
while (current) {
|
|
733
|
-
yield current.value;
|
|
734
|
-
current = current.next;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
/**
|
|
739
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
740
|
-
* Space Complexity: O(1)
|
|
741
|
-
*/
|
|
742
|
-
|
|
743
|
-
/**
|
|
744
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
745
|
-
* Space Complexity: O(1)
|
|
746
|
-
*
|
|
747
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
748
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
749
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
750
|
-
* current node in the linked list.
|
|
751
|
-
*/
|
|
752
|
-
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void {
|
|
753
|
-
let index = 0;
|
|
754
|
-
for (const el of this) {
|
|
755
|
-
callback(el, index, this);
|
|
756
|
-
index++;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
/**
|
|
761
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
731
|
+
* Time Complexity: O(n)
|
|
762
732
|
* Space Complexity: O(n)
|
|
763
733
|
*/
|
|
764
734
|
|
|
765
735
|
/**
|
|
766
|
-
* Time Complexity: O(n)
|
|
736
|
+
* Time Complexity: O(n)
|
|
767
737
|
* Space Complexity: O(n)
|
|
768
738
|
*
|
|
769
|
-
* The `filter` function
|
|
770
|
-
*
|
|
771
|
-
*
|
|
772
|
-
*
|
|
773
|
-
*
|
|
774
|
-
|
|
775
|
-
|
|
739
|
+
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
|
|
740
|
+
* list and applying a callback function to each element, returning only the elements for which the
|
|
741
|
+
* callback function returns true.
|
|
742
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
743
|
+
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current
|
|
744
|
+
* element, and the DoublyLinkedList itself. The callback function should return a boolean value
|
|
745
|
+
* indicating whether the current element should be included
|
|
746
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
747
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
748
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
749
|
+
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
|
|
750
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
751
|
+
*/
|
|
752
|
+
filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E> {
|
|
776
753
|
const filteredList = new DoublyLinkedList<E>();
|
|
777
754
|
let index = 0;
|
|
778
755
|
for (const current of this) {
|
|
779
|
-
if (callback(current, index, this)) {
|
|
756
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
780
757
|
filteredList.push(current);
|
|
781
758
|
}
|
|
782
759
|
index++;
|
|
@@ -790,21 +767,27 @@ export class DoublyLinkedList<E = any> {
|
|
|
790
767
|
*/
|
|
791
768
|
|
|
792
769
|
/**
|
|
793
|
-
* Time Complexity: O(n)
|
|
770
|
+
* Time Complexity: O(n)
|
|
794
771
|
* Space Complexity: O(n)
|
|
795
772
|
*
|
|
796
|
-
* The `map` function
|
|
797
|
-
*
|
|
798
|
-
* @param callback - The callback parameter is a function that
|
|
799
|
-
*
|
|
800
|
-
* DoublyLinkedList
|
|
801
|
-
*
|
|
802
|
-
|
|
803
|
-
|
|
773
|
+
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element
|
|
774
|
+
* in the original list.
|
|
775
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
776
|
+
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
|
|
777
|
+
* and the DoublyLinkedList itself. The callback function should return a value that will be added to
|
|
778
|
+
* the new DoublyLinkedList that
|
|
779
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
780
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
781
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
782
|
+
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
|
|
783
|
+
* of applying the provided `callback` function to each element in the original `DoublyLinkedList`
|
|
784
|
+
* object.
|
|
785
|
+
*/
|
|
786
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T> {
|
|
804
787
|
const mappedList = new DoublyLinkedList<T>();
|
|
805
788
|
let index = 0;
|
|
806
789
|
for (const current of this) {
|
|
807
|
-
mappedList.push(callback(current, index, this));
|
|
790
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
808
791
|
index++;
|
|
809
792
|
}
|
|
810
793
|
|
|
@@ -816,31 +799,19 @@ export class DoublyLinkedList<E = any> {
|
|
|
816
799
|
* Space Complexity: O(n)
|
|
817
800
|
*/
|
|
818
801
|
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
821
|
-
* Space Complexity: O(n)
|
|
822
|
-
*
|
|
823
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
824
|
-
* single value.
|
|
825
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
826
|
-
* used to perform a specific operation on each element of the linked list.
|
|
827
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
828
|
-
* point for the reduction operation.
|
|
829
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
830
|
-
* elements in the linked list.
|
|
831
|
-
*/
|
|
832
|
-
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T {
|
|
833
|
-
let accumulator = initialValue;
|
|
834
|
-
let index = 0;
|
|
835
|
-
for (const current of this) {
|
|
836
|
-
accumulator = callback(accumulator, current, index, this);
|
|
837
|
-
index++;
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
return accumulator;
|
|
841
|
-
}
|
|
842
|
-
|
|
843
802
|
print(): void {
|
|
844
803
|
console.log([...this]);
|
|
845
804
|
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
808
|
+
*/
|
|
809
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
810
|
+
let current = this.head;
|
|
811
|
+
|
|
812
|
+
while (current) {
|
|
813
|
+
yield current.value;
|
|
814
|
+
current = current.next;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
846
817
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* data-structure-typed
|
|
3
6
|
*
|
|
@@ -20,11 +23,12 @@ export class SinglyLinkedListNode<E = any> {
|
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
export class SinglyLinkedList<E = any> {
|
|
26
|
+
export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
24
27
|
/**
|
|
25
28
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
29
|
*/
|
|
27
30
|
constructor(elements?: Iterable<E>) {
|
|
31
|
+
super();
|
|
28
32
|
this._head = undefined;
|
|
29
33
|
this._tail = undefined;
|
|
30
34
|
this._length = 0;
|
|
@@ -670,59 +674,32 @@ export class SinglyLinkedList<E = any> {
|
|
|
670
674
|
}
|
|
671
675
|
|
|
672
676
|
/**
|
|
673
|
-
*
|
|
674
|
-
*/
|
|
675
|
-
* [Symbol.iterator]() {
|
|
676
|
-
let current = this.head;
|
|
677
|
-
|
|
678
|
-
while (current) {
|
|
679
|
-
yield current.value;
|
|
680
|
-
current = current.next;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
/**
|
|
685
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
686
|
-
* Space Complexity: O(1)
|
|
687
|
-
*/
|
|
688
|
-
|
|
689
|
-
/**
|
|
690
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
691
|
-
* Space Complexity: O(1)
|
|
692
|
-
*
|
|
693
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
694
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
695
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
696
|
-
* current node in the linked list.
|
|
697
|
-
*/
|
|
698
|
-
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void {
|
|
699
|
-
let index = 0;
|
|
700
|
-
for (const el of this) {
|
|
701
|
-
callback(el, index, this);
|
|
702
|
-
index++;
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
/**
|
|
707
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
677
|
+
* Time Complexity: O(n)
|
|
708
678
|
* Space Complexity: O(n)
|
|
709
679
|
*/
|
|
710
680
|
|
|
711
681
|
/**
|
|
712
|
-
* Time Complexity: O(n)
|
|
682
|
+
* Time Complexity: O(n)
|
|
713
683
|
* Space Complexity: O(n)
|
|
714
684
|
*
|
|
715
|
-
* The `filter` function
|
|
716
|
-
*
|
|
717
|
-
*
|
|
718
|
-
*
|
|
719
|
-
*
|
|
720
|
-
|
|
721
|
-
|
|
685
|
+
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
|
|
686
|
+
* list and applying a callback function to each element to determine if it should be included in the
|
|
687
|
+
* filtered list.
|
|
688
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
689
|
+
* list. It takes three arguments: the current element, the index of the current element, and the
|
|
690
|
+
* list itself. The callback function should return a boolean value indicating whether the current
|
|
691
|
+
* element should be included in the filtered list or not
|
|
692
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
693
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
694
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
695
|
+
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
|
|
696
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
697
|
+
*/
|
|
698
|
+
filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E> {
|
|
722
699
|
const filteredList = new SinglyLinkedList<E>();
|
|
723
700
|
let index = 0;
|
|
724
701
|
for (const current of this) {
|
|
725
|
-
if (callback(current, index, this)) {
|
|
702
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
726
703
|
filteredList.push(current);
|
|
727
704
|
}
|
|
728
705
|
index++;
|
|
@@ -730,27 +707,30 @@ export class SinglyLinkedList<E = any> {
|
|
|
730
707
|
return filteredList;
|
|
731
708
|
}
|
|
732
709
|
|
|
710
|
+
|
|
733
711
|
/**
|
|
734
712
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
735
713
|
* Space Complexity: O(n)
|
|
736
714
|
*/
|
|
737
|
-
|
|
738
715
|
/**
|
|
739
|
-
* Time Complexity: O(n)
|
|
716
|
+
* Time Complexity: O(n)
|
|
740
717
|
* Space Complexity: O(n)
|
|
741
718
|
*
|
|
742
|
-
* The `map` function
|
|
743
|
-
*
|
|
744
|
-
* @param callback - The callback parameter is a function that
|
|
745
|
-
* the
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
|
|
749
|
-
|
|
719
|
+
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element
|
|
720
|
+
* of the original list.
|
|
721
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
722
|
+
* the linked list. It takes three arguments:
|
|
723
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
724
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
725
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
726
|
+
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
|
|
727
|
+
* of applying the provided `callback` function to each element in the original list.
|
|
728
|
+
*/
|
|
729
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T> {
|
|
750
730
|
const mappedList = new SinglyLinkedList<T>();
|
|
751
731
|
let index = 0;
|
|
752
732
|
for (const current of this) {
|
|
753
|
-
mappedList.push(callback(current, index, this));
|
|
733
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
754
734
|
index++;
|
|
755
735
|
}
|
|
756
736
|
|
|
@@ -762,31 +742,16 @@ export class SinglyLinkedList<E = any> {
|
|
|
762
742
|
* Space Complexity: O(n)
|
|
763
743
|
*/
|
|
764
744
|
|
|
765
|
-
/**
|
|
766
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
767
|
-
* Space Complexity: O(n)
|
|
768
|
-
*
|
|
769
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
770
|
-
* single value.
|
|
771
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
772
|
-
* used to perform a specific operation on each element of the linked list.
|
|
773
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
774
|
-
* point for the reduction operation.
|
|
775
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
776
|
-
* elements in the linked list.
|
|
777
|
-
*/
|
|
778
|
-
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T {
|
|
779
|
-
let accumulator = initialValue;
|
|
780
|
-
let index = 0;
|
|
781
|
-
for (const current of this) {
|
|
782
|
-
accumulator = callback(accumulator, current, index, this);
|
|
783
|
-
index++;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
return accumulator;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
745
|
print(): void {
|
|
790
746
|
console.log([...this]);
|
|
791
747
|
}
|
|
748
|
+
|
|
749
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
750
|
+
let current = this.head;
|
|
751
|
+
|
|
752
|
+
while (current) {
|
|
753
|
+
yield current.value;
|
|
754
|
+
current = current.next;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
792
757
|
}
|