summaryrefslogtreecommitdiffstats
path: root/IO/GraphFile.cs
diff options
context:
space:
mode:
Diffstat (limited to 'IO/GraphFile.cs')
-rw-r--r--IO/GraphFile.cs28
1 files changed, 14 insertions, 14 deletions
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