linked-list-typed 1.44.1 → 1.45.1
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/hash/hash-map.d.ts +230 -37
- package/dist/data-structures/hash/hash-map.js +427 -115
- package/dist/types/data-structures/hash/hash-map.d.ts +15 -1
- package/dist/types/data-structures/hash/index.d.ts +6 -0
- package/dist/types/data-structures/hash/index.js +20 -0
- package/dist/utils/utils.d.ts +3 -0
- package/dist/utils/utils.js +15 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +7 -7
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
- package/src/data-structures/binary-tree/binary-tree.ts +39 -31
- package/src/data-structures/binary-tree/bst.ts +12 -8
- package/src/data-structures/binary-tree/rb-tree.ts +17 -6
- package/src/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
- package/src/data-structures/graph/abstract-graph.ts +46 -31
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +9 -9
- package/src/data-structures/hash/hash-map.ts +430 -123
- package/src/data-structures/hash/hash-table.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +8 -5
- package/src/data-structures/heap/max-heap.ts +3 -3
- package/src/data-structures/heap/min-heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +3 -3
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/data-structures/tree/tree.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +17 -1
- package/src/types/data-structures/hash/index.ts +7 -0
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +18 -4
- package/src/utils/utils.ts +16 -3
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { HashFunction } from '../../types';
|
|
2
1
|
/**
|
|
3
2
|
* data-structure-typed
|
|
4
3
|
*
|
|
@@ -6,45 +5,239 @@ import { HashFunction } from '../../types';
|
|
|
6
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
6
|
* @license MIT License
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
import { HashMapLinkedNode, HashMapOptions, IterateDirection } from '../../types';
|
|
9
|
+
/**
|
|
10
|
+
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
|
|
11
|
+
* these underlying structures have already dealt with dynamic expansion and hash collisions.
|
|
12
|
+
* Therefore, there is no need for additional logic to handle these issues.
|
|
13
|
+
*/
|
|
14
|
+
export declare class HashMapIterator<K, V> {
|
|
15
|
+
readonly hashMap: HashMap<K, V>;
|
|
16
|
+
readonly iterateDirection: IterateDirection;
|
|
17
|
+
protected _node: HashMapLinkedNode<K, V>;
|
|
18
|
+
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
19
|
+
/**
|
|
20
|
+
* This is a constructor function for a linked list iterator in a HashMap data structure.
|
|
21
|
+
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object
|
|
22
|
+
* represents a node in a linked list used in a hash map data structure. It contains a key-value pair
|
|
23
|
+
* and references to the previous and next nodes in the linked list.
|
|
24
|
+
* @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It
|
|
25
|
+
* is used to mark the beginning or end of the list and is typically used in data structures like
|
|
26
|
+
* hash maps or linked lists to simplify operations and boundary checks.
|
|
27
|
+
* @param hashMap - A HashMap object that stores key-value pairs.
|
|
28
|
+
* @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional
|
|
29
|
+
* parameter that specifies the direction in which the iterator should iterate over the elements of
|
|
30
|
+
* the HashMap. It can take one of the following values:
|
|
31
|
+
* @returns The constructor does not return anything. It is used to initialize the properties and
|
|
32
|
+
* methods of the object being created.
|
|
33
|
+
*/
|
|
34
|
+
constructor(node: HashMapLinkedNode<K, V>, sentinel: HashMapLinkedNode<K, V>, hashMap: HashMap<K, V>, iterateDirection?: IterateDirection);
|
|
35
|
+
/**
|
|
36
|
+
* The above function returns a Proxy object that allows access to the key and value of a node in a
|
|
37
|
+
* data structure.
|
|
38
|
+
* @returns The code is returning a Proxy object.
|
|
39
|
+
*/
|
|
40
|
+
get current(): [K, V];
|
|
41
|
+
/**
|
|
42
|
+
* The function checks if a node is accessible.
|
|
43
|
+
* @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`.
|
|
44
|
+
*/
|
|
45
|
+
isAccessible(): boolean;
|
|
46
|
+
prev(): this;
|
|
47
|
+
next(): this;
|
|
48
|
+
}
|
|
49
|
+
export declare class HashMap<K = any, V = any> {
|
|
50
|
+
readonly OBJ_KEY_INDEX: symbol;
|
|
51
|
+
protected _nodes: HashMapLinkedNode<K, V>[];
|
|
52
|
+
protected _orgMap: Record<string, HashMapLinkedNode<K, V>>;
|
|
53
|
+
protected _head: HashMapLinkedNode<K, V>;
|
|
54
|
+
protected _tail: HashMapLinkedNode<K, V>;
|
|
55
|
+
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
56
|
+
/**
|
|
57
|
+
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
58
|
+
* @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
|
|
59
|
+
* V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
|
|
60
|
+
* `K` represents the type of the key and `V` represents the
|
|
61
|
+
*/
|
|
62
|
+
constructor(hashMap?: HashMapOptions<[K, V]>);
|
|
29
63
|
protected _size: number;
|
|
30
64
|
get size(): number;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(1)
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
69
|
+
* The function returns a new iterator object for a HashMap.
|
|
70
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
71
|
+
*/
|
|
72
|
+
get begin(): HashMapIterator<K, V>;
|
|
73
|
+
/**
|
|
74
|
+
* Time Complexity: O(1)
|
|
75
|
+
* Space Complexity: O(1)
|
|
76
|
+
*
|
|
77
|
+
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
78
|
+
* end values.
|
|
79
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
80
|
+
*/
|
|
81
|
+
get end(): HashMapIterator<K, V>;
|
|
82
|
+
/**
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
85
|
+
*
|
|
86
|
+
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
87
|
+
* a HashMap in reverse order.
|
|
88
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
89
|
+
*/
|
|
90
|
+
get reverseBegin(): HashMapIterator<K, V>;
|
|
91
|
+
/**
|
|
92
|
+
* Time Complexity: O(1)
|
|
93
|
+
* Space Complexity: O(1)
|
|
94
|
+
*
|
|
95
|
+
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
96
|
+
* HashMap in reverse order.
|
|
97
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
98
|
+
*/
|
|
99
|
+
get reverseEnd(): HashMapIterator<K, V>;
|
|
100
|
+
/**
|
|
101
|
+
* Time Complexity: O(1)
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*
|
|
104
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
105
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
106
|
+
* value (V).
|
|
107
|
+
*/
|
|
108
|
+
get front(): [K, V] | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* Time Complexity: O(1)
|
|
111
|
+
* Space Complexity: O(1)
|
|
112
|
+
*
|
|
113
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
114
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
115
|
+
* data structure.
|
|
116
|
+
*/
|
|
117
|
+
get back(): [K, V] | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*
|
|
122
|
+
* The `set` function adds a new key-value pair to a data structure, either using an object key or a
|
|
123
|
+
* string key.
|
|
124
|
+
* @param {K} key - The `key` parameter is the key to be set in the data structure. It can be of any
|
|
125
|
+
* type, but typically it is a string or symbol.
|
|
126
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
127
|
+
* value associated with the key being set in the data structure.
|
|
128
|
+
* @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
|
|
129
|
+
* @returns the size of the data structure after the key-value pair has been set.
|
|
130
|
+
*/
|
|
131
|
+
set(key: K, value?: V, isObjectKey?: boolean): number;
|
|
132
|
+
/**
|
|
133
|
+
* Time Complexity: O(1)
|
|
134
|
+
* Space Complexity: O(1)
|
|
135
|
+
*
|
|
136
|
+
* The function `get` retrieves the value associated with a given key from a map, either by using the
|
|
137
|
+
* key directly or by using an index stored in the key object.
|
|
138
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
139
|
+
* of any type, but typically it is a string or symbol.
|
|
140
|
+
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
141
|
+
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
|
|
142
|
+
* `key` is an object key. If `isObjectKey` is `false`, it means that `key`
|
|
143
|
+
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
144
|
+
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
145
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_orgMap` object
|
|
146
|
+
* using the key itself. If the key is not found, `undefined` is
|
|
147
|
+
*/
|
|
148
|
+
get(key: K, isObjectKey?: boolean): V | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* Time Complexity: O(n), where n is the index.
|
|
151
|
+
* Space Complexity: O(1)
|
|
152
|
+
*
|
|
153
|
+
* The function `getAt` retrieves the key-value pair at a specified index in a linked list.
|
|
154
|
+
* @param {number} index - The index parameter is a number that represents the position of the
|
|
155
|
+
* element we want to retrieve from the data structure.
|
|
156
|
+
* @returns The method `getAt(index: number)` is returning an array containing the key-value pair at
|
|
157
|
+
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
158
|
+
* where `K` is the key and `V` is the value.
|
|
159
|
+
*/
|
|
160
|
+
getAt(index: number): [K, V];
|
|
161
|
+
/**
|
|
162
|
+
* Time Complexity: O(1)
|
|
163
|
+
* Space Complexity: O(1)
|
|
164
|
+
*
|
|
165
|
+
* The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key
|
|
166
|
+
* and whether it is an object key or not.
|
|
167
|
+
* @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It
|
|
168
|
+
* can be of any type, depending on how the HashMap is implemented.
|
|
169
|
+
* @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that
|
|
170
|
+
* indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that
|
|
171
|
+
* the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false`
|
|
172
|
+
* @returns a new instance of the `HashMapIterator` class.
|
|
173
|
+
*/
|
|
174
|
+
getIterator(key: K, isObjectKey?: boolean): HashMapIterator<K, V>;
|
|
175
|
+
/**
|
|
176
|
+
* Time Complexity: O(1)
|
|
177
|
+
* Space Complexity: O(1)
|
|
178
|
+
*
|
|
179
|
+
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
180
|
+
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
181
|
+
* It can be of any type, but typically it is a string or an object.
|
|
182
|
+
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
183
|
+
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
|
|
184
|
+
* `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
|
|
185
|
+
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
186
|
+
* was not found.
|
|
187
|
+
*/
|
|
188
|
+
delete(key: K, isObjectKey?: boolean): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Time Complexity: O(n), where n is the index.
|
|
191
|
+
* Space Complexity: O(1)
|
|
192
|
+
*
|
|
193
|
+
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
194
|
+
* @param {number} index - The index parameter represents the position at which the node should be
|
|
195
|
+
* deleted in the linked list.
|
|
196
|
+
* @returns The size of the list after deleting the element at the specified index.
|
|
197
|
+
*/
|
|
198
|
+
deleteAt(index: number): number;
|
|
199
|
+
/**
|
|
200
|
+
* Time Complexity: O(1)
|
|
201
|
+
* Space Complexity: O(1)
|
|
202
|
+
*
|
|
203
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
204
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
205
|
+
* not.
|
|
206
|
+
*/
|
|
41
207
|
isEmpty(): boolean;
|
|
42
|
-
protected _hash(key: K): number;
|
|
43
208
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
209
|
+
* Time Complexity: O(1)
|
|
210
|
+
* Space Complexity: O(1)
|
|
211
|
+
*
|
|
212
|
+
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
213
|
+
*/
|
|
214
|
+
clear(): void;
|
|
215
|
+
/**
|
|
216
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
217
|
+
* Space Complexity: O(1)
|
|
218
|
+
*
|
|
219
|
+
* The `forEach` function iterates over each element in a HashMap and executes a callback function on
|
|
220
|
+
* each element.
|
|
221
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
222
|
+
* HashMap. It takes three arguments:
|
|
223
|
+
*/
|
|
224
|
+
forEach(callback: (element: [K, V], index: number, hashMap: HashMap<K, V>) => void): void;
|
|
225
|
+
/**
|
|
226
|
+
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
227
|
+
* Space Complexity: O(1)
|
|
228
|
+
*
|
|
229
|
+
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
230
|
+
*/
|
|
231
|
+
[Symbol.iterator](): Generator<[K, V], void, unknown>;
|
|
232
|
+
/**
|
|
233
|
+
* Time Complexity: O(1)
|
|
234
|
+
* Space Complexity: O(1)
|
|
235
|
+
*
|
|
236
|
+
* The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail
|
|
237
|
+
* pointers if necessary.
|
|
238
|
+
* @param node - The `node` parameter is an instance of the `HashMapLinkedNode` class, which
|
|
239
|
+
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
240
|
+
* and next nodes in the list.
|
|
48
241
|
*/
|
|
49
|
-
protected
|
|
242
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V>): void;
|
|
50
243
|
}
|