diff options
-rw-r--r-- | Graph.cs | 8 | ||||
-rw-r--r-- | IO/FileReader.cs | 3 | ||||
-rw-r--r-- | IO/FileWriter.cs | 2 | ||||
-rw-r--r-- | IO/GraphFile.cs | 28 |
4 files changed, 23 insertions, 18 deletions
@@ -14,19 +14,23 @@ namespace Graph Vertices = vertices; } - // Indexer for accessing vertex with id of index + // indexer for accessing vertex with id of index public Vertex<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) { + // look for existing vertex, and use it if found Vertex<T> vertex = Vertices.Find(u => u.Id == edge.U); if (vertex == null) { + // if not found, create a new vertex Vertices.Add(vertex = new Vertex<T>(edge.U)); vertex = Vertices[Vertices.Count - 1]; } - + + // add the edge to the new/found vertex vertex.Edges.Add(edge); } diff --git a/IO/FileReader.cs b/IO/FileReader.cs index 12aa12b..a207be2 100644 --- a/IO/FileReader.cs +++ b/IO/FileReader.cs @@ -2,7 +2,8 @@ using System.IO; using Graph.Interfaces; namespace Graph.IO -{ +{ + // class for reading from file using IFileReader (i.e. Dependancy injection for unit testing) public class FileReader : IFileReader { public string[] Lines { get; } diff --git a/IO/FileWriter.cs b/IO/FileWriter.cs index 7e10f62..846b759 100644 --- a/IO/FileWriter.cs +++ b/IO/FileWriter.cs @@ -1,9 +1,9 @@ -using System; using System.IO; using Graph.Interfaces; namespace Graph.IO { + // class for writing to file using IFileWriter (i.e. Dependancy injection for unit testing) public class FileWriter : IFileWriter { string FilePath { get; } diff --git a/IO/GraphFile.cs b/IO/GraphFile.cs index 8562170..dc1d851 100644 --- a/IO/GraphFile.cs +++ b/IO/GraphFile.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; -using System.Linq; using System.Text.RegularExpressions; using Graph.Interfaces; @@ -7,6 +5,7 @@ namespace Graph.IO { public static class GraphFile { + // read a graph from file using IFileReader (i.e. Dependancy injection for unit testing) public static Graph<double> Read(IFileReader reader) { string[] lines = reader.Lines; @@ -42,31 +41,32 @@ namespace Graph.IO return graph; } + // write a graph to file using IFileWriter (i.e. Dependancy injection for unit testing) public static void Print(Graph<double> graph, IFileWriter writer) { + // create array of strings with a size equal to vertices string[] lines = new string[graph.Vertices.Count]; + + // add each vertex to it's parents output line graph.Vertices.ForEach(vertex => { + // do nothing if it has no parent if (vertex.Parent == null) return; + // needed due to making vertex.Parent nullable int parent = vertex.Parent.GetValueOrDefault(); - - lines[parent] ??= $"{parent}"; - lines[vertex.Id] ??= $"{vertex.Id}"; + // for vertex & parent, create string if not already + lines[vertex.Id] ??= $"{vertex.Id}"; + lines[parent] ??= $"{parent}"; + + // add vertex + weight to parent's string & visa versa lines[parent] += $" {vertex.Id} {vertex.Key.ToString("F1")}"; lines[vertex.Id] += $" {parent} {vertex.Key.ToString("F1")}"; }); - + // write the array of strings to file writer.WriteAllLines(lines); } } -} -// vertex.Edges.ForEach(e => line += $" {e.V} {e.Weight.ToString("F1")}"); - -// var neighbors = graph.Vertices -// .Where(u => u.Parent == vertex.Id && vertex.Edges.Exists(e => e.V == u.Id)); - -// var edges = vertex.Edges -// .Where(e => graph.Vertices.Find(v => v.Id == e.V).Parent == vertex.Id);
\ No newline at end of file +}
\ No newline at end of file |