-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.h
126 lines (107 loc) · 3.19 KB
/
base.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
#ifndef BASE_H_
#define BASE_H_
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using std::map;
using std::pair;
using std::string;
using std::vector;
class OstreamVoidifier {
public:
// This has to be an operator with a precedence lower than << but
// higher than ?:
void operator&(::std::ostream&) {}
};
class Logger {
public:
Logger(const char* file, int line) {
stream() << file << ":" << line << ": ";
}
~Logger() {
stream() << std::endl;
}
std::ostream& stream() { return std::cerr; }
};
class FatalLogger : public Logger {
public:
FatalLogger(const char* file, int line, const char* message)
: Logger(file, line) {
stream() << "Assertion failed: " << message << " ";
}
~FatalLogger() {
stream() << std::endl;
exit(1);
}
std::ostream& stream() { return std::cerr; }
};
#define CHECK(x) (x) ? (void)0 \
: OstreamVoidifier() & FatalLogger(__FILE__, __LINE__, #x).stream()
#define CHECK_BINARY(test, x, y) \
CHECK((x) test (y)) << std::endl << "Left value: " << (x) << std::endl\
<< "Right value: " << (y) << std::endl
#define CHECK_EQ(x, y) CHECK_BINARY(==, x, y)
#define CHECK_EQ(x, y) CHECK_BINARY(==, x, y)
#define CHECK_NE(x, y) CHECK_BINARY(!=, x, y)
#define CHECK_LT(x, y) CHECK_BINARY(<, x, y)
#define CHECK_GT(x, y) CHECK_BINARY(>, x, y)
#define CHECK_LE(x, y) CHECK_BINARY(<=, x, y)
#define CHECK_GE(x, y) CHECK_BINARY(>=, x, y)
#define CHECK_NEAR(x, y, eps) \
CHECK(std::abs(double(x) - double(y)) <= double(eps))\
<< "Left value: " << (x) << std::endl\
<< "Right value: " << (y) << std::endl\
<< "Diff: " << std::endl
#define LOG Logger(__FILE__, __LINE__).stream()
#ifdef DEBUG
#define DLOG LOG
#else
#define DLOG true ? (void)0 : OstreamVoidifier() & std::cerr
#endif
inline bool IsValidLatitude(double lat) { return lat >= -90 && lat <= 90; }
inline bool IsValidLongitude(double lng) { return lng >= -180 && lng < 180; }
template<class T>
string PrintHistogram(const T& v) {
typedef typename T::value_type Value;
map<Value, int> count;
for (typename T::const_iterator it = v.begin(); it != v.end(); ++it) ++count[*it];
vector<pair<int, Value> > pairs;
for (typename map<Value, int>::const_iterator it = count.begin(); it != count.end(); ++it) {
pairs.push_back(std::make_pair(it->second, it->first));
}
std::sort(pairs.begin(), pairs.end());
std::reverse(pairs.begin(), pairs.end());
std::ostringstream os;
for (const auto& p : pairs) {
os << " #" << p.first << " of '" << p.second << "'\n";
}
return os.str();
}
template<class T>
string PrintList(const T& v) {
std::ostringstream os;
os << "[ ";
for (typename T::const_iterator it = v.begin(); it != v.end(); ++it) os << *it << " ";
os << "]";
return os.str();
}
template<class T>
vector<T> Sorted(const vector<T>& v) {
vector<T> s = v;
std::sort(s.begin(), s.end());
return s;
}
template<class T>
void SortAndRemoveDuplicates(vector<T>* v) {
std::sort(v->begin(), v->end());
v->erase(std::unique(v->begin(), v->end()), v->end());
}
#define kint32max 0x7ffffff
#endif // BASE_H_