FSARep.h

Go to the documentation of this file.
00001 // Copyright (C) 2005 Alexander Ushakov
00002 // Contents: Definition of class FSARep
00003 //
00004 // Principal Authors: Alexander Ushakov
00005 //
00006 // Revision History:
00007 //
00008 
00009 #ifndef _FSARep_h_
00010 #define _FSARep_h_
00011 
00012 
00013 #include "RefCounter.h"
00014 
00015 #include "map"
00016 #include "set"
00017 #include "list"
00018 #include "vector"
00019 #include <stdlib.h>
00020 
00021 using namespace std;
00022 
00023 
00024 //---------------------------------------------------------------------------//
00025 //-------------------------------- FSAEdge ----------------------------------//
00026 //---------------------------------------------------------------------------//
00027 
00028 
00029 struct FSAEdge 
00030 {
00031 
00032   FSAEdge( ) : target(-1), label(0) { }
00033   // dummy constructor, not to be used, required for STL::map
00034 
00035   FSAEdge( int t , int l ) : target(t), label(l) { }
00036 
00037   bool operator ==( const FSAEdge& e ) const {
00038     return target==e.target && label==e.label;
00039   }
00040 
00041   bool operator < ( const FSAEdge& e ) const {
00042     if( label<e.label )
00043       return true;
00044     if( label>e.label )
00045       return false;
00046 
00047     if( target<e.target )
00048       return true;
00049     return false;
00050   }
00051 
00052   int target;
00053   int label;
00054 };
00055 
00056 
00057 //---------------------------------------------------------------------------//
00058 //------------------------------- FSAState ----------------------------------//
00059 //---------------------------------------------------------------------------//
00060 
00061 
00062 struct FSAState 
00063 {
00064   typedef FSAEdge edge_type;
00065   
00066   FSAState( int s ) : theState(s) { }
00067   FSAState( ) : theState(0) { }
00068   // doomy constructor for class map
00069   
00070   bool operator== ( const FSAState& s ) const { 
00071     return theState==s.theState; 
00072   }
00073   bool operator< ( const FSAState& s ) const { 
00074     return theState<s.theState; 
00075   }
00076 
00077   set< edge_type > in;
00078   set< edge_type > out;
00079   int theState;
00080 };
00081 
00082 
00083 //---------------------------------------------------------------------------//
00084 //------------------------------- FSAState ----------------------------------//
00085 //---------------------------------------------------------------------------//
00086 
00087 
00088 struct FoldDetails
00089 {
00090         FoldDetails( int b , int l , int n , const FSAState& s1 , const FSAState& s2 ) :
00091                 base(b), label(l), state_num( n ), state1(s1), state2(s2) { }
00092 
00093         int base;
00094         int label;
00095         int state_num;
00096         FSAState state1;
00097         FSAState state2;
00098 };
00099 
00100 
00101 //---------------------------------------------------------------------------//
00102 //-------------------------------- FSARep -----------------------------------//
00103 //---------------------------------------------------------------------------//
00104 
00105 
00106 class FSARep : public RefCounter
00107 {
00108   friend class FSA;
00109 
00110   typedef FSAState::edge_type edge_type;
00111 
00113   //                                                     //
00114   //  Constructors                                       //
00115   //                                                     //
00117 
00118  private:
00119 
00120   FSARep( );
00121 
00122 
00124   //                                                     //
00125   //  Accessors:                                         //
00126   //                                                     //
00128 public:
00129 
00130   
00131   FSARep* clone( ) const { return new FSARep( *this ); }
00132 
00134   //                                                     //
00135   //  Subgroup graph functions:                          //
00136   //                                                     //
00138 public:
00139 
00141   void fold( const set< int >* candidates = NULL ,  list< FoldDetails >* details = NULL );
00142   
00144   void pinch( int state1 , int state2 );
00145   
00147   void unfold( const list< FoldDetails >& details );
00148   
00150   void liftup( const list< FoldDetails >& details , list< FSAEdge >& path , int init_state );
00151   
00153   //                                                     //
00154   //  Adding and removing elements                       //
00155   //                                                     //
00157 
00158 public:
00160   int    newState( );
00162   void eraseState( int state );
00164   void   newEdge( int state1 , int state2 , int label );
00166   void eraseEdge( int state1 , int state2 , int label );
00167   
00169   template< class ConstIntIterator > void addLoop( int vert  , ConstIntIterator F , ConstIntIterator L ) {
00170     if( F==L ) return;
00171     int cur_vert = vert;
00172     ConstIntIterator L2 = L;
00173     --L2;
00174     for( ConstIntIterator IT=F ; IT!=L2 ; ++IT ) {
00175       int new_vert = newState( );
00176       newEdge( cur_vert , new_vert ,  *IT );
00177       newEdge( new_vert , cur_vert , -*IT );
00178       cur_vert = new_vert;
00179     }
00180     
00181     newEdge( cur_vert , vert ,  *L2 );
00182     newEdge( vert , cur_vert , -*L2 );
00183   }
00184 
00186   template< class ConstIntIterator > void addRay ( int vert , ConstIntIterator F , ConstIntIterator L ) {
00187     int cur_vert = vert;
00188     for( ConstIntIterator IT=F ; IT!=L ; ++IT ) {
00189       int new_vert = newState( );
00190       newEdge( cur_vert , new_vert ,  *IT );
00191       newEdge( new_vert , cur_vert , -*IT );
00192       cur_vert = new_vert;
00193     }
00194   }
00195   
00197   void addFSA ( int vert1 , int vert2 , const FSARep& fsa ) { exit( 1 ); }
00198 
00199   const map< int , FSAState >& getStates( ) const { return theStates; }
00200         map< int , FSAState >& getStates( )       { return theStates; }
00201 
00203   //                                                     //
00204   //  Initial and terminal states:                       //
00205   //                                                     //
00207 public:
00208   void makeInitial    ( int s ) { initStates.insert( s ); }
00209   void makeTerminal   ( int s ) { termStates.insert( s ); }
00210   void makeNonInitial ( int s ) { initStates.erase ( s ); }
00211   void makeNonTerminal( int s ) { termStates.erase ( s ); }
00212   const set< int >& getInitStates( ) const { return initStates; }
00213   const set< int >& getTermStates( ) const { return termStates; }
00214 
00216   //                                                     //
00217   //  Data members                                       //
00218   //                                                     //
00220   
00221 private:
00222 
00223   // friend class AdvCPAlgorithm;
00224   
00225   map< int , FSAState > theStates;
00226   // here argument (int) coincide with FSAState.theState
00227 
00228   int maxState;
00229   
00230   set< int > initStates;
00231   // the set of initial states
00232   set< int > termStates;
00233   // the set of terminal states
00234 };
00235 
00237 void reducePath( list< FSAEdge >& path , int init_state );
00238 
00239 
00240 #endif
00241 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Mon Sep 26 18:43:45 2011 for CRyptography And Groups (CRAG) by  doxygen 1.6.1