-
Notifications
You must be signed in to change notification settings - Fork 1
/
rlcsa.cpp
1685 lines (1418 loc) · 49.9 KB
/
rlcsa.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include "rlcsa.h"
#include "misc/utils.h"
#include "bits/vectors.h"
#ifdef MULTITHREAD_SUPPORT
#include <omp.h>
#endif
namespace CSA
{
RLCSA::RLCSA(const std::string& base_name, bool print) :
ok(false),
alphabet(0),
sa_samples(0), support_locate(false), support_display(false),
end_points(0)
{
for(usint c = 0; c < CHARS; c++) { this->array[c] = 0; }
std::string array_name = base_name + ARRAY_EXTENSION;
std::ifstream array_file(array_name.c_str(), std::ios_base::binary);
if(!array_file)
{
std::cerr << "RLCSA: Error opening Psi array file!" << std::endl;
return;
}
usint distribution[CHARS];
array_file.read((char*)distribution, CHARS * sizeof(usint));
this->alphabet = new Alphabet(distribution); this->data_size = this->alphabet->getDataSize();
Parameters parameters;
parameters.read(base_name + PARAMETERS_EXTENSION);
for(usint c = 0; c < CHARS; c++)
{
if(this->alphabet->hasChar(c)) { this->array[c] = new PsiVector(array_file); }
}
this->end_points = new DeltaVector(array_file);
this->number_of_sequences = this->end_points->getNumberOfItems();
array_file.read((char*)&(this->sample_rate), sizeof(this->sample_rate));
array_file.close();
if(parameters.get(SUPPORT_LOCATE) || parameters.get(SUPPORT_DISPLAY))
{
std::string sa_sample_name = base_name + SA_SAMPLES_EXTENSION;
std::ifstream sa_sample_file(sa_sample_name.c_str(), std::ios_base::binary);
if(!sa_sample_file)
{
std::cerr << "RLCSA: Error opening suffix array sample file!" << std::endl;
return;
}
bool weighted = parameters.get(WEIGHTED_SAMPLES);
this->sa_samples = new SASamples(sa_sample_file, this->sample_rate, weighted);
sa_sample_file.close();
this->support_locate = this->sa_samples->supportsLocate();
this->support_display = this->sa_samples->supportsDisplay();
}
if(print) { parameters.print(); }
this->ok = true;
}
RLCSA::RLCSA(uchar* data, usint bytes, usint block_size, usint sa_sample_rate, usint threads, bool delete_data) :
ok(false),
alphabet(0),
sa_samples(0), support_locate(false), support_display(false),
sample_rate(sa_sample_rate), end_points(0)
{
for(usint c = 0; c < CHARS; c++) { this->array[c] = 0; }
if(!data || bytes == 0)
{
std::cerr << "RLCSA: No input data given!" << std::endl;
if(delete_data) { delete[] data; }
return;
}
if(block_size < 2 * sizeof(usint) || block_size % sizeof(usint) != 0)
{
std::cerr << "RLCSA: Block size must be a multiple of " << sizeof(usint) << " bytes!" << std::endl;
if(delete_data) { delete[] data; }
return;
}
this->buildRLCSA(data, 0, bytes, block_size, threads, 0, true, delete_data);
}
RLCSA::RLCSA(uchar* data, usint* ranks, usint bytes, usint block_size, usint sa_sample_rate, usint threads, bool delete_data) :
ok(false),
sa_samples(0), support_locate(false), support_display(false),
sample_rate(sa_sample_rate), end_points(0)
{
for(usint c = 0; c < CHARS; c++) { this->array[c] = 0; }
if(!data || !ranks || bytes == 0)
{
std::cerr << "RLCSA: No input data given!" << std::endl;
if(delete_data) { delete[] data; }
return;
}
if(block_size < 2 * sizeof(usint) || block_size % sizeof(usint) != 0)
{
std::cerr << "RLCSA: Block size must be a multiple of " << sizeof(usint) << " bytes!" << std::endl;
if(delete_data) { delete[] data; }
return;
}
this->buildRLCSA(data, ranks, bytes, block_size, threads, 0, true, delete_data);
}
RLCSA::RLCSA(uchar* data, usint bytes, usint block_size, usint sa_sample_rate, usint threads, Sampler* sampler, bool delete_data) :
ok(false),
alphabet(0),
sa_samples(0), support_locate(false), support_display(false),
sample_rate(sa_sample_rate),
end_points(0)
{
for(usint c = 0; c < CHARS; c++) { this->array[c] = 0; }
if(!data || bytes == 0)
{
std::cerr << "RLCSA: No input data given!" << std::endl;
if(delete_data) { delete[] data; }
return;
}
if(block_size < 2 * sizeof(usint) || block_size % sizeof(usint) != 0)
{
std::cerr << "RLCSA: Block size must be a multiple of " << sizeof(usint) << " bytes!" << std::endl;
if(delete_data) { delete[] data; }
return;
}
if(sampler != 0 && sampler->getStatus() != Sampler::SAMPLED)
{
std::cerr << "RLCSA: No samples given!" << std::endl;
if(delete_data) { delete[] data; }
return;
}
this->buildRLCSA(data, 0, bytes, block_size, threads, sampler, false, delete_data);
}
RLCSA::RLCSA(RLCSA& index, RLCSA& increment, usint* positions, usint block_size, usint threads) :
ok(false),
alphabet(0),
sa_samples(0), support_locate(false), support_display(false),
end_points(0)
{
for(usint c = 0; c < CHARS; c++) { this->array[c] = 0; }
if(!index.isOk() || !increment.isOk())
{
return; // Fail silently. Actual error has already been reported.
}
if(positions == 0)
{
std::cerr << "RLCSA: Positions for insertions not available!" << std::endl;
return;
}
if(index.sample_rate != increment.sample_rate)
{
std::cerr << "RLCSA: Cannot combine indexes with different sample rates!" << std::endl;
return;
}
index.strip();
increment.strip();
// Build character tables etc.
usint distribution[CHARS];
for(usint c = 0; c < CHARS; c++)
{
distribution[c] = index.alphabet->countOf(c) + increment.alphabet->countOf(c);
}
this->alphabet = new Alphabet(distribution); this->data_size = this->alphabet->getDataSize();
this->sample_rate = index.sample_rate;
this->number_of_sequences = index.number_of_sequences + increment.number_of_sequences;
// Merge end points, SA samples, and Psi.
usint psi_size = this->data_size + this->number_of_sequences;
bool should_be_ok = true;
#ifdef MULTITHREAD_SUPPORT
omp_set_num_threads(threads);
#endif
#pragma omp parallel for schedule(dynamic, 1)
for(int c = -2; c < (int)CHARS; c++)
{
if(c == -2) { this->mergeEndPoints(index, increment); }
else if(c == -1) { this->mergeSamples(index, increment, positions); }
else if(this->alphabet->hasChar(c) != 0)
{
this->array[c] = mergeVectors<PsiVector, PsiVector::Encoder, PsiVector::Iterator>(index.array[c], increment.array[c], positions, increment.data_size + increment.number_of_sequences, psi_size, block_size);
index.array[c] = 0;
increment.array[c] = 0;
if(this->array[c] == 0)
{
std::cerr << "RLCSA: Merge failed for vectors " << c << "!" << std::endl;
should_be_ok = false;
}
}
}
this->ok = should_be_ok;
}
RLCSA::~RLCSA()
{
for(usint c = 0; c < CHARS; c++) { delete this->array[c]; this->array[c] = 0; }
delete this->alphabet; this->alphabet = 0;
delete this->sa_samples; this->sa_samples = 0;
delete this->end_points; this->end_points = 0;
}
//--------------------------------------------------------------------------
void
RLCSA::writeTo(const std::string& base_name) const
{
std::string array_name = base_name + ARRAY_EXTENSION;
std::ofstream array_file(array_name.c_str(), std::ios_base::binary);
if(!array_file)
{
std::cerr << "RLCSA: Error creating Psi array file!" << std::endl;
return;
}
this->alphabet->writeTo(array_file);
for(usint c = 0; c < CHARS; c++)
{
if(this->array[c] != 0)
{
this->array[c]->writeTo(array_file);
}
}
this->end_points->writeTo(array_file);
array_file.write((char*)&(this->sample_rate), sizeof(this->sample_rate));
array_file.close();
if(this->sa_samples != 0)
{
std::string sa_sample_name = base_name + SA_SAMPLES_EXTENSION;
std::ofstream sa_sample_file(sa_sample_name.c_str(), std::ios_base::binary);
if(!sa_sample_file)
{
std::cerr << "RLCSA: Error creating suffix array sample file!" << std::endl;
return;
}
this->sa_samples->writeTo(sa_sample_file);
sa_sample_file.close();
}
Parameters parameters;
parameters.set(RLCSA_BLOCK_SIZE.first, this->getBlockSize() * sizeof(usint));
parameters.set(SAMPLE_RATE.first, this->sample_rate);
parameters.set(SUPPORT_LOCATE.first, this->support_locate);
parameters.set(SUPPORT_DISPLAY.first, this->support_display);
if(this->sa_samples != 0 && this->sa_samples->isWeighted())
{
parameters.set(WEIGHTED_SAMPLES.first, 1);
}
else { parameters.set(WEIGHTED_SAMPLES); }
parameters.write(base_name + PARAMETERS_EXTENSION);
}
//--------------------------------------------------------------------------
pair_type
RLCSA::count(const std::string& pattern) const
{
if(pattern.length() == 0) { return this->getSARange(); }
std::string::const_reverse_iterator iter = pattern.rbegin();
pair_type index_range = this->getCharRange((uchar)*iter);
if(isEmpty(index_range)) { return index_range; }
for(++iter; iter != pattern.rend(); ++iter)
{
index_range = this->LF(index_range, (uchar)*iter);
if(isEmpty(index_range)) { return EMPTY_PAIR; }
}
this->convertToSARange(index_range);
return index_range;
}
//--------------------------------------------------------------------------
void
RLCSA::reportPositions(uchar* data, usint length, usint* positions) const
{
if(data == 0 || length == 0 || positions == 0) { return; }
PsiVector::Iterator** iters = this->getIterators();
usint current = this->number_of_sequences - 1;
positions[length] = current; // "immediately after current"
for(sint i = (sint)(length - 1); i >= 0; i--)
{
usint c = (usint)data[i];
if(this->array[c] != 0)
{
current = this->LF(current, c, *(iters[c]));
}
else
{
if(c < this->alphabet->getFirstChar()) // No previous characters either.
{
current = this->number_of_sequences - 1;
}
else
{
current = this->alphabet->cumulative(c) - 1 + this->number_of_sequences;
}
}
positions[i] = current; // "immediately after current"
}
this->deleteIterators(iters);
}
//--------------------------------------------------------------------------
usint*
RLCSA::locate(pair_type range, bool direct, bool steps) const
{
if(!(this->support_locate) || isEmpty(range) || range.second >= this->data_size) { return 0; }
usint* data = new usint[length(range)];
if(direct) { this->directLocate(range, data, steps); }
else { this->locateUnsafe(range, data, steps); }
return data;
}
usint*
RLCSA::locate(pair_type range, usint* data, bool direct, bool steps) const
{
if(!(this->support_locate) || isEmpty(range) || range.second >= this->data_size || data == 0) { return 0; }
if(direct) { this->directLocate(range, data, steps); }
else { this->locateUnsafe(range, data, steps); }
return data;
}
usint
RLCSA::locate(usint index, bool steps) const
{
if(!(this->support_locate) || index >= this->data_size) { return (steps ? 0 : this->data_size); }
return this->directLocate(index + this->number_of_sequences, steps);
}
usint
RLCSA::inverseLocate(usint location) const
{
if(!(this->support_locate)) { return this->data_size; }
// TODO: Check for out-of-bounds locations somehow.
// Inverse-locate the given location in BWT space, and convert back to SA
// space before returning.
return this->directInverseLocate(location) - this->number_of_sequences;
}
void
RLCSA::directLocate(pair_type range, usint* data, bool steps) const
{
// range is in SA coordinates, so first we need to convert to BWT coordinates.
this->convertToBWTRange(range);
for(usint i = 0, j = range.first; j <= range.second; i++, j++)
{
data[i] = this->directLocate(j, steps);
}
}
usint
RLCSA::directLocate(usint index, bool steps) const
{
// Note that index is in BWT coordinates initially.
// This keeps track of how far along the sequence we had to go to find an SA
// sample.
usint offset = 0;
while(true)
{
// First try in BWT space, so we can account for the text end characters.
if(this->hasImplicitSample(index))
{
// If we took an implicit sample at this index in the BWT (due to us being
// in the range occupied by sequence start characters), we know where it
// falls in the original sequences: at the endpoint of the appropriate
// text.
if(steps) { return offset; }
else { return this->getImplicitSample(index) - offset; }
}
// Pop index into SA space, where the SA samples live
index -= this->number_of_sequences;
if(this->sa_samples->isSampled(index))
{
// If we took a real SA sample here, we know where it falls in the
// original sequences.
return (steps ? offset : this->sa_samples->getSampleAt(index) - offset);
}
// If we get here, we couldn't map this position. Proceed forwards (towards
// the end of the sequence), in hopes of hitting either a sample or the
// sequence end character. Note that psi maps from SA position to the *BWT*
// position of the subsequent character, popping index back into BWT space.
index = this->psi(index);
offset++;
}
}
usint
RLCSA::directInverseLocate(usint location) const
{
// Get the SA value and SA index (in that order) of the last SA sample
// before the given text location.
pair_type last_sample = this->sa_samples->inverseSA(location);
// TODO: catch the (size, size) sentinel.
while(last_sample.first < location) {
// We're not at the desired text location, so we must be before it.
// Advance the text location (SA value) by 1
last_sample.first += 1;
// Advance the SA index to that corresponding to the next character. Note
// that psi returns BWT coordinates, so we have to convert back to SA
// coordinates.
last_sample.second = (this->psi(last_sample.second) -
this->number_of_sequences);
}
if(last_sample.first != location) {
// We managed to start on the wrong side of what we're trying to locate;
// inverseSA lied to us.
throw "Somehow skipped desired inverse locate position";
}
// Return the answer (SA index) in BWT coordinates. It will probably be
// immediately converted back to SA coordinates, but it's worth it for
// consistency with the directLocate function, which takes in BWT coordinates.
return last_sample.second + this->number_of_sequences;
}
void
RLCSA::locateUnsafe(pair_type range, usint* data, bool steps) const
{
this->convertToBWTRange(range);
usint items = length(range);
usint* offsets = new usint[items];
bool* finished = new bool[items]; // FIXME This could be more space efficient...
PsiVector::Iterator** iters = this->getIterators();
for(usint i = 0, j = range.first; i < items; i++, j++)
{
data[i] = j;
offsets[i] = 0;
finished[i] = false;
}
bool found = false;
while(!found)
{
found = true;
pair_type run = EMPTY_PAIR;
for(usint i = 0; i < items; i++)
{
if(finished[i])
{
continue; // The run might continue after this.
}
else if(isEmpty(run))
{
run = pair_type(i, i);
}
else if(data[i] - data[run.first] == i - run.first)
{
run.second = i;
}
else
{
found &= this->processRun(run, data, offsets, finished, iters, steps);
run = pair_type(i, i);
}
}
if(!isEmpty(run)) { found &= this->processRun(run, data, offsets, finished, iters, steps); }
}
this->deleteIterators(iters);
delete[] offsets;
delete[] finished;
}
bool
RLCSA::processRun(pair_type run, usint* data, usint* offsets, bool* finished, PsiVector::Iterator** iters, bool steps) const
{
bool found = true;
usint run_start = 0, run_left = 0;
pair_type next_sample = pair_type(0, 0);
for(usint i = run.first; i <= run.second; i++)
{
if(finished[i])
{
if(run_left > 0) { run_left--; }
continue;
}
if(data[i] < this->number_of_sequences) // Implicit sample here.
{
DeltaVector::Iterator iter(*(this->end_points));
data[i] = (steps ? offsets[i] : iter.select(data[i]) + 1 - offsets[i]);
finished[i] = true;
if(run_left > 0) { run_left--; }
continue;
}
if(next_sample.first < data[i]) // Need another sample.
{
next_sample = this->sa_samples->getFirstSampleAfter(data[i] - this->number_of_sequences);
next_sample.first += this->number_of_sequences;
}
if(data[i] < next_sample.first) // No sample found for current position.
{
if(run_left > 0)
{
data[i] = data[run_start] + i - run_start;
run_left--;
}
else
{
pair_type value = this->psi(data[i] - this->number_of_sequences, run.second - i, iters);
data[i] = value.first;
run_left = value.second;
run_start = i;
}
offsets[i]++;
found = false;
}
else // Sampled position found.
{
data[i] = (steps ? offsets[i] : this->sa_samples->getSample(next_sample.second) - offsets[i]);
finished[i] = true;
if(run_left > 0) { run_left--; }
}
}
return found;
}
//--------------------------------------------------------------------------
uchar*
RLCSA::display(usint sequence, bool include_end_marker) const
{
if(!(this->support_display)) { return 0; }
pair_type seq_range = this->getSequenceRange(sequence);
if(isEmpty(seq_range)) { return 0; }
uchar* data = new uchar[length(seq_range) + include_end_marker];
this->displayUnsafe(seq_range, data);
if(include_end_marker) { data[length(seq_range)] = 0; }
return data;
}
uchar*
RLCSA::display(usint sequence, pair_type range) const
{
if(!(this->support_display) || isEmpty(range)) { return 0; }
pair_type seq_range = this->getSequenceRange(sequence);
if(isEmpty(seq_range)) { return 0; }
range.first += seq_range.first; range.second += seq_range.first;
if(range.second > seq_range.second) { return 0; }
uchar* data = new uchar[length(range)];
this->displayUnsafe(range, data);
return data;
}
uchar*
RLCSA::display(usint sequence, pair_type range, uchar* data) const
{
if(!(this->support_display) || isEmpty(range) || data == 0) { return 0; }
pair_type seq_range = this->getSequenceRange(sequence);
if(isEmpty(seq_range)) { return 0; }
range.first += seq_range.first; range.second += seq_range.first;
if(range.second > seq_range.second) { return 0; }
this->displayUnsafe(range, data);
return data;
}
uchar*
RLCSA::display(usint position, usint len, usint context, usint& result_length) const
{
if(!(this->support_display)) { return 0; }
pair_type range = this->getSequenceRangeForPosition(position);
if(isEmpty(range)) { return 0; }
range.first = position - std::min(context, position - range.first);
range.second = std::min(range.second, position + len + context - 1);
result_length = length(range);
if(isEmpty(range)) { return 0; }
uchar* data = new uchar[length(range)];
this->displayUnsafe(range, data);
return data;
}
usint
RLCSA::displayPrefix(usint sequence, usint len, uchar* data) const
{
if(!(this->support_display) || len == 0 || data == 0) { return 0; }
pair_type seq_range = this->getSequenceRange(sequence);
if(isEmpty(seq_range)) { return 0; }
pair_type range(seq_range.first, std::min(seq_range.second, seq_range.first + len - 1));
this->displayUnsafe(range, data);
return length(range);
}
usint
RLCSA::displayFromPosition(usint index, usint max_len, uchar* data) const
{
if(max_len == 0 || data == 0 || index >= this->data_size) { return 0; }
for(usint i = 0; i < max_len; i++)
{
data[i] = this->getCharacter(index);
index = this->psiUnsafe(index, data[i]);
if(index < this->number_of_sequences) { return i + 1; }
index -= this->number_of_sequences;
}
return max_len;
}
void
RLCSA::displayUnsafe(pair_type range, uchar* data, bool get_ranks, usint* ranks) const
{
pair_type res = this->sa_samples->inverseSA(range.first);
usint i = res.first, pos = res.second;
if(length(range) >= 1024)
{
PsiVector::Iterator** iters = this->getIterators();
for(; i < range.first; i++)
{
pos = this->psi(pos, iters) - this->number_of_sequences;
}
for(; i <= range.second; i++)
{
usint c = this->getCharacter(pos);
data[i - range.first] = c;
if(get_ranks) { ranks[i - range.first] = pos + this->number_of_sequences; }
pos = this->psiUnsafe(pos, c, *(iters[c])) - this->number_of_sequences;
}
this->deleteIterators(iters);
}
else
{
for(; i < range.first; i++)
{
pos = this->psi(pos) - this->number_of_sequences;
}
for(; i <= range.second; i++)
{
usint c = this->getCharacter(pos);
data[i - range.first] = c;
if(get_ranks) { ranks[i - range.first] = pos + this->number_of_sequences; }
pos = this->psiUnsafe(pos, c) - this->number_of_sequences;
}
}
}
//--------------------------------------------------------------------------
pair_type
RLCSA::getSARange() const
{
return pair_type(0, this->data_size - 1);
}
pair_type
RLCSA::getBWTRange() const
{
return pair_type(this->number_of_sequences, this->number_of_sequences + this->data_size - 1);
}
pair_type
RLCSA::getCharRange(usint c) const
{
if(c >= CHARS) { return EMPTY_PAIR; }
pair_type index_range = this->alphabet->getRange(c);
this->convertToBWTRange(index_range);
return index_range;
}
void
RLCSA::convertToSARange(pair_type& bwt_range) const
{
bwt_range.first -= this->number_of_sequences;
bwt_range.second -= this->number_of_sequences;
}
void
RLCSA::convertToBWTRange(pair_type& sa_range) const
{
sa_range.first += this->number_of_sequences;
sa_range.second += this->number_of_sequences;
}
void
RLCSA::convertToSARange(std::vector<pair_type>& bwt_ranges) const
{
for(std::vector<pair_type>::iterator iter = bwt_ranges.begin(); iter != bwt_ranges.end(); ++iter)
{
this->convertToSARange(*iter);
}
}
pair_type
RLCSA::LF(pair_type range, usint c) const
{
if(c >= CHARS || this->array[c] == 0) { return EMPTY_PAIR; }
PsiVector::Iterator iter(*(this->array[c]));
usint start = this->alphabet->cumulative(c) + this->number_of_sequences - 1;
range.first = start + iter.rank(range.first, true);
range.second = start + iter.rank(range.second);
return range;
}
std::vector<usint>*
RLCSA::locateRange(pair_type range) const
{
std::vector<usint>* results = new std::vector<usint>;
if(!(this->support_locate)) { return results; }
this->locateRange(range, *results);
removeDuplicates(results, false);
return results;
}
void
RLCSA::locateRange(pair_type range, std::vector<usint>& vec) const
{
if(isEmpty(range) || range.second >= this->data_size) { return; }
usint* data = new usint[length(range)];
this->locateUnsafe(range, data, false);
for(usint i = 0; i < length(range); i++) { vec.push_back(data[i]); }
delete[] data;
}
std::vector<usint>*
RLCSA::locateRanges(std::vector<pair_type>& ranges) const
{
std::vector<usint>* results = new std::vector<usint>;
if(!(this->support_locate)) { return results; }
for(std::vector<pair_type>::iterator iter = ranges.begin(); iter != ranges.end(); ++iter)
{
this->locateRange(*iter, *results);
}
removeDuplicates(results, false);
return results;
}
//--------------------------------------------------------------------------
pair_type
RLCSA::getSequenceRange(usint number) const
{
if(number >= this->number_of_sequences) { return EMPTY_PAIR; }
pair_type result;
DeltaVector::Iterator iter(*(this->end_points));
if(number == 0)
{
result.first = 0;
result.second = iter.select(number);
}
else
{
result.first = nextMultipleOf(this->sample_rate, iter.select(number - 1));
result.second = iter.selectNext();
}
return result;
}
usint
RLCSA::getSequenceForPosition(usint value) const
{
if(value == 0) { return 0; }
DeltaVector::Iterator iter(*(this->end_points));
return iter.rank(value - 1);
}
pair_type
RLCSA::getSequenceRangeForPosition(usint value) const
{
return this->getSequenceRange(this->getSequenceForPosition(value));
}
usint*
RLCSA::getSequenceForPosition(usint* values, usint len) const
{
if(values == 0) { return 0; }
DeltaVector::Iterator iter(*(this->end_points));
for(usint i = 0; i < len; i++)
{
if(values[i] > 0) { values[i] = iter.rank(values[i] - 1); }
}
return values;
}
pair_type
RLCSA::getRelativePosition(usint value) const
{
// Get an iterator so we can use the vector of sequence endpoints.
DeltaVector::Iterator iter(*(this->end_points));
// Start out saying we're in text 0 at index whatever our index in the whole
// string of texts is.
pair_type result(0, value);
// Adjust the text number to whatever text hadn't yet ended at the position
// before the one we're interested in.
if(value > 0) { result.first = iter.rank(value - 1); }
if(result.first > 0)
{
// If we're not still in the 0th text, re-base our index in the text to
// count from the first multiple of the SA sample rate after the end of the
// text before this text (since we have declared text coordinates start on
// SA sample multiples).
result.second -= nextMultipleOf(this->sample_rate, iter.select(result.first - 1));
}
// Return the fixed-up relative position.
return result;
}
usint
RLCSA::getAbsolutePosition(pair_type position) const
{
// Get an iterator so we can use the vector of sequence endpoints.
DeltaVector::Iterator iter(*(this->end_points));
// Where is this position as an absolute position? Start off at the beginning.
usint value = 0;
if(position.first > 0) {
// Only the 0th text starts at 0. Find the absolute endpoint of the previous
// text, and then start at the next multiple of the sample rate after that.
// That is where this text is going to start.
value = nextMultipleOf(this->sample_rate, iter.select(position.first - 1));
}
// Advance the start of the text by the index into the text.
value += position.second;
// Return the resulting absolute position.
return value;
}
//--------------------------------------------------------------------------
uchar*
RLCSA::readBWT() const
{
return this->readBWT(pair_type(0, this->data_size + this->number_of_sequences - 1));
}
uchar*
RLCSA::readBWT(pair_type range) const
{
if(isEmpty(range) || range.second >= this->data_size + this->number_of_sequences) { return 0; }
usint n = length(range);
uchar* bwt = new uchar[n];
memset(bwt, 0, n);
for(usint c = 0; c < CHARS; c++)
{
if(this->array[c] != 0)
{
PsiVector::Iterator iter(*(this->array[c]));
usint pos = iter.valueAfter(range.first).first;
while(pos <= range.second)
{
bwt[pos - range.first] = c;
pos = iter.selectNext();
}
}
}
return bwt;
}
usint
RLCSA::countRuns() const
{
usint runs = 0;
for(usint c = 0; c < CHARS; c++)
{
if(this->array[c] != 0)
{
PsiVector::Iterator iter(*(this->array[c]));
runs += iter.countRuns();
}
}
return runs;
}
//--------------------------------------------------------------------------
SuffixArray*
RLCSA::getSuffixArrayForSequence(usint number) const
{
if(!this->supportsDisplay()) { return 0; }
pair_type seq_range = this->getSequenceRange(number);
if(isEmpty(seq_range)) { return 0; }
uchar* data = new uchar[length(seq_range) + 1];
usint* ranks = new usint[length(seq_range) + 1];
this->displayUnsafe(seq_range, data, true, ranks);
data[length(seq_range)] = 0; ranks[length(seq_range)] = number;
return new SuffixArray(data, ranks, length(seq_range) + 1, 1);
}
//--------------------------------------------------------------------------
PsiVector::Iterator**
RLCSA::getIterators() const
{
PsiVector::Iterator** iters = new PsiVector::Iterator*[CHARS];
for(usint i = 0; i < CHARS; i++)
{
if(this->array[i] == 0) { iters[i] = 0; }
else { iters[i] = new PsiVector::Iterator(*(this->array[i])); }
}
return iters;
}
void
RLCSA::deleteIterators(PsiVector::Iterator** iters) const
{
if(iters == 0) { return; }
for(usint i = 0; i < CHARS; i++) { delete iters[i]; }
delete[] iters;
}
//--------------------------------------------------------------------------
usint
RLCSA::reportSize(bool print) const