Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
HammingMatchingAutomaton.h
Go to the documentation of this file.
1
6/*
7*/
8
9#pragma once
10
11#include <automaton/FSM/NFA.h>
12#include <string/LinearString.h>
14
15namespace stringology {
16
17namespace matching {
18
20public:
26 template < class SymbolType >
28
34 template < class SymbolType >
36};
37
38template < class SymbolType >
40 auto initial_state = ext::make_pair(0, 0);
41
43 result.setInputAlphabet(pattern.getAlphabet());
44
45 // add k+1 paralel automatas (sfoeco type = exact matching) (where k is allowed_errors)
46 for (unsigned int j = 0; j<allowed_errors + 1; j++) {
47 for (unsigned int i = j; i<pattern.getContent().size() + 1; i++) {
48 result.addState(ext::make_pair(i, j));
49 if (i == pattern.getContent().size()) {
50 result.addFinalState(ext::make_pair(i, j));
51 }
52 }
53 }
54
55 for (const SymbolType& symbol : pattern.getAlphabet()) {
56 result.addTransition(initial_state, symbol, initial_state);
57 }
58
59 for (unsigned int j = 0; j < allowed_errors + 1; j++) {
60 for (unsigned int i = j; i<pattern.getContent().size(); i++) {
61 auto from = ext::make_pair(i, j);
62 auto to = ext::make_pair(i+1, j);
63 result.addTransition(from, pattern.getContent()[i], to);
64 }
65 }
66
67 // add diagonal addTransition
68 for (unsigned int j = 0; j<allowed_errors; j++) {
69 for (unsigned int i = j; i<pattern.getContent().size(); i++) {
70 auto from = ext::make_pair(i, j);
71 auto to = ext::make_pair(i + 1, j + 1);
72
73 for ( const SymbolType & symbol : pattern.getAlphabet()) {
74 if (symbol != pattern.getContent()[i]) {
75 result.addTransition(from, symbol, to);
76 }
77 }
78 }
79 }
80
81 // remove all inaccessible states from state
82 return result;
83}
84
85
86template < class SymbolType >
88 auto initial_state = ext::make_pair(0, 0);
89
91 result.setInputAlphabet(pattern.getAlphabet());
92
93 const SymbolType & wildcard = pattern.getWildcardSymbol();
94 ext::set<SymbolType> alphabet_without_wildcard = pattern.getAlphabet();
95 alphabet_without_wildcard.erase(wildcard);
96
97 // add k+1 paralel automatas (sfoeco type = exact matching) (where k is allowed_errors)
98 for (unsigned int j = 0; j<allowed_errors + 1; j++) {
99 for (unsigned int i = j; i<pattern.getContent().size() + 1; i++) {
100 result.addState(ext::make_pair(i, j));
101 if (i == pattern.getContent().size()) {
102 result.addFinalState(ext::make_pair(i, j));
103 }
104 }
105 }
106
107 for (const SymbolType& symbol : alphabet_without_wildcard) {
108 result.addTransition(initial_state, symbol, initial_state);
109 }
110
111 for (unsigned int j = 0; j < allowed_errors + 1; j++) {
112 for (unsigned int i = j; i<pattern.getContent().size(); i++) {
113 auto from = ext::make_pair(i, j);
114 auto to = ext::make_pair(i+1, j);
115 if (pattern.getContent()[i] == pattern.getWildcardSymbol()) {
116 for (const SymbolType& symbol : alphabet_without_wildcard) {
117 result.addTransition(from, symbol, to);
118 }
119 } else {
120 result.addTransition(from, pattern.getContent()[i], to);
121 }
122 }
123 }
124
125 // add diagonal addTransition
126 for (unsigned int j = 0; j<allowed_errors; j++) {
127 for (unsigned int i = j; i<pattern.getContent().size(); i++) {
128 auto from = ext::make_pair(i, j);
129 auto to = ext::make_pair(i + 1, j + 1);
130
131 if (pattern.getContent()[i] != wildcard) {
132 for ( const SymbolType & symbol : alphabet_without_wildcard ) {
133 if (symbol != pattern.getContent()[i]) {
134 result.addTransition(from, symbol, to);
135 }
136 }
137 }
138 }
139 }
140
141 return result;
142}
143
144} /* namespace matching */
145
146} /* namespace stringology */
147
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
Definition: set.hpp:44
Linear string.
Definition: LinearString.h:57
const ext::set< SymbolType > & getAlphabet() const &
Definition: LinearString.h:103
const ext::vector< SymbolType > & getContent() const &
Definition: LinearString.h:238
Linear wildcard string.
Definition: WildcardLinearString.h:44
const ext::vector< SymbolType > & getContent() const &
Definition: WildcardLinearString.h:279
const SymbolType & getWildcardSymbol() const &
Definition: WildcardLinearString.h:139
const ext::set< SymbolType > & getAlphabet() const &
Definition: WildcardLinearString.h:112
Definition: HammingMatchingAutomaton.h:19
static automaton::NFA< SymbolType, ext::pair< unsigned int, unsigned int > > construct(const string::LinearString< SymbolType > &pattern, unsigned int allowed_errors)
Definition: HammingMatchingAutomaton.h:39
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
for(const StateType &state :fsm.getStates()) renamingData.insert(std Rename::RenamedAutomaton< T > result(renamingData.at(fsm.getInitialState()))
Definition: Rename.h:253
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ArithmeticCompression.h:18