-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtils.h
97 lines (69 loc) · 1.89 KB
/
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
///////////////////////////////////////////////////////////////////////////////
// File name: Utils.h
// This file defines the class of Utils.
// It constains some auxillary functions.
// Lantao Liu, Dec 8, 2011
///////////////////////////////////////////////////////////////////////////////
#ifndef UTILS_H
#define UTILS_H
#include "Define.h"
#include "Assignment.h"
#include <sys/time.h>
#include <time.h>
#include <unistd.h> // for usleep()
#include <math.h> // for pow, sin, cos
#include <cfloat> // DBL_MIN, DBL_MAX
#define QUIET
#define LOG_DIR "outlog/"
class Utils{
public:
Utils();
~Utils();
public:
double GetStartTime(void){ return start_time; }
double GetLastTime(void){ return last_time; }
double GetCurTime(void);
void Stats(const vector<double>& _data,
double& _sum, double& _avg, double& _var);
void WriteMatrix(const mat& _m, char* _fname);
void WriteMatrix(Assignment& _as, char* _fname=(char*)DEBUG_FILE);
template<typename T>
void WriteVec(const vector<T>&, char* _fname);
template<typename T>
void WriteVecAppend(const vector<T>&, char* _fname);
void OutLog(void);
template<typename T>
void DisplayVec(const vector<T>&);
public:
double start_time;
double last_time;
};
template<typename T>
void
Utils::WriteVec(const vector<T>& _vec, char* _fname){
ofstream fout(_fname);
if(!fout)
cerr << "Cannot create output file"<<endl;
for(uint i=0; i<_vec.size(); i++)
fout<<_vec[i]<<" ";
fout<<endl;
}
template<typename T>
void
Utils::WriteVecAppend(const vector<T>& _vec, char* _fname){
ofstream fout;
fout.open(_fname, ios::app); // open file for appending
assert(!fout.fail());
for(uint i=0; i<_vec.size(); i++)
fout<<_vec[i]<<" ";
fout<<endl;
fout.close();
}
template<typename T>
void
Utils::DisplayVec(const vector<T>& _vec){
for(uint i=0; i<_vec.size(); i++)
cout<<_vec[i]<<" ";
cout<<endl;
}
#endif