-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.h
122 lines (94 loc) · 2.12 KB
/
helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//Helper functions to read a symmetric pattern sparse matrix from Matrix Market format
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <sstream>
using namespace std;
struct CSC {
vector<int> row_ind;
vector<int> col_ptr;
};
void printArray(vector<int> v) {
for (int i = 0; i < v.size(); i++)
cout << v[i] << ' ';
cout << '\n';
}
void printArray(vector<double> v) {
for (int i = 0; i < v.size(); i++)
cout << v[i] << ' ';
cout << '\n';
}
void printCSCMatrix(CSC csc) {
int idx = 0;
int n = csc.col_ptr.size() - 1;
vector<int> col_ptr = csc.col_ptr;
vector<int> row_ind = csc.row_ind;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (idx < col_ptr[i + 1] && j == row_ind[idx])
{
cout << 1 << " ";
idx++;
}
else
cout << "0 ";
}
cout << "\n";
}
}
CSC readMTXSymPatToCSC(string fileName) {
vector<int> col_ptr;
vector<int> row_ind;
ifstream file(fileName);
int M, N, L;
bool isPattern = false;
bool isSymmetric = false;
bool foundHeader = false;
while (file.peek() == '%')
{
if (file.peek() == '%' && !foundHeader)
{
string line;
getline(file, line);
if (line.find("pattern") != std::string::npos)
isPattern = true;
if (line.find("symmetric") != std::string::npos)
{
isSymmetric = true;
}
foundHeader = true;
}
else
file.ignore(2048, '\n');
}
file >> M >> N >> L;
vector<vector<int>> mtxElements(L);
for (int l = 0; l < L; l++) {
int row, col;
file >> row >> col;
row--;
col--;
mtxElements[row].push_back(col);
if (isSymmetric)
mtxElements[col].push_back(row);
}
col_ptr.push_back(0);
for (int i = 0; i < L; i++) {
int size = mtxElements[i].size();
if (size > 0) {
col_ptr.push_back(size + col_ptr[i]);
for (int j = 0; j < size; j++) {
row_ind.push_back(mtxElements[i][j]);
}
}
else {
col_ptr.push_back(col_ptr[i]);
}
}
CSC matrix;
matrix.col_ptr = col_ptr;
matrix.row_ind = row_ind;
return matrix;
}