-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlf_gbwt.h
More file actions
1374 lines (1179 loc) · 70.4 KB
/
lf_gbwt.h
File metadata and controls
1374 lines (1179 loc) · 70.4 KB
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
/*
MIT License
Copyright (c) 2024 Ahsan Sanaullah
Copyright (c) 2024 S. Zhang Lab at UCF
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//Classes in this header are derived from the GBWT and CompressedRecord, with the following license:
/*
Copyright (c) 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 Jouni Siren
Copyright (c) 2015, 2016, 2017 Genome Research Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef GBWT_QUERY_LF_GBWT_H
#define GBWT_QUERY_LF_GBWT_H
//heavily derived/modified from FastLocate
#include<gbwt/gbwt.h>
#include<gbwt/internal.h>
#include<random>
namespace lf_gbwt{
typedef std::uint8_t byte_type;
class GBWT;
//all inputs and outputs to CompressedRecord are in the compressed alphabet (toComp)
struct CompressedRecord
{
typedef gbwt::size_type size_type;
//outgoing[i] stores the value BWT.rank(v,i) where v is the current node
sdsl::int_vector<0> outgoing;
//bit vector of length n where n is the length of the current node
//bit i is set if a concrete run starts at i
sdsl::sd_vector<> first;
//bit vector of length n*\sigma where \sigma is the size of outgoing
//bit i is set if a concrete run of value \floor(i/n) starts at i%n
sdsl::sd_vector<> firstByAlphabet;
//bit vector of length n where the ith set bit corresponds to the ith set bit in firstByAlphabet
//the number of bits in [ith set bit, i+1th set bit) is the length of the concrete run corresponding to the
//ith set bit
sdsl::sd_vector<> firstByAlphComp;
//bit vector of length gbwt.effective(), bit i is set if node i is contained in the BWT of this record
sdsl::sd_vector<> alphabet;
//int i stores the mapped alphabet value of the BWT run
sdsl::int_vector<0> alphabetByRun;
CompressedRecord() = default;
CompressedRecord(const gbwt::CompressedRecord &, const GBWT *);
size_type serialize(std::ostream& out, sdsl::structure_tree_node* v = nullptr, std::string name = "") const;
void load(std::istream& in);
size_type size() const { return this->first.size(); }
bool empty() const { return (this->size() == 0); }
std::pair<size_type, size_type> runs() const; // (concrete, logical)
size_type outdegree() const { return this->outgoing.size(); }
// Returns (node, LF(i, node)) or invalid_edge() if the offset is invalid.
gbwt::edge_type LF(size_type i) const;
// Returns `offset` such that `LF(offset) == (to, i)`, or `invalid_offset()`
// if there is no such offset.
// This can be used for computing inverse LF in a bidirectional GBWT.
size_type offsetTo(gbwt::comp_type to, size_type i) const;
// As above, but also reports the closed offset range ('run') and the identifier
// ('run_id') of the logical run used for computing LF().
gbwt::edge_type LF(size_type i, gbwt::range_type& run, size_type& run_id) const;
// As above, but also sets 'run_end' to the last offset of the current logical run.
gbwt::edge_type runLF(size_type i, size_type& run_end) const;
// Returns invalid_offset() if there is no edge to the destination.
size_type LF(size_type i, gbwt::comp_type to) const;
// Returns Range::empty_range() if the range is empty or the destination is invalid.
gbwt::range_type LF(gbwt::range_type range, gbwt::comp_type to) const;
// the following two functions assume valid i, 0 <= i < this->size()
size_type compAlphabetAt(size_type i) const { return this->alphabetByRun[this->first.predecessor(i)->first]; }
// Returns BWT[i] within the record
gbwt::comp_type operator[](size_type i) const { return this->alphabet.select_iter(this->compAlphabetAt(i)+1)->second; }
bool hasEdge(gbwt::comp_type to) const { return this->edgeTo(to) != gbwt::invalid_offset(); }
// Maps successor nodes to outranks.
gbwt::rank_type edgeTo(gbwt::comp_type to) const {
auto iter = this->alphabet.predecessor(to);
return (iter != this->alphabet.one_end() && iter->second == to)? iter->first : gbwt::invalid_offset();
};
// These assume that 'outrank' is a valid outgoing edge.
gbwt::comp_type successor(gbwt::rank_type outrank) const { return this->alphabet.select_iter(outrank+1)->second; }
//gbwt::node_type successor(gbwt::rank_type outrank) const { return this->alphabet.successor(outrank)->second; }
//what is this? when is it used?
//size_type offset(gbwt::rank_type outrank) const { return this->outgoing[outrank]; }
//returns the run id by logical runs of an offset
size_type logicalRunId(size_type i) const {
if (i >= this->size())
return gbwt::invalid_offset();
size_type concRunId = this->first.predecessor(i)->first;
if (!this->hasEdge(gbwt::ENDMARKER))
return concRunId;
size_type num0RunsBefore = this->firstByAlphabet.successor(i)->first;
size_type num0Before = this->firstByAlphComp.select_iter(num0RunsBefore+1)->second;
//error if in the middle of an endmarker run? counts endmarkers past current
//if ((*this)[i] == gbwt::ENDMARKER) {
// num0Before -= (++this->first.predecessor(i))->second - i;
return concRunId - num0RunsBefore + num0Before;
}
};
//The GBWT is split into two subsets, one of small records and one of large records
//small records are those with outdegree < maxOutDegree
struct SmallRecordArray{
typedef gbwt::size_type size_type;
size_type maxOutdegree = 0;
size_type records = 0;
size_type effective = 0;
sdsl::sd_vector<> outDegreePrefixSum;
sdsl::sd_vector<> emptyRecords;
//bit vector, if the c-th set bit (0-indexed) is at position i in prefixSum,
//then, i is c + the sum of the lengths of all nodes < c
//has \sigma set bits
//note by GBWT assumption, \sigma < 2^32 and sum of their lengths <= (2^32 - 1)^2
//therefore the max length of this array is 2^64 - 2^33 + 1 + 2^32 - 1 = 2^64 - 2^32
sdsl::sd_vector<> prefixSum;
//outgoing[i*maxOutdegree + j] stores the value BWT.rank(v,j) where v is the i-th node in the small record array
//above wrong, use prefix sum
sdsl::int_vector<0> outgoing;
//stores start positions of runs of each node concatenated, starting at prefixSum
sdsl::sd_vector<> first;
//bit vector of length prefixSum.length() * maxOutdegree
//call prefixSum(i) the position of the i-th set bit in prefix sum
//call the i-th window (0-indexed) [maxOutdegree*prefixSum(i), maxOutdegree*prefixSum(i) + (prefixSum(i+1) - prefixSum(i+) - 1)*maxOutdegree)
//the has maxOutdegree sections, each of length prefixSum(i+1) - prefixSum(i) - 1 (the length of the node)
//the i-th window is equivalent to firstByAlphabet if this node was a CompressedRecord
sdsl::sd_vector<>firstByAlphabet;
//bit vector of length prefixSum.length()
//call the i-th window (0-indexed) [prefixSum(i), prefixSum(i+1))
//this windodw is equivalent to firstByAlphComp if node i was a CompressedRecord
sdsl::sd_vector<> firstByAlphComp;
//bit vector of length \sigma^2
//\sigma is the size the number of nodes in the GBWT ( = gbwt.effective())
//note by GBWT assumption, \sigma <= 2^32
//the i-th bit is set if the i/\sigma-th node has an edge to i%\sigma
//
//emptynodes windows of length gbwt.effective()
sdsl::sd_vector<> alphabet;
//stores the mapped alphabet value of the BWT run
//concatenated for all smallRecords
sdsl::int_vector<0> alphabetByRun;
size_type serialize(std::ostream& out, sdsl::structure_tree_node* v = nullptr, std::string name = "") const;
void load(std::istream& in);
//these assume node is a valid index in smallRecordArray
size_type size(const size_type node) const;
bool empty(const size_type node) const;
std::pair<bool, size_type> emptyAndNonEmptyIndex(const size_type node) const;
std::pair<size_type, size_type> runs(const size_type node) const;
size_type outdegree(const size_type node) const;
size_type nonEmptyRecords() const { return records - emptyRecords.ones(); }
gbwt::edge_type LF(const size_type node, const size_type pos) const;
size_type offsetTo(const size_type node, const gbwt::comp_type to, size_type i) const;
gbwt::edge_type LF(const size_type node, const size_type i, gbwt::range_type& run, size_type& run_id) const;
gbwt::edge_type runLF(const size_type node, const size_type i, size_type& run_end) const;
size_type LF(const size_type node, const size_type i, const gbwt::comp_type to) const;
gbwt::range_type LF(const size_type node, const gbwt::range_type range, const gbwt::comp_type to) const;
size_type compAlphabetAt(const size_type node, const size_type i) const;
gbwt::comp_type bwtAt(const size_type node, const size_type i) const;
bool hasEdge(const size_type node, const gbwt::comp_type to) const;
gbwt::rank_type edgeTo(const gbwt::size_type node, const gbwt::comp_type to) const;
gbwt::comp_type successor(size_type node, gbwt::rank_type outrank) const;
size_type offset(size_type node, gbwt::rank_type outrank) const;
size_type logicalRunId(const size_type node, const size_type i) const;
};
class GBWT
{
public:
typedef gbwt::size_type size_type;
//------------------------------------------------------------------------------
GBWT() = default;
explicit GBWT(const gbwt::GBWT& source);
size_type serialize(std::ostream& out, sdsl::structure_tree_node* v = nullptr, std::string name = "") const;
void load(std::istream& in);
const static std::string EXTENSION; // .lfgbwt
//------------------------------------------------------------------------------
/*
Low-level interface: Statistics.
*/
size_type size() const { return this->header.size; }
bool empty() const { return (this->size() == 0); }
size_type sequences() const { return this->header.sequences; }
size_type sigma() const { return this->header.alphabet_size; }
size_type effective() const { return this->header.alphabet_size - this->header.offset; }
std::pair<size_type, size_type> runs() const;
std::pair<size_type, size_type> runs(const gbwt::node_type node) const {
//std::cout << "In runs(" << node << ")" << std::endl;
if (!contains(node))
return gbwt::invalid_edge();
//std::cout << "Valid Node" << std::endl;
auto ind = isSmallAndIndex(this->toComp(node));
return (ind.first)? this->smallRecords.runs(ind.second) : this->largeRecords[ind.second].runs();
}
bool bidirectional() const { return this->header.get(gbwt::GBWTHeader::FLAG_BIDIRECTIONAL); }
//------------------------------------------------------------------------------
/*
Metadata interface.
*/
bool hasMetadata() const { return this->header.get(gbwt::GBWTHeader::FLAG_METADATA); }
void addMetadata() { this->header.set(gbwt::GBWTHeader::FLAG_METADATA); }
void clearMetadata() { this->metadata.clear(); this->header.unset(gbwt::GBWTHeader::FLAG_METADATA); };
//------------------------------------------------------------------------------
/*
High-level interface. The queries check that the parameters are valid. Iterators
must be InputIterators. On error or failed search, the return values will be the
following:
find empty search state
prefix empty search state
extend empty search state
locate invalid_sequence() or empty vector
extract empty vector
*/
gbwt::vector_type extract(size_type sequence) const;
bool contains(gbwt::node_type node) const
{
return ((node < this->sigma() && node > this->header.offset) || node == gbwt::ENDMARKER);
}
gbwt::node_type firstNode() const { return this->header.offset + 1; }
gbwt::comp_type toComp(gbwt::node_type node) const { return (node == 0 ? node : node - this->header.offset); }
gbwt::node_type toNode(gbwt::comp_type comp) const { return (comp == 0 ? comp : comp + this->header.offset); }
gbwt::node_type bwt(const gbwt::node_type node, const gbwt::size_type i) const {
//std::cout << "In bwt(node:" << node << ", i: " << i << ")" << std::endl;
if (!contains(node))
return gbwt::invalid_offset();
//std::cout << "Valid node" << std::endl;
auto ind = isSmallAndIndex(this->toComp(node));
//std::cout << "ind: (" << ind.first << ", " << ind.second << std::endl;
return this->toNode(
(ind.first)? this->smallRecords.bwtAt(ind.second, i): this->largeRecords[ind.second][i]
);
}
size_type nodeSize(gbwt::node_type node) const {
//std::cout << "In GBWT::nodeSize(node: " << node << "); toComp: " << this->toComp(node) << std::endl;
auto ind = this->isSmallAndIndex(this->toComp(node));
//std::cout << "ind :(" << ind.first << ", " << ind.second << ")" << std::endl;
return (ind.first)? smallRecords.size(ind.second) : this->largeRecords[ind.second].size(); }
bool empty(gbwt::node_type node) const { return this->nodeSize(node) == 0; }
//------------------------------------------------------------------------------
/*
Low-level interface: Navigation and searching. The interface assumes that node
identifiers are valid. This can be checked with contains().
*/
//returns pair first value is true if node is small, second value is index in smallRecords if small, otherwise index in largeRecords
std::pair<bool, gbwt::size_type> isSmallAndIndex(gbwt::comp_type comp) const {
assert(comp < this->effective());
//std::cout << "isSmallAndIndex(comp: " << comp << ")" << std::endl;
/*for (auto it = isSmall.one_begin(); it != isSmall.one_end(); ++it)
std::cout << "(" << it->first << ", " << it->second << ")\t";
std::cout << std::endl;*/
//numSmall in [0, comp)
gbwt::size_type numSmall = isSmall.successor(comp)->first;
//auto nextSmall = isSmall.successor(comp);
return (isSmall[comp])? std::pair<bool,gbwt::size_type>{true, numSmall} : std::pair<bool,gbwt::size_type>{false, comp-numSmall};
}
// On error: invalid_edge().
gbwt::edge_type LF(gbwt::edge_type position) const
{
//std::cout << "In bwt( edge:(" << position.first << ", " << position.second << "))" << std::endl;
auto ind = this->isSmallAndIndex(this->toComp(position.first));
//std::cout << "ind: (" << ind.first << ", " << ind.second << ")" << std::endl;
gbwt::edge_type ans = (ind.first)? this->smallRecords.LF(ind.second, position.second) : this->largeRecords[ind.second].LF(position.second);
//std::cout << "ans: (" << ans.first << ", " << ans.second << ")" << std::endl;
ans.first = this->toNode(ans.first);
//std::cout << "ans after toNode: (" << ans.first << ", " << ans.second << ")" << std::endl;
return ans;
}
gbwt::edge_type LF(gbwt::edge_type position, gbwt::range_type& run, gbwt::size_type& run_id) const {
auto ind = this->isSmallAndIndex(this->toComp(position.first));
gbwt::edge_type next = (ind.first)? this->smallRecords.LF(ind.second, position.second, run, run_id) : this->largeRecords[ind.second].LF(position.second, run, run_id);
next.first = this->toNode(next.first);
return next;
}
// Only works in bidirectional indexes. May be slow when the predecessor is the endmarker.
// On error: invalid_edge().
gbwt::edge_type inverseLF(gbwt::node_type from, size_type i) const;
// Only works in bidirectional indexes. May be slow when the predecessor is the endmarker.
// On error: invalid_edge().
gbwt::edge_type inverseLF(gbwt::edge_type position) const
{
return this->inverseLF(position.first, position.second);
}
gbwt::node_type predecessorAt(gbwt::node_type, size_type i) const;
//------------------------------------------------------------------------------
/*
Low-level interface: Sequences. The interface assumes that node identifiers are
valid. This can be checked with contains().
*/
//------------------------------------------------------------------------------
gbwt::GBWTHeader header;
gbwt::Tags tags;
sdsl::sd_vector<> isSmall;
SmallRecordArray smallRecords;
std::vector<CompressedRecord> largeRecords;
gbwt::Metadata metadata;
// Decompress and cache the endmarker, because decompressing it is expensive.
//CompressedRecord *endmarker_record;
//------------------------------------------------------------------------------
/*
Internal interface. Do not use.
*/
/*const CompressedRecord& record(gbwt::node_type node) const {
if (!this->contains(node))
throw std::invalid_argument("node given to lf_gbwt.record invalid!");
return this->bwt[this->toComp(node)];
}*/
bool verify(const gbwt::GBWT& g) {
//assert(this->size() == g.size());
//assert(this->sequences() == g.sequences());
//assert(this->sigma() == g.sigma());
//assert(this->effective() == g.effective());
//std::cout << "this runs " << this->runs().first << " g runs " << g.runs().first << std::endl;
//std::cout << "this runs " << this->runs().second<< " g runs " << g.runs().second<< std::endl;
//assert(this->runs() == g.runs());
//assert(this->bidirectional() == g.bidirectional());
bool equal = (this->size() == g.size()) && (this->sequences() == g.sequences()) && (this->sigma() == g.sigma()) && (this->effective() == g.effective()) && (this->runs() == g.runs()) && (this->bidirectional() == g.bidirectional());
//std::cout << std::boolalpha << "Precheck passed? " << equal << std::endl;
//assert(equal);
equal = equal && this->verifyBWT(g);
//assert(equal);
//std::cout << std::boolalpha << "BWT passed? " << equal << std::endl;
equal = equal && this->verifyLF(g);
//assert(equal);
//std::cout << std::boolalpha << "LF passed? " << equal << std::endl;
equal = equal && (!this->bidirectional() || this->verifyInverseLF(g));
//assert(equal);
//std::cout << std::boolalpha << "inverseLF passed? " << equal << std::endl;
return equal;
}
//assumes number of nodes and node ids are equivalent
bool verifyBWT(const gbwt::GBWT& g) {
bool equal = true;
#pragma omp parallel for schedule(dynamic, 1)
for (gbwt::comp_type i = 0; i < this->effective(); ++i) {
auto ind = this->isSmallAndIndex(i);
bool nodeEqual = this->toNode(i) == g.toNode(i);
gbwt::CompressedRecord rec = g.record(g.toNode(i));
nodeEqual = nodeEqual && this->nodeSize(this->toNode(i)) == rec.size();
for (gbwt::size_type j = 0; nodeEqual && j < rec.size(); ++j)
nodeEqual = nodeEqual && rec[j] == this->bwt(this->toNode(i), j);
#pragma omp critical
{
equal = equal && nodeEqual;
}
}
return equal;
}
bool verifyLF(const gbwt::GBWT& g) {
bool equal = true;
#pragma omp parallel for schedule(dynamic, 1)
for (gbwt::comp_type i = 0; i < this->effective(); ++i) {
gbwt::node_type node = this->toNode(i);
bool nodeEqual = node == g.toNode(i);
gbwt::size_type nodeSize = g.nodeSize(node);
nodeEqual = nodeEqual && nodeSize == this->nodeSize(node);
for (gbwt::size_type j = 0; nodeEqual && j < nodeSize; ++j)
nodeEqual = nodeEqual && this->LF({node, j}) == g.LF({node, j});
#pragma omp critical
{
equal = equal && nodeEqual;
}
}
return equal;
}
bool verifyInverseLF(const gbwt::GBWT& g) {
bool equal = this->bidirectional() && g.bidirectional();
#pragma omp parallel for schedule(dynamic, 1)
for (gbwt::comp_type i = 0; i < this->effective(); ++i) {
gbwt::node_type node = this->toNode(i);
bool nodeEqual = node == g.toNode(i);
gbwt::size_type nodeSize = g.nodeSize(node);
nodeEqual = nodeEqual && nodeSize == this->nodeSize(node);
for (gbwt::size_type j = 0; nodeEqual && j < nodeSize; ++j)
nodeEqual = nodeEqual && this->inverseLF({node, j}) == g.inverseLF({node, j});
#pragma omp critical
{
equal = equal && nodeEqual;
}
}
return equal;
}
}; // class GBWT
const std::string GBWT::EXTENSION = ".lfgbwt"; // .lfgbwt
//CompressedRecord member functions
CompressedRecord::CompressedRecord(const gbwt::CompressedRecord & rec, const GBWT *source){
size_type n = rec.size(), sigma = rec.outgoing.size(), runs = rec.runs().first;
if (n == 0)
return;
//outgoing
this->outgoing.resize(sigma);
for (unsigned i = 0; i < sigma; ++i)
this->outgoing[i] = rec.outgoing[i].second;
sdsl::util::bit_compress(this->outgoing);
//alphabet
sdsl::sd_vector_builder alphabetBuilder(source->effective(), sigma);
for (unsigned i = 0; i < sigma; ++i)
alphabetBuilder.set(source->toComp(rec.outgoing[i].first));
this->alphabet = sdsl::sd_vector<>(alphabetBuilder);
//first, firstByAlphabet, firstByAlphComp, and alphabetByRun;
sdsl::sd_vector_builder
firstBuilder(n, runs),
firstByAlphabetBuilder(n*sigma, runs),
firstByAlphCompBuilder(n, runs);
this->alphabetByRun.resize(runs);
//start location of run in firstByAlphabetAssist and length of run
std::vector<std::vector<std::pair<size_type,size_type>>> firstByAlphabetAssist;
firstByAlphabetAssist.resize(sigma);
gbwt::CompressedRecordFullIterator iter(rec);
size_type start, runInd = 0;
while (!iter.end()){
start = iter.offset() - iter.run.second;
firstBuilder.set(start);
firstByAlphabetAssist[iter.run.first].emplace_back(iter.run.first*n + start, iter.run.second);
this->alphabetByRun[runInd] = iter.run.first;
++iter;
++runInd;
}
this->first = sdsl::sd_vector<>(firstBuilder);
sdsl::util::bit_compress(this->alphabetByRun);
size_type currInd = 0;
for (const auto & alphArr : firstByAlphabetAssist){
for (const auto & a : alphArr){
firstByAlphabetBuilder.set(a.first);
firstByAlphCompBuilder.set(currInd);
currInd += a.second;
}
}
this->firstByAlphabet = sdsl::sd_vector<>(firstByAlphabetBuilder);
this->firstByAlphComp = sdsl::sd_vector<>(firstByAlphCompBuilder);
}
CompressedRecord::size_type CompressedRecord::serialize(std::ostream& out, sdsl::structure_tree_node* v, std::string name) const {
sdsl::structure_tree_node* child = sdsl::structure_tree::add_child(v, name, sdsl::util::class_name(*this));
size_type written_bytes = 0;
written_bytes += sdsl::serialize(this->outgoing, out, child, "outgoing");
written_bytes += sdsl::serialize(this->first, out, child, "first");
written_bytes += sdsl::serialize(this->firstByAlphabet, out, child, "firstByAlphabet");
written_bytes += sdsl::serialize(this->firstByAlphComp, out, child, "firstByAlphComp");
written_bytes += sdsl::serialize(this->alphabet, out, child, "alphabet");
written_bytes += sdsl::serialize(this->alphabetByRun, out, child, "alphabetByRun");
sdsl::structure_tree::add_size(child, written_bytes);
return written_bytes;
}
void CompressedRecord::load(std::istream& in) {
sdsl::load(this->outgoing, in);
sdsl::load(this->first, in);
sdsl::load(this->firstByAlphabet, in);
sdsl::load(this->firstByAlphComp, in);
sdsl::load(this->alphabet, in);
sdsl::load(this->alphabetByRun, in);
}
std::pair<CompressedRecord::size_type, CompressedRecord::size_type> CompressedRecord::runs() const {
size_type totRuns = this->first.ones();
if (!this->hasEdge(gbwt::ENDMARKER))
return {totRuns, totRuns};
if (this->outgoing.size() == 1)
return {totRuns, this->size()};
size_type numENDMARKERruns = this->firstByAlphabet.predecessor(this->size()-1)->first + 1;
size_type numENDMARKERS = this->firstByAlphComp.select_iter(numENDMARKERruns+1)->second;
return {totRuns, totRuns - numENDMARKERruns + numENDMARKERS};
}; // (concrete, logical)
// Returns (node, LF(i, node)) or invalid_edge() if the offset is invalid.
gbwt::edge_type CompressedRecord::LF(size_type i) const {
if (i >= this->size())
return gbwt::invalid_edge();
gbwt::comp_type next = (*this)[i];
return {next, this->LF(i, next)};
}
//SmallRecordArray member functions
SmallRecordArray::size_type SmallRecordArray::serialize(std::ostream& out, sdsl::structure_tree_node* v, std::string name) const {
sdsl::structure_tree_node* child = sdsl::structure_tree::add_child(v, name, sdsl::util::class_name(*this));
size_type written_bytes = 0;
written_bytes += sdsl::serialize(this->maxOutdegree, out, child, "maxOutdegree");
written_bytes += sdsl::serialize(this->records, out, child, "records");
written_bytes += sdsl::serialize(this->effective, out, child, "effective");
written_bytes += sdsl::serialize(this->outDegreePrefixSum, out, child, "outDegreePrefixSum");
written_bytes += sdsl::serialize(this->emptyRecords, out, child, "emptyRecords");
written_bytes += sdsl::serialize(this->prefixSum, out, child, "prefixSum");
written_bytes += sdsl::serialize(this->outgoing, out, child, "outgoing");
written_bytes += sdsl::serialize(this->first, out, child, "first");
written_bytes += sdsl::serialize(this->firstByAlphabet, out, child, "firstByAlphabet");
written_bytes += sdsl::serialize(this->firstByAlphComp, out, child, "firstByAlphComp");
written_bytes += sdsl::serialize(this->alphabet, out, child, "alphabet");
written_bytes += sdsl::serialize(this->alphabetByRun, out, child, "alphabetByRun");
sdsl::structure_tree::add_size(child, written_bytes);
return written_bytes;
}
void SmallRecordArray::load(std::istream& in) {
sdsl::load(this->maxOutdegree, in);
sdsl::load(this->records, in);
sdsl::load(this->effective, in);
sdsl::load(this->outDegreePrefixSum, in);
sdsl::load(this->emptyRecords, in);
sdsl::load(this->prefixSum, in);
sdsl::load(this->outgoing, in);
sdsl::load(this->first, in);
sdsl::load(this->firstByAlphabet, in);
sdsl::load(this->firstByAlphComp, in);
sdsl::load(this->alphabet, in);
sdsl::load(this->alphabetByRun, in);
}
SmallRecordArray::size_type SmallRecordArray::size(const SmallRecordArray::size_type node) const {
//std::cout << "In SmallRecordArray::size(" << node << ")" << std::endl;
assert(node < records);
auto p = emptyAndNonEmptyIndex(node);
//std::cout << "p: (" << p.first << ", " << p.second << ")" << std::endl;
if (p.first) return 0;
//std::cout << "prefixSum.ones() " << prefixSum.ones() << std::endl;
auto it = prefixSum.select_iter(p.second + 1);
//std::cout << "*beg: (" << it->first << ", " << it->second << ")" << std::endl;
//size_type beg = (it++)->second;
size_type beg = it->second;
++it;
//std::cout << "*it: (" << it->first << ", " << it->second << ")" << std::endl;
//std::cout << "Leaving SmallRecordArray::size(" << node << ")" << std::endl;
return it->second - beg;
}
bool SmallRecordArray::empty(const size_type node) const {
assert(node < records);
return emptyRecords[node];
}
std::pair<bool, SmallRecordArray::size_type> SmallRecordArray::emptyAndNonEmptyIndex(const SmallRecordArray::size_type node) const {
assert(node < records);
if (maxOutdegree == 1)
return {true, 0};
auto it = emptyRecords.successor(node);
return {it->second == node, node - it->first};
}
std::pair<SmallRecordArray::size_type, SmallRecordArray::size_type> SmallRecordArray::runs(const SmallRecordArray::size_type node) const {
//std::cout << "Entering SmallRecordArray::runs(" << node << ")" << std::endl;
assert (node < records);
auto pos = emptyAndNonEmptyIndex(node);
if (pos.first) return {0, 0};
auto beg = prefixSum.select_iter(pos.second + 1);
//std::cout << "Post increment bug?" << std::endl;
auto end = beg;
++end;
//std::cout << "Post increment bug?" << std::endl;
auto runBeg = first.successor(beg->second);
auto runEnd = first.successor(end->second);
assert(runBeg->second == beg->second);
assert(runEnd->second == end->second);
size_type concRuns = runEnd->first - runBeg->first;
//check if this has an edge to the endmarker
assert(hasEdge(node, gbwt::ENDMARKER) == alphabet[pos.second*effective]);
if (!alphabet[pos.second*effective])
return {concRuns, concRuns};
//std::cout << "This node has an edge to the endmarker" << std::endl;
auto begAlphRun = firstByAlphabet.successor(maxOutdegree*beg->second);
auto endAlphRun = firstByAlphabet.successor(maxOutdegree*beg->second + (end->second - beg->second));
size_type numENDMARKERruns = endAlphRun->first - begAlphRun->first;
//std::cout << "numENDMARKERruns: " << numENDMARKERruns << std::endl;
assert(numENDMARKERruns > 0);
size_type numENDMARKERS = firstByAlphComp.select_iter(endAlphRun->first + 1)->second - firstByAlphComp.select_iter(begAlphRun->first + 1)->second;
//std::cout << "numENDMARKERS: " << numENDMARKERS << std::endl;
assert(numENDMARKERS > 0);
return {concRuns, concRuns - numENDMARKERruns + numENDMARKERS};
}
SmallRecordArray::size_type SmallRecordArray::outdegree(const SmallRecordArray::size_type node) const {
assert(node < records);
auto pos = emptyAndNonEmptyIndex(node);
if (pos.first) return 0;
size_type ans = alphabet.successor((pos.second + 1) * effective)->first - alphabet.successor(pos.second * effective)->first;
assert(ans != 0);
return ans;
}
gbwt::edge_type SmallRecordArray::LF(const SmallRecordArray::size_type node, const SmallRecordArray::size_type pos) const {
assert (node < records);
if (pos >= size(node))
return gbwt::invalid_edge();
gbwt::comp_type next = bwtAt(node, pos);
return {next, LF(node, pos, next)};
}
SmallRecordArray::size_type SmallRecordArray::offsetTo(const SmallRecordArray::size_type node, const gbwt::comp_type to, SmallRecordArray::size_type i) const {
assert(node < records);
//std::cout << "In SmalRecordArray::offsetTo( node: " << node << ", to: " << to << ", i: " << i << ")" << std::endl;
auto p = emptyAndNonEmptyIndex(node);
//std::cout << "p: (" << p.first << ", " << p.second << ")" << std::endl;
if (p.first) return gbwt::invalid_offset();
size_type outrank = edgeTo(node, to);
//std::cout << "outrank: " << outrank << std::endl;
if (outrank == gbwt::invalid_offset()) return gbwt::invalid_offset();
size_type n = nonEmptyRecords();
//std::cout << "nonEmptyRecords(): " << n << std::endl;
size_type outgoingPrefixSum = alphabet.successor(p.second * effective)->first;
//std::cout << "outgoingPrefixSum: " << outgoingPrefixSum << std::endl;
i -= outgoing[outgoingPrefixSum + outrank];
//std::cout << "new i: " << i << std::endl;
auto prefixBeg = prefixSum.select_iter(p.second + 1);
auto prefixEnd = prefixBeg;
++prefixEnd;
size_type lengthPrefixSum = prefixBeg->second;
size_type nodeLength = prefixEnd->second - prefixBeg->second;
auto firstToRun = firstByAlphabet.successor((lengthPrefixSum * maxOutdegree) + nodeLength*outrank);
auto firstToCompRun = firstByAlphComp.select_iter(firstToRun->first+1);
auto runComp = firstByAlphComp.predecessor(firstToCompRun->second + i);
i -= (runComp->second - firstToCompRun->second);
auto run = firstByAlphabet.select_iter(runComp->first+1);
//std::cout << "Leaving offsetTo" << std::endl;
if ((run->second - (lengthPrefixSum * maxOutdegree))/nodeLength != outrank)
return gbwt::invalid_offset();
return (run->second - (lengthPrefixSum*maxOutdegree) - (outrank*nodeLength)) + i;
}
gbwt::edge_type SmallRecordArray::LF(const SmallRecordArray::size_type node, const SmallRecordArray::size_type i, gbwt::range_type& run, SmallRecordArray::size_type& run_id) const {
assert(node < records);
size_type nodeLength = size(node);
if (i >= nodeLength){
run.first = run.second = gbwt::invalid_offset();
run_id = gbwt::invalid_offset();
return gbwt::invalid_edge();
}
size_type nonEmptyIndex = emptyAndNonEmptyIndex(node).second;
run_id = logicalRunId(node, i);
auto prefixBeg = prefixSum.select_iter(nonEmptyIndex + 1);
auto it = first.predecessor(prefixBeg->second + i);
run.first = it->second - prefixBeg->second;
++it;
run.second = it->second - prefixBeg->second - 1;
return LF(node, i);
}
SmallRecordArray::size_type SmallRecordArray::LF(const SmallRecordArray::size_type node, const SmallRecordArray::size_type i, const gbwt::comp_type to) const {
assert(node < records);
//std::cout << "In SmallRecordArray::LF(node: " << node ",
size_type nodeLength = size(node);
bool isEmpty;
size_type nonEmptyIndex;
std::tie(isEmpty, nonEmptyIndex) = emptyAndNonEmptyIndex(node);
//why > instead of >=?
if (isEmpty || i > nodeLength || !hasEdge(node, to))
return gbwt::invalid_offset();
size_type outrank = edgeTo(node, to);
if (outrank == gbwt::invalid_offset()) return gbwt::invalid_offset();
auto prefixBeg = prefixSum.select_iter(nonEmptyIndex + 1);
size_type lengthPrefixSum = prefixBeg->second;
auto nextRun = firstByAlphabet.successor((lengthPrefixSum*maxOutdegree) + (outrank*nodeLength) + i);
auto firstOutrankRun = firstByAlphabet.successor((lengthPrefixSum*maxOutdegree) + (outrank*nodeLength));
auto nextRunComp = firstByAlphComp.select_iter(nextRun->first+1);
auto firstOutrankComp = firstByAlphComp.select_iter(firstOutrankRun->first+1);
size_type numOutrank = nextRunComp->second - firstOutrankComp->second;
if (bwtAt(node, i) == to) {
size_type afteriInRun = this->first.successor(lengthPrefixSum + i)->second - lengthPrefixSum - i;
numOutrank -= afteriInRun;
}
size_type outgoingPrefixSum = alphabet.successor(nonEmptyIndex*effective)->first;
return outgoing[outgoingPrefixSum + outrank] + numOutrank;
}
SmallRecordArray::size_type SmallRecordArray::compAlphabetAt(const SmallRecordArray::size_type node, const SmallRecordArray::size_type i) const {
assert (node < records);
auto p = emptyAndNonEmptyIndex(node);
if (p.first) return gbwt::invalid_offset();
if (i >= size(node)) return gbwt::invalid_offset();
size_type lengthPrefixSum = prefixSum.select_iter(p.second + 1)->second;
size_type runInd = first.predecessor(lengthPrefixSum + i)->first;
return alphabetByRun[runInd];
}
SmallRecordArray::size_type SmallRecordArray::bwtAt(const SmallRecordArray::size_type node, const SmallRecordArray::size_type i) const {
assert (node < records);
auto p = emptyAndNonEmptyIndex(node);
if (p.first) return gbwt::invalid_offset();
if (i >= size(node)) return gbwt::invalid_offset();
size_type outrank = compAlphabetAt(node, i);
auto it = alphabet.select_iter(outrank + alphabet.successor(p.second*effective)->first + 1);
return it->second - p.second*effective;
}
bool SmallRecordArray::hasEdge(const SmallRecordArray::size_type node, const gbwt::comp_type to) const {
return edgeTo(node, to) != gbwt::invalid_offset();
}
gbwt::rank_type SmallRecordArray::edgeTo(const gbwt::size_type node, const gbwt::comp_type to) const {
assert(node < records);
auto p = emptyAndNonEmptyIndex(node);
if (p.first) return gbwt::invalid_offset();
auto it = alphabet.successor(effective*p.second + to);
return (it->second == effective*p.second + to)? it->first - alphabet.successor(effective*p.second)->first : gbwt::invalid_offset();
}
gbwt::comp_type SmallRecordArray::successor(const SmallRecordArray::size_type node, const gbwt::rank_type outrank) const {
assert(node < records);
auto p = emptyAndNonEmptyIndex(node);
if (p.first) return gbwt::invalid_offset();
auto it = alphabet.select_iter(alphabet.successor(effective*p.second)->first + outrank + 1);
if (it->second >= effective*(p.second+1))
return gbwt::invalid_offset();
return it->second - (effective*p.second);
}
SmallRecordArray::size_type SmallRecordArray::logicalRunId(const SmallRecordArray::size_type node, const SmallRecordArray::size_type i) const {
//std::cout << "logicalRunId(node: " << node << ", i " << i << ")" << std::endl;
assert(node < records);
size_type nodeLength = size(node);
if (i >= nodeLength)
return gbwt::invalid_offset();
size_type nonEmptyIndex = emptyAndNonEmptyIndex(node).second;
auto prefixBeg = prefixSum.select_iter(nonEmptyIndex + 1);
auto it = first.predecessor(prefixBeg->second + i);
assert(first[prefixBeg->second] && first.predecessor(prefixBeg->second)->second == prefixBeg->second);
size_type concRunId = it->first - first.predecessor(prefixBeg->second)->first;
if (!hasEdge(node, gbwt::ENDMARKER))
return concRunId;
size_type num0RunsBefore = firstByAlphabet.successor(maxOutdegree*prefixBeg->second + i)->first;
size_type num0Before = firstByAlphComp.select_iter(num0RunsBefore + 1)->second;
auto alphPref = firstByAlphabet.successor(maxOutdegree*prefixBeg->second);
num0RunsBefore -= alphPref->first;
num0Before -= this->firstByAlphComp.select_iter(alphPref->first + 1)->second;
//std::cout << "concRunId " << concRunId << " num0RunsBefore " << num0RunsBefore << " num0Before " << num0Before << std::endl;
if (i != it->second - prefixBeg->second && bwtAt(node, i - 1) == gbwt::ENDMARKER) {
//remove 0s after and including i from num0before if the last run before i is of endmarkers
++it;
num0Before -= it->second - i - prefixBeg->second;
}
//std::cout << "concRunId " << concRunId << " num0RunsBefore " << num0RunsBefore << " num0Before " << num0Before << std::endl;
//std::cout << "leaving logicalRunId" << std::endl;
return concRunId - num0RunsBefore + num0Before;
}
//GBWT member functions
gbwt::node_type GBWT::predecessorAt(gbwt::node_type revFrom, size_type i) const {
auto revNode = [this](gbwt::comp_type x) {
if (x == gbwt::ENDMARKER)
return x;
return gbwt::Node::reverse(this->toNode(x));
};
if (this->toComp(revFrom) >= this->effective())
std::cout << "In predecessorAt, revFrom " << revFrom << " i " << i << " toComp(revFrom) " << this->toComp(revFrom) << " effective() " << this->effective() << std::endl;
auto ind = this->isSmallAndIndex(this->toComp(revFrom));
if (i >= this->nodeSize(revFrom))
return gbwt::invalid_node();
if (ind.first) {
auto t = smallRecords.emptyAndNonEmptyIndex(ind.second);
if (t.first)
return gbwt::invalid_node();
size_type prefixLength = this->smallRecords.prefixSum.select_iter(t.second + 1)->second;
size_type predoutrank = (this->smallRecords.firstByAlphabet.select_iter(
this->smallRecords.firstByAlphComp.predecessor(prefixLength + i)->first + 1
)->second - (prefixLength*this->smallRecords.maxOutdegree))/this->smallRecords.size(ind.second);
//check if before predoutrank is its reverse
auto alphIter = this->smallRecords.alphabet.select_iter(
this->smallRecords.alphabet.successor(t.second * this->effective())->first + 1 + predoutrank
);
if (predoutrank > 0) {
auto prevAlphIter = alphIter;
--prevAlphIter;
if (this->toNode(prevAlphIter->second - t.second * this->effective()) == revNode(alphIter->second - t.second * this->effective())) {
size_type beforePrevAlph = this->smallRecords.firstByAlphComp.select_iter(1+
this->smallRecords.firstByAlphabet.successor((predoutrank-1)*this->smallRecords.size(ind.second) + prefixLength*this->smallRecords.maxOutdegree)->first
)->second;
size_type prevAlphSize = this->smallRecords.firstByAlphComp.select_iter(1+
this->smallRecords.firstByAlphabet.successor((predoutrank) *this->smallRecords.size(ind.second) + prefixLength*this->smallRecords.maxOutdegree)->first
)->second;
size_type AlphSize = this->smallRecords.firstByAlphComp.select_iter(1+
this->smallRecords.firstByAlphabet.successor((predoutrank+1)*this->smallRecords.size(ind.second) + prefixLength*this->smallRecords.maxOutdegree)->first
)->second;
AlphSize -= prevAlphSize;
prevAlphSize -= beforePrevAlph;
beforePrevAlph -= prefixLength;
if (i < beforePrevAlph + AlphSize)
return revNode(alphIter->second - t.second*this->effective());
return this->toNode(alphIter->second - t.second*this->effective());
}
}
//check if after predoutrank is its reverse
auto afterAlphIter = alphIter;
++afterAlphIter;
if (afterAlphIter->second/this->effective() == t.second) {
if (this->toNode(afterAlphIter->second - t.second * this->effective()) == revNode(alphIter->second - t.second * this->effective())) {
size_type beforeAlph = this->smallRecords.firstByAlphComp.select_iter(1+
this->smallRecords.firstByAlphabet.successor((predoutrank) *this->smallRecords.size(ind.second) + prefixLength*this->smallRecords.maxOutdegree)->first
)->second;
size_type AlphSize = this->smallRecords.firstByAlphComp.select_iter(1+
this->smallRecords.firstByAlphabet.successor((predoutrank+1)*this->smallRecords.size(ind.second) + prefixLength*this->smallRecords.maxOutdegree)->first
)->second;
size_type AfterAlphSize = this->smallRecords.firstByAlphComp.select_iter(1+
this->smallRecords.firstByAlphabet.successor((predoutrank+2)*this->smallRecords.size(ind.second) + prefixLength*this->smallRecords.maxOutdegree)->first
)->second;
AfterAlphSize -= AlphSize;
AlphSize -= beforeAlph;
beforeAlph -= prefixLength;
if (i < beforeAlph + AfterAlphSize)
return revNode(afterAlphIter->second - t.second*this->effective());
return this->toNode(afterAlphIter->second - t.second*this->effective());
}
}
return revNode(alphIter->second - t.second*this->effective());
}
else {
const CompressedRecord & rev = this->largeRecords[ind.second];
size_type predoutrank = rev.firstByAlphabet.select_iter(
rev.firstByAlphComp.predecessor(i)->first+1
)->second/rev.size();
//check if before predoutrank is its reverse
auto alphIter = rev.alphabet.select_iter(predoutrank+1);
if (predoutrank > 0){
auto prevAlphIter = --alphIter;
++alphIter;
if (this->toNode(prevAlphIter->second) == revNode(alphIter->second)){
size_type beforePrevAlph = rev.firstByAlphComp.select_iter(1+
rev.firstByAlphabet.successor((predoutrank-1)*rev.size())->first
)->second;
size_type prevAlphSize = rev.firstByAlphComp.select_iter(1+
rev.firstByAlphabet.successor((predoutrank)*rev.size())->first
)->second;
size_type AlphSize = rev.firstByAlphComp.select_iter(1+
rev.firstByAlphabet.successor((predoutrank+1)*rev.size())->first
)->second;
AlphSize -= prevAlphSize;
prevAlphSize -= beforePrevAlph;
if (i < beforePrevAlph + AlphSize)
return revNode(alphIter->second);
return this->toNode(alphIter->second);
}
}
//check if after predoutrank is its reverse
if (predoutrank < rev.outgoing.size()-1){
auto afterAlphIter = ++alphIter;
--alphIter;
if (this->toNode(afterAlphIter->second) == revNode(alphIter->second)){
size_type beforeAlph = rev.firstByAlphComp.select_iter(1+
rev.firstByAlphabet.successor((predoutrank)*rev.size())->first
)->second;
size_type AlphSize = rev.firstByAlphComp.select_iter(1+
rev.firstByAlphabet.successor((predoutrank+1)*rev.size())->first
)->second;
size_type AfterAlphSize = rev.firstByAlphComp.select_iter(1+
rev.firstByAlphabet.successor((predoutrank+2)*rev.size())->first
)->second;
AfterAlphSize -= AlphSize;
AlphSize -= beforeAlph;
if (i < beforeAlph + AfterAlphSize)
return revNode(afterAlphIter->second);
return this->toNode(afterAlphIter->second);
}
}