diff options
Diffstat (limited to 'Graph.cs')
-rw-r--r-- | Graph.cs | 26 |
1 files changed, 16 insertions, 10 deletions
@@ -1,35 +1,41 @@ using System; using System.Collections.Generic; +using Interfaces; namespace Graph { - public class Graph<T> where T : IComparable<T> + public class Graph<T, U, V> + where T : IVertex<U, V>, new() + where U : IEdge<V>, new() + where V : IComparable<V> { - public List<Vertex<T>> Vertices { get; set; } = new List<Vertex<T>>(); + public List<T> Vertices { get; set; } = new List<T>(); public Graph() { } - public Graph(List<Vertex<T>> vertices) + public Graph(List<T> vertices) { Vertices = vertices; } // indexer for accessing vertex with id of index - public Vertex<T> this[int index] { get => Vertices.Find(vertex => vertex.Id == index); } + public T this[int index] { get => Vertices.Find(vertex => vertex.Id == index); } // create virtex if needed, then add the edge to it's edges list - public void AddEdge(Edge<T> edge) + public void AddEdge(U edge) { // look for existing vertex, and use it if found - Vertex<T> vertex = Vertices.Find(u => u.Id == edge.U); + T vertex = Vertices.Find(u => u.Id == edge.U); + // if not found, create a new vertex if (vertex == null) { - // if not found, create a new vertex - Vertices.Add(vertex = new Vertex<T>(edge.U)); - vertex = Vertices[Vertices.Count - 1]; + // weird constructor call to avoid using more generics + vertex = new T(); + vertex.Id = edge.U; + Vertices.Add(vertex); } - + // add the edge to the new/found vertex vertex.Edges.Add(edge); } |