Skip to content

Commit 4e71aae

Browse files
authored
fix: serialize tuple before using as key (#161)
Javascript and Typescript don't have tuple types. When comparing fixed-length arrays, they always return "not equal", because they are objects. Here we change it to serialize tuples before using them as Map keys
1 parent 09982ce commit 4e71aae

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/types/graphs/viewgraph.ts

Lines changed: 14 additions & 13 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-
export type EdgeId = [DeviceId, DeviceId];
12+
type EdgePair = [DeviceId, DeviceId];
1313

1414
export class ViewGraph {
1515
private ctx: GlobalContext;
@@ -28,7 +28,7 @@ export class ViewGraph {
2828

2929
private constructView() {
3030
console.log("Constructing ViewGraph from DataGraph");
31-
const allConnections = new Set<EdgeId>();
31+
const allConnections = new Map<string, EdgePair>();
3232

3333
for (const [deviceId, graphNode] of this.datagraph.getDevices()) {
3434
if (layerIncluded(layerFromType(graphNode.type), this.layer)) {
@@ -46,7 +46,7 @@ export class ViewGraph {
4646
addDevice(deviceData: CreateDevice) {
4747
const device = this.createDevice(deviceData);
4848
if (deviceData.connections.length !== 0) {
49-
const connections = new Set<EdgeId>();
49+
const connections = new Map<string, EdgePair>();
5050
this.computeLayerConnections(deviceData.id, connections);
5151

5252
this.addConnections(connections);
@@ -70,7 +70,7 @@ export class ViewGraph {
7070
return this.graph.getVertex(deviceData.id);
7171
}
7272

73-
private addConnections(connections: Set<EdgeId>) {
73+
private addConnections(connections: Map<string, EdgePair>) {
7474
connections.forEach(([id1, id2]) => {
7575
const device1 = this.getDevice(id1);
7676
const device2 = this.getDevice(id2);
@@ -97,7 +97,7 @@ export class ViewGraph {
9797
return edge;
9898
}
9999

100-
addEdge(device1Id: DeviceId, device2Id: DeviceId): EdgeId | null {
100+
addEdge(device1Id: DeviceId, device2Id: DeviceId): EdgePair | null {
101101
if (device1Id === device2Id) {
102102
console.warn(
103103
`Cannot create a connection between the same device (ID ${device1Id}).`,
@@ -324,7 +324,10 @@ export class ViewGraph {
324324
return null;
325325
}
326326

327-
private computeLayerConnections(source: DeviceId, connections: Set<EdgeId>) {
327+
private computeLayerConnections(
328+
source: DeviceId,
329+
connections: Map<string, EdgePair>,
330+
) {
328331
this.layer_dfs(
329332
this.datagraph,
330333
source,
@@ -339,7 +342,7 @@ export class ViewGraph {
339342
s: DeviceId, // source node
340343
v: DeviceId,
341344
visited: Set<DeviceId>,
342-
connections: Set<EdgeId>,
345+
connections: Map<string, EdgePair>,
343346
) {
344347
graph.getConnections(v).forEach((w) => {
345348
if (visited.has(w)) {
@@ -350,12 +353,10 @@ export class ViewGraph {
350353
visited.add(w);
351354

352355
if (layerIncluded(layerFromType(adjacent.type), this.layer)) {
353-
// add connection between s and w
354-
const connectionKey: [DeviceId, DeviceId] = [w, s];
355-
connectionKey.sort();
356-
if (!connections.has(connectionKey)) {
357-
connections.add(connectionKey);
358-
}
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);
359360
} else {
360361
// continue with recursive search
361362
this.layer_dfs(graph, s, w, visited, connections);

0 commit comments

Comments
 (0)