undirected-graph-typed 1.51.9 → 1.52.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/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 +13 -13
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -99
- package/dist/data-structures/binary-tree/binary-tree.js +56 -54
- package/dist/data-structures/binary-tree/bst.d.ts +37 -45
- package/dist/data-structures/binary-tree/bst.js +19 -27
- package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -10
- package/dist/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/data-structures/graph/directed-graph.js +2 -1
- 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 +27 -18
- package/dist/data-structures/queue/deque.js +43 -21
- package/dist/data-structures/queue/queue.d.ts +26 -29
- package/dist/data-structures/queue/queue.js +47 -38
- 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 +4 -4
- package/dist/types/common.d.ts +1 -22
- 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 +20 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +5 -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 +4 -2
- package/dist/types/data-structures/queue/queue.d.ts +4 -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} +26 -217
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +26 -26
- package/src/data-structures/binary-tree/avl-tree.ts +21 -21
- package/src/data-structures/binary-tree/binary-tree.ts +166 -161
- package/src/data-structures/binary-tree/bst.ts +61 -68
- package/src/data-structures/binary-tree/rb-tree.ts +19 -19
- package/src/data-structures/binary-tree/tree-multi-map.ts +19 -19
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +9 -7
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +72 -153
- 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 +33 -33
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -30
- 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 +53 -28
- package/src/data-structures/queue/queue.ts +61 -44
- package/src/data-structures/stack/stack.ts +32 -27
- package/src/data-structures/trie/trie.ts +34 -19
- package/src/interfaces/binary-tree.ts +4 -5
- package/src/types/common.ts +2 -24
- 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 +24 -5
- package/src/types/data-structures/binary-tree/bst.ts +9 -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/graph/abstract-graph.ts +8 -8
- 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 +6 -1
- package/src/types/data-structures/queue/queue.ts +5 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
|
@@ -5,9 +5,9 @@
|
|
|
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
|
-
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
10
|
+
export declare class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
11
11
|
/**
|
|
12
12
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
13
13
|
* comparator function.
|
|
@@ -15,9 +15,55 @@ export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
|
15
15
|
* elements to be added to the priority queue. It is optional and defaults to an empty array if not
|
|
16
16
|
* provided.
|
|
17
17
|
* @param options - The `options` parameter is an object that contains additional configuration
|
|
18
|
-
* options for the priority queue. In this case, it has a property called `comparator
|
|
18
|
+
* options for the priority queue. In this case, it has a property called `comparator,` which is a
|
|
19
19
|
* function used to compare elements in the priority queue. The `comparator` function takes two
|
|
20
|
-
* parameters `a` and `b
|
|
20
|
+
* parameters `a` and `b`
|
|
21
21
|
*/
|
|
22
|
-
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
22
|
+
constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
|
|
23
|
+
/**
|
|
24
|
+
* The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
|
|
25
|
+
* comparator and toElementFn as the original instance.
|
|
26
|
+
* @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
|
|
27
|
+
* properties as the current instance.
|
|
28
|
+
*/
|
|
29
|
+
clone(): MinPriorityQueue<E, R>;
|
|
30
|
+
/**
|
|
31
|
+
* Time Complexity: O(n)
|
|
32
|
+
* Space Complexity: O(n)
|
|
33
|
+
*
|
|
34
|
+
* The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
|
|
35
|
+
* function.
|
|
36
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
37
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
38
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
39
|
+
* element should be included in the filtered list
|
|
40
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
41
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
42
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
43
|
+
* @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
|
|
44
|
+
* the filter condition specified by the `callback` function.
|
|
45
|
+
*/
|
|
46
|
+
filter(callback: ElementCallback<E, R, boolean, MinPriorityQueue<E, R>>, thisArg?: any): MinPriorityQueue<E, R>;
|
|
47
|
+
/**
|
|
48
|
+
* Time Complexity: O(n log n)
|
|
49
|
+
* Space Complexity: O(n)
|
|
50
|
+
*
|
|
51
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
52
|
+
* original heap.
|
|
53
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
54
|
+
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
55
|
+
* element), and `this` (the heap itself). The callback function should return a value of
|
|
56
|
+
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
57
|
+
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
58
|
+
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
59
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
60
|
+
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
61
|
+
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
62
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
63
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
64
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
65
|
+
* value of
|
|
66
|
+
* @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
|
|
67
|
+
*/
|
|
68
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM, MinPriorityQueue<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MinPriorityQueue<EM, RM>;
|
|
23
69
|
}
|
|
@@ -10,21 +10,81 @@ class MinPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
|
10
10
|
* elements to be added to the priority queue. It is optional and defaults to an empty array if not
|
|
11
11
|
* provided.
|
|
12
12
|
* @param options - The `options` parameter is an object that contains additional configuration
|
|
13
|
-
* options for the priority queue. In this case, it has a property called `comparator
|
|
13
|
+
* options for the priority queue. In this case, it has a property called `comparator,` which is a
|
|
14
14
|
* function used to compare elements in the priority queue. The `comparator` function takes two
|
|
15
|
-
* parameters `a` and `b
|
|
15
|
+
* parameters `a` and `b`
|
|
16
16
|
*/
|
|
17
|
-
constructor(elements = [], options
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
constructor(elements = [], options) {
|
|
18
|
+
super(elements, options);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
|
|
22
|
+
* comparator and toElementFn as the original instance.
|
|
23
|
+
* @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
|
|
24
|
+
* properties as the current instance.
|
|
25
|
+
*/
|
|
26
|
+
clone() {
|
|
27
|
+
return new MinPriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Time Complexity: O(n)
|
|
31
|
+
* Space Complexity: O(n)
|
|
32
|
+
*
|
|
33
|
+
* The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
|
|
34
|
+
* function.
|
|
35
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
36
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
37
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
38
|
+
* element should be included in the filtered list
|
|
39
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
40
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
41
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
42
|
+
* @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
|
|
43
|
+
* the filter condition specified by the `callback` function.
|
|
44
|
+
*/
|
|
45
|
+
filter(callback, thisArg) {
|
|
46
|
+
const filteredPriorityQueue = new MinPriorityQueue([], {
|
|
47
|
+
toElementFn: this.toElementFn,
|
|
48
|
+
comparator: this.comparator
|
|
49
|
+
});
|
|
50
|
+
let index = 0;
|
|
51
|
+
for (const current of this) {
|
|
52
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
53
|
+
filteredPriorityQueue.add(current);
|
|
24
54
|
}
|
|
55
|
+
index++;
|
|
25
56
|
}
|
|
26
|
-
|
|
27
|
-
|
|
57
|
+
return filteredPriorityQueue;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Time Complexity: O(n log n)
|
|
61
|
+
* Space Complexity: O(n)
|
|
62
|
+
*
|
|
63
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
64
|
+
* original heap.
|
|
65
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
66
|
+
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
67
|
+
* element), and `this` (the heap itself). The callback function should return a value of
|
|
68
|
+
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
69
|
+
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
70
|
+
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
71
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
72
|
+
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
73
|
+
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
74
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
75
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
76
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
77
|
+
* value of
|
|
78
|
+
* @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
|
|
79
|
+
*/
|
|
80
|
+
map(callback, comparator, toElementFn, thisArg) {
|
|
81
|
+
const mappedPriorityQueue = new MinPriorityQueue([], { comparator, toElementFn });
|
|
82
|
+
let index = 0;
|
|
83
|
+
for (const el of this) {
|
|
84
|
+
mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
|
|
85
|
+
index++;
|
|
86
|
+
}
|
|
87
|
+
return mappedPriorityQueue;
|
|
28
88
|
}
|
|
29
89
|
}
|
|
30
90
|
exports.MinPriorityQueue = MinPriorityQueue;
|
|
@@ -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 { PriorityQueueOptions } from '../../types';
|
|
8
|
+
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
|
9
9
|
import { Heap } from '../heap';
|
|
10
10
|
/**
|
|
11
11
|
* 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
|
|
@@ -15,14 +15,60 @@ import { Heap } from '../heap';
|
|
|
15
15
|
* 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
|
|
16
16
|
* 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
|
|
17
17
|
*/
|
|
18
|
-
export declare class PriorityQueue<E = any> extends Heap<E> {
|
|
18
|
+
export declare class PriorityQueue<E = any, R = any> extends Heap<E, R> {
|
|
19
19
|
/**
|
|
20
20
|
* The constructor initializes a priority queue with optional elements and options.
|
|
21
21
|
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
22
|
-
* elements to be added to the priority queue. It is an optional parameter and if not provided, the
|
|
22
|
+
* elements to be added to the priority queue. It is an optional parameter, and if not provided, the
|
|
23
23
|
* priority queue will be initialized as empty.
|
|
24
24
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
25
25
|
* behavior of the priority queue. It can contain the following properties:
|
|
26
26
|
*/
|
|
27
|
-
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
27
|
+
constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
|
|
28
|
+
/**
|
|
29
|
+
* The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
|
|
30
|
+
* and toElementFn as the original instance.
|
|
31
|
+
* @returns The method is returning a new instance of the `PriorityQueue` class with the same
|
|
32
|
+
* elements and properties as the current instance.
|
|
33
|
+
*/
|
|
34
|
+
clone(): PriorityQueue<E, R>;
|
|
35
|
+
/**
|
|
36
|
+
* Time Complexity: O(n)
|
|
37
|
+
* Space Complexity: O(n)
|
|
38
|
+
*
|
|
39
|
+
* The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
|
|
40
|
+
* function.
|
|
41
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
42
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
43
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
44
|
+
* element should be included in the filtered list
|
|
45
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
46
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
47
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
48
|
+
* @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
|
|
49
|
+
* the filter condition specified by the `callback` function.
|
|
50
|
+
*/
|
|
51
|
+
filter(callback: ElementCallback<E, R, boolean, PriorityQueue<E, R>>, thisArg?: any): PriorityQueue<E, R>;
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(n log n)
|
|
54
|
+
* Space Complexity: O(n)
|
|
55
|
+
*
|
|
56
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
57
|
+
* original heap.
|
|
58
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
59
|
+
* the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
|
|
60
|
+
* element), and `this` (the heap itself). The callback function should return a value of
|
|
61
|
+
* @param comparator - The `comparator` parameter is a function that defines the order of the
|
|
62
|
+
* elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
|
|
63
|
+
* if `a` should be placed before `b`, a positive number if `a` should be placed after
|
|
64
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
|
|
65
|
+
* element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
|
|
66
|
+
* returns a value of type `T`. This function is used to transform the elements of the original
|
|
67
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
68
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
69
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
70
|
+
* value of
|
|
71
|
+
* @returns a new instance of the `PriorityQueue` class with the mapped elements.
|
|
72
|
+
*/
|
|
73
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM, PriorityQueue<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): PriorityQueue<EM, RM>;
|
|
28
74
|
}
|
|
@@ -14,7 +14,7 @@ class PriorityQueue extends heap_1.Heap {
|
|
|
14
14
|
/**
|
|
15
15
|
* The constructor initializes a priority queue with optional elements and options.
|
|
16
16
|
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
17
|
-
* elements to be added to the priority queue. It is an optional parameter and if not provided, the
|
|
17
|
+
* elements to be added to the priority queue. It is an optional parameter, and if not provided, the
|
|
18
18
|
* priority queue will be initialized as empty.
|
|
19
19
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
20
20
|
* behavior of the priority queue. It can contain the following properties:
|
|
@@ -22,5 +22,74 @@ class PriorityQueue extends heap_1.Heap {
|
|
|
22
22
|
constructor(elements = [], options) {
|
|
23
23
|
super(elements, options);
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
|
|
27
|
+
* and toElementFn as the original instance.
|
|
28
|
+
* @returns The method is returning a new instance of the `PriorityQueue` class with the same
|
|
29
|
+
* elements and properties as the current instance.
|
|
30
|
+
*/
|
|
31
|
+
clone() {
|
|
32
|
+
return new PriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Time Complexity: O(n)
|
|
36
|
+
* Space Complexity: O(n)
|
|
37
|
+
*
|
|
38
|
+
* The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
|
|
39
|
+
* function.
|
|
40
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
41
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
42
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
43
|
+
* element should be included in the filtered list
|
|
44
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
45
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
46
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
47
|
+
* @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
|
|
48
|
+
* the filter condition specified by the `callback` function.
|
|
49
|
+
*/
|
|
50
|
+
filter(callback, thisArg) {
|
|
51
|
+
const filteredPriorityQueue = new PriorityQueue([], {
|
|
52
|
+
toElementFn: this.toElementFn,
|
|
53
|
+
comparator: this.comparator
|
|
54
|
+
});
|
|
55
|
+
let index = 0;
|
|
56
|
+
for (const current of this) {
|
|
57
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
58
|
+
filteredPriorityQueue.add(current);
|
|
59
|
+
}
|
|
60
|
+
index++;
|
|
61
|
+
}
|
|
62
|
+
return filteredPriorityQueue;
|
|
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 `PriorityQueue` class with the mapped elements.
|
|
84
|
+
*/
|
|
85
|
+
map(callback, comparator, toElementFn, thisArg) {
|
|
86
|
+
const mappedPriorityQueue = new PriorityQueue([], { comparator, toElementFn });
|
|
87
|
+
let index = 0;
|
|
88
|
+
for (const el of this) {
|
|
89
|
+
mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
|
|
90
|
+
index++;
|
|
91
|
+
}
|
|
92
|
+
return mappedPriorityQueue;
|
|
93
|
+
}
|
|
25
94
|
}
|
|
26
95
|
exports.PriorityQueue = PriorityQueue;
|
|
@@ -14,9 +14,9 @@ import { IterableElementBase } from '../base';
|
|
|
14
14
|
* 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
|
|
15
15
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
16
16
|
*/
|
|
17
|
-
export declare class Deque<E> extends IterableElementBase<E
|
|
17
|
+
export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E, R>> {
|
|
18
18
|
/**
|
|
19
|
-
* The constructor initializes a Deque object with
|
|
19
|
+
* The constructor initializes a Deque object with optional iterable of elements and options.
|
|
20
20
|
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
21
21
|
* elements to be added to the deque. It can also be an object with a `length` or `size` property
|
|
22
22
|
* that represents the number of elements in the iterable object. If no elements are provided, an
|
|
@@ -26,7 +26,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
26
26
|
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
|
|
27
27
|
* or is not a number
|
|
28
28
|
*/
|
|
29
|
-
constructor(elements?: IterableWithSizeOrLength<E>, options?: DequeOptions);
|
|
29
|
+
constructor(elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>, options?: DequeOptions<E, R>);
|
|
30
30
|
protected _bucketSize: number;
|
|
31
31
|
/**
|
|
32
32
|
* The bucketSize function returns the size of the bucket.
|
|
@@ -34,6 +34,13 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
34
34
|
* @return The size of the bucket
|
|
35
35
|
*/
|
|
36
36
|
get bucketSize(): number;
|
|
37
|
+
protected _maxLen: number;
|
|
38
|
+
/**
|
|
39
|
+
* The maxLen function returns the max length of the deque.
|
|
40
|
+
*
|
|
41
|
+
* @return The max length of the deque
|
|
42
|
+
*/
|
|
43
|
+
get maxLen(): number;
|
|
37
44
|
protected _bucketFirst: number;
|
|
38
45
|
/**
|
|
39
46
|
* The function returns the value of the protected variable `_bucketFirst`.
|
|
@@ -392,7 +399,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
392
399
|
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
393
400
|
* elements as the original deque (`this`) and the same bucket size.
|
|
394
401
|
*/
|
|
395
|
-
clone(): Deque<E>;
|
|
402
|
+
clone(): Deque<E, R>;
|
|
396
403
|
/**
|
|
397
404
|
* Time Complexity: O(n)
|
|
398
405
|
* Space Complexity: O(n)
|
|
@@ -413,25 +420,27 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
413
420
|
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
414
421
|
* satisfy the given predicate function.
|
|
415
422
|
*/
|
|
416
|
-
filter(predicate: ElementCallback<E, boolean
|
|
423
|
+
filter(predicate: ElementCallback<E, R, boolean, Deque<E, R>>, thisArg?: any): Deque<E, R>;
|
|
417
424
|
/**
|
|
418
425
|
* Time Complexity: O(n)
|
|
419
426
|
* Space Complexity: O(n)
|
|
420
427
|
*/
|
|
421
428
|
/**
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
* @param
|
|
428
|
-
* the
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
|
|
434
|
-
|
|
429
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
430
|
+
* returning a new deque with the results.
|
|
431
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
432
|
+
* deque. It takes three arguments: the current element, the index of the element, and the deque
|
|
433
|
+
* itself. It should return a value of type EM.
|
|
434
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
435
|
+
* transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
|
|
436
|
+
* provided, this function will be called for each raw element in the original deque.
|
|
437
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
438
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
439
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
440
|
+
* value of
|
|
441
|
+
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
442
|
+
*/
|
|
443
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM, Deque<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Deque<EM, RM>;
|
|
435
444
|
/**
|
|
436
445
|
* Time Complexity: O(n)
|
|
437
446
|
* Space Complexity: O(1)
|
|
@@ -12,7 +12,7 @@ const utils_1 = require("../../utils");
|
|
|
12
12
|
*/
|
|
13
13
|
class Deque extends base_1.IterableElementBase {
|
|
14
14
|
/**
|
|
15
|
-
* The constructor initializes a Deque object with
|
|
15
|
+
* The constructor initializes a Deque object with optional iterable of elements and options.
|
|
16
16
|
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
17
17
|
* elements to be added to the deque. It can also be an object with a `length` or `size` property
|
|
18
18
|
* that represents the number of elements in the iterable object. If no elements are provided, an
|
|
@@ -23,8 +23,9 @@ class Deque extends base_1.IterableElementBase {
|
|
|
23
23
|
* or is not a number
|
|
24
24
|
*/
|
|
25
25
|
constructor(elements = [], options) {
|
|
26
|
-
super();
|
|
26
|
+
super(options);
|
|
27
27
|
this._bucketSize = 1 << 12;
|
|
28
|
+
this._maxLen = -1;
|
|
28
29
|
this._bucketFirst = 0;
|
|
29
30
|
this._firstInBucket = 0;
|
|
30
31
|
this._bucketLast = 0;
|
|
@@ -33,9 +34,11 @@ class Deque extends base_1.IterableElementBase {
|
|
|
33
34
|
this._buckets = [];
|
|
34
35
|
this._size = 0;
|
|
35
36
|
if (options) {
|
|
36
|
-
const { bucketSize } = options;
|
|
37
|
+
const { bucketSize, maxLen } = options;
|
|
37
38
|
if (typeof bucketSize === 'number')
|
|
38
39
|
this._bucketSize = bucketSize;
|
|
40
|
+
if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0)
|
|
41
|
+
this._maxLen = maxLen;
|
|
39
42
|
}
|
|
40
43
|
let _size;
|
|
41
44
|
if ('length' in elements) {
|
|
@@ -57,8 +60,13 @@ class Deque extends base_1.IterableElementBase {
|
|
|
57
60
|
const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
|
|
58
61
|
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
59
62
|
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
60
|
-
for (const
|
|
61
|
-
this.
|
|
63
|
+
for (const el of elements) {
|
|
64
|
+
if (this.toElementFn) {
|
|
65
|
+
this.push(this.toElementFn(el));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.push(el);
|
|
69
|
+
}
|
|
62
70
|
}
|
|
63
71
|
}
|
|
64
72
|
/**
|
|
@@ -69,6 +77,14 @@ class Deque extends base_1.IterableElementBase {
|
|
|
69
77
|
get bucketSize() {
|
|
70
78
|
return this._bucketSize;
|
|
71
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* The maxLen function returns the max length of the deque.
|
|
82
|
+
*
|
|
83
|
+
* @return The max length of the deque
|
|
84
|
+
*/
|
|
85
|
+
get maxLen() {
|
|
86
|
+
return this._maxLen;
|
|
87
|
+
}
|
|
72
88
|
/**
|
|
73
89
|
* The function returns the value of the protected variable `_bucketFirst`.
|
|
74
90
|
* @returns The value of the `_bucketFirst` property.
|
|
@@ -170,6 +186,8 @@ class Deque extends base_1.IterableElementBase {
|
|
|
170
186
|
}
|
|
171
187
|
this._size += 1;
|
|
172
188
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
189
|
+
if (this._maxLen > 0 && this._size > this._maxLen)
|
|
190
|
+
this.shift();
|
|
173
191
|
return true;
|
|
174
192
|
}
|
|
175
193
|
/**
|
|
@@ -236,6 +254,8 @@ class Deque extends base_1.IterableElementBase {
|
|
|
236
254
|
}
|
|
237
255
|
this._size += 1;
|
|
238
256
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
257
|
+
if (this._maxLen > 0 && this._size > this._maxLen)
|
|
258
|
+
this.pop();
|
|
239
259
|
return true;
|
|
240
260
|
}
|
|
241
261
|
/**
|
|
@@ -708,7 +728,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
708
728
|
* elements as the original deque (`this`) and the same bucket size.
|
|
709
729
|
*/
|
|
710
730
|
clone() {
|
|
711
|
-
return new Deque(
|
|
731
|
+
return new Deque(this, { bucketSize: this.bucketSize, toElementFn: this.toElementFn });
|
|
712
732
|
}
|
|
713
733
|
/**
|
|
714
734
|
* Time Complexity: O(n)
|
|
@@ -731,7 +751,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
731
751
|
* satisfy the given predicate function.
|
|
732
752
|
*/
|
|
733
753
|
filter(predicate, thisArg) {
|
|
734
|
-
const newDeque = new Deque([], { bucketSize: this._bucketSize });
|
|
754
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn: this.toElementFn });
|
|
735
755
|
let index = 0;
|
|
736
756
|
for (const el of this) {
|
|
737
757
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -746,20 +766,22 @@ class Deque extends base_1.IterableElementBase {
|
|
|
746
766
|
* Space Complexity: O(n)
|
|
747
767
|
*/
|
|
748
768
|
/**
|
|
749
|
-
*
|
|
750
|
-
*
|
|
751
|
-
*
|
|
752
|
-
*
|
|
753
|
-
*
|
|
754
|
-
* @param
|
|
755
|
-
* the
|
|
756
|
-
*
|
|
757
|
-
*
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
769
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
770
|
+
* returning a new deque with the results.
|
|
771
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
772
|
+
* deque. It takes three arguments: the current element, the index of the element, and the deque
|
|
773
|
+
* itself. It should return a value of type EM.
|
|
774
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
775
|
+
* transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
|
|
776
|
+
* provided, this function will be called for each raw element in the original deque.
|
|
777
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
778
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
779
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
780
|
+
* value of
|
|
781
|
+
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
782
|
+
*/
|
|
783
|
+
map(callback, toElementFn, thisArg) {
|
|
784
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn });
|
|
763
785
|
let index = 0;
|
|
764
786
|
for (const el of this) {
|
|
765
787
|
newDeque.push(callback.call(thisArg, el, index, this));
|