Changeset 7538 for branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBParser.cs
- Timestamp:
- 03/01/12 15:15:22 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBParser.cs
r7445 r7538 28 28 public double[,] Distances { get; private set; } 29 29 public double[,] Weights { get; private set; } 30 public Exception Error { get; private set; }31 30 32 31 public QAPLIBParser() { … … 38 37 Distances = null; 39 38 Weights = null; 40 Error = null;41 39 } 42 40 43 public boolParse(string file) {41 public void Parse(string file) { 44 42 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 45 returnParse(stream);43 Parse(stream); 46 44 } 47 45 } … … 55 53 /// <param name="stream">The stream to read data from.</param> 56 54 /// <returns>True if the file was successfully read or false otherwise.</returns> 57 public bool Parse(Stream stream) { 58 Error = null; 59 try { 60 StreamReader reader = new StreamReader(stream); 61 Size = int.Parse(reader.ReadLine()); 62 Distances = new double[Size, Size]; 63 Weights = new double[Size, Size]; 64 string valLine = null; 65 char[] delim = new char[] { ' ' }; 66 int read = 0; 67 int k = 0; 68 while (k < Size) { 69 if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading first matrix."); 70 valLine = reader.ReadLine(); 71 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); 72 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 73 foreach (string val in vals) { 74 Weights[k, read++] = double.Parse(val); 75 if (read == Size) { 76 read = 0; 77 k++; 78 } 55 public void Parse(Stream stream) { 56 var reader = new StreamReader(stream); 57 Size = int.Parse(reader.ReadLine()); 58 Distances = new double[Size, Size]; 59 Weights = new double[Size, Size]; 60 char[] delim = new char[] { ' ' }; 61 62 Distances = ParseMatrix(reader, delim); 63 Weights = ParseMatrix(reader, delim); 64 } 65 66 private double[,] ParseMatrix(StreamReader reader, char[] delim) { 67 int read = 0, k = 0; 68 double[,] result = new double[Size, Size]; 69 while (k < Size) { 70 if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix."); 71 string valLine = reader.ReadLine(); 72 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine(); 73 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries); 74 foreach (string val in vals) { 75 Distances[k, read++] = double.Parse(val); 76 if (read == Size) { 77 read = 0; 78 k++; 79 79 } 80 80 } 81 82 read = 0;83 k = 0;84 85 while (k < Size) {86 if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix.");87 valLine = reader.ReadLine();88 while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();89 string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);90 foreach (string val in vals) {91 Distances[k, read++] = double.Parse(val);92 if (read == Size) {93 read = 0;94 k++;95 }96 }97 }98 return true;99 } catch (Exception e) {100 Error = e;101 return false;102 81 } 82 return result; 103 83 } 104 84 }
Note: See TracChangeset
for help on using the changeset viewer.