-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.h
431 lines (347 loc) · 12 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
Copyright (c) 2015 Genome Research Ltd.
Author: Jouni Siren <jouni.siren@iki.fi>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _BWTMERGE_UTILS_H
#define _BWTMERGE_UTILS_H
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <sdsl/wavelet_trees.hpp>
namespace bwtmerge
{
//------------------------------------------------------------------------------
typedef std::uint64_t size_type;
typedef std::uint8_t char_type;
typedef std::uint8_t comp_type;
typedef std::uint8_t byte_type;
const size_type WORD_BITS = 64;
const size_type BYTE_BITS = 8;
const size_type KILOBYTE = 1024;
const size_type MILLION = 1000000;
const size_type MEGABYTE = KILOBYTE * KILOBYTE;
const size_type GIGABYTE = KILOBYTE * MEGABYTE;
const double BYTE_BITS_DOUBLE = 8.0;
const double KILOBYTE_DOUBLE = 1024.0;
const double MILLION_DOUBLE = 1000000.0;
const double MEGABYTE_DOUBLE = KILOBYTE_DOUBLE * KILOBYTE_DOUBLE;
const double GIGABYTE_DOUBLE = KILOBYTE_DOUBLE * MEGABYTE_DOUBLE;
//------------------------------------------------------------------------------
/*
range_type stores a closed range [first, second]. Empty ranges are indicated by
first > second. The emptiness check uses +1 to handle a common special case
[0, -1].
*/
typedef std::pair<size_type, size_type> range_type;
struct Range
{
inline static size_type length(range_type range)
{
return range.second + 1 - range.first;
}
inline static bool empty(range_type range)
{
return (range.first + 1 > range.second + 1);
}
inline static size_type bound(size_type value, range_type bounds)
{
return bound(value, bounds.first, bounds.second);
}
inline static size_type bound(size_type value, size_type low, size_type high)
{
return std::max(std::min(value, high), low);
}
inline static range_type empty_range()
{
return range_type(1, 0);
}
};
template<class A, class B>
std::ostream& operator<<(std::ostream& stream, const std::pair<A, B>& data)
{
return stream << "(" << data.first << ", " << data.second << ")";
}
//------------------------------------------------------------------------------
/*
RunBuffer transforms a sequence of values or runs into a sequence of maximal runs.
Usage:
RunBuffer buffer;
while(...)
{
if(buffer.add(...)) { doSomething(buffer.run); }
}
buffer.flush(); doSomething(buffer.run);
*/
struct RunBuffer
{
RunBuffer() : value(0), length(0), run(0, 0) { }
inline bool add(size_type v, size_type n = 1)
{
if(v == value) { length += n; return false; }
else
{
this->flush();
this->value = v; this->length = n;
return (this->run.second > 0);
}
}
inline bool add(range_type run) { return this->add(run.first, run.second); }
inline void flush() { this->run.first = this->value; this->run.second = this->length; }
size_type value, length;
range_type run;
};
//------------------------------------------------------------------------------
template<class IntegerType>
inline size_type
bit_length(IntegerType val)
{
return sdsl::bits::hi(val) + 1;
}
//------------------------------------------------------------------------------
const size_type FNV_OFFSET_BASIS = 0xcbf29ce484222325UL;
const size_type FNV_PRIME = 0x100000001b3UL;
inline size_type fnv1a_hash(byte_type b, size_type seed)
{
return (seed ^ b) * FNV_PRIME;
}
inline size_type fnv1a_hash(size_type val, size_type seed)
{
byte_type* chars = (byte_type*)&val;
for(size_type i = 0; i < sizeof(val); i++) { seed = fnv1a_hash(chars[i], seed); }
return seed;
}
template<class ByteArray>
size_type fnv1a_hash(ByteArray& array)
{
size_type res = FNV_OFFSET_BASIS;
for(size_type i = 0; i < array.size(); i++) { res = fnv1a_hash((byte_type)(array[i]), res); }
return res;
}
//------------------------------------------------------------------------------
inline double
inMegabytes(size_type bytes)
{
return bytes / MEGABYTE_DOUBLE;
}
inline double
inGigabytes(size_type bytes)
{
return bytes / GIGABYTE_DOUBLE;
}
inline double
inBPC(size_type bytes, size_type size)
{
return (BYTE_BITS_DOUBLE * bytes) / size;
}
inline double
inMicroseconds(double seconds)
{
return seconds * MILLION_DOUBLE;
}
const size_type DEFAULT_INDENT = 18;
void printHeader(const std::string& header, size_type indent = DEFAULT_INDENT);
void printSize(const std::string& header, size_type bytes, size_type data_size, size_type indent = DEFAULT_INDENT);
void printTime(const std::string& header, size_type found, size_type matches, size_type bytes, double seconds, size_type indent = DEFAULT_INDENT);
void printTime(const std::string& header, size_type queries, double seconds, size_type indent = DEFAULT_INDENT);
void tokenize(const std::string& source, std::vector<std::string>& tokens, char delim);
//------------------------------------------------------------------------------
double readTimer(); // Seconds from an arbitrary time point.
size_type memoryUsage(); // Peak memory usage in bytes.
//------------------------------------------------------------------------------
// Returns the total length of the rows, excluding line ends.
size_type readRows(const std::string& filename, std::vector<std::string>& rows, bool skip_empty_rows);
std::string tempFile(const std::string& name_part);
size_type fileSize(std::ifstream& file);
size_type fileSize(std::ofstream& file);
//------------------------------------------------------------------------------
template<class Iterator, class Comparator>
void
sequentialSort(Iterator first, Iterator last, const Comparator& comp)
{
#ifdef _GLIBCXX_PARALLEL
std::sort(first, last, comp, __gnu_parallel::sequential_tag());
#else
std::sort(first, last, comp);
#endif
}
template<class Iterator>
void
sequentialSort(Iterator first, Iterator last)
{
#ifdef _GLIBCXX_PARALLEL
std::sort(first, last, __gnu_parallel::sequential_tag());
#else
std::sort(first, last);
#endif
}
//------------------------------------------------------------------------------
struct Parallel
{
static size_type max_threads;
static std::mutex stderr_access;
};
/*
Split the range approximately evenly between the blocks. The actual number of blocks
will not be greater than the length of the range or smaller than 1.
*/
std::vector<range_type> getBounds(range_type range, size_type blocks);
/*
Execute a loop over the range (semiopen) in (at most) block_count blocks with
(at most) thread_count threads. The blocks are represented as range_types (closed
ranges).
The function to be executed will get a reference to the ParallelLoop object as its
first argument. The function should call next() to get the next range to process,
with an empty range meaning that it should finish. The threads are joined when the
object is destroyed or when join() is explicitly called.
Like with std::thread, use std::ref() to give references as arguments to execute().
*/
class ParallelLoop
{
public:
ParallelLoop(size_type start, size_type limit, size_type block_count, size_type thread_count);
~ParallelLoop();
ParallelLoop(const ParallelLoop&) = delete;
ParallelLoop& operator= (const ParallelLoop&) = delete;
template<class Function, class... Args>
void execute(Function&& fn, Args&&... args)
{
for(size_type i = 0; i < this->threads.size(); i++)
{
this->threads[i] = std::thread(fn, std::ref(*this), args...);
}
}
range_type next();
void join();
std::atomic<size_type> tail;
std::vector<range_type> blocks;
std::vector<std::thread> threads;
};
//------------------------------------------------------------------------------
/*
BWT-merge uses a contiguous byte alphabet [0, sigma - 1] internally. Array C is based on the
number of occurrences of each character in the BWT.
*/
template<class AlphabetType>
inline bool
hasChar(const AlphabetType& alpha, comp_type comp)
{
return (alpha.C[comp + 1] > alpha.C[comp]);
}
template<class AlphabetType>
inline range_type
charRange(const AlphabetType& alpha, comp_type comp)
{
return range_type(alpha.C[comp], alpha.C[comp + 1] - 1);
}
template<class AlphabetType>
comp_type
findChar(const AlphabetType& alpha, size_type bwt_pos)
{
comp_type comp = 0;
while(alpha.C[comp + 1] <= bwt_pos) { comp++; }
return comp;
}
// Returns (LF(i), BWT[i]).
template<class BWTType, class AlphabetType>
inline range_type
LF(const BWTType& bwt, const AlphabetType& alpha, size_type i)
{
auto temp = bwt.inverse_select(i);
return range_type(temp.first + alpha.C[temp.second], temp.second);
}
template<class BWTType, class AlphabetType>
inline size_type
LF(const BWTType& bwt, const AlphabetType& alpha, size_type i, comp_type comp)
{
return alpha.C[comp] + bwt.rank(i, comp);
}
template<class BWTType, class AlphabetType>
inline range_type
LF(const BWTType& bwt, const AlphabetType& alpha, range_type range, comp_type comp)
{
return range_type(LF(bwt, alpha, range.first, comp), LF(bwt, alpha, range.second + 1, comp) - 1);
}
template<class BWTType, class AlphabetType>
inline size_type
Psi(const BWTType& bwt, const AlphabetType& alpha, size_type i)
{
comp_type comp = findChar(alpha, i);
return bwt.select(i + 1 - alpha.C[comp], comp);
}
//------------------------------------------------------------------------------
/*
Some SDSL extensions.
*/
/*
Utility methods for writing int_vector_buffer<k> compatible files, for k = 8, 16, 32, 64.
*/
template<class Element>
struct IntVectorBuffer
{
typedef uint64_t code_type;
static void writeHeader(std::ofstream& out, size_type elements)
{
size_type bits = elements * BYTE_BITS * sizeof(Element);
sdsl::write_member(bits, out);
}
static size_type readHeader(std::ifstream& in)
{
size_type bits = 0;
sdsl::read_member(bits, in);
return bits / (BYTE_BITS * sizeof(Element));
}
static void writeData(std::ofstream& out, Element* data, size_type elements)
{
size_type bytes = elements * sizeof(Element);
if(bytes % sizeof(code_type) != 0) { bytes += sizeof(code_type) - bytes % sizeof(code_type); }
char* ptr = (char*)data;
for(size_type i = elements * sizeof(Element); i < bytes; i++) { ptr[i] = 0; }
out.write(ptr, bytes);
}
static void readData(std::ifstream& in, Element* data, size_type elements)
{
size_type bytes = elements * sizeof(Element);
if(bytes % sizeof(code_type) != 0) { bytes += sizeof(code_type) - bytes % sizeof(code_type); }
in.read((char*)data, bytes);
}
};
/*
Generic in-memory construction from int_vector_buffer<8> and size. Not very space-efficient, as it
duplicates the data.
*/
template<class Type>
void
directConstruct(Type& structure, const sdsl::int_vector<8>& data)
{
std::string ramfile = sdsl::ram_file_name(sdsl::util::to_string(&structure));
sdsl::store_to_file(data, ramfile);
{
sdsl::int_vector_buffer<8> buffer(ramfile); // Must remove the buffer before removing the ramfile.
Type temp(buffer, data.size());
structure.swap(temp);
}
sdsl::ram_fs::remove(ramfile);
}
//------------------------------------------------------------------------------
} // namespace bwtmerge
#endif // _BWTMERGE_UTILS_H