linked-list-typed 1.44.1 → 1.45.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.
@@ -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
- export declare class HashMap<K, V> {
10
- /**
11
- * The constructor initializes the properties of a hash table, including the initial capacity, load factor, capacity
12
- * multiplier, size, table array, and hash function.
13
- * @param [initialCapacity=16] - The initial capacity is the initial size of the hash table. It determines the number of
14
- * buckets or slots available for storing key-value pairs. The default value is 16.
15
- * @param [loadFactor=0.75] - The load factor is a measure of how full the hash table can be before it is resized. It is
16
- * a value between 0 and 1, where 1 means the hash table is completely full and 0 means it is completely empty. When the
17
- * load factor is reached, the hash table will
18
- * @param [hashFn] - The `hashFn` parameter is an optional parameter that represents the hash function used to calculate
19
- * the index of a key in the hash table. If a custom hash function is not provided, a default hash function is used. The
20
- * default hash function converts the key to a string, calculates the sum of the
21
- */
22
- constructor(initialCapacity?: number, loadFactor?: number, hashFn?: HashFunction<K>);
23
- protected _initialCapacity: number;
24
- get initialCapacity(): number;
25
- protected _loadFactor: number;
26
- get loadFactor(): number;
27
- protected _capacityMultiplier: number;
28
- get capacityMultiplier(): number;
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
+ protected _node: HashMapLinkedNode<K, V>;
17
+ readonly iterateDirection: IterateDirection;
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
+ protected _nodes: HashMapLinkedNode<K, V>[];
51
+ protected _orgMap: Record<string, HashMapLinkedNode<K, V>>;
52
+ protected _head: HashMapLinkedNode<K, V>;
53
+ protected _tail: HashMapLinkedNode<K, V>;
54
+ protected readonly _sentinel: HashMapLinkedNode<K, V>;
55
+ readonly OBJ_KEY_INDEX: symbol;
29
56
  protected _size: number;
30
57
  get size(): number;
31
- protected _table: Array<Array<[K, V]>>;
32
- get table(): Array<Array<[K, V]>>;
33
- protected _hashFn: HashFunction<K>;
34
- get hashFn(): HashFunction<K>;
35
- set(key: K, value: V): void;
36
- get(key: K): V | undefined;
37
- delete(key: K): void;
38
- entries(): IterableIterator<[K, V]>;
39
- [Symbol.iterator](): IterableIterator<[K, V]>;
40
- clear(): void;
58
+ /**
59
+ * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
60
+ * @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
61
+ * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
62
+ * `K` represents the type of the key and `V` represents the
63
+ */
64
+ constructor(hashMap?: HashMapOptions<[K, V]>);
65
+ /**
66
+ * Time Complexity: O(1)
67
+ * Space Complexity: O(1)
68
+ *
69
+ * The `set` function adds a new key-value pair to a data structure, either using an object key or a
70
+ * string key.
71
+ * @param {K} key - The `key` parameter is the key to be set in the data structure. It can be of any
72
+ * type, but typically it is a string or symbol.
73
+ * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
74
+ * value associated with the key being set in the data structure.
75
+ * @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
76
+ * @returns the size of the data structure after the key-value pair has been set.
77
+ */
78
+ set(key: K, value?: V, isObjectKey?: boolean): number;
79
+ /**
80
+ * Time Complexity: O(1)
81
+ * Space Complexity: O(1)
82
+ *
83
+ * The function `get` retrieves the value associated with a given key from a map, either by using the
84
+ * key directly or by using an index stored in the key object.
85
+ * @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
86
+ * of any type, but typically it is a string or symbol.
87
+ * @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
88
+ * whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
89
+ * `key` is an object key. If `isObjectKey` is `false`, it means that `key`
90
+ * @returns The value associated with the given key is being returned. If the key is an object key,
91
+ * the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
92
+ * property of the key. If the key is a string key, the value is retrieved from the `_orgMap` object
93
+ * using the key itself. If the key is not found, `undefined` is
94
+ */
95
+ get(key: K, isObjectKey?: boolean): V | undefined;
96
+ /**
97
+ * Time Complexity: O(n), where n is the index.
98
+ * Space Complexity: O(1)
99
+ *
100
+ * The function `getAt` retrieves the key-value pair at a specified index in a linked list.
101
+ * @param {number} index - The index parameter is a number that represents the position of the
102
+ * element we want to retrieve from the data structure.
103
+ * @returns The method `getAt(index: number)` is returning an array containing the key-value pair at
104
+ * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
105
+ * where `K` is the key and `V` is the value.
106
+ */
107
+ getAt(index: number): [K, V];
108
+ /**
109
+ * Time Complexity: O(1)
110
+ * Space Complexity: O(1)
111
+ *
112
+ * The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key
113
+ * and whether it is an object key or not.
114
+ * @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It
115
+ * can be of any type, depending on how the HashMap is implemented.
116
+ * @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that
117
+ * indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that
118
+ * the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false`
119
+ * @returns a new instance of the `HashMapIterator` class.
120
+ */
121
+ getIterator(key: K, isObjectKey?: boolean): HashMapIterator<K, V>;
122
+ /**
123
+ * Time Complexity: O(1)
124
+ * Space Complexity: O(1)
125
+ *
126
+ * The `delete` function removes a key-value pair from a map-like data structure.
127
+ * @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
128
+ * It can be of any type, but typically it is a string or an object.
129
+ * @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
130
+ * whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
131
+ * `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
132
+ * @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
133
+ * was not found.
134
+ */
135
+ delete(key: K, isObjectKey?: boolean): boolean;
136
+ /**
137
+ * Time Complexity: O(n), where n is the index.
138
+ * Space Complexity: O(1)
139
+ *
140
+ * The `deleteAt` function deletes a node at a specified index in a linked list.
141
+ * @param {number} index - The index parameter represents the position at which the node should be
142
+ * deleted in the linked list.
143
+ * @returns The size of the list after deleting the element at the specified index.
144
+ */
145
+ deleteAt(index: number): number;
146
+ /**
147
+ * Time Complexity: O(1)
148
+ * Space Complexity: O(1)
149
+ *
150
+ * The function checks if a data structure is empty by comparing its size to zero.
151
+ * @returns The method is returning a boolean value indicating whether the size of the object is 0 or
152
+ * not.
153
+ */
41
154
  isEmpty(): boolean;
42
- protected _hash(key: K): number;
43
155
  /**
44
- * The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
45
- * rehashing the key-value pairs from the old table into the new table.
46
- * @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
47
- * the number of buckets that the new table should have.
156
+ * Time Complexity: O(1)
157
+ * Space Complexity: O(1)
158
+ *
159
+ * The `clear` function clears all the elements in a data structure and resets its properties.
160
+ */
161
+ clear(): void;
162
+ /**
163
+ * Time Complexity: O(1)
164
+ * Space Complexity: O(1)
165
+ *
166
+ * The function returns a new iterator object for a HashMap.
167
+ * @returns A new instance of the HashMapIterator class is being returned.
168
+ */
169
+ get begin(): HashMapIterator<K, V>;
170
+ /**
171
+ * Time Complexity: O(1)
172
+ * Space Complexity: O(1)
173
+ *
174
+ * The function returns a new HashMapIterator object with the _sentinel value as both the start and
175
+ * end values.
176
+ * @returns A new instance of the HashMapIterator class is being returned.
177
+ */
178
+ get end(): HashMapIterator<K, V>;
179
+ /**
180
+ * Time Complexity: O(1)
181
+ * Space Complexity: O(1)
182
+ *
183
+ * The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
184
+ * a HashMap in reverse order.
185
+ * @returns A new instance of the HashMapIterator class is being returned.
186
+ */
187
+ get reverseBegin(): HashMapIterator<K, V>;
188
+ /**
189
+ * Time Complexity: O(1)
190
+ * Space Complexity: O(1)
191
+ *
192
+ * The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
193
+ * HashMap in reverse order.
194
+ * @returns A new instance of the HashMapIterator class is being returned.
195
+ */
196
+ get reverseEnd(): HashMapIterator<K, V>;
197
+ /**
198
+ * Time Complexity: O(1)
199
+ * Space Complexity: O(1)
200
+ *
201
+ * The function returns the key-value pair at the front of a data structure.
202
+ * @returns The front element of the data structure, represented as a tuple with a key (K) and a
203
+ * value (V).
204
+ */
205
+ get front(): [K, V] | undefined;
206
+ /**
207
+ * Time Complexity: O(1)
208
+ * Space Complexity: O(1)
209
+ *
210
+ * The function returns the key-value pair at the end of a data structure.
211
+ * @returns The method is returning an array containing the key-value pair of the tail element in the
212
+ * data structure.
213
+ */
214
+ get back(): [K, V] | undefined;
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 resizeTable(newCapacity: number): void;
242
+ protected _deleteNode(node: HashMapLinkedNode<K, V>): void;
50
243
  }