Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ReversedQuickSearch.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/set>
9#include <alib/map>
11
16
23
24namespace arbology {
25
26namespace exact {
27
34public:
39 template < class SymbolType >
41 template < class SymbolType >
43 template < class SymbolType >
45 template < class SymbolType >
47 template < class SymbolType >
49 template < class SymbolType >
51
52};
53
54template < class SymbolType >
56 return match ( subject, tree::PrefixRankedBarPattern < SymbolType > ( pattern ) );
57}
58
59template < class SymbolType >
62 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedQuickSearchBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
63 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
64
65 if ( subject.getContent ( ).size ( ) < pattern.getContent ( ).size ( ) )
66 return occ;
67
68 // index to the subject
69 size_t i = subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( );
70
71 // main loop of the algorithm over all possible indexes where the pattern can start
72 while ( true ) {
73 // index to the pattern
74 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, pattern, i );
75
76 // match was found
77 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
78
79 // we have to break if the pattern is already aligned at the leftmost position
80 // otherwise we would ask for symbol at the position -1 in the shift heuristics
81 if ( i == 0 ) {
82 break;
83 }
84
85 // shift heuristics
86 size_t shift = bcs [ subject.getContent ( ) [ i - 1 ] ];
87
88 if ( shift > i )
89 break;
90
91 i -= shift;
92 }
93
94 return occ;
95}
96
97template < class SymbolType >
100 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedQuickSearchBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
101 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
102 ext::map < common::ranked_symbol < SymbolType >, unsigned > variablesSetting;
103
104 tree::PrefixRankedBarTree < unsigned > repeats = tree::properties::ExactSubtreeRepeatsNaive::repeats ( subject );
105
106 if ( subject.getContent ( ).size ( ) < pattern.getContent ( ).size ( ) )
107 return occ;
108
109 // index to the subject
110 size_t i = subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( );
111
112 // main loop of the algorithm over all possible indexes where the pattern can start
113 while ( true ) {
114 // index to the pattern
115 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, repeats, pattern, i );
116
117 // match was found
118 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
119
120 // we have to break if the pattern is already aligned at the leftmost position
121 // otherwise we would ask for symbol at the position -1 in the shift heuristics
122 if ( i == 0 ) {
123 break;
124 }
125
126 // shift heuristics
127 size_t shift = bcs [ subject.getContent ( ) [ i - 1 ] ];
128
129 if ( shift > i )
130 break;
131
132 i -= shift;
133 }
134
135 return occ;
136}
137
138template < class SymbolType >
140 return match ( subject, tree::PrefixRankedPattern < SymbolType > ( pattern ) );
141}
142
143template < class SymbolType >
146 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedQuickSearchBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
147 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
148
149 if ( subject.getContent ( ).size ( ) < pattern.getContent ( ).size ( ) )
150 return occ;
151
152 // index to the subject
153 size_t i = subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( );
154
155 // main loop of the algorithm over all possible indexes where the pattern can start
156 while ( true ) {
157 // index to the pattern
158 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, pattern, i );
159
160 // match was found
161 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
162
163 // we have to break if the pattern is already aligned at the leftmost position
164 // otherwise we would ask for symbol at the position -1 in the shift heuristics
165 if ( i == 0 ) {
166 break;
167 }
168
169 // shift heuristics
170 size_t shift = bcs [ subject.getContent ( ) [ i - 1 ] ];
171
172 if ( shift > i )
173 break;
174
175 i -= shift;
176 }
177
178 return occ;
179}
180
181template < class SymbolType >
184 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedQuickSearchBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
185 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
186 tree::PrefixRankedTree < unsigned > repeats = tree::properties::ExactSubtreeRepeatsNaive::repeats ( subject );
187
188 if ( subject.getContent ( ).size ( ) < pattern.getContent ( ).size ( ) )
189 return occ;
190
191 // index to the subject
192 size_t i = subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( );
193
194 // main loop of the algorithm over all possible indexes where the pattern can start
195 while ( true ) {
196 // index to the pattern
197 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, repeats, pattern, i );
198
199 // match was found
200 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
201
202 // we have to break if the pattern is already aligned at the leftmost position
203 // otherwise we would ask for symbol at the position -1 in the shift heuristics
204 if ( i == 0 ) {
205 break;
206 }
207
208 // shift heuristics
209 size_t shift = bcs [ subject.getContent ( ) [ i - 1 ] ];
210
211 if ( shift > i )
212 break;
213
214 i -= shift;
215 }
216
217 return occ;
218}
219
220} /* namespace exact */
221
222} /* namespace arbology */
223
Definition: ReversedQuickSearch.h:33
static ext::set< unsigned > match(const tree::PrefixRankedBarTree< SymbolType > &subject, const tree::PrefixRankedBarTree< SymbolType > &pattern)
Definition: ReversedQuickSearch.h:55
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Nonlinear tree pattern represented as linear sequece as result of preorder traversal with additional ...
Definition: PrefixRankedBarNonlinearPattern.h:91
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarNonlinearPattern.h:434
Tree pattern represented as linear sequece as result of preorder traversal with additional bar symbol...
Definition: PrefixRankedBarPattern.h:85
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarPattern.h:334
Tree structure represented as linear sequece as result of preorder traversal with additional bar symb...
Definition: PrefixRankedBarTree.h:78
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarTree.h:273
Nonlinear tree pattern represented as linear sequece as result of preorder traversal....
Definition: PrefixRankedNonlinearPattern.h:82
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedNonlinearPattern.h:333
Tree pattern represented as linear sequece as result of preorder traversal. The representation is so ...
Definition: PrefixRankedPattern.h:77
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedPattern.h:262
Tree structure represented as linear sequece as result of preorder traversal. The representation is s...
Definition: PrefixRankedTree.h:71
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedTree.h:235
static size_t occurrence(const PrefixRankedBarTree< SymbolType > &subject, const ext::vector< int > &subjectSubtreeJumpTable, const PrefixRankedBarTree< SymbolType > &pattern, size_t subjectPosition, size_t patternStartIndex=0)
Definition: ForwardOccurrenceTest.h:42
static ext::map< common::ranked_symbol< SymbolType >, size_t > bcs(const tree::PrefixRankedBarPattern< SymbolType > &pattern)
Definition: ReversedQuickSearchBadCharacterShiftTable.h:39
static ext::vector< int > compute(const tree::PrefixRankedBarTree< SymbolType > &subject)
Definition: SubtreeJumpTable.h:49
Definition: BoyerMooreHorspool.h:22
int i
Definition: AllEpsilonClosure.h:118