00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef ASTL_DFA_HASH_H
00023 #define ASTL_DFA_HASH_H
00024
00025
00026
00027
00028
00029
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>
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 }
00151
00152 #endif // _GNUG_
00153
00154 #endif // ASTL_CLASS_DFA_HASH
00155
00156