data-structure-typed 1.50.2 → 1.50.4
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/.github/ISSUE_TEMPLATE/bug_report.md +0 -13
- package/CHANGELOG.md +1 -1
- package/README.md +23 -21
- package/README_zh-CN.md +7 -7
- 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 -155
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/cjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +50 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +33 -1
- 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 +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js +51 -20
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/cjs/data-structures/binary-tree/index.js +2 -1
- package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +90 -24
- 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-multi-map.d.ts +200 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +400 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +0 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +126 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/cjs/data-structures/hash/hash-map.js +196 -62
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/cjs/data-structures/heap/heap.js +29 -20
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +83 -25
- 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 +26 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +34 -3
- 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 +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js +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 +95 -21
- package/dist/cjs/data-structures/queue/deque.js +100 -16
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/cjs/data-structures/queue/queue.js +65 -45
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/cjs/data-structures/stack/stack.js +36 -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 +100 -36
- package/dist/cjs/data-structures/trie/trie.js +115 -36
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/index.js +2 -1
- package/dist/cjs/types/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/binary-tree/{tree-multimap.js → tree-multi-map.js} +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/mjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +46 -9
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +34 -2
- 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 +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/mjs/data-structures/binary-tree/bst.js +51 -20
- package/dist/mjs/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/index.js +2 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +91 -25
- 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-multi-map.d.ts +200 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +397 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +0 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +127 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/mjs/data-structures/hash/hash-map.js +204 -70
- package/dist/mjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/mjs/data-structures/heap/heap.js +29 -20
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +86 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +36 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/mjs/data-structures/matrix/matrix.js +1 -1
- 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 +95 -21
- package/dist/mjs/data-structures/queue/deque.js +105 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/mjs/data-structures/queue/queue.js +65 -45
- package/dist/mjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/mjs/data-structures/stack/stack.js +36 -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 +100 -36
- package/dist/mjs/data-structures/trie/trie.js +118 -39
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/index.js +2 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/dist/umd/data-structure-typed.js +1876 -656
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +59 -58
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +59 -20
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +51 -19
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +99 -28
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- 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 +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/test/integration/index.html +2 -2
- package/test/performance/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/{tree-multimap.test.ts → avl-tree-multi-map.test.ts} +55 -55
- package/test/unit/data-structures/binary-tree/overall.test.ts +24 -25
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +706 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +3 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +189 -253
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/queue/queue.test.ts +2 -0
- package/test/unit/unrestricted-interconversion.test.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.js.map +0 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /package/dist/mjs/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -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.
|
|
@@ -47,15 +63,6 @@ export class HashMap extends IterableEntryBase {
|
|
|
47
63
|
get toEntryFn() {
|
|
48
64
|
return this._toEntryFn;
|
|
49
65
|
}
|
|
50
|
-
/**
|
|
51
|
-
* The hasFn function is a function that takes in an item and returns a boolean
|
|
52
|
-
* indicating whether the item is contained within the hash table.
|
|
53
|
-
*
|
|
54
|
-
* @return The hash function
|
|
55
|
-
*/
|
|
56
|
-
get hasFn() {
|
|
57
|
-
return this._hashFn;
|
|
58
|
-
}
|
|
59
66
|
_size = 0;
|
|
60
67
|
/**
|
|
61
68
|
* The function returns the size of an object.
|
|
@@ -64,6 +71,16 @@ export class HashMap extends IterableEntryBase {
|
|
|
64
71
|
get size() {
|
|
65
72
|
return this._size;
|
|
66
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
|
+
}
|
|
67
84
|
/**
|
|
68
85
|
* The function checks if a given element is an array with exactly two elements.
|
|
69
86
|
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
@@ -100,14 +117,14 @@ export class HashMap extends IterableEntryBase {
|
|
|
100
117
|
*/
|
|
101
118
|
set(key, value) {
|
|
102
119
|
if (this._isObjKey(key)) {
|
|
103
|
-
if (!this.
|
|
120
|
+
if (!this.objMap.has(key)) {
|
|
104
121
|
this._size++;
|
|
105
122
|
}
|
|
106
|
-
this.
|
|
123
|
+
this.objMap.set(key, value);
|
|
107
124
|
}
|
|
108
125
|
else {
|
|
109
126
|
const strKey = this._getNoObjKey(key);
|
|
110
|
-
if (this.
|
|
127
|
+
if (this.store[strKey] === undefined) {
|
|
111
128
|
this._size++;
|
|
112
129
|
}
|
|
113
130
|
this._store[strKey] = { key, value };
|
|
@@ -148,7 +165,7 @@ export class HashMap extends IterableEntryBase {
|
|
|
148
165
|
*/
|
|
149
166
|
get(key) {
|
|
150
167
|
if (this._isObjKey(key)) {
|
|
151
|
-
return this.
|
|
168
|
+
return this.objMap.get(key);
|
|
152
169
|
}
|
|
153
170
|
else {
|
|
154
171
|
const strKey = this._getNoObjKey(key);
|
|
@@ -163,11 +180,11 @@ export class HashMap extends IterableEntryBase {
|
|
|
163
180
|
*/
|
|
164
181
|
has(key) {
|
|
165
182
|
if (this._isObjKey(key)) {
|
|
166
|
-
return this.
|
|
183
|
+
return this.objMap.has(key);
|
|
167
184
|
}
|
|
168
185
|
else {
|
|
169
186
|
const strKey = this._getNoObjKey(key);
|
|
170
|
-
return strKey in this.
|
|
187
|
+
return strKey in this.store;
|
|
171
188
|
}
|
|
172
189
|
}
|
|
173
190
|
/**
|
|
@@ -179,21 +196,25 @@ export class HashMap extends IterableEntryBase {
|
|
|
179
196
|
*/
|
|
180
197
|
delete(key) {
|
|
181
198
|
if (this._isObjKey(key)) {
|
|
182
|
-
if (this.
|
|
199
|
+
if (this.objMap.has(key)) {
|
|
183
200
|
this._size--;
|
|
184
201
|
}
|
|
185
|
-
return this.
|
|
202
|
+
return this.objMap.delete(key);
|
|
186
203
|
}
|
|
187
204
|
else {
|
|
188
205
|
const strKey = this._getNoObjKey(key);
|
|
189
|
-
if (strKey in this.
|
|
190
|
-
delete this.
|
|
206
|
+
if (strKey in this.store) {
|
|
207
|
+
delete this.store[strKey];
|
|
191
208
|
this._size--;
|
|
192
209
|
return true;
|
|
193
210
|
}
|
|
194
211
|
return false;
|
|
195
212
|
}
|
|
196
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Time Complexity: O(n)
|
|
216
|
+
* Space Complexity: O(n)
|
|
217
|
+
*/
|
|
197
218
|
/**
|
|
198
219
|
* The clone function creates a new HashMap with the same key-value pairs as
|
|
199
220
|
* this one. The clone function is useful for creating a copy of an existing
|
|
@@ -202,7 +223,7 @@ export class HashMap extends IterableEntryBase {
|
|
|
202
223
|
* @return A new hashmap with the same values as this one
|
|
203
224
|
*/
|
|
204
225
|
clone() {
|
|
205
|
-
return new HashMap(this, { hashFn: this.
|
|
226
|
+
return new HashMap(this, { hashFn: this.hashFn, toEntryFn: this.toEntryFn });
|
|
206
227
|
}
|
|
207
228
|
/**
|
|
208
229
|
* Time Complexity: O(n)
|
|
@@ -230,10 +251,6 @@ export class HashMap extends IterableEntryBase {
|
|
|
230
251
|
}
|
|
231
252
|
return resultMap;
|
|
232
253
|
}
|
|
233
|
-
/**
|
|
234
|
-
* Time Complexity: O(n)
|
|
235
|
-
* Space Complexity: O(n)
|
|
236
|
-
*/
|
|
237
254
|
/**
|
|
238
255
|
* Time Complexity: O(n)
|
|
239
256
|
* Space Complexity: O(n)
|
|
@@ -276,23 +293,34 @@ export class HashMap extends IterableEntryBase {
|
|
|
276
293
|
* object map.
|
|
277
294
|
*/
|
|
278
295
|
*_getIterator() {
|
|
279
|
-
for (const node of Object.values(this.
|
|
296
|
+
for (const node of Object.values(this.store)) {
|
|
280
297
|
yield [node.key, node.value];
|
|
281
298
|
}
|
|
282
|
-
for (const node of this.
|
|
299
|
+
for (const node of this.objMap) {
|
|
283
300
|
yield node;
|
|
284
301
|
}
|
|
285
302
|
}
|
|
286
|
-
|
|
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
|
+
*/
|
|
287
308
|
_isObjKey(key) {
|
|
288
309
|
const keyType = typeof key;
|
|
289
310
|
return (keyType === 'object' || keyType === 'function') && key !== null;
|
|
290
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
|
+
*/
|
|
291
319
|
_getNoObjKey(key) {
|
|
292
320
|
const keyType = typeof key;
|
|
293
321
|
let strKey;
|
|
294
322
|
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
|
|
295
|
-
strKey = this.
|
|
323
|
+
strKey = this.hashFn(key);
|
|
296
324
|
}
|
|
297
325
|
else {
|
|
298
326
|
if (keyType === 'number') {
|
|
@@ -312,10 +340,6 @@ export class HashMap extends IterableEntryBase {
|
|
|
312
340
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
313
341
|
*/
|
|
314
342
|
export class LinkedHashMap extends IterableEntryBase {
|
|
315
|
-
_noObjMap = {};
|
|
316
|
-
_objMap = new WeakMap();
|
|
317
|
-
_head;
|
|
318
|
-
_tail;
|
|
319
343
|
_sentinel;
|
|
320
344
|
/**
|
|
321
345
|
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
@@ -347,6 +371,57 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
347
371
|
}
|
|
348
372
|
}
|
|
349
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
|
+
}
|
|
350
425
|
_toEntryFn = (rawElement) => {
|
|
351
426
|
if (this.isEntry(rawElement)) {
|
|
352
427
|
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
@@ -371,6 +446,10 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
371
446
|
get size() {
|
|
372
447
|
return this._size;
|
|
373
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* Time Complexity: O(1)
|
|
451
|
+
* Space Complexity: O(1)
|
|
452
|
+
*/
|
|
374
453
|
/**
|
|
375
454
|
* Time Complexity: O(1)
|
|
376
455
|
* Space Complexity: O(1)
|
|
@@ -382,8 +461,12 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
382
461
|
get first() {
|
|
383
462
|
if (this._size === 0)
|
|
384
463
|
return;
|
|
385
|
-
return [this.
|
|
464
|
+
return [this.head.key, this.head.value];
|
|
386
465
|
}
|
|
466
|
+
/**
|
|
467
|
+
* Time Complexity: O(1)
|
|
468
|
+
* Space Complexity: O(1)
|
|
469
|
+
*/
|
|
387
470
|
/**
|
|
388
471
|
* Time Complexity: O(1)
|
|
389
472
|
* Space Complexity: O(1)
|
|
@@ -395,13 +478,13 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
395
478
|
get last() {
|
|
396
479
|
if (this._size === 0)
|
|
397
480
|
return;
|
|
398
|
-
return [this.
|
|
481
|
+
return [this.tail.key, this.tail.value];
|
|
399
482
|
}
|
|
400
483
|
/**
|
|
401
484
|
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
402
485
|
*/
|
|
403
486
|
*begin() {
|
|
404
|
-
let node = this.
|
|
487
|
+
let node = this.head;
|
|
405
488
|
while (node !== this._sentinel) {
|
|
406
489
|
yield [node.key, node.value];
|
|
407
490
|
node = node.next;
|
|
@@ -412,12 +495,16 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
412
495
|
* key and value.
|
|
413
496
|
*/
|
|
414
497
|
*reverseBegin() {
|
|
415
|
-
let node = this.
|
|
498
|
+
let node = this.tail;
|
|
416
499
|
while (node !== this._sentinel) {
|
|
417
500
|
yield [node.key, node.value];
|
|
418
501
|
node = node.prev;
|
|
419
502
|
}
|
|
420
503
|
}
|
|
504
|
+
/**
|
|
505
|
+
* Time Complexity: O(1)
|
|
506
|
+
* Space Complexity: O(1)
|
|
507
|
+
*/
|
|
421
508
|
/**
|
|
422
509
|
* Time Complexity: O(1)
|
|
423
510
|
* Space Complexity: O(1)
|
|
@@ -434,12 +521,12 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
434
521
|
let node;
|
|
435
522
|
const isNewKey = !this.has(key); // Check if the key is new
|
|
436
523
|
if (isWeakKey(key)) {
|
|
437
|
-
const hash = this.
|
|
438
|
-
node = this.
|
|
524
|
+
const hash = this.objHashFn(key);
|
|
525
|
+
node = this.objMap.get(hash);
|
|
439
526
|
if (!node && isNewKey) {
|
|
440
527
|
// Create new node
|
|
441
|
-
node = { key: hash, value, prev: this.
|
|
442
|
-
this.
|
|
528
|
+
node = { key: hash, value, prev: this.tail, next: this._sentinel };
|
|
529
|
+
this.objMap.set(hash, node);
|
|
443
530
|
}
|
|
444
531
|
else if (node) {
|
|
445
532
|
// Update the value of an existing node
|
|
@@ -447,10 +534,10 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
447
534
|
}
|
|
448
535
|
}
|
|
449
536
|
else {
|
|
450
|
-
const hash = this.
|
|
451
|
-
node = this.
|
|
537
|
+
const hash = this.hashFn(key);
|
|
538
|
+
node = this.noObjMap[hash];
|
|
452
539
|
if (!node && isNewKey) {
|
|
453
|
-
this.
|
|
540
|
+
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
|
|
454
541
|
}
|
|
455
542
|
else if (node) {
|
|
456
543
|
// Update the value of an existing node
|
|
@@ -464,8 +551,8 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
464
551
|
this._sentinel.next = node;
|
|
465
552
|
}
|
|
466
553
|
else {
|
|
467
|
-
this.
|
|
468
|
-
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
|
|
469
556
|
}
|
|
470
557
|
this._tail = node;
|
|
471
558
|
this._sentinel.prev = node;
|
|
@@ -497,14 +584,18 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
497
584
|
*/
|
|
498
585
|
has(key) {
|
|
499
586
|
if (isWeakKey(key)) {
|
|
500
|
-
const hash = this.
|
|
501
|
-
return this.
|
|
587
|
+
const hash = this.objHashFn(key);
|
|
588
|
+
return this.objMap.has(hash);
|
|
502
589
|
}
|
|
503
590
|
else {
|
|
504
|
-
const hash = this.
|
|
505
|
-
return hash in this.
|
|
591
|
+
const hash = this.hashFn(key);
|
|
592
|
+
return hash in this.noObjMap;
|
|
506
593
|
}
|
|
507
594
|
}
|
|
595
|
+
/**
|
|
596
|
+
* Time Complexity: O(1)
|
|
597
|
+
* Space Complexity: O(1)
|
|
598
|
+
*/
|
|
508
599
|
/**
|
|
509
600
|
* Time Complexity: O(1)
|
|
510
601
|
* Space Complexity: O(1)
|
|
@@ -520,18 +611,23 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
520
611
|
*/
|
|
521
612
|
get(key) {
|
|
522
613
|
if (isWeakKey(key)) {
|
|
523
|
-
const hash = this.
|
|
524
|
-
const node = this.
|
|
614
|
+
const hash = this.objHashFn(key);
|
|
615
|
+
const node = this.objMap.get(hash);
|
|
525
616
|
return node ? node.value : undefined;
|
|
526
617
|
}
|
|
527
618
|
else {
|
|
528
|
-
const hash = this.
|
|
529
|
-
const node = this.
|
|
619
|
+
const hash = this.hashFn(key);
|
|
620
|
+
const node = this.noObjMap[hash];
|
|
530
621
|
return node ? node.value : undefined;
|
|
531
622
|
}
|
|
532
623
|
}
|
|
533
624
|
/**
|
|
534
|
-
* Time Complexity: O(n)
|
|
625
|
+
* Time Complexity: O(n)
|
|
626
|
+
* Space Complexity: O(1)
|
|
627
|
+
* /
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Time Complexity: O(n)
|
|
535
631
|
* Space Complexity: O(1)
|
|
536
632
|
*
|
|
537
633
|
* The function `at` retrieves the key-value pair at a specified index in a linked list.
|
|
@@ -543,13 +639,18 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
543
639
|
*/
|
|
544
640
|
at(index) {
|
|
545
641
|
rangeCheck(index, 0, this._size - 1);
|
|
546
|
-
let node = this.
|
|
642
|
+
let node = this.head;
|
|
547
643
|
while (index--) {
|
|
548
644
|
node = node.next;
|
|
549
645
|
}
|
|
550
646
|
return node.value;
|
|
551
647
|
}
|
|
552
648
|
/**
|
|
649
|
+
* Time Complexity: O(1)
|
|
650
|
+
* Space Complexity: O(1)
|
|
651
|
+
* /
|
|
652
|
+
|
|
653
|
+
/**
|
|
553
654
|
* Time Complexity: O(1)
|
|
554
655
|
* Space Complexity: O(1)
|
|
555
656
|
*
|
|
@@ -562,30 +663,35 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
562
663
|
delete(key) {
|
|
563
664
|
let node;
|
|
564
665
|
if (isWeakKey(key)) {
|
|
565
|
-
const hash = this.
|
|
666
|
+
const hash = this.objHashFn(key);
|
|
566
667
|
// Get nodes from WeakMap
|
|
567
|
-
node = this.
|
|
668
|
+
node = this.objMap.get(hash);
|
|
568
669
|
if (!node) {
|
|
569
670
|
return false; // If the node does not exist, return false
|
|
570
671
|
}
|
|
571
672
|
// Remove nodes from WeakMap
|
|
572
|
-
this.
|
|
673
|
+
this.objMap.delete(hash);
|
|
573
674
|
}
|
|
574
675
|
else {
|
|
575
|
-
const hash = this.
|
|
676
|
+
const hash = this.hashFn(key);
|
|
576
677
|
// Get nodes from noObjMap
|
|
577
|
-
node = this.
|
|
678
|
+
node = this.noObjMap[hash];
|
|
578
679
|
if (!node) {
|
|
579
680
|
return false; // If the node does not exist, return false
|
|
580
681
|
}
|
|
581
682
|
// Remove nodes from orgMap
|
|
582
|
-
delete this.
|
|
683
|
+
delete this.noObjMap[hash];
|
|
583
684
|
}
|
|
584
685
|
// Remove node from doubly linked list
|
|
585
686
|
this._deleteNode(node);
|
|
586
687
|
return true;
|
|
587
688
|
}
|
|
588
689
|
/**
|
|
690
|
+
* Time Complexity: O(n)
|
|
691
|
+
* Space Complexity: O(1)
|
|
692
|
+
* /
|
|
693
|
+
|
|
694
|
+
/**
|
|
589
695
|
* Time Complexity: O(n)
|
|
590
696
|
* Space Complexity: O(1)
|
|
591
697
|
*
|
|
@@ -596,13 +702,18 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
596
702
|
*/
|
|
597
703
|
deleteAt(index) {
|
|
598
704
|
rangeCheck(index, 0, this._size - 1);
|
|
599
|
-
let node = this.
|
|
705
|
+
let node = this.head;
|
|
600
706
|
while (index--) {
|
|
601
707
|
node = node.next;
|
|
602
708
|
}
|
|
603
709
|
return this._deleteNode(node);
|
|
604
710
|
}
|
|
605
711
|
/**
|
|
712
|
+
* Time Complexity: O(1)
|
|
713
|
+
* Space Complexity: O(1)
|
|
714
|
+
* /
|
|
715
|
+
|
|
716
|
+
/**
|
|
606
717
|
* Time Complexity: O(1)
|
|
607
718
|
* Space Complexity: O(1)
|
|
608
719
|
*
|
|
@@ -623,6 +734,11 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
623
734
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
624
735
|
}
|
|
625
736
|
/**
|
|
737
|
+
* Time Complexity: O(1)
|
|
738
|
+
* Space Complexity: O(1)
|
|
739
|
+
* /
|
|
740
|
+
|
|
741
|
+
/**
|
|
626
742
|
* Time Complexity: O(1)
|
|
627
743
|
* Space Complexity: O(1)
|
|
628
744
|
*
|
|
@@ -647,7 +763,7 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
647
763
|
* of the original `LinkedHashMap` object.
|
|
648
764
|
*/
|
|
649
765
|
clone() {
|
|
650
|
-
const cloned = new LinkedHashMap([], { hashFn: this.
|
|
766
|
+
const cloned = new LinkedHashMap([], { hashFn: this.hashFn, objHashFn: this.objHashFn });
|
|
651
767
|
for (const entry of this) {
|
|
652
768
|
const [key, value] = entry;
|
|
653
769
|
cloned.set(key, value);
|
|
@@ -655,6 +771,11 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
655
771
|
return cloned;
|
|
656
772
|
}
|
|
657
773
|
/**
|
|
774
|
+
* Time Complexity: O(n)
|
|
775
|
+
* Space Complexity: O(n)
|
|
776
|
+
* /
|
|
777
|
+
|
|
778
|
+
/**
|
|
658
779
|
* Time Complexity: O(n)
|
|
659
780
|
* Space Complexity: O(n)
|
|
660
781
|
*
|
|
@@ -681,6 +802,11 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
681
802
|
return filteredMap;
|
|
682
803
|
}
|
|
683
804
|
/**
|
|
805
|
+
* Time Complexity: O(n)
|
|
806
|
+
* Space Complexity: O(n)
|
|
807
|
+
* /
|
|
808
|
+
|
|
809
|
+
/**
|
|
684
810
|
* Time Complexity: O(n)
|
|
685
811
|
* Space Complexity: O(n)
|
|
686
812
|
*
|
|
@@ -725,9 +851,13 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
725
851
|
put(key, value) {
|
|
726
852
|
return this.set(key, value);
|
|
727
853
|
}
|
|
728
|
-
_hashFn = (key) => String(key);
|
|
729
|
-
_objHashFn = (key) => key;
|
|
730
854
|
/**
|
|
855
|
+
* Time Complexity: O(n)
|
|
856
|
+
* Space Complexity: O(1)
|
|
857
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
858
|
+
* /
|
|
859
|
+
|
|
860
|
+
/**
|
|
731
861
|
* Time Complexity: O(n)
|
|
732
862
|
* Space Complexity: O(1)
|
|
733
863
|
* where n is the number of entries in the LinkedHashMap.
|
|
@@ -735,12 +865,16 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
735
865
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
736
866
|
*/
|
|
737
867
|
*_getIterator() {
|
|
738
|
-
let node = this.
|
|
868
|
+
let node = this.head;
|
|
739
869
|
while (node !== this._sentinel) {
|
|
740
870
|
yield [node.key, node.value];
|
|
741
871
|
node = node.next;
|
|
742
872
|
}
|
|
743
873
|
}
|
|
874
|
+
/**
|
|
875
|
+
* Time Complexity: O(1)
|
|
876
|
+
* Space Complexity: O(1)
|
|
877
|
+
*/
|
|
744
878
|
/**
|
|
745
879
|
* Time Complexity: O(1)
|
|
746
880
|
* Space Complexity: O(1)
|
|
@@ -755,10 +889,10 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
755
889
|
const { prev, next } = node;
|
|
756
890
|
prev.next = next;
|
|
757
891
|
next.prev = prev;
|
|
758
|
-
if (node === this.
|
|
892
|
+
if (node === this.head) {
|
|
759
893
|
this._head = next;
|
|
760
894
|
}
|
|
761
|
-
if (node === this.
|
|
895
|
+
if (node === this.tail) {
|
|
762
896
|
this._tail = prev;
|
|
763
897
|
}
|
|
764
898
|
this._size -= 1;
|