Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
GlushkovFollow.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/map>
9#include <alib/set>
10#include <alib/vector>
12
15
16#include "GlushkovFirst.h"
17
18namespace rte {
19
20
21 template < class SymbolType >
23
24 template < class SymbolType >
26
27 template < class SymbolType >
29
30 template < class SymbolType >
32
34
35
36 template < class SymbolType >
38
39 template < class SymbolType >
40 static void preprocessSubMap ( TSubstMap < SymbolType > & subMap, const rte::FormalRTE < ext::pair < SymbolType, unsigned > > & rte );
41
42public:
47 template < class SymbolType >
49
50 template < class SymbolType >
51 class Formal {
52 public:
59 };
60};
61
62template < class SymbolType >
66
67 /* initialize subMap keys
68 \forall a \in K: sub[a] = \emptyset
69 */
70 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & ssymb : rte.getSubstitutionAlphabet ( ) )
71 subMap.insert ( std::make_pair ( ssymb, TSetOfSymbols < SymbolType > { } ) );
72
73 /* initialize follow keys
74 \forall a \in F: follow[a] = \emptyset
75 */
76 /*
77 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symb : rte.getAlphabet ( ) )
78 res.insert ( std::make_pair ( symb, TFollowTuple < SymbolType > ( ) ) );
79 */
80
81 rte.getRTE ( ).getStructure ( ).template accept < void, GlushkovFollow::Formal < SymbolType > > ( rte, subMap, res );
82 return res;
83}
84
85// -----------------------------------------------------------------------------
86
87template < class SymbolType >
88TFollowTuple < SymbolType > GlushkovFollow::replaceConstants ( const rte::FormalRTE < ext::pair < SymbolType, unsigned > > & rte, const ext::vector < TSetOfSymbols < SymbolType > > & children, const TSubstMap < SymbolType > & subMap ) {
90
91 for ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > & s : children ) {
94 if ( rte.getSubstitutionAlphabet ( ).count ( e ) > 0 )
95 processed.insert ( subMap.at ( e ).begin ( ), subMap.at ( e ).end ( ) );
96 else
97 processed.insert ( e );
98 }
99 follow.push_back ( processed );
100 }
101
102 return follow;
103}
104
110template < class SymbolType >
111void GlushkovFollow::preprocessSubMap ( TSubstMap < SymbolType > & subMap, const rte::FormalRTE < ext::pair < SymbolType, unsigned > > & rte ) {
112 const TSetOfSymbols < SymbolType > & alphabetK = rte.getSubstitutionAlphabet ( );
113 for ( bool change = true; change; change = false )
114 for ( std::pair < const common::ranked_symbol < ext::pair < SymbolType, unsigned > >, TSetOfSymbols < SymbolType > > & kv : subMap ) {
115 TSetOfSymbols < SymbolType > & substSet = kv.second;
116
117 for ( auto eIter = substSet.begin ( ); eIter != substSet.end ( ); ) {
118 if ( alphabetK.count ( * eIter ) == 0 ) {
119 ++eIter;
120 } else {
121 auto it = subMap.find ( * eIter );
122 size_t oldSize = substSet.size ( );
123 substSet.insert ( it->second.begin ( ), it->second.end ( ) );
124 change = ( oldSize != substSet.size ( ) ); // something was added
125 eIter = substSet.erase ( eIter );
126 }
127 }
128 }
129}
130
131
132// -----------------------------------------------------------------------------
133
134template < class SymbolType >
136
137 /* update substitution mapping */
138 // none
139
140 // just recurse here
141 node.getLeftElement ( ).template accept < void, GlushkovFollow::Formal < SymbolType > > ( rte, subMap, res );
142 node.getRightElement ( ).template accept < void, GlushkovFollow::Formal < SymbolType > > ( rte, subMap, res );
143}
144
145template < class SymbolType >
147 preprocessSubMap ( subMap, rte );
148
149 /* update substitution mapping */
150 // prepare left map
151 auto itMap = subMap.find ( node.getSubstitutionSymbol ( ).getSymbol ( ) );
152 const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > backup = std::move ( itMap->second );
153 itMap->second.clear();
154
155 for ( const auto & s : node.getRightElement ( ).template accept < ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > >, GlushkovFirst::Formal < ext::pair < SymbolType, unsigned > > > ( ) )
156 itMap->second.insert ( s );
157
158 node.getLeftElement ( ).template accept < void, GlushkovFollow::Formal < SymbolType > > ( rte, subMap, res );
159
160 // restore original map
161 itMap->second = std::move ( backup );
162
163 node.getRightElement ( ).template accept < void, GlushkovFollow::Formal < SymbolType > > ( rte, subMap, res );
164}
165
166template < class SymbolType >
168 preprocessSubMap ( subMap, rte );
169
170 /* update substitution mapping */
171 auto itMap = subMap.find ( node.getSubstitutionSymbol ( ).getSymbol ( ) );
172 const TSetOfSymbols < SymbolType > backup = itMap->second;
173 for ( const auto & s : node.getElement ( ).template accept < TSetOfSymbols < SymbolType >, GlushkovFirst::Formal < ext::pair < SymbolType, unsigned > > > ( ) ) {
174 subMap [ node.getSubstitutionSymbol ( ).getSymbol ( ) ].insert ( s );
175 }
176
177 node.getElement ( ).template accept < void, GlushkovFollow::Formal < SymbolType > > ( rte, subMap, res );
178
179 /* restore substitution mapping */
180 itMap->second = backup;
181}
182
183template < class SymbolType >
185 preprocessSubMap ( subMap, rte );
186
187 /* update substitution mapping */
188 // none
189
190 /* compute first for all children and replace substitution symbols */
192 for ( const rte::FormalRTEElement < ext::pair < SymbolType, unsigned > > & c : node.getElements ( ) )
193 children.push_back ( c.template accept < TSetOfSymbols < SymbolType >, GlushkovFirst::Formal < ext::pair < SymbolType, unsigned > > > ( ) );
194
195 res.insert ( std::make_pair ( node.getSymbol ( ), replaceConstants ( rte, children, subMap ) ) );
196
197 // call follow on children
198 for ( const auto & childNode : node.getElements ( ) ) {
199 childNode . template accept < void, GlushkovFollow::Formal < SymbolType > > ( rte, subMap, res );
200 }
201}
202
203template < class SymbolType >
205 preprocessSubMap ( subMap, rte );
206}
207
208template < class SymbolType >
210 preprocessSubMap ( subMap, rte );
211}
212
213} /* namespace rte */
214
Definition: ranked_symbol.hpp:20
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
R & at(const T &key, R &defaultValue)
Definition: map.hpp:163
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
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
Represents the alternation operator in the regular tree expression. The node must have exactly two ch...
Definition: FormalRTEAlternation.h:44
Definition: FormalRTEElement.h:54
Represents the empty expression in the regular tree expression. The node can't have any children.
Definition: FormalRTEEmpty.h:40
Represents the iteration operator in the regular tree expression. The node has exactly one child.
Definition: FormalRTEIteration.h:45
Represents the concatenation operator in the regular tree expression. The node must have exactly two ...
Definition: FormalRTESubstitution.h:44
Represents the terminal symbol in the regular tree expression. The number of children must be the sam...
Definition: FormalRTESymbolAlphabet.h:44
Represents the substitution symbol in the regular tree expression. The node can't have any children.
Definition: FormalRTESymbolSubst.h:43
Formal regular tree expression represents regular tree expression. It describes regular tree language...
Definition: FormalRTE.h:71
Definition: GlushkovFirst.h:25
Definition: GlushkovFollow.h:51
static void visit(const FormalRTEAlternation< ext::pair< SymbolType, unsigned > > &node, const rte::FormalRTE< ext::pair< SymbolType, unsigned > > &rte, TSubstMap< SymbolType > &subMap, TFollowMap< SymbolType > &res)
Definition: GlushkovFollow.h:135
Definition: GlushkovFollow.h:33
static ext::map< common::ranked_symbol< ext::pair< SymbolType, unsigned > >, TFollowTuple< SymbolType > > follow(const rte::FormalRTE< ext::pair< SymbolType, unsigned > > &rte)
Definition: GlushkovFollow.h:63
return res
Definition: MinimizeByPartitioning.h:145
constexpr auto visit(Visitor &&vis, Variants &&... vars)
Definition: variant.hpp:42
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: Node.cpp:11
Definition: ToFTAGlushkov.h:22