This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
GraphMatcher.cs
85 lines (76 loc) · 2.74 KB
/
GraphMatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the BSD license (for details please see \src\AddIns\Debugger\Debugger.AddIn\license.txt)
using System;
using System.Collections.Generic;
using Debugger.AddIn.Visualizers.Utils;
namespace Debugger.AddIn.Visualizers.Graph.Layout
{
/// <summary>
/// Calculates diff between 2 <see cref="PositionedGraph"/>s.
/// </summary>
public class GraphMatcher
{
/// <summary>
/// Calculates diff between 2 <see cref="PositionedGraph"/>s.
/// The <see cref="GraphDiff"/> describes a matching between nodes in the graphs, added and removed nodes.
/// </summary>
public GraphDiff MatchGraphs(PositionedGraph oldGraph, PositionedGraph newGraph)
{
if (oldGraph == null) {
if (newGraph == null) {
return new GraphDiff();
} else {
GraphDiff addAllDiff = new GraphDiff();
foreach (PositionedNode newNode in newGraph.Nodes)
addAllDiff.SetAdded(newNode);
return addAllDiff;
}
} else if (newGraph == null) {
GraphDiff removeAllDiff = new GraphDiff();
foreach (PositionedNode oldNode in oldGraph.Nodes)
removeAllDiff.SetRemoved(oldNode);
return removeAllDiff;
}
// none of the graphs is null
GraphDiff diff = new GraphDiff();
Dictionary<int, PositionedNode> newNodeForHashCode = BuildHashToNodeMap(newGraph);
Dictionary<PositionedNode, bool> newNodeMatched = new Dictionary<PositionedNode, bool>();
foreach (PositionedNode oldNode in oldGraph.Nodes) {
PositionedNode matchingNode = MatchNode(oldNode, newNodeForHashCode);
if (matchingNode != null) {
diff.SetMatching(oldNode, matchingNode);
newNodeMatched[matchingNode] = true;
} else {
diff.SetRemoved(oldNode);
}
}
foreach (PositionedNode newNode in newGraph.Nodes) {
if (!newNodeMatched.ContainsKey(newNode)) {
diff.SetAdded(newNode);
}
}
return diff;
}
Dictionary<int, PositionedNode> BuildHashToNodeMap(PositionedGraph graph)
{
var hashToNodeMap = new Dictionary<int, PositionedNode>();
foreach (PositionedNode node in graph.Nodes) {
hashToNodeMap[node.ObjectNode.HashCode] = node;
}
return hashToNodeMap;
}
PositionedNode MatchNode(PositionedNode oldNode, Dictionary<int, PositionedNode> newNodeMap)
{
PositionedNode newNodeFound = newNodeMap.GetValue(oldNode.ObjectNode.HashCode);
if ((newNodeFound != null) && IsSameAddress(oldNode, newNodeFound)) {
return newNodeFound;
} else {
return null;
}
}
bool IsSameAddress(PositionedNode node1, PositionedNode node2)
{
return node1.ObjectNode.PermanentReference.GetObjectAddress() == node2.ObjectNode.PermanentReference.GetObjectAddress();
}
}
}