default.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_DEFAULT_H
00023 #define ASTL_DEFAULT_H
00024 
00025 
00026 
00027 namespace astl {
00028 
00029 //
00030 // A def_trans_cursor (default transition cursor) uses the transition labelled 
00031 // with default_letter when the required transition is undefined.
00032 template <typename Cursor>
00033 class def_trans_cursor : public Cursor
00034 {
00035 public:
00036   typedef Cursor                     super;
00037   typedef def_trans_cursor           self;
00038   typedef typename super::char_type  char_type;
00039   typedef typename super::state_type state_type;
00040 
00041   def_trans_cursor()
00042   { }
00043 
00044   def_trans_cursor(const Cursor &x, const char_type& default_letter)
00045     : super(x), def_letter(default_letter)
00046   { }
00047 
00048   bool forward(const char_type &a) {
00049     if (exists(a)) {
00050       super::forward(a);
00051       return true;
00052     }
00053     return super::forward(def_letter);
00054   }
00055 
00056   self& operator=(const state_type &p) {
00057     super::operator=(p);
00058     return *this;
00059   }
00060 
00061 protected:
00062   typename Cursor::char_type def_letter;
00063 };
00064 
00065 // A def_state_cursor (default state cursor) moves to default state
00066 // when required transition is undefined (makes the DFA complete and
00067 // make the default state the new sink state).
00068 // Requirements:
00069 // the default state is an existing state
00070 // Warning: this makes the automaton cyclic
00071 template <class ForwardC>
00072 class def_state_cursor : public ForwardC
00073 {
00074 public:
00075   typedef typename ForwardC::state_type  state_type;
00076   typedef typename ForwardC::char_type   char_type;
00077   typedef typename ForwardC::char_traits char_traits;
00078   typedef ForwardC                       super;
00079   typedef def_state_cursor               self;
00080   
00081   def_state_cursor()
00082   { }
00083 
00084   def_state_cursor(const ForwardC &x, const state_type &default_state)
00085     : super(x), def_state(default_state)
00086   { }
00087 
00088   void forward() { super::forward(); }
00089 
00090   bool forward(const char_type &letter) {
00091     if (!super::forward(letter))
00092       *this = def_state;
00093     return true;
00094   }
00095 
00096   self& operator=(const state_type &x) {
00097     super::operator=(x);
00098     return *this;
00099   }
00100 
00101 protected:
00102   state_type def_state;
00103 };
00104 
00105 // A substitute_cursor follows a default transition when the required
00106 // transition is undefined and tries to move forward from this state
00107 // again. (cf. Aho & Corasick search algorithm). 
00108 template <typename ForwardC>
00109 class substitute_cursor : public ForwardC
00110 {
00111 public:
00112   typedef ForwardC                    super;
00113   typedef substitute_cursor           self;
00114   typedef typename super::char_type   char_type;
00115   typedef typename super::state_type  state_type;
00116   typedef typename super::tag_type    tag_type;
00117   typedef typename super::char_traits char_traits;
00118   
00119   substitute_cursor()
00120   { }
00121 
00122   substitute_cursor(const ForwardC &x, const char_type &c)
00123     : super(x), a(c)
00124   { }
00125 
00126   void forward() { super::forward(); }
00127 
00128   bool forward(const char_type& letter) {
00129     do {
00130       if (super::find(letter)) {
00131         forward();
00132         return true;
00133       }
00134       if (super::find(a) && super::aim() == super::src())
00135           return true;
00136     } while (super::forward(a));
00137     return false;
00138   }
00139 
00140   self& operator=(const state_type &x) {
00141     super::operator=(x);
00142     return *this;
00143   }
00144 
00145 protected:
00146   typename super::char_type a;  // default transition label
00147 };
00148 
00149 template <typename Cursor>
00150 inline
00151 def_trans_cursor<Cursor> 
00152 def_transc(const Cursor &x, const typename Cursor::char_type& def_letter) {
00153   return def_trans_cursor<Cursor>(x, def_letter);
00154 }
00155 
00156 template <class ForwardC>
00157 inline
00158 def_state_cursor<ForwardC> 
00159 def_statec(const ForwardC &x, const typename ForwardC::state_type& p) {
00160   return def_state_cursor<ForwardC>(x, p);
00161 }
00162 
00163 template <class ForwardC>
00164 inline
00165 substitute_cursor<ForwardC> 
00166 substitutec(const ForwardC &x, const typename ForwardC::char_type &c) {
00167   return substitute_cursor<ForwardC>(x, c);
00168 } 
00169 
00170 } // namespace astl
00171 
00172 #endif // ASTL_DEFAULT_H
00173 

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