dfa_hash.h

00001 /*
00002  * ASTL - the Automaton Standard Template Library.
00003  * C++ generic components for Finite State Automata handling.
00004  * Copyright (C) 2000-2003 Vincent Le Maout (vincent.lemaout@chello.fr).
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef ASTL_DFA_HASH_H
00023 #define ASTL_DFA_HASH_H
00024 
00025 // Descrition:  
00026 //   ASTL 2.0 Determinisic Finite Automaton Class Template DFA_hash
00027 //   Representation by a unique hash table
00028 //   This class implements a restricted subset of the DFA requirements
00029 //   (no tags, no edges traversal, no state/transition deletions)   
00030 
00031 #ifdef __GNUG__
00032 #if __GNUG__ < 4
00033 #include <ext/hash_map>
00034 #define HASH_TABLE_TYPE __gnu_cxx::hash_map
00035 #else
00036 #include <tr1/unordered_map>
00037 #define HASH_TABLE_TYPE tr1::unordered_map
00038 #endif
00039 #include <utility>  // pair<>
00040 #include <functional>
00041 
00042 
00043 namespace astl {
00044 
00045 template <class CharTraits = plain>
00046 class DFA_hash : public DFA_concept
00047 {
00048 public:
00049   typedef CharTraits char_traits;
00050   typedef typename char_traits::char_type char_type;
00051   typedef empty_tag tag_type;
00052   typedef size_t state_type;
00053 
00054 protected:
00055   typedef pair<state_type, char_type> key;
00056   typedef state_type                 data;
00057   struct hash_function 
00058     : public unary_function<pair<state_type, char_type>, size_t>
00059   {
00060     size_t operator() (const pair<state_type, char_type> &x) const {
00061       return x.first * 1023 + char_traits::to_int_type(x.second);
00062     }
00063   };
00064 
00065   typedef HASH_TABLE_TYPE<key, data, 
00066                           hash_function, equal_to<key> > hasher_type;
00067   hasher_type hasher;
00068 
00069   unsigned long state_count_;
00070   state_type    current;
00071   state_type    I;
00072   vector<char>  F;
00073   tag_type      bogus;
00074 
00075 public:
00076   static const state_type null_state = 0;
00077 
00078   DFA_hash(unsigned long = 0)
00079     : state_count_(0), current(0), 
00080       I(0), F(1, '\0')
00081   { }
00082 
00083   state_type new_state() {
00084     ++state_count_;
00085     F.push_back('\0');
00086     return ++current;
00087   }
00088 
00089   template <class OutputIterator>
00090   OutputIterator new_state(unsigned long how_many, OutputIterator x) {
00091     for(; how_many > 0; --how_many)
00092       *x++ = new_state();
00093     return x;
00094   }
00095 
00096   bool final(state_type s) const {
00097     return F[s] != '\0';
00098   }
00099 
00100   char& final(state_type s) {
00101     return F[s];
00102   }
00103 
00104   state_type initial() const { 
00105     return (I); 
00106   }
00107 
00108   void initial(state_type s) { 
00109     I = s; 
00110   }
00111 
00112   void set_trans(state_type s, const char_type &a, state_type aim) {
00113     hasher[make_pair(s, a)] = aim;
00114   }
00115 
00116   void del_trans(state_type s, const char_type &a) {
00117     hasher.erase(make_pair(s, a));
00118   }
00119 
00120   void change_trans(state_type s, const char_type &a, state_type new_aim) {
00121     hasher[make_pair(s, a)] = new_aim;
00122   }
00123 
00124   state_type delta1(state_type s, const char_type &a) const {
00125     typename hasher_type::const_iterator i = hasher.find(make_pair(s, a));
00126     return i == hasher.end() ? null_state : i->second;
00127   }
00128 
00129   unsigned long state_count() const { 
00130     return (state_count_); 
00131   } 
00132 
00133   unsigned long trans_count() const { 
00134     return hasher.size();
00135   }
00136 
00137   tag_type& tag(state_type) {
00138     return bogus;
00139   }
00140         
00141   const tag_type& tag(state_type) const {
00142     return bogus;
00143   }
00144 };
00145 
00146 template <class T>
00147 const typename DFA_hash<T>::state_type DFA_hash<T>::null_state;
00148 
00149 
00150 } // namespace astl
00151 
00152 #endif // _GNUG_
00153 
00154 #endif // ASTL_CLASS_DFA_HASH   
00155         
00156         

Generated on Sun Mar 8 02:41:32 2009 for ASTL by  doxygen 1.5.7.1