-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
153 lines (122 loc) · 3.71 KB
/
Utils.cpp
File metadata and controls
153 lines (122 loc) · 3.71 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Created by serafeim on 27/5/2019.
//
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include "Utils.h"
using namespace std;
void Utils::usage() {
cerr << "Usage: ./run -c <config_file>" << endl;
}
int Utils::checkArg(int i, int max) {
if (i == max) {
usage();
exit(1);
}
return i + 1;
}
int Utils::get_column_idx(string filename, string fieldName) {
ifstream infile(filename);
if (infile.good()) {
string line, columnName;
getline(infile, line);
std::istringstream iss(line);
int i = 0;
while (getline(iss, columnName, '\t')) {
if (columnName == fieldName) {
infile.close();
return i;
}
i++;
}
}
infile.close();
return -1;
}
int Utils::get_max_column_value(string filename, int column_idx) {
ifstream infile;
infile.open(filename);
int max_value = -1;
if (infile.good()) {
string line;
while (getline(infile, line)) {
int i = 0;
std::istringstream iss(line);
while (getline(iss, line, '\t')) {
if (i == column_idx) {
int cur_value = strtol(line.c_str(), NULL, 10);
if (cur_value > max_value) {
max_value = cur_value;
}
}
i++;
}
}
}
infile.close();
return max_value;
}
void Utils::print(Eigen::SparseMatrix<int, RowMajor> *matrix_) {
for (int k=0; k < (*matrix_).outerSize(); ++k)
{
for (Eigen::SparseMatrix<int, RowMajor>::InnerIterator it(*matrix_, k); it; ++it)
{
std::cout << it.row() << "\t" << it.col() << "\t" << it.value() << endl;
}
}
std::cout << endl;
}
void Utils::log(string msg) {
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "[" << 1900 + ltm->tm_year;
cout << "-" << 1 + ltm->tm_mon;
cout << "-" << ltm->tm_mday;
cout << " "<< 1 + ltm->tm_hour << ":";
cout << 1 + ltm->tm_min << ":";
cout << 1 + ltm->tm_sec << "] ";
cout << msg << endl;
}
void Utils::logProgress(string msg) {
static int step = 0;
cout << "Associations Mining\t" << ++step << "\t" << msg;
}
void Utils::logTime(clock_t begin) {
cout << "\tTime = " << Utils::diffTime(begin) << endl;
}
double Utils::diffTime(clock_t begin) {
return double(clock() - begin) / CLOCKS_PER_SEC;
}
vector<TransitionMatrix*> Utils::slice(vector<TransitionMatrix*> matrices, size_t start, size_t len) {
return vector<TransitionMatrix*>(matrices.begin() + start, matrices.begin() + start + len);
}
vector<int> Utils::slice(vector<int> matrices, size_t start, size_t len) {
return vector<int>(matrices.begin() + start, matrices.begin() + start + len);
}
void Utils::split(string line, vector<string> &tokens, char delim) {
std::istringstream iss(line);
std::string token;
while(std::getline(iss, token, delim)) {
tokens.push_back(token);
}
}
void Utils::printConstraint(tuple<string, string, string> constraint) {
cout << "\t-\t[ C: " << get<0>(constraint) << ", P: " << get<1>(constraint) << ", V: " << get<2>(constraint) << " ]";
}
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
// trim from both ends (in place)
void Utils::trim(std::string &s) {
ltrim(s);
rtrim(s);
}