-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.hpp
268 lines (241 loc) · 8.88 KB
/
csv.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
258
259
260
261
262
263
264
265
266
267
268
#pragma once
#include <x86intrin.h>
#include <cassert>
#include <string>
#include <vector>
#include <thread>
#include "util.hpp"
namespace io::csv {
struct CharIter {
const char* iter;
const char* limit;
template<typename T>
static CharIter from_iterable(const T& iter) {
return CharIter{iter.begin(), iter.end()};
}
};
namespace {
template<char delim>
inline void find(CharIter& pos) {
auto&& [iter, limit] = pos;
const __m256i search_mask = _mm256_set1_epi8(delim);
auto limit32 = limit - 32;
while (iter < limit32) {
auto block = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(iter));
uint32_t matches = _mm256_movemask_epi8(_mm256_cmpeq_epi8(block, search_mask));
if (matches) {
iter += __builtin_ctz(matches);
return;
} else {
iter += 32;
}
}
while ((iter != limit) && ((*iter) != delim)) {
++iter;
}
}
template<char delim1, char delim2>
inline void find_either(CharIter& pos) {
auto&& [iter, limit] = pos;
const __m256i search_mask1 = _mm256_set1_epi8(delim1);
const __m256i search_mask2 = _mm256_set1_epi8(delim2);
auto limit32 = limit - 32;
while (iter < limit32) {
auto block = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(iter));
uint32_t matches = _mm256_movemask_epi8(_mm256_cmpeq_epi8(block, search_mask1))
| _mm256_movemask_epi8(_mm256_cmpeq_epi8(block, search_mask2));
if (matches) {
iter += __builtin_ctz(matches);
return;
}
iter += 32;
}
while ((iter != limit) && ((*iter) != delim1) && ((*iter) != delim2)) {
++iter;
}
}
template<char delim>
inline void find_nth(CharIter& pos, unsigned n) {
auto&& [iter, limit] = pos;
const __m256i search_mask = _mm256_set1_epi8(delim);
auto limit32 = limit - 32;
while (iter < limit32) {
auto block = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(iter));
uint32_t matches = _mm256_movemask_epi8(_mm256_cmpeq_epi8(block, search_mask));
if (matches) {
unsigned hits = _mm_popcnt_u32(matches);
if (hits < n) {
n -= hits;
iter += 32;
} else {
while (n > 1) {
matches &= matches - 1;
--n;
}
iter += __builtin_ctz(matches);
return;
}
} else {
iter += 32;
}
}
for (; iter != limit && n; ++iter) {
n -= ((*iter) == delim);
}
iter -= iter != limit;
}
template<char delim, char eol = '\n'>
inline size_t parse_unsigned(CharIter& pos) {
auto&& [iter, limit] = pos;
size_t v = *iter++ - '0';
for (;iter != limit; ++iter) {
char c = *iter;
if (c == delim || c == eol) break;
v = 10 * v + c - '0';
}
return v;
}
template<typename F, char delim, char eol = '\n'>
inline auto parse_from_to(F fn, CharIter& pos) {
auto start = pos.iter;
char* end = nullptr;
auto result = fn(start, &end);
assert(end != nullptr && (*end == delim || *end == eol));
pos.iter = end;
return result;
}
template<char delim, char eol = '\n', int base = 10>
inline long parse_int(CharIter& pos) {
auto start = pos.iter;
char* end = nullptr;
auto result = std::strtol(start, &end, base);
assert(end != nullptr && (*end == delim || *end == eol));
pos.iter = end;
return result;
}
template<char delim, char eol = '\n'>
inline double parse_double(CharIter& pos) {
auto start = pos.iter;
char* end = nullptr;
auto result = std::strtod(start, &end);
assert(end != nullptr && (*end == delim || *end == eol));
pos.iter = end;
return result;
}
template <typename Executor>
inline void parallel_exec(const Executor &executor, unsigned thread_count = std::thread::hardware_concurrency() / 2) {
std::vector<std::thread> threads;
for (auto thread_id = 1u; thread_id != thread_count; ++thread_id) {
threads.push_back(std::thread([thread_id, thread_count, &executor]() {
executor(thread_id, thread_count);
}));
}
executor(0, thread_count);
for (auto &thread : threads) {
thread.join();
}
}
} // namsepace
template<typename T>
struct Parser {
template<char delim, char eol = '\n'>
T parse_value(CharIter& pos) ;
};
template<>
struct Parser<char> {
static constexpr char TYPE_NAME[] = "char";
template <char delim, char eol = '\n'>
inline char parse_value(CharIter& pos) {
return *(pos.iter++);
}
};
template<>
struct Parser<unsigned long> {
static constexpr char TYPE_NAME[] = "long.unsigned";
template <char delim, char eol = '\n'>
inline unsigned long parse_value(CharIter& pos) {
return parse_unsigned<delim, eol>(pos);
}
};
template<>
struct Parser<unsigned> {
static constexpr char TYPE_NAME[] = "int.unsigned";
template <char delim, char eol = '\n'>
inline unsigned parse_value(CharIter& pos) {
return parse_unsigned<delim, eol>(pos);
}
};
template<>
struct Parser<long> {
static constexpr char TYPE_NAME[] = "long";
template <char delim, char eol = '\n'>
inline long parse_value(CharIter& pos) {
return parse_int<delim, eol>(pos);
}
};
template<>
struct Parser<int> {
static constexpr char TYPE_NAME[] = "int";
template <char delim, char eol = '\n'>
inline int parse_value(CharIter& pos) {
return parse_int<delim, eol>(pos);
}
};
template<>
struct Parser<double> {
static constexpr char TYPE_NAME[] = "double";
template <char delim, char eol = '\n'>
inline double parse_value(CharIter& pos) {
return parse_double<delim, eol>(pos);
}
};
template<>
struct Parser<std::string_view> {
static constexpr char TYPE_NAME[] = "string";
template <char delim, char eol = '\n'>
inline std::string_view parse_value(CharIter& pos) {
auto start = pos.iter;
find_either<delim, eol>(pos);
return std::string_view(start, pos.iter - start);
}
};
template<char delim = ',', char eol = '\n', typename Consumer>
inline bool read_line(CharIter& pos, const std::vector<unsigned>& cols, const Consumer& consumer) {
unsigned skipped = 0;
for (auto col : cols) {
auto n = col - skipped;
switch(n) {
case 0:
break;
case 1:
find<delim>(pos);
break;
default:
find_nth<delim>(pos, n);
}
if (pos.iter >= pos.limit) { return false; }
pos.iter += *pos.iter == delim;
consumer(col, pos);
skipped = col + 1;
pos.iter += pos.iter < pos.limit && *pos.iter != eol;
}
if (pos.iter < pos.limit && *pos.iter != eol) { find<eol>(pos); }
pos.iter += pos.iter != pos.limit;
return true;
}
template<char delim = ',', char eol = '\n', typename Consumer>
inline std::size_t read_file(const char* filename, const std::vector<unsigned>& cols, const Consumer& consumer) {
MMapping<char> input(filename);
auto pos = CharIter::from_iterable(input);
std::size_t lines{0};
while (read_line<delim, eol, Consumer>(pos, cols, consumer)) { ++lines; }
return lines;
}
template<char delim = ',', char eol = '\n', typename Consumer>
inline std::size_t read_file(const MMapping<char>& input, const std::vector<unsigned>& cols, const Consumer& consumer) {
auto pos = CharIter::from_iterable(input);
std::size_t lines{0};
while (read_line<delim, eol, Consumer>(pos, cols, consumer)) { ++lines; }
return lines;
}
}