-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.cpp
177 lines (153 loc) · 4.23 KB
/
utility.cpp
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
/*
* utility.cpp
*
* Created on: May 19, 2015
* Author: trungvv1
*/
#include "utility.h"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cmath>
/**
* @brief Utility::readTsvData
* TSV file with first line storing the number of rows and columns
* @param filename
* @return
*/
vector<vector<double>> Utility::readTsvData(const string & filename) {
vector<vector<double>> data;
ifstream infile(filename);
if (!infile.is_open()) {
cout << "Open file error!" << endl;
return data;
}
uint m, n;
double value;
infile >> m >> n;
for (uint i = 0; i < m; i++) {
vector<double> row;
for (uint j = 0; j < n; j++) {
infile >> value;
// cout << value << endl;
row.push_back(value);
}
data.push_back(row);
}
infile.close();
// cout << data[1][4];
cout << "Read " << filename << " data successfully." << endl;
return data;
}
vector<vector<uint>> Utility::readTsvUintData(const string &filename)
{
vector<vector<uint>> data;
ifstream infile(filename);
if (!infile.is_open()) {
cout << "Open file error!" << endl;
return data;
}
uint m, n;
uint value;
infile >> m >> n;
for (uint i = 0; i < m; i++) {
vector<uint> row;
for (uint j = 0; j < n; j++) {
infile >> value;
// cout << value << endl;
row.push_back(value);
}
data.push_back(row);
}
infile.close();
cout << "Read " << filename << " data successfully." << endl;
return data;
}
void Utility::writeTsvData(const string & filename, const vector<vector<double>> & data) {
ofstream outfile(filename);
if (!outfile.is_open()) {
cout << "Write file error!" << endl;
return;
}
if (data.empty())
return;
outfile << data.size() << "\t" << data[0].size() << endl;
for (const auto & row : data) {
for (const auto & value : row) {
outfile << value << "\t";
}
outfile << endl;
}
outfile.close();
cout << "Write " << filename << " data successfully."<< endl;
}
void Utility::writeTsvUintData(const string &filename, const vector<vector<uint>> &data)
{
ofstream outfile(filename);
if (!outfile.is_open()) {
cout << "Write file error!" << endl;
return;
}
outfile << data.size() << "\t" << data[0].size() << endl;
for (const auto & row : data) {
for (const auto & value : row) {
outfile << value << "\t";
}
outfile << endl;
}
outfile.close();
cout << "Write " << filename << " uint data successfully."<< endl;
}
void Utility::writeString(const string& filename, const string &content) {
ifstream infile(filename);
if (infile.good())
{
cout << "file " << filename <<" has already existed! removing file..."<< endl;
if (std::remove(filename.c_str())) {
cout << "cannot remove " << filename<< endl;
}
}
infile.close();
ofstream outfile(filename);
if (!outfile.is_open())
{
cout << "Open file error!"<< endl;
return;
}
// Start writing data
outfile << content;
outfile.close();
cout << "Write " << filename << " file successfully."<< endl;
}
uint Utility::vectorToNumber(const vector<uint>& numValues, const vector<uint>& values) {
uint result = 0;
if (!numValues.empty())
{
uint n = numValues.size();
uint multiplier = 1;
for (uint i = n; i >= 1; --i) {
result += values.at(i-1) * multiplier;
multiplier *= numValues.at(i-1);
}
}
return result;
}
vector<uint> Utility::numberToVector(const vector<uint>& numValues, uint value) {
vector<uint> results;
if (!numValues.empty())
{
uint n = numValues.size();
uint multiplier = 1;
for (uint i = n - 1; i >= 1; --i) {
multiplier *= numValues.at(i);
}
for (uint i = 1; i < n; ++i) {
uint d = value % multiplier;
results.push_back((value - d) / multiplier);
value = d;
multiplier /= numValues.at(i);
}
results.push_back(value);
}
return results;
}