-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.h
206 lines (165 loc) · 5.34 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
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
//
// Created by André Schnabel on 23.10.15.
//
#pragma once
#include <cstdlib>
#include <vector>
#include <list>
#include <fstream>
#include <chrono>
#include <numeric>
#include <boost/format.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include "Stopwatch.h"
#include "Matrix.h"
#define LOG_I(msg) Utils::Logger::getInstance()->log(Utils::Logger::LogLevel::INFO, msg)
#define LOG_W(msg) Utils::Logger::getInstance()->log(Utils::Logger::LogLevel::WARNING, msg)
#define LOG_E(msg) Utils::Logger::getInstance()->log(Utils::Logger::LogLevel::ERROR, msg)
namespace Utils {
std::string slurp(const std::string &filename);
std::vector<std::string> readLines(const std::string &filename);
int extractIntFromStr(const std::string &s, const std::string &rx);
std::vector<int> extractIntsFromLine(const std::string &line);
template<class T>
void batchResize(int size, std::initializer_list<std::vector<T> *> vecs) {
for(auto v : vecs) v->resize(size);
}
inline int max(int a, int b) { return a > b ? a : b; }
inline int max(int a, int b, int c) { return max(max(a, b), c); }
inline int min(int a, int b) { return a < b ? a : b; }
inline int min(int a, int b, int c) { return min(min(a, b), c); }
void serializeSchedule(const std::vector<int> &sts, const std::string &filename);
void serializeProfit(float profit, const std::string &filename);
inline bool randBool() {
return rand() % 2 == 0;
}
inline int randRangeIncl(int lb, int ub) {
return lb + rand() % (ub-lb+1);
}
inline float randUnitFloat() {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
int pickWithDistribution(std::vector<float> &probs, float q = randUnitFloat());
template<class T>
bool rangeInclContains(const std::vector<T> &elems, int lb, int ub, int e) {
for(int i=lb; i<=ub; i++)
if(elems[i] == e) return true;
return false;
}
template<class T>
int indexOfNthEqualTo(int nth, T val, const std::vector<T> & coll) {
int xth = 0;
for (int k = 0; k < coll.size(); k++)
if (coll[k] == val) {
if (xth == nth) return k;
xth++;
}
throw std::runtime_error("No nth found!");
}
template<class T>
int indexOfFirstEqualTo(T val, const std::vector<T> & coll) {
return indexOfNthEqualTo(0, val, coll);
}
void spit(const std::string &s, const std::string &filename);
void spitAppend(const std::string &s, const std::string &filename);
template<class T>
void swap(std::vector<T> &v, int i1, int i2) {
T tmp = v[i1];
v[i1] = v[i2];
v[i2] = tmp;
}
template<class Func>
float maxInRangeIncl(int lb, int ub, Func transform) {
float r = std::numeric_limits<float>::lowest();
for(int i = lb; i<=ub; i++) {
float v = transform(i);
if(v > r) r = v;
}
return r;
}
template<class Func, class A, class B>
std::vector<B> mapVec(Func f, const std::vector<A> &elems) {
std::vector<B> res(elems.size());
for (int i = 0; i < elems.size(); i++)
res[i] = f(elems[i]);
return res;
}
template<class Func, class A, class B>
std::list<B> mapLst(Func f, std::list<A> &elems) {
std::list<B> res;
for(auto elem : elems)
res.push_back(elem);
return res;
}
template<class A, class Func>
std::vector<A> constructVector(int size, Func f) {
std::vector<A> v(size);
for(int i=0; i<size; i++) {
v[i] = f(i);
}
return v;
}
template<class A, class Func>
std::list<A> constructList(int size, Func f) {
std::list<A> l;
for(int i=0; i<size; i++) {
l.push_back(f(i));
}
return l;
}
std::list<std::string> filenamesInDir(const std::string &dir);
std::list<std::string> filenamesInDirWithExt(const std::string& dir, const std::string& ext);
inline bool int2bool(int i) {
return i != 0;
}
inline int bool2int(bool b) {
return b ? 1 : 0;
}
std::string formattedNow();
void partitionDirectory(const std::string& dirPath, int numPartitions, const std::string& infix = "_");
std::vector<int> deserializeSchedule(int njobs, const std::string &filename);
std::vector<std::string> splitLines(const std::string& s);
template<class T, class Pred>
int indexOf(std::vector<T> elems, Pred p) {
for(int i=0; i<elems.size(); i++) {
if (p(elems[i]))
return i;
}
return -1;
}
std::vector<std::string> parseArgumentList(int argc, const char** argv);
bool fileExists(const std::string &filename);
float average(const std::vector<int> &values);
float average(const std::vector<float> &values);
int sum(const std::vector<int> &values);
template<class Func>
int sum(Func f, int fromIncl, int toExcl) {
int acc = 0;
for(int i=fromIncl; i<toExcl; i++) {
acc += f(i);
}
return acc;
}
float variance(const std::vector<float> &values);
Matrix<char> transitiveClosure(const Matrix<char> &mx);
inline int sum(const Matrix<char> &mx) {
int accum = 0;
mx.foreach([&accum](int i, int j, char v) { accum += v; });
return accum;
}
template<class Func>
bool any(Func pred, int fromIncl, int toExcl) {
for (int i = fromIncl; i < toExcl; i++)
if (pred(i)) return true;
return false;
}
template<class Func>
int countPred(const Matrix<char> &mx, Func pred) {
int acc = 0;
mx.foreach([&acc, &pred](int i, int j, char v) {
acc += v && pred(i, j) ? 1 : 0;
});
return acc;
}
}