data-structure-typed 2.0.0 → 2.0.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/dist/cjs/data-structures/graph/abstract-graph.js +14 -14
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +66 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +66 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +47 -0
- package/dist/cjs/data-structures/queue/queue.js +47 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +121 -0
- package/dist/cjs/data-structures/stack/stack.js +121 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +14 -14
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/esm/data-structures/hash/hash-map.js +46 -0
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +66 -0
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +66 -0
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +47 -0
- package/dist/esm/data-structures/queue/queue.js +47 -0
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +121 -0
- package/dist/esm/data-structures/stack/stack.js +121 -0
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
- package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
- package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
- package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
- package/dist/individuals/binary-tree/bst.mjs +3858 -0
- package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
- package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
- package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
- package/dist/individuals/graph/directed-graph.mjs +2910 -0
- package/dist/individuals/graph/undirected-graph.mjs +2745 -0
- package/dist/individuals/hash/hash-map.mjs +1040 -0
- package/dist/individuals/heap/heap.mjs +909 -0
- package/dist/individuals/heap/max-heap.mjs +671 -0
- package/dist/individuals/heap/min-heap.mjs +659 -0
- package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
- package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
- package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
- package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
- package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
- package/dist/individuals/queue/deque.mjs +1262 -0
- package/dist/individuals/queue/queue.mjs +1865 -0
- package/dist/individuals/stack/stack.mjs +415 -0
- package/dist/individuals/trie/trie.mjs +687 -0
- package/dist/umd/data-structure-typed.js +14 -14
- 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/graph/abstract-graph.ts +14 -14
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +66 -0
- package/src/data-structures/queue/queue.ts +47 -0
- package/src/data-structures/stack/stack.ts +121 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +72 -1
- package/test/unit/data-structures/queue/queue.test.ts +214 -0
- package/test/unit/data-structures/stack/stack.test.ts +165 -0
|
@@ -0,0 +1,1040 @@
|
|
|
1
|
+
// src/data-structures/base/iterable-entry-base.ts
|
|
2
|
+
var IterableEntryBase = class {
|
|
3
|
+
/**
|
|
4
|
+
* Time Complexity: O(n)
|
|
5
|
+
* Space Complexity: O(1)
|
|
6
|
+
*
|
|
7
|
+
* The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
|
|
8
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
9
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
10
|
+
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
11
|
+
*/
|
|
12
|
+
*[Symbol.iterator](...args) {
|
|
13
|
+
yield* this._getIterator(...args);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Time Complexity: O(n)
|
|
17
|
+
* Space Complexity: O(n)
|
|
18
|
+
*
|
|
19
|
+
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
20
|
+
* be undefined.
|
|
21
|
+
*/
|
|
22
|
+
*entries() {
|
|
23
|
+
for (const item of this) {
|
|
24
|
+
yield item;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Time Complexity: O(n)
|
|
29
|
+
* Space Complexity: O(n)
|
|
30
|
+
*
|
|
31
|
+
* The function returns an iterator that yields the keys of a data structure.
|
|
32
|
+
*/
|
|
33
|
+
*keys() {
|
|
34
|
+
for (const item of this) {
|
|
35
|
+
yield item[0];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Time Complexity: O(n)
|
|
40
|
+
* Space Complexity: O(n)
|
|
41
|
+
*
|
|
42
|
+
* The function returns an iterator that yields the values of a collection.
|
|
43
|
+
*/
|
|
44
|
+
*values() {
|
|
45
|
+
for (const item of this) {
|
|
46
|
+
yield item[1];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Time Complexity: O(n)
|
|
51
|
+
* Space Complexity: O(1)
|
|
52
|
+
*
|
|
53
|
+
* The `every` function checks if every element in a collection satisfies a given condition.
|
|
54
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
55
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
56
|
+
* met for the current element in the iteration.
|
|
57
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
58
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
59
|
+
* passed as the first argument to the `predicate` function. If `thisArg` is not provided
|
|
60
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
61
|
+
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
62
|
+
*/
|
|
63
|
+
every(predicate, thisArg) {
|
|
64
|
+
let index = 0;
|
|
65
|
+
for (const item of this) {
|
|
66
|
+
if (!predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Time Complexity: O(n)
|
|
74
|
+
* Space Complexity: O(1)
|
|
75
|
+
*
|
|
76
|
+
* The "some" function iterates over a collection and returns true if at least one element satisfies
|
|
77
|
+
* a given predicate.
|
|
78
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
79
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
80
|
+
* met for the current element in the iteration.
|
|
81
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
82
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
83
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
84
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
85
|
+
* the collection, and false otherwise.
|
|
86
|
+
*/
|
|
87
|
+
some(predicate, thisArg) {
|
|
88
|
+
let index = 0;
|
|
89
|
+
for (const item of this) {
|
|
90
|
+
if (predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Time Complexity: O(n)
|
|
98
|
+
* Space Complexity: O(1)
|
|
99
|
+
*
|
|
100
|
+
* The `forEach` function iterates over each key-value pair in a collection and executes a callback
|
|
101
|
+
* function for each pair.
|
|
102
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
103
|
+
* It takes four parameters: the value of the current element, the key of the current element, the
|
|
104
|
+
* index of the current element, and the collection itself.
|
|
105
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
106
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
107
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
108
|
+
*/
|
|
109
|
+
forEach(callbackfn, thisArg) {
|
|
110
|
+
let index = 0;
|
|
111
|
+
for (const item of this) {
|
|
112
|
+
const [key, value] = item;
|
|
113
|
+
callbackfn.call(thisArg, key, value, index++, this);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Time Complexity: O(n)
|
|
118
|
+
* Space Complexity: O(1)
|
|
119
|
+
*
|
|
120
|
+
* The `find` function iterates over the entries of a collection and returns the first value for
|
|
121
|
+
* which the callback function returns true.
|
|
122
|
+
* @param callbackfn - The callback function that will be called for each entry in the collection. It
|
|
123
|
+
* takes three arguments: the value of the entry, the key of the entry, and the index of the entry in
|
|
124
|
+
* the collection. It should return a boolean value indicating whether the current entry matches the
|
|
125
|
+
* desired condition.
|
|
126
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
127
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
128
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
129
|
+
* @returns The method `find` returns the value of the first element in the iterable that satisfies
|
|
130
|
+
* the provided callback function. If no element satisfies the callback function, `undefined` is
|
|
131
|
+
* returned.
|
|
132
|
+
*/
|
|
133
|
+
find(callbackfn, thisArg) {
|
|
134
|
+
let index = 0;
|
|
135
|
+
for (const item of this) {
|
|
136
|
+
const [key, value] = item;
|
|
137
|
+
if (callbackfn.call(thisArg, key, value, index++, this)) return item;
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Time Complexity: O(n)
|
|
143
|
+
* Space Complexity: O(1)
|
|
144
|
+
*
|
|
145
|
+
* The function checks if a given key exists in a collection.
|
|
146
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents
|
|
147
|
+
* the key that we want to check for existence in the data structure.
|
|
148
|
+
* @returns a boolean value. It returns true if the key is found in the collection, and false
|
|
149
|
+
* otherwise.
|
|
150
|
+
*/
|
|
151
|
+
has(key) {
|
|
152
|
+
for (const item of this) {
|
|
153
|
+
const [itemKey] = item;
|
|
154
|
+
if (itemKey === key) return true;
|
|
155
|
+
}
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Time Complexity: O(n)
|
|
160
|
+
* Space Complexity: O(1)
|
|
161
|
+
*
|
|
162
|
+
* The function checks if a given value exists in a collection.
|
|
163
|
+
* @param {V} value - The parameter "value" is the value that we want to check if it exists in the
|
|
164
|
+
* collection.
|
|
165
|
+
* @returns a boolean value, either true or false.
|
|
166
|
+
*/
|
|
167
|
+
hasValue(value) {
|
|
168
|
+
for (const [, elementValue] of this) {
|
|
169
|
+
if (elementValue === value) return true;
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Time Complexity: O(n)
|
|
175
|
+
* Space Complexity: O(1)
|
|
176
|
+
*
|
|
177
|
+
* The `get` function retrieves the value associated with a given key from a collection.
|
|
178
|
+
* @param {K} key - K (the type of the key) - This parameter represents the key that is being
|
|
179
|
+
* searched for in the collection.
|
|
180
|
+
* @returns The `get` method returns the value associated with the specified key if it exists in the
|
|
181
|
+
* collection, otherwise it returns `undefined`.
|
|
182
|
+
*/
|
|
183
|
+
get(key) {
|
|
184
|
+
for (const item of this) {
|
|
185
|
+
const [itemKey, value] = item;
|
|
186
|
+
if (itemKey === key) return value;
|
|
187
|
+
}
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Time Complexity: O(n)
|
|
192
|
+
* Space Complexity: O(1)
|
|
193
|
+
*
|
|
194
|
+
* The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
|
|
195
|
+
* accumulating a single value.
|
|
196
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
197
|
+
* It takes four arguments: the current accumulator value, the current value of the element, the key
|
|
198
|
+
* of the element, and the index of the element in the collection. It should return the updated
|
|
199
|
+
* accumulator value.
|
|
200
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
201
|
+
* is the value that will be used as the first argument to the `callbackfn` function when reducing
|
|
202
|
+
* the elements of the collection.
|
|
203
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
204
|
+
* all the elements in the collection.
|
|
205
|
+
*/
|
|
206
|
+
reduce(callbackfn, initialValue) {
|
|
207
|
+
let accumulator = initialValue;
|
|
208
|
+
let index = 0;
|
|
209
|
+
for (const item of this) {
|
|
210
|
+
const [key, value] = item;
|
|
211
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
212
|
+
}
|
|
213
|
+
return accumulator;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Time Complexity: O(n)
|
|
217
|
+
* Space Complexity: O(n)
|
|
218
|
+
*
|
|
219
|
+
* The print function logs the elements of an array to the console.
|
|
220
|
+
*/
|
|
221
|
+
toVisual() {
|
|
222
|
+
return [...this];
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Time Complexity: O(n)
|
|
226
|
+
* Space Complexity: O(n)
|
|
227
|
+
*
|
|
228
|
+
* The print function logs the elements of an array to the console.
|
|
229
|
+
*/
|
|
230
|
+
print() {
|
|
231
|
+
console.log(this.toVisual());
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// src/utils/utils.ts
|
|
236
|
+
var THUNK_SYMBOL = Symbol("thunk");
|
|
237
|
+
var rangeCheck = (index, min, max, message = "Index out of bounds.") => {
|
|
238
|
+
if (index < min || index > max) throw new RangeError(message);
|
|
239
|
+
};
|
|
240
|
+
var isWeakKey = (input) => {
|
|
241
|
+
const inputType = typeof input;
|
|
242
|
+
return inputType === "object" && input !== null || inputType === "function";
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// src/data-structures/hash/hash-map.ts
|
|
246
|
+
var HashMap = class _HashMap extends IterableEntryBase {
|
|
247
|
+
/**
|
|
248
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
249
|
+
* options.
|
|
250
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
251
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
252
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
253
|
+
*/
|
|
254
|
+
constructor(entryOrRawElements = [], options) {
|
|
255
|
+
super();
|
|
256
|
+
if (options) {
|
|
257
|
+
const { hashFn, toEntryFn } = options;
|
|
258
|
+
if (hashFn) this._hashFn = hashFn;
|
|
259
|
+
if (toEntryFn) this._toEntryFn = toEntryFn;
|
|
260
|
+
}
|
|
261
|
+
if (entryOrRawElements) {
|
|
262
|
+
this.setMany(entryOrRawElements);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
_store = {};
|
|
266
|
+
/**
|
|
267
|
+
* The function returns the store object, which is a dictionary of HashMapStoreItem objects.
|
|
268
|
+
* @returns The store property is being returned. It is a dictionary-like object with string keys and
|
|
269
|
+
* values of type HashMapStoreItem<K, V>.
|
|
270
|
+
*/
|
|
271
|
+
get store() {
|
|
272
|
+
return this._store;
|
|
273
|
+
}
|
|
274
|
+
_objMap = /* @__PURE__ */ new Map();
|
|
275
|
+
/**
|
|
276
|
+
* The function returns the object map.
|
|
277
|
+
* @returns The `objMap` property is being returned, which is a `Map` object with keys of type
|
|
278
|
+
* `object` and values of type `V`.
|
|
279
|
+
*/
|
|
280
|
+
get objMap() {
|
|
281
|
+
return this._objMap;
|
|
282
|
+
}
|
|
283
|
+
_toEntryFn;
|
|
284
|
+
/**
|
|
285
|
+
* The function returns the value of the _toEntryFn property.
|
|
286
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
287
|
+
*/
|
|
288
|
+
get toEntryFn() {
|
|
289
|
+
return this._toEntryFn;
|
|
290
|
+
}
|
|
291
|
+
_size = 0;
|
|
292
|
+
/**
|
|
293
|
+
* The function returns the size of an object.
|
|
294
|
+
* @returns The size of the object, which is a number.
|
|
295
|
+
*/
|
|
296
|
+
get size() {
|
|
297
|
+
return this._size;
|
|
298
|
+
}
|
|
299
|
+
_hashFn = (key) => String(key);
|
|
300
|
+
/**
|
|
301
|
+
* The hasFn function is a function that takes in an item and returns a boolean
|
|
302
|
+
* indicating whether the item is contained within the hash table.
|
|
303
|
+
*
|
|
304
|
+
* @return The hash function
|
|
305
|
+
*/
|
|
306
|
+
get hashFn() {
|
|
307
|
+
return this._hashFn;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Time Complexity: O(1)
|
|
311
|
+
* Space Complexity: O(1)
|
|
312
|
+
*
|
|
313
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
314
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
315
|
+
* data type.
|
|
316
|
+
* @returns a boolean value.
|
|
317
|
+
*/
|
|
318
|
+
isEntry(rawElement) {
|
|
319
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Time Complexity: O(1)
|
|
323
|
+
* Space Complexity: O(1)
|
|
324
|
+
*
|
|
325
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
326
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
327
|
+
*/
|
|
328
|
+
isEmpty() {
|
|
329
|
+
return this._size === 0;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Time Complexity: O(1)
|
|
333
|
+
* Space Complexity: O(1)
|
|
334
|
+
*
|
|
335
|
+
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
336
|
+
* size.
|
|
337
|
+
*/
|
|
338
|
+
clear() {
|
|
339
|
+
this._store = {};
|
|
340
|
+
this._objMap.clear();
|
|
341
|
+
this._size = 0;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Time Complexity: O(1)
|
|
345
|
+
* Space Complexity: O(1)
|
|
346
|
+
*
|
|
347
|
+
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
348
|
+
* the key is not already present.
|
|
349
|
+
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
350
|
+
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be
|
|
351
|
+
* stored in a regular JavaScript object.
|
|
352
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
353
|
+
* key in the data structure.
|
|
354
|
+
*/
|
|
355
|
+
set(key, value) {
|
|
356
|
+
if (this._isObjKey(key)) {
|
|
357
|
+
if (!this.objMap.has(key)) {
|
|
358
|
+
this._size++;
|
|
359
|
+
}
|
|
360
|
+
this.objMap.set(key, value);
|
|
361
|
+
} else {
|
|
362
|
+
const strKey = this._getNoObjKey(key);
|
|
363
|
+
if (this.store[strKey] === void 0) {
|
|
364
|
+
this._size++;
|
|
365
|
+
}
|
|
366
|
+
this._store[strKey] = { key, value };
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Time Complexity: O(k)
|
|
372
|
+
* Space Complexity: O(k)
|
|
373
|
+
*
|
|
374
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
375
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
376
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
377
|
+
* `T`.
|
|
378
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
379
|
+
*/
|
|
380
|
+
setMany(entryOrRawElements) {
|
|
381
|
+
const results = [];
|
|
382
|
+
for (const rawEle of entryOrRawElements) {
|
|
383
|
+
let key, value;
|
|
384
|
+
if (this.isEntry(rawEle)) {
|
|
385
|
+
key = rawEle[0];
|
|
386
|
+
value = rawEle[1];
|
|
387
|
+
} else if (this._toEntryFn) {
|
|
388
|
+
const item = this._toEntryFn(rawEle);
|
|
389
|
+
key = item[0];
|
|
390
|
+
value = item[1];
|
|
391
|
+
}
|
|
392
|
+
if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
|
|
393
|
+
}
|
|
394
|
+
return results;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Time Complexity: O(1)
|
|
398
|
+
* Space Complexity: O(1)
|
|
399
|
+
*
|
|
400
|
+
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
401
|
+
* a string map.
|
|
402
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
403
|
+
* of any type, but it should be compatible with the key type used when the map was created.
|
|
404
|
+
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
|
|
405
|
+
* or `_store`, otherwise it returns `undefined`.
|
|
406
|
+
*/
|
|
407
|
+
get(key) {
|
|
408
|
+
if (this._isObjKey(key)) {
|
|
409
|
+
return this.objMap.get(key);
|
|
410
|
+
} else {
|
|
411
|
+
const strKey = this._getNoObjKey(key);
|
|
412
|
+
return this._store[strKey]?.value;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Time Complexity: O(1)
|
|
417
|
+
* Space Complexity: O(1)
|
|
418
|
+
*
|
|
419
|
+
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
420
|
+
* is an object key or not.
|
|
421
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
422
|
+
* @returns The `has` method is returning a boolean value.
|
|
423
|
+
*/
|
|
424
|
+
has(key) {
|
|
425
|
+
if (this._isObjKey(key)) {
|
|
426
|
+
return this.objMap.has(key);
|
|
427
|
+
} else {
|
|
428
|
+
const strKey = this._getNoObjKey(key);
|
|
429
|
+
return strKey in this.store;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Time Complexity: O(1)
|
|
434
|
+
* Space Complexity: O(1)
|
|
435
|
+
*
|
|
436
|
+
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
437
|
+
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
438
|
+
* data structure.
|
|
439
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was
|
|
440
|
+
* successfully deleted from the map, and `false` if the key was not found in the map.
|
|
441
|
+
*/
|
|
442
|
+
delete(key) {
|
|
443
|
+
if (this._isObjKey(key)) {
|
|
444
|
+
if (this.objMap.has(key)) {
|
|
445
|
+
this._size--;
|
|
446
|
+
}
|
|
447
|
+
return this.objMap.delete(key);
|
|
448
|
+
} else {
|
|
449
|
+
const strKey = this._getNoObjKey(key);
|
|
450
|
+
if (strKey in this.store) {
|
|
451
|
+
delete this.store[strKey];
|
|
452
|
+
this._size--;
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Time Complexity: O(n)
|
|
460
|
+
* Space Complexity: O(n)
|
|
461
|
+
*
|
|
462
|
+
* The clone function creates a new HashMap with the same key-value pairs as
|
|
463
|
+
* this one. The clone function is useful for creating a copy of an existing
|
|
464
|
+
* HashMap, and then modifying that copy without affecting the original.
|
|
465
|
+
*
|
|
466
|
+
* @return A new hashmap with the same values as this one
|
|
467
|
+
*/
|
|
468
|
+
clone() {
|
|
469
|
+
return new _HashMap(this, { hashFn: this._hashFn, toEntryFn: this._toEntryFn });
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Time Complexity: O(n)
|
|
473
|
+
* Space Complexity: O(n)
|
|
474
|
+
*
|
|
475
|
+
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each
|
|
476
|
+
* key-value pair in the original HashMap.
|
|
477
|
+
* @param callbackfn - The callback function that will be called for each key-value pair in the
|
|
478
|
+
* HashMap. It takes four parameters:
|
|
479
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
480
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
481
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
482
|
+
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
483
|
+
* the provided callback function.
|
|
484
|
+
*/
|
|
485
|
+
map(callbackfn, thisArg) {
|
|
486
|
+
const resultMap = new _HashMap();
|
|
487
|
+
let index = 0;
|
|
488
|
+
for (const [key, value] of this) {
|
|
489
|
+
resultMap.set(key, callbackfn.call(thisArg, key, value, index++, this));
|
|
490
|
+
}
|
|
491
|
+
return resultMap;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Time Complexity: O(n)
|
|
495
|
+
* Space Complexity: O(n)
|
|
496
|
+
*
|
|
497
|
+
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
|
|
498
|
+
* that satisfy a given predicate function.
|
|
499
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
500
|
+
* index, and map. It is used to determine whether an element should be included in the filtered map
|
|
501
|
+
* or not. The function should return a boolean value - true if the element should be included, and
|
|
502
|
+
* false otherwise.
|
|
503
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
504
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
505
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
506
|
+
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
507
|
+
* from the original `HashMap` that pass the provided `predicate` function.
|
|
508
|
+
*/
|
|
509
|
+
filter(predicate, thisArg) {
|
|
510
|
+
const filteredMap = new _HashMap();
|
|
511
|
+
let index = 0;
|
|
512
|
+
for (const [key, value] of this) {
|
|
513
|
+
if (predicate.call(thisArg, key, value, index++, this)) {
|
|
514
|
+
filteredMap.set(key, value);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return filteredMap;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
521
|
+
* object map.
|
|
522
|
+
*/
|
|
523
|
+
*_getIterator() {
|
|
524
|
+
for (const node of Object.values(this.store)) {
|
|
525
|
+
yield [node.key, node.value];
|
|
526
|
+
}
|
|
527
|
+
for (const node of this.objMap) {
|
|
528
|
+
yield node;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* The function checks if a given key is an object or a function.
|
|
533
|
+
* @param {any} key - The parameter "key" can be of any type.
|
|
534
|
+
* @returns a boolean value.
|
|
535
|
+
*/
|
|
536
|
+
_isObjKey(key) {
|
|
537
|
+
const keyType = typeof key;
|
|
538
|
+
return (keyType === "object" || keyType === "function") && key !== null;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* The function `_getNoObjKey` takes a key and returns a string representation of the key, handling
|
|
542
|
+
* different types of keys.
|
|
543
|
+
* @param {K} key - The `key` parameter is of type `K`, which represents the type of the key being
|
|
544
|
+
* passed to the `_getNoObjKey` function.
|
|
545
|
+
* @returns a string value.
|
|
546
|
+
*/
|
|
547
|
+
_getNoObjKey(key) {
|
|
548
|
+
const keyType = typeof key;
|
|
549
|
+
let strKey;
|
|
550
|
+
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
|
|
551
|
+
strKey = this._hashFn(key);
|
|
552
|
+
} else {
|
|
553
|
+
if (keyType === "number") {
|
|
554
|
+
strKey = key;
|
|
555
|
+
} else {
|
|
556
|
+
strKey = key;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return strKey;
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
var LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
563
|
+
_sentinel;
|
|
564
|
+
/**
|
|
565
|
+
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
566
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements. It is
|
|
567
|
+
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
|
|
568
|
+
* `entryOrRawElements` is converted to a key-value pair using the `toEntryFn` function (if provided) and
|
|
569
|
+
* then added to the HashMap
|
|
570
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
571
|
+
* properties:
|
|
572
|
+
*/
|
|
573
|
+
constructor(entryOrRawElements = [], options) {
|
|
574
|
+
super();
|
|
575
|
+
this._sentinel = {};
|
|
576
|
+
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
577
|
+
if (options) {
|
|
578
|
+
const { hashFn, objHashFn, toEntryFn } = options;
|
|
579
|
+
if (hashFn) this._hashFn = hashFn;
|
|
580
|
+
if (objHashFn) this._objHashFn = objHashFn;
|
|
581
|
+
if (toEntryFn) {
|
|
582
|
+
this._toEntryFn = toEntryFn;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
if (entryOrRawElements) {
|
|
586
|
+
this.setMany(entryOrRawElements);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
_hashFn = (key) => String(key);
|
|
590
|
+
/**
|
|
591
|
+
* The function returns the hash function used for generating a hash value for a given key.
|
|
592
|
+
* @returns The hash function that takes a key of type K and returns a string.
|
|
593
|
+
*/
|
|
594
|
+
get hashFn() {
|
|
595
|
+
return this._hashFn;
|
|
596
|
+
}
|
|
597
|
+
_objHashFn = (key) => key;
|
|
598
|
+
/**
|
|
599
|
+
* The function returns the object hash function.
|
|
600
|
+
* @returns The function `objHashFn` is being returned.
|
|
601
|
+
*/
|
|
602
|
+
get objHashFn() {
|
|
603
|
+
return this._objHashFn;
|
|
604
|
+
}
|
|
605
|
+
_noObjMap = {};
|
|
606
|
+
/**
|
|
607
|
+
* The function returns a record of HashMapLinkedNode objects with string keys.
|
|
608
|
+
* @returns The method is returning a Record object, which is a TypeScript type that represents an
|
|
609
|
+
* object with string keys and values that are HashMapLinkedNode objects with keys of type K and
|
|
610
|
+
* values of type V or undefined.
|
|
611
|
+
*/
|
|
612
|
+
get noObjMap() {
|
|
613
|
+
return this._noObjMap;
|
|
614
|
+
}
|
|
615
|
+
_objMap = /* @__PURE__ */ new WeakMap();
|
|
616
|
+
/**
|
|
617
|
+
* The function returns the WeakMap object used to map objects to HashMapLinkedNode instances.
|
|
618
|
+
* @returns The `objMap` property is being returned.
|
|
619
|
+
*/
|
|
620
|
+
get objMap() {
|
|
621
|
+
return this._objMap;
|
|
622
|
+
}
|
|
623
|
+
_head;
|
|
624
|
+
/**
|
|
625
|
+
* The function returns the head node of a HashMapLinkedNode.
|
|
626
|
+
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and
|
|
627
|
+
* a value type `V | undefined`.
|
|
628
|
+
*/
|
|
629
|
+
get head() {
|
|
630
|
+
return this._head;
|
|
631
|
+
}
|
|
632
|
+
_tail;
|
|
633
|
+
/**
|
|
634
|
+
* The function returns the tail node of a HashMapLinkedNode.
|
|
635
|
+
* @returns The `_tail` property of type `HashMapLinkedNode<K, V | undefined>` is being returned.
|
|
636
|
+
*/
|
|
637
|
+
get tail() {
|
|
638
|
+
return this._tail;
|
|
639
|
+
}
|
|
640
|
+
_toEntryFn = (rawElement) => {
|
|
641
|
+
if (this.isEntry(rawElement)) {
|
|
642
|
+
return rawElement;
|
|
643
|
+
} else {
|
|
644
|
+
throw new Error(
|
|
645
|
+
"If the provided entryOrRawElements does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
/**
|
|
650
|
+
* The function returns the value of the _toEntryFn property.
|
|
651
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
652
|
+
*/
|
|
653
|
+
get toEntryFn() {
|
|
654
|
+
return this._toEntryFn;
|
|
655
|
+
}
|
|
656
|
+
_size = 0;
|
|
657
|
+
/**
|
|
658
|
+
* The function returns the size of an object.
|
|
659
|
+
* @returns The size of the object.
|
|
660
|
+
*/
|
|
661
|
+
get size() {
|
|
662
|
+
return this._size;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Time Complexity: O(1)
|
|
666
|
+
* Space Complexity: O(1)
|
|
667
|
+
*
|
|
668
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
669
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
670
|
+
* value (V).
|
|
671
|
+
*/
|
|
672
|
+
get first() {
|
|
673
|
+
if (this._size === 0) return;
|
|
674
|
+
return [this.head.key, this.head.value];
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Time Complexity: O(1)
|
|
678
|
+
* Space Complexity: O(1)
|
|
679
|
+
*
|
|
680
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
681
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
682
|
+
* data structure.
|
|
683
|
+
*/
|
|
684
|
+
get last() {
|
|
685
|
+
if (this._size === 0) return;
|
|
686
|
+
return [this.tail.key, this.tail.value];
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
690
|
+
*/
|
|
691
|
+
*begin() {
|
|
692
|
+
let node = this.head;
|
|
693
|
+
while (node !== this._sentinel) {
|
|
694
|
+
yield [node.key, node.value];
|
|
695
|
+
node = node.next;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
700
|
+
* key and value.
|
|
701
|
+
*/
|
|
702
|
+
*reverseBegin() {
|
|
703
|
+
let node = this.tail;
|
|
704
|
+
while (node !== this._sentinel) {
|
|
705
|
+
yield [node.key, node.value];
|
|
706
|
+
node = node.prev;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Time Complexity: O(1)
|
|
711
|
+
* Space Complexity: O(1)
|
|
712
|
+
*
|
|
713
|
+
* The `set` function adds a new key-value pair to a data structure, either using an object key or a
|
|
714
|
+
* string key.
|
|
715
|
+
* @param {K} key - The `key` parameter is the key to be set in the data structure. It can be of any
|
|
716
|
+
* type, but typically it is a string or symbol.
|
|
717
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
718
|
+
* value associated with the key being set in the data structure.
|
|
719
|
+
* @returns the size of the data structure after the key-value pair has been set.
|
|
720
|
+
*/
|
|
721
|
+
set(key, value) {
|
|
722
|
+
let node;
|
|
723
|
+
const isNewKey = !this.has(key);
|
|
724
|
+
if (isWeakKey(key)) {
|
|
725
|
+
const hash = this._objHashFn(key);
|
|
726
|
+
node = this.objMap.get(hash);
|
|
727
|
+
if (!node && isNewKey) {
|
|
728
|
+
node = { key: hash, value, prev: this.tail, next: this._sentinel };
|
|
729
|
+
this.objMap.set(hash, node);
|
|
730
|
+
} else if (node) {
|
|
731
|
+
node.value = value;
|
|
732
|
+
}
|
|
733
|
+
} else {
|
|
734
|
+
const hash = this._hashFn(key);
|
|
735
|
+
node = this.noObjMap[hash];
|
|
736
|
+
if (!node && isNewKey) {
|
|
737
|
+
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
|
|
738
|
+
} else if (node) {
|
|
739
|
+
node.value = value;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
if (node && isNewKey) {
|
|
743
|
+
if (this._size === 0) {
|
|
744
|
+
this._head = node;
|
|
745
|
+
this._sentinel.next = node;
|
|
746
|
+
} else {
|
|
747
|
+
this.tail.next = node;
|
|
748
|
+
node.prev = this.tail;
|
|
749
|
+
}
|
|
750
|
+
this._tail = node;
|
|
751
|
+
this._sentinel.prev = node;
|
|
752
|
+
this._size++;
|
|
753
|
+
}
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Time Complexity: O(k)
|
|
758
|
+
* Space Complexity: O(k)
|
|
759
|
+
*
|
|
760
|
+
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
761
|
+
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
762
|
+
* of booleans indicating the success of each set operation.
|
|
763
|
+
* @param entryOrRawElements - The entryOrRawElements parameter is an iterable collection of elements of type
|
|
764
|
+
* R.
|
|
765
|
+
* @returns The `setMany` function returns an array of booleans.
|
|
766
|
+
*/
|
|
767
|
+
setMany(entryOrRawElements) {
|
|
768
|
+
const results = [];
|
|
769
|
+
for (const rawEle of entryOrRawElements) {
|
|
770
|
+
let key, value;
|
|
771
|
+
if (this.isEntry(rawEle)) {
|
|
772
|
+
key = rawEle[0];
|
|
773
|
+
value = rawEle[1];
|
|
774
|
+
} else if (this._toEntryFn) {
|
|
775
|
+
const item = this._toEntryFn(rawEle);
|
|
776
|
+
key = item[0];
|
|
777
|
+
value = item[1];
|
|
778
|
+
}
|
|
779
|
+
if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
|
|
780
|
+
}
|
|
781
|
+
return results;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Time Complexity: O(1)
|
|
785
|
+
* Space Complexity: O(1)
|
|
786
|
+
*
|
|
787
|
+
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
788
|
+
* key is a weak key or not.
|
|
789
|
+
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
790
|
+
* @returns The method `has` is returning a boolean value.
|
|
791
|
+
*/
|
|
792
|
+
has(key) {
|
|
793
|
+
if (isWeakKey(key)) {
|
|
794
|
+
const hash = this._objHashFn(key);
|
|
795
|
+
return this.objMap.has(hash);
|
|
796
|
+
} else {
|
|
797
|
+
const hash = this._hashFn(key);
|
|
798
|
+
return hash in this.noObjMap;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Time Complexity: O(1)
|
|
803
|
+
* Space Complexity: O(1)
|
|
804
|
+
*
|
|
805
|
+
* The function `get` retrieves the value associated with a given key from a map, either by using the
|
|
806
|
+
* key directly or by using an index stored in the key object.
|
|
807
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
808
|
+
* of any type, but typically it is a string or symbol.
|
|
809
|
+
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
810
|
+
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
811
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
812
|
+
* using the key itself. If the key is not found, `undefined` is
|
|
813
|
+
*/
|
|
814
|
+
get(key) {
|
|
815
|
+
if (isWeakKey(key)) {
|
|
816
|
+
const hash = this._objHashFn(key);
|
|
817
|
+
const node = this.objMap.get(hash);
|
|
818
|
+
return node ? node.value : void 0;
|
|
819
|
+
} else {
|
|
820
|
+
const hash = this._hashFn(key);
|
|
821
|
+
const node = this.noObjMap[hash];
|
|
822
|
+
return node ? node.value : void 0;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Time Complexity: O(n)
|
|
827
|
+
* Space Complexity: O(1)
|
|
828
|
+
*
|
|
829
|
+
* The function `at` retrieves the key-value pair at a specified index in a linked list.
|
|
830
|
+
* @param {number} index - The index parameter is a number that represents the position of the
|
|
831
|
+
* element we want to retrieve from the data structure.
|
|
832
|
+
* @returns The method `at(index: number)` is returning an array containing the key-value pair at
|
|
833
|
+
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
834
|
+
* where `K` is the key and `V` is the value.
|
|
835
|
+
*/
|
|
836
|
+
at(index) {
|
|
837
|
+
rangeCheck(index, 0, this._size - 1);
|
|
838
|
+
let node = this.head;
|
|
839
|
+
while (index--) {
|
|
840
|
+
node = node.next;
|
|
841
|
+
}
|
|
842
|
+
return node.value;
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Time Complexity: O(1)
|
|
846
|
+
* Space Complexity: O(1)
|
|
847
|
+
*
|
|
848
|
+
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
849
|
+
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
850
|
+
* It can be of any type, but typically it is a string or an object.
|
|
851
|
+
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
852
|
+
* was not found.
|
|
853
|
+
*/
|
|
854
|
+
delete(key) {
|
|
855
|
+
let node;
|
|
856
|
+
if (isWeakKey(key)) {
|
|
857
|
+
const hash = this._objHashFn(key);
|
|
858
|
+
node = this.objMap.get(hash);
|
|
859
|
+
if (!node) {
|
|
860
|
+
return false;
|
|
861
|
+
}
|
|
862
|
+
this.objMap.delete(hash);
|
|
863
|
+
} else {
|
|
864
|
+
const hash = this._hashFn(key);
|
|
865
|
+
node = this.noObjMap[hash];
|
|
866
|
+
if (!node) {
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
869
|
+
delete this.noObjMap[hash];
|
|
870
|
+
}
|
|
871
|
+
this._deleteNode(node);
|
|
872
|
+
return true;
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Time Complexity: O(n)
|
|
876
|
+
* Space Complexity: O(1)
|
|
877
|
+
*
|
|
878
|
+
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
879
|
+
* @param {number} index - The index parameter represents the position at which the node should be
|
|
880
|
+
* deleted in the linked list.
|
|
881
|
+
* @returns The size of the list after deleting the element at the specified index.
|
|
882
|
+
*/
|
|
883
|
+
deleteAt(index) {
|
|
884
|
+
rangeCheck(index, 0, this._size - 1);
|
|
885
|
+
let node = this.head;
|
|
886
|
+
while (index--) {
|
|
887
|
+
node = node.next;
|
|
888
|
+
}
|
|
889
|
+
return this._deleteNode(node);
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Time Complexity: O(1)
|
|
893
|
+
* Space Complexity: O(1)
|
|
894
|
+
*
|
|
895
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
896
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
897
|
+
* not.
|
|
898
|
+
*/
|
|
899
|
+
isEmpty() {
|
|
900
|
+
return this._size === 0;
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
904
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
905
|
+
* data type.
|
|
906
|
+
* @returns a boolean value.
|
|
907
|
+
*/
|
|
908
|
+
isEntry(rawElement) {
|
|
909
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* Time Complexity: O(1)
|
|
913
|
+
* Space Complexity: O(1)
|
|
914
|
+
*
|
|
915
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
916
|
+
*/
|
|
917
|
+
clear() {
|
|
918
|
+
this._noObjMap = {};
|
|
919
|
+
this._size = 0;
|
|
920
|
+
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Time Complexity: O(n)
|
|
924
|
+
* Space Complexity: O(n)
|
|
925
|
+
*
|
|
926
|
+
* The `clone` function creates a new instance of a `LinkedHashMap` with the same key-value pairs as
|
|
927
|
+
* the original.
|
|
928
|
+
* @returns The `clone()` method is returning a new instance of `LinkedHashMap<K, V>` that is a clone
|
|
929
|
+
* of the original `LinkedHashMap` object.
|
|
930
|
+
*/
|
|
931
|
+
clone() {
|
|
932
|
+
const cloned = new _LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
|
933
|
+
for (const entry of this) {
|
|
934
|
+
const [key, value] = entry;
|
|
935
|
+
cloned.set(key, value);
|
|
936
|
+
}
|
|
937
|
+
return cloned;
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Time Complexity: O(n)
|
|
941
|
+
* Space Complexity: O(n)
|
|
942
|
+
*
|
|
943
|
+
* The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
|
|
944
|
+
* map that satisfy a given predicate function.
|
|
945
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
946
|
+
* `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
|
|
947
|
+
* current element should be included in the filtered map or not.
|
|
948
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
949
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
950
|
+
* specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
|
|
951
|
+
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
|
|
952
|
+
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
953
|
+
*/
|
|
954
|
+
filter(predicate, thisArg) {
|
|
955
|
+
const filteredMap = new _LinkedHashMap();
|
|
956
|
+
let index = 0;
|
|
957
|
+
for (const [key, value] of this) {
|
|
958
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
959
|
+
filteredMap.set(key, value);
|
|
960
|
+
}
|
|
961
|
+
index++;
|
|
962
|
+
}
|
|
963
|
+
return filteredMap;
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Time Complexity: O(n)
|
|
967
|
+
* Space Complexity: O(n)
|
|
968
|
+
*
|
|
969
|
+
* The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
|
|
970
|
+
* each key-value pair in the original map.
|
|
971
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
972
|
+
* in the map. It takes four arguments: the value of the current key-value pair, the key of the
|
|
973
|
+
* current key-value pair, the index of the current key-value pair, and the map itself. The callback
|
|
974
|
+
* function should
|
|
975
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
976
|
+
* specify the value of `this` within the callback function. If provided, the callback function will
|
|
977
|
+
* be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
|
|
978
|
+
* map
|
|
979
|
+
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
980
|
+
* function.
|
|
981
|
+
*/
|
|
982
|
+
map(callback, thisArg) {
|
|
983
|
+
const mappedMap = new _LinkedHashMap();
|
|
984
|
+
let index = 0;
|
|
985
|
+
for (const [key, value] of this) {
|
|
986
|
+
const [newKey, newValue] = callback.call(thisArg, key, value, index, this);
|
|
987
|
+
mappedMap.set(newKey, newValue);
|
|
988
|
+
index++;
|
|
989
|
+
}
|
|
990
|
+
return mappedMap;
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Time Complexity: O(n)
|
|
994
|
+
* Space Complexity: O(1)
|
|
995
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
996
|
+
*
|
|
997
|
+
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
998
|
+
*/
|
|
999
|
+
*_getIterator() {
|
|
1000
|
+
let node = this.head;
|
|
1001
|
+
while (node !== this._sentinel) {
|
|
1002
|
+
yield [node.key, node.value];
|
|
1003
|
+
node = node.next;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Time Complexity: O(1)
|
|
1008
|
+
* Space Complexity: O(1)
|
|
1009
|
+
*
|
|
1010
|
+
* The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail
|
|
1011
|
+
* pointers if necessary.
|
|
1012
|
+
* @param node - The `node` parameter is an instance of the `HashMapLinkedNode` class, which
|
|
1013
|
+
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
1014
|
+
* and next nodes in the list.
|
|
1015
|
+
*/
|
|
1016
|
+
_deleteNode(node) {
|
|
1017
|
+
const { prev, next } = node;
|
|
1018
|
+
prev.next = next;
|
|
1019
|
+
next.prev = prev;
|
|
1020
|
+
if (node === this.head) {
|
|
1021
|
+
this._head = next;
|
|
1022
|
+
}
|
|
1023
|
+
if (node === this.tail) {
|
|
1024
|
+
this._tail = prev;
|
|
1025
|
+
}
|
|
1026
|
+
this._size -= 1;
|
|
1027
|
+
return true;
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
export {
|
|
1031
|
+
HashMap,
|
|
1032
|
+
LinkedHashMap
|
|
1033
|
+
};
|
|
1034
|
+
/**
|
|
1035
|
+
* data-structure-typed
|
|
1036
|
+
*
|
|
1037
|
+
* @author Pablo Zeng
|
|
1038
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
1039
|
+
* @license MIT License
|
|
1040
|
+
*/
|