heap-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,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.
|
|
@@ -392,7 +392,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
392
392
|
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
393
393
|
* elements as the original deque (`this`) and the same bucket size.
|
|
394
394
|
*/
|
|
395
|
-
clone(): Deque<E>;
|
|
395
|
+
clone(): Deque<E, R>;
|
|
396
396
|
/**
|
|
397
397
|
* Time Complexity: O(n)
|
|
398
398
|
* Space Complexity: O(n)
|
|
@@ -413,25 +413,27 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
413
413
|
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
414
414
|
* satisfy the given predicate function.
|
|
415
415
|
*/
|
|
416
|
-
filter(predicate: ElementCallback<E, boolean
|
|
416
|
+
filter(predicate: ElementCallback<E, R, boolean, Deque<E, R>>, thisArg?: any): Deque<E, R>;
|
|
417
417
|
/**
|
|
418
418
|
* Time Complexity: O(n)
|
|
419
419
|
* Space Complexity: O(n)
|
|
420
420
|
*/
|
|
421
421
|
/**
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
* @param
|
|
428
|
-
* the
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
|
|
434
|
-
|
|
422
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
423
|
+
* returning a new deque with the results.
|
|
424
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
425
|
+
* deque. It takes three arguments: the current element, the index of the element, and the deque
|
|
426
|
+
* itself. It should return a value of type EM.
|
|
427
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
428
|
+
* transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
|
|
429
|
+
* provided, this function will be called for each raw element in the original deque.
|
|
430
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
431
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
432
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
433
|
+
* value of
|
|
434
|
+
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
435
|
+
*/
|
|
436
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM, Deque<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Deque<EM, RM>;
|
|
435
437
|
/**
|
|
436
438
|
* Time Complexity: O(n)
|
|
437
439
|
* 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,7 +23,7 @@ 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
28
|
this._bucketFirst = 0;
|
|
29
29
|
this._firstInBucket = 0;
|
|
@@ -57,8 +57,13 @@ class Deque extends base_1.IterableElementBase {
|
|
|
57
57
|
const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
|
|
58
58
|
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
59
59
|
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
60
|
-
for (const
|
|
61
|
-
this.
|
|
60
|
+
for (const el of elements) {
|
|
61
|
+
if (this.toElementFn) {
|
|
62
|
+
this.push(this.toElementFn(el));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this.push(el);
|
|
66
|
+
}
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
/**
|
|
@@ -708,7 +713,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
708
713
|
* elements as the original deque (`this`) and the same bucket size.
|
|
709
714
|
*/
|
|
710
715
|
clone() {
|
|
711
|
-
return new Deque(
|
|
716
|
+
return new Deque(this, { bucketSize: this.bucketSize, toElementFn: this.toElementFn });
|
|
712
717
|
}
|
|
713
718
|
/**
|
|
714
719
|
* Time Complexity: O(n)
|
|
@@ -731,7 +736,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
731
736
|
* satisfy the given predicate function.
|
|
732
737
|
*/
|
|
733
738
|
filter(predicate, thisArg) {
|
|
734
|
-
const newDeque = new Deque([], { bucketSize: this._bucketSize });
|
|
739
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn: this.toElementFn });
|
|
735
740
|
let index = 0;
|
|
736
741
|
for (const el of this) {
|
|
737
742
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -746,20 +751,22 @@ class Deque extends base_1.IterableElementBase {
|
|
|
746
751
|
* Space Complexity: O(n)
|
|
747
752
|
*/
|
|
748
753
|
/**
|
|
749
|
-
*
|
|
750
|
-
*
|
|
751
|
-
*
|
|
752
|
-
*
|
|
753
|
-
*
|
|
754
|
-
* @param
|
|
755
|
-
* the
|
|
756
|
-
*
|
|
757
|
-
*
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
754
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
755
|
+
* returning a new deque with the results.
|
|
756
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
757
|
+
* deque. It takes three arguments: the current element, the index of the element, and the deque
|
|
758
|
+
* itself. It should return a value of type EM.
|
|
759
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
760
|
+
* transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
|
|
761
|
+
* provided, this function will be called for each raw element in the original deque.
|
|
762
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
763
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
764
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
765
|
+
* value of
|
|
766
|
+
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
767
|
+
*/
|
|
768
|
+
map(callback, toElementFn, thisArg) {
|
|
769
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn });
|
|
763
770
|
let index = 0;
|
|
764
771
|
for (const el of this) {
|
|
765
772
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
|
-
import type { ElementCallback } from '../../types';
|
|
6
|
+
import type { ElementCallback, QueueOptions } from '../../types';
|
|
7
7
|
import { IterableElementBase } from '../base';
|
|
8
8
|
import { SinglyLinkedList } from '../linked-list';
|
|
9
9
|
/**
|
|
@@ -15,14 +15,8 @@ import { SinglyLinkedList } from '../linked-list';
|
|
|
15
15
|
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
16
16
|
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
17
17
|
*/
|
|
18
|
-
export declare class Queue<E = any> extends IterableElementBase<E
|
|
19
|
-
|
|
20
|
-
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
21
|
-
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
22
|
-
* will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be
|
|
23
|
-
* initialized as an empty array.
|
|
24
|
-
*/
|
|
25
|
-
constructor(elements?: Iterable<E>);
|
|
18
|
+
export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E, R>> {
|
|
19
|
+
constructor(elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>);
|
|
26
20
|
protected _elements: E[];
|
|
27
21
|
/**
|
|
28
22
|
* The elements function returns the elements of this set.
|
|
@@ -178,7 +172,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
178
172
|
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
179
173
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
180
174
|
*/
|
|
181
|
-
clone(): Queue<E>;
|
|
175
|
+
clone(): Queue<E, R>;
|
|
182
176
|
/**
|
|
183
177
|
* Time Complexity: O(n)
|
|
184
178
|
* Space Complexity: O(n)
|
|
@@ -199,26 +193,12 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
199
193
|
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
200
194
|
* satisfy the given predicate function.
|
|
201
195
|
*/
|
|
202
|
-
filter(predicate: ElementCallback<E, boolean
|
|
203
|
-
/**
|
|
204
|
-
* Time Complexity: O(n)
|
|
205
|
-
* Space Complexity: O(n)
|
|
206
|
-
*/
|
|
196
|
+
filter(predicate: ElementCallback<E, R, boolean, Queue<E, R>>, thisArg?: any): Queue<E, R>;
|
|
207
197
|
/**
|
|
208
198
|
* Time Complexity: O(n)
|
|
209
199
|
* Space Complexity: O(n)
|
|
210
|
-
*
|
|
211
|
-
* The `map` function takes a callback function and applies it to each element in the queue,
|
|
212
|
-
* returning a new queue with the results.
|
|
213
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
214
|
-
* queue. It takes three arguments: the current element, the index of the current element, and the
|
|
215
|
-
* queue itself. The callback function should return a new value that will be added to the new queue.
|
|
216
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
217
|
-
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
218
|
-
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
219
|
-
* @returns The `map` function is returning a new `Queue` object with the transformed elements.
|
|
220
200
|
*/
|
|
221
|
-
map<
|
|
201
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM, Queue<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
|
|
222
202
|
/**
|
|
223
203
|
* Time Complexity: O(n)
|
|
224
204
|
* Space Complexity: O(n)
|
|
@@ -237,7 +217,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
237
217
|
* 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
|
|
238
218
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
239
219
|
*/
|
|
240
|
-
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
220
|
+
export declare class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
|
|
241
221
|
/**
|
|
242
222
|
* Time Complexity: O(n)
|
|
243
223
|
* Space Complexity: O(n)
|
|
@@ -250,5 +230,5 @@ export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
250
230
|
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
|
251
231
|
* values as the original `LinkedListQueue`.
|
|
252
232
|
*/
|
|
253
|
-
clone(): LinkedListQueue<E>;
|
|
233
|
+
clone(): LinkedListQueue<E, R>;
|
|
254
234
|
}
|