dfa_map.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_MAP_H
00023 #define ASTL_DFA_MAP_H
00024 
00025 
00026 #include <dfa_base.h>
00027 #include <map>            
00028 #include <vector>  
00029 #include <functional>   // binary_function<>       
00030 
00031 using namespace std;
00032 
00033 namespace astl {
00034 
00035 template <typename CharTraits>
00036 struct dfa_map_key_compare 
00037   : public binary_function<typename CharTraits::char_type, 
00038                            typename CharTraits::char_type, bool>
00039 {
00040   bool operator()(const typename CharTraits::char_type &x, 
00041                   const typename CharTraits::char_type &y) const {
00042     return CharTraits::lt(x, y);
00043   }
00044 };
00045 
00066 template <class CharTraits = plain,
00067           class Tag   = empty_tag>
00068 class DFA_map 
00069   : public DFA_base<CharTraits, Tag, 
00070                     map<typename CharTraits::char_type, 
00071                         unsigned int, dfa_map_key_compare<CharTraits> > > 
00072 {
00073 protected:
00074   typedef map<typename CharTraits::char_type, unsigned int, 
00075               dfa_map_key_compare<CharTraits> > transition_container;
00076 
00077 public:
00078   typedef DFA_base<CharTraits, Tag, transition_container> super;
00079   typedef CharTraits                          char_traits;
00080   typedef Tag                            tag_type;
00081   typedef typename CharTraits::char_type      char_type;
00082   typedef unsigned int                    state_type;
00083   using super::null_state;
00084   
00085   class edges_type
00086   {
00087     friend class DFA_map<CharTraits, Tag>;
00088   private:
00089     const transition_container *container;
00090     
00091   public:
00092     typedef typename transition_container::key_type        key_type;
00093     typedef unsigned int                                   data_type;
00094     typedef typename transition_container::value_type      value_type;
00095     typedef typename transition_container::key_compare     key_compare;
00096     typedef typename transition_container::const_reference const_reference;
00097     typedef typename transition_container::size_type       size_type;
00098     typedef typename transition_container::difference_type difference_type;
00099     typedef typename transition_container::value_compare   value_compare;
00100 
00101     typedef typename transition_container::const_iterator 
00102     const_iterator;
00103     typedef typename transition_container::const_reverse_iterator 
00104     const_reverse_iterator;
00105 
00106   protected:
00107     edges_type(const transition_container *c)
00108       : container(c) { }
00109 
00110   public:
00111     edges_type(const edges_type& x) : container(x.container) { }
00112     ~edges_type() { }
00113     
00114     // accessors:
00115     key_compare key_comp() const { return (container->key_comp()); } 
00116     value_compare value_comp() const { return (container->value_comp()); }
00117     const_iterator begin() const { return (container->begin()); }
00118     const_iterator end() const { return (container->end()); }
00119     const_reverse_iterator rbegin() const { return (container->rbegin()); }
00120     const_reverse_iterator rend() const { return (container->rend()); }
00121     bool empty() const { return (container->empty()); }
00122     size_type size() const { return (container->size()); }
00123     size_type max_size() const { return (container->max_size()); }
00124     
00125     // map operations:
00126     const_iterator find(const key_type& x) const { 
00127       return (container->find(x)); 
00128     }
00129     size_type count(const key_type& x) const { return (container->count(x)); }
00130     const_iterator lower_bound(const key_type& x) const { 
00131       return (container->lower_bound(x)); 
00132     }
00133     const_iterator upper_bound(const key_type& x) const { 
00134       return (container->upper_bound(x)); 
00135     }
00136     pair<const_iterator, const_iterator> equal_range(const key_type& x) const {
00137       return (container->equal_range(x));
00138     }
00139 
00140     // comparison:
00141     friend bool operator == (const edges_type& x, const edges_type& y) {
00142       return (x.container == y.container || *x.container == *y.container);
00143     }
00144   };
00145 
00146   // Backward compatibility
00147   typedef edges_type Edges;
00148   
00149   void del_trans(state_type s, const char_type &l)
00150   {
00151     Q[s]->edges().erase(l);
00152     --trans_count_;
00153   }
00154 
00155   void change_trans(state_type s, const char_type &l, state_type aim) {
00156     Q[s]->edges()[l] = aim;
00157   }
00158 
00159   state_type delta1(state_type s, const char_type &l) const
00160   {
00161     typename transition_container::iterator vi = Q[s]->edges().find(l);
00162     return vi == Q[s]->edges().end() ? null_state : (*vi).second;
00163   }
00164 
00165   void set_trans(state_type s, const char_type &l, state_type aim)
00166   {
00167     Q[s]->edges()[l] = aim;
00168     ++trans_count_;
00169   }
00170 
00171   edges_type delta2(state_type s) const { 
00172     return edges_type(&Q[s]->edges()); 
00173   }
00174 
00175   DFA_map(unsigned long n = 0) 
00176     : super(n)
00177   { }
00178 
00179 protected:
00180   using super::Q;
00181   using super::trans_count_;
00182 };
00183 
00184 } // namespace astl
00185 
00186 #endif   // ASTL_DFA_MAP_H
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 

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