-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicSuffixArray.cpp
457 lines (340 loc) · 13.1 KB
/
DynamicSuffixArray.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* ADD MIT license
*/
#include "DynamicSuffixArray.h"
#define FROM_LEAF(i) ((char) ((i) - C_DIM))
#define TO_LEAF(c) (C_DIM + (size_t) (c))
#define DUMP(x) std::cout << x << std::endl
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
namespace dynsa {
DynamicSuffixArray::DynamicSuffixArray(float* factors) {
this->operation = none;
this->L = new DynRankS();
this->initialize(factors);
this->sample = new DSASampling(this, SAMPLE);
}
DynamicSuffixArray::~DynamicSuffixArray() {
delete this->sample;
delete this->L;
}
void DynamicSuffixArray::initialize(float* factors) {
this->L->initEmptyDynRankS(factors);
//Initialize the partial sums tree
for(int i = 0; i < C_SIZE; i++) {
this->C[i] = 0;
}
}
void DynamicSuffixArray::insert(uchar c, size_t position) {
if(operation == inserting || operation == deleting || operation == replacing) {
if(position <= previous_position) {
previous_position++;
}
if(position <= first_modification_position) {
first_modification_position++;
}
}
this->L->insert(c, position);
//Update the numbers in the tree, updating our partial sums that way
size_t i = TO_LEAF(c);
while(i > 1) {
this->C[i]++;
i = this->getParent(i);
}
this->C[i]++;
}
void DynamicSuffixArray::del(size_t position) {
//Pop the symbol
uchar c = this->L->deleteSymbol(position);
//Update the partial sums
size_t i = TO_LEAF(c);
while(i > 1) {
this->C[i]--;
i = this->getParent(i);
}
this->C[i]--;
if(operation == inserting || operation == deleting || operation == replacing) {
if(previous_position >= position) {
previous_position--;
}
if(first_modification_position >= position) {
first_modification_position--;
}
}
}
void DynamicSuffixArray::moveRow(size_t i, size_t j) {
uchar c = this->getBWTAt(i);
del(i);
insert(c, j);
}
void DynamicSuffixArray::insertToText(uchar c, size_t position) {
ustring a = new uchar[2];
a[0] = c;
a[1] = 0;
this -> insertFactor(a, position, 1);
}
void DynamicSuffixArray::insertFactor(ustring s, size_t position, size_t length) {
position = MIN(position, this->size());
//Step Ib modifies T^[position]
//a) Find the position via sampling
size_t pos_in_bwt = this->getISA(position);
size_t rank_of_deleted = 0;
//b) Perform the replacement, deleting the old char if there is one
if(! this->isEmpty()) {
old_sym = this->getBWTAt(pos_in_bwt);
rank_of_deleted = rank(old_sym, pos_in_bwt);
this->L->deleteSymbol(pos_in_bwt);
}
//The last character of the string
uchar c = s[length - 1];
this->insert(c, pos_in_bwt); //Insert the replacement symbol
first_modification_position = pos_in_bwt; //Update our first modification position in this run
insertion_point = pos_in_bwt;
this->new_sym = c;
this->operation = inserting;
previous_position = this->countSymbolsSmallerThan(old_sym) + rank_of_deleted;
if(old_sym > new_sym) {
previous_position--;
}
size_t old_insertion_point = insertion_point;
//Step IIa inserts a bunch of rows
insertion_point = this->countSymbolsSmallerThan(c) + this->rank(c, insertion_point);
for(size_t j = length - 1; j > 0; j--) {
this->insert(s[j -1], insertion_point);
modifications_remaining = k;
new_sym = s[j - 1];
sample->insertBWT(position, insertion_point);
old_insertion_point = insertion_point;
insertion_point = this->countSymbolsSmallerThan(s[j - 1]) + this->rank(s[j - 1], insertion_point);
if(first_modification_position < old_insertion_point && s[j - 1] == old_sym) {
insertion_point++;
}
}
new_sym = old_sym;
L->insert(old_sym, insertion_point);
modifications_remaining = 0;
if(insertion_point <= previous_position) {
previous_position++;
}
if(insertion_point <= first_modification_position) {
first_modification_position++;
}
//Update our sampler. I honestly still have no idea what the sampler does
sample->insertBWT(position, insertion_point);
//Finally, step IIb, REORDER. We give it
reorder();
//Perform the final update of our sampler
sample->insertBWT(position);
}
void DynamicSuffixArray::deleteAt(size_t position, size_t length) {
if(length <= 0) {
return; //Nothing to do here
}
size_t end_position = position + length - 1; //The end of the deleted block
size_t rank_of_deleted;
uchar last_symbol;
//Make sure we do not delete the '\0' or anything outside of the string
if(end_position > this->size()) {
length = this->size() - position + 1;
}
//Sample the ISA for the last character in the substring
first_modification_position = this->getISA(length + position);
old_sym = this->getBWTAt(first_modification_position);
//Notify the object that we are deleting (for various off-by-one error resolving)
operation = deleting;
insertion_point = this->countSymbolsSmallerThan(old_sym) + rank(old_sym, first_modification_position);
//Actually delete the substring
for(size_t i = length + position - 1; i >= position; i--) {
last_symbol = this->getBWTAt(insertion_point);
rank_of_deleted = this->rank(last_symbol, insertion_point);
this->del(insertion_point);
this->sample->deleteBWT(i, insertion_point);
if(i == position) {
break;
}
insertion_point = this->countSymbolsSmallerThan(last_symbol) + rank_of_deleted;
}
previous_position = this->countSymbolsSmallerThan(last_symbol) + rank_of_deleted;
insertion_point = first_modification_position;
this->del(insertion_point);
this->new_sym = last_symbol;
insert(last_symbol, insertion_point);
reorder();
//Perform a final sampler update
this->sample->deleteBWT(position);
}
void DynamicSuffixArray::replace(ustring s, size_t position, size_t length) {
this -> deleteAt(position, length);
this -> insertFactor(s, position, length);
}
void DynamicSuffixArray::setText(ustring s, size_t size) {
this->insert(s[size - 1], 1);
sample->insertBWT(1, 1);
sample->insertBWT(1);
this->insertFactor(s, 1, size - 1);
}
ustring DynamicSuffixArray::getBWT() {
//One extra for the '$'
ustring bwt = new uchar[this->size() + 1];
uchar c; //Temporary storage for a character
size_t i = 0; //The current index in text
this->L->iterateReset(); //Reset the iterator
while(true) {
//Check for $ as a special character
c = this->L->iterateGetSymbol();
bwt[i++] = c == '\0' ? '$' : c;
//Break if done
if(! this->L->iterateNext()) {
break;
}
}
bwt[this->size()] = '\0'; //Delimit the string
return bwt;
}
uchar DynamicSuffixArray::getBWTAt(size_t i) {
return (*L)[i];
}
ustring DynamicSuffixArray::getText() {
size_t N = this->size();
ustring text = new uchar[N];
for(size_t i = N - 1, j = 1; i > 0; i--) {
text[i - 1] = this->getBWTAt(j);
if(operation == inserting && j == insertion_point) {
text[i - 1] = old_sym;
}
j = this->LF(j);
}
//Set the EOS
text[N - 1] = '\0';
return text;
}
size_t DynamicSuffixArray::rank(uchar c, size_t i) {
size_t r = this->L->rank(c, i);
if(operation == deleting && first_modification_position < i && old_sym == c) {
r--;
}
return r;
}
size_t DynamicSuffixArray::size() {
return this->L->getSize();
}
bool DynamicSuffixArray::isEmpty() {
return this->size() == 0;
}
/*
* Private methods! Possibly also dragons.
*/
size_t DynamicSuffixArray::countSymbolsSmallerThan(uchar c) {
//Start from the character's leaf in the tree
size_t i = TO_LEAF(c);
size_t cnt = 0;
//As long as there can be left subtrees (smaller counts in them), go up
while(i > 1) {
size_t parent = getParent(i);
//If this is the right subtree, everything in the left
//sibling is smaller!
if(this->isRightSubtree(i)) {
cnt += this->C[getLeftSubtree(parent)];
}
i = parent;
}
/**
* We have not yet deleted the symbol from the tree, so we must move any
* characters that would appear afterwards in F backwards one spot.
*/
return (operation == deleting && c > old_sym) ? cnt - 1 : cnt;
}
size_t DynamicSuffixArray::getSA(size_t i) {
return sample->getSA(i);
}
size_t DynamicSuffixArray::getISA(size_t i) {
if(this->isEmpty()) {
return 1;
}
return sample->getISA(i);
}
size_t DynamicSuffixArray::LF(size_t i) {
if(operation != none && i == insertion_point) {
return previous_position;
}
uchar c = this->getBWTAt(i);
size_t smaller = this->countSymbolsSmallerThan(c);
size_t r = this->rank(c, i);
if(operation != none) {
if(operation == reordering || (operation == inserting && modifications_remaining == 0)) {
uchar c2 = this->getBWTAt(insertion_point);
size_t tempLF = this->countSymbolsSmallerThan(c2) + this->rank(c2, insertion_point);
size_t result = smaller + r;
if(result <= tempLF && result >= previous_position) {
result++;
} else if(result >= tempLF && result <= previous_position) {
result--;
}
return result;
} else if(operation == inserting) {
uchar other = this->getBWTAt(insertion_point);
if(other == old_sym && i > first_modification_position) {
r++;
}
if(c > new_sym) {
smaller--;
}
if(other == c && i > insertion_point) {
r--;
}
}
}
return smaller + r;
}
size_t DynamicSuffixArray::FL(size_t i) {
uchar c;
size_t smaller, node = 1; //Start at root
//Descend down the tree to locate the character
//Correct the cumulative sum while descending right
while(node < C_DIM) {
smaller = C[getLeftSubtree(node)];
if(smaller >= i) {
node = getLeftSubtree(node);
} else {
node = getRightSubtree(node);
i -= smaller;
}
}
c = FROM_LEAF(node); //Get char to which the leaf node belongs
return this->L->select(c, i);
}
bool DynamicSuffixArray::isRightSubtree(size_t i) {
return i % 2 == 1;
}
bool DynamicSuffixArray::isLeftSubtree(size_t i) {
return i % 2 == 0;
}
size_t DynamicSuffixArray::getLeftSubtree(size_t i) {
return 2 * i;
}
size_t DynamicSuffixArray::getRightSubtree(size_t i) {
return 2 * i + 1;
}
size_t DynamicSuffixArray::getParent(size_t i) {
return floor(i / 2);
}
void DynamicSuffixArray::reorder() {
operation = reordering;
uchar L_store = new_sym;
size_t expected_position = this->countSymbolsSmallerThan(L_store) + this->rank(L_store, insertion_point);
size_t smaller = this->countSymbolsSmallerThan(L_store);
while(expected_position != previous_position) {
L_store = this->getBWTAt(previous_position);
smaller = this->countSymbolsSmallerThan(L_store);
size_t tmp_prev_pos = rank(L_store, previous_position) + smaller;
this->del(previous_position);
this->insert(L_store, expected_position);
sample->moveBWT(previous_position, expected_position);
insertion_point = expected_position;
previous_position = tmp_prev_pos;
expected_position = smaller + rank(L_store, insertion_point);
}
operation = none;
insertion_point = expected_position;
}
}