str_cursor.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_STR_CURSOR_H
00023 #define ASTL_STR_CURSOR_H
00024 
00025 #include <astl.h>
00026 #include <string>
00027 
00028 using namespace std;
00029 
00030 namespace astl {
00031 
00032 template <class ForwardIterator>
00033 class string_cursor : public forward_cursor_concept
00034 {
00035 protected:
00036   ForwardIterator first, last;
00037   bool sink_state;
00038 
00039 public:
00040   typedef empty_tag       tag_type;
00041   typedef ForwardIterator state_type;
00042   typedef string_cursor   self;
00043   typedef typename iterator_traits<ForwardIterator>::value_type char_type;
00044   typedef std::char_traits<char_type> char_traits;
00045 
00046   typedef tag_type       Tag;
00047   typedef state_type      State;
00048   typedef char_type Alphabet;
00049 
00050   string_cursor(ForwardIterator first, ForwardIterator last)
00051     : first(first), last(last), sink_state(false)
00052     { }
00053 
00054   bool sink() const {
00055     return sink_state;
00056   }
00057 
00058   void forward() {
00059     ++first;
00060   }
00061 
00062   bool forward(const char_type &letter) {
00063     sink_state = !(*first == letter);
00064     ++first;
00065     return !sink_state;
00066   }
00067 
00068   bool first_transition() {
00069     return !(first == last);
00070   }
00071 
00072   bool next_transition() {
00073     return false;
00074   }
00075 
00076   char_type letter() const {
00077     return *first;
00078   }
00079 
00080   state_type src() const {
00081     return first;
00082   }
00083 
00084   state_type aim() const {
00085     ForwardIterator tmp = first;
00086     return ++tmp;
00087   }
00088 
00089   bool aim_final() const {
00090     ForwardIterator tmp = first;
00091     return ++tmp == last;
00092   }
00093   
00094   bool src_final() const {
00095     return first == last;
00096   }
00097 
00098   bool operator==(const self &x) const {
00099     return first == x.first;
00100   }
00101 
00102   bool find(const char_type &letter) {
00103     return exists(letter);
00104   }
00105 
00106   // Warning: x must be part of the [first, last) sequence:
00107   self& operator=(const ForwardIterator &x) { 
00108     first = x;
00109     return *this;
00110   }
00111 
00112   bool exists(const char_type &letter) const {
00113     return *first == letter;
00114   }
00115 };
00116 
00117 template <>
00118 class string_cursor<const char*> : public forward_cursor_concept
00119 {
00120 protected:
00121   const char *p;
00122 
00123 public:
00124   typedef empty_tag     tag_type;
00125   typedef const char*   state_type;
00126   typedef string_cursor self;
00127   typedef char          char_type;
00128   
00129   typedef tag_type       Tag;
00130   typedef state_type      State;
00131   typedef char_type Alphabet;
00132 
00133   string_cursor(const char *p_ = NULL)
00134     : p(p_)
00135     { }
00136 
00137   bool sink() const {
00138     return p == NULL;
00139   }
00140   
00141   bool exists(char letter) const {
00142     return *p == letter;
00143   }
00144 
00145   void forward() {
00146     ++p;
00147   }
00148 
00149   bool forward(char letter) {
00150     p = (*p == letter) ? p + 1 : NULL;
00151     return !sink();
00152   }
00153 
00154   bool first_transition() {
00155     return *p != 0;
00156   }
00157 
00158   bool next_transition() {
00159     return false;
00160   }
00161 
00162   char letter() const {
00163     return *p;
00164   }
00165 
00166   State src() const {
00167     return p;
00168   }
00169 
00170   State aim() const {
00171     return p + 1;
00172   }
00173 
00174   bool src_final() const {
00175     return *p == 0;
00176   }
00177 
00178   bool aim_final() const {
00179     return p[1] == 0;
00180   }
00181 
00182   bool operator==(const string_cursor<const char*> &x) const {
00183     return p == x.p;
00184   }
00185 
00186   bool find(char letter) {
00187     return exists(letter);
00188   }
00189   
00190   self& operator= (const char *c) {
00191     p = c;
00192     return *this;
00193   }
00194 };
00195 
00196 template <typename InputIterator>
00197 inline
00198 string_cursor<InputIterator> stringc(InputIterator first, InputIterator last) {
00199   return string_cursor<InputIterator>(first, last);
00200 }
00201 
00202 inline
00203 string_cursor<const char*> stringc(const char *s) {
00204   return string_cursor<const char*>(s);
00205 }
00206 
00207 inline
00208 string_cursor<const char*> stringc(char *s) {
00209   return string_cursor<const char*>(s);
00210 }
00211 #endif 
00212 
00213 } // namespace astl
00214 
00215 #endif 
00216 
00217 
00218 
00219 
00220 
00221 
00222 
00223 

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