-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon_utils.h
210 lines (167 loc) · 5.36 KB
/
common_utils.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef COMMON_UTILS_COMMON_UTILS_H_
#define COMMON_UTILS_COMMON_UTILS_H_
#include "../array_core/configurator.h"
//#START_GRAB_TO_INCLUDES_LIST
#include <string>
#include <vector>
#include <iterator>
#include <map>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stdint.h>
#include <numeric>
#include <stdexcept>
#include <typeinfo>
//#STOP_GRAB_TO_INCLUDES_LIST
namespace dvs {
//#START_GRAB_TO_DVS_NAMESPACE
enum SEPARATOR_RESULT {
GOOD_SEPARATOR,
MORE_THAN_ONE_SEPARATOR,
NO_SEPARATOR,
MABE_COMMA_MABE_DOT,
UNDEFINED_BEHAVIOR
};
using std::string;
using std::vector;
string getCurrentPath();
bool is_file_exists(const string& file_name);
void openFileBySystem(const string& file_name);
bool isPlotlyScriptExists();
bool saveStringToFile(const string& file_name,
const string& data);
void mayBeCreateJsWorkingFolder();
void sleepMicroSec(unsigned long microsec);
void openPlotlyHtml(const string& file_name);
bool get_data_from_file(const string& path,
vector<std::string>& result);
vector<string> split(const string& target, char c);
bool readMatrix(vector<vector<double>>& outMatrix, const string& path, char dlmtr);
bool make_string(const string& src,
const vector<string>& args,
string& out);
// Now it doesn't work.
bool deleteFolder(const char* fname);
int find_separator(const std::string& src,
char& separator);
//! remove special characters except letters, numbers and '-', '_'. Spaces -> '_'
string removeSpecialCharacters(const string& s);
//! convert this cases to string "null" for Plotly
string nullIfNotFinite(double val);
//! convert vec to string, separated by ","
string vectorToString(const vector<double>& vec);
//! only name
string makeUniqueDavisHtmlName();
//! folder + name + .html
string makeUniqueDavisHtmlRelativePath();
//! sometimes std::to_string reurn str with ',' as separator what is wrong
template <typename T>
string toStringDotSeparator(T data) {
string str = std::to_string(data);
std::replace(str.begin(), str.end(), ',', '.');
return str;
}
//! save to disk vector<T> data
template <typename T>
bool saveVec(const vector<T>& vec, const string& filename, dv::configSaveToDisk config) {
if (vec.size() == 0) {
return false;
}
std::ofstream fout(filename);
if (!fout.is_open()) {
return false;
}
size_t rows = vec.size();
for (int i = 0; i < rows; ++i) {
fout << vec.at(i) << config.separatorOfRows;
}
fout.close();
return true;
}
//! save to disk vector<vector<T>> data
template <typename T>
bool saveVecVec(const vector<vector<T>>& vecVec, const string& filename, dv::configSaveToDisk config) {
if (vecVec.size() == 0) {
return false;
} else if (vecVec.at(0).size() == 0) {
return false;
}
std::ofstream fout(filename);
if (!fout.is_open()) {
return false;
}
if (config.isTranspose) {
size_t rows = vecVec.at(0).size();
size_t cols = vecVec.size();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
double val = vecVec.at(j).at(i);
fout << val;
if (j < cols - 1) { // we dont need sep at row end
fout << config.separatorOfCols;
}
}
fout << config.separatorOfRows;
}
} else {
size_t rows = vecVec.size();
size_t cols = vecVec.at(0).size();
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
double val = vecVec.at(i).at(j);
fout << val;
if (j < cols - 1) { // we dont need sep at row end
fout << config.separatorOfCols;
}
}
fout << config.separatorOfRows;
}
}
fout.close();
return true;
}
//! convert any container to std::vector with G type
template<typename G,
typename C, //https://devblogs.microsoft.com/oldnewthing/20190619-00/?p=102599
typename T = std::decay_t<decltype(*std::begin(std::declval<C>()))>,
typename = std::enable_if_t<std::is_convertible_v<T, double>>>
vector<G> vecFromTemplate(const C& container) {
vector<G> vec(container.size());
uint64_t i = 0;
for (auto v : container) {
vec[i] = static_cast<G>(v);
++i;
}
return vec;
}
bool is_string_convertable_to_digit(const string& sample);
void transponeMatrix(std::vector<std::vector<double>>& matrix);
vector<double> calculateAverageVector(const vector<vector<double>>& vectors);
vector<double> calculateStandardDeviation(const vector<double>& mean,
const vector<vector<double>>& data);
vector<double> doubleAndReverse(const vector<double>& input, const vector<double>& mean);
std::string reverseString(const std::string& input);
template <typename T>
std::vector<std::vector<T>> readBinaryFile(const std::string& filePath,
size_t rowLength) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
throw std::runtime_error("Cannot open file");
}
std::vector<std::vector<T>> data;
std::vector<T> row(rowLength);
while (file.read(reinterpret_cast<char*>(row.data()), rowLength * sizeof(T))) {
data.push_back(row);
}
// Handle the last row if it's not fully filled
if (file.gcount() > 0) {
row.resize(file.gcount() / sizeof(T));
data.push_back(row);
}
file.close();
return data;
}
//#STOP_GRAB_TO_DVS_NAMESPACE
}; // namespace dvs
#endif // COMMON_UTILS_COMMON_UTILS_H_