priority-queue-typed 1.49.5 → 1.49.7
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/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +114 -91
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
|
@@ -24,11 +24,11 @@ exports.TreeMultimapNode = TreeMultimapNode;
|
|
|
24
24
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
25
25
|
*/
|
|
26
26
|
class TreeMultimap extends avl_tree_1.AVLTree {
|
|
27
|
-
constructor(
|
|
27
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
28
28
|
super([], options);
|
|
29
29
|
this._count = 0;
|
|
30
|
-
if (
|
|
31
|
-
this.addMany(
|
|
30
|
+
if (keysOrNodesOrEntries)
|
|
31
|
+
this.addMany(keysOrNodesOrEntries);
|
|
32
32
|
}
|
|
33
33
|
// TODO the _count is not accurate after nodes count modified
|
|
34
34
|
get count() {
|
|
@@ -52,26 +52,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
52
52
|
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
|
-
* The function
|
|
56
|
-
* @param
|
|
57
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
58
|
-
* class.
|
|
59
|
-
*/
|
|
60
|
-
isNode(exemplar) {
|
|
61
|
-
return exemplar instanceof TreeMultimapNode;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
-
* data type.
|
|
67
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
-
*/
|
|
69
|
-
isNotNodeInstance(potentialKey) {
|
|
70
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
74
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
55
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
56
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
75
57
|
* can be one of the following:
|
|
76
58
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
77
59
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -80,16 +62,16 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
80
62
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
81
63
|
* @returns a node of type `N` or `undefined`.
|
|
82
64
|
*/
|
|
83
|
-
exemplarToNode(
|
|
65
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
84
66
|
let node;
|
|
85
|
-
if (
|
|
67
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
86
68
|
return;
|
|
87
69
|
}
|
|
88
|
-
else if (this.isNode(
|
|
89
|
-
node =
|
|
70
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
71
|
+
node = keyOrNodeOrEntry;
|
|
90
72
|
}
|
|
91
|
-
else if (this.isEntry(
|
|
92
|
-
const [key, value] =
|
|
73
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
74
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
93
75
|
if (key === undefined || key === null) {
|
|
94
76
|
return;
|
|
95
77
|
}
|
|
@@ -97,8 +79,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
97
79
|
node = this.createNode(key, value, count);
|
|
98
80
|
}
|
|
99
81
|
}
|
|
100
|
-
else if (this.isNotNodeInstance(
|
|
101
|
-
node = this.createNode(
|
|
82
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
83
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
102
84
|
}
|
|
103
85
|
else {
|
|
104
86
|
return;
|
|
@@ -106,12 +88,31 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
106
88
|
return node;
|
|
107
89
|
}
|
|
108
90
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
91
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
92
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
93
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
94
|
+
* class.
|
|
111
95
|
*/
|
|
96
|
+
isNode(keyOrNodeOrEntry) {
|
|
97
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
98
|
+
}
|
|
112
99
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
100
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
101
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
102
|
+
* data type.
|
|
103
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
104
|
+
*/
|
|
105
|
+
isNotNodeInstance(potentialKey) {
|
|
106
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Time Complexity: O(log n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
111
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Time Complexity: O(log n)
|
|
115
|
+
* Space Complexity: O(1)
|
|
115
116
|
*
|
|
116
117
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
117
118
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -128,21 +129,22 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
128
129
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
129
130
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
130
131
|
if (newNode === undefined)
|
|
131
|
-
return;
|
|
132
|
+
return false;
|
|
132
133
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
133
134
|
const inserted = super.add(newNode);
|
|
134
135
|
if (inserted) {
|
|
135
136
|
this._count += orgNodeCount;
|
|
136
137
|
}
|
|
137
|
-
return
|
|
138
|
+
return true;
|
|
138
139
|
}
|
|
139
140
|
/**
|
|
140
|
-
* Time Complexity: O(k log n)
|
|
141
|
-
* Space Complexity: O(1)
|
|
141
|
+
* Time Complexity: O(k log n)
|
|
142
|
+
* Space Complexity: O(1)
|
|
143
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
142
144
|
*/
|
|
143
145
|
/**
|
|
144
|
-
* Time Complexity: O(k log n)
|
|
145
|
-
* Space Complexity: O(1)
|
|
146
|
+
* Time Complexity: O(k log n)
|
|
147
|
+
* Space Complexity: O(1)
|
|
146
148
|
*
|
|
147
149
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
148
150
|
* structure.
|
|
@@ -154,12 +156,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
154
156
|
return super.addMany(keysOrNodesOrEntries);
|
|
155
157
|
}
|
|
156
158
|
/**
|
|
157
|
-
* Time Complexity: O(
|
|
158
|
-
* Space Complexity: O(
|
|
159
|
+
* Time Complexity: O(n log n)
|
|
160
|
+
* Space Complexity: O(n)
|
|
161
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
159
162
|
*/
|
|
160
163
|
/**
|
|
161
|
-
* Time Complexity: O(n log n)
|
|
162
|
-
* Space Complexity: O(n)
|
|
164
|
+
* Time Complexity: O(n log n)
|
|
165
|
+
* Space Complexity: O(n)
|
|
163
166
|
*
|
|
164
167
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
165
168
|
* tree using either a recursive or iterative approach.
|
|
@@ -205,12 +208,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
205
208
|
}
|
|
206
209
|
}
|
|
207
210
|
/**
|
|
208
|
-
* Time Complexity: O(k log n)
|
|
209
|
-
* Space Complexity: O(1)
|
|
211
|
+
* Time Complexity: O(k log n)
|
|
212
|
+
* Space Complexity: O(1)
|
|
213
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
210
214
|
*/
|
|
211
215
|
/**
|
|
212
|
-
* Time Complexity: O(log n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
216
|
+
* Time Complexity: O(k log n)
|
|
217
|
+
* Space Complexity: O(1)
|
|
214
218
|
*
|
|
215
219
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
216
220
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -286,10 +290,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
286
290
|
return deletedResult;
|
|
287
291
|
}
|
|
288
292
|
/**
|
|
289
|
-
* Time Complexity: O(
|
|
290
|
-
* Space Complexity: O(
|
|
293
|
+
* Time Complexity: O(1)
|
|
294
|
+
* Space Complexity: O(1)
|
|
291
295
|
*/
|
|
292
296
|
/**
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
299
|
+
*
|
|
293
300
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
294
301
|
*/
|
|
295
302
|
clear() {
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
8
|
+
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem, LinkedHashMapOptions } from '../../types';
|
|
9
9
|
import { IterableEntryBase } from '../base';
|
|
10
10
|
/**
|
|
11
11
|
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
12
|
-
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete
|
|
12
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
13
13
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
14
|
-
* 4. Unordered Collection: HashMap does not guarantee the order of
|
|
14
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
15
15
|
*/
|
|
16
16
|
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
17
17
|
protected _store: {
|
|
@@ -19,16 +19,14 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
19
19
|
};
|
|
20
20
|
protected _objMap: Map<object, V>;
|
|
21
21
|
/**
|
|
22
|
-
* The constructor function initializes a new instance of a class with optional
|
|
23
|
-
* @param
|
|
22
|
+
* The constructor function initializes a new instance of a class with optional entries and options.
|
|
23
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
24
24
|
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
25
25
|
* key-value pairs.
|
|
26
26
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
27
27
|
* configuration options for the constructor. In this case, it has one property:
|
|
28
28
|
*/
|
|
29
|
-
constructor(
|
|
30
|
-
hashFn: (key: K) => string;
|
|
31
|
-
});
|
|
29
|
+
constructor(entries?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
32
30
|
protected _size: number;
|
|
33
31
|
get size(): number;
|
|
34
32
|
isEmpty(): boolean;
|
|
@@ -45,10 +43,10 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
45
43
|
set(key: K, value: V): boolean;
|
|
46
44
|
/**
|
|
47
45
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
48
|
-
* @param
|
|
49
|
-
* key-value pair is represented as an array with two
|
|
46
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
|
|
47
|
+
* key-value pair is represented as an array with two entries: the key and the value.
|
|
50
48
|
*/
|
|
51
|
-
setMany(
|
|
49
|
+
setMany(entries: Iterable<[K, V]>): boolean[];
|
|
52
50
|
/**
|
|
53
51
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
54
52
|
* a string map.
|
|
@@ -113,7 +111,6 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
113
111
|
* from the original `HashMap` that pass the provided `predicate` function.
|
|
114
112
|
*/
|
|
115
113
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
116
|
-
print(): void;
|
|
117
114
|
put(key: K, value: V): boolean;
|
|
118
115
|
/**
|
|
119
116
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
@@ -125,8 +122,8 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
125
122
|
protected _getNoObjKey(key: K): string;
|
|
126
123
|
}
|
|
127
124
|
/**
|
|
128
|
-
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which
|
|
129
|
-
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of
|
|
125
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map.
|
|
126
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
|
130
127
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
131
128
|
*/
|
|
132
129
|
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
@@ -135,9 +132,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
135
132
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
136
133
|
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
137
134
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
138
|
-
|
|
139
|
-
protected _objHashFn: (key: K) => object;
|
|
140
|
-
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
135
|
+
constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>);
|
|
141
136
|
protected _size: number;
|
|
142
137
|
get size(): number;
|
|
143
138
|
/**
|
|
@@ -242,14 +237,10 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
242
237
|
* Time Complexity: O(1)
|
|
243
238
|
* Space Complexity: O(1)
|
|
244
239
|
*
|
|
245
|
-
* The `clear` function clears all the
|
|
240
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
246
241
|
*/
|
|
247
242
|
clear(): void;
|
|
248
243
|
clone(): LinkedHashMap<K, V>;
|
|
249
|
-
/**
|
|
250
|
-
* Time Complexity: O(n)
|
|
251
|
-
* Space Complexity: O(n)
|
|
252
|
-
*/
|
|
253
244
|
/**
|
|
254
245
|
* Time Complexity: O(n)
|
|
255
246
|
* Space Complexity: O(n)
|
|
@@ -266,10 +257,6 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
266
257
|
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
267
258
|
*/
|
|
268
259
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
|
|
269
|
-
/**
|
|
270
|
-
* Time Complexity: O(n)
|
|
271
|
-
* Space Complexity: O(n)
|
|
272
|
-
*/
|
|
273
260
|
/**
|
|
274
261
|
* Time Complexity: O(n)
|
|
275
262
|
* Space Complexity: O(n)
|
|
@@ -288,9 +275,19 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
288
275
|
* function.
|
|
289
276
|
*/
|
|
290
277
|
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
278
|
+
/**
|
|
279
|
+
* Time Complexity: O(n)
|
|
280
|
+
* Space Complexity: O(n)
|
|
281
|
+
*/
|
|
291
282
|
put(key: K, value: V): boolean;
|
|
292
283
|
/**
|
|
293
|
-
* Time Complexity: O(n)
|
|
284
|
+
* Time Complexity: O(n)
|
|
285
|
+
* Space Complexity: O(n)
|
|
286
|
+
*/
|
|
287
|
+
protected _hashFn: (key: K) => string;
|
|
288
|
+
protected _objHashFn: (key: K) => object;
|
|
289
|
+
/**
|
|
290
|
+
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
|
|
294
291
|
* Space Complexity: O(1)
|
|
295
292
|
*
|
|
296
293
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -5,20 +5,20 @@ const base_1 = require("../base");
|
|
|
5
5
|
const utils_1 = require("../../utils");
|
|
6
6
|
/**
|
|
7
7
|
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
8
|
-
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete
|
|
8
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
9
9
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
10
|
-
* 4. Unordered Collection: HashMap does not guarantee the order of
|
|
10
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
11
11
|
*/
|
|
12
12
|
class HashMap extends base_1.IterableEntryBase {
|
|
13
13
|
/**
|
|
14
|
-
* The constructor function initializes a new instance of a class with optional
|
|
15
|
-
* @param
|
|
14
|
+
* The constructor function initializes a new instance of a class with optional entries and options.
|
|
15
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
16
16
|
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
17
17
|
* key-value pairs.
|
|
18
18
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
19
19
|
* configuration options for the constructor. In this case, it has one property:
|
|
20
20
|
*/
|
|
21
|
-
constructor(
|
|
21
|
+
constructor(entries = [], options) {
|
|
22
22
|
super();
|
|
23
23
|
this._store = {};
|
|
24
24
|
this._objMap = new Map();
|
|
@@ -30,8 +30,8 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
30
30
|
this._hashFn = hashFn;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
if (
|
|
34
|
-
this.setMany(
|
|
33
|
+
if (entries) {
|
|
34
|
+
this.setMany(entries);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
get size() {
|
|
@@ -72,12 +72,12 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
74
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
75
|
-
* @param
|
|
76
|
-
* key-value pair is represented as an array with two
|
|
75
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
|
|
76
|
+
* key-value pair is represented as an array with two entries: the key and the value.
|
|
77
77
|
*/
|
|
78
|
-
setMany(
|
|
78
|
+
setMany(entries) {
|
|
79
79
|
const results = [];
|
|
80
|
-
for (const [key, value] of
|
|
80
|
+
for (const [key, value] of entries)
|
|
81
81
|
results.push(this.set(key, value));
|
|
82
82
|
return results;
|
|
83
83
|
}
|
|
@@ -194,9 +194,6 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
194
194
|
}
|
|
195
195
|
return filteredMap;
|
|
196
196
|
}
|
|
197
|
-
print() {
|
|
198
|
-
console.log([...this.entries()]);
|
|
199
|
-
}
|
|
200
197
|
put(key, value) {
|
|
201
198
|
return this.set(key, value);
|
|
202
199
|
}
|
|
@@ -236,26 +233,33 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
236
233
|
}
|
|
237
234
|
exports.HashMap = HashMap;
|
|
238
235
|
/**
|
|
239
|
-
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which
|
|
240
|
-
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of
|
|
236
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map.
|
|
237
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
|
241
238
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
242
239
|
*/
|
|
243
240
|
class LinkedHashMap extends base_1.IterableEntryBase {
|
|
244
|
-
constructor(
|
|
245
|
-
hashFn: (key) => String(key),
|
|
246
|
-
objHashFn: (key) => key
|
|
247
|
-
}) {
|
|
241
|
+
constructor(entries, options) {
|
|
248
242
|
super();
|
|
249
243
|
this._noObjMap = {};
|
|
250
244
|
this._objMap = new WeakMap();
|
|
251
245
|
this._size = 0;
|
|
246
|
+
/**
|
|
247
|
+
* Time Complexity: O(n)
|
|
248
|
+
* Space Complexity: O(n)
|
|
249
|
+
*/
|
|
250
|
+
this._hashFn = (key) => String(key);
|
|
251
|
+
this._objHashFn = (key) => key;
|
|
252
252
|
this._sentinel = {};
|
|
253
253
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
254
|
+
if (options) {
|
|
255
|
+
const { hashFn, objHashFn } = options;
|
|
256
|
+
if (hashFn)
|
|
257
|
+
this._hashFn = hashFn;
|
|
258
|
+
if (objHashFn)
|
|
259
|
+
this._objHashFn = objHashFn;
|
|
260
|
+
}
|
|
261
|
+
if (entries) {
|
|
262
|
+
for (const el of entries) {
|
|
259
263
|
this.set(el[0], el[1]);
|
|
260
264
|
}
|
|
261
265
|
}
|
|
@@ -495,7 +499,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
495
499
|
* Time Complexity: O(1)
|
|
496
500
|
* Space Complexity: O(1)
|
|
497
501
|
*
|
|
498
|
-
* The `clear` function clears all the
|
|
502
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
499
503
|
*/
|
|
500
504
|
clear() {
|
|
501
505
|
this._noObjMap = {};
|
|
@@ -510,10 +514,6 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
510
514
|
}
|
|
511
515
|
return cloned;
|
|
512
516
|
}
|
|
513
|
-
/**
|
|
514
|
-
* Time Complexity: O(n)
|
|
515
|
-
* Space Complexity: O(n)
|
|
516
|
-
*/
|
|
517
517
|
/**
|
|
518
518
|
* Time Complexity: O(n)
|
|
519
519
|
* Space Complexity: O(n)
|
|
@@ -540,10 +540,6 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
540
540
|
}
|
|
541
541
|
return filteredMap;
|
|
542
542
|
}
|
|
543
|
-
/**
|
|
544
|
-
* Time Complexity: O(n)
|
|
545
|
-
* Space Complexity: O(n)
|
|
546
|
-
*/
|
|
547
543
|
/**
|
|
548
544
|
* Time Complexity: O(n)
|
|
549
545
|
* Space Complexity: O(n)
|
|
@@ -571,11 +567,15 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
571
567
|
}
|
|
572
568
|
return mappedMap;
|
|
573
569
|
}
|
|
570
|
+
/**
|
|
571
|
+
* Time Complexity: O(n)
|
|
572
|
+
* Space Complexity: O(n)
|
|
573
|
+
*/
|
|
574
574
|
put(key, value) {
|
|
575
575
|
return this.set(key, value);
|
|
576
576
|
}
|
|
577
577
|
/**
|
|
578
|
-
* Time Complexity: O(n), where n is the number of
|
|
578
|
+
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
|
|
579
579
|
* Space Complexity: O(1)
|
|
580
580
|
*
|
|
581
581
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -14,5 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./hash-table"), exports);
|
|
18
17
|
__exportStar(require("./hash-map"), exports);
|
|
@@ -19,8 +19,9 @@ import { IterableElementBase } from '../base';
|
|
|
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
21
|
export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
22
|
-
options: HeapOptions<E>;
|
|
23
22
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
23
|
+
protected _comparator: (a: E, b: E) => number;
|
|
24
|
+
get comparator(): (a: E, b: E) => number;
|
|
24
25
|
protected _elements: E[];
|
|
25
26
|
get elements(): E[];
|
|
26
27
|
/**
|
|
@@ -21,10 +21,9 @@ const base_1 = require("../base");
|
|
|
21
21
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
22
22
|
*/
|
|
23
23
|
class Heap extends base_1.IterableElementBase {
|
|
24
|
-
constructor(elements, options) {
|
|
24
|
+
constructor(elements = [], options) {
|
|
25
25
|
super();
|
|
26
|
-
this.
|
|
27
|
-
const defaultComparator = (a, b) => {
|
|
26
|
+
this._comparator = (a, b) => {
|
|
28
27
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
29
28
|
throw new Error('The a, b params of compare function must be number');
|
|
30
29
|
}
|
|
@@ -32,13 +31,11 @@ class Heap extends base_1.IterableElementBase {
|
|
|
32
31
|
return a - b;
|
|
33
32
|
}
|
|
34
33
|
};
|
|
34
|
+
this._elements = [];
|
|
35
35
|
if (options) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.options = {
|
|
40
|
-
comparator: defaultComparator
|
|
41
|
-
};
|
|
36
|
+
const { comparator } = options;
|
|
37
|
+
if (comparator)
|
|
38
|
+
this._comparator = comparator;
|
|
42
39
|
}
|
|
43
40
|
if (elements) {
|
|
44
41
|
for (const el of elements) {
|
|
@@ -47,6 +44,9 @@ class Heap extends base_1.IterableElementBase {
|
|
|
47
44
|
// this.fix();
|
|
48
45
|
}
|
|
49
46
|
}
|
|
47
|
+
get comparator() {
|
|
48
|
+
return this._comparator;
|
|
49
|
+
}
|
|
50
50
|
get elements() {
|
|
51
51
|
return this._elements;
|
|
52
52
|
}
|
|
@@ -256,7 +256,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
256
256
|
* @returns A new Heap instance containing the same elements.
|
|
257
257
|
*/
|
|
258
258
|
clone() {
|
|
259
|
-
const clonedHeap = new Heap([], this.
|
|
259
|
+
const clonedHeap = new Heap([], { comparator: this.comparator });
|
|
260
260
|
clonedHeap._elements = [...this.elements];
|
|
261
261
|
return clonedHeap;
|
|
262
262
|
}
|
|
@@ -382,7 +382,7 @@ class Heap extends base_1.IterableElementBase {
|
|
|
382
382
|
while (index > 0) {
|
|
383
383
|
const parent = (index - 1) >> 1;
|
|
384
384
|
const parentItem = this.elements[parent];
|
|
385
|
-
if (this.
|
|
385
|
+
if (this.comparator(parentItem, element) <= 0)
|
|
386
386
|
break;
|
|
387
387
|
this.elements[index] = parentItem;
|
|
388
388
|
index = parent;
|
|
@@ -404,11 +404,11 @@ class Heap extends base_1.IterableElementBase {
|
|
|
404
404
|
let left = (index << 1) | 1;
|
|
405
405
|
const right = left + 1;
|
|
406
406
|
let minItem = this.elements[left];
|
|
407
|
-
if (right < this.elements.length && this.
|
|
407
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
408
408
|
left = right;
|
|
409
409
|
minItem = this.elements[right];
|
|
410
410
|
}
|
|
411
|
-
if (this.
|
|
411
|
+
if (this.comparator(minItem, element) >= 0)
|
|
412
412
|
break;
|
|
413
413
|
this.elements[index] = minItem;
|
|
414
414
|
index = left;
|
|
@@ -13,7 +13,7 @@ const heap_1 = require("./heap");
|
|
|
13
13
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
14
14
|
*/
|
|
15
15
|
class MaxHeap extends heap_1.Heap {
|
|
16
|
-
constructor(elements, options = {
|
|
16
|
+
constructor(elements = [], options = {
|
|
17
17
|
comparator: (a, b) => {
|
|
18
18
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
19
19
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -13,7 +13,7 @@ const heap_1 = require("./heap");
|
|
|
13
13
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
14
14
|
*/
|
|
15
15
|
class MinHeap extends heap_1.Heap {
|
|
16
|
-
constructor(elements, options = {
|
|
16
|
+
constructor(elements = [], options = {
|
|
17
17
|
comparator: (a, b) => {
|
|
18
18
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
19
19
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -25,7 +25,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
25
25
|
/**
|
|
26
26
|
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
27
27
|
*/
|
|
28
|
-
constructor(elements) {
|
|
28
|
+
constructor(elements = []) {
|
|
29
29
|
super();
|
|
30
30
|
this._head = undefined;
|
|
31
31
|
this._tail = undefined;
|
|
@@ -18,10 +18,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
18
18
|
/**
|
|
19
19
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
20
20
|
*/
|
|
21
|
-
constructor(elements) {
|
|
21
|
+
constructor(elements = []) {
|
|
22
22
|
super();
|
|
23
|
-
this._head = undefined;
|
|
24
|
-
this._tail = undefined;
|
|
25
23
|
this._size = 0;
|
|
26
24
|
if (elements) {
|
|
27
25
|
for (const el of elements)
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { SkipLinkedListOptions } from '../../types';
|
|
8
9
|
export declare class SkipListNode<K, V> {
|
|
9
10
|
key: K;
|
|
10
11
|
value: V;
|
|
@@ -12,14 +13,7 @@ export declare class SkipListNode<K, V> {
|
|
|
12
13
|
constructor(key: K, value: V, level: number);
|
|
13
14
|
}
|
|
14
15
|
export declare class SkipList<K, V> {
|
|
15
|
-
|
|
16
|
-
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
17
|
-
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
18
|
-
* the maximum number of levels that can be created in the skip list.
|
|
19
|
-
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
20
|
-
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
21
|
-
*/
|
|
22
|
-
constructor(maxLevel?: number, probability?: number);
|
|
16
|
+
constructor(elements?: Iterable<[K, V]>, options?: SkipLinkedListOptions);
|
|
23
17
|
protected _head: SkipListNode<K, V>;
|
|
24
18
|
get head(): SkipListNode<K, V>;
|
|
25
19
|
protected _level: number;
|