Skip to content
Snippets Groups Projects
Commit 804b8f73 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

c++11 std::move

parent e8dd9d08
No related branches found
No related tags found
No related merge requests found
...@@ -29,14 +29,14 @@ regexp::UnboundedRegExp BrzozowskiAlgebraic::convert( const automaton::NFA & aut ...@@ -29,14 +29,14 @@ regexp::UnboundedRegExp BrzozowskiAlgebraic::convert( const automaton::NFA & aut
for( const auto & q : automaton.getStates( ) ) for( const auto & q : automaton.getStates( ) )
{ {
if( isInSet( q, automaton.getFinalStates( ) ) ) if( isInSet( q, automaton.getFinalStates( ) ) )
solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( q.getName( ) ) ), new regexp::UnboundedRegExpEpsilon( ) ); solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( q.getName( ) ) ), regexp::UnboundedRegExpEpsilon { } );
} }
   
for( const auto & p : automaton.getTransitions() ) for( const auto & p : automaton.getTransitions() )
{ {
for( const auto & q : p.second ) for( const auto & q : p.second )
{ {
solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( p.first.first.getName() ) ), alphabet::Symbol( alphabet::LabeledSymbol( q.getName() ) ), new regexp::UnboundedRegExpSymbol { p.first.second } ); solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( p.first.first.getName() ) ), alphabet::Symbol( alphabet::LabeledSymbol( q.getName() ) ), regexp::UnboundedRegExpSymbol { p.first.second } );
} }
} }
   
...@@ -63,12 +63,12 @@ regexp::UnboundedRegExp BrzozowskiAlgebraic::convert( const automaton::DFA & aut ...@@ -63,12 +63,12 @@ regexp::UnboundedRegExp BrzozowskiAlgebraic::convert( const automaton::DFA & aut
for( const auto & q : automaton.getStates( ) ) for( const auto & q : automaton.getStates( ) )
{ {
if( isInSet( q, automaton.getFinalStates( ) ) ) if( isInSet( q, automaton.getFinalStates( ) ) )
solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( q.getName( ) ) ), new regexp::UnboundedRegExpEpsilon( ) ); solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( q.getName( ) ) ), regexp::UnboundedRegExpEpsilon { } );
} }
   
for( const auto & p : automaton.getTransitions() ) for( const auto & p : automaton.getTransitions() )
{ {
solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( p.first.first.getName() ) ), alphabet::Symbol( alphabet::LabeledSymbol( p.second.getName() ) ), new regexp::UnboundedRegExpSymbol { p.first.second } ); solver.addEquation( alphabet::Symbol( alphabet::LabeledSymbol ( p.first.first.getName() ) ), alphabet::Symbol( alphabet::LabeledSymbol( p.second.getName() ) ), regexp::UnboundedRegExpSymbol { p.first.second } );
} }
   
return regexp::UnboundedRegExp { solver.solve( alphabet::Symbol( alphabet::LabeledSymbol (automaton.getInitialState().getName() ) ) ).getRegExp() }; return regexp::UnboundedRegExp { solver.solve( alphabet::Symbol( alphabet::LabeledSymbol (automaton.getInitialState().getName() ) ) ).getRegExp() };
......
...@@ -23,8 +23,6 @@ LeftRegularEquationSolver::LeftRegularEquationSolver( void ) ...@@ -23,8 +23,6 @@ LeftRegularEquationSolver::LeftRegularEquationSolver( void )
   
LeftRegularEquationSolver::~LeftRegularEquationSolver( void ) LeftRegularEquationSolver::~LeftRegularEquationSolver( void )
{ {
for( const auto & kv : m_eqFinal )
delete kv.second;
} }
   
regexp::UnboundedRegExpElement* LeftRegularEquationSolver::eliminate( void ) regexp::UnboundedRegExpElement* LeftRegularEquationSolver::eliminate( void )
...@@ -33,32 +31,32 @@ regexp::UnboundedRegExpElement* LeftRegularEquationSolver::eliminate( void ) ...@@ -33,32 +31,32 @@ regexp::UnboundedRegExpElement* LeftRegularEquationSolver::eliminate( void )
   
for( auto itA = m_symbolsByDepth.rbegin( ); itA != m_symbolsByDepth.rend( ); itA ++ ) for( auto itA = m_symbolsByDepth.rbegin( ); itA != m_symbolsByDepth.rend( ); itA ++ )
{ {
const Symbol & a = * itA; const alphabet::Symbol & a = * itA;
   
/* /*
* Apply Arden's Lemma * Apply Arden's Lemma
* A = A0 + B1 + C2 * A = A0 + B1 + C2
* => A = 10*B + 20*C * => A = 10*B + 20*C
*/ */
regexp::UnboundedRegExpIteration* loop = new regexp::UnboundedRegExpIteration( * m_eqTransition[ a ][ a ] ); regexp::UnboundedRegExpIteration loop( std::move( m_eqTransition[ a ][ a ] ) );
   
// for all transitions from A apply Arden's Lemma // for all transitions from A apply Arden's Lemma
for( auto itB = std::next( itA ) ; itB != m_symbolsByDepth.rend( ); itB ++ ) for( auto itB = std::next( itA ) ; itB != m_symbolsByDepth.rend( ); itB ++ )
{ {
const Symbol & b = * itB; const alphabet::Symbol & b = * itB;
regexp::UnboundedRegExpConcatenation* concat = new regexp::UnboundedRegExpConcatenation( ); regexp::UnboundedRegExpConcatenation concat;
concat->appendElement( * m_eqTransition[ a ][ b ] ); concat.appendElement( std::move( m_eqTransition[ a ][ b ] ) );
concat->appendElement( * loop ); concat.appendElement( loop );
m_eqTransition[ a ][ b ] = /* opt.optimize( */ concat /* ) */; regexp::UnboundedRegExpAlternation alt;
// delete concat; alt.appendElement( std::move( concat ) );
m_eqTransition[ a ][ b ] = std::move( alt );
} }
regexp::UnboundedRegExpConcatenation* concat = new regexp::UnboundedRegExpConcatenation( ); regexp::UnboundedRegExpConcatenation concat;
concat->appendElement( * m_eqFinal[ a ] ); concat.appendElement( std::move( m_eqFinal[ a ] ) );
concat->appendElement( * loop ); concat.appendElement( std::move( loop ) );
m_eqFinal[ a ] = /* opt.optimize( */ concat /* ) */; regexp::UnboundedRegExpAlternation alt;
//delete concat; alt.appendElement( std::move( concat ) );
m_eqFinal[ a ] = std::move( alt );
delete loop;
   
/* /*
* eliminate A from rest of the equations using this pattern: * eliminate A from rest of the equations using this pattern:
...@@ -66,41 +64,32 @@ regexp::UnboundedRegExpElement* LeftRegularEquationSolver::eliminate( void ) ...@@ -66,41 +64,32 @@ regexp::UnboundedRegExpElement* LeftRegularEquationSolver::eliminate( void )
*/ */
for( auto itB = std::next( itA ); itB != m_symbolsByDepth.rend( ); itB ++ ) for( auto itB = std::next( itA ); itB != m_symbolsByDepth.rend( ); itB ++ )
{ {
const Symbol & b = * itB; const alphabet::Symbol & b = * itB;
   
for( auto itC = std::next( itA ); itC != m_symbolsByDepth.rend( ); itC ++ ) for( auto itC = std::next( itA ); itC != m_symbolsByDepth.rend( ); itC ++ )
{ {
const Symbol & c = * itC; const alphabet::Symbol & c = * itC;
regexp::UnboundedRegExpAlternation* alt = new regexp::UnboundedRegExpAlternation( ); regexp::UnboundedRegExpConcatenation concat;
regexp::UnboundedRegExpConcatenation* concat = new regexp::UnboundedRegExpConcatenation( ); concat.appendElement( m_eqTransition[ a ][ c ] );
concat->appendElement( * m_eqTransition[ a ][ c ] ); concat.appendElement( m_eqTransition[ b ][ a ] );
concat->appendElement( * m_eqTransition[ b ][ a ] ); regexp::UnboundedRegExpAlternation alt;
alt->appendElement( * m_eqTransition[ b ][ c ] ); alt.appendElement( std::move( m_eqTransition[ b ][ c ] ) );
alt->appendElement( * concat ); alt.appendElement( std::move( concat ) );
m_eqTransition[ b ][ c ] = /* opt.optimize( */ alt /* ) */; m_eqTransition[ b ][ c ] = /* opt.optimize( */ std::move( alt ) /* ) */;
// delete alt;
} }
   
regexp::UnboundedRegExpAlternation* alt = new regexp::UnboundedRegExpAlternation( ); regexp::UnboundedRegExpConcatenation concat;
regexp::UnboundedRegExpConcatenation* concat = new regexp::UnboundedRegExpConcatenation( ); concat.appendElement( m_eqFinal[ a ] );
concat->appendElement( * m_eqFinal[ a ] ); concat.appendElement( std::move( m_eqTransition[ b ][ a ] ) );
concat->appendElement( * m_eqTransition[ b ][ a ] ); regexp::UnboundedRegExpAlternation alt;
alt->appendElement( * m_eqFinal[ b ] ); alt.appendElement( std::move( m_eqFinal[ b ] ) );
alt->appendElement( * concat ); alt.appendElement( std::move( concat ) );
m_eqFinal[ b ] = /* opt.optimize( */ alt /* ) */; m_eqFinal[ b ] = /* opt.optimize( */ std::move( alt ) /* ) */;
// delete alt;
} }
// no need for this now, release some memory
for( const auto & kv : m_eqTransition[ a ] )
delete kv.second;
} }
   
regexp::UnboundedRegExpElement* result = m_eqFinal[ * m_symbolsByDepth.begin( ) ]; return m_eqFinal[ * m_symbolsByDepth.begin( ) ].clone();
m_eqFinal[ * m_symbolsByDepth.begin( ) ] = NULL;
return result;
} }
   
} /* namespace equations */ } /* namespace equations */
...@@ -50,45 +50,31 @@ void RegularEquationSolver::addSymbol( const alphabet::Symbol & symb ) ...@@ -50,45 +50,31 @@ void RegularEquationSolver::addSymbol( const alphabet::Symbol & symb )
   
for( const auto & s1 : m_symbols ) for( const auto & s1 : m_symbols )
{ {
m_eqTransition[ symb ][ s1 ] = new regexp::UnboundedRegExpEmpty( ); m_eqTransition[ symb ][ s1 ] = regexp::UnboundedRegExpAlternation { };
if( s1 != symb ) if( s1 != symb )
m_eqTransition[ s1 ][ symb ] = new regexp::UnboundedRegExpEmpty( ); m_eqTransition[ s1 ][ symb ] = regexp::UnboundedRegExpAlternation { };
} }
   
m_eqFinal[ symb ] = new regexp::UnboundedRegExpEmpty( ); m_eqFinal[ symb ] = regexp::UnboundedRegExpAlternation { };
} }
   
void RegularEquationSolver::addEquation( const Symbol & from, const Symbol & to, const UnboundedRegExpElement* eq ) void RegularEquationSolver::addEquation( const Symbol & from, const Symbol & to, const UnboundedRegExpElement& eq )
{ {
// RegExpOptimize opt; //TODO uncomment when implemented
if( ! isInSet( from, m_symbols ) ) if( ! isInSet( from, m_symbols ) )
throw exception::AlibException( "Symbol '" + (std::string) from + "' not in equation system." ); throw exception::AlibException( "Symbol '" + (std::string) from + "' not in equation system." );
   
if( ! isInSet( to, m_symbols ) ) if( ! isInSet( to, m_symbols ) )
throw exception::AlibException( "Symbol '" + (std::string) to + "' not in equation system." ); throw exception::AlibException( "Symbol '" + (std::string) to + "' not in equation system." );
   
regexp::UnboundedRegExpAlternation* alt = new regexp::UnboundedRegExpAlternation( ); m_eqTransition[ from ][ to ].appendElement( eq );
alt->appendElement( * eq );
alt->appendElement( * m_eqTransition[ from ][ to ] );
m_eqTransition[ from ][ to ] = /* opt.optimize( */ alt /* ) */;
delete eq;
// delete alt;
} }
   
void RegularEquationSolver::addEquation( const Symbol & from, const UnboundedRegExpElement* eq ) void RegularEquationSolver::addEquation( const Symbol & from, const UnboundedRegExpElement& eq )
{ {
// RegExpOptimize opt; //TODO uncommend when implemented
if( ! isInSet( from, m_symbols ) ) if( ! isInSet( from, m_symbols ) )
throw exception::AlibException( "Symbol '" + (std::string) from + "' not in equation system." ); throw exception::AlibException( "Symbol '" + (std::string) from + "' not in equation system." );
   
regexp::UnboundedRegExpAlternation* alt = new regexp::UnboundedRegExpAlternation( ); m_eqFinal[ from ].appendElement( eq );
alt->appendElement( * eq );
alt->appendElement( * m_eqFinal[ from ] );
m_eqFinal[ from ] = /* opt.optimize( */ alt /* ) */;
delete eq;
// delete alt;
} }
   
void RegularEquationSolver::symbolsByDepth( const Symbol & solveFor ) void RegularEquationSolver::symbolsByDepth( const Symbol & solveFor )
...@@ -113,9 +99,7 @@ void RegularEquationSolver::symbolsByDepth( const Symbol & solveFor ) ...@@ -113,9 +99,7 @@ void RegularEquationSolver::symbolsByDepth( const Symbol & solveFor )
auto map_s = m_eqTransition.find( s ); auto map_s = m_eqTransition.find( s );
for( auto it = map_s->second.begin( ); it != map_s->second.end( ); it ++ ) for( auto it = map_s->second.begin( ); it != map_s->second.end( ); it ++ )
{ {
// neighbouring if regexp between s and kv.first is not \0 if( it->second.getElements().size() > 0 && visited[ it->first ] == false ) {
if( dynamic_cast<regexp::UnboundedRegExpEmpty*>( it->second ) == NULL && visited [ it->first ] == false )
{
visited[ it->first ] = true; visited[ it->first ] = true;
queue.push( it->first ); queue.push( it->first );
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <alphabet/Symbol.h> #include <alphabet/Symbol.h>
#include <exception/AlibException.h> #include <exception/AlibException.h>
#include <regexp/unbounded/UnboundedRegExpElement.h> #include <regexp/unbounded/UnboundedRegExpElement.h>
#include <regexp/unbounded/UnboundedRegExpAlternation.h>
   
#include "common/macros.h" #include "common/macros.h"
   
...@@ -45,7 +46,7 @@ public: ...@@ -45,7 +46,7 @@ public:
* @param to symbol * @param to symbol
* @param eq equation * @param eq equation
*/ */
void addEquation( const alphabet::Symbol & from, const alphabet::Symbol & to, const regexp::UnboundedRegExpElement* eq ); void addEquation( const alphabet::Symbol & from, const alphabet::Symbol & to, const regexp::UnboundedRegExpElement& eq );
   
/** /**
* Adds equation in form: FROM = eq * Adds equation in form: FROM = eq
...@@ -53,7 +54,7 @@ public: ...@@ -53,7 +54,7 @@ public:
* @param from * @param from
* @param eq * @param eq
*/ */
void addEquation( const alphabet::Symbol & from, const regexp::UnboundedRegExpElement* eq ); void addEquation( const alphabet::Symbol & from, const regexp::UnboundedRegExpElement& eq );
   
/** /**
* Solve expression system * Solve expression system
...@@ -84,12 +85,12 @@ protected: ...@@ -84,12 +85,12 @@ protected:
/** /**
* Stores transitions from nonterminal to nonterminal, eg A = 2A + 2B + 1C * Stores transitions from nonterminal to nonterminal, eg A = 2A + 2B + 1C
*/ */
std::map<alphabet::Symbol, std::map<alphabet::Symbol, regexp::UnboundedRegExpElement*>> m_eqTransition; std::map<alphabet::Symbol, std::map<alphabet::Symbol, regexp::UnboundedRegExpAlternation>> m_eqTransition;
   
/** /**
* Stores equation not going to particular nonterminal, eg A = 01* * Stores equation not going to particular nonterminal, eg A = 01*
*/ */
std::map<alphabet::Symbol, regexp::UnboundedRegExpElement*> m_eqFinal; std::map<alphabet::Symbol, regexp::UnboundedRegExpAlternation> m_eqFinal;
   
/** /**
* Set of symbols * Set of symbols
......
...@@ -23,8 +23,6 @@ RightRegularEquationSolver::RightRegularEquationSolver( void ) ...@@ -23,8 +23,6 @@ RightRegularEquationSolver::RightRegularEquationSolver( void )
   
RightRegularEquationSolver::~RightRegularEquationSolver( void ) RightRegularEquationSolver::~RightRegularEquationSolver( void )
{ {
for( const auto & kv : m_eqFinal )
delete kv.second;
} }
   
regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void ) regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void )
...@@ -33,14 +31,14 @@ regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void ) ...@@ -33,14 +31,14 @@ regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void )
   
for( auto itA = m_symbolsByDepth.rbegin( ); itA != m_symbolsByDepth.rend( ); itA ++ ) for( auto itA = m_symbolsByDepth.rbegin( ); itA != m_symbolsByDepth.rend( ); itA ++ )
{ {
const Symbol & a = * itA; const alphabet::Symbol & a = * itA;
   
/* /*
* Apply Arden's Lemma * Apply Arden's Lemma
* A = 0A + 1B + 2C * A = 0A + 1B + 2C
* => A = 0*1B + 0*2C * => A = 0*1B + 0*2C
*/ */
regexp::UnboundedRegExpIteration loop( * m_eqTransition[ a ][ a ] ); regexp::UnboundedRegExpIteration loop( std::move( m_eqTransition[ a ][ a ] ) );
   
// for all transitions from A apply Arden's Lemma // for all transitions from A apply Arden's Lemma
for( auto itB = std::next( itA ) ; itB != m_symbolsByDepth.rend( ); itB ++ ) for( auto itB = std::next( itA ) ; itB != m_symbolsByDepth.rend( ); itB ++ )
...@@ -48,13 +46,17 @@ regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void ) ...@@ -48,13 +46,17 @@ regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void )
const alphabet::Symbol & b = * itB; const alphabet::Symbol & b = * itB;
regexp::UnboundedRegExpConcatenation concat; regexp::UnboundedRegExpConcatenation concat;
concat.appendElement( loop ); concat.appendElement( loop );
concat.appendElement( * m_eqTransition[ a ][ b ] ); concat.appendElement( std::move( m_eqTransition[ a ][ b ] ) );
m_eqTransition[ a ][ b ] = /* opt.optimize( */ concat.clone() /* ) */; regexp::UnboundedRegExpAlternation alt;
alt.appendElement( std::move( concat ) );
m_eqTransition[ a ][ b ] = std::move( alt );
} }
regexp::UnboundedRegExpConcatenation concat; regexp::UnboundedRegExpConcatenation concat;
concat.appendElement( loop ); concat.appendElement( std::move( loop ) );
concat.appendElement( * m_eqFinal[ a ] ); concat.appendElement( std::move( m_eqFinal[ a ] ) );
m_eqFinal[ a ] = /* opt.optimize( */ concat.clone() /* ) */; regexp::UnboundedRegExpAlternation alt;
alt.appendElement( std::move( concat ) );
m_eqFinal[ a ] = std::move( alt );
   
/* /*
* eliminate A from rest of the equations using this pattern: * eliminate A from rest of the equations using this pattern:
...@@ -68,33 +70,26 @@ regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void ) ...@@ -68,33 +70,26 @@ regexp::UnboundedRegExpElement* RightRegularEquationSolver::eliminate( void )
{ {
const alphabet::Symbol & c = * itC; const alphabet::Symbol & c = * itC;
   
regexp::UnboundedRegExpAlternation alt;
regexp::UnboundedRegExpConcatenation concat; regexp::UnboundedRegExpConcatenation concat;
concat.appendElement( * m_eqTransition[ b ][ a ] ); concat.appendElement( m_eqTransition[ b ][ a ] );
concat.appendElement( * m_eqTransition[ a ][ c ] ); concat.appendElement( m_eqTransition[ a ][ c ] );
alt.appendElement( * m_eqTransition[ b ][ c ] ); regexp::UnboundedRegExpAlternation alt;
alt.appendElement( concat ); alt.appendElement( std::move( m_eqTransition[ b ][ c ] ) );
m_eqTransition[ b ][ c ] = /*opt.optimize( */ alt.clone() /* ) */; alt.appendElement( std::move( concat ) );
m_eqTransition[ b ][ c ] = /* opt.optimize( */ std::move( alt ) /* ) */;
} }
   
regexp::UnboundedRegExpAlternation alt;
regexp::UnboundedRegExpConcatenation concat; regexp::UnboundedRegExpConcatenation concat;
concat.appendElement( * m_eqTransition[ b ][ a ] ); concat.appendElement( std::move( m_eqTransition[ b ][ a ] ) );
concat.appendElement( * m_eqFinal[ a ] ); concat.appendElement( m_eqFinal[ a ] );
alt.appendElement( * m_eqFinal[ b ] ); regexp::UnboundedRegExpAlternation alt;
alt.appendElement( concat ); alt.appendElement( std::move( m_eqFinal[ b ] ) );
m_eqFinal[ b ] = /* opt.optimize( */ alt.clone() /* ) */; alt.appendElement( std::move( concat ) );
m_eqFinal[ b ] = /* opt.optimize( */ std::move( alt ) /* ) */;
} }
// no need for this now, release some memory
for( const auto & kv : m_eqTransition[ a ] )
delete kv.second;
} }
   
regexp::UnboundedRegExpElement* result = m_eqFinal[ * m_symbolsByDepth.begin( ) ]; return m_eqFinal[ * m_symbolsByDepth.begin( ) ].clone();
m_eqFinal[ * m_symbolsByDepth.begin( ) ] = NULL;
return result;
} }
   
} /* namespace equations */ } /* namespace equations */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment