-
Notifications
You must be signed in to change notification settings - Fork 1
/
adaptive_samples.cpp
323 lines (275 loc) · 8.77 KB
/
adaptive_samples.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include "adaptive_samples.h"
#include "misc/utils.h"
namespace CSA
{
//--------------------------------------------------------------------------
AdaptiveSamples::AdaptiveSamples(const RLCSA& rlcsa, const std::string& base_name) :
index(rlcsa),
samples(0), size(0), items(0),
candidate_samples(0), c_items(0),
regular_samples(0),
text_start(0),
promote_probability(0.0),
pos(0), sum(0), window(0),
ok(false)
{
Parameters parameters;
parameters.read(base_name + PARAMETERS_EXTENSION);
this->use_candidates = parameters.get(CANDIDATE_SAMPLES);
this->half_greedy = parameters.get(HALF_GREEDY_SAMPLES);
this->window_size = parameters.get(SAMPLE_WINDOW_SIZE);
if(parameters.get(SAMPLE_PROMOTE_RATE) > 0)
{
this->promote_probability = 1.0 / parameters.get(SAMPLE_PROMOTE_RATE);
}
std::string file_name = base_name + SA_SAMPLES_EXTENSION;
std::ifstream sample_file(file_name.c_str(), std::ios_base::binary);
if(!sample_file)
{
std::cerr << "AdaptiveSamples: Cannot open sample file!" << std::endl;
return;
}
// Determine the number of adaptive, candidate, and regular samples.
usint total_samples = 0;
usint sample_rate = parameters.get(SAMPLE_RATE);
sample_file.read((char*)&(total_samples), sizeof(total_samples)); // Actually text size.
sample_file.read((char*)&(total_samples), sizeof(total_samples));
this->size = (this->half_greedy ? total_samples / 2 : total_samples);
if(this->use_candidates) { this->size /= 2; }
// Initialize the samples.
this->samples = new sample_type[this->size];
for(key_type i = 0; i < this->size; i++)
{
this->samples[i] = sample_type(this->index.getSize(), this->index.getSize());
}
if(this->use_candidates)
{
this->candidate_samples = new sample_type[this->size];
for(key_type i = 0; i < this->size; i++) { this->candidate_samples[i] = this->samples[i]; }
}
// Insert the initial samples.
pair_type buffer;
pair_type* regulars = 0;
usint reg_samples = 0;
if(this->half_greedy) { regulars = new pair_type[total_samples / 2]; }
for(usint i = 0; i < total_samples; i++)
{
sample_file.read((char*)&buffer, sizeof(pair_type));
if(this->half_greedy && buffer.second % (2 * sample_rate) == 0)
{
regulars[reg_samples++] = buffer;
}
else
{
this->addSample(sample_type(buffer.first, buffer.second));
}
if(buffer.second == 0) { this->text_start = buffer.first; }
}
if(this->half_greedy)
{
this->regular_samples = new SASamples(regulars, this->index.getSize(), 2 * sample_rate, 1);
delete[] regulars; regulars = 0;
}
// Initialize the window.
if(this->promote_probability > 0.0)
{
this->window = new key_type[this->window_size];
for(usint i = 0; i < this->window_size; i++) { this->window[i] = sample_rate / 2; }
this->sum = this->window_size * sample_rate / 2;
}
sample_file.close();
this->ok = true;
}
AdaptiveSamples::~AdaptiveSamples()
{
delete[] samples; samples = 0;
delete[] candidate_samples; candidate_samples = 0;
delete regular_samples; regular_samples = 0;
delete[] window; window = 0;
}
//--------------------------------------------------------------------------
usint
AdaptiveSamples::getNumberOfSamples() const
{
usint temp = this->items + this->c_items;
if(this->half_greedy) { temp += this->regular_samples->getNumberOfSamples(); }
return temp;
}
double
AdaptiveSamples::getLoad() const
{
double temp = this->items + this->c_items;
temp /= (this->use_candidates ? 2 * this->size : this->size);
return temp;
}
double
AdaptiveSamples::getPrimaryLoad() const
{
return this->items / (double)(this->size);
}
double
AdaptiveSamples::getCandidateLoad() const
{
return this->c_items / (double)(this->size);
}
usint
AdaptiveSamples::reportSize() const
{
usint temp = sizeof(*this) + this->size * sizeof(sample_type);
if(this->use_candidates) { temp += this->size * sizeof(sample_type); }
if(this->half_greedy) { temp += this->regular_samples->reportSize(); }
if(this->promote_probability > 0.0) { temp += this->window_size * sizeof(key_type); }
return temp;
}
void
AdaptiveSamples::report() const
{
std::cout << "Adaptive samples:" << std::endl;
std::cout << "Size: " << this->reportSize() / (double)MEGABYTE << " MB" << std::endl;
std::cout << "Load: " << this->getLoad() << " (" << this->getPrimaryLoad() << " / " << this->getCandidateLoad() << ")" << std::endl;
std::cout << "Options: ";
if(this->use_candidates) { std::cout << " candidates"; }
if(this->half_greedy) { std::cout << " half-greedy"; }
if(this->promote_probability > 0.0)
{
std::cout << " random(" << this->promote_probability << ", " << this->window_size << ")";
}
std::cout << std::endl;
std::cout << std::endl;
}
//--------------------------------------------------------------------------
usint
AdaptiveSamples::locate(usint i, bool steps)
{
if(i >= this->index.getSize()) { return (steps ? 0 : this->index.getSize()); }
usint offset = 0;
sample_type sample(i, 0);
this->index.convertToBWTIndex(i);
while(true)
{
// Priority 1: Implicit samples.
if(this->index.hasImplicitSample(i))
{
sample.second = this->index.getImplicitSample(i) - offset;
break;
}
this->index.convertToSAIndex(i);
// Priority 2: Regular samples.
if(this->half_greedy && this->regular_samples->isSampled(i))
{
sample.second = this->regular_samples->getSampleAt(i) - offset;
break;
}
// Priority 3: Primary samples.
key_type j = this->hash(i);
if(this->samples[j].first == i)
{
sample.second = this->samples[j].second - offset;
break;
}
// Priority 4: Candidate samples.
if(this->use_candidates)
{
j = this->secondaryHash(i);
if(this->candidate_samples[j].first == i)
{
sample.second = this->candidate_samples[j].second - offset;
this->addSample(this->candidate_samples[j]);
break;
}
}
i = this->index.psi(i); offset++;
}
if(offset > 0) { this->trySample(sample, offset); }
// Update the average number of steps taken.
if(this->promote_probability > 0.0)
{
this->sum = this->sum + offset - this->window[this->pos];
this->window[this->pos] = offset; this->pos = (this->pos + 1) % this->window_size;
}
return (steps ? offset : sample.second);
}
usint*
AdaptiveSamples::locate(pair_type range, bool steps)
{
if(length(range) == 0) { return 0; }
usint* data = new usint[length(range)];
for(usint i = 0, j = range.first; j <= range.second; i++, j++)
{
data[i] = this->locate(j, steps);
}
return data;
}
void
AdaptiveSamples::display(pair_type range, uchar* data)
{
if(!(this->supportsDisplay()) || isEmpty(range) || range.second >= this->index.getSize() || data == 0) { return; }
pair_type res = this->regular_samples->inverseSA(range.first);
usint i = res.first, pos = res.second;
for(; i < range.first; i++)
{
pos = this->index.psi(pos);
this->index.convertToSAIndex(pos);
}
for(; i <= range.second; i++)
{
data[i - range.first] = this->index.getCharacter(pos);
pos = this->index.psi(pos);
this->index.convertToSAIndex(pos);
}
}
//--------------------------------------------------------------------------
void
AdaptiveSamples::addSample(sample_type sample)
{
key_type i = this->hash(sample.first);
if(this->samples[i].first >= this->index.getSize()) { this->items++; }
else if(this->use_candidates) { this->addCandidate(this->samples[i]); }
this->samples[i] = sample;
}
void
AdaptiveSamples::addCandidate(sample_type sample)
{
key_type i = this->secondaryHash(sample.first);
if(this->candidate_samples[i].first >= this->index.getSize()) { this->c_items++; }
this->candidate_samples[i] = sample;
}
void
AdaptiveSamples::trySample(sample_type sample, usint offset)
{
if(this->promote_probability > 0.0)
{
double temp = (this->promote_probability * this->window_size * offset) / this->sum;
if(rand() / (RAND_MAX + 1.0) >= temp) { return; }
}
if(this->use_candidates) { this->addCandidate(sample); }
else { this->addSample(sample); }
}
AdaptiveSamples::key_type
AdaptiveSamples::hash(key_type key)
{
key = ~key + (key << 15);
key = key ^ (key >> 12);
key = key + (key << 2);
key = key ^ (key >> 4);
key = key * 2057;
key = key ^ (key >> 16);
return key % this->size;
}
AdaptiveSamples::key_type
AdaptiveSamples::secondaryHash(key_type key)
{
key = (key + 0x7ed55d16) + (key << 12);
key = (key ^ 0xc761c23c) ^ (key >> 19);
key = (key + 0x165667b1) + (key << 5);
key = (key + 0xd3a2646c) ^ (key << 9);
key = (key + 0xfd7046c5) + (key << 3);
key = (key ^ 0xb55a4f09) ^ (key >> 16);
return key % this->size;
}
//--------------------------------------------------------------------------
} // namespace CSA