directed-graph-typed 1.48.0 → 1.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +28 -19
- package/dist/data-structures/binary-tree/avl-tree.js +22 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
- package/dist/data-structures/binary-tree/binary-tree.js +241 -215
- package/dist/data-structures/binary-tree/bst.d.ts +64 -48
- package/dist/data-structures/binary-tree/bst.js +94 -65
- package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
- package/dist/data-structures/binary-tree/rb-tree.js +42 -49
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
- package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
- package/dist/data-structures/graph/abstract-graph.js +130 -103
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +42 -55
- package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
- package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
- package/dist/data-structures/queue/deque.d.ts +35 -167
- package/dist/data-structures/queue/deque.js +43 -249
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -25
- package/src/data-structures/binary-tree/binary-tree.ts +336 -296
- package/src/data-structures/binary-tree/bst.ts +135 -89
- package/src/data-structures/binary-tree/rb-tree.ts +60 -69
- package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
- package/src/data-structures/graph/abstract-graph.ts +136 -104
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- package/src/data-structures/hash/hash-map.ts +335 -84
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +54 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
- package/src/data-structures/queue/deque.ts +43 -275
- package/src/data-structures/queue/queue.ts +71 -86
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/index.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
|
@@ -36,20 +36,20 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
|
36
36
|
*/
|
|
37
37
|
constructor(v1, v2, weight, value) {
|
|
38
38
|
super(weight, value);
|
|
39
|
-
this.
|
|
39
|
+
this.vertexMap = [v1, v2];
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
exports.UndirectedEdge = UndirectedEdge;
|
|
43
43
|
class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
44
44
|
/**
|
|
45
|
-
* The constructor initializes a new Map object to store
|
|
45
|
+
* The constructor initializes a new Map object to store edgeMap.
|
|
46
46
|
*/
|
|
47
47
|
constructor() {
|
|
48
48
|
super();
|
|
49
|
-
this.
|
|
49
|
+
this._edgeMap = new Map();
|
|
50
50
|
}
|
|
51
|
-
get
|
|
52
|
-
return this.
|
|
51
|
+
get edgeMap() {
|
|
52
|
+
return this._edgeMap;
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The function creates a new vertex with an optional value and returns it.
|
|
@@ -64,7 +64,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
64
64
|
return new UndirectedVertex(key, value !== null && value !== void 0 ? value : key);
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
|
-
* The function creates an undirected edge between two
|
|
67
|
+
* The function creates an undirected edge between two vertexMap with an optional weight and value.
|
|
68
68
|
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
69
69
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
70
70
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
@@ -77,14 +77,14 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
77
77
|
return new UndirectedEdge(v1, v2, weight !== null && weight !== void 0 ? weight : 1, value);
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
80
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
81
81
|
* Space Complexity: O(1)
|
|
82
82
|
*/
|
|
83
83
|
/**
|
|
84
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
84
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
85
85
|
* Space Complexity: O(1)
|
|
86
86
|
*
|
|
87
|
-
* The function `getEdge` returns the first edge that connects two
|
|
87
|
+
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
|
|
88
88
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
89
89
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
90
90
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -93,29 +93,29 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
93
93
|
*/
|
|
94
94
|
getEdge(v1, v2) {
|
|
95
95
|
var _a;
|
|
96
|
-
let
|
|
96
|
+
let edgeMap = [];
|
|
97
97
|
if (v1 !== undefined && v2 !== undefined) {
|
|
98
98
|
const vertex1 = this._getVertex(v1);
|
|
99
99
|
const vertex2 = this._getVertex(v2);
|
|
100
100
|
if (vertex1 && vertex2) {
|
|
101
|
-
|
|
101
|
+
edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.vertexMap.includes(vertex2.key));
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
return
|
|
104
|
+
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
107
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
108
108
|
* Space Complexity: O(1)
|
|
109
109
|
*/
|
|
110
110
|
/**
|
|
111
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
111
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
112
112
|
* Space Complexity: O(1)
|
|
113
113
|
*
|
|
114
|
-
* The function removes an edge between two
|
|
114
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
115
115
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
116
116
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
117
117
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
118
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
118
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
|
|
119
119
|
*/
|
|
120
120
|
deleteEdgeBetween(v1, v2) {
|
|
121
121
|
const vertex1 = this._getVertex(v1);
|
|
@@ -123,31 +123,94 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
123
123
|
if (!vertex1 || !vertex2) {
|
|
124
124
|
return undefined;
|
|
125
125
|
}
|
|
126
|
-
const v1Edges = this.
|
|
126
|
+
const v1Edges = this._edgeMap.get(vertex1);
|
|
127
127
|
let removed = undefined;
|
|
128
128
|
if (v1Edges) {
|
|
129
|
-
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.
|
|
129
|
+
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.vertexMap.includes(vertex2.key))[0] || undefined;
|
|
130
130
|
}
|
|
131
|
-
const v2Edges = this.
|
|
131
|
+
const v2Edges = this._edgeMap.get(vertex2);
|
|
132
132
|
if (v2Edges) {
|
|
133
|
-
(0, utils_1.arrayRemove)(v2Edges, (e) => e.
|
|
133
|
+
(0, utils_1.arrayRemove)(v2Edges, (e) => e.vertexMap.includes(vertex1.key));
|
|
134
134
|
}
|
|
135
135
|
return removed;
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
|
-
* Time Complexity: O(
|
|
138
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
139
139
|
* Space Complexity: O(1)
|
|
140
140
|
*/
|
|
141
141
|
/**
|
|
142
|
-
* Time Complexity: O(
|
|
142
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
143
143
|
* Space Complexity: O(1)
|
|
144
144
|
*
|
|
145
|
-
* The deleteEdge
|
|
146
|
-
* @param {EO}
|
|
147
|
-
*
|
|
145
|
+
* The function `deleteEdge` deletes an edge between two vertexMap in a graph.
|
|
146
|
+
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
147
|
+
* either an edge object or a vertex key.
|
|
148
|
+
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
149
|
+
* parameter that represents the key of the vertex on the other side of the edge. It is used when the
|
|
150
|
+
* `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
|
|
151
|
+
* other side of the
|
|
152
|
+
* @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
|
|
153
|
+
*/
|
|
154
|
+
deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
|
|
155
|
+
let oneSide, otherSide;
|
|
156
|
+
if (this.isVertexKey(edgeOrOneSideVertexKey)) {
|
|
157
|
+
if (this.isVertexKey(otherSideVertexKey)) {
|
|
158
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey);
|
|
159
|
+
otherSide = this._getVertex(otherSideVertexKey);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
|
|
167
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
|
|
168
|
+
}
|
|
169
|
+
if (oneSide && otherSide) {
|
|
170
|
+
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
178
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
148
179
|
*/
|
|
149
|
-
|
|
150
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
182
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
183
|
+
*
|
|
184
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
185
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
186
|
+
* (`VertexKey`).
|
|
187
|
+
* @returns The method is returning a boolean value.
|
|
188
|
+
*/
|
|
189
|
+
deleteVertex(vertexOrKey) {
|
|
190
|
+
let vertexKey;
|
|
191
|
+
let vertex;
|
|
192
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
193
|
+
vertex = this.getVertex(vertexOrKey);
|
|
194
|
+
vertexKey = vertexOrKey;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
vertex = vertexOrKey;
|
|
198
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
199
|
+
}
|
|
200
|
+
const neighbors = this.getNeighbors(vertexOrKey);
|
|
201
|
+
if (vertex) {
|
|
202
|
+
neighbors.forEach(neighbor => {
|
|
203
|
+
const neighborEdges = this._edgeMap.get(neighbor);
|
|
204
|
+
if (neighborEdges) {
|
|
205
|
+
const restEdges = neighborEdges.filter(edge => {
|
|
206
|
+
return !edge.vertexMap.includes(vertexKey);
|
|
207
|
+
});
|
|
208
|
+
this._edgeMap.set(neighbor, restEdges);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
this._edgeMap.delete(vertex);
|
|
212
|
+
}
|
|
213
|
+
return this._vertexMap.delete(vertexKey);
|
|
151
214
|
}
|
|
152
215
|
/**
|
|
153
216
|
* Time Complexity: O(1)
|
|
@@ -157,17 +220,17 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
157
220
|
* Time Complexity: O(1)
|
|
158
221
|
* Space Complexity: O(1)
|
|
159
222
|
*
|
|
160
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of
|
|
223
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
|
|
161
224
|
* vertex.
|
|
162
225
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
163
226
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
164
|
-
*
|
|
227
|
+
* edgeMap connected to that vertex.
|
|
165
228
|
*/
|
|
166
229
|
degreeOf(vertexOrKey) {
|
|
167
230
|
var _a;
|
|
168
231
|
const vertex = this._getVertex(vertexOrKey);
|
|
169
232
|
if (vertex) {
|
|
170
|
-
return ((_a = this.
|
|
233
|
+
return ((_a = this._edgeMap.get(vertex)) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
171
234
|
}
|
|
172
235
|
else {
|
|
173
236
|
return 0;
|
|
@@ -181,52 +244,52 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
181
244
|
* Time Complexity: O(1)
|
|
182
245
|
* Space Complexity: O(1)
|
|
183
246
|
*
|
|
184
|
-
* The function returns the
|
|
247
|
+
* The function returns the edgeMap of a given vertex or vertex ID.
|
|
185
248
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
186
249
|
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
187
|
-
* @returns an array of
|
|
250
|
+
* @returns an array of edgeMap.
|
|
188
251
|
*/
|
|
189
252
|
edgesOf(vertexOrKey) {
|
|
190
253
|
const vertex = this._getVertex(vertexOrKey);
|
|
191
254
|
if (vertex) {
|
|
192
|
-
return this.
|
|
255
|
+
return this._edgeMap.get(vertex) || [];
|
|
193
256
|
}
|
|
194
257
|
else {
|
|
195
258
|
return [];
|
|
196
259
|
}
|
|
197
260
|
}
|
|
198
261
|
/**
|
|
199
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
262
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
200
263
|
* Space Complexity: O(|E|)
|
|
201
264
|
*/
|
|
202
265
|
/**
|
|
203
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
266
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
204
267
|
* Space Complexity: O(|E|)
|
|
205
268
|
*
|
|
206
|
-
* The function "edgeSet" returns an array of unique
|
|
269
|
+
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
207
270
|
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
208
271
|
*/
|
|
209
272
|
edgeSet() {
|
|
210
273
|
const edgeSet = new Set();
|
|
211
|
-
this.
|
|
212
|
-
|
|
274
|
+
this._edgeMap.forEach(edgeMap => {
|
|
275
|
+
edgeMap.forEach(edge => {
|
|
213
276
|
edgeSet.add(edge);
|
|
214
277
|
});
|
|
215
278
|
});
|
|
216
279
|
return [...edgeSet];
|
|
217
280
|
}
|
|
218
281
|
/**
|
|
219
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
282
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
220
283
|
* Space Complexity: O(|E|)
|
|
221
284
|
*/
|
|
222
285
|
/**
|
|
223
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
286
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
224
287
|
* Space Complexity: O(|E|)
|
|
225
288
|
*
|
|
226
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
289
|
+
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
|
|
227
290
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
228
291
|
* (`VertexKey`).
|
|
229
|
-
* @returns an array of
|
|
292
|
+
* @returns an array of vertexMap (VO[]).
|
|
230
293
|
*/
|
|
231
294
|
getNeighbors(vertexOrKey) {
|
|
232
295
|
const neighbors = [];
|
|
@@ -234,7 +297,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
234
297
|
if (vertex) {
|
|
235
298
|
const neighborEdges = this.edgesOf(vertex);
|
|
236
299
|
for (const edge of neighborEdges) {
|
|
237
|
-
const neighbor = this._getVertex(edge.
|
|
300
|
+
const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
|
|
238
301
|
if (neighbor) {
|
|
239
302
|
neighbors.push(neighbor);
|
|
240
303
|
}
|
|
@@ -250,18 +313,18 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
250
313
|
* Time Complexity: O(1)
|
|
251
314
|
* Space Complexity: O(1)
|
|
252
315
|
*
|
|
253
|
-
* The function "getEndsOfEdge" returns the
|
|
316
|
+
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
|
|
254
317
|
* it returns undefined.
|
|
255
318
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
256
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
319
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
257
320
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
258
321
|
*/
|
|
259
322
|
getEndsOfEdge(edge) {
|
|
260
|
-
if (!this.hasEdge(edge.
|
|
323
|
+
if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
|
|
261
324
|
return undefined;
|
|
262
325
|
}
|
|
263
|
-
const v1 = this._getVertex(edge.
|
|
264
|
-
const v2 = this._getVertex(edge.
|
|
326
|
+
const v1 = this._getVertex(edge.vertexMap[0]);
|
|
327
|
+
const v2 = this._getVertex(edge.vertexMap[1]);
|
|
265
328
|
if (v1 && v2) {
|
|
266
329
|
return [v1, v2];
|
|
267
330
|
}
|
|
@@ -277,22 +340,22 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
277
340
|
* Time Complexity: O(1)
|
|
278
341
|
* Space Complexity: O(1)
|
|
279
342
|
*
|
|
280
|
-
* The function adds an edge to the graph by updating the adjacency list with the
|
|
343
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
|
|
281
344
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
282
345
|
* @returns a boolean value.
|
|
283
346
|
*/
|
|
284
347
|
_addEdgeOnly(edge) {
|
|
285
|
-
for (const end of edge.
|
|
348
|
+
for (const end of edge.vertexMap) {
|
|
286
349
|
const endVertex = this._getVertex(end);
|
|
287
350
|
if (endVertex === undefined)
|
|
288
351
|
return false;
|
|
289
352
|
if (endVertex) {
|
|
290
|
-
const
|
|
291
|
-
if (
|
|
292
|
-
|
|
353
|
+
const edgeMap = this._edgeMap.get(endVertex);
|
|
354
|
+
if (edgeMap) {
|
|
355
|
+
edgeMap.push(edge);
|
|
293
356
|
}
|
|
294
357
|
else {
|
|
295
|
-
this.
|
|
358
|
+
this._edgeMap.set(endVertex, [edge]);
|
|
296
359
|
}
|
|
297
360
|
}
|
|
298
361
|
}
|
|
@@ -5,8 +5,119 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { HashMapLinkedNode, HashMapOptions } from '../../types';
|
|
9
|
-
|
|
8
|
+
import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
+
import { IterableEntryBase } from "../base";
|
|
10
|
+
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
11
|
+
protected _store: {
|
|
12
|
+
[key: string]: HashMapStoreItem<K, V>;
|
|
13
|
+
};
|
|
14
|
+
protected _objMap: Map<object, V>;
|
|
15
|
+
/**
|
|
16
|
+
* The constructor function initializes a new instance of a class with optional elements and options.
|
|
17
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
18
|
+
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
19
|
+
* key-value pairs.
|
|
20
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
21
|
+
* configuration options for the constructor. In this case, it has one property:
|
|
22
|
+
*/
|
|
23
|
+
constructor(elements?: Iterable<[K, V]>, options?: {
|
|
24
|
+
hashFn: (key: K) => string;
|
|
25
|
+
});
|
|
26
|
+
protected _size: number;
|
|
27
|
+
get size(): number;
|
|
28
|
+
isEmpty(): boolean;
|
|
29
|
+
clear(): void;
|
|
30
|
+
/**
|
|
31
|
+
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
32
|
+
* the key is not already present.
|
|
33
|
+
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
34
|
+
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be
|
|
35
|
+
* stored in a regular JavaScript object.
|
|
36
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
37
|
+
* key in the data structure.
|
|
38
|
+
*/
|
|
39
|
+
set(key: K, value: V): void;
|
|
40
|
+
/**
|
|
41
|
+
* The function "setMany" sets multiple key-value pairs in a map.
|
|
42
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
43
|
+
* key-value pair is represented as an array with two elements: the key and the value.
|
|
44
|
+
*/
|
|
45
|
+
setMany(elements: Iterable<[K, V]>): void;
|
|
46
|
+
/**
|
|
47
|
+
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
48
|
+
* a string map.
|
|
49
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
50
|
+
* of any type, but it should be compatible with the key type used when the map was created.
|
|
51
|
+
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
|
|
52
|
+
* or `_store`, otherwise it returns `undefined`.
|
|
53
|
+
*/
|
|
54
|
+
get(key: K): V | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
57
|
+
* is an object key or not.
|
|
58
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
59
|
+
* @returns The `has` method is returning a boolean value.
|
|
60
|
+
*/
|
|
61
|
+
has(key: K): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
64
|
+
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
65
|
+
* data structure.
|
|
66
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was
|
|
67
|
+
* successfully deleted from the map, and `false` if the key was not found in the map.
|
|
68
|
+
*/
|
|
69
|
+
delete(key: K): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Time Complexity: O(n)
|
|
72
|
+
* Space Complexity: O(n)
|
|
73
|
+
*/
|
|
74
|
+
/**
|
|
75
|
+
* Time Complexity: O(n)
|
|
76
|
+
* Space Complexity: O(n)
|
|
77
|
+
*
|
|
78
|
+
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each
|
|
79
|
+
* key-value pair in the original HashMap.
|
|
80
|
+
* @param callbackfn - The callback function that will be called for each key-value pair in the
|
|
81
|
+
* HashMap. It takes four parameters:
|
|
82
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
83
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
84
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
85
|
+
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
86
|
+
* the provided callback function.
|
|
87
|
+
*/
|
|
88
|
+
map<U>(callbackfn: EntryCallback<K, V, U>, thisArg?: any): HashMap<K, U>;
|
|
89
|
+
/**
|
|
90
|
+
* Time Complexity: O(n)
|
|
91
|
+
* Space Complexity: O(n)
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* Time Complexity: O(n)
|
|
95
|
+
* Space Complexity: O(n)
|
|
96
|
+
*
|
|
97
|
+
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
|
|
98
|
+
* that satisfy a given predicate function.
|
|
99
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
100
|
+
* index, and map. It is used to determine whether an element should be included in the filtered map
|
|
101
|
+
* or not. The function should return a boolean value - true if the element should be included, and
|
|
102
|
+
* false otherwise.
|
|
103
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
104
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
105
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
106
|
+
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
107
|
+
* from the original `HashMap` that pass the provided `predicate` function.
|
|
108
|
+
*/
|
|
109
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
110
|
+
print(): void;
|
|
111
|
+
/**
|
|
112
|
+
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
113
|
+
* object map.
|
|
114
|
+
*/
|
|
115
|
+
protected _getIterator(): IterableIterator<[K, V]>;
|
|
116
|
+
protected _hashFn: (key: K) => string;
|
|
117
|
+
protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
|
|
118
|
+
protected _getNoObjKey(key: K): string;
|
|
119
|
+
}
|
|
120
|
+
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
10
121
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
|
|
11
122
|
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
|
|
12
123
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
@@ -57,6 +168,8 @@ export declare class HashMap<K = any, V = any> {
|
|
|
57
168
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
58
169
|
*/
|
|
59
170
|
set(key: K, value?: V): number;
|
|
171
|
+
has(key: K): boolean;
|
|
172
|
+
setMany(entries: Iterable<[K, V]>): void;
|
|
60
173
|
/**
|
|
61
174
|
* Time Complexity: O(1)
|
|
62
175
|
* Space Complexity: O(1)
|
|
@@ -120,54 +233,57 @@ export declare class HashMap<K = any, V = any> {
|
|
|
120
233
|
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
121
234
|
*/
|
|
122
235
|
clear(): void;
|
|
236
|
+
clone(): LinkedHashMap<K, V>;
|
|
123
237
|
/**
|
|
124
|
-
* Time Complexity: O(n)
|
|
125
|
-
* Space Complexity: O(
|
|
238
|
+
* Time Complexity: O(n)
|
|
239
|
+
* Space Complexity: O(n)
|
|
240
|
+
*/
|
|
241
|
+
/**
|
|
242
|
+
* Time Complexity: O(n)
|
|
243
|
+
* Space Complexity: O(n)
|
|
126
244
|
*
|
|
127
|
-
* The `
|
|
128
|
-
*
|
|
129
|
-
* @param
|
|
130
|
-
*
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
*
|
|
135
|
-
* key-value pairs
|
|
136
|
-
*
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* @param
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
245
|
+
* The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
|
|
246
|
+
* map that satisfy a given predicate function.
|
|
247
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
248
|
+
* `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
|
|
249
|
+
* current element should be included in the filtered map or not.
|
|
250
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
251
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
252
|
+
* specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
|
|
253
|
+
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
|
|
254
|
+
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
255
|
+
*/
|
|
256
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
|
|
257
|
+
/**
|
|
258
|
+
* Time Complexity: O(n)
|
|
259
|
+
* Space Complexity: O(n)
|
|
260
|
+
*/
|
|
261
|
+
/**
|
|
262
|
+
* Time Complexity: O(n)
|
|
263
|
+
* Space Complexity: O(n)
|
|
264
|
+
*
|
|
265
|
+
* The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
|
|
266
|
+
* each key-value pair in the original map.
|
|
267
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
268
|
+
* in the map. It takes four arguments: the value of the current key-value pair, the key of the
|
|
269
|
+
* current key-value pair, the index of the current key-value pair, and the map itself. The callback
|
|
270
|
+
* function should
|
|
271
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
272
|
+
* specify the value of `this` within the callback function. If provided, the callback function will
|
|
273
|
+
* be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
|
|
274
|
+
* map
|
|
275
|
+
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
276
|
+
* function.
|
|
277
|
+
*/
|
|
278
|
+
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
279
|
+
print(): void;
|
|
280
|
+
/**
|
|
281
|
+
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
165
282
|
* Space Complexity: O(1)
|
|
166
283
|
*
|
|
167
284
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
168
285
|
*/
|
|
169
|
-
|
|
170
|
-
print(): void;
|
|
286
|
+
protected _getIterator(): Generator<[K, V], void, unknown>;
|
|
171
287
|
/**
|
|
172
288
|
* Time Complexity: O(1)
|
|
173
289
|
* Space Complexity: O(1)
|