min-heap-typed 1.50.1 → 1.50.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -45,18 +45,65 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
45
45
|
this.setMany(rawCollection);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* The function returns the store object, which is a dictionary of HashMapStoreItem objects.
|
|
50
|
+
* @returns The store property is being returned. It is a dictionary-like object with string keys and
|
|
51
|
+
* values of type HashMapStoreItem<K, V>.
|
|
52
|
+
*/
|
|
53
|
+
get store() {
|
|
54
|
+
return this._store;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The function returns the object map.
|
|
58
|
+
* @returns The `objMap` property is being returned, which is a `Map` object with keys of type
|
|
59
|
+
* `object` and values of type `V`.
|
|
60
|
+
*/
|
|
61
|
+
get objMap() {
|
|
62
|
+
return this._objMap;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The function returns the value of the _toEntryFn property.
|
|
66
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
67
|
+
*/
|
|
48
68
|
get toEntryFn() {
|
|
49
69
|
return this._toEntryFn;
|
|
50
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* The function returns the size of an object.
|
|
73
|
+
* @returns The size of the object, which is a number.
|
|
74
|
+
*/
|
|
51
75
|
get size() {
|
|
52
76
|
return this._size;
|
|
53
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* The hasFn function is a function that takes in an item and returns a boolean
|
|
80
|
+
* indicating whether the item is contained within the hash table.
|
|
81
|
+
*
|
|
82
|
+
* @return The hash function
|
|
83
|
+
*/
|
|
84
|
+
get hashFn() {
|
|
85
|
+
return this._hashFn;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
89
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
90
|
+
* data type.
|
|
91
|
+
* @returns a boolean value.
|
|
92
|
+
*/
|
|
54
93
|
isEntry(rawElement) {
|
|
55
94
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
56
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
98
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
99
|
+
*/
|
|
57
100
|
isEmpty() {
|
|
58
101
|
return this.size === 0;
|
|
59
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
105
|
+
* size.
|
|
106
|
+
*/
|
|
60
107
|
clear() {
|
|
61
108
|
this._store = {};
|
|
62
109
|
this._objMap.clear();
|
|
@@ -73,14 +120,14 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
73
120
|
*/
|
|
74
121
|
set(key, value) {
|
|
75
122
|
if (this._isObjKey(key)) {
|
|
76
|
-
if (!this.
|
|
123
|
+
if (!this.objMap.has(key)) {
|
|
77
124
|
this._size++;
|
|
78
125
|
}
|
|
79
|
-
this.
|
|
126
|
+
this.objMap.set(key, value);
|
|
80
127
|
}
|
|
81
128
|
else {
|
|
82
129
|
const strKey = this._getNoObjKey(key);
|
|
83
|
-
if (this.
|
|
130
|
+
if (this.store[strKey] === undefined) {
|
|
84
131
|
this._size++;
|
|
85
132
|
}
|
|
86
133
|
this._store[strKey] = { key, value };
|
|
@@ -97,7 +144,16 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
97
144
|
setMany(rawCollection) {
|
|
98
145
|
const results = [];
|
|
99
146
|
for (const rawEle of rawCollection) {
|
|
100
|
-
|
|
147
|
+
let key, value;
|
|
148
|
+
if (this.isEntry(rawEle)) {
|
|
149
|
+
key = rawEle[0];
|
|
150
|
+
value = rawEle[1];
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const item = this.toEntryFn(rawEle);
|
|
154
|
+
key = item[0];
|
|
155
|
+
value = item[1];
|
|
156
|
+
}
|
|
101
157
|
results.push(this.set(key, value));
|
|
102
158
|
}
|
|
103
159
|
return results;
|
|
@@ -113,7 +169,7 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
113
169
|
get(key) {
|
|
114
170
|
var _a;
|
|
115
171
|
if (this._isObjKey(key)) {
|
|
116
|
-
return this.
|
|
172
|
+
return this.objMap.get(key);
|
|
117
173
|
}
|
|
118
174
|
else {
|
|
119
175
|
const strKey = this._getNoObjKey(key);
|
|
@@ -128,11 +184,11 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
128
184
|
*/
|
|
129
185
|
has(key) {
|
|
130
186
|
if (this._isObjKey(key)) {
|
|
131
|
-
return this.
|
|
187
|
+
return this.objMap.has(key);
|
|
132
188
|
}
|
|
133
189
|
else {
|
|
134
190
|
const strKey = this._getNoObjKey(key);
|
|
135
|
-
return strKey in this.
|
|
191
|
+
return strKey in this.store;
|
|
136
192
|
}
|
|
137
193
|
}
|
|
138
194
|
/**
|
|
@@ -144,21 +200,35 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
144
200
|
*/
|
|
145
201
|
delete(key) {
|
|
146
202
|
if (this._isObjKey(key)) {
|
|
147
|
-
if (this.
|
|
203
|
+
if (this.objMap.has(key)) {
|
|
148
204
|
this._size--;
|
|
149
205
|
}
|
|
150
|
-
return this.
|
|
206
|
+
return this.objMap.delete(key);
|
|
151
207
|
}
|
|
152
208
|
else {
|
|
153
209
|
const strKey = this._getNoObjKey(key);
|
|
154
|
-
if (strKey in this.
|
|
155
|
-
delete this.
|
|
210
|
+
if (strKey in this.store) {
|
|
211
|
+
delete this.store[strKey];
|
|
156
212
|
this._size--;
|
|
157
213
|
return true;
|
|
158
214
|
}
|
|
159
215
|
return false;
|
|
160
216
|
}
|
|
161
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Time Complexity: O(n)
|
|
220
|
+
* Space Complexity: O(n)
|
|
221
|
+
*/
|
|
222
|
+
/**
|
|
223
|
+
* The clone function creates a new HashMap with the same key-value pairs as
|
|
224
|
+
* this one. The clone function is useful for creating a copy of an existing
|
|
225
|
+
* HashMap, and then modifying that copy without affecting the original.
|
|
226
|
+
*
|
|
227
|
+
* @return A new hashmap with the same values as this one
|
|
228
|
+
*/
|
|
229
|
+
clone() {
|
|
230
|
+
return new HashMap(this, { hashFn: this.hashFn, toEntryFn: this.toEntryFn });
|
|
231
|
+
}
|
|
162
232
|
/**
|
|
163
233
|
* Time Complexity: O(n)
|
|
164
234
|
* Space Complexity: O(n)
|
|
@@ -185,10 +255,6 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
185
255
|
}
|
|
186
256
|
return resultMap;
|
|
187
257
|
}
|
|
188
|
-
/**
|
|
189
|
-
* Time Complexity: O(n)
|
|
190
|
-
* Space Complexity: O(n)
|
|
191
|
-
*/
|
|
192
258
|
/**
|
|
193
259
|
* Time Complexity: O(n)
|
|
194
260
|
* Space Complexity: O(n)
|
|
@@ -215,6 +281,14 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
215
281
|
}
|
|
216
282
|
return filteredMap;
|
|
217
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* The put function sets a value in a data structure using a specified key.
|
|
286
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
|
|
287
|
+
* to the function.
|
|
288
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
289
|
+
* specified key in the data structure.
|
|
290
|
+
* @returns The method is returning a boolean value.
|
|
291
|
+
*/
|
|
218
292
|
put(key, value) {
|
|
219
293
|
return this.set(key, value);
|
|
220
294
|
}
|
|
@@ -223,22 +297,34 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
223
297
|
* object map.
|
|
224
298
|
*/
|
|
225
299
|
*_getIterator() {
|
|
226
|
-
for (const node of Object.values(this.
|
|
300
|
+
for (const node of Object.values(this.store)) {
|
|
227
301
|
yield [node.key, node.value];
|
|
228
302
|
}
|
|
229
|
-
for (const node of this.
|
|
303
|
+
for (const node of this.objMap) {
|
|
230
304
|
yield node;
|
|
231
305
|
}
|
|
232
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* The function checks if a given key is an object or a function.
|
|
309
|
+
* @param {any} key - The parameter "key" can be of any type.
|
|
310
|
+
* @returns a boolean value.
|
|
311
|
+
*/
|
|
233
312
|
_isObjKey(key) {
|
|
234
313
|
const keyType = typeof key;
|
|
235
314
|
return (keyType === 'object' || keyType === 'function') && key !== null;
|
|
236
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* The function `_getNoObjKey` takes a key and returns a string representation of the key, handling
|
|
318
|
+
* different types of keys.
|
|
319
|
+
* @param {K} key - The `key` parameter is of type `K`, which represents the type of the key being
|
|
320
|
+
* passed to the `_getNoObjKey` function.
|
|
321
|
+
* @returns a string value.
|
|
322
|
+
*/
|
|
237
323
|
_getNoObjKey(key) {
|
|
238
324
|
const keyType = typeof key;
|
|
239
325
|
let strKey;
|
|
240
326
|
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
|
|
241
|
-
strKey = this.
|
|
327
|
+
strKey = this.hashFn(key);
|
|
242
328
|
}
|
|
243
329
|
else {
|
|
244
330
|
if (keyType === 'number') {
|
|
@@ -259,35 +345,113 @@ exports.HashMap = HashMap;
|
|
|
259
345
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
260
346
|
*/
|
|
261
347
|
class LinkedHashMap extends base_1.IterableEntryBase {
|
|
262
|
-
|
|
348
|
+
/**
|
|
349
|
+
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
350
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements. It is
|
|
351
|
+
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
|
|
352
|
+
* `rawCollection` is converted to a key-value pair using the `toEntryFn` function (if provided) and
|
|
353
|
+
* then added to the HashMap
|
|
354
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
355
|
+
* properties:
|
|
356
|
+
*/
|
|
357
|
+
constructor(rawCollection = [], options) {
|
|
263
358
|
super();
|
|
359
|
+
this._hashFn = (key) => String(key);
|
|
360
|
+
this._objHashFn = (key) => key;
|
|
264
361
|
this._noObjMap = {};
|
|
265
362
|
this._objMap = new WeakMap();
|
|
363
|
+
this._toEntryFn = (rawElement) => {
|
|
364
|
+
if (this.isEntry(rawElement)) {
|
|
365
|
+
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
366
|
+
return rawElement;
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
throw new Error("If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified.");
|
|
370
|
+
}
|
|
371
|
+
};
|
|
266
372
|
this._size = 0;
|
|
267
|
-
/**
|
|
268
|
-
* Time Complexity: O(n)
|
|
269
|
-
* Space Complexity: O(n)
|
|
270
|
-
*/
|
|
271
|
-
this._hashFn = (key) => String(key);
|
|
272
|
-
this._objHashFn = (key) => key;
|
|
273
373
|
this._sentinel = {};
|
|
274
374
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
275
375
|
if (options) {
|
|
276
|
-
const { hashFn, objHashFn } = options;
|
|
376
|
+
const { hashFn, objHashFn, toEntryFn } = options;
|
|
277
377
|
if (hashFn)
|
|
278
378
|
this._hashFn = hashFn;
|
|
279
379
|
if (objHashFn)
|
|
280
380
|
this._objHashFn = objHashFn;
|
|
381
|
+
if (toEntryFn) {
|
|
382
|
+
this._toEntryFn = toEntryFn;
|
|
383
|
+
}
|
|
281
384
|
}
|
|
282
|
-
if (
|
|
283
|
-
for (const el of
|
|
284
|
-
this.
|
|
385
|
+
if (rawCollection) {
|
|
386
|
+
for (const el of rawCollection) {
|
|
387
|
+
const [key, value] = this.toEntryFn(el);
|
|
388
|
+
this.set(key, value);
|
|
285
389
|
}
|
|
286
390
|
}
|
|
287
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* The function returns the hash function used for generating a hash value for a given key.
|
|
394
|
+
* @returns The hash function that takes a key of type K and returns a string.
|
|
395
|
+
*/
|
|
396
|
+
get hashFn() {
|
|
397
|
+
return this._hashFn;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* The function returns the object hash function.
|
|
401
|
+
* @returns The function `objHashFn` is being returned.
|
|
402
|
+
*/
|
|
403
|
+
get objHashFn() {
|
|
404
|
+
return this._objHashFn;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* The function returns a record of HashMapLinkedNode objects with string keys.
|
|
408
|
+
* @returns The method is returning a Record object, which is a TypeScript type that represents an
|
|
409
|
+
* object with string keys and values that are HashMapLinkedNode objects with keys of type K and
|
|
410
|
+
* values of type V or undefined.
|
|
411
|
+
*/
|
|
412
|
+
get noObjMap() {
|
|
413
|
+
return this._noObjMap;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* The function returns the WeakMap object used to map objects to HashMapLinkedNode instances.
|
|
417
|
+
* @returns The `objMap` property is being returned.
|
|
418
|
+
*/
|
|
419
|
+
get objMap() {
|
|
420
|
+
return this._objMap;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* The function returns the head node of a HashMapLinkedNode.
|
|
424
|
+
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and
|
|
425
|
+
* value type `V | undefined`.
|
|
426
|
+
*/
|
|
427
|
+
get head() {
|
|
428
|
+
return this._head;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* The function returns the tail node of a HashMapLinkedNode.
|
|
432
|
+
* @returns The `_tail` property of type `HashMapLinkedNode<K, V | undefined>` is being returned.
|
|
433
|
+
*/
|
|
434
|
+
get tail() {
|
|
435
|
+
return this._tail;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* The function returns the value of the _toEntryFn property.
|
|
439
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
440
|
+
*/
|
|
441
|
+
get toEntryFn() {
|
|
442
|
+
return this._toEntryFn;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* The function returns the size of an object.
|
|
446
|
+
* @returns The size of the object.
|
|
447
|
+
*/
|
|
288
448
|
get size() {
|
|
289
449
|
return this._size;
|
|
290
450
|
}
|
|
451
|
+
/**
|
|
452
|
+
* Time Complexity: O(1)
|
|
453
|
+
* Space Complexity: O(1)
|
|
454
|
+
*/
|
|
291
455
|
/**
|
|
292
456
|
* Time Complexity: O(1)
|
|
293
457
|
* Space Complexity: O(1)
|
|
@@ -299,8 +463,12 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
299
463
|
get first() {
|
|
300
464
|
if (this._size === 0)
|
|
301
465
|
return;
|
|
302
|
-
return [this.
|
|
466
|
+
return [this.head.key, this.head.value];
|
|
303
467
|
}
|
|
468
|
+
/**
|
|
469
|
+
* Time Complexity: O(1)
|
|
470
|
+
* Space Complexity: O(1)
|
|
471
|
+
*/
|
|
304
472
|
/**
|
|
305
473
|
* Time Complexity: O(1)
|
|
306
474
|
* Space Complexity: O(1)
|
|
@@ -312,13 +480,13 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
312
480
|
get last() {
|
|
313
481
|
if (this._size === 0)
|
|
314
482
|
return;
|
|
315
|
-
return [this.
|
|
483
|
+
return [this.tail.key, this.tail.value];
|
|
316
484
|
}
|
|
317
485
|
/**
|
|
318
486
|
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
319
487
|
*/
|
|
320
488
|
*begin() {
|
|
321
|
-
let node = this.
|
|
489
|
+
let node = this.head;
|
|
322
490
|
while (node !== this._sentinel) {
|
|
323
491
|
yield [node.key, node.value];
|
|
324
492
|
node = node.next;
|
|
@@ -329,12 +497,16 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
329
497
|
* key and value.
|
|
330
498
|
*/
|
|
331
499
|
*reverseBegin() {
|
|
332
|
-
let node = this.
|
|
500
|
+
let node = this.tail;
|
|
333
501
|
while (node !== this._sentinel) {
|
|
334
502
|
yield [node.key, node.value];
|
|
335
503
|
node = node.prev;
|
|
336
504
|
}
|
|
337
505
|
}
|
|
506
|
+
/**
|
|
507
|
+
* Time Complexity: O(1)
|
|
508
|
+
* Space Complexity: O(1)
|
|
509
|
+
*/
|
|
338
510
|
/**
|
|
339
511
|
* Time Complexity: O(1)
|
|
340
512
|
* Space Complexity: O(1)
|
|
@@ -351,12 +523,12 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
351
523
|
let node;
|
|
352
524
|
const isNewKey = !this.has(key); // Check if the key is new
|
|
353
525
|
if ((0, utils_1.isWeakKey)(key)) {
|
|
354
|
-
const hash = this.
|
|
355
|
-
node = this.
|
|
526
|
+
const hash = this.objHashFn(key);
|
|
527
|
+
node = this.objMap.get(hash);
|
|
356
528
|
if (!node && isNewKey) {
|
|
357
529
|
// Create new node
|
|
358
|
-
node = { key: hash, value, prev: this.
|
|
359
|
-
this.
|
|
530
|
+
node = { key: hash, value, prev: this.tail, next: this._sentinel };
|
|
531
|
+
this.objMap.set(hash, node);
|
|
360
532
|
}
|
|
361
533
|
else if (node) {
|
|
362
534
|
// Update the value of an existing node
|
|
@@ -364,10 +536,10 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
364
536
|
}
|
|
365
537
|
}
|
|
366
538
|
else {
|
|
367
|
-
const hash = this.
|
|
368
|
-
node = this.
|
|
539
|
+
const hash = this.hashFn(key);
|
|
540
|
+
node = this.noObjMap[hash];
|
|
369
541
|
if (!node && isNewKey) {
|
|
370
|
-
this.
|
|
542
|
+
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
|
|
371
543
|
}
|
|
372
544
|
else if (node) {
|
|
373
545
|
// Update the value of an existing node
|
|
@@ -381,8 +553,8 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
381
553
|
this._sentinel.next = node;
|
|
382
554
|
}
|
|
383
555
|
else {
|
|
384
|
-
this.
|
|
385
|
-
node.prev = this.
|
|
556
|
+
this.tail.next = node;
|
|
557
|
+
node.prev = this.tail; // Make sure that the prev of the new node points to the current tail node
|
|
386
558
|
}
|
|
387
559
|
this._tail = node;
|
|
388
560
|
this._sentinel.prev = node;
|
|
@@ -390,24 +562,42 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
390
562
|
}
|
|
391
563
|
return true;
|
|
392
564
|
}
|
|
565
|
+
/**
|
|
566
|
+
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
567
|
+
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
568
|
+
* of booleans indicating the success of each set operation.
|
|
569
|
+
* @param rawCollection - The rawCollection parameter is an iterable collection of elements of type
|
|
570
|
+
* R.
|
|
571
|
+
* @returns The `setMany` function returns an array of booleans.
|
|
572
|
+
*/
|
|
573
|
+
setMany(rawCollection) {
|
|
574
|
+
const results = [];
|
|
575
|
+
for (const rawEle of rawCollection) {
|
|
576
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
577
|
+
results.push(this.set(key, value));
|
|
578
|
+
}
|
|
579
|
+
return results;
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
583
|
+
* key is a weak key or not.
|
|
584
|
+
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
585
|
+
* @returns The method `has` is returning a boolean value.
|
|
586
|
+
*/
|
|
393
587
|
has(key) {
|
|
394
588
|
if ((0, utils_1.isWeakKey)(key)) {
|
|
395
|
-
const hash = this.
|
|
396
|
-
return this.
|
|
589
|
+
const hash = this.objHashFn(key);
|
|
590
|
+
return this.objMap.has(hash);
|
|
397
591
|
}
|
|
398
592
|
else {
|
|
399
|
-
const hash = this.
|
|
400
|
-
return hash in this.
|
|
593
|
+
const hash = this.hashFn(key);
|
|
594
|
+
return hash in this.noObjMap;
|
|
401
595
|
}
|
|
402
596
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
results.push(this.set(key, value));
|
|
408
|
-
}
|
|
409
|
-
return results;
|
|
410
|
-
}
|
|
597
|
+
/**
|
|
598
|
+
* Time Complexity: O(1)
|
|
599
|
+
* Space Complexity: O(1)
|
|
600
|
+
*/
|
|
411
601
|
/**
|
|
412
602
|
* Time Complexity: O(1)
|
|
413
603
|
* Space Complexity: O(1)
|
|
@@ -423,36 +613,46 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
423
613
|
*/
|
|
424
614
|
get(key) {
|
|
425
615
|
if ((0, utils_1.isWeakKey)(key)) {
|
|
426
|
-
const hash = this.
|
|
427
|
-
const node = this.
|
|
616
|
+
const hash = this.objHashFn(key);
|
|
617
|
+
const node = this.objMap.get(hash);
|
|
428
618
|
return node ? node.value : undefined;
|
|
429
619
|
}
|
|
430
620
|
else {
|
|
431
|
-
const hash = this.
|
|
432
|
-
const node = this.
|
|
621
|
+
const hash = this.hashFn(key);
|
|
622
|
+
const node = this.noObjMap[hash];
|
|
433
623
|
return node ? node.value : undefined;
|
|
434
624
|
}
|
|
435
625
|
}
|
|
436
626
|
/**
|
|
437
|
-
* Time Complexity: O(n)
|
|
627
|
+
* Time Complexity: O(n)
|
|
628
|
+
* Space Complexity: O(1)
|
|
629
|
+
* /
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Time Complexity: O(n)
|
|
438
633
|
* Space Complexity: O(1)
|
|
439
634
|
*
|
|
440
|
-
* The function `
|
|
635
|
+
* The function `at` retrieves the key-value pair at a specified index in a linked list.
|
|
441
636
|
* @param {number} index - The index parameter is a number that represents the position of the
|
|
442
637
|
* element we want to retrieve from the data structure.
|
|
443
|
-
* @returns The method `
|
|
638
|
+
* @returns The method `at(index: number)` is returning an array containing the key-value pair at
|
|
444
639
|
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
445
640
|
* where `K` is the key and `V` is the value.
|
|
446
641
|
*/
|
|
447
|
-
|
|
642
|
+
at(index) {
|
|
448
643
|
(0, utils_1.rangeCheck)(index, 0, this._size - 1);
|
|
449
|
-
let node = this.
|
|
644
|
+
let node = this.head;
|
|
450
645
|
while (index--) {
|
|
451
646
|
node = node.next;
|
|
452
647
|
}
|
|
453
648
|
return node.value;
|
|
454
649
|
}
|
|
455
650
|
/**
|
|
651
|
+
* Time Complexity: O(1)
|
|
652
|
+
* Space Complexity: O(1)
|
|
653
|
+
* /
|
|
654
|
+
|
|
655
|
+
/**
|
|
456
656
|
* Time Complexity: O(1)
|
|
457
657
|
* Space Complexity: O(1)
|
|
458
658
|
*
|
|
@@ -465,31 +665,36 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
465
665
|
delete(key) {
|
|
466
666
|
let node;
|
|
467
667
|
if ((0, utils_1.isWeakKey)(key)) {
|
|
468
|
-
const hash = this.
|
|
668
|
+
const hash = this.objHashFn(key);
|
|
469
669
|
// Get nodes from WeakMap
|
|
470
|
-
node = this.
|
|
670
|
+
node = this.objMap.get(hash);
|
|
471
671
|
if (!node) {
|
|
472
672
|
return false; // If the node does not exist, return false
|
|
473
673
|
}
|
|
474
674
|
// Remove nodes from WeakMap
|
|
475
|
-
this.
|
|
675
|
+
this.objMap.delete(hash);
|
|
476
676
|
}
|
|
477
677
|
else {
|
|
478
|
-
const hash = this.
|
|
678
|
+
const hash = this.hashFn(key);
|
|
479
679
|
// Get nodes from noObjMap
|
|
480
|
-
node = this.
|
|
680
|
+
node = this.noObjMap[hash];
|
|
481
681
|
if (!node) {
|
|
482
682
|
return false; // If the node does not exist, return false
|
|
483
683
|
}
|
|
484
684
|
// Remove nodes from orgMap
|
|
485
|
-
delete this.
|
|
685
|
+
delete this.noObjMap[hash];
|
|
486
686
|
}
|
|
487
687
|
// Remove node from doubly linked list
|
|
488
688
|
this._deleteNode(node);
|
|
489
689
|
return true;
|
|
490
690
|
}
|
|
491
691
|
/**
|
|
492
|
-
* Time Complexity: O(n)
|
|
692
|
+
* Time Complexity: O(n)
|
|
693
|
+
* Space Complexity: O(1)
|
|
694
|
+
* /
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Time Complexity: O(n)
|
|
493
698
|
* Space Complexity: O(1)
|
|
494
699
|
*
|
|
495
700
|
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
@@ -499,13 +704,18 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
499
704
|
*/
|
|
500
705
|
deleteAt(index) {
|
|
501
706
|
(0, utils_1.rangeCheck)(index, 0, this._size - 1);
|
|
502
|
-
let node = this.
|
|
707
|
+
let node = this.head;
|
|
503
708
|
while (index--) {
|
|
504
709
|
node = node.next;
|
|
505
710
|
}
|
|
506
711
|
return this._deleteNode(node);
|
|
507
712
|
}
|
|
508
713
|
/**
|
|
714
|
+
* Time Complexity: O(1)
|
|
715
|
+
* Space Complexity: O(1)
|
|
716
|
+
* /
|
|
717
|
+
|
|
718
|
+
/**
|
|
509
719
|
* Time Complexity: O(1)
|
|
510
720
|
* Space Complexity: O(1)
|
|
511
721
|
*
|
|
@@ -516,7 +726,21 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
516
726
|
isEmpty() {
|
|
517
727
|
return this._size === 0;
|
|
518
728
|
}
|
|
729
|
+
/**
|
|
730
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
731
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
732
|
+
* data type.
|
|
733
|
+
* @returns a boolean value.
|
|
734
|
+
*/
|
|
735
|
+
isEntry(rawElement) {
|
|
736
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
737
|
+
}
|
|
519
738
|
/**
|
|
739
|
+
* Time Complexity: O(1)
|
|
740
|
+
* Space Complexity: O(1)
|
|
741
|
+
* /
|
|
742
|
+
|
|
743
|
+
/**
|
|
520
744
|
* Time Complexity: O(1)
|
|
521
745
|
* Space Complexity: O(1)
|
|
522
746
|
*
|
|
@@ -527,8 +751,21 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
527
751
|
this._size = 0;
|
|
528
752
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
529
753
|
}
|
|
754
|
+
/**
|
|
755
|
+
* Time Complexity: O(n)
|
|
756
|
+
* Space Complexity: O(n)
|
|
757
|
+
*/
|
|
758
|
+
/**
|
|
759
|
+
* Time Complexity: O(n)
|
|
760
|
+
* Space Complexity: O(n)
|
|
761
|
+
*
|
|
762
|
+
* The `clone` function creates a new instance of a `LinkedHashMap` with the same key-value pairs as
|
|
763
|
+
* the original.
|
|
764
|
+
* @returns The `clone()` method is returning a new instance of `LinkedHashMap<K, V>` that is a clone
|
|
765
|
+
* of the original `LinkedHashMap` object.
|
|
766
|
+
*/
|
|
530
767
|
clone() {
|
|
531
|
-
const cloned = new LinkedHashMap([], { hashFn: this.
|
|
768
|
+
const cloned = new LinkedHashMap([], { hashFn: this.hashFn, objHashFn: this.objHashFn });
|
|
532
769
|
for (const entry of this) {
|
|
533
770
|
const [key, value] = entry;
|
|
534
771
|
cloned.set(key, value);
|
|
@@ -536,6 +773,11 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
536
773
|
return cloned;
|
|
537
774
|
}
|
|
538
775
|
/**
|
|
776
|
+
* Time Complexity: O(n)
|
|
777
|
+
* Space Complexity: O(n)
|
|
778
|
+
* /
|
|
779
|
+
|
|
780
|
+
/**
|
|
539
781
|
* Time Complexity: O(n)
|
|
540
782
|
* Space Complexity: O(n)
|
|
541
783
|
*
|
|
@@ -562,6 +804,11 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
562
804
|
return filteredMap;
|
|
563
805
|
}
|
|
564
806
|
/**
|
|
807
|
+
* Time Complexity: O(n)
|
|
808
|
+
* Space Complexity: O(n)
|
|
809
|
+
* /
|
|
810
|
+
|
|
811
|
+
/**
|
|
565
812
|
* Time Complexity: O(n)
|
|
566
813
|
* Space Complexity: O(n)
|
|
567
814
|
*
|
|
@@ -589,25 +836,47 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
589
836
|
return mappedMap;
|
|
590
837
|
}
|
|
591
838
|
/**
|
|
592
|
-
* Time Complexity: O(
|
|
593
|
-
* Space Complexity: O(
|
|
839
|
+
* Time Complexity: O(1)
|
|
840
|
+
* Space Complexity: O(1)
|
|
841
|
+
*/
|
|
842
|
+
/**
|
|
843
|
+
* Time Complexity: O(1)
|
|
844
|
+
* Space Complexity: O(1)
|
|
845
|
+
*
|
|
846
|
+
* The put function sets a value in a data structure using a specified key.
|
|
847
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
|
|
848
|
+
* to the function.
|
|
849
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
850
|
+
* specified key in the data structure.
|
|
851
|
+
* @returns The method is returning a boolean value.
|
|
594
852
|
*/
|
|
595
853
|
put(key, value) {
|
|
596
854
|
return this.set(key, value);
|
|
597
855
|
}
|
|
598
856
|
/**
|
|
599
|
-
* Time Complexity: O(n)
|
|
857
|
+
* Time Complexity: O(n)
|
|
858
|
+
* Space Complexity: O(1)
|
|
859
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
860
|
+
* /
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Time Complexity: O(n)
|
|
600
864
|
* Space Complexity: O(1)
|
|
865
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
601
866
|
*
|
|
602
867
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
603
868
|
*/
|
|
604
869
|
*_getIterator() {
|
|
605
|
-
let node = this.
|
|
870
|
+
let node = this.head;
|
|
606
871
|
while (node !== this._sentinel) {
|
|
607
872
|
yield [node.key, node.value];
|
|
608
873
|
node = node.next;
|
|
609
874
|
}
|
|
610
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* Time Complexity: O(1)
|
|
878
|
+
* Space Complexity: O(1)
|
|
879
|
+
*/
|
|
611
880
|
/**
|
|
612
881
|
* Time Complexity: O(1)
|
|
613
882
|
* Space Complexity: O(1)
|
|
@@ -622,10 +891,10 @@ class LinkedHashMap extends base_1.IterableEntryBase {
|
|
|
622
891
|
const { prev, next } = node;
|
|
623
892
|
prev.next = next;
|
|
624
893
|
next.prev = prev;
|
|
625
|
-
if (node === this.
|
|
894
|
+
if (node === this.head) {
|
|
626
895
|
this._head = next;
|
|
627
896
|
}
|
|
628
|
-
if (node === this.
|
|
897
|
+
if (node === this.tail) {
|
|
629
898
|
this._tail = prev;
|
|
630
899
|
}
|
|
631
900
|
this._size -= 1;
|