From 2b4ba1045af36515c5c29579567532baf4138809 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <peckato1@fit.cvut.cz>
Date: Mon, 3 Mar 2014 18:08:00 +0100
Subject: [PATCH] derivation method, variable renames etc

---
 aconversions/src/re2fa/Brzozowski.cpp | 54 ++++++++++++++-------------
 aconversions/src/re2fa/Brzozowski.h   |  6 +--
 2 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/aconversions/src/re2fa/Brzozowski.cpp b/aconversions/src/re2fa/Brzozowski.cpp
index 6736176ac6..280c0dfd37 100644
--- a/aconversions/src/re2fa/Brzozowski.cpp
+++ b/aconversions/src/re2fa/Brzozowski.cpp
@@ -30,37 +30,39 @@ FSM Brzozowski::convert( void )
 
     RegExp V = opt.optimize( m_re );
     set<RegExpSymbol> alphabet = RegExpAlphabet::getSymbols( m_re );
-    set<RegExp> Q = { V }, Qprev = { V }, Qcurr;
-    // In practice, only two Q_i sets are needed, the previous one and current. No need for having Q_1, Q_2, ... Q_n
 
-    while( true )
+    set<RegExp> Q = { V };
+    deque<set<RegExp>> Qi;
+
+    Qi.push_back( set<RegExp>( ) );
+    Qi.at( 0 ).insert( V );
+
+    int i = 1;
+    while( ! Qi.at( i - 1 ).empty( ) )
     {
-        for( const auto & regexp : Qprev )
+        Qi.push_back( set<RegExp>( ) ); // initialize set Q_i
+
+        for( const auto & regexp : Qi.at( i - 1 ) )
         {
             RegExpDerivation deriv( regexp );
 
-            for( const auto & symbol : alphabet )
+            for( const auto & a : alphabet )
             {
-                RegExpElement* dSymbol = new RegExpSymbol( symbol.getSymbol( ) );
+                RegExpElement* dSymbol = new RegExpSymbol( a.getSymbol( ) );
                 RegExp derived = deriv.derivation( list<RegExpElement*>( 1, dSymbol ) );
                 derived = opt.optimize( derived );
 
                 if( ! isInSet( derived, Q ) ) // if this state has already been found, do not add
-                    Qcurr.insert( derived );
+                    Qi.at( i ).insert( derived );
 
-                m_transitions.insert( Transition( regexp, symbol, derived ) );
+                m_transitions.insert( Transition( regexp, Symbol( a.getSymbol( ) ), derived ) );
 
                 delete dSymbol;
             }
         }
 
-        if( Qcurr.empty( ) )
-            break;
-
-        // i += 1
-        Q.insert( Qcurr.begin( ), Qcurr.end( ) );
-        Qprev = Qcurr;
-        Qcurr.clear( );
+        Q.insert( Qi.at( i ).begin( ), Qi.at( i ).end( ) );
+        i += 1;
     }
 
     // ------------------------------------------------------------------------
@@ -70,25 +72,25 @@ FSM Brzozowski::convert( void )
     for( const auto & r : Q )
         m_fsm.addState( builder.getState( r ) );
 
-    for( const auto & symbol : alphabet )
-        m_fsm.addInputSymbol( symbol.getSymbol( ) );
+    for( const auto & a : alphabet )
+        m_fsm.addInputSymbol( a.getSymbol( ) );
 
     for( const auto & t : m_transitions )
-        m_fsm.addTransition( TransitionFSM( builder.getState( t.m_from ), Symbol( t.m_regexpSymbol.getSymbol( ) ), builder.getState( t.m_to ) ) );
+        m_fsm.addTransition( builder.getState( t.m_from ), t.m_symbol, builder.getState( t.m_to ) );
 
-    m_fsm.addInitialState( builder.getState( m_re ) );
+    m_fsm.addInitialState( builder.getState( V ) );
 
-    for( const auto & r : Q )
-        if( r.containsEmptyString( ) )
-            m_fsm.addFinalState( builder.getState( r ) );
+    for( const auto & U : Q )
+        if( U.containsEmptyString( ) )
+            m_fsm.addFinalState( builder.getState( U ) );
 
     return m_fsm;
 }
 
 // ----------------------------------------------------------------------------
 
-Brzozowski::Transition::Transition( const RegExp & from, const RegExpSymbol & symb, const RegExp & to )
-    : m_from( from ), m_to( to ), m_regexpSymbol( symb )
+Brzozowski::Transition::Transition( const RegExp & from, const Symbol & symb, const RegExp & to )
+    : m_from( from ), m_to( to ), m_symbol( symb )
 {
 
 }
@@ -97,8 +99,8 @@ bool Brzozowski::Transition::operator<( const Transition & x ) const
 {
     if( m_from != x.m_from )
         return m_from < x.m_from;
-    else if( m_regexpSymbol != x.m_regexpSymbol )
-        return m_regexpSymbol < x.m_regexpSymbol;
+    else if( m_symbol != x.m_symbol )
+        return m_symbol < x.m_symbol;
     else
         return m_to < x.m_to;
 }
diff --git a/aconversions/src/re2fa/Brzozowski.h b/aconversions/src/re2fa/Brzozowski.h
index 2baf48c254..4ab0be4cfa 100644
--- a/aconversions/src/re2fa/Brzozowski.h
+++ b/aconversions/src/re2fa/Brzozowski.h
@@ -8,9 +8,9 @@
 #ifndef BRZOZOWSKI_H_
 #define BRZOZOWSKI_H_
 
-#include <algorithm>
 #include <map>
 #include <set>
+#include <deque>
 
 #include <automaton/State.h>
 #include <AlibException.h>
@@ -50,9 +50,9 @@ private:
     struct Transition
     {
         const regexp::RegExp m_from, m_to;
-        const regexp::RegExpSymbol m_regexpSymbol;
+        const alphabet::Symbol m_symbol;
 
-        Transition( const regexp::RegExp & from, const regexp::RegExpSymbol & symb, const regexp::RegExp & to );
+        Transition( const regexp::RegExp & from, const alphabet::Symbol & symb, const regexp::RegExp & to );
         bool operator<( const Transition & other ) const;
     };
 
-- 
GitLab