-
Notifications
You must be signed in to change notification settings - Fork 1
/
BWT_iteration.hh
249 lines (181 loc) · 8.68 KB
/
BWT_iteration.hh
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
#ifndef BWT_ITERATION_HH
#define BWT_ITERATION_HH
#include "Interfaces.hh"
// Iterators that give all nodes that should be included in the topology.
class Rev_ST_Iterator : public Iterator{
public:
std::stack<Stack_frame> iteration_stack;
Stack_frame top;
BIBWT* index;
typename BIBWT::Interval_Data interval_data;
Rev_ST_Iterator() {}
Rev_ST_Iterator(BIBWT* index) : index(index) {}
virtual void set_index(BIBWT* index){
this->index = index;
}
virtual Iterator::Stack_frame get_top(){
return top;
}
virtual void init(){
// Make space
interval_data.symbols.resize(index->get_alphabet().size());
interval_data.ranks_start.resize(index->get_alphabet().size());
interval_data.ranks_end.resize(index->get_alphabet().size());
// Clear the stack
while(!iteration_stack.empty()) iteration_stack.pop();
// Push the empty string
iteration_stack.push(Stack_frame(Interval_pair(0,index->size()-1,0,index->size()-1),0,true)); // Suppose empty string is always a maxrep (special case)
}
virtual bool next(){
// 1) Take an interval off the top of a stack
// 2) If it is a leaf, return
// 3) Push all right-extensions that are left-maximal to the stack
// and all left-extensions that are leaves.
// WARNING: Depths are not accurate for leaves
if(iteration_stack.empty()) return false;
top = iteration_stack.top();
iteration_stack.pop();
if(top.intervals.reverse.size() == 1) return true; // Leaf
index->compute_rev_bwt_interval_data(top.intervals.reverse, interval_data);
// Iterate alphabet in reverse lexicographic order, so the smallest is pushed to the
// stack the last, so the iteration is done in lexicographic order
for(int64_t i = interval_data.n_distinct_symbols-1; i >= 0; i--){
Interval_pair I2 = index->right_extend(top.intervals,interval_data,i);
if(I2.forward.size() != 0 && index->is_left_maximal(I2)){
iteration_stack.push(Stack_frame(I2, top.depth+1, index->is_right_maximal(I2)));
}
}
index->compute_bwt_interval_data(top.intervals.forward, interval_data);
//std::sort(interval_data.symbols.begin(), interval_data.symbols.begin() + interval_data.n_distinct_symbols);
// Push all rev st children that are leaves
for(int64_t i = interval_data.n_distinct_symbols-1; i >= 0; i--){
Interval_pair I2 = index->left_extend(top.intervals,interval_data,i);
if(I2.reverse.size() == 1){
iteration_stack.push(Stack_frame(I2, -1, false)); // Don't know the depth so just put -1
}
}
return true;
}
};
class Depth_Bounded_SLT_Iterator : public Iterator{
public:
std::stack<Stack_frame> iteration_stack;
Stack_frame top;
typename BIBWT::Interval_Data interval_data;
BIBWT* index;
int64_t depth_bound;
string label; // debug
Depth_Bounded_SLT_Iterator(int64_t depth_bound) : depth_bound(depth_bound) {}
Depth_Bounded_SLT_Iterator(BIBWT* index, int64_t depth_bound) : index(index), depth_bound(depth_bound) {}
virtual void set_index(BIBWT* index){
this->index = index;
}
virtual Iterator::Stack_frame get_top(){
return top;
}
virtual void init(){
label = ""; // debug
// Make space
interval_data.symbols.resize(index->get_alphabet().size());
interval_data.ranks_start.resize(index->get_alphabet().size());
interval_data.ranks_end.resize(index->get_alphabet().size());
// Clear the stack
while(!iteration_stack.empty()) iteration_stack.pop();
// Push the empty string (suppose it is a maxrep (holds as long as the text is not an empty string?)
iteration_stack.push(Stack_frame(Interval_pair(0,index->size()-1,0,index->size()-1),0, true));
}
virtual bool next(){
// 1) Take an interval off the top of a stack
// 2) Push all right-maximal left extensions to stack
if(iteration_stack.empty()) return false;
top = iteration_stack.top();
iteration_stack.pop();
index->compute_bwt_interval_data(top.intervals.forward, interval_data);
//std::sort(interval_data.symbols.begin(), interval_data.symbols.begin() + interval_data.n_distinct_symbols);
if(top.depth <= depth_bound - 1){
// Iterate alphabet in reverse lexicographic order, so the smallest is pushed to the
// stack the last, so the iteration is done in lexicographic DFS order
for(int64_t i = interval_data.n_distinct_symbols-1; i >= 0; i--){
Interval_pair I2 = index->left_extend(top.intervals,interval_data,i);
if(I2.forward.size() != 0 && index->is_right_maximal(I2)){
iteration_stack.push(Stack_frame(I2, top.depth+1, index->is_left_maximal(I2)));
}
}
}
return true;
}
};
class SLT_Iterator : public Depth_Bounded_SLT_Iterator{
public:
SLT_Iterator() : Depth_Bounded_SLT_Iterator(1e18) {}
SLT_Iterator(BIBWT* index) : Depth_Bounded_SLT_Iterator(index, 1e18) {}
};
class Rev_ST_Depth_Bounded_Maxrep_Iterator : public Iterator{
private:
std::vector<Stack_frame> iteration_stack;
public:
Stack_frame top;
BIBWT* index;
typename BIBWT::Interval_Data interval_data;
int64_t depth_bound;
Rev_ST_Depth_Bounded_Maxrep_Iterator(int64_t depth_bound) : depth_bound(depth_bound) {}
Rev_ST_Depth_Bounded_Maxrep_Iterator(BIBWT* index, int64_t depth_bound) : index(index), depth_bound(depth_bound) {}
virtual void set_index(BIBWT* index){
this->index = index;
}
virtual Iterator::Stack_frame get_top(){
return top;
}
virtual void init(){
// Make space
interval_data.symbols.resize(index->get_alphabet().size());
interval_data.ranks_start.resize(index->get_alphabet().size());
interval_data.ranks_end.resize(index->get_alphabet().size());
// Clear the stack
while(!iteration_stack.empty()) iteration_stack.pop_back();
// Push the empty string (suppose it is a maxrep)
iteration_stack.push_back(Stack_frame(Interval_pair(0,index->size()-1,0,index->size()-1),0, true));
}
bool next(){
start:
if(iteration_stack.empty()) return false;
top = iteration_stack.back();
iteration_stack.pop_back();
// top has depth at most equal to the depth bound
index->compute_bwt_interval_data(top.intervals.forward, interval_data);
bool leftmax = interval_data.n_distinct_symbols >= 2;
bool rightmax = index->is_right_maximal(top.intervals);
top.is_maxrep = leftmax && rightmax;
if(!rightmax){
// Just have gone outside of the maxrep tree
return true;
}
if(rightmax && !leftmax && top.depth == depth_bound){
// The left-saturation is maxrep, but will not reach it because of
// the depth bound -> return a node inside the edge
return true;
}
if(top.depth <= depth_bound - 1){
// Iterate alphabet in reverse lexicographic order, so the smallest is pushed to the
// stack the last, so the iteration is done in lexicographic DFS order
for(int64_t i = interval_data.n_distinct_symbols-1; i >= 0; i--){
//char c = interval_data.symbols[i];
Interval_pair I2 = index->left_extend(top.intervals, interval_data, i);
if(I2.forward.size() != 0){
iteration_stack.push_back(Stack_frame(I2, top.depth+1, false)); // is_maxrep will be computed when the frame is popped
}
}
}
if(leftmax) return true; // Is a maxrep within the depth bound
else goto start; // Could use recursion but goto is faster and does not increase the size of the stack
}
};
class Rev_ST_Maxrep_Iterator : public Rev_ST_Depth_Bounded_Maxrep_Iterator{
public:
Stack_frame top;
BIBWT* index;
typename BIBWT::Interval_Data interval_data;
Rev_ST_Maxrep_Iterator() : Rev_ST_Depth_Bounded_Maxrep_Iterator(1e18) {}
Rev_ST_Maxrep_Iterator(BIBWT* index) : Rev_ST_Depth_Bounded_Maxrep_Iterator(index, 1e18) {}
};
#endif