addedge no longer inherited
parent
fcd9c23ecd
commit
f2f1b8979b
|
@ -20,6 +20,25 @@ namespace MoonTools.Core.Graph
|
|||
}
|
||||
}
|
||||
|
||||
public virtual void AddEdge(TNode v, TNode u, TEdgeData edgeData)
|
||||
{
|
||||
CheckNodes(v, u);
|
||||
|
||||
if (v.Equals(u)) { throw new ArgumentException("Self-edges are not allowed in a simple graph. Use a multigraph instead"); }
|
||||
|
||||
neighbors[v].Add(u);
|
||||
edges.Add((v, u));
|
||||
edgeToEdgeData.Add((v, u), edgeData);
|
||||
}
|
||||
|
||||
public virtual void AddEdges(params (TNode, TNode, TEdgeData)[] edges)
|
||||
{
|
||||
foreach (var edge in edges)
|
||||
{
|
||||
AddEdge(edge.Item1, edge.Item2, edge.Item3);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveNode(TNode node)
|
||||
{
|
||||
CheckNodes(node);
|
||||
|
@ -45,25 +64,6 @@ namespace MoonTools.Core.Graph
|
|||
neighbors.Remove(node);
|
||||
}
|
||||
|
||||
public virtual void AddEdge(TNode v, TNode u, TEdgeData edgeData)
|
||||
{
|
||||
CheckNodes(v, u);
|
||||
|
||||
if (v.Equals(u)) { throw new ArgumentException("Self-edges are not allowed in a simple graph. Use a multigraph instead"); }
|
||||
|
||||
neighbors[v].Add(u);
|
||||
edges.Add((v, u));
|
||||
edgeToEdgeData.Add((v, u), edgeData);
|
||||
}
|
||||
|
||||
public virtual void AddEdges(params (TNode, TNode, TEdgeData)[] edges)
|
||||
{
|
||||
foreach (var edge in edges)
|
||||
{
|
||||
AddEdge(edge.Item1, edge.Item2, edge.Item3);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void RemoveEdge(TNode v, TNode u)
|
||||
{
|
||||
CheckEdge(v, u);
|
||||
|
@ -469,12 +469,9 @@ namespace MoonTools.Core.Graph
|
|||
return subGraph;
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
public override void Clear()
|
||||
{
|
||||
nodes.Clear();
|
||||
neighbors.Clear();
|
||||
edges.Clear();
|
||||
edgeToEdgeData.Clear();
|
||||
base.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,17 +6,24 @@ using MoreLinq;
|
|||
|
||||
namespace MoonTools.Core.Graph
|
||||
{
|
||||
public class DirectedWeightedGraph<TNode, TEdgeData> : DirectedGraph<TNode, TEdgeData> where TNode : System.IEquatable<TNode>
|
||||
public class DirectedWeightedGraph<TNode, TEdgeData> : SimpleGraph<TNode, TEdgeData> where TNode : System.IEquatable<TNode>
|
||||
{
|
||||
protected Dictionary<(TNode, TNode), int> weights = new Dictionary<(TNode, TNode), int>();
|
||||
|
||||
public void AddEdge(TNode v, TNode u, int weight, TEdgeData data)
|
||||
public void AddEdge(TNode v, TNode u, int weight, TEdgeData edgeData)
|
||||
{
|
||||
base.AddEdge(v, u, data);
|
||||
CheckNodes(v, u);
|
||||
|
||||
if (v.Equals(u)) { throw new ArgumentException("Self-edges are not allowed in a simple graph. Use a multigraph instead"); }
|
||||
|
||||
neighbors[v].Add(u);
|
||||
edges.Add((v, u));
|
||||
edgeToEdgeData.Add((v, u), edgeData);
|
||||
|
||||
weights.Add((v, u), weight);
|
||||
}
|
||||
|
||||
public void AddEdges(params (TNode, TNode, int, TEdgeData)[] edges)
|
||||
public void AddEdges(params (TNode, TNode, int weight, TEdgeData)[] edges)
|
||||
{
|
||||
foreach (var edge in edges)
|
||||
{
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
using Collections.Pooled;
|
||||
|
||||
namespace MoonTools.Core.Graph
|
||||
{
|
||||
abstract public class Graph<TNode, TEdgeData> where TNode : System.IEquatable<TNode>
|
||||
{
|
||||
protected HashSet<TNode> nodes = new HashSet<TNode>();
|
||||
protected Dictionary<TNode, HashSet<TNode>> neighbors = new Dictionary<TNode, HashSet<TNode>>();
|
||||
protected Dictionary<TNode, PooledSet<TNode>> neighbors = new Dictionary<TNode, PooledSet<TNode>>();
|
||||
|
||||
public IEnumerable<TNode> Nodes => nodes;
|
||||
|
||||
|
@ -16,7 +17,7 @@ namespace MoonTools.Core.Graph
|
|||
if (!Exists(node))
|
||||
{
|
||||
nodes.Add(node);
|
||||
neighbors.Add(node, new HashSet<TNode>());
|
||||
neighbors.Add(node, new PooledSet<TNode>(ClearMode.Always));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,5 +57,10 @@ namespace MoonTools.Core.Graph
|
|||
}
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
nodes.Clear();
|
||||
neighbors.Clear();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,5 +29,12 @@ namespace MoonTools.Core.Graph
|
|||
CheckNodes(v, u);
|
||||
if (!Exists(v, u)) { throw new ArgumentException($"Edge between vertex {v} and vertex {u} does not exist in the graph"); }
|
||||
}
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
base.Clear();
|
||||
edges.Clear();
|
||||
edgeToEdgeData.Clear();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue