bst-typed 1.51.9 → 1.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/data-structures/binary-tree/binary-tree.js +5 -3
- package/dist/data-structures/binary-tree/bst.d.ts +3 -11
- package/dist/data-structures/binary-tree/bst.js +2 -10
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +20 -18
- package/dist/data-structures/queue/deque.js +27 -20
- package/dist/data-structures/queue/queue.d.ts +8 -28
- package/dist/data-structures/queue/queue.js +15 -31
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +18 -13
- package/dist/data-structures/trie/trie.js +26 -15
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
- package/src/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/data-structures/binary-tree/bst.ts +8 -19
- package/src/data-structures/binary-tree/rb-tree.ts +2 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -3
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +35 -24
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +33 -18
- package/src/interfaces/binary-tree.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { HeapOptions } from '../../types';
|
|
8
|
+
import type { Comparator, ElementCallback, HeapOptions } from '../../types';
|
|
9
9
|
import { Heap } from './heap';
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -16,21 +16,96 @@ import { Heap } from './heap';
|
|
|
16
16
|
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
17
17
|
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
|
18
18
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
19
|
-
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum
|
|
19
|
+
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
20
20
|
*/
|
|
21
|
-
export class MaxHeap<E = any> extends Heap<E> {
|
|
22
|
-
constructor(
|
|
23
|
-
elements
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return b - a;
|
|
21
|
+
export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
22
|
+
constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
|
|
23
|
+
super(elements, {
|
|
24
|
+
comparator: (a: E, b: E): number => {
|
|
25
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
26
|
+
throw TypeError(
|
|
27
|
+
`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
|
|
28
|
+
);
|
|
30
29
|
}
|
|
30
|
+
if (a < b) return 1;
|
|
31
|
+
if (a > b) return -1;
|
|
32
|
+
return 0;
|
|
33
|
+
},
|
|
34
|
+
...options
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The `clone` function returns a new instance of the `MaxHeap` class with the same properties as the
|
|
40
|
+
* current instance.
|
|
41
|
+
* @returns The `clone()` method is returning a new instance of the `MaxHeap` class with the same
|
|
42
|
+
* properties as the current instance.
|
|
43
|
+
*/
|
|
44
|
+
override clone(): MaxHeap<E, R> {
|
|
45
|
+
return new MaxHeap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Time Complexity: O(n)
|
|
50
|
+
* Space Complexity: O(n)
|
|
51
|
+
*
|
|
52
|
+
* The `filter` function creates a new MaxHeap object containing elements that pass a given callback
|
|
53
|
+
* function.
|
|
54
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
55
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
56
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
57
|
+
* element should be included in the filtered list
|
|
58
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
59
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
60
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
61
|
+
* @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
|
|
62
|
+
* the filter condition specified by the `callback` function.
|
|
63
|
+
*/
|
|
64
|
+
override filter(callback: ElementCallback<E, R, boolean, MaxHeap<E, R>>, thisArg?: any): MaxHeap<E, R> {
|
|
65
|
+
const filteredList = new MaxHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
66
|
+
let index = 0;
|
|
67
|
+
for (const current of this) {
|
|
68
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
69
|
+
filteredList.add(current);
|
|
31
70
|
}
|
|
71
|
+
index++;
|
|
72
|
+
}
|
|
73
|
+
return filteredList;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Time Complexity: O(n log n)
|
|
78
|
+
* Space Complexity: O(n)
|
|
79
|
+
*
|
|
80
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
81
|
+
* original heap.
|
|
82
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
83
|
+
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
84
|
+
* element), and `this` (the heap itself). The callback function should return a value of
|
|
85
|
+
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
86
|
+
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
87
|
+
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
88
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
89
|
+
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
90
|
+
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
91
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
92
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
93
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
94
|
+
* value of
|
|
95
|
+
* @returns a new instance of the `MaxHeap` class with the mapped elements.
|
|
96
|
+
*/
|
|
97
|
+
override map<EM, RM>(
|
|
98
|
+
callback: ElementCallback<E, R, EM, MaxHeap<E, R>>,
|
|
99
|
+
comparator: Comparator<EM>,
|
|
100
|
+
toElementFn?: (rawElement: RM) => EM,
|
|
101
|
+
thisArg?: any
|
|
102
|
+
): MaxHeap<EM, RM> {
|
|
103
|
+
const mappedHeap: MaxHeap<EM, RM> = new MaxHeap<EM, RM>([], { comparator, toElementFn });
|
|
104
|
+
let index = 0;
|
|
105
|
+
for (const el of this) {
|
|
106
|
+
mappedHeap.add(callback.call(thisArg, el, index, this));
|
|
107
|
+
index++;
|
|
32
108
|
}
|
|
33
|
-
|
|
34
|
-
super(elements, options);
|
|
109
|
+
return mappedHeap;
|
|
35
110
|
}
|
|
36
111
|
}
|
|
@@ -5,32 +5,95 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { HeapOptions } from '../../types';
|
|
8
|
+
import type { Comparator, ElementCallback, HeapOptions } from '../../types';
|
|
9
9
|
import { Heap } from './heap';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
13
|
-
* 2.
|
|
13
|
+
* 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
|
|
14
14
|
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
15
15
|
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
16
16
|
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
17
17
|
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
|
18
|
-
* 7. Efficient Sorting Algorithms: For example, heap sort.
|
|
18
|
+
* 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
|
|
19
19
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
20
20
|
*/
|
|
21
|
-
export class MinHeap<E = any> extends Heap<E> {
|
|
22
|
-
constructor(
|
|
23
|
-
elements
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
export class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
22
|
+
constructor(elements: Iterable<E> | Iterable<R> = [], options?: HeapOptions<E, R>) {
|
|
23
|
+
super(elements, options);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The `clone` function returns a new instance of the `MinHeap` class with the same comparator and
|
|
28
|
+
* toElementFn as the original instance.
|
|
29
|
+
* @returns The `clone()` method is returning a new instance of the `MinHeap` class with the same
|
|
30
|
+
* properties as the current instance.
|
|
31
|
+
*/
|
|
32
|
+
override clone(): MinHeap<E, R> {
|
|
33
|
+
return new MinHeap<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Time Complexity: O(n)
|
|
38
|
+
* Space Complexity: O(n)
|
|
39
|
+
*
|
|
40
|
+
* The `filter` function creates a new MinHeap object containing elements that pass a given callback
|
|
41
|
+
* function.
|
|
42
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
43
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
44
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
45
|
+
* element should be included in the filtered list
|
|
46
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
47
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
48
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
49
|
+
* @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
|
|
50
|
+
* the filter condition specified by the `callback` function.
|
|
51
|
+
*/
|
|
52
|
+
override filter(callback: ElementCallback<E, R, boolean, MinHeap<E, R>>, thisArg?: any): MinHeap<E, R> {
|
|
53
|
+
const filteredList = new MinHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
54
|
+
let index = 0;
|
|
55
|
+
for (const current of this) {
|
|
56
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
57
|
+
filteredList.add(current);
|
|
31
58
|
}
|
|
59
|
+
index++;
|
|
32
60
|
}
|
|
33
|
-
|
|
34
|
-
|
|
61
|
+
return filteredList;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(n log n)
|
|
66
|
+
* Space Complexity: O(n)
|
|
67
|
+
*
|
|
68
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
69
|
+
* original heap.
|
|
70
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
71
|
+
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
72
|
+
* element), and `this` (the heap itself). The callback function should return a value of
|
|
73
|
+
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
74
|
+
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
75
|
+
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
76
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
77
|
+
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
78
|
+
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
79
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
80
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
81
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
82
|
+
* value of
|
|
83
|
+
* @returns a new instance of the `MinHeap` class with the mapped elements.
|
|
84
|
+
*/
|
|
85
|
+
override map<EM, RM>(
|
|
86
|
+
callback: ElementCallback<E, R, EM, MinHeap<E, R>>,
|
|
87
|
+
comparator: Comparator<EM>,
|
|
88
|
+
toElementFn?: (rawElement: RM) => EM,
|
|
89
|
+
thisArg?: any
|
|
90
|
+
): MinHeap<EM, RM> {
|
|
91
|
+
const mappedHeap: MinHeap<EM, RM> = new MinHeap<EM, RM>([], { comparator, toElementFn });
|
|
92
|
+
let index = 0;
|
|
93
|
+
for (const el of this) {
|
|
94
|
+
mappedHeap.add(callback.call(thisArg, el, index, this));
|
|
95
|
+
index++;
|
|
96
|
+
}
|
|
97
|
+
return mappedHeap;
|
|
35
98
|
}
|
|
36
99
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElementCallback } from '../../types';
|
|
8
|
+
import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
|
|
9
9
|
import { IterableElementBase } from '../base';
|
|
10
10
|
|
|
11
11
|
export class DoublyLinkedListNode<E = any> {
|
|
@@ -87,21 +87,17 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
87
87
|
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
88
88
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
89
89
|
*/
|
|
90
|
-
export class DoublyLinkedList<E = any> extends IterableElementBase<E
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
|
94
|
-
* initial elements to be added to the data structure. It defaults to an empty array if no elements
|
|
95
|
-
* are provided.
|
|
96
|
-
*/
|
|
97
|
-
constructor(elements: Iterable<E> = []) {
|
|
98
|
-
super();
|
|
90
|
+
export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
|
|
91
|
+
constructor(elements: Iterable<E> | Iterable<R> = [], options?: DoublyLinkedListOptions<E, R>) {
|
|
92
|
+
super(options);
|
|
99
93
|
this._head = undefined;
|
|
100
94
|
this._tail = undefined;
|
|
101
95
|
this._size = 0;
|
|
102
96
|
if (elements) {
|
|
103
97
|
for (const el of elements) {
|
|
104
|
-
this.
|
|
98
|
+
if (this.toElementFn) {
|
|
99
|
+
this.push(this.toElementFn(el as R));
|
|
100
|
+
} else this.push(el as E);
|
|
105
101
|
}
|
|
106
102
|
}
|
|
107
103
|
}
|
|
@@ -729,8 +725,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
729
725
|
* @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
|
|
730
726
|
* is a copy of the original list.
|
|
731
727
|
*/
|
|
732
|
-
clone(): DoublyLinkedList<E> {
|
|
733
|
-
return new DoublyLinkedList(this
|
|
728
|
+
clone(): DoublyLinkedList<E, R> {
|
|
729
|
+
return new DoublyLinkedList<E, R>(this);
|
|
734
730
|
}
|
|
735
731
|
|
|
736
732
|
/**
|
|
@@ -755,8 +751,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
755
751
|
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
|
|
756
752
|
* elements that pass the filter condition specified by the `callback` function.
|
|
757
753
|
*/
|
|
758
|
-
filter(callback: ElementCallback<E, boolean
|
|
759
|
-
const filteredList = new DoublyLinkedList<E>();
|
|
754
|
+
filter(callback: ElementCallback<E, R, boolean, DoublyLinkedList<E, R>>, thisArg?: any): DoublyLinkedList<E, R> {
|
|
755
|
+
const filteredList = new DoublyLinkedList<E, R>([], { toElementFn: this.toElementFn });
|
|
760
756
|
let index = 0;
|
|
761
757
|
for (const current of this) {
|
|
762
758
|
if (callback.call(thisArg, current, index, this)) {
|
|
@@ -773,24 +769,28 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
773
769
|
*/
|
|
774
770
|
|
|
775
771
|
/**
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
779
|
-
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element
|
|
780
|
-
* in the original list.
|
|
772
|
+
* The `map` function takes a callback function and returns a new DoublyLinkedList with the results
|
|
773
|
+
* of applying the callback to each element in the original list.
|
|
781
774
|
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
782
|
-
* DoublyLinkedList. It takes three arguments:
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
* @param
|
|
786
|
-
*
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
775
|
+
* original DoublyLinkedList. It takes three arguments: current (the current element being
|
|
776
|
+
* processed), index (the index of the current element), and this (the original DoublyLinkedList).
|
|
777
|
+
* The callback function should return a value of type
|
|
778
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
779
|
+
* convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
|
|
780
|
+
* input and returns the converted element. If this parameter is not provided, the raw element will
|
|
781
|
+
* be used as is.
|
|
782
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
783
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
784
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
785
|
+
* value of
|
|
786
|
+
* @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
|
|
787
|
+
*/
|
|
788
|
+
map<EM, RM>(
|
|
789
|
+
callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>,
|
|
790
|
+
toElementFn?: (rawElement: RM) => EM,
|
|
791
|
+
thisArg?: any
|
|
792
|
+
): DoublyLinkedList<EM, RM> {
|
|
793
|
+
const mappedList = new DoublyLinkedList<EM, RM>([], { toElementFn });
|
|
794
794
|
let index = 0;
|
|
795
795
|
for (const current of this) {
|
|
796
796
|
mappedList.push(callback.call(thisArg, current, index, this));
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElementCallback } from '../../types';
|
|
8
|
+
import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
|
|
9
9
|
import { IterableElementBase } from '../base';
|
|
10
10
|
|
|
11
11
|
export class SinglyLinkedListNode<E = any> {
|
|
@@ -59,17 +59,17 @@ export class SinglyLinkedListNode<E = any> {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
export class SinglyLinkedList<E = any> extends IterableElementBase<E
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
|
66
|
-
* initial elements to be added to the instance of the class. If no `elements` are provided, an empty
|
|
67
|
-
* array will be used as the default value.
|
|
68
|
-
*/
|
|
69
|
-
constructor(elements: Iterable<E> = []) {
|
|
70
|
-
super();
|
|
62
|
+
export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
|
|
63
|
+
constructor(elements: Iterable<E> | Iterable<R> = [], options?: SinglyLinkedListOptions<E, R>) {
|
|
64
|
+
super(options);
|
|
71
65
|
if (elements) {
|
|
72
|
-
for (const el of elements)
|
|
66
|
+
for (const el of elements) {
|
|
67
|
+
if (this.toElementFn) {
|
|
68
|
+
this.push(this.toElementFn(el as R));
|
|
69
|
+
} else {
|
|
70
|
+
this.push(el as E);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -673,8 +673,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
673
673
|
* @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
|
|
674
674
|
* is a clone of the original list.
|
|
675
675
|
*/
|
|
676
|
-
clone(): SinglyLinkedList<E> {
|
|
677
|
-
return new SinglyLinkedList<E>(this.
|
|
676
|
+
clone(): SinglyLinkedList<E, R> {
|
|
677
|
+
return new SinglyLinkedList<E, R>(this, { toElementFn: this.toElementFn });
|
|
678
678
|
}
|
|
679
679
|
|
|
680
680
|
/**
|
|
@@ -699,8 +699,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
699
699
|
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
|
|
700
700
|
* elements that pass the filter condition specified by the `callback` function.
|
|
701
701
|
*/
|
|
702
|
-
filter(callback: ElementCallback<E, boolean
|
|
703
|
-
const filteredList = new SinglyLinkedList<E>();
|
|
702
|
+
filter(callback: ElementCallback<E, R, boolean, SinglyLinkedList<E, R>>, thisArg?: any): SinglyLinkedList<E, R> {
|
|
703
|
+
const filteredList = new SinglyLinkedList<E, R>([], { toElementFn: this.toElementFn });
|
|
704
704
|
let index = 0;
|
|
705
705
|
for (const current of this) {
|
|
706
706
|
if (callback.call(thisArg, current, index, this)) {
|
|
@@ -715,22 +715,30 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
715
715
|
* Time Complexity: O(n)
|
|
716
716
|
* Space Complexity: O(n)
|
|
717
717
|
*/
|
|
718
|
+
|
|
718
719
|
/**
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
*
|
|
722
|
-
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element
|
|
723
|
-
* of the original list.
|
|
720
|
+
* The `map` function takes a callback function and returns a new SinglyLinkedList with the results
|
|
721
|
+
* of applying the callback to each element in the original list.
|
|
724
722
|
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
725
|
-
* the
|
|
726
|
-
*
|
|
727
|
-
*
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
723
|
+
* the original list. It takes three arguments: `current` (the current element being processed),
|
|
724
|
+
* `index` (the index of the current element), and `this` (the original list). It should return a
|
|
725
|
+
* value
|
|
726
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
727
|
+
* convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
|
|
728
|
+
* input and returns the converted element. If this parameter is not provided, the raw element will
|
|
729
|
+
* be used as is.
|
|
730
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
731
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
732
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
733
|
+
* value of
|
|
734
|
+
* @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
|
|
735
|
+
*/
|
|
736
|
+
map<EM, RM>(
|
|
737
|
+
callback: ElementCallback<E, R, EM, SinglyLinkedList<E, R>>,
|
|
738
|
+
toElementFn?: (rawElement: RM) => EM,
|
|
739
|
+
thisArg?: any
|
|
740
|
+
): SinglyLinkedList<EM, RM> {
|
|
741
|
+
const mappedList = new SinglyLinkedList<EM, RM>([], { toElementFn });
|
|
734
742
|
let index = 0;
|
|
735
743
|
for (const current of this) {
|
|
736
744
|
mappedList.push(callback.call(thisArg, current, index, this));
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { PriorityQueueOptions } from '../../types';
|
|
8
|
+
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
|
9
9
|
import { PriorityQueue } from './priority-queue';
|
|
10
10
|
|
|
11
|
-
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
+
export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
14
14
|
* comparator function.
|
|
@@ -16,21 +16,102 @@ export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
|
16
16
|
* elements to be added to the priority queue. It is optional and defaults to an empty array if not
|
|
17
17
|
* provided.
|
|
18
18
|
* @param options - The `options` parameter is an object that contains additional configuration
|
|
19
|
-
* options for the priority queue. In this case, it has a property called `comparator
|
|
19
|
+
* options for the priority queue. In this case, it has a property called `comparator,` which is a
|
|
20
20
|
* function used to compare elements in the priority queue.
|
|
21
21
|
*/
|
|
22
|
-
constructor(
|
|
23
|
-
elements
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return b - a;
|
|
22
|
+
constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {
|
|
23
|
+
super(elements, {
|
|
24
|
+
comparator: (a: E, b: E): number => {
|
|
25
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
26
|
+
throw TypeError(
|
|
27
|
+
`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
|
|
28
|
+
);
|
|
30
29
|
}
|
|
30
|
+
if (a < b) return 1;
|
|
31
|
+
if (a > b) return -1;
|
|
32
|
+
return 0;
|
|
33
|
+
},
|
|
34
|
+
...options
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
|
|
40
|
+
* comparator and toElementFn as the current instance.
|
|
41
|
+
* @returns The method is returning a new instance of the MaxPriorityQueue class with the same
|
|
42
|
+
* comparator and toElementFn as the current instance.
|
|
43
|
+
*/
|
|
44
|
+
override clone(): MaxPriorityQueue<E, R> {
|
|
45
|
+
return new MaxPriorityQueue<E, R>(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Time Complexity: O(n)
|
|
50
|
+
* Space Complexity: O(n)
|
|
51
|
+
*
|
|
52
|
+
* The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
|
|
53
|
+
* function.
|
|
54
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
55
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
56
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
57
|
+
* element should be included in the filtered list
|
|
58
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
59
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
60
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
61
|
+
* @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
|
|
62
|
+
* the filter condition specified by the `callback` function.
|
|
63
|
+
*/
|
|
64
|
+
override filter(
|
|
65
|
+
callback: ElementCallback<E, R, boolean, MaxPriorityQueue<E, R>>,
|
|
66
|
+
thisArg?: any
|
|
67
|
+
): MaxPriorityQueue<E, R> {
|
|
68
|
+
const filteredPriorityQueue = new MaxPriorityQueue<E, R>([], {
|
|
69
|
+
toElementFn: this.toElementFn,
|
|
70
|
+
comparator: this.comparator
|
|
71
|
+
});
|
|
72
|
+
let index = 0;
|
|
73
|
+
for (const current of this) {
|
|
74
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
75
|
+
filteredPriorityQueue.add(current);
|
|
31
76
|
}
|
|
77
|
+
index++;
|
|
78
|
+
}
|
|
79
|
+
return filteredPriorityQueue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Time Complexity: O(n log n)
|
|
84
|
+
* Space Complexity: O(n)
|
|
85
|
+
*
|
|
86
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
87
|
+
* original heap.
|
|
88
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
89
|
+
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
90
|
+
* element), and `this` (the heap itself). The callback function should return a value of
|
|
91
|
+
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
92
|
+
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
93
|
+
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
94
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
95
|
+
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
96
|
+
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
97
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
98
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
99
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
100
|
+
* value of
|
|
101
|
+
* @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
|
|
102
|
+
*/
|
|
103
|
+
override map<EM, RM>(
|
|
104
|
+
callback: ElementCallback<E, R, EM, MaxPriorityQueue<E, R>>,
|
|
105
|
+
comparator: Comparator<EM>,
|
|
106
|
+
toElementFn?: (rawElement: RM) => EM,
|
|
107
|
+
thisArg?: any
|
|
108
|
+
): MaxPriorityQueue<EM, RM> {
|
|
109
|
+
const mappedPriorityQueue = new MaxPriorityQueue<EM, RM>([], { comparator, toElementFn });
|
|
110
|
+
let index = 0;
|
|
111
|
+
for (const el of this) {
|
|
112
|
+
mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
|
|
113
|
+
index++;
|
|
32
114
|
}
|
|
33
|
-
|
|
34
|
-
super(elements, options);
|
|
115
|
+
return mappedPriorityQueue;
|
|
35
116
|
}
|
|
36
117
|
}
|