Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ReversedBoyerMooreHorspool.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
33public:
38 template < class SymbolType >
40 template < class SymbolType >
42 template < class SymbolType >
44 template < class SymbolType >
46 template < class SymbolType >
48 template < class SymbolType >
50
51};
52
53template < class SymbolType >
55 return match ( subject, tree::PrefixRankedBarPattern < SymbolType > ( pattern ) );
56}
57
58template < class SymbolType >
61 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
62 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
63
64 // index to the subject
65 int i = static_cast < int > ( subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( ) );
66
67 // main loop of the algorithm over all possible indexes where the pattern can start
68 while ( i >= 0 ) {
69
70 // index to the pattern
71 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, pattern, i );
72
73 // match was found
74 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
75
76 // shift heuristics
77 i -= bcs[subject.getContent ( )[i]];
78 }
79
80 return occ;
81}
82
83template < class SymbolType >
86 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
87 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
88
89 tree::PrefixRankedBarTree < unsigned > repeats = tree::properties::ExactSubtreeRepeatsNaive::repeats ( subject );
90
91 // index to the subject
92 int i = static_cast < int > ( subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( ) );
93
94 // main loop of the algorithm over all possible indexes where the pattern can start
95 while ( i >= 0 ) {
96 // index to the pattern
97 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, repeats, pattern, i );
98
99 // match was found
100 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
101
102 // shift heuristics
103 i -= bcs[subject.getContent ( )[i]];
104 }
105
106 return occ;
107}
108
109template < class SymbolType >
111 return match ( subject, tree::PrefixRankedPattern < SymbolType > ( pattern ) );
112}
113
114template < class SymbolType >
117 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
118 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
119
120 if ( subject.getContent ( ).size ( ) < pattern.getContent ( ).size ( ) )
121 return occ;
122
123 // index to the subject
124 size_t i = subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( );
125
126 // main loop of the algorithm over all possible indexes where the pattern can start
127 while ( true ) {
128 // index to the pattern
129 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, pattern, i );
130
131 // match was found
132 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
133
134 // shift heristics
135 size_t shift = bcs[subject.getContent ( )[i]];
136
137 if ( shift > i )
138 break;
139
140 i -= shift;
141 }
142
143 return occ;
144}
145
146template < class SymbolType >
149 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::ReversedBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
150
151 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
152 tree::PrefixRankedTree < unsigned > repeats = tree::properties::ExactSubtreeRepeatsNaive::repeats ( subject );
153
154 if ( subject.getContent ( ).size ( ) < pattern.getContent ( ).size ( ) )
155 return occ;
156
157 // index to the subject
158 size_t i = subject.getContent ( ).size ( ) - pattern.getContent ( ).size ( );
159
160 // main loop of the algorithm over all possible indexes where the pattern can start
161 while ( true ) {
162 // index to the pattern
163 unsigned j = tree::exact::ForwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, repeats, pattern, i );
164
165 // match was found
166 if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
167
168 // shift heristics
169 size_t shift = bcs[subject.getContent ( )[i]];
170
171 if ( shift > i )
172 break;
173
174 i -= shift;
175 }
176
177 return occ;
178}
179
180} /* namespace exact */
181
182} /* namespace arbology */
183
Definition: ReversedBoyerMooreHorspool.h:32
static ext::set< unsigned > match(const tree::PrefixRankedBarTree< SymbolType > &subject, const tree::PrefixRankedBarTree< SymbolType > &pattern)
Definition: ReversedBoyerMooreHorspool.h:54
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: ReversedBadCharacterShiftTable.h:44
static ext::vector< int > compute(const tree::PrefixRankedBarTree< SymbolType > &subject)
Definition: SubtreeJumpTable.h:49
Definition: BoyerMooreHorspool.h:22
int i
Definition: AllEpsilonClosure.h:118