Skip to content
Snippets Groups Projects
Commit 1a51c78e authored by Paul Nitzke's avatar Paul Nitzke
Browse files

Major API change

* Problem: We needed a way to read in the matrix into a Matrix
  datastructure in addition to building the graph. This has multiple
  reasons:
  1. We may need to compute the compressed matrix using the input
     matrix.
  2. We need to obtain the number of rows of the input matrix to
     generate the seed matrix.
* This required major API changes, mainly the split of IReader's Read
  into ReadGraph and ReadMatrix
* I was able to avoid major code duplication in MMReader, nonetheless:
  The file is currently a mess that uses many shortcuts and is generally
  unclean
parent 145b00d9
No related branches found
No related tags found
1 merge request!22Non-minor rewrites of the design spec to accomodate new requirements
......@@ -25,11 +25,11 @@ public class Program
{
Output = Partition2ColoredDot(MMMatrix, o.TextColor);
}
else if (o.Format == Formats.ColoredDot)
else if (o.Format == Formats.SeedMatrix)
{
Output = Partition2SeedMatrix(MMMatrix);
}
else if (o.Format == Formats.ColoredDot)
else if (o.Format == Formats.CompressedMatrix)
{
Output = Partition2CompressedMatrix(MMMatrix);
}
......
......@@ -14,7 +14,7 @@ namespace SparseTransform
public static String Convert2Dot(String MMMatrix)
{
MatrixMarketReader reader = new MatrixMarketReader();
IGraph graph = reader.Read(MMMatrix);
IGraph graph = reader.ReadGraph(MMMatrix);
DotWriter writer = new DotWriter();
return writer.Write(graph);
}
......@@ -29,7 +29,7 @@ namespace SparseTransform
public static String Partition2ColoredDot(String MMMatrix, bool textColor= false)
{
MatrixMarketReader reader = new MatrixMarketReader();
IGraph graph = reader.Read(MMMatrix);
IGraph graph = reader.ReadGraph(MMMatrix);
Colorer color = new Colorer();
if (graph is AdjacencyGraph)
{
......@@ -52,7 +52,7 @@ namespace SparseTransform
public static String Partition2SeedMatrix(String MMMatrix)
{
MatrixMarketReader reader = new MatrixMarketReader();
IGraph graph = reader.Read(MMMatrix);
IGraph graph = reader.ReadGraph(MMMatrix);
Colorer color = new Colorer();
if (graph is AdjacencyGraph)
{
......@@ -63,7 +63,8 @@ namespace SparseTransform
}
else
color.PartialD2Color((BipartiteGraph)graph);
MatrixMarketWriter writer = new MatrixMarketWriter(true);
DoubleMatrix inputMatrix = reader.ReadMatrix(MMMatrix);
MatrixMarketWriter writer = new MatrixMarketWriter(true, inputMatrix);
return writer.Write(graph);
}
/// <summary>
......@@ -74,7 +75,7 @@ namespace SparseTransform
public static String Partition2CompressedMatrix(String MMMatrix)
{
MatrixMarketReader reader = new MatrixMarketReader();
IGraph graph = reader.Read(MMMatrix);
IGraph graph = reader.ReadGraph(MMMatrix);
Colorer color = new Colorer();
if (graph is AdjacencyGraph)
{
......@@ -85,7 +86,8 @@ namespace SparseTransform
}
else
color.PartialD2Color((BipartiteGraph)graph);
MatrixMarketWriter writer = new MatrixMarketWriter(false);
DoubleMatrix inputMatrix = reader.ReadMatrix(MMMatrix);
MatrixMarketWriter writer = new MatrixMarketWriter(false, inputMatrix);
return writer.Write(graph);
}
}
......
......@@ -15,7 +15,9 @@ namespace SparseTransform.Convert
/// </summary>
/// <param name="matrix"></param>
/// <returns>transformed graph <c>IGraph</c></returns>
public IGraph Read(String matrix);
public IGraph ReadGraph(String matrix);
public DoubleMatrix ReadMatrix(String matrix);
/// <summary>
......
......@@ -10,6 +10,7 @@ using Antlr4.Runtime;
using DataStructures;
using Transform;
using Transform.Convert;
using System.Text.RegularExpressions;
namespace SparseTransform.Convert
......@@ -17,7 +18,7 @@ namespace SparseTransform.Convert
internal class MatrixMarketReader : IReader
{
public IGraph Read(string matrix)
public IGraph ReadGraph(string matrix)
{
string[] lines = matrix.Split(
new string[] { "\r\n", "\r", "\n" },
......@@ -29,15 +30,40 @@ namespace SparseTransform.Convert
if (checkHeader(lines[0]))
{
BipartiteGraph graph = new BipartiteGraph();
return ReadGraph(lines, graph);
ReadContent(lines, (i, j, value) => {
graph.AddEdge(i, j);
});
return graph;
}
else
{
AdjacencyGraph graph = new AdjacencyGraph();
return ReadGraph(lines, graph);
ReadContent(lines, (i, j, value) => {
graph.AddEdge(i, j);
});
return graph;
}
}
public DoubleMatrix ReadMatrix(string matrix)
{
string[] lines = matrix.Split(
new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
checkComments(lines);
String dimLine = lines[getFirstDataLine(lines)];
String[] dimensions = dimLine.Split(" ");
DoubleMatrix mat = new DoubleMatrix(Int32.Parse(dimensions[0]), Int32.Parse(dimensions[1]));
ReadContent(lines, (i, j, value) =>
{
mat[i - 1, j - 1] = value;
});
return mat;
}
/// <summary>
/// checks with ANTLR4.0 whether the header is a valid instance. The header grammar can be found in the file MMFHeader.g4
/// </summary>
......@@ -95,13 +121,13 @@ namespace SparseTransform.Convert
/// <param name="graph"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public IGraph ReadGraph(string[] matrix, IGraph graph)
public void ReadContent(string[] matrix, Action<int, int, double> processor)
{
string[] values;
int lineCount = getFirstDataLine(matrix);
matrix[lineCount] = "% " + matrix[lineCount];
try
{
// try
// {
foreach (String line in matrix)
{
if (line != "")
......@@ -116,20 +142,19 @@ namespace SparseTransform.Convert
}
int xCoordinate = Int32.Parse(values[0]);
int yCoordinate = Int32.Parse(values[1]);
String value = values[2];
if (xCoordinate != yCoordinate)
{
graph.AddEdge(xCoordinate, yCoordinate);
}
// if (xCoordinate != yCoordinate)
// {
processor(xCoordinate, yCoordinate, Double.Parse(values[2]));
// graph.AddEdge(xCoordinate, yCoordinate);
// }
}
}
}
return graph;
}
catch (Exception e)
{
throw new Exception("data section mismatch on input");
}
// }
// catch (Exception e)
// {
// throw e;
// }
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment