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)
|
public void RemoveNode(TNode node)
|
||||||
{
|
{
|
||||||
CheckNodes(node);
|
CheckNodes(node);
|
||||||
|
@ -45,25 +64,6 @@ namespace MoonTools.Core.Graph
|
||||||
neighbors.Remove(node);
|
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)
|
public virtual void RemoveEdge(TNode v, TNode u)
|
||||||
{
|
{
|
||||||
CheckEdge(v, u);
|
CheckEdge(v, u);
|
||||||
|
@ -469,12 +469,9 @@ namespace MoonTools.Core.Graph
|
||||||
return subGraph;
|
return subGraph;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Clear()
|
public override void Clear()
|
||||||
{
|
{
|
||||||
nodes.Clear();
|
base.Clear();
|
||||||
neighbors.Clear();
|
|
||||||
edges.Clear();
|
|
||||||
edgeToEdgeData.Clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,17 +6,24 @@ using MoreLinq;
|
||||||
|
|
||||||
namespace MoonTools.Core.Graph
|
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>();
|
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);
|
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)
|
foreach (var edge in edges)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Collections.Pooled;
|
||||||
|
|
||||||
namespace MoonTools.Core.Graph
|
namespace MoonTools.Core.Graph
|
||||||
{
|
{
|
||||||
abstract public class Graph<TNode, TEdgeData> where TNode : System.IEquatable<TNode>
|
abstract public class Graph<TNode, TEdgeData> where TNode : System.IEquatable<TNode>
|
||||||
{
|
{
|
||||||
protected HashSet<TNode> nodes = new HashSet<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;
|
public IEnumerable<TNode> Nodes => nodes;
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ namespace MoonTools.Core.Graph
|
||||||
if (!Exists(node))
|
if (!Exists(node))
|
||||||
{
|
{
|
||||||
nodes.Add(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);
|
CheckNodes(v, u);
|
||||||
if (!Exists(v, u)) { throw new ArgumentException($"Edge between vertex {v} and vertex {u} does not exist in the graph"); }
|
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