data-structure-typed 1.48.0 → 1.48.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/CHANGELOG.md +1 -1
- package/README.md +16 -16
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +172 -292
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +38 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +54 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +173 -16
- package/dist/cjs/data-structures/hash/hash-map.js +373 -37
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +38 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +54 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +173 -16
- package/dist/mjs/data-structures/hash/hash-map.js +369 -35
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/umd/data-structure-typed.js +429 -31
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +62 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
- package/src/data-structures/hash/hash-map.ts +400 -40
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +21 -25
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +16 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +16 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +16 -0
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
|
@@ -7,6 +7,305 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
9
9
|
export class HashMap {
|
|
10
|
+
_store = {};
|
|
11
|
+
_objMap = new Map();
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes a new instance of a class with optional elements and options.
|
|
14
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
15
|
+
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
16
|
+
* key-value pairs.
|
|
17
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
18
|
+
* configuration options for the constructor. In this case, it has one property:
|
|
19
|
+
*/
|
|
20
|
+
constructor(elements = [], options) {
|
|
21
|
+
if (options) {
|
|
22
|
+
const { hashFn } = options;
|
|
23
|
+
if (hashFn) {
|
|
24
|
+
this._hashFn = hashFn;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (elements) {
|
|
28
|
+
this.setMany(elements);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
_size = 0;
|
|
32
|
+
get size() {
|
|
33
|
+
return this._size;
|
|
34
|
+
}
|
|
35
|
+
isEmpty() {
|
|
36
|
+
return this.size === 0;
|
|
37
|
+
}
|
|
38
|
+
clear() {
|
|
39
|
+
this._store = {};
|
|
40
|
+
this._objMap.clear();
|
|
41
|
+
this._size = 0;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
45
|
+
* the key is not already present.
|
|
46
|
+
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
47
|
+
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be
|
|
48
|
+
* stored in a regular JavaScript object.
|
|
49
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
50
|
+
* key in the data structure.
|
|
51
|
+
*/
|
|
52
|
+
set(key, value) {
|
|
53
|
+
if (this._isObjKey(key)) {
|
|
54
|
+
if (!this._objMap.has(key)) {
|
|
55
|
+
this._size++;
|
|
56
|
+
}
|
|
57
|
+
this._objMap.set(key, value);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const strKey = this._getNoObjKey(key);
|
|
61
|
+
if (this._store[strKey] === undefined) {
|
|
62
|
+
this._size++;
|
|
63
|
+
}
|
|
64
|
+
this._store[strKey] = { key, value };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The function "setMany" sets multiple key-value pairs in a map.
|
|
69
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
70
|
+
* key-value pair is represented as an array with two elements: the key and the value.
|
|
71
|
+
*/
|
|
72
|
+
setMany(elements) {
|
|
73
|
+
for (const [key, value] of elements)
|
|
74
|
+
this.set(key, value);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
78
|
+
* a string map.
|
|
79
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
80
|
+
* of any type, but it should be compatible with the key type used when the map was created.
|
|
81
|
+
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
|
|
82
|
+
* or `_store`, otherwise it returns `undefined`.
|
|
83
|
+
*/
|
|
84
|
+
get(key) {
|
|
85
|
+
if (this._isObjKey(key)) {
|
|
86
|
+
return this._objMap.get(key);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const strKey = this._getNoObjKey(key);
|
|
90
|
+
return this._store[strKey]?.value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
95
|
+
* is an object key or not.
|
|
96
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
97
|
+
* @returns The `has` method is returning a boolean value.
|
|
98
|
+
*/
|
|
99
|
+
has(key) {
|
|
100
|
+
if (this._isObjKey(key)) {
|
|
101
|
+
return this._objMap.has(key);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const strKey = this._getNoObjKey(key);
|
|
105
|
+
return strKey in this._store;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
110
|
+
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
111
|
+
* data structure.
|
|
112
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was
|
|
113
|
+
* successfully deleted from the map, and `false` if the key was not found in the map.
|
|
114
|
+
*/
|
|
115
|
+
delete(key) {
|
|
116
|
+
if (this._isObjKey(key)) {
|
|
117
|
+
if (this._objMap.has(key)) {
|
|
118
|
+
this._size--;
|
|
119
|
+
}
|
|
120
|
+
return this._objMap.delete(key);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const strKey = this._getNoObjKey(key);
|
|
124
|
+
if (strKey in this._store) {
|
|
125
|
+
delete this._store[strKey];
|
|
126
|
+
this._size--;
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
134
|
+
* object map.
|
|
135
|
+
*/
|
|
136
|
+
*[Symbol.iterator]() {
|
|
137
|
+
for (const node of Object.values(this._store)) {
|
|
138
|
+
yield [node.key, node.value];
|
|
139
|
+
}
|
|
140
|
+
for (const node of this._objMap) {
|
|
141
|
+
yield node;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The function returns an iterator that yields key-value pairs from the object.
|
|
146
|
+
*/
|
|
147
|
+
*entries() {
|
|
148
|
+
for (const item of this) {
|
|
149
|
+
yield item;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The function `keys()` returns an iterator that yields all the keys of the object.
|
|
154
|
+
*/
|
|
155
|
+
*keys() {
|
|
156
|
+
for (const [key] of this) {
|
|
157
|
+
yield key;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
*values() {
|
|
161
|
+
for (const [, value] of this) {
|
|
162
|
+
yield value;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The `every` function checks if every element in a HashMap satisfies a given predicate function.
|
|
167
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
168
|
+
* index, and map. It is used to test each element in the map against a condition. If the predicate
|
|
169
|
+
* function returns false for any element, the every() method will return false. If the predicate
|
|
170
|
+
* function returns true for all
|
|
171
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
172
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
173
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
174
|
+
* @returns The method is returning a boolean value. It returns true if the predicate function
|
|
175
|
+
* returns true for every element in the map, and false otherwise.
|
|
176
|
+
*/
|
|
177
|
+
every(predicate, thisArg) {
|
|
178
|
+
let index = 0;
|
|
179
|
+
for (const [key, value] of this) {
|
|
180
|
+
if (!predicate.call(thisArg, value, key, index++, this)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The "some" function checks if at least one element in a HashMap satisfies a given predicate.
|
|
188
|
+
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
|
|
189
|
+
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
|
|
190
|
+
* key-value pair in the `HashMap`.
|
|
191
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
192
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
193
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
194
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
195
|
+
* in the map, and false otherwise.
|
|
196
|
+
*/
|
|
197
|
+
some(predicate, thisArg) {
|
|
198
|
+
let index = 0;
|
|
199
|
+
for (const [key, value] of this) {
|
|
200
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to
|
|
208
|
+
* each element.
|
|
209
|
+
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
|
|
210
|
+
* takes four parameters:
|
|
211
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
212
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
213
|
+
* be passed as the `this` value inside the `callbackfn` function. If `thisArg
|
|
214
|
+
*/
|
|
215
|
+
forEach(callbackfn, thisArg) {
|
|
216
|
+
let index = 0;
|
|
217
|
+
for (const [key, value] of this) {
|
|
218
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each
|
|
223
|
+
* key-value pair in the original HashMap.
|
|
224
|
+
* @param callbackfn - The callback function that will be called for each key-value pair in the
|
|
225
|
+
* HashMap. It takes four parameters:
|
|
226
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
227
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
228
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
229
|
+
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
230
|
+
* the provided callback function.
|
|
231
|
+
*/
|
|
232
|
+
map(callbackfn, thisArg) {
|
|
233
|
+
const resultMap = new HashMap();
|
|
234
|
+
let index = 0;
|
|
235
|
+
for (const [key, value] of this) {
|
|
236
|
+
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
237
|
+
}
|
|
238
|
+
return resultMap;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
|
|
242
|
+
* that satisfy a given predicate function.
|
|
243
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
244
|
+
* index, and map. It is used to determine whether an element should be included in the filtered map
|
|
245
|
+
* or not. The function should return a boolean value - true if the element should be included, and
|
|
246
|
+
* false otherwise.
|
|
247
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
248
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
249
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
250
|
+
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
251
|
+
* from the original `HashMap` that pass the provided `predicate` function.
|
|
252
|
+
*/
|
|
253
|
+
filter(predicate, thisArg) {
|
|
254
|
+
const filteredMap = new HashMap();
|
|
255
|
+
let index = 0;
|
|
256
|
+
for (const [key, value] of this) {
|
|
257
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
258
|
+
filteredMap.set(key, value);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return filteredMap;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to
|
|
265
|
+
* each element, accumulating a single value.
|
|
266
|
+
* @param callbackfn - The callback function that will be called for each element in the HashMap. It
|
|
267
|
+
* takes five parameters:
|
|
268
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
269
|
+
* is the value that will be used as the first argument of the callback function when reducing the
|
|
270
|
+
* elements of the map.
|
|
271
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
272
|
+
* all the elements in the `HashMap`.
|
|
273
|
+
*/
|
|
274
|
+
reduce(callbackfn, initialValue) {
|
|
275
|
+
let accumulator = initialValue;
|
|
276
|
+
let index = 0;
|
|
277
|
+
for (const [key, value] of this) {
|
|
278
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
279
|
+
}
|
|
280
|
+
return accumulator;
|
|
281
|
+
}
|
|
282
|
+
print() {
|
|
283
|
+
console.log([...this.entries()]);
|
|
284
|
+
}
|
|
285
|
+
_hashFn = (key) => String(key);
|
|
286
|
+
_isObjKey(key) {
|
|
287
|
+
const keyType = typeof key;
|
|
288
|
+
return (keyType === 'object' || keyType === 'function') && key !== null;
|
|
289
|
+
}
|
|
290
|
+
_getNoObjKey(key) {
|
|
291
|
+
const keyType = typeof key;
|
|
292
|
+
let strKey;
|
|
293
|
+
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
|
|
294
|
+
strKey = this._hashFn(key);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
if (keyType === "number") {
|
|
298
|
+
// TODO numeric key should has its own hash
|
|
299
|
+
strKey = key;
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
strKey = key;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return strKey;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
export class LinkedHashMap {
|
|
10
309
|
_noObjMap = {};
|
|
11
310
|
_objMap = new WeakMap();
|
|
12
311
|
_head;
|
|
@@ -94,47 +393,74 @@ export class HashMap {
|
|
|
94
393
|
*/
|
|
95
394
|
set(key, value) {
|
|
96
395
|
let node;
|
|
396
|
+
const isNewKey = !this.has(key); // Check if the key is new
|
|
97
397
|
if (isWeakKey(key)) {
|
|
98
398
|
const hash = this._objHashFn(key);
|
|
99
399
|
node = this._objMap.get(hash);
|
|
100
|
-
if (node) {
|
|
101
|
-
// If the node already exists, update its value
|
|
102
|
-
node.value = value;
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
400
|
+
if (!node && isNewKey) {
|
|
105
401
|
// Create new node
|
|
106
402
|
node = { key: hash, value, prev: this._tail, next: this._sentinel };
|
|
107
|
-
// Add new nodes to _objMap and linked list
|
|
108
403
|
this._objMap.set(hash, node);
|
|
109
404
|
}
|
|
405
|
+
else if (node) {
|
|
406
|
+
// Update the value of an existing node
|
|
407
|
+
node.value = value;
|
|
408
|
+
}
|
|
110
409
|
}
|
|
111
410
|
else {
|
|
112
411
|
const hash = this._hashFn(key);
|
|
113
|
-
// Non-object keys are handled in the same way as the original implementation
|
|
114
412
|
node = this._noObjMap[hash];
|
|
115
|
-
if (node) {
|
|
413
|
+
if (!node && isNewKey) {
|
|
414
|
+
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel };
|
|
415
|
+
}
|
|
416
|
+
else if (node) {
|
|
417
|
+
// Update the value of an existing node
|
|
116
418
|
node.value = value;
|
|
117
419
|
}
|
|
420
|
+
}
|
|
421
|
+
if (node && isNewKey) {
|
|
422
|
+
// Update the head and tail of the linked list
|
|
423
|
+
if (this._size === 0) {
|
|
424
|
+
this._head = node;
|
|
425
|
+
this._sentinel.next = node;
|
|
426
|
+
}
|
|
118
427
|
else {
|
|
119
|
-
this.
|
|
120
|
-
|
|
121
|
-
value,
|
|
122
|
-
prev: this._tail,
|
|
123
|
-
next: this._sentinel
|
|
124
|
-
};
|
|
428
|
+
this._tail.next = node;
|
|
429
|
+
node.prev = this._tail; // Make sure that the prev of the new node points to the current tail node
|
|
125
430
|
}
|
|
431
|
+
this._tail = node;
|
|
432
|
+
this._sentinel.prev = node;
|
|
433
|
+
this._size++;
|
|
126
434
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
435
|
+
return this._size;
|
|
436
|
+
}
|
|
437
|
+
has(key) {
|
|
438
|
+
if (isWeakKey(key)) {
|
|
439
|
+
const hash = this._objHashFn(key);
|
|
440
|
+
return this._objMap.has(hash);
|
|
130
441
|
}
|
|
131
442
|
else {
|
|
132
|
-
|
|
443
|
+
const hash = this._hashFn(key);
|
|
444
|
+
return hash in this._noObjMap;
|
|
133
445
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
446
|
+
}
|
|
447
|
+
setMany(entries) {
|
|
448
|
+
for (const entry of entries) {
|
|
449
|
+
const [key, value] = entry;
|
|
450
|
+
this.set(key, value);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
keys() {
|
|
454
|
+
const keys = [];
|
|
455
|
+
for (const [key] of this)
|
|
456
|
+
keys.push(key);
|
|
457
|
+
return keys;
|
|
458
|
+
}
|
|
459
|
+
values() {
|
|
460
|
+
const values = [];
|
|
461
|
+
for (const [, value] of this)
|
|
462
|
+
values.push(value);
|
|
463
|
+
return values;
|
|
138
464
|
}
|
|
139
465
|
/**
|
|
140
466
|
* Time Complexity: O(1)
|
|
@@ -256,14 +582,22 @@ export class HashMap {
|
|
|
256
582
|
this._size = 0;
|
|
257
583
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
258
584
|
}
|
|
585
|
+
clone() {
|
|
586
|
+
const cloned = new LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
|
587
|
+
for (const entry of this) {
|
|
588
|
+
const [key, value] = entry;
|
|
589
|
+
cloned.set(key, value);
|
|
590
|
+
}
|
|
591
|
+
return cloned;
|
|
592
|
+
}
|
|
259
593
|
/**
|
|
260
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
594
|
+
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
261
595
|
* Space Complexity: O(1)
|
|
262
596
|
*
|
|
263
|
-
* The `forEach` function iterates over each element in a
|
|
597
|
+
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on
|
|
264
598
|
* each element.
|
|
265
599
|
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
266
|
-
*
|
|
600
|
+
* LinkedHashMap. It takes three arguments:
|
|
267
601
|
*/
|
|
268
602
|
forEach(callback) {
|
|
269
603
|
let index = 0;
|
|
@@ -274,15 +608,15 @@ export class HashMap {
|
|
|
274
608
|
}
|
|
275
609
|
}
|
|
276
610
|
/**
|
|
277
|
-
* The `filter` function takes a predicate function and returns a new
|
|
611
|
+
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the
|
|
278
612
|
* key-value pairs that satisfy the predicate.
|
|
279
613
|
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
|
|
280
614
|
* `map`.
|
|
281
|
-
* @returns a new
|
|
615
|
+
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that
|
|
282
616
|
* satisfy the given predicate function.
|
|
283
617
|
*/
|
|
284
618
|
filter(predicate) {
|
|
285
|
-
const filteredMap = new
|
|
619
|
+
const filteredMap = new LinkedHashMap();
|
|
286
620
|
let index = 0;
|
|
287
621
|
for (const [key, value] of this) {
|
|
288
622
|
if (predicate([key, value], index, this)) {
|
|
@@ -293,14 +627,14 @@ export class HashMap {
|
|
|
293
627
|
return filteredMap;
|
|
294
628
|
}
|
|
295
629
|
/**
|
|
296
|
-
* The `map` function takes a callback function and returns a new
|
|
630
|
+
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed
|
|
297
631
|
* by the callback.
|
|
298
632
|
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and
|
|
299
633
|
* `map`.
|
|
300
|
-
* @returns a new
|
|
634
|
+
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function.
|
|
301
635
|
*/
|
|
302
636
|
map(callback) {
|
|
303
|
-
const mappedMap = new
|
|
637
|
+
const mappedMap = new LinkedHashMap();
|
|
304
638
|
let index = 0;
|
|
305
639
|
for (const [key, value] of this) {
|
|
306
640
|
const newValue = callback([key, value], index, this);
|
|
@@ -310,16 +644,16 @@ export class HashMap {
|
|
|
310
644
|
return mappedMap;
|
|
311
645
|
}
|
|
312
646
|
/**
|
|
313
|
-
* The `reduce` function iterates over the elements of a
|
|
647
|
+
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to
|
|
314
648
|
* each element, accumulating a single value.
|
|
315
649
|
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
316
|
-
* element, and map. It is called for each element in the
|
|
650
|
+
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single
|
|
317
651
|
* result.
|
|
318
652
|
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
319
653
|
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
320
654
|
* the elements of the map.
|
|
321
655
|
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
322
|
-
* over all the elements in the
|
|
656
|
+
* over all the elements in the LinkedHashMap and applying the callback function to each element.
|
|
323
657
|
*/
|
|
324
658
|
reduce(callback, initialValue) {
|
|
325
659
|
let accumulator = initialValue;
|
|
@@ -331,7 +665,7 @@ export class HashMap {
|
|
|
331
665
|
return accumulator;
|
|
332
666
|
}
|
|
333
667
|
/**
|
|
334
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
668
|
+
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
335
669
|
* Space Complexity: O(1)
|
|
336
670
|
*
|
|
337
671
|
* The above function is an iterator that yields key-value pairs from a linked list.
|