10template <
class SymbolType >
17 if( alternationOptimized ) {
18 * alternation = std::move( * alternationOptimized );
28 if( concatenationOptimized ) {
29 * concatenation = std::move( * concatenationOptimized );
39 if( iterationOptimized ) {
40 * iteration = std::move( * iterationOptimized );
50template <
class SymbolType >
55 while( A1( elem ) || A2( elem ) || A3( elem ) || A4( elem )
56 || A5( elem ) || A6( elem ) || A7( elem ) || A8( elem ) || A9( elem )
57 || A11( elem ) || V1( elem ) || S(elem) );
62template <
class SymbolType >
64 bool optimized =
false;
82 FormalRTESubstitution < SymbolType > * concatenation =
dynamic_cast<FormalRTESubstitution < SymbolType >*
>(
node );
85 if(* tmp != concatenation->getLeftElement ( ) ) {
87 concatenation->setLeftElement ( * tmp );
90 tmp = optimizeInner ( concatenation->getRightElement ( ));
91 if(* tmp != concatenation->getRightElement ( )) {
93 concatenation->setRightElement ( * tmp );
99 FormalRTEIteration < SymbolType > * iteration =
dynamic_cast<FormalRTEIteration < SymbolType >*
>(
node );
103 if(* tmp != iteration->getElement ( ) ) {
105 iteration->setElement ( * tmp );
119template <
class SymbolType >
120bool RTEOptimize::A1( FormalRTEElement < SymbolType > * &
node ) {
121 FormalRTEAlternation < SymbolType > * n =
dynamic_cast<FormalRTEAlternation < SymbolType > *
>(
node );
122 if( ! n )
return false;
124 if(
dynamic_cast < FormalRTEAlternation < SymbolType > *
> ( & n->getLeft ( ) ) ) {
125 FormalRTEAlternation < SymbolType > leftAlt ( std::move (
static_cast < FormalRTEAlternation < SymbolType > &
> ( n->getLeft ( ) ) ) );
127 n->setLeft ( std::move ( leftAlt.getLeft ( ) ) );
128 leftAlt.setLeft ( std::move ( leftAlt.getRight ( ) ) );
129 leftAlt.setRight ( std::move ( n->getRight ( ) ) );
130 n->setRight ( std::move ( leftAlt ) );
143template <
class SymbolType >
144bool RTEOptimize::A2 ( FormalRTEElement < SymbolType > * &
node ) {
145 FormalRTEAlternation < SymbolType > * n =
dynamic_cast<FormalRTEAlternation < SymbolType > *
>(
node );
146 if( ! n )
return false;
148 if (
dynamic_cast < FormalRTEAlternation < SymbolType > *
> ( & n->getRight ( ) ) ) {
149 FormalRTEAlternation < SymbolType > & rightAlt =
static_cast < FormalRTEAlternation < SymbolType > &
> ( n->getRight ( ) );
151 if ( n->getLeft ( ) > rightAlt.getLeft ( ) ) {
154 n->setLeft ( std::move ( rightAlt.getLeft ( ) ) );
155 rightAlt.setLeft ( std::move ( tmp ) );
170template <
class SymbolType >
171bool RTEOptimize::A3 ( FormalRTEElement < SymbolType > * &
node ) {
172 FormalRTEAlternation < SymbolType > * n =
dynamic_cast < FormalRTEAlternation < SymbolType > *
> (
node );
173 if( ! n )
return false;
177 if (
dynamic_cast < FormalRTEEmpty < SymbolType > *
> ( & n->getRight ( ) ) ) {
178 node = std::move ( n->getLeft ( ) ).clone ( );
183 if (
dynamic_cast < FormalRTEEmpty < SymbolType > *
> ( & n->getLeft ( ) ) ) {
184 node = std::move ( n->getRight ( ) ).clone ( );
197template <
class SymbolType >
198bool RTEOptimize::A4( FormalRTEElement < SymbolType > * &
node ) {
207 FormalRTEAlternation < SymbolType > * n =
dynamic_cast < FormalRTEAlternation < SymbolType > *
> (
node );
208 if ( ! n )
return false;
210 if ( n->getLeftElement() == n->getRightElement() ) {
211 node = std::move ( n->getRight ( ) ).clone ( );
224template <
class SymbolType >
225bool RTEOptimize::A5( FormalRTEElement < SymbolType > * &
node ) {
226 FormalRTESubstitution < SymbolType > * n =
dynamic_cast<FormalRTESubstitution < SymbolType > *
>(
node );
227 if( ! n )
return false;
229 FormalRTESubstitution < SymbolType > * leftNode =
dynamic_cast < FormalRTESubstitution < SymbolType > *
> ( & n->getLeft ( ) );
230 if( leftNode && n->getSubstitutionSymbol ( ) == leftNode->getSubstitutionSymbol ( ) ) {
231 FormalRTESubstitution < SymbolType > leftCon ( std::move ( * leftNode ) );
233 n->setLeft ( std::move ( leftCon.getLeft ( ) ) );
234 leftCon.setLeft ( std::move ( leftCon.getRight ( ) ) );
235 leftCon.setRight ( std::move ( n->getRight ( ) ) );
236 n->setRight ( std::move ( leftCon ) );
249template <
class SymbolType >
250bool RTEOptimize::A6( FormalRTEElement < SymbolType > * &
node ) {
251 FormalRTESubstitution < SymbolType > * n =
dynamic_cast<FormalRTESubstitution < SymbolType > *
>(
node );
252 if( ! n )
return false;
256 if ( n->getSubstitutionSymbol ( ) == n->getLeft ( ) ) {
257 node = std::move ( n->getRight ( ) ).clone ( );
262 if ( n->getSubstitutionSymbol ( ) == n->getRight ( ) ) {
263 node = std::move ( n->getRight ( ) ).clone ( );
277template <
class SymbolType >
278bool RTEOptimize::A7( FormalRTEElement < SymbolType > * &
node ) {
279 FormalRTESubstitution < SymbolType > * n =
dynamic_cast<FormalRTESubstitution < SymbolType > *
>(
node );
280 if( ! n )
return false;
282 if (
dynamic_cast < FormalRTEEmpty < SymbolType > *
> ( & n->getLeft ( ) ) ) {
284 node =
new FormalRTEEmpty < SymbolType > { };
296template <
class SymbolType >
297bool RTEOptimize::A8( FormalRTEElement < SymbolType > * &
node ) {
298 FormalRTEAlternation < SymbolType > * n =
dynamic_cast<FormalRTEAlternation < SymbolType > *
>(
node );
299 if( ! n )
return false;
301 FormalRTESubstitution < SymbolType > * left =
dynamic_cast < FormalRTESubstitution < SymbolType > *
> ( & n->getLeft ( ) );
302 FormalRTESubstitution < SymbolType > * right =
dynamic_cast < FormalRTESubstitution < SymbolType > *
> ( & n->getRight ( ) );
303 if ( left && right && left->getLeft ( ) == right->getLeft ( ) && left->getSubstitutionSymbol ( ) == right->getSubstitutionSymbol ( ) ) {
304 FormalRTEAlternation < SymbolType > alt { std::move ( left->getRight ( ) ), std::move ( right->getRight ( ) ) };
306 node =
new FormalRTESubstitution < SymbolType > ( std::move ( left->getLeft ( ) ), std::move ( alt ), std::move ( left->getSubstitutionSymbol ( ) ) );
319template <
class SymbolType >
320bool RTEOptimize::A9( FormalRTEElement < SymbolType > * &
node ) {
321 FormalRTEAlternation < SymbolType > * n =
dynamic_cast<FormalRTEAlternation < SymbolType > *
>(
node );
322 if( ! n )
return false;
324 FormalRTESubstitution < SymbolType > * left =
dynamic_cast < FormalRTESubstitution < SymbolType > *
> ( & n->getLeft ( ) );
325 FormalRTESubstitution < SymbolType > * right =
dynamic_cast < FormalRTESubstitution < SymbolType > *
> ( & n->getRight ( ) );
326 if ( left && right && left->getRight ( ) == right->getRight ( ) && left->getSubstitutionSymbol ( ) == right->getSubstitutionSymbol ( ) ) {
327 FormalRTEAlternation < SymbolType > alt { std::move ( left->getLeft ( ) ), std::move ( right->getLeft ( ) ) };
329 node =
new FormalRTESubstitution < SymbolType > ( std::move ( alt ), std::move ( left->getRight ( ) ), std::move ( left->getSubstitutionSymbol ( ) ) );
407template <
class SymbolType >
408bool RTEOptimize::A11( FormalRTEElement < SymbolType > * &
node ) {
409 FormalRTEIteration < SymbolType > * n =
dynamic_cast<FormalRTEIteration < SymbolType > *
>(
node );
410 if( ! n )
return false;
412 FormalRTEAlternation < SymbolType > * childAlt =
dynamic_cast < FormalRTEAlternation < SymbolType > *
> ( & n->getChild ( ) );
414 if ( childAlt->getLeft ( ) == n->getSubstitutionSymbol ( ) ) {
415 n->setChild ( std::move ( childAlt->getRight ( ) ) );
418 if ( childAlt->getRight ( ) == n->getSubstitutionSymbol ( ) ) {
419 n->setChild ( std::move ( childAlt->getLeft ( ) ) );
432template <
class SymbolType >
433bool RTEOptimize::V1( FormalRTEElement < SymbolType > * &
node ) {
434 FormalRTEIteration < SymbolType > * n =
dynamic_cast<FormalRTEIteration < SymbolType > *
>(
node );
435 if( ! n )
return false;
437 if (
dynamic_cast < FormalRTEEmpty < SymbolType > *
> ( & n->getChild ( ) ) ) {
438 delete std::exchange (
node, n->getSubstitutionSymbol ( ).clone ( ) );
Class representing wrapper of dynamically allocated object behaving like rvalue reference.
Definition: ptr_value.hpp:40
Managed pointer simulating value like behavior.
Definition: memory.hpp:233
T * get()
Getter of the raw managed pointer.
Definition: memory.hpp:367
static rte::FormalRTE< SymbolType > optimize(const rte::FormalRTE< SymbolType > &rte)
Definition: ToFTAGlushkov.h:22