From 5c322f292c98e7995ca207bd84132b61dfade6e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <peckato1@fit.cvut.cz>
Date: Fri, 25 Apr 2014 22:29:49 +0200
Subject: [PATCH] aepsilon: math...

---
 aepsilon/src/EpsilonClosure.cpp | 23 ++++--------
 aepsilon/src/EpsilonClosure.h   |  9 +----
 aepsilon/src/EpsilonRemover.cpp | 63 ++++++++++++---------------------
 aepsilon/src/EpsilonRemover.h   | 13 +------
 aepsilon/src/aepsilon.fsm.cpp   |  3 +-
 5 files changed, 32 insertions(+), 79 deletions(-)

diff --git a/aepsilon/src/EpsilonClosure.cpp b/aepsilon/src/EpsilonClosure.cpp
index 051a1f8583..abcedaaa90 100644
--- a/aepsilon/src/EpsilonClosure.cpp
+++ b/aepsilon/src/EpsilonClosure.cpp
@@ -14,18 +14,13 @@ using namespace std;
 namespace epsilon
 {
 
-EpsilonClosure::EpsilonClosure( const FSM & fsm ) : m_fsm( fsm )
-{
-    for( const auto & q : fsm.getStates( ) )
-        closure( q );
-}
-
-void EpsilonClosure::closure( const State & q )
+set<State> EpsilonClosure::closure( FSM const& fsm, State const& q )
 {
+    set<State> closure;
     queue<State> queue;
     map<State, bool> visited;
 
-    for( const auto & p : m_fsm.getStates( ) )
+    for( const auto & p : fsm.getStates( ) )
         visited[ p ] = false;
 
     queue.push( q );
@@ -34,20 +29,14 @@ void EpsilonClosure::closure( const State & q )
         const State & p = queue.front( );
         queue.pop( );
         visited[ p ] = true;
-        m_closure[ q ].insert( p );
+        closure.insert( p );
 
-        for( const auto & transition : m_fsm.getTransitionsFromState( p ) )
+        for( const auto & transition : fsm.getTransitionsFromState( p ) )
             if( transition.getInput( ) == Symbol( "" ) && visited [ transition.getTo( ) ] == false )
                 queue.push( transition.getTo( ) );
     }
-}
-
-const set<State> & EpsilonClosure::getClosure( const State & state ) const
-{
-    if( m_closure.find( state ) == m_closure.end( ) )
-        throw AlibException( "EpsilonClosure: Trying to get closure of state not in automata." );
 
-    return m_closure.at( state );
+    return closure;
 }
 
 } /* namespace epsilon */
diff --git a/aepsilon/src/EpsilonClosure.h b/aepsilon/src/EpsilonClosure.h
index f119d7aa5c..9d277f7018 100644
--- a/aepsilon/src/EpsilonClosure.h
+++ b/aepsilon/src/EpsilonClosure.h
@@ -23,14 +23,7 @@ namespace epsilon
 class EpsilonClosure
 {
 public:
-    EpsilonClosure( const automaton::FSM & fsm );
-    const std::set<automaton::State> & getClosure( const automaton::State & state ) const;
-
-private:
-    void closure( const automaton::State & state );
-
-    std::map<const automaton::State, std::set<automaton::State>> m_closure;
-    const automaton::FSM & m_fsm;
+    static std::set<automaton::State> closure( automaton::FSM const& fsm, automaton::State const& state );
 };
 
 } /* namespace epsilon */
diff --git a/aepsilon/src/EpsilonRemover.cpp b/aepsilon/src/EpsilonRemover.cpp
index 54029ee257..da6ebcf2b5 100644
--- a/aepsilon/src/EpsilonRemover.cpp
+++ b/aepsilon/src/EpsilonRemover.cpp
@@ -13,55 +13,36 @@ using namespace automaton;
 namespace epsilon
 {
 
-EpsilonRemover::EpsilonRemover( const FSM & fsm ) : m_origFSM( fsm )
+FSM EpsilonRemover::remove( FSM const& origFSM )
 {
-    m_closure = new EpsilonClosure( fsm );
+    FSM fsm;
 
-    for( const auto & state : fsm.getStates() )
-        m_fsm.addState( state );
+    for( const auto & state : origFSM.getStates() )
+        fsm.addState( state );
 
-    for( const auto & state : fsm.getInitialStates() )
-        m_fsm.addInitialState( state );
+    for( const auto & state : origFSM.getInitialStates() )
+        fsm.addInitialState( state );
 
-    for( const auto & symbol : fsm.getInputAlphabet() )
-        m_fsm.addInputSymbol( symbol );
-}
-
-EpsilonRemover::~EpsilonRemover( void )
-{
-    delete m_closure;
-}
-
-const FSM EpsilonRemover::remove( void )
-{
-    setTransitions( );
-    setFinalStates( );
-
-    return m_fsm;
-}
+    for( const auto & symbol : origFSM.getInputAlphabet() )
+        fsm.addInputSymbol( symbol );
 
-/**
- * Step 1 from Melichar 2.41
- */
-void EpsilonRemover::setTransitions( void )
-{
-    for( const auto & q : m_fsm.getStates( ) )
-        for( const auto & p : m_closure->getClosure( q ) )
-            for( const auto & transition : m_origFSM.getTransitionsFromState( p ) )
+    /**
+     * Step 1 from Melichar 2.41
+     */
+    for( const auto & q : origFSM.getStates( ) )
+        for( const auto & p : EpsilonClosure::closure( origFSM, q ) )
+            for( const auto & transition : origFSM.getTransitionsFromState( p ) )
                 if( transition.getInput() != Symbol( "" ) )
-                    m_fsm.addTransition( q, transition.getInput( ), transition.getTo( ) );
-}
+                    fsm.addTransition( q, transition.getInput( ), transition.getTo( ) );
 
-/**
- * Step 2 from Melichar 2.41
- */
-void EpsilonRemover::setFinalStates( void )
-{
+    /**
+     * Step 2 from Melichar 2.41
+     */
     set<State> finalStates;
 
-    for( const auto & q : m_fsm.getStates( ) )
+    for( const auto & q : fsm.getStates( ) )
     {
-        const set<State> & cl = m_closure->getClosure( q ), & F = m_origFSM.getFinalStates( );
+        const set<State> & cl = EpsilonClosure::closure( origFSM, q ), & F = origFSM.getFinalStates( );
         set<State> intersect;
 
         set_intersection( cl.begin(), cl.end(), F.begin(), F.end(), std::inserter( intersect, intersect.begin() ) );
@@ -70,7 +51,9 @@ void EpsilonRemover::setFinalStates( void )
     }
 
     for( const auto & q : finalStates )
-        m_fsm.addFinalState( q );
+        fsm.addFinalState( q );
+
+    return fsm;
 }
 
 } /* namespace epsilon */
diff --git a/aepsilon/src/EpsilonRemover.h b/aepsilon/src/EpsilonRemover.h
index 4e8899656d..b7696b84d5 100644
--- a/aepsilon/src/EpsilonRemover.h
+++ b/aepsilon/src/EpsilonRemover.h
@@ -22,18 +22,7 @@ namespace epsilon
 class EpsilonRemover
 {
 public:
-    EpsilonRemover( const automaton::FSM & fsm );
-    ~EpsilonRemover( void );
-    const automaton::FSM remove( void );
-
-private:
-    void setTransitions( void );
-    void setFinalStates( void );
-
-    const automaton::FSM & m_origFSM;
-    automaton::FSM m_fsm;
-
-    EpsilonClosure * m_closure;
+    static automaton::FSM remove( automaton::FSM const& );
 };
 
 } /* namespace epsilon */
diff --git a/aepsilon/src/aepsilon.fsm.cpp b/aepsilon/src/aepsilon.fsm.cpp
index f4ab4bf828..63dabaf934 100644
--- a/aepsilon/src/aepsilon.fsm.cpp
+++ b/aepsilon/src/aepsilon.fsm.cpp
@@ -56,8 +56,7 @@ int main(int argc, char** argv) {
         UnknownAutomaton automaton = AutomatonParser::parse( tokens );
         FSM fsm = AutomatonFactory::buildFSM( automaton );
 
-        EpsilonRemover epsrem ( fsm );
-        epsrem.remove().toXML( cout );
+        EpsilonRemover::remove( fsm ).toXML( cout );
 
     } catch (AlibException& e) {
         cout << e.what() << endl;
-- 
GitLab