-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_ktn.h
64 lines (53 loc) · 1.67 KB
/
read_ktn.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
/*
Read in kinetic transition network information from files "ts_conns.dat", "ts_weights.dat" and "stat_prob.dat"
*/
#ifndef __READ_KTN_H_INCLUDED__
#define __READ_KTN_H_INCLUDED__
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iterator>
#include <typeinfo>
using namespace std;
class Read_ktn {
public:
template <typename T> static vector<pair<T,T>> read_double_col(int,string);
template <typename T> static vector<T> read_single_col(int,string);
};
template <typename T>
vector<pair<T,T>> Read_ktn::read_double_col(int nlines, string inp_fname) {
string line;
ifstream inp_f;
inp_f.open(inp_fname);
vector<pair<T,T>> entries_pairs(nlines);
for (int i=0;i<nlines;i++) {
T min1, min2;
getline(inp_f,line);
istringstream min_pair(line);
auto itt = istream_iterator<string>(min_pair);
if (typeid(T)==typeid(int)) { min1 = stoi(*itt);
} else if (typeid(T)==typeid(double)) { min1 = stod(*itt); }
itt++;
if (typeid(T)==typeid(int)) { min2 = stoi(*itt);
} else if (typeid(T)==typeid(double)) { min2 = stod(*itt); }
entries_pairs[i] = make_pair(min1,min2);
}
inp_f.close();
return entries_pairs;
}
template <typename T>
vector<T> Read_ktn::read_single_col(int nlines, string inp_fname) {
string val_str;
vector<T> entries(nlines);
ifstream inp_f;
inp_f.open(inp_fname);
for (int i=0;i<nlines;i++) {
getline(inp_f,val_str);
if (typeid(T)==typeid(double)) { entries[i] = stod(val_str);
} else if (typeid(T)==typeid(int)) { entries[i] = stoi(val_str); }
}
inp_f.close();
return entries;
}
#endif