Skip to content

Commit 0ac2529

Browse files
authored
refactor: Convert layerDFS into a function (#162)
1 parent 84492c3 commit 0ac2529

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

src/types/graphs/viewgraph.ts

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IpAddress } from "../../packets/ip";
99
import { GlobalContext } from "../../context";
1010
import { Graph } from "./graph";
1111

12-
type EdgePair = [DeviceId, DeviceId];
12+
export type EdgePair = [DeviceId, DeviceId];
1313

1414
export class ViewGraph {
1515
private ctx: GlobalContext;
@@ -36,7 +36,14 @@ export class ViewGraph {
3636
const deviceInfo = { id: deviceId, node: graphNode, connections };
3737
this.createDevice(deviceInfo);
3838

39-
this.computeLayerConnections(deviceId, allConnections);
39+
layerDFS(
40+
this.datagraph,
41+
this.layer,
42+
deviceId,
43+
deviceId,
44+
new Set([deviceId]),
45+
allConnections,
46+
);
4047
}
4148
}
4249
this.addConnections(allConnections);
@@ -47,7 +54,14 @@ export class ViewGraph {
4754
const device = this.createDevice(deviceData);
4855
if (deviceData.connections.length !== 0) {
4956
const connections = new Map<string, EdgePair>();
50-
this.computeLayerConnections(deviceData.id, connections);
57+
layerDFS(
58+
this.datagraph,
59+
this.layer,
60+
deviceData.id,
61+
deviceData.id,
62+
new Set([deviceData.id]),
63+
connections,
64+
);
5165

5266
this.addConnections(connections);
5367
}
@@ -324,46 +338,6 @@ export class ViewGraph {
324338
return null;
325339
}
326340

327-
private computeLayerConnections(
328-
source: DeviceId,
329-
connections: Map<string, EdgePair>,
330-
) {
331-
this.layer_dfs(
332-
this.datagraph,
333-
source,
334-
source,
335-
new Set([source]),
336-
connections,
337-
);
338-
}
339-
340-
private layer_dfs(
341-
graph: DataGraph,
342-
s: DeviceId, // source node
343-
v: DeviceId,
344-
visited: Set<DeviceId>,
345-
connections: Map<string, EdgePair>,
346-
) {
347-
graph.getConnections(v).forEach((w) => {
348-
if (visited.has(w)) {
349-
return;
350-
}
351-
const adjacent = this.datagraph.getDevice(w);
352-
// mark node as visited
353-
visited.add(w);
354-
355-
if (layerIncluded(layerFromType(adjacent.type), this.layer)) {
356-
// NOTE: we use strings because according to JavaScript, [1, 2] !== [1, 2]
357-
const edgePair: EdgePair = [w, s];
358-
edgePair.sort();
359-
connections.set(edgePair.toString(), edgePair);
360-
} else {
361-
// continue with recursive search
362-
this.layer_dfs(graph, s, w, visited, connections);
363-
}
364-
});
365-
}
366-
367341
clear() {
368342
this.viewport.clear();
369343
for (const [, device] of this.graph.getAllVertices()) {
@@ -375,3 +349,31 @@ export class ViewGraph {
375349
this.graph.clear();
376350
}
377351
}
352+
353+
function layerDFS(
354+
datagraph: DataGraph,
355+
layer: Layer,
356+
s: DeviceId, // source node
357+
v: DeviceId,
358+
visited: Set<DeviceId>,
359+
connections: Map<string, EdgePair>,
360+
) {
361+
datagraph.getConnections(v).forEach((w) => {
362+
if (visited.has(w)) {
363+
return;
364+
}
365+
const adjacent = datagraph.getDevice(w);
366+
// mark node as visited
367+
visited.add(w);
368+
369+
if (layerIncluded(layerFromType(adjacent.type), layer)) {
370+
// NOTE: we use strings because according to JavaScript, [1, 2] !== [1, 2]
371+
const edgePair: EdgePair = [w, s];
372+
edgePair.sort();
373+
connections.set(edgePair.toString(), edgePair);
374+
} else {
375+
// continue with recursive search
376+
layerDFS(datagraph, layer, s, w, visited, connections);
377+
}
378+
});
379+
}

0 commit comments

Comments
 (0)