-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelfGrammarIndex.h
More file actions
executable file
·1260 lines (914 loc) · 39.5 KB
/
SelfGrammarIndex.h
File metadata and controls
executable file
·1260 lines (914 loc) · 39.5 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
//
// Created by inspironXV on 8/16/2018.
//
#ifndef IMPROVED_GRAMMAR_INDEX_SELFGRAMMARINDEX_H
#define IMPROVED_GRAMMAR_INDEX_SELFGRAMMARINDEX_H
#include <string>
#include <stack>
#include <sdsl/bit_vectors.hpp>
#include "compressed_grammar.h"
#include "binary_relation.h"
#include "trees/patricia_tree/compact_patricia_tree.h"
#ifdef MEM_MONITOR
#include <ctime>
#include "utils/memory/mem_monitor/mem_monitor.hpp"
#include "utils/CLogger.h"
using timer = std::chrono::high_resolution_clock;
using namespace std::chrono;
#endif
struct range{
uint x1,x2,y1,y2,len;
range(){};
};
class SelfGrammarIndex {
public:
typedef compressed_grammar grammar_representation;
typedef binary_relation range_search2d;
unsigned int code;
protected:
grammar_representation _g;
range_search2d grid;
public:
SelfGrammarIndex() {};
virtual ~SelfGrammarIndex() {};
//virtual void build(const grammar_representation&, const range_search2d& ) = 0;
virtual void find_ranges_trie(std::string &, std::vector<uint> &)=0;
virtual void find_ranges(std::string &, std::vector<uint>& )=0;
virtual void find_ranges_trie(std::string &, std::vector<uint>&, std::vector<range> & ) = 0;
virtual void find_ranges_dfs(std::string &, std::vector<uint>& ) = 0;
virtual void locate(std::string &, sdsl::bit_vector &) = 0;
virtual void locate(std::string &, std::vector<uint> &) = 0;
// virtual const m_patricia::compact_patricia_tree &get_pt_rules() const = 0;
// virtual const m_patricia::compact_patricia_tree &get_pt_suffixes() const = 0;
virtual void locateNoTrie(std::string &, std::vector<uint> &) = 0;
void build_bitvector_occ(sdsl::bit_vector& B) const;
virtual void set_code(const unsigned int &c) { code = c; }
virtual void build(const std::string &
#ifdef MEM_MONITOR
, mem_monitor& mm
#endif
);
virtual void build_basics(const std::string &, fstream &, fstream &, fstream &
#ifdef MEM_MONITOR
,mem_monitor &mm
#endif
);
virtual void build_basics(const std::string &, fstream &, fstream &, fstream &,
grammar ¬_compressed_grammar,
std::vector< std::pair< std::pair<size_t ,size_t >,std::pair<size_t ,size_t > > >& grammar_sfx
#ifdef MEM_MONITOR
, mem_monitor& mm
#endif
);
virtual void build_basics(const std::string &,grammar ¬_compressed_grammar,
std::vector< std::pair< std::pair<size_t ,size_t >,std::pair<size_t ,size_t > > >& grammar_sfx
#ifdef MEM_MONITOR
, mem_monitor&
#endif
);
virtual void build_basics_bal(const std::string &, fstream &, fstream &,fstream &, fstream &, fstream &
#ifdef MEM_MONITOR
,mem_monitor&
#endif
);
virtual void load_basics(fstream &);
virtual void save(std::fstream &);
virtual void load(std::fstream &);
virtual grammar_representation &get_grammar() { return _g; }
virtual range_search2d &get_grid() { return grid; }
virtual void display(const std::size_t &, const std::size_t &, std::string &);
virtual void display_trie(const std::size_t &, const std::size_t &, std::string &);
virtual void display_L_trie(const std::size_t &i, const std::size_t &j, std::string &str) {
str.resize(j - i + 1);
size_t p = 0;
expand_interval_trie(_g.m_tree.root(), make_pair(i, j), str, p);
}
virtual void display_L(const std::size_t &i, const std::size_t &j, std::string &str) {
str.resize(j - i + 1);
size_t p = 0;
expand_interval(_g.m_tree.root(), make_pair(i, j), str, p);
}
virtual void display_L_rec(const std::size_t &i, const std::size_t &j, std::string &str) {
str.resize(j - i + 1);
size_t p = 0;
expand_interval_rec(_g.m_tree.root(), make_pair(i, j), str, p);
}
virtual void display_dfs(const std::size_t &i, const std::size_t &j, std::string &str) const{
str.resize(j - i + 1);
size_t p = 0;
display_dfs_aux(_g.m_tree.root(), make_pair(i, j), str, p);
}
virtual size_t size_in_bytes() const;
virtual unsigned long size_basics_in_bytes() const;
virtual unsigned long size_tries_in_bytes() const;
// protected:
virtual void locate_ch(const char &, sdsl::bit_vector &) const;
virtual void locate_ch(const char &, std::vector<uint> &) const;
bool expand_prefix_slp(const grammar_representation::g_long &, std::string &, const size_t &, size_t &pos) const;
bool expand_suffix_slp(const grammar_representation::g_long &, std::string &, const size_t &, size_t &pos) const;
virtual bool bp_expand_prefix(const grammar_representation::g_long &, std::string &, const size_t &, size_t &pos) const;
bool virtual bp_expand_suffix(const grammar_representation::g_long &, std::string &, const size_t &, size_t &pos) const;
bool expand_prefix2(const size_t &, std::string &, const size_t &, size_t &pos) const;
virtual bool expand_prefix(const grammar_representation::g_long &, std::string &, const size_t &, size_t &pos) const;
bool expand_suffix(const grammar_representation::g_long &, std::string &, const size_t &, size_t &) const;
void expand_grammar_sfx(const size_t &, std::string &, const size_t &) const;
void track_occ(uint &, sdsl::bit_vector& , const uint& )const;
void find_second_occ(uint r1,uint r2,uint c1,uint c2, long len, std::vector<uint> &occ){
const auto& g_tree = _g.get_parser_tree();
std::vector< std::pair<size_t,size_t> > pairs;
grid.range2(r1,r2,c1,c2,pairs);
for (auto &pair : pairs) {
size_t p = grid.first_label_col(pair.second);
size_t pos_p = _g.offsetText(g_tree[p]);
unsigned int parent = g_tree.parent(g_tree[p]);
long l = long (- len + pos_p) - _g.offsetText(parent);
find_second_occ(l,parent,occ);
}
}
void find_second_occ(long int &, unsigned int &, sdsl::bit_vector &) const;
void find_second_occ_rec(long int, unsigned int &, std::vector<uint> &) const;
void find_second_occ_rec_aux(long int, unsigned int &, std::vector<uint> &) const;
void find_second_occ(long int &, unsigned int &, std::vector<uint> &) const;
template<typename K>
bool lower_bound(bool &found , grammar_representation::g_long &lr, grammar_representation::g_long &hr, const K &f) const {
found = false;
while(lr < hr){
uint64_t mid = (lr+hr)/2;
int c = f(mid);
if(c < 0){
/*the rule is greater than the pattern */
hr = mid -1;
}else{
if(c > 0)
lr = mid+1;
else{
hr = mid;
found = true;
}
}
}
if(!found && lr == hr && f(lr) == 0 ) {
found = true;
return true;
}
return false;
}
template<typename K>
bool upper_bound(bool &found, grammar_representation::g_long &lr, grammar_representation::g_long &hr, const K &f) const {
found = false;
while(lr < hr){
uint64_t mid = ceil((lr+hr)/2.0);
int c = f(mid);
if(c < 0){
/*the rule is greater than the pattern */
hr = mid -1;
}else{
if(c > 0)
lr = mid+1;
else{
lr = mid;
found = true;
}
}
}
if(!found && lr == hr && f(lr) == 0 ) {
found = true;
return true;
}
return false;
}
template<typename K>
bool lower_bound(grammar_representation::g_long &lr, grammar_representation::g_long &hr, const K &f) const {
if (lr >= hr || lr + 1 == hr) {
if (f(lr) == 0) return true;
if (f(hr) == 0) {
lr = hr;
return true;
}
return false;
}
grammar_representation::g_long m = (lr + hr) / 2;
auto res = f(m);
if (res < 0) {
/*
* If value < value(node) f must return
* */
hr = m - 1;
} else {
if (res > 0) {
/*
* If value > value(node) f must return true
* */
(lr = m);
} else {
/*
* If value == value(node) f must return true
* */
hr = m;
}
}
return lower_bound(lr, hr, f);
}
template<typename K>
bool upper_bound(grammar_representation::g_long &lr, grammar_representation::g_long &hr, const K &f) const {
if (lr >= hr || lr + 1 == hr) {
if (f(hr) == 0) return true;
if (f(lr) == 0) {
hr = lr;
return true;
}
return false;
}
grammar_representation::g_long m = (lr + hr) / 2;
auto res = f(m);
if (res < 0) {
/*
* If value < value(node) f must return true
* */
hr = m;
} else {
if (res > 0) {
/*
* If value > value(node) f must return true
* */
(lr = m + 1);
} else {
/*
* If value == value(node) f must return true
* */
lr = m;
}
}
return upper_bound(lr, hr, f);
}
virtual int cmp_prefix(const grammar_representation::g_long &, std::string::iterator &, std::string::iterator &) const;
virtual int cmp_suffix(const grammar_representation::g_long &, std::string::iterator &, std::string::iterator &) const;
int cmp_suffix_grammar(const size_t &, std::string::iterator &, std::string::iterator &);
int bp_cmp_prefix(const grammar_representation::g_long &, std::string::iterator &, std::string::iterator &) const;
int bp_cmp_suffix(const grammar_representation::g_long &, std::string::iterator &, std::string::iterator &) const;
int bp_cmp_suffix_grammar(const size_t &, std::string::iterator &, std::string::iterator &) const;
virtual void load_rules_pt(fstream &f) = 0;
virtual void load_sfx_pt(fstream &f) = 0;
void expand_interval_trie(const size_t &node, const std::pair<size_t, size_t> &range, std::string &s, std::size_t &pos)
{
/*precondition range always belongs to [node.leftmost-leaf, node.rigthmost-leaf]*/
/*base case*/
/*recursion case*/
auto llb = _g.rank_L(range.first) + _g.L[range.first]; //left leaf begin
auto pllb = _g.select_L(llb); // position of left leaf begin
auto lle = _g.rank_L(range.second) + _g.L[range.second]; //left leaf end
auto plle = _g.select_L(lle); // position of left leaf end
/* If the range only expands a unique leaf */
int added_ch = 0;
if (llb == lle) {
/* Need to jump to llb definition */
/* compute label X of llb */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(llb))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
}
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
auto l_range = std::make_pair(p + range.first - pllb, p + range.second - plle);
expand_interval(node_def, l_range, s, pos);
return;
}
if (added_ch > range.second - range.first + 1)
return;
/* If the range expands more than one leaf */
/* Divide the expansion in three range left, middle and right*/
/* left range*/
{
/* Need to jump to llb definition */
/* compute label X of llb */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(llb))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
} else {
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
///auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
///auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
size_t pnllb = _g.select_L(llb + 1); // position of left + 1 leaf begin
////auto l_range = std::make_pair(p + range.first - pllb, p + (pnllb - pllb) - 1);
///expand_interval(node_def, l_range, s, pos);
size_t off = pnllb - range.first;
size_t p_f = pos;
bp_expand_suffix(X,s,off,pos);
std::reverse(s.begin()+p_f,s.begin()+p_f+off);
}
}
if (added_ch > range.second - range.first + 1)
return;
/* middle range*/
if (llb != lle - 1) {
/*expand all the phrases in the middle*/
auto t = llb + 1;
while (t < lle) {
auto off = _g.select_L(t + 1) - _g.select_L(t) + pos;
//expand_interval(_g.m_tree.leafselect(t), make_pair(_g.select_L(t),_g.select_L(t + 1)-1),s,pos);
bp_expand_prefix(_g[_g.m_tree.pre_order(_g.m_tree.leafselect(t))], s, off, pos);
++t;
}
}
if (added_ch > range.second - range.first + 1)
return;
/* right range*/
{
/* Need to jump to llb definition */
/* compute label X of lle */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(lle))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
} else
{
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
///auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
auto off = range.second - plle + 1;
/// auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
/// auto l_range = std::make_pair(p, p + range.second - plle);
bp_expand_prefix(X,s,pos+off,pos);
///expand_interval(node_def, l_range, s, pos);
}
}
}
void expand_interval(const size_t &node, const std::pair<size_t, size_t> &range, std::string &s, std::size_t &pos)
{
/*precondition range always belongs to [node.leftmost-leaf, node.rigthmost-leaf]*/
/*base case*/
/*recursion case*/
auto llb = _g.rank_L(range.first) + _g.L[range.first]; //left leaf begin
auto pllb = _g.select_L(llb); // position of left leaf begin
auto lle = _g.rank_L(range.second) + _g.L[range.second]; //left leaf end
auto plle = _g.select_L(lle); // position of left leaf end
/* If the range only expands a unique leaf */
int added_ch = 0;
if (llb == lle) {
/* Need to jump to llb definition */
/* compute label X of llb */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(llb))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
}
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
auto l_range = std::make_pair(p + range.first - pllb, p + range.second - plle);
expand_interval(node_def, l_range, s, pos);
return;
}
if (added_ch > range.second - range.first + 1)
return;
/* If the range expands more than one leaf */
/* Divide the expansion in three range left, middle and right*/
/* left range*/
{
/* Need to jump to llb definition */
/* compute label X of llb */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(llb))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
} else {
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
///auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
///auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
size_t pnllb = _g.select_L(llb + 1); // position of left + 1 leaf begin
////auto l_range = std::make_pair(p + range.first - pllb, p + (pnllb - pllb) - 1);
///expand_interval(node_def, l_range, s, pos);
size_t off = pnllb - range.first;
size_t p_f = pos;
expand_suffix(X,s,off,pos);
std::reverse(s.begin()+p_f,s.begin()+p_f+off);
}
}
if (added_ch > range.second - range.first + 1)
return;
/* middle range*/
if (llb != lle - 1) {
/*expand all the phrases in the middle*/
auto t = llb + 1;
while (t < lle) {
auto off = _g.select_L(t + 1) - _g.select_L(t) + pos;
//expand_interval(_g.m_tree.leafselect(t), make_pair(_g.select_L(t),_g.select_L(t + 1)-1),s,pos);
expand_prefix(_g[_g.m_tree.pre_order(_g.m_tree.leafselect(t))], s, off, pos);
++t;
}
}
if (added_ch > range.second - range.first + 1)
return;
/* right range*/
{
/* Need to jump to llb definition */
/* compute label X of lle */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(lle))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
} else
{
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
///auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
auto off = range.second - plle + 1;
/// auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
/// auto l_range = std::make_pair(p, p + range.second - plle);
expand_prefix(X,s,pos+off,pos);
///expand_interval(node_def, l_range, s, pos);
}
}
}
void expand_interval_rec(const size_t &node, const std::pair<size_t, size_t> &range, std::string &s, std::size_t &pos)
{
/*precondition range always belongs to [node.leftmost-leaf, node.rigthmost-leaf]*/
/*base case*/
/*recursion case*/
auto llb = _g.rank_L(range.first) + _g.L[range.first]; //left leaf begin
auto pllb = _g.select_L(llb); // position of left leaf begin
auto lle = _g.rank_L(range.second) + _g.L[range.second]; //left leaf end
auto plle = _g.select_L(lle); // position of left leaf end
/* If the range only expands a unique leaf */
int added_ch = 0;
if (llb == lle) {
/* Need to jump to llb definition */
/* compute label X of llb */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(llb))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
}
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
auto l_range = std::make_pair(p + range.first - pllb, p + range.second - plle);
expand_interval(node_def, l_range, s, pos);
return;
}
if (added_ch > range.second - range.first + 1)
return;
/* If the range expands more than one leaf */
/* Divide the expansion in three range left, middle and right*/
/* left range*/
{
/* Need to jump to llb definition */
/* compute label X of llb */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(llb))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
} else {
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
size_t pnllb = _g.select_L(llb + 1); // position of left + 1 leaf begin
auto l_range = std::make_pair(p + range.first - pllb, p + (pnllb - pllb) - 1);
expand_interval_rec(node_def, l_range, s, pos);
///size_t off = pnllb - range.first;
///size_t p_f = pos;
///expand_suffix(X,s,off,pos);
///std::reverse(s.begin()+p_f,s.begin()+p_f+off);
}
}
if (added_ch > range.second - range.first + 1)
return;
/* middle range*/
if (llb != lle - 1) {
/*expand all the phrases in the middle*/
auto t = llb + 1;
while (t < lle) {
// auto off = _g.select_L(t + 1) - _g.select_L(t) + pos;
expand_interval_rec(_g.m_tree.leafselect(t), make_pair(_g.select_L(t),_g.select_L(t + 1)-1),s,pos);
///expand_prefix(_g[_g.m_tree.pre_order(_g.m_tree.leafselect(t))], s, off, pos);
++t;
}
}
if (added_ch > range.second - range.first + 1)
return;
/* right range*/
{
/* Need to jump to llb definition */
/* compute label X of lle */
auto X = _g[_g.m_tree.pre_order(_g.m_tree.leafselect(lle))];
/* if X is a terminal symbol append to the string*/
if (_g.isTerminal(X)) {
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
++added_ch;
if (added_ch >= range.second - range.first + 1)
return;
} else
{
/* if X is not a terminal symbol */
/* compute first occurrence of X in the parser tree*/
auto node_def = _g.m_tree[_g.select_occ(X, 1)];
/* compute new range */
///auto off = range.second - plle + 1;
auto p = _g.select_L(_g.m_tree.leafrank(node_def)); // pos of leftmost leaf of node_def
auto l_range = std::make_pair(p, p + range.second - plle);
///expand_prefix(X,s,pos+off,pos);
expand_interval_rec(node_def, l_range, s, pos);
}
}
}
int cmp_prefix_L(const grammar_representation::g_long & X, std::string::iterator & itera, std::string::iterator & end) const{
if(_g.isTerminal(X))
{
unsigned char a_th = _g.terminal_simbol(X); // a_th symbol in the sorted alphabet
if(a_th < (unsigned char)(*itera)) return 1;
if(a_th > (unsigned char)(*itera)) return -1;
++itera;
//if(itera == end) return 0;
return 0;
}
size_t pnode = _g.select_occ(X,1);
size_t node = _g.m_tree[pnode];
size_t leaf = _g.m_tree.leafrank(node);
size_t l_leaf = _g.m_tree.lastleaf(node);
size_t l_node = _g.m_tree.leafselect(leaf);
size_t X_i = _g[_g.m_tree.pre_order(l_node)];
int r = cmp_prefix_L(X_i,itera,end);
while (r == 0 && itera != end && ++leaf <= l_leaf){
l_node = _g.m_tree.leafselect(leaf);
X_i = _g[_g.m_tree.pre_order(l_node)];
r = cmp_prefix_L(X_i,itera,end);
}
return r;
}
int cmp_suffix_L(const grammar_representation::g_long & X, std::string::iterator & itera, std::string::iterator & end) const{
if(_g.isTerminal(X))
{
unsigned char a_th = _g.terminal_simbol(X); // a_th symbol in the sorted alphabet
if(a_th < (unsigned char)(*itera)) return 1;
if(a_th > (unsigned char)(*itera)) return -1;
--itera;
///if(itera == end) return 0;
return 0;
}
size_t pnode = _g.select_occ(X,1);
size_t node = _g.m_tree[pnode];
size_t leaf = _g.m_tree.leafrank(node);
size_t l_leaf = _g.m_tree.lastleaf(node);
size_t l_node = _g.m_tree.leafselect(l_leaf);
size_t X_i = _g[_g.m_tree.pre_order(l_node)];
int r = cmp_suffix_L(X_i,itera,end);
while (r == 0 && itera != end && leaf <= --l_leaf ){
size_t _l_node = _g.m_tree.leafselect(l_leaf);
size_t Y_i = _g[_g.m_tree.pre_order(_l_node)];
r = cmp_suffix_L(Y_i,itera,end);
}
return r;
}
virtual void dfs_expand_prefix (const grammar_representation::g_long & X, std::string & s, const size_t & l, size_t &pos) const{
if(_g.isTerminal(X) ){
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;
++pos;
return;
}
uint rule_node = _g.m_tree[_g.select_occ(X,1)];
uint nch = _g.m_tree.children(rule_node);
for (int j = 1; j <= nch ; ++j)
{ uint child = _g.m_tree.child(rule_node,j);
uint V = _g[_g.m_tree.pre_order(child)];
dfs_expand_prefix(V,s,l,pos);
if(l == pos ) return ;
}
// if(_g.isTerminal(X) && pos < l){
//
// unsigned char a_th = _g.terminal_simbol(X);
// s[pos] = a_th;
// ++pos;
// return;
// }
//
// std::stack<uint> stack1;
//
// uint rule_node = _g.m_tree[_g.select_occ(X,1)];
// stack1.push(rule_node);
//
// while(!stack1.empty()){
//
// uint cnode = stack1.top();
// stack1.pop();
//
// if(_g.m_tree.isleaf(cnode))
// {
// uint V = _g[_g.m_tree.pre_order(cnode)];
//
// if(_g.isTerminal(V) && pos < l)
// {
// unsigned char a_th = _g.terminal_simbol(V);
// s[pos] = a_th;
// ++pos;
// if(pos == l)
// return;
// }
// else
// {
// dfs_expand_prefix(V,s,l,pos);
// if(pos == l)
// return;
// }
// }
// else
// {
// uint nch = _g.m_tree.children(cnode);
// for (int i = nch; i > 0 ; --i) {
// stack1.push(_g.m_tree.child(cnode,i));
// }
//
// }
// }
}
virtual void dfs_expand_suffix (const grammar_representation::g_long & X, std::string & s, const size_t & l, size_t &pos) const{
//
// if(_g.isTerminal(X) && pos < l){
//
// unsigned char a_th = _g.terminal_simbol(X);
// s[pos] = a_th;
// ++pos;
// return;
// }
//
// std::stack<uint> stack1;
//
// uint rule_node = _g.m_tree[_g.select_occ(X,1)];
// stack1.push(rule_node);
//
// while(!stack1.empty()){
//
// uint cnode = stack1.top();
// stack1.pop();
//
// if(_g.m_tree.isleaf(cnode))
// {
// uint V = _g[_g.m_tree.pre_order(cnode)];
//
// if(_g.isTerminal(V) && pos < l)
// {
// unsigned char a_th = _g.terminal_simbol(V);
// s[pos] = a_th;
// ++pos;
// if(pos == l)
// return;
// }
// else
// {
// dfs_expand_suffix(V,s,l,pos);
// if(pos == l)
// return;
// }
// }
// else
// {
// uint nch = _g.m_tree.children(cnode);
// uint child = _g.m_tree.fchild(cnode);
// for (int i = 0; i < nch ; ++i) {
// stack1.push(child);
// child = _g.m_tree.nsibling(child);
// }
//
// }
// }
if(_g.isTerminal(X) ){
unsigned char a_th = _g.terminal_simbol(X);
s[pos] = a_th;