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
14
15#include <string/LinearString.h>
16
17namespace stringology {
18
19namespace exact {
20
26public:
31 template < class SymbolType >
33
34};
35
36template < class SymbolType >
40
41 //measurements::start("Algorithm", measurements::Type::MAIN);
42
43 // index to the subject
44 unsigned i = 0;
45 size_t j = 0;
46
47 // main loop of the algorithm over all possible indexes where the pattern can start
48 while ( i + pattern.getContent ( ).size ( ) <= subject.getContent ( ).size ( ) ) {
49 // index to the pattern
50
51 while ( j < pattern.getContent ( ).size ( ) && subject.getContent ( )[i + j] == pattern.getContent ( )[j] )
52 j++;
53
54 // match was found
55 if ( j >= pattern.getContent ( ).size ( ) ) occ.insert ( i );
56
57 // shift heristics
58 if ( j != 0 ) {
59 i += j - ba[j];
60 j = ba[j];
61 } else {
62 i += 1;
63 }
64 }
65
66 //measurements::end();
67
68 return occ;
69}
70
71} /* namespace exact */
72
73} /* namespace stringology */
74
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
Linear string.
Definition: LinearString.h:57
const ext::vector< SymbolType > & getContent() const &
Definition: LinearString.h:238
static ext::vector< size_t > construct(const string::LinearString< SymbolType > &string)
Definition: BorderArray.h:27
Definition: KnuthMorrisPratt.h:25
static ext::set< unsigned > match(const string::LinearString< SymbolType > &subject, const string::LinearString< SymbolType > &pattern)
Definition: KnuthMorrisPratt.h:37
int i
Definition: AllEpsilonClosure.h:118
Definition: ArithmeticCompression.h:18