-
Notifications
You must be signed in to change notification settings - Fork 0
/
lz_rlbwt.h
1302 lines (866 loc) · 34.2 KB
/
lz_rlbwt.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
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
/*
* This file is part of lz-rlbwt.
* Copyright (c) by
* Nicola Prezza <[email protected]>,
* Djamal Belazzougui, Fabio Cunial, Travis Gagie, and Mathieu Raffinot
*
* lz-rlbwt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* lz-rlbwt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details (<http://www.gnu.org/licenses/>).
*/
/*
* lz_rlbwt.h
*
* Created on: Mar 18, 2015
* Author: nicola
*
* The lz-rlbwt data strucures combines the rlbwt and components from a LZ77 index. Space is
* O(r+z) words, r being the number of runs in the BWT of the text and z being the number of phrases
* in the LZ77 parse of the text.
*
* This class permits to answer count and locate queries. Letting n be the text length,
* m the pattern length, and occ the number of pattern occurrences, count takes O(m log log n) time,
* and locate takes (depending on the version - see constructor):
*
* -full: O( (m+occ) * log n) time
* -bidirectional: O( (m^2+occ) * log n ) time
* -light: O( m * log n * (occ+1) ) time
*
* bidirectional and light indexes have several optimizations implemented, so the worst cases
* rarely should occur.
*
* For more info, read the paper: "Djamal Belazzougui, Fabio Cunial, Travis Gagie, Nicola Prezza, and Mathieu
* Raffinot. Composite repetition-aware data structures"
*
*/
#ifndef LZrlbwt_H_
#define LZrlbwt_H_
#include <cstdlib>
#include <iostream>
#include <vector>
#include <sdsl/wavelet_trees.hpp>
#include <rlbwt.h>
#include <data_structures/lz77_parser.h>
#include <sparse_sd_vector.h>
#include <range_search_2D.h>
#include <st_subset.h>
#include <packed_view.h>
#include "dynamic/dynamic.hpp"
using namespace std;
using namespace sdsl;
using namespace bwtil;
using namespace dyn;
namespace lzrlbwt{
typedef lz77_parser<gap_bv,packed_spsi> lz77_parser_t;
//typedef lz77_parser<> lz77_parser_t;
//type of the run-length BWT: Elias-Fano RLBWT / Huffman RLBWT
enum bwt_type {ef_rlbwt,h_rlbwt};
/*
* Components stored in the index:
*
* - bidirectional: fwd BWT, 4-sided range, 2-sided range, st_subset, bitvector last, SA samples
* - light: rev BWT, 2-sided range, bitvector last, SA samples
* - full: fwd BWT, rev BWT, 4-sided range, 2-sided range, st_subset, bitvector last, SA samples
*
*/
enum lz_rlbwt_type {bidirectional,light,full};
enum lzrlbwt_options{
verbose_out, //print verbose ouput
bidirectional_bwt, //if true, reverse index is not created / saved / loaded
light_index, //if true, both fwd index and 4-sided structure are not
//created/saved/loaded
};
template<
class fm_index_t = rlbwt_sd, // RLBWT using Elias-Fano compression on the gap lengths.
class sparse_bitvector_t = sparse_sd_vector<>, // elias-fano sparse bitvector
class range_search_t = range_search_2D<> // class implementing 2-sided and 4-sided range search.
>
class lz_rlbwt{
public:
~lz_rlbwt(){
if(fm_index_fwd != NULL)
delete fm_index_fwd;
if(fm_index_rev != NULL)
delete fm_index_rev;
if(lz_4_sided_range_search != NULL)
delete lz_4_sided_range_search;
if(lz_2_sided_range_search != NULL)
delete lz_2_sided_range_search;
if(st_sub != NULL)
delete st_sub;
if(last != NULL)
delete last;
}
//default empty constructor
lz_rlbwt(){};
// build the lz_rlbwt of the input string
/* \param input string to be indexed
* \param options list of options: by default, empty (no verbose and no bidirectional bwt.)
* NOTE: we assume that all letters in the input text are right-maximal.
*/
lz_rlbwt(string &input, vector<lzrlbwt_options> opt = {}){
bool verbose=false;
type = full;
for(auto o:opt){
if(o==verbose_out)
verbose=true;
if(o==bidirectional_bwt)
type = bidirectional;
if(o==light_index)
type = light;
}
build(input, verbose);
}
// return number of occurrences of input pattern
/* \param pattern string to be searched
* \return number of occurrences of the input pattern
*/
ulint count(string &pattern){
ulint occ = 0;
range_t range;
//if type == light, we have only reverse index
if(type==light){
range = fm_index_rev->count(pattern,true);
}else{
range = fm_index_fwd->count(pattern);
}
if(range.second >= range.first)
occ = (range.second - range.first) + 1;
return occ;
}
// locate all occurrences of a pattern in the indexed text
/* \param pattern string to be searched
* \return all occurrences (i.e. text positions) of the input pattern
*/
vector<ulint> locate(string &pattern, bool optimize = true){
//if index is light, then locate is particular
if(type == light){
return locate_light(pattern, optimize);
}
//else: locate with full or bidirectional index
return locate_bidir_full(pattern, optimize);
}
// save the index to file
/* \param basename basename of the output index files. Extensions will be automatically
* added to the input path
* \param verbose verbose output?
*/
void save_to_file(string base_name, bool verbose = false){
string type_basename = string(base_name).append(".lzrlbwt.index_type");
string fm_index_fwd_basename = string(base_name).append(".lzrlbwt.fmi_fwd");
string fm_index_rev_basename = string(base_name).append(".lzrlbwt.fmi_rev");
string two_sided_filename = string(base_name).append(".lzrlbwt.2_sided_range");
string four_sided_filename = string(base_name).append(".lzrlbwt.4_sided_range");
string st_subset_filename = string(base_name).append(".lzrlbwt.st_subset");
string last_filename = string(base_name).append(".lzrlbwt.end_phrases");
string start_filename = string(base_name).append(".lzrlbwt.beg_phrases");
string sa_samples_filename = string(base_name).append(".lzrlbwt.sa_samples");
//write lz-rlbwt type
std::ofstream type_ofs (type_basename,std::ofstream::binary);
type_ofs.write((char*)&type,sizeof(type));
type_ofs.close();
{
if(verbose)
cout << "Saving start of phrases (text positions) ... " << flush;
std::ofstream out (start_filename,std::ofstream::binary);
begin_of_phrase.serialize(out);
if(verbose)
cout << "done!" << endl;
}
if(type != light){
if(verbose)
cout << "Saving forward RLBWT ... " << flush;
assert(fm_index_fwd!=0);
fm_index_fwd->save_to_disk(fm_index_fwd_basename);
if(verbose)
cout << "done!" << endl;
}
if(type!=bidirectional){
if(verbose)
cout << "Saving reverse RLBWT ... " << flush;
assert(fm_index_rev!=0);
fm_index_rev->save_to_disk(fm_index_rev_basename);
if(verbose)
cout << "done!" << endl;
}
if(verbose)
cout << "Saving two-sided range data structure ... " << flush;
lz_2_sided_range_search->save_to_file(two_sided_filename);
if(verbose)
cout << "done!" << endl;
if(type != light){
if(verbose)
cout << "Saving four-sided range data structure ... " << flush;
lz_4_sided_range_search->save_to_file(four_sided_filename);
if(verbose)
cout << "done!" << endl;
if(verbose)
cout << "Saving interval data structure on the subset of ST nodes... " << flush;
st_sub->save_to_file(st_subset_filename);
if(verbose)
cout << "done!" << endl;
}
if(verbose)
cout << "Saving end of phrases (BWT positions) ... " << flush;
std::ofstream out (last_filename,std::ofstream::binary);
last->serialize(out);
//save also global variables in this file
out.write((char*)&terminator_pos,sizeof(ulint));
out.write((char*)&bwt_length,sizeof(ulint));
uint16_t a_size = alphabet.size();
out.write((char*)&a_size,sizeof(uint16_t));
out.write((char*)alphabet.data(),a_size*sizeof(uchar));
out.close();
if(verbose)
cout << "done!" << endl;
if(type==light){
if(verbose)
cout << "Saving SA samples ... " << flush;
std::ofstream out (sa_samples_filename,std::ofstream::binary);
ulint sa_samples_container_size = SA_samples.container().size();
ulint bitlength = SA_samples.width();
out.write((char*)&sa_samples_container_size,sizeof(sa_samples_container_size));
out.write((char*)&bitlength,sizeof(bitlength));
out.write((char*)SA_samples.container().data(),sa_samples_container_size*sizeof(ulint));
out.close();
if(verbose)
cout << "done!" << endl;
}
}
// index from file
/* \param basename_path basename of the index files
* \param opt options: verbose output / load bidirectional index (i.e. do not load reverse bwt)
*/
void load_from_file(string base_name, vector<lzrlbwt_options> opt = {}){
bool verbose=false;
for(auto o:opt){
if(o==verbose_out)
verbose=true;
}
string type_basename = string(base_name).append(".lzrlbwt.index_type");
string fm_index_fwd_basename = string(base_name).append(".lzrlbwt.fmi_fwd");
string fm_index_rev_basename = string(base_name).append(".lzrlbwt.fmi_rev");
string two_sided_filename = string(base_name).append(".lzrlbwt.2_sided_range");
string four_sided_filename = string(base_name).append(".lzrlbwt.4_sided_range");
string st_subset_filename = string(base_name).append(".lzrlbwt.st_subset");
string last_filename = string(base_name).append(".lzrlbwt.end_phrases");
string start_filename = string(base_name).append(".lzrlbwt.beg_phrases");
string sa_samples_filename = string(base_name).append(".lzrlbwt.sa_samples");
//read lz-rlbwt type
std::ifstream type_ofs (type_basename,std::ifstream::binary);
type_ofs.read((char*)&type,sizeof(type));
type_ofs.close();
{
if(verbose)
cout << "Loading sparse bitvector data structure (begin of phrases on text) ... " << flush;
std::ifstream in (start_filename,std::ifstream::binary);
begin_of_phrase.load(in);
in.close();
if(verbose)
cout << "done!" << endl;
}
if(type != light){
if(verbose)
cout << "Loading forward FM index ... " << flush;
fm_index_fwd = new fm_index_t();
fm_index_fwd->load_from_disk(fm_index_fwd_basename);
if(verbose)
cout << "done!" << endl;
}
if(type != bidirectional){
if(verbose)
cout << "Loading reverse FM index ... " << flush;
fm_index_rev = new fm_index_t();
fm_index_rev->load_from_disk(fm_index_rev_basename);
if(verbose)
cout << "done!" << endl;
}
if(verbose)
cout << "Loading two-sided range data structure ... " << flush;
lz_2_sided_range_search = new range_search_t();
lz_2_sided_range_search->load_from_file(two_sided_filename);
if(verbose)
cout << "done! number of LZ77 phrases: " << lz_2_sided_range_search->number_of_phrases() << endl;
if(type != light){
if(verbose)
cout << "Loading four-sided range data structure ... " << flush;
lz_4_sided_range_search = new range_search_t();
lz_4_sided_range_search->load_from_file(four_sided_filename);
if(verbose)
cout << "done!" << endl;
if(verbose)
cout << "Loading interval data structure on the subset of ST nodes... " << flush;
st_sub = new st_subset();
st_sub->load_from_file(st_subset_filename);
if(verbose)
cout << "done!" << endl;
}
if(verbose)
cout << "Loading sparse bitvector data structure (end of phrases on BWT) ... " << flush;
std::ifstream in (last_filename,std::ifstream::binary);
last = new sparse_bitvector_t();
last->load(in);
//load also global variables from this file
in.read((char*)&terminator_pos,sizeof(ulint));
in.read((char*)&bwt_length,sizeof(ulint));
uint16_t a_size;
in.read((char*)&a_size,sizeof(uint16_t));
alphabet = vector<uchar>(a_size);
in.read((char*)alphabet.data(),a_size*sizeof(uchar));
in.close();
if(verbose)
cout << "done!" << endl;
if(type==light){
if(verbose)
cout << "Loading SA samples ... " << flush;
std::ifstream in (sa_samples_filename,std::ifstream::binary);
ulint sa_samples_container_size;
ulint bitlength;
in.read((char*)&sa_samples_container_size,sizeof(sa_samples_container_size));
in.read((char*)&bitlength,sizeof(bitlength));
SA_samples = packed_view<vector>(bitlength,last->rank(last->size()));
in.read((char*)SA_samples.container().data(),sa_samples_container_size*sizeof(ulint));
in.close();
if(verbose)
cout << "done!" << endl;
}
}
private:
vector<ulint> locate_bidir_full(string &pattern, bool optimize = true){
//vector containing all occurrences of the pattern
vector<ulint> occ;
ulint n_occ = count(pattern);
if(n_occ==0)
return occ;
ulint m = pattern.size();
//interval of all proper reverse pattern suffixes
//for convenience, we treat occurrences that start at the
//beginning/end of a phrase as internal occurrences.
//length of this vector is m-1
vector<pair<ulint,ulint> > rev_ranges;
//range in reverse bwt
pair<ulint, ulint> rev_range = fm_index_fwd->get_char_range(pattern[0]);
//if we have the reverse BWT, then we compute now the reverse ranges.
if(type != bidirectional){
for(ulint i=1;i<m;++i){
//first_one = rank of the first one in this range, if any
assert(rev_range.first<=last->size());
ulint first_one = last->rank(rev_range.first);
//ones = number of ones in last bitvector in this interval
assert(rev_range.second+1<=last->size());
ulint ones = last->rank(rev_range.second+1) - first_one;
//if there are phrase ends, then insert a proper range. Otherwise, insert an empty range
if(ones>0){
rev_ranges.push_back({first_one,first_one+ones-1});
}else{
//insert an empty range
rev_ranges.push_back({1,0});
}
rev_range = fm_index_rev->LF(rev_range,pattern[i]);
}
}
//now, from end to begin of pattern, search fwd ranges in the st_sub structure
//start from last character
pair<ulint, ulint> fwd_range = fm_index_fwd->get_char_range(pattern[m-1]);
for(ulint i=1; i<m and occ.size() < n_occ; ++i){
pair<ulint, ulint> range_fwd = st_sub->interval(fwd_range);
//if ranges are non-empty
//if optimize = false, execute this in any case
if((not optimize) or (range_fwd.second>=range_fwd.first)){
if(type == bidirectional){
//compute rev_range using bidirectional BWT
rev_range = bidirectional_LF(pattern.substr(0,m-i));
//first_one = rank of the first one in this range, if any
assert(rev_range.first<=last->size());
ulint first_one = last->rank(rev_range.first);
//ones = number of ones in last bitvector in this interval
assert(rev_range.second+1<=last->size());
ulint ones = last->rank(rev_range.second+1) - first_one;
if(ones>0){
rev_range = {first_one,first_one+ones-1};
}else{
rev_range = {1,0};
}
}else{
//we already computed rev_range
rev_range = rev_ranges[m-i-1];
}
if( rev_range.second>=rev_range.first ){
//rev ranges start from 1. remap so that they start from 0
assert(rev_range.first>0);
assert(rev_range.second>0);
rev_range.first--;
rev_range.second--;
//query 4-sided range data structure
auto four_sided_result =
lz_4_sided_range_search->four_sided_range_search(rev_range,range_fwd);
//cout << "OCC : " << four_sided_result.size() << endl;
if(four_sided_result.size()>0){
//store here positions on text found with 4-sided range search
vector<ulint> occ_temp;
for(auto o : four_sided_result){
//o.second is the phrase number (of right factor)
//convert from phrase rank to text position with a select query
assert(o.second < begin_of_phrase.rank(begin_of_phrase.size()));
ulint start_position = begin_of_phrase.select(o.second);
//compute correct coordinate (left-shift)
assert(start_position >= m-i);
occ_temp.push_back( start_position - (m-i) );
}
for(auto o : occ_temp)
occ.push_back(o);
//call recursive 2-range search
for(auto o : occ_temp){
//perform 2-sided range search only if there still are occurrences to be found
//if optimize = false, do this in any case
if((not optimize) or occ.size() < n_occ){
//recursively compute occurrences of phrases that copy T[o,...,o+m-1]
auto occ_2_sided = query_2_sided_recursive(o,o+m-1);
//add all 2 sided occurrences to the result
for(auto h : occ_2_sided)
occ.push_back(h);
}
}
}
}
}
//extend search
fwd_range = fm_index_fwd->LF(fwd_range,pattern[m-i-1]);
}
assert(occ.size() == count(pattern));
return occ;
}
vector<ulint> locate_light(string &pattern, bool optimize = true){
//first, find range of pattern (reversed because we use the reverse index)
auto range = fm_index_rev->count(pattern,true);
ulint n_occ = range.second < range.first ? 0 : range.second - range.first + 1;
//we will extract text in range and put here all primary occurrences found
set<ulint> primary_occurrences;
extract_occ_fwd(range,primary_occurrences,pattern.size());
vector<ulint> occ;
for(auto o1:primary_occurrences) occ.push_back(o1);
for(auto o1:primary_occurrences){
if((not optimize) or occ.size() < n_occ){
for(auto o2 : query_2_sided_recursive(o1,o1+pattern.size()-1))
occ.push_back(o2);
}
}
assert(n_occ == count(pattern));
return occ;
}
/*
* input: F range containing a unique character, a set where to put found
* primary occurrences, number i of FL steps left to do
*/
void extract_occ_fwd(range_t rn, set<ulint>& occ, ulint i, bool ignore_interval=true){
if(i==0) return;
//search and extract SA samples of marked positions inside interval rn
//we ignore SA samples at last pattern position (ignore_interval)
//because we want to treat occurrences that are suffix of a LZ phrase
//as secondary occurrences.
if(not ignore_interval) find_occ(rn, occ, i);
//map range from F to L
vector<range_t> ranges_on_F = fm_index_rev->FL(rn);
//recursion
for(auto r : ranges_on_F){
extract_occ_fwd(r,occ,i-1,false);
}
}
void find_occ(range_t rn, set<ulint>& occ, ulint i){
assert(i>0);
assert(rn.first>0); //# must not be contained in the range
assert(rn.first<=last->size());
assert(rn.second+1<=last->size());
assert(SA_samples.size() == last->rank(last->size()));
assert(SA_samples.size() == begin_of_phrase.rank(begin_of_phrase.size()));
for(ulint j = last->rank(rn.first);j<last->rank(rn.second+1);++j){
assert(j<SA_samples.size());
ulint text_position = begin_of_phrase.select(SA_samples[j]);
//decrement: text_position is the first position of a phrase, but we
//are interested in the last position of previous phrase
assert(text_position>0);
text_position--;
assert(text_position >= (i - 1));
occ.insert( text_position - (i - 1) );
}
}
void build(string &input, bool verbose){
if(verbose)
cout << "Initializing structures for the LZ77 parser ... " << endl;
set<pair<uchar,ulint> > alphabet_and_freq;
{
std::istringstream iss(input);
alphabet_and_freq = lz77_parser_t::get_alphabet_and_frequencies(iss);
}
alphabet = vector<uchar>();
for(auto c : alphabet_and_freq)
alphabet.push_back(c.first);
std::sort(alphabet.begin(),alphabet.end());
std::istringstream istr(input);
auto parser = lz77_parser_t(istr,alphabet_and_freq,128,true);
//size of the text
ulint text_length = input.size();
bwt_length=text_length + 1;
{
//this vector will contain true in positions that are the first character of a phrase
vector<bool> phrase_start_vec;
ulint z=0; //number of LZ phrases
//this vector will contain the 2D points for range search, one per phrase
vector<pair<point_2d_t, ulint> > two_sided_points;
ulint phrase_start_position = 0; //start position of the current phrase
//start parsing
while(not parser.eof()){
auto token = parser.get_token(); //get a factor
assert((not token.start_position_is_defined) or token.start_position < phrase_start_position);
//append a true: we are at the beginning of a phrase
phrase_start_vec.push_back(true);
//append false for the remaining phrase characters
for(ulint i=1;i<token.phrase.size();++i)
phrase_start_vec.push_back(false);
//for all factors with a start position, insert the corresponding point in the
//2-sided range search structure
if(token.start_position_is_defined){
//pair: <start_position, end_position> of the copied string
point_2d_t point = {(ulint)token.start_position,
(ulint)(token.start_position + token.phrase.size()-1)};
//insert the point in the vector of points.
//we associate phrase rank (z) to each point
two_sided_points.push_back({point,z});
}
z++; //increment number of phrases
//compute start position of next phrase
phrase_start_position += token.phrase.size();
}
if(verbose)
cout << "Building two-sided range data structure ... " << flush;
//build range search structure. Do not re-map y coordinates: saves z words of space
lz_2_sided_range_search = new range_search_t(two_sided_points,true,false);
if(verbose)
cout << "done!" << endl;
assert(phrase_start_vec.size()==text_length);
begin_of_phrase = sparse_bitvector_t(phrase_start_vec);
}
// Now build FM indexes
// reverse index:
if(verbose)
cout << "Building reverse RLBWT ... " << flush;
{
string rev_text;
for(ulint i=0;i<text_length;++i)
rev_text.push_back(input[text_length - i - 1]);
fm_index_rev = new fm_index_t(rev_text);
}
if(verbose)
cout << "done!" << endl;
// build array 'last'
{
if(verbose)
cout << "Building sparse bitvector marking end of phrases ... " << flush;
//first, build a standard bitvector where phrase ends (on the F column of the BWT of the
//reversed text) are marked with a 1. Then, convert it to a sparse bitvector.
vector<bool> last_bv(bwt_length, false);
//position on rev BWT: initially, position 0 == position 0 on text
ulint bwt_pos = 0;
//navigate the reverse BWT, reading the (forward) text from first to last character
for(ulint i=0;i<text_length;++i){
//if i is the first position of a phrase and i>0, then the current BWT position is last
//position of a phrase on column F
if(begin_of_phrase[i] and i>0){
assert(bwt_pos < last_bv.size());
last_bv[bwt_pos] = true; //mark the position
}
bwt_pos = fm_index_rev->LF(bwt_pos); //update position in the BWT
}
//now bwt_pos is the position on BWT of #. With another step we go back to the
//position of # on the F column, which must be equal to 0 if everything is correct
bwt_pos = fm_index_rev->LF(bwt_pos);
assert(bwt_pos==0);
last_bv[bwt_pos] = true;
//now convert the bitvector to a sparse bitvector
last = new sparse_bitvector_t(last_bv);
if(verbose)
cout << "done!" << endl;
}
//populate SA_samples
if(type==light){
if(verbose)
cout << "Sampling suffix array on LZ phrase borders ... " << flush;
assert(text_length>0);
//in SA_samples we write phrase ranks (on the text)
ulint z = begin_of_phrase.rank(begin_of_phrase.size());
assert( z == last->rank(last->size()));
ulint bitlength = 64 - __builtin_clzll(z);
SA_samples = packed_view<vector>(bitlength, z);
assert(SA_samples.width()==bitlength);
//position on F column of rev BWT
ulint F_pos = 0;
//navigate the reverse BWT, reading the (forward) text from first to last character
//i = text position corresponding to F_pos
for(ulint i=0;i<text_length;++i){
//if i is the first position of a phrase and i>0, then the current BWT position
//is last position of a phrase
if(begin_of_phrase[i] and i>0){
assert(F_pos < last->size());
assert(last->at(F_pos));
SA_samples[last->rank(F_pos)] = begin_of_phrase.rank(i);
}
F_pos = fm_index_rev->LF(F_pos); //update position in the BWT
}
if(verbose)
cout << "done!" << endl;
}
//forward index
if(verbose)
cout << "Building forward RLBWT ... " << flush;
fm_index_fwd = new fm_index_t(input);
//detect position of terminator character in the BWT
terminator_pos = fm_index_fwd->get_terminator_position();
if(verbose)
cout << "done!" << endl;
//Now build the interval data structure on the suffix tree nodes corresponding to the LZ factors
if(type!=light){
if(verbose)
cout << "Building interval data structure on the subset of ST nodes ... " << flush;
vector<pair<ulint, ulint> > bwt_intervals;
//the bwt intervals corresponding to the factors
//we use an ordered set to keep only one occurrence of each interval
//here, ordering is lexicographic (in the cooordinates of the pairs)
set<pair<ulint, ulint> > bwt_intervals_set;
//initial range: full
pair<ulint, ulint> range = {0,bwt_length-1};
//scan text from end to begin, except first character: in this way we do not include
//the first phrase. This is correct since before the first phrase there are no characters, so
//the 2D point is not well-defined.
for(ulint i=0;i<text_length-1;++i){
//extend range with current text character
range = fm_index_fwd->LF(range, input[text_length-i-1]);
//if this character is the begin of a phrase, then current range is a the range of a factor
if(begin_of_phrase[text_length-i-1]){
//insert range in the set
bwt_intervals_set.insert(range);
//reset interval to full
range = {0,bwt_length-1};
}
}
//build structure
st_sub = new st_subset(bwt_length,bwt_intervals_set);
//convert intervals set to a vector
bwt_intervals = vector<pair<ulint, ulint> >(bwt_intervals_set.size());
std::copy(bwt_intervals_set.begin(), bwt_intervals_set.end(), bwt_intervals.begin());
auto comp = [](const pair<ulint,ulint> &A, const pair<ulint,ulint> &B){
//A precedes B
bool preceded = A.second < B.first;
//B is contained in A and the two intervals are distinct
bool contained = (B.first >= A.first and B.second < A.second) or
(B.second <= A.second and B.first > A.first);
return preceded or contained;