Browse code

Optimized matrix constructor and improved readibility

Tiger Gao authored on 08/06/2018 16:59:24
Showing 3 changed files

... ...
@@ -94,25 +94,16 @@ public:
94 94
 template <class Matrix, class Parser>
95 95
 inline void fill(Matrix &mat, Parser &p, bool partitionRows, std::vector<unsigned> whichIndices)
96 96
 {
97
-    // TODO implement
97
+    unsigned rowSelect = partitionRows ? 0 : 1;
98
+    unsigned colSelect = partitionRows ? 1 : 0;
99
+    
98 100
     while (p.hasNext())
99 101
     {
100 102
         MatrixElement e(p.getNext());
101
-        if (partitionRows)
102
-        {
103
-            std::vector<unsigned>::iterator newRowIndex = std::find(whichIndices.begin(), whichIndices.end(), e.row);
104
-            if (newRowIndex != whichIndices.end())
105
-            {
106
-                mat.operator()(std::distance(whichIndices.begin(), newRowIndex), e.col) = e.value;
107
-            }
108
-        }
109
-        else //transpose
103
+        std::vector<unsigned>::iterator newRowIndex = std::find(whichIndices.begin(), whichIndices.end(), e[rowSelect]);
104
+        if (newRowIndex != whichIndices.end())
110 105
         {
111
-            std::vector<unsigned>::iterator newRowIndex = std::find(whichIndices.begin(), whichIndices.end(), e.col);
112
-            if (newRowIndex != whichIndices.end())
113
-            {
114
-                mat.operator()(std::distance(whichIndices.begin(), newRowIndex), e.row) = e.value;
115
-            }
106
+            mat.operator()(std::distance(whichIndices.begin(), newRowIndex), e[colSelect]) = e.value;
116 107
         }
117 108
     }
118 109
 }
... ...
@@ -6,20 +6,39 @@
6 6
 
7 7
 struct MatrixElement
8 8
 {
9
-    unsigned row;
10
-    unsigned col;
9
+    unsigned dim[2];
11 10
     float value;
12 11
 
13 12
     MatrixElement(unsigned r, unsigned c, float v)
14
-        : row(r), col(c), value(v)
15
-    {}
13
+        : value(v)
14
+    {
15
+        dim[0] = r;
16
+        dim[1] = c;
17
+    }
16 18
 
17 19
     MatrixElement(unsigned r, unsigned c, const std::string &s)
18
-        : row(r), col(c), value(0.f)
20
+        :  value(0.f)
19 21
     {
22
+        dim[0] = r;
23
+        dim[1] = c;
20 24
         std::stringstream ss(s);
21 25
         ss >> value;
22 26
     }
27
+
28
+    unsigned operator[](unsigned i)
29
+    {
30
+        return dim[i];
31
+    }
32
+
33
+    unsigned row() const
34
+    {
35
+        return dim[0];
36
+    }
37
+
38
+    unsigned col() const
39
+    {
40
+        return dim[1];
41
+    }
23 42
 };
24 43
 
25
-#endif
26 44
\ No newline at end of file
45
+#endif
... ...
@@ -14,7 +14,7 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0)
14 14
         std::getline(mFile, line);
15 15
     }
16 16
     std::stringstream ss(line); // this line contains dimensions
17
-    
17
+
18 18
     // store dimensions
19 19
     ss >> mNumRows >> mNumCols;
20 20
 }
... ...
@@ -27,6 +27,6 @@ bool MtxParser::hasNext()
27 27
 MatrixElement MtxParser::getNext()
28 28
 {
29 29
     MatrixElement e(0, 0, 0.f);
30
-    mFile >> e.row >> e.col >> e.value;
30
+    mFile >> e.dim[0] >> e.dim[1] >> e.value;
31 31
     return e;
32 32
 }