-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.hpp
257 lines (212 loc) · 5.93 KB
/
common.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#pragma once
#include <cstdint>
#include <iterator>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <locale>
#include <memory>
#include <ctime>
#include <cstring>
#include <cassert>
#include "emphf_config.hpp"
namespace emphf {
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 << ": ";
}
// XXX(ot): the following I/O code is adapted from succinct
// library, avoiding the dependency for now
typedef std::pair<uint8_t const*, uint8_t const*> byte_range_t;
struct identity_adaptor
{
byte_range_t operator()(byte_range_t s) const
{
return s;
}
};
struct stl_string_adaptor
{
byte_range_t operator()(std::string const& s) const
{
const uint8_t* buf = reinterpret_cast<uint8_t const*>(s.c_str());
const uint8_t* end = buf + s.size() + 1; // add the null terminator
return byte_range_t(buf, end);
}
};
class line_iterator
: public std::iterator<std::forward_iterator_tag, const std::string> {
public:
line_iterator()
: m_is(nullptr)
, m_buf(nullptr)
{}
line_iterator(FILE* is)
: m_is(is)
, m_pos(0)
, m_buf(nullptr)
, m_buf_len(0)
{
advance();
}
~line_iterator()
{
free(m_buf);
}
value_type const& operator*() const {
return m_line;
}
line_iterator& operator++() {
advance();
return *this;
}
friend bool operator==(line_iterator const& lhs, line_iterator const& rhs)
{
if (!lhs.m_is || !rhs.m_is) {
if (!lhs.m_is && !rhs.m_is) {
return true;
} else {
return false;
}
}
assert(lhs.m_is == rhs.m_is);
return rhs.m_pos == lhs.m_pos;
}
friend bool operator!=(line_iterator const& lhs, line_iterator const& rhs)
{
return !(lhs == rhs);
}
private:
void advance()
{
assert(m_is);
fseek(m_is, m_pos, SEEK_SET);
// this is significantly faster than std::getline on C++
// streams
auto avail = getline(&m_buf, &m_buf_len, m_is);
if (avail == -1) {
m_is = nullptr;
return;
}
m_pos = ftell(m_is);
// trim newline character
if (avail && m_buf[avail - 1] == '\n') {
avail -= 1;
}
m_line.assign(m_buf, m_buf + avail);
}
FILE* m_is;
long m_pos;
std::string m_line;
char* m_buf;
size_t m_buf_len;
};
class file_lines
{
public:
file_lines(const char* filename)
{
m_is = fopen(filename, "rb");
if (!m_is) {
throw std::invalid_argument("Error opening " + std::string(filename));
}
}
~file_lines()
{
fclose(m_is);
}
line_iterator begin() const
{
return line_iterator(m_is);
}
line_iterator end() const { return line_iterator(); }
size_t size() const
{
size_t lines = 0;
fseek(m_is, 0, SEEK_SET);
static const size_t buf_size = 4096;
char buf[buf_size];
size_t avail;
bool last_is_newline = false;
while ((avail = fread(buf, 1, buf_size, m_is))) {
for (size_t i = 0; i < avail; ++i) {
if (buf[i] == '\n') lines += 1;
}
last_is_newline = (buf[avail - 1] == '\n');
}
if (!last_is_newline) lines += 1;
return lines;
}
private:
// noncopyble
file_lines(file_lines const&);
file_lines& operator=(file_lines const&);
FILE* m_is;
};
template <typename Iterator>
struct iter_range
{
iter_range(Iterator b, Iterator e)
: m_begin(b)
, m_end(e)
{}
Iterator begin() const
{ return m_begin; }
Iterator end() const
{ return m_end; }
Iterator m_begin, m_end;
};
template <typename Iterator>
iter_range<Iterator> range(Iterator begin, Iterator end)
{
return iter_range<Iterator>(begin, end);
}
uint64_t nonzero_pairs(uint64_t x)
{
static const uint64_t ones_step_4 = 0x1111111111111111ULL;
x = (x | (x >> 1)) & (0x5 * ones_step_4);
#if EMPHF_USE_POPCOUNT
return (uint64_t)__builtin_popcountll(x);
#else
static const uint64_t ones_step_8 = 0x0101010101010101ULL;
x = (x & 3 * ones_step_4) + ((x >> 2) & 3 * ones_step_4);
x = (x + (x >> 4)) & 0x0f * ones_step_8;
return (x * ones_step_8) >> 56;
#endif
}
inline uint64_t msb(uint64_t x)
{
assert(x);
return 63 - __builtin_clzll(x);
}
struct uninitialized_uint64 {
uninitialized_uint64() {}
uninitialized_uint64& operator=(uint64_t v)
{
m_val = v;
return *this;
}
operator uint64_t&()
{
return m_val;
}
operator uint64_t const&() const
{
return m_val;
}
private:
uint64_t m_val;
};
}