Skip to content

Commit d7d9104

Browse files
committed
Graph: +RemoveNode, +RemoveEdge
1 parent 244846b commit d7d9104

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/Wishmaster.Helpers.ContainerDependency.Console/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public static int Main()
1616
graph.SetEdge(b, c, new EdgeData(EdgeType.ProjectReference, ""));
1717
graph.SetEdge(c, a, new EdgeData(EdgeType.ProjectReference, ""));
1818

19+
graph.RemoveNode(b);
20+
graph.RemoveEdge(c, a);
21+
1922
return 0;
2023
}
2124
}

src/Wishmaster.Helpers.ContainerDependency/Models/Edge.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ namespace Wishmaster.Helpers.ContainerDependency.Models
88
{
99
public class Edge<T>
1010
{
11-
public Guid Uid { get; private set; }
1211
public Guid From { get; private set; }
1312
public Guid To { get; private set; }
1413
public T Value { get; private set; }
1514

1615
public Edge(T value, Guid from, Guid to)
1716
{
18-
Uid = Guid.NewGuid();
1917
From = from;
2018
To = to;
2119
Value = value;
@@ -28,7 +26,7 @@ public void SetValue(T value)
2826

2927
public override int GetHashCode()
3028
{
31-
return HashCode.Combine(Uid, From, To, Value);
29+
return HashCode.Combine(From, To, Value);
3230
}
3331
}
3432
}

src/Wishmaster.Helpers.ContainerDependency/Models/Graph.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,31 @@ public Node<TNode> AddNode(Node<TNode> node)
4242
return node;
4343
}
4444

45-
public void RemoveNode()
45+
public void RemoveNode(Node<TNode> node)
4646
{
47+
if (node is null)
48+
{
49+
throw new ArgumentNullException(nameof(node));
50+
}
4751

52+
var identity = new NodeIdentity(node.Uid);
53+
54+
if (!_nodes.ContainsKey(identity))
55+
{
56+
throw new Exception($"Node {node.Uid} is not found");
57+
}
58+
59+
var edges = _edges
60+
.Where(x => x.Key.From == node.Uid || x.Key.To == node.Uid)
61+
.Select(x => x.Key)
62+
.ToArray();
63+
64+
foreach (var edge in edges)
65+
{
66+
RemoveEdge(edge.From, edge.To);
67+
}
68+
69+
_nodes.Remove(identity);
4870
}
4971

5072
public void SetEdge(Node<TNode> from, Node<TNode> to, TEdge value)
@@ -68,9 +90,31 @@ public void SetEdge(Guid from, Guid to, TEdge value)
6890
_edges[identity] = new Edge<TEdge>(value, from, to);
6991
}
7092

71-
public void RemoveEdge()
93+
public void RemoveEdge(Node<TNode> from, Node<TNode> to)
94+
{
95+
if (from is null)
96+
{
97+
throw new ArgumentNullException(nameof(from));
98+
}
99+
100+
if (to is null)
101+
{
102+
throw new ArgumentNullException(nameof(to));
103+
}
104+
105+
RemoveEdge(from.Uid, to.Uid);
106+
}
107+
108+
public void RemoveEdge(Guid from, Guid to)
72109
{
110+
var identity = new EdgeIdentity(from, to);
111+
112+
if (!_edges.ContainsKey(identity))
113+
{
114+
throw new Exception($"Edge {identity} is not found");
115+
}
73116

117+
_edges.Remove(identity);
74118
}
75119

76120
public override int GetHashCode()

0 commit comments

Comments
 (0)