Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
KnuthMorrisPratt.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/measure>
9
10#include <alib/set>
11#include <alib/vector>
12
18
26
27namespace arbology {
28
29namespace exact {
30
36public:
41 template < class SymbolType >
43 template < class SymbolType >
45 template < class SymbolType >
47
48 template < class SymbolType >
50 template < class SymbolType >
52 template < class SymbolType >
54 template < class SymbolType >
56
57};
58
59template < class SymbolType >
61 return match ( subject, tree::PrefixRankedBarPattern < SymbolType > ( pattern ) );
62}
63
64template < class SymbolType >
68
69 //measurements::start("Algorithm", measurements::Type::MAIN);
70
71 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
72
73 unsigned firstUnknownPos = pattern.getContent ( ).size ( );
74 for ( unsigned i = 0; i < pattern.getContent ( ).size ( ); ++ i ) {
75 if ( pattern.getContent ( ) [ i ] == pattern.getSubtreeWildcard ( ) ) {
76 firstUnknownPos = i;
77 break;
78 }
79 }
80
81 // index to the subject
82 unsigned i = 0;
83 // index to the pattern
84 unsigned j = 0;
85
86 // main loop of the algorithm over all possible indexes where the pattern can start
87 while ( i + pattern.getContent ( ).size ( ) <= subject.getContent ( ).size ( ) ) {
88 j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, pattern, i, j );
89
90 // match was found
91 if ( j >= pattern.getContent ( ).size ( ) ) occ.insert ( i );
92
93 if ( j == 0 ) {
94 i += 1;
95 } else {
96 unsigned shift = j - borderArray [ j ];
97 // shift heuristics
98 i += shift;
99 j = std::min ( firstUnknownPos, j ) - shift;
100 }
101 }
102
103 //measurements::end();
104
105 return occ;
106}
107
108template < class SymbolType >
112
113 //measurements::start("Algorithm", measurements::Type::MAIN);
114
115 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
116 tree::PrefixRankedBarTree < unsigned > repeats = tree::properties::ExactSubtreeRepeatsNaive::repeats ( subject );
117
118 unsigned firstUnknownPos = pattern.getContent ( ).size ( );
119 for ( unsigned i = 0; i < pattern.getContent ( ).size ( ); ++ i ) {
120 if ( pattern.getContent ( ) [ i ] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).contains ( pattern.getContent ( ) [ i ] ) ) {
121 firstUnknownPos = i;
122 break;
123 }
124 }
125
126 // index to the subject
127 unsigned i = 0;
128 // index to the pattern
129 unsigned j = 0;
130
131 // main loop of the algorithm over all possible indexes where the pattern can start
132 while ( i + pattern.getContent ( ).size ( ) <= subject.getContent ( ).size ( ) ) {
133 j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, repeats, pattern, i, j );
134
135 // match was found
136 if ( j >= pattern.getContent ( ).size ( ) ) occ.insert ( i );
137
138 if ( j == 0 ) {
139 i += 1;
140 } else {
141 unsigned shift = j - borderArray [ j ];
142 // shift heuristics
143 i += shift;
144 j = std::min ( firstUnknownPos, j ) - shift;
145 }
146 }
147
148 //measurements::end();
149
150 return occ;
151}
152
153template < class SymbolType >
155 return match ( subject, tree::PrefixRankedPattern < SymbolType > ( pattern ) );
156}
157
158template < class SymbolType >
160 return match ( subject, tree::PrefixRankedExtendedPattern < SymbolType > ( pattern ) );
161}
162
163template < class SymbolType >
167
168 //measurements::start("Algorithm", measurements::Type::MAIN);
169
170 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
171
172 unsigned firstUnknownPos = pattern.getContent ( ).size ( );
173 for ( unsigned i = 0; i < pattern.getContent ( ).size ( ); ++ i ) {
174 if ( pattern.getNodeWildcards ( ).contains ( pattern.getContent ( ) [ i ] ) || pattern.getContent ( ) [ i ] == pattern.getSubtreeWildcard ( ) ) {
175 firstUnknownPos = i;
176 break;
177 }
178 }
179
180 // index to the subject
181 unsigned i = 0;
182 // index to the pattern
183 unsigned j = 0;
184
185 // main loop of the algorithm over all possible indexes where the pattern can start
186 while ( i + pattern.getContent ( ).size ( ) <= subject.getContent ( ).size ( ) ) {
187 j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, pattern, i, j );
188
189 // match was found
190 if ( j >= pattern.getContent ( ).size ( ) ) occ.insert ( i );
191
192 if ( j == 0 ) {
193 i += 1;
194 } else {
195 unsigned shift = j - borderArray [ j ];
196 // shift heuristics
197 i += shift;
198 j = std::max ( shift, std::min ( firstUnknownPos, j ) ) - shift;
199 }
200 }
201
202 //measurements::end();
203
204 return occ;
205}
206
207template < class SymbolType >
211
212 //measurements::start("Algorithm", measurements::Type::MAIN);
213
214 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
215 tree::PrefixRankedTree < unsigned > repeats = tree::properties::ExactSubtreeRepeatsNaive::repeats ( subject );
216
217 unsigned firstUnknownPos = pattern.getContent ( ).size ( );
218 for ( unsigned i = 0; i < pattern.getContent ( ).size ( ); ++ i ) {
219 if ( pattern.getContent ( ) [ i ] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).contains ( pattern.getContent ( ) [ i ] ) ) {
220 firstUnknownPos = i;
221 break;
222 }
223 }
224
225 // index to the subject
226 unsigned i = 0;
227 // index to the pattern
228 unsigned j = 0;
229
230 // main loop of the algorithm over all possible indexes where the pattern can start
231 while ( i + pattern.getContent ( ).size ( ) <= subject.getContent ( ).size ( ) ) {
232 j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, repeats, pattern, i, j );
233
234 // match was found
235 if ( j >= pattern.getContent ( ).size ( ) ) occ.insert ( i );
236
237 if ( j == 0 ) {
238 i += 1;
239 } else {
240 unsigned shift = j - borderArray [ j ];
241 // shift heuristics
242 i += shift;
243 j = std::min ( firstUnknownPos, j ) - shift;
244 }
245 }
246
247 //measurements::end();
248
249 return occ;
250}
251
252} /* namespace exact */
253
254} /* namespace arbology */
255
Definition: KnuthMorrisPratt.h:35
static ext::set< unsigned > match(const tree::PrefixRankedBarTree< SymbolType > &subject, const tree::PrefixRankedBarTree< SymbolType > &pattern)
Definition: KnuthMorrisPratt.h:60
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::set< common::ranked_symbol< SymbolType > > & getNonlinearVariables() const &
Definition: PrefixRankedBarNonlinearPattern.h:277
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedBarNonlinearPattern.h:259
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 common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedBarPattern.h:202
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
Tree pattern represented as linear sequece as result of preorder traversal. The representation is so ...
Definition: PrefixRankedExtendedPattern.h:80
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedExtendedPattern.h:293
const ext::set< common::ranked_symbol< SymbolType > > & getNodeWildcards() const &
Definition: PrefixRankedExtendedPattern.h:157
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedExtendedPattern.h:184
Nonlinear tree pattern represented as linear sequece as result of preorder traversal....
Definition: PrefixRankedNonlinearPattern.h:82
const ext::set< common::ranked_symbol< SymbolType > > & getNonlinearVariables() const &
Definition: PrefixRankedNonlinearPattern.h:208
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedNonlinearPattern.h:333
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedNonlinearPattern.h:190
Tree pattern represented as linear sequece as result of preorder traversal. The representation is so ...
Definition: PrefixRankedPattern.h:77
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::vector< size_t > construct(const T &pattern)
Definition: BorderArrayNaive.h:59
static ext::vector< size_t > construct(const PrefixRankedPatternType &pattern)
Definition: BorderArray.h:47
static ext::vector< int > compute(const tree::PrefixRankedBarTree< SymbolType > &subject)
Definition: SubtreeJumpTable.h:49
Definition: BoyerMooreHorspool.h:22
int i
Definition: AllEpsilonClosure.h:118
constexpr const T & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
constexpr const T & min(const T &a)
Definition: algorithm.hpp:310