-
Notifications
You must be signed in to change notification settings - Fork 1
/
rlcsa_builder.cpp
347 lines (278 loc) · 7.96 KB
/
rlcsa_builder.cpp
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
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "rlcsa_builder.h"
#include "misc/utils.h"
#ifdef MULTITHREAD_SUPPORT
#include <omp.h>
#endif
namespace CSA
{
RLCSABuilder::RLCSABuilder(usint _block_size, usint _sample_rate, usint _buffer_size, usint _threads, RLCSA* _index) :
block_size(_block_size), sample_rate(_sample_rate), buffer_size(_buffer_size),
threads(_threads),
buffer(0)
{
this->reset();
this->build_time = this->search_time = this->sort_time = this->merge_time = 0.0;
if(_index != NULL)
{
this->setRLCSA(_index);
}
}
RLCSABuilder::~RLCSABuilder()
{
delete this->index;
delete[] this->buffer;
}
//--------------------------------------------------------------------------
void
RLCSABuilder::insertSequence(char* sequence, usint length, bool delete_sequence)
{
if(sequence == 0 || length == 0 || !this->ok)
{
if(delete_sequence) { delete[] sequence; }
return;
}
#ifdef MULTITHREAD_SUPPORT
omp_set_num_threads(this->threads);
#endif
if(this->buffer == 0)
{
this->addLongSequence((uchar*)sequence, length, delete_sequence);
return;
}
if(this->buffer_size - this->chars > length)
{
memcpy(this->buffer + this->chars, sequence, length);
if(delete_sequence) { delete[] sequence; }
this->chars += length;
this->buffer[this->chars] = 0;
this->chars++;
}
else
{
if(this->chars > 0) { this->flush(); }
if(length >= this->buffer_size - 1)
{
this->addLongSequence((uchar*)sequence, length, delete_sequence);
}
else
{
memcpy(this->buffer + this->chars, sequence, length);
if(delete_sequence) { delete[] sequence; }
this->chars += length;
this->buffer[this->chars] = 0;
this->chars++;
}
}
}
void
RLCSABuilder::insertFromFile(const std::string& base_name)
{
if(!this->ok) { return; }
#ifdef MULTITHREAD_SUPPORT
omp_set_num_threads(this->threads);
#endif
this->flush();
std::ifstream input(base_name.c_str(), std::ios_base::binary);
if(!input) { return; }
RLCSA* increment = new RLCSA(base_name);
usint data_size = increment->getSize() + increment->getNumberOfSequences();
uchar* data = new uchar[data_size];
input.read((char*)data, data_size);
input.close();
this->addRLCSA(increment, data, data_size, true);
}
void
RLCSABuilder::insertCollection(const std::string& base_name)
{
if(!this->ok) { return; }
#ifdef MULTITHREAD_SUPPORT
omp_set_num_threads(this->threads);
#endif
this->flush();
std::ifstream input(base_name.c_str(), std::ios_base::binary);
if(!input) { return; }
usint data_size = fileSize(input);
uchar* data = new uchar[data_size];
input.read((char*)data, data_size);
input.close();
if(this->index == 0)
{
this->addCollection(data, data_size, true);
return;
}
std::vector<usint> end_markers;
usint* ranks = this->getRanks(data, data_size, end_markers);
double start = readTimer();
#pragma omp parallel for schedule(static)
for(usint i = 0; i < data_size; i++) { ranks[i] += end_markers.size() + 1 + data[i]; }
for(usint i = 0; i < end_markers.size(); i++)
{
ranks[end_markers[i]] = this->index->getNumberOfSequences() + i;
}
RLCSA* increment = new RLCSA(data, ranks, data_size, this->block_size, this->sample_rate, this->threads, false);
#pragma omp parallel for schedule(static)
for(usint i = 0; i < data_size; i++) { ranks[i] -= data[i]; }
delete[] data;
double mark = readTimer();
this->build_time += mark - start;
parallelSort(ranks, ranks + data_size);
#pragma omp parallel for schedule(static)
for(usint i = end_markers.size(); i < data_size; i++) { ranks[i] += i - end_markers.size(); }
this->sort_time += readTimer() - mark;
this->mergeRLCSA(increment, ranks, data_size);
}
//--------------------------------------------------------------------------
RLCSA*
RLCSABuilder::getRLCSA()
{
if(this->chars > 0) { this->flush(); }
RLCSA* temp = this->index;
this->reset();
return temp;
}
char*
RLCSABuilder::getBWT(usint& length)
{
this->flush();
if(this->index == 0 || !(this->ok))
{
length = 0;
return 0;
}
length = this->index->getSize() + this->index->getNumberOfSequences();
return (char*)(this->index->readBWT());
}
bool
RLCSABuilder::isOk()
{
return this->ok;
}
double
RLCSABuilder::getBuildTime()
{
return this->build_time;
}
double
RLCSABuilder::getSearchTime()
{
return this->search_time;
}
double
RLCSABuilder::getSortTime()
{
return this->sort_time;
}
double
RLCSABuilder::getMergeTime()
{
return this->merge_time;
}
//--------------------------------------------------------------------------
void
RLCSABuilder::flush()
{
if(this->buffer == 0 || this->chars == 0) { return; }
double start = readTimer();
RLCSA* temp = new RLCSA(this->buffer, this->chars, this->block_size, this->sample_rate, this->threads, (this->index == 0));
this->build_time += readTimer() - start;
this->addRLCSA(temp, this->buffer, this->chars, (this->index != 0));
this->chars = 0;
this->buffer = new uchar[this->buffer_size];
}
void
RLCSABuilder::reset()
{
this->index = 0;
if(this->buffer_size != 0)
{
delete[] this->buffer;
this->buffer = new uchar[this->buffer_size];
}
this->chars = 0;
this->ok = true;
}
//--------------------------------------------------------------------------
void
RLCSABuilder::addRLCSA(RLCSA* increment, uchar* sequence, usint length, bool delete_sequence)
{
if(this->index == 0)
{
if(delete_sequence) { delete[] sequence; }
this->setRLCSA(increment);
return;
}
std::vector<usint> end_markers;
usint* ranks = this->getRanks(sequence, length, end_markers);
if(delete_sequence) { delete[] sequence; }
double mark = readTimer();
parallelSort(ranks, ranks + length);
#pragma omp parallel for schedule(static)
for(usint i = 0; i < length; i++) { ranks[i] += i + 1; }
this->sort_time += readTimer() - mark;
this->mergeRLCSA(increment, ranks, length);
}
void
RLCSABuilder::setRLCSA(RLCSA* new_index)
{
this->index = new_index;
this->ok &= this->index->isOk();
}
void
RLCSABuilder::mergeRLCSA(RLCSA* increment, usint* ranks, usint length)
{
double mark = readTimer();
RLCSA* merged = new RLCSA(*(this->index), *increment, ranks, this->block_size, this->threads);
delete[] ranks;
delete this->index;
delete increment;
this->index = merged;
this->merge_time += readTimer() - mark;
this->ok &= this->index->isOk();
}
//--------------------------------------------------------------------------
usint*
RLCSABuilder::getRanks(uchar* sequence, usint length, std::vector<usint>& end_markers)
{
double start = readTimer();
usint sequences = 0;
for(usint i = 0; i < length; i++)
{
if(sequence[i] == 0) { end_markers.push_back(i); sequences++; }
}
usint* ranks = new usint[length];
#ifdef MULTITHREAD_SUPPORT
usint chunk = std::max((usint)1, sequences / (8 * this->threads));
#endif
#pragma omp parallel for schedule(dynamic, chunk)
for(usint i = 0; i < sequences; i++)
{
usint begin = (i > 0 ? end_markers[i - 1] + 1 : 0);
this->index->reportPositions(sequence + begin, end_markers[i] - begin, ranks + begin);
}
this->index->strip();
this->search_time += readTimer() - start;
return ranks;
}
//--------------------------------------------------------------------------
void
RLCSABuilder::addLongSequence(uchar* sequence, usint length, bool delete_sequence)
{
double start = readTimer();
RLCSA* temp = new RLCSA(sequence, length, this->block_size, this->sample_rate, this->threads, 0, false);
this->build_time += readTimer() - start;
this->addRLCSA(temp, sequence, length, delete_sequence);
}
void
RLCSABuilder::addCollection(uchar* sequence, usint length, bool delete_sequence)
{
double start = readTimer();
RLCSA* temp = new RLCSA(sequence, length, this->block_size, this->sample_rate, this->threads, delete_sequence);
this->build_time += readTimer() - start;
this->setRLCSA(temp);
}
//--------------------------------------------------------------------------
} // namespace CSA