Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
BoyerMooreHorspool.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/set>
9#include <alib/map>
10
12
17
21
22namespace arbology {
23
24namespace exact {
25
31public:
36 template < class SymbolType >
38 template < class SymbolType >
40 template < class SymbolType >
42
43};
44
45template < class SymbolType >
47 return match ( subject, tree::PrefixRankedBarPattern < SymbolType > ( pattern ) );
48}
49
50template < class SymbolType >
53 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::BadCharacterShiftTable::bcs ( pattern ); //NOTE: the subjects alphabet must be a subset or equal to the pattern
54 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
55
56 // index to the subject
57 unsigned i = pattern.getContent ( ).size ( ) - 1;
58
59 // main loop of the algorithm over all possible indexes where the pattern can start
60 while ( i < subject.getContent ( ).size ( ) ) {
61 // pair j and offset
62 ext::pair < int, int > jOffset = tree::exact::BackwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, pattern, i );
63
64 // match was found
65 if ( jOffset.first == -1 ) occ.insert ( jOffset.second + 1);
66
67 // shift heuristics
68 i += bcs[subject.getContent ( )[i]];
69 }
70
71 return occ;
72}
73
74template < class SymbolType >
77 ext::map < common::ranked_symbol < SymbolType >, size_t > bcs = tree::properties::BadCharacterShiftTable::bcs ( pattern ); //NOTE: the subjects alphabet must be a subset or equal to the pattern
78
79 ext::vector < int > subjectSubtreeJumpTable = tree::properties::SubtreeJumpTable::compute ( subject );
80 tree::PrefixRankedBarTree < unsigned > repeats = tree::properties::ExactSubtreeRepeatsNaive::repeats ( subject );
81
82 // index to the subject
83 unsigned i = pattern.getContent ( ).size ( ) - 1;
84
85 // main loop of the algorithm over all possible indexes where the pattern can start
86 while ( i < subject.getContent ( ).size ( ) ) {
87 // pair j and offset
88 ext::pair < int, int > jOffset = tree::exact::BackwardOccurrenceTest::occurrence ( subject, subjectSubtreeJumpTable, repeats, pattern, i );
89
90 // match was found
91 if ( jOffset.first == -1 ) occ.insert ( jOffset.second + 1);
92
93 // shift heuristics
94 i += bcs[subject.getContent ( )[i]];
95 }
96
97 return occ;
98}
99
100} /* namespace exact */
101
102} /* namespace arbology */
103
Definition: BoyerMooreHorspool.h:30
static ext::set< unsigned > match(const tree::PrefixRankedBarTree< SymbolType > &subject, const tree::PrefixRankedBarTree< SymbolType > &pattern)
Definition: BoyerMooreHorspool.h:46
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
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
static ext::pair< int, int > occurrence(const PrefixRankedBarTree< SymbolType > &subject, const ext::vector< int > &subjectSubtreeJumpTable, const PrefixRankedBarTree< SymbolType > &pattern, size_t subjectPosition)
Definition: BackwardOccurrenceTest.h:33
static ext::map< common::ranked_symbol< SymbolType >, size_t > bcs(const tree::PrefixRankedBarPattern< SymbolType > &pattern)
Definition: BadCharacterShiftTable.h:34
static ext::vector< int > compute(const tree::PrefixRankedBarTree< SymbolType > &subject)
Definition: SubtreeJumpTable.h:49
Definition: BoyerMooreHorspool.h:22
int i
Definition: AllEpsilonClosure.h:118