forked from ot/partitioned_elias_fano
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.hpp
192 lines (159 loc) · 5.01 KB
/
util.hpp
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
#pragma once
#include <iostream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <locale>
#include <sys/time.h>
#include <sys/resource.h>
#include "succinct/broadword.hpp"
#define QS_LIKELY(x) __builtin_expect(!!(x), 1)
#define QS_UNLIKELY(x) __builtin_expect(!!(x), 0)
#define QS_NOINLINE __attribute__((noinline))
#define QS_ALWAYSINLINE __attribute__((always_inline))
#if defined(__GNUC__) && !defined(__clang__)
# define QS_FLATTEN_FUNC __attribute__((always_inline,flatten))
#else
# define QS_FLATTEN_FUNC QS_ALWAYSINLINE
#endif
namespace quasi_succinct {
inline uint64_t ceil_log2(const uint64_t x) {
assert(x > 0);
return (x > 1) ? succinct::broadword::msb(x - 1) + 1 : 0;
}
inline std::ostream& logger()
{
time_t t = std::time(nullptr);
// XXX(ot): put_time unsupported in g++ 4.7
// return std::cerr
// << std::put_time(std::localtime(&t), "%F %T")
// << ": ";
std::locale loc;
const std::time_put<char>& tp =
std::use_facet<std::time_put<char>>(loc);
const char *fmt = "%F %T";
tp.put(std::cerr, std::cerr, ' ',
std::localtime(&t), fmt, fmt + strlen(fmt));
return std::cerr << ": ";
}
inline double get_time_usecs() {
timeval tv;
gettimeofday(&tv, NULL);
return double(tv.tv_sec) * 1000000 + double(tv.tv_usec);
}
inline double get_user_time_usecs() {
rusage ru;
getrusage(RUSAGE_SELF, &ru);
return double(ru.ru_utime.tv_sec) * 1000000 + double(ru.ru_utime.tv_usec);
}
// stolen from folly
template <class T>
inline void do_not_optimize_away(T&& datum) {
asm volatile("" : "+r" (datum));
}
template<typename T>
struct has_next_geq
{
template<typename Fun> struct sfinae {};
template<typename U> static char test(sfinae<decltype(U::has_next)>);
template<typename U> static int test(...);
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
// A more powerful version of boost::function_input_iterator that also works
// with lambdas.
//
// Important: the functors must be stateless, otherwise the behavior is
// undefined.
template <typename State, typename AdvanceFunctor, typename ValueFunctor>
class function_iterator
: public std::iterator<std::forward_iterator_tag,
typename std::result_of<ValueFunctor(State)>::type> {
public:
function_iterator()
{}
function_iterator(State initial_state)
: m_state(initial_state)
{}
friend inline
void swap(function_iterator& lhs, function_iterator& rhs)
{
using std::swap;
swap(lhs.m_state, rhs.m_state);
}
// XXX why isn't this inherited from std::iterator?
typedef typename std::result_of<ValueFunctor(State)>::type value_type;
value_type operator*() const
{
// XXX I do not know if this trick is legal for stateless lambdas,
// but it seems to work on GCC and Clang
return (*static_cast<ValueFunctor*>(nullptr))(m_state);
}
function_iterator& operator++()
{
(*static_cast<AdvanceFunctor*>(nullptr))(m_state);
return *this;
}
function_iterator operator++(int)
{
function_iterator it(*this);
operator++();
return it;
}
bool operator==(function_iterator const& other) const
{
return m_state == other.m_state;
}
bool operator!=(function_iterator const& other) const
{
return !(*this == other);
}
private:
State m_state;
};
template <typename State, typename AdvanceFunctor, typename ValueFunctor>
function_iterator<State, AdvanceFunctor, ValueFunctor>
make_function_iterator(State initial_state, AdvanceFunctor, ValueFunctor)
{
return function_iterator<State, AdvanceFunctor, ValueFunctor>(initial_state);
}
struct stats_line {
stats_line()
: first(true)
{
std::cout << "{";
}
~stats_line()
{
std::cout << "}" << std::endl;
}
template <typename K, typename T>
stats_line& operator()(K const& key, T const& value)
{
if (!first) {
std::cout << ", ";
} else {
first = false;
}
emit(key);
std::cout << ": ";
emit(value);
return *this;
}
private:
template <typename T>
void emit(T const& v) const
{
std::cout << v;
}
// XXX properly escape strings
void emit(const char* s) const
{
std::cout << '"' << s << '"';
}
void emit(std::string const& s) const
{
emit(s.c_str());
}
bool first;
};
}