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