SIS
Symmetric Index Structures
|
00001 #ifndef COMPRESSEDAUTOMATONADAPTER_CPP 00002 #define COMPRESSEDAUTOMATONADAPTER_CPP 00003 00004 #include "CompressedAutomatonAdapter.hpp" 00005 00006 namespace lmu { namespace cis { namespace sis { 00007 00008 00009 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00010 void CompressedAutomatonAdapter::Free() { 00011 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::Free( )" << std::endl; 00012 00013 CompressedAutomatonFree( C_CompressedAutomaton ); 00014 } 00015 00016 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00017 PairState CompressedAutomatonAdapter::GetInitialPairState( ) const { 00018 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline PairState CompressedAutomatonAdapter::GetInitialPairState( ) const" << std::endl; 00019 assert( ManagedAutomatonStage_ > EMPTY ); 00020 return CompressedAutomatonGetInitialPairState( C_CompressedAutomaton ); 00021 } 00022 00023 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00024 void CompressedAutomatonAdapter::AddState( UINT state ) { 00025 if (DEBUG_LEVEL > 5) std::cerr << "inline void CompressedAutomatonAdapter::AddState( UINT state )" << std::endl; 00026 00027 CompressedAutomatonAddState( C_CompressedAutomaton, state ); 00028 ManagedAutomatonStage_ = UNSORTED; 00029 00030 } 00031 00032 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00033 void CompressedAutomatonAdapter::AddTransition( UINT stateFrom, UINT start, UINT stateTo ) { 00034 if (DEBUG_LEVEL > 5) std::cerr << "inline void CompressedAutomatonAdapter::AddTransition( UINT stateFrom, UINT start, UINT stateTo )" << std::endl; 00035 00036 CompressedAutomatonAddTransition( C_CompressedAutomaton, stateFrom, start, stateTo ); 00037 ManagedAutomatonStage_ = UNSORTED; 00038 00039 } 00040 00041 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00042 UINT CompressedAutomatonAdapter::Add( const VoidSequenceAdapter& 00043 sharpDocumentDollar ) 00044 { 00045 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::Add( const VoidSequenceAdapter& sharpDocumentDollar )" << std::endl; 00046 00047 assert( ManagedAutomatonStage_ < CLOSED ); 00048 00049 ManagedAutomatonStage_ = UNSORTED; 00050 00051 return Add( sharpDocumentDollar.get() ); 00052 } 00053 00054 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00055 UINT CompressedAutomatonAdapter::Add( const char* documentDollar ) { 00056 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::Add( const S8* documentDollar )" << std::endl; 00057 00058 assert( ManagedAutomatonStage_ < CLOSED ); 00059 00060 ManagedAutomatonStage_ = UNSORTED; 00061 00062 VoidSequenceAdapter v(C_symbolsize); 00063 00064 v.Add(documentDollar); 00065 00066 return Add( v.get() ); 00067 } 00068 00069 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00070 UINT CompressedAutomatonAdapter::Add( const std::string& word ) { 00071 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::Add( const std::string& word )" << std::endl; 00072 00073 assert( ManagedAutomatonStage_ < CLOSED ); 00074 00075 ManagedAutomatonStage_ = UNSORTED; 00076 00077 return Add( word.c_str() ); 00078 } 00079 00080 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00081 void CompressedAutomatonAdapter::AddDictionary( std::istream& in) { 00082 assert( ManagedAutomatonStage_ < CLOSED ); 00083 00084 ManagedAutomatonStage_ = UNSORTED; 00085 00086 while (in.good()) 00087 ReadAll(in); // TODO Clean up: Isn't there an interface by now like in >> automaton? 00088 } 00089 00090 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00091 void CompressedAutomatonAdapter::Shrink() { 00092 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::Shrink( )" << std::endl; 00093 00094 assert( ManagedAutomatonStage_ < CLOSED ); 00095 00096 CompressedAutomatonShrink( C_CompressedAutomaton ); 00097 00098 ManagedAutomatonStage_ = SHRUNK; 00099 00100 } 00101 00102 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00103 void CompressedAutomatonAdapter::Write( FILE * fp ) const { 00104 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::Write( FILE * fp ) const" << std::endl; 00105 00106 assert( ManagedAutomatonStage_ == CLOSED ); 00107 00108 CompressedAutomatonWrite( C_CompressedAutomaton, fp ); 00109 } 00110 00111 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00112 void CompressedAutomatonAdapter::Write( const S8* fn ) const { 00113 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::Write( const S8* fn ) const" << std::endl; 00114 00115 assert( ManagedAutomatonStage_ == CLOSED ); 00116 00117 FILE * fpOut = Fopen(fn, "wb"); 00118 CompressedAutomatonWrite( C_CompressedAutomaton, fpOut ); 00119 Fclose(fpOut); 00120 } 00121 00122 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00123 void CompressedAutomatonAdapter::DumpStat( FILE * fp ) const { 00124 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::DumpStat( FILE * fp ) const" << std::endl; 00125 00126 assert( ManagedAutomatonStage_ >= SORTED ); 00127 00128 CompressedAutomatonDumpStat( C_CompressedAutomaton, fp ); 00129 } 00130 00131 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00132 void CompressedAutomatonAdapter::DumpStat( const S8* fn ) const { 00133 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::DumpStat( const S8* fn ) const" << std::endl; 00134 00135 assert( ManagedAutomatonStage_ >= SORTED ); 00136 00137 FILE * fpOut = Fopen(fn, "wb"); 00138 CompressedAutomatonDumpStat( C_CompressedAutomaton, fpOut); 00139 Fclose(fpOut); 00140 } 00141 00142 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00143 void CompressedAutomatonAdapter::GenerateLanguage( const S8* fn, 00144 UINT encoding ) const 00145 { 00146 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::GenerateLanguage( const S8* fn, UINT encoding = ENCODING_PLAIN ) const" << std::endl; 00147 00148 assert( ManagedAutomatonStage_ >= SORTED ); 00149 00150 FILE * fpOut = Fopen(fn, "wb"); 00151 CompressedAutomatonGenerateLanguage( C_CompressedAutomaton, fpOut, DumpSequenceOfLabels, encoding ); 00152 Fclose( fpOut ); 00153 } 00154 00155 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00156 void CompressedAutomatonAdapter::LeftAutomatonGenerateLanguage( FILE * fp, 00157 void (*DumpSequenceOfLabels)(FILE *, const VoidSequence *, UINT), 00158 UINT encoding ) const 00159 { 00160 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::LeftAutomatonGenerateLanguage( FILE * fp, void (*DumpSequenceOfLabels)(FILE *, const VoidSequence *, UINT), UINT encoding ) const" << std::endl; 00161 00162 assert( ManagedAutomatonStage_ >= SORTED ); 00163 00164 CompressedLeftAutomatonGenerateLanguage( C_CompressedAutomaton, fp, 00165 DumpSequenceOfLabels, encoding ); 00166 } 00167 00168 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00169 void CompressedAutomatonAdapter::AddTarjanTable( ){ 00170 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::AddTarjanTable( )" << std::endl; 00171 00172 assert( ManagedAutomatonStage_ >= SORTED ); 00173 00174 CompressedAutomatonAddTarjanTable( C_CompressedAutomaton ); 00175 } 00176 00177 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00178 PairState CompressedAutomatonAdapter::Delta( const PairState* state, const void* symbol ) const { 00179 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline PairState CompressedAutomatonAdapter::Delta( const PairState * state, const void * symbol ) const" << std::endl; 00180 00181 assert( ManagedAutomatonStage_ >= SORTED ); 00182 00183 return CompressedAutomatonDelta( C_CompressedAutomaton, state, symbol ); 00184 } 00185 00186 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00187 PairState CompressedAutomatonAdapter::Delta( const PairState * state, const char symbol ) const { 00188 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline PairState CompressedAutomatonAdapter::Delta( const PairState * state, const void * symbol ) const" << std::endl; 00189 00190 assert( ManagedAutomatonStage_ >= SORTED ); 00191 00192 VoidSequenceAdapter v; //TODO Cleanup 00193 v.Add( &symbol ); 00194 return Delta(state, v.get()); 00195 } 00196 00197 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00198 PairState CompressedAutomatonAdapter::Delta( const void * symbol ) const { 00199 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline PairState CompressedAutomatonAdapter::Delta( const PairState * state, const void * symbol ) const" << std::endl; 00200 00201 assert( ManagedAutomatonStage_ >= SORTED ); 00202 00203 PairState from = GetInitialPairState(); 00204 if (DEBUG_LEVEL > 5) std::cerr << "States Stored: " << C_CompressedAutomaton->statesTransitions->seqStored << std::endl; 00205 return Delta( &from, symbol ); 00206 } 00207 00208 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00209 PairState CompressedAutomatonAdapter::Delta( const S8* pattern ) const { 00210 if (DEBUG_LEVEL > 5) std::cerr << "PairState CompressedAutomatonAdapter::Delta( const S8* pattern ) const" << std::endl; 00211 00212 assert( ManagedAutomatonStage_ >= SORTED ); 00213 00214 VoidSequenceAdapter v; 00215 v.Add( pattern ); 00216 00217 return Delta(v.get()); 00218 } 00219 00220 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00221 void CompressedAutomatonAdapter::DumpGV( FILE * fp, 00222 void (*DumpLabel)(const CompressedAutomaton *, FILE *, UINT, UINT), 00223 UINT encoding ) 00224 { 00225 if (DEBUG_LEVEL > 5) std::cerr << "virtual inline void CompressedAutomatonAdapter::DumpGV( FILE * fp, void (*DumpLabel)(const CompressedAutomaton *, FILE *, UINT, UINT), UINT encoding )" << std::endl; 00226 00227 assert( ManagedAutomatonStage_ >= SORTED ); 00228 00229 CompressedAutomatonDumpGV( C_CompressedAutomaton, fp, DumpLabel, encoding ); 00230 } 00231 00232 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00233 UINT CompressedAutomatonAdapter::number_of_states() const { 00234 assert( ManagedAutomatonStage_ > EMPTY ); 00235 00236 return automaton()->statesTransitions->seqStored; 00237 } 00238 00239 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00240 UINT CompressedAutomatonAdapter::number_of_transitions() const { 00241 assert( ManagedAutomatonStage_ > EMPTY ); 00242 00243 return automaton()->transitionsStart->seqStored; 00244 } 00245 00246 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00247 UINT CompressedAutomatonAdapter::number_of_transitions(State s) const { 00248 assert( ManagedAutomatonStage_ > EMPTY ); 00249 00250 return automaton()->statesNumberOfTransitions->seq[s]; 00251 } 00252 00253 00255 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00256 void CompressedAutomatonAdapter::Print( std::ostream& os) const { 00257 os << number_of_transitions(); //TODO! For now, just give ouput. CompressedAutomatonGenerateLanguage() ? 00258 } 00259 00260 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00261 void CompressedAutomatonAdapter::ReadAll( std::istream& in) { 00262 std::string tmp; 00263 while(in.good()) { 00264 in >> tmp; 00265 Add(tmp); 00266 } 00267 ManagedAutomatonStage_ = UNSORTED; 00268 00269 } 00270 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00271 void CompressedAutomatonAdapter::ReadOne( std::istream& in) { 00272 std::string tmp; 00273 in >> tmp; 00274 Add(tmp); 00275 ManagedAutomatonStage_ = UNSORTED; 00276 00277 } 00278 00279 }}} /* End of namespace lmu::cis::sis */ 00280 00281 #endif /* end of include guard: COMPRESSEDAUTOMATONADAPTER_CPP */