data-structure-typed 1.48.3 → 1.48.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +6 -6
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/cjs/data-structures/base/iterable-base.js +3 -3
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +16 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/cjs/data-structures/binary-tree/bst.js +17 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +53 -52
- package/dist/cjs/data-structures/graph/abstract-graph.js +82 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/cjs/data-structures/graph/directed-graph.js +111 -65
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/cjs/data-structures/graph/map-graph.js +8 -8
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/cjs/data-structures/graph/undirected-graph.js +117 -54
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +3 -3
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -6
- package/dist/mjs/data-structures/base/iterable-base.js +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +18 -15
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +17 -14
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +15 -11
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +53 -52
- package/dist/mjs/data-structures/graph/abstract-graph.js +83 -79
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/mjs/data-structures/graph/directed-graph.js +111 -65
- package/dist/mjs/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/mjs/data-structures/graph/map-graph.js +8 -8
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/mjs/data-structures/graph/undirected-graph.js +119 -56
- package/dist/mjs/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/mjs/data-structures/hash/hash-map.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +413 -285
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +8 -5
- package/src/data-structures/binary-tree/binary-tree.ts +23 -19
- package/src/data-structures/binary-tree/bst.ts +19 -14
- package/src/data-structures/binary-tree/rb-tree.ts +20 -14
- package/src/data-structures/binary-tree/tree-multimap.ts +27 -19
- package/src/data-structures/graph/abstract-graph.ts +87 -82
- 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 +8 -8
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/base/base.ts +3 -3
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +3 -3
- package/test/unit/data-structures/graph/directed-graph.test.ts +52 -15
- package/test/unit/data-structures/graph/map-graph.test.ts +3 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +42 -5
|
@@ -20,7 +20,7 @@ export class UndirectedVertex extends AbstractVertex {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
export class UndirectedEdge extends AbstractEdge {
|
|
23
|
-
|
|
23
|
+
vertexMap;
|
|
24
24
|
/**
|
|
25
25
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
26
26
|
* value.
|
|
@@ -33,20 +33,20 @@ export class UndirectedEdge extends AbstractEdge {
|
|
|
33
33
|
*/
|
|
34
34
|
constructor(v1, v2, weight, value) {
|
|
35
35
|
super(weight, value);
|
|
36
|
-
this.
|
|
36
|
+
this.vertexMap = [v1, v2];
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
export class UndirectedGraph extends AbstractGraph {
|
|
40
40
|
/**
|
|
41
|
-
* The constructor initializes a new Map object to store
|
|
41
|
+
* The constructor initializes a new Map object to store edgeMap.
|
|
42
42
|
*/
|
|
43
43
|
constructor() {
|
|
44
44
|
super();
|
|
45
|
-
this.
|
|
45
|
+
this._edgeMap = new Map();
|
|
46
46
|
}
|
|
47
|
-
|
|
48
|
-
get
|
|
49
|
-
return this.
|
|
47
|
+
_edgeMap;
|
|
48
|
+
get edgeMap() {
|
|
49
|
+
return this._edgeMap;
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* The function creates a new vertex with an optional value and returns it.
|
|
@@ -61,7 +61,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
61
61
|
return new UndirectedVertex(key, value ?? key);
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
|
-
* The function creates an undirected edge between two
|
|
64
|
+
* The function creates an undirected edge between two vertexMap with an optional weight and value.
|
|
65
65
|
* @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
66
66
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
67
67
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
@@ -74,14 +74,14 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
74
74
|
return new UndirectedEdge(v1, v2, weight ?? 1, value);
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
77
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
78
78
|
* Space Complexity: O(1)
|
|
79
79
|
*/
|
|
80
80
|
/**
|
|
81
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
81
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
82
82
|
* Space Complexity: O(1)
|
|
83
83
|
*
|
|
84
|
-
* The function `getEdge` returns the first edge that connects two
|
|
84
|
+
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists.
|
|
85
85
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
86
86
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
87
87
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -89,29 +89,29 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
89
89
|
* @returns an edge (EO) or undefined.
|
|
90
90
|
*/
|
|
91
91
|
getEdge(v1, v2) {
|
|
92
|
-
let
|
|
92
|
+
let edgeMap = [];
|
|
93
93
|
if (v1 !== undefined && v2 !== undefined) {
|
|
94
94
|
const vertex1 = this._getVertex(v1);
|
|
95
95
|
const vertex2 = this._getVertex(v2);
|
|
96
96
|
if (vertex1 && vertex2) {
|
|
97
|
-
|
|
97
|
+
edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.vertexMap.includes(vertex2.key));
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
return
|
|
100
|
+
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
|
-
* Time Complexity: O(|E|), where |E| is the number of
|
|
103
|
+
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
104
104
|
* Space Complexity: O(1)
|
|
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
|
-
* The function removes an edge between two
|
|
110
|
+
* The function removes an edge between two vertexMap in a graph and returns the removed edge.
|
|
111
111
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
112
112
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
113
113
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
114
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
114
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist.
|
|
115
115
|
*/
|
|
116
116
|
deleteEdgeBetween(v1, v2) {
|
|
117
117
|
const vertex1 = this._getVertex(v1);
|
|
@@ -119,31 +119,94 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
119
119
|
if (!vertex1 || !vertex2) {
|
|
120
120
|
return undefined;
|
|
121
121
|
}
|
|
122
|
-
const v1Edges = this.
|
|
122
|
+
const v1Edges = this._edgeMap.get(vertex1);
|
|
123
123
|
let removed = undefined;
|
|
124
124
|
if (v1Edges) {
|
|
125
|
-
removed = arrayRemove(v1Edges, (e) => e.
|
|
125
|
+
removed = arrayRemove(v1Edges, (e) => e.vertexMap.includes(vertex2.key))[0] || undefined;
|
|
126
126
|
}
|
|
127
|
-
const v2Edges = this.
|
|
127
|
+
const v2Edges = this._edgeMap.get(vertex2);
|
|
128
128
|
if (v2Edges) {
|
|
129
|
-
arrayRemove(v2Edges, (e) => e.
|
|
129
|
+
arrayRemove(v2Edges, (e) => e.vertexMap.includes(vertex1.key));
|
|
130
130
|
}
|
|
131
131
|
return removed;
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
|
-
* Time Complexity: O(
|
|
134
|
+
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
135
135
|
* Space Complexity: O(1)
|
|
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
|
-
* The deleteEdge
|
|
142
|
-
* @param {EO}
|
|
143
|
-
*
|
|
141
|
+
* The function `deleteEdge` deletes an edge between two vertexMap in a graph.
|
|
142
|
+
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
143
|
+
* either an edge object or a vertex key.
|
|
144
|
+
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
145
|
+
* parameter that represents the key of the vertex on the other side of the edge. It is used when the
|
|
146
|
+
* `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
|
|
147
|
+
* other side of the
|
|
148
|
+
* @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
|
|
149
|
+
*/
|
|
150
|
+
deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
|
|
151
|
+
let oneSide, otherSide;
|
|
152
|
+
if (this.isVertexKey(edgeOrOneSideVertexKey)) {
|
|
153
|
+
if (this.isVertexKey(otherSideVertexKey)) {
|
|
154
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey);
|
|
155
|
+
otherSide = this._getVertex(otherSideVertexKey);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]);
|
|
163
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]);
|
|
164
|
+
}
|
|
165
|
+
if (oneSide && otherSide) {
|
|
166
|
+
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
174
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
144
175
|
*/
|
|
145
|
-
|
|
146
|
-
|
|
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.
|
|
179
|
+
*
|
|
180
|
+
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
181
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
182
|
+
* (`VertexKey`).
|
|
183
|
+
* @returns The method is returning a boolean value.
|
|
184
|
+
*/
|
|
185
|
+
deleteVertex(vertexOrKey) {
|
|
186
|
+
let vertexKey;
|
|
187
|
+
let vertex;
|
|
188
|
+
if (this.isVertexKey(vertexOrKey)) {
|
|
189
|
+
vertex = this.getVertex(vertexOrKey);
|
|
190
|
+
vertexKey = vertexOrKey;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
vertex = vertexOrKey;
|
|
194
|
+
vertexKey = this._getVertexKey(vertexOrKey);
|
|
195
|
+
}
|
|
196
|
+
const neighbors = this.getNeighbors(vertexOrKey);
|
|
197
|
+
if (vertex) {
|
|
198
|
+
neighbors.forEach(neighbor => {
|
|
199
|
+
const neighborEdges = this._edgeMap.get(neighbor);
|
|
200
|
+
if (neighborEdges) {
|
|
201
|
+
const restEdges = neighborEdges.filter(edge => {
|
|
202
|
+
return !edge.vertexMap.includes(vertexKey);
|
|
203
|
+
});
|
|
204
|
+
this._edgeMap.set(neighbor, restEdges);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
this._edgeMap.delete(vertex);
|
|
208
|
+
}
|
|
209
|
+
return this._vertexMap.delete(vertexKey);
|
|
147
210
|
}
|
|
148
211
|
/**
|
|
149
212
|
* Time Complexity: O(1)
|
|
@@ -153,16 +216,16 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
153
216
|
* Time Complexity: O(1)
|
|
154
217
|
* Space Complexity: O(1)
|
|
155
218
|
*
|
|
156
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of
|
|
219
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
|
|
157
220
|
* vertex.
|
|
158
221
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
159
222
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
160
|
-
*
|
|
223
|
+
* edgeMap connected to that vertex.
|
|
161
224
|
*/
|
|
162
225
|
degreeOf(vertexOrKey) {
|
|
163
226
|
const vertex = this._getVertex(vertexOrKey);
|
|
164
227
|
if (vertex) {
|
|
165
|
-
return this.
|
|
228
|
+
return this._edgeMap.get(vertex)?.length || 0;
|
|
166
229
|
}
|
|
167
230
|
else {
|
|
168
231
|
return 0;
|
|
@@ -176,52 +239,52 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
176
239
|
* Time Complexity: O(1)
|
|
177
240
|
* Space Complexity: O(1)
|
|
178
241
|
*
|
|
179
|
-
* The function returns the
|
|
242
|
+
* The function returns the edgeMap of a given vertex or vertex ID.
|
|
180
243
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
181
244
|
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
182
|
-
* @returns an array of
|
|
245
|
+
* @returns an array of edgeMap.
|
|
183
246
|
*/
|
|
184
247
|
edgesOf(vertexOrKey) {
|
|
185
248
|
const vertex = this._getVertex(vertexOrKey);
|
|
186
249
|
if (vertex) {
|
|
187
|
-
return this.
|
|
250
|
+
return this._edgeMap.get(vertex) || [];
|
|
188
251
|
}
|
|
189
252
|
else {
|
|
190
253
|
return [];
|
|
191
254
|
}
|
|
192
255
|
}
|
|
193
256
|
/**
|
|
194
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
257
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
195
258
|
* Space Complexity: O(|E|)
|
|
196
259
|
*/
|
|
197
260
|
/**
|
|
198
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
261
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
199
262
|
* Space Complexity: O(|E|)
|
|
200
263
|
*
|
|
201
|
-
* The function "edgeSet" returns an array of unique
|
|
264
|
+
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
202
265
|
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
203
266
|
*/
|
|
204
267
|
edgeSet() {
|
|
205
268
|
const edgeSet = new Set();
|
|
206
|
-
this.
|
|
207
|
-
|
|
269
|
+
this._edgeMap.forEach(edgeMap => {
|
|
270
|
+
edgeMap.forEach(edge => {
|
|
208
271
|
edgeSet.add(edge);
|
|
209
272
|
});
|
|
210
273
|
});
|
|
211
274
|
return [...edgeSet];
|
|
212
275
|
}
|
|
213
276
|
/**
|
|
214
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
277
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
215
278
|
* Space Complexity: O(|E|)
|
|
216
279
|
*/
|
|
217
280
|
/**
|
|
218
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of
|
|
281
|
+
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
219
282
|
* Space Complexity: O(|E|)
|
|
220
283
|
*
|
|
221
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
284
|
+
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID.
|
|
222
285
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
223
286
|
* (`VertexKey`).
|
|
224
|
-
* @returns an array of
|
|
287
|
+
* @returns an array of vertexMap (VO[]).
|
|
225
288
|
*/
|
|
226
289
|
getNeighbors(vertexOrKey) {
|
|
227
290
|
const neighbors = [];
|
|
@@ -229,7 +292,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
229
292
|
if (vertex) {
|
|
230
293
|
const neighborEdges = this.edgesOf(vertex);
|
|
231
294
|
for (const edge of neighborEdges) {
|
|
232
|
-
const neighbor = this._getVertex(edge.
|
|
295
|
+
const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]);
|
|
233
296
|
if (neighbor) {
|
|
234
297
|
neighbors.push(neighbor);
|
|
235
298
|
}
|
|
@@ -245,18 +308,18 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
245
308
|
* Time Complexity: O(1)
|
|
246
309
|
* Space Complexity: O(1)
|
|
247
310
|
*
|
|
248
|
-
* The function "getEndsOfEdge" returns the
|
|
311
|
+
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise
|
|
249
312
|
* it returns undefined.
|
|
250
313
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
251
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
314
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
252
315
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
253
316
|
*/
|
|
254
317
|
getEndsOfEdge(edge) {
|
|
255
|
-
if (!this.hasEdge(edge.
|
|
318
|
+
if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) {
|
|
256
319
|
return undefined;
|
|
257
320
|
}
|
|
258
|
-
const v1 = this._getVertex(edge.
|
|
259
|
-
const v2 = this._getVertex(edge.
|
|
321
|
+
const v1 = this._getVertex(edge.vertexMap[0]);
|
|
322
|
+
const v2 = this._getVertex(edge.vertexMap[1]);
|
|
260
323
|
if (v1 && v2) {
|
|
261
324
|
return [v1, v2];
|
|
262
325
|
}
|
|
@@ -272,22 +335,22 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
272
335
|
* Time Complexity: O(1)
|
|
273
336
|
* Space Complexity: O(1)
|
|
274
337
|
*
|
|
275
|
-
* The function adds an edge to the graph by updating the adjacency list with the
|
|
338
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
|
|
276
339
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
277
340
|
* @returns a boolean value.
|
|
278
341
|
*/
|
|
279
342
|
_addEdgeOnly(edge) {
|
|
280
|
-
for (const end of edge.
|
|
343
|
+
for (const end of edge.vertexMap) {
|
|
281
344
|
const endVertex = this._getVertex(end);
|
|
282
345
|
if (endVertex === undefined)
|
|
283
346
|
return false;
|
|
284
347
|
if (endVertex) {
|
|
285
|
-
const
|
|
286
|
-
if (
|
|
287
|
-
|
|
348
|
+
const edgeMap = this._edgeMap.get(endVertex);
|
|
349
|
+
if (edgeMap) {
|
|
350
|
+
edgeMap.push(edge);
|
|
288
351
|
}
|
|
289
352
|
else {
|
|
290
|
-
this.
|
|
353
|
+
this._edgeMap.set(endVertex, [edge]);
|
|
291
354
|
}
|
|
292
355
|
}
|
|
293
356
|
}
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem
|
|
9
|
-
import {
|
|
10
|
-
export declare class HashMap<K = any, V = any> extends
|
|
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
11
|
protected _store: {
|
|
12
12
|
[key: string]: HashMapStoreItem<K, V>;
|
|
13
13
|
};
|
|
@@ -85,7 +85,7 @@ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
|
|
|
85
85
|
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
86
86
|
* the provided callback function.
|
|
87
87
|
*/
|
|
88
|
-
map<U>(callbackfn:
|
|
88
|
+
map<U>(callbackfn: EntryCallback<K, V, U>, thisArg?: any): HashMap<K, U>;
|
|
89
89
|
/**
|
|
90
90
|
* Time Complexity: O(n)
|
|
91
91
|
* Space Complexity: O(n)
|
|
@@ -106,7 +106,7 @@ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
|
|
|
106
106
|
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
107
107
|
* from the original `HashMap` that pass the provided `predicate` function.
|
|
108
108
|
*/
|
|
109
|
-
filter(predicate:
|
|
109
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
110
110
|
print(): void;
|
|
111
111
|
/**
|
|
112
112
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
@@ -117,7 +117,7 @@ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
|
|
|
117
117
|
protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
|
|
118
118
|
protected _getNoObjKey(key: K): string;
|
|
119
119
|
}
|
|
120
|
-
export declare class LinkedHashMap<K = any, V = any> extends
|
|
120
|
+
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
121
121
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
|
|
122
122
|
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
|
|
123
123
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
@@ -253,7 +253,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterablePairBase<K,
|
|
|
253
253
|
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
|
|
254
254
|
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
255
255
|
*/
|
|
256
|
-
filter(predicate:
|
|
256
|
+
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
|
|
257
257
|
/**
|
|
258
258
|
* Time Complexity: O(n)
|
|
259
259
|
* Space Complexity: O(n)
|
|
@@ -275,7 +275,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterablePairBase<K,
|
|
|
275
275
|
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
276
276
|
* function.
|
|
277
277
|
*/
|
|
278
|
-
map<NV>(callback:
|
|
278
|
+
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
279
279
|
print(): void;
|
|
280
280
|
/**
|
|
281
281
|
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
9
|
-
import {
|
|
10
|
-
export class HashMap extends
|
|
9
|
+
import { IterableEntryBase } from "../base";
|
|
10
|
+
export class HashMap extends IterableEntryBase {
|
|
11
11
|
_store = {};
|
|
12
12
|
_objMap = new Map();
|
|
13
13
|
/**
|
|
@@ -225,7 +225,7 @@ export class HashMap extends IterablePairBase {
|
|
|
225
225
|
return strKey;
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
|
-
export class LinkedHashMap extends
|
|
228
|
+
export class LinkedHashMap extends IterableEntryBase {
|
|
229
229
|
_noObjMap = {};
|
|
230
230
|
_objMap = new WeakMap();
|
|
231
231
|
_head;
|
|
@@ -3,7 +3,7 @@ import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDelete
|
|
|
3
3
|
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
6
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count?: number): N | null | undefined;
|
|
6
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
|
7
7
|
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
9
9
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IterableElementBase,
|
|
2
|
-
export type
|
|
1
|
+
import { IterableElementBase, IterableEntryBase } from "../../../data-structures";
|
|
2
|
+
export type EntryCallback<K, V, R> = (value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
3
3
|
export type ElementCallback<V, R> = (element: V, index: number, container: IterableElementBase<V>) => R;
|
|
4
|
-
export type
|
|
4
|
+
export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
5
5
|
export type ReduceElementCallback<V, R> = (accumulator: R, element: V, index: number, container: IterableElementBase<V>) => R;
|