-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse.h
55 lines (50 loc) · 1.24 KB
/
sparse.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
#ifndef PROJ_SPARSE_H
#define PROJ_SPARSE_H
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <iterator>
#include <cstdlib>
class sparse_matrix {
public:
int num_rows;
int num_elements;
int *col_idx;
int *delim;
float *vals;
sparse_matrix(std::string fname) {
std::string filename(fname);
std::string line;
std::stringstream ss;
std::ifstream input_file(filename.c_str());
if (!input_file.is_open()) {
std::cerr << "Could not open the file - '"
<< filename << "'" << std::endl;
}
std::getline(input_file, line);
ss << line;
ss >> num_rows >> num_elements;
col_idx = new int[num_elements];
delim = new int[num_rows + 1];
vals = new float[num_elements];
std::stringstream iss;
std::getline(input_file, line);
iss << line;
for (int i = 0; i < num_elements; i++) {
iss >> col_idx[i];
}
std::getline(input_file, line);
std::stringstream jss;
jss << line;
for (int i = 0; i < num_rows; i++) {
jss >> delim[i];
}
}
~sparse_matrix() {
delete col_idx;
delete delim;
delete vals;
}
};
#endif //PROJ_SPARSE_H