alphabet.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_ALPHABET_H
00023 #define ASTL_ALPHABET_H
00024 
00025 // This file implements four alphabet traits:
00026 // 1. template <class T, T lower_bound, T upper_bound> range
00027 // 2. plain (8 bits signed)
00028 // 3. ASCII (7 bits)
00029 // 4. french
00030 // 5. Unicode (wchar_t)
00031 // 6. strings (std::string)
00032 // 7. uplain (8 bits unsigned)
00033 
00034 
00035 #include <iterator>
00036 #include <string>
00037 
00038 namespace astl {
00039 
00040 using namespace std;
00041 
00042 #if (__GNUG__ && __GNUG__ < 3)
00043 #define CHAR_TRAITS string_char_traits
00044 #else
00045 #define CHAR_TRAITS std::char_traits
00046 #endif
00047 
00048 template <typename T, T lower_bound, T upper_bound>
00049 class range : public CHAR_TRAITS<T>
00050 {
00051 public:
00052   typedef typename CHAR_TRAITS<T>::char_type char_type;
00053   typedef long                               int_type;
00054   static const size_t                        size;
00055   static char_type to_char_type(int_type e) {
00056     return (char_type) (e + lower_bound);
00057   }
00058   static int_type to_int_type(char_type c) {
00059     return (int_type) c - lower_bound;
00060   }
00061   static bool eq_int_type(int_type x, int_type y) {
00062     return x == y;
00063   }
00064 
00065 #if (__GNUG__ && __GNUG__ < 3)
00066   class const_iterator : public bidirectional_iterator<T, ptrdiff_t>
00067 #else
00068   class const_iterator 
00069     : public std::iterator<std::bidirectional_iterator_tag, T>
00070 #endif
00071   {
00072   private:
00073     int_type c;
00074 
00075   public:
00076     const_iterator()
00077     { }
00078 
00079     const_iterator(int_type x)
00080       : c(x)
00081     { }
00082 
00083     const_iterator& operator++ () {
00084       ++c;
00085       return (*this);
00086     }
00087 
00088     const_iterator operator++ (int) {
00089       const_iterator tmp = *this;
00090       ++(*this);
00091       return (tmp);
00092     }
00093 
00094     char_type operator* () const {
00095       return (char_type) c;
00096     }
00097 
00098     bool operator== (const const_iterator& i) const {
00099       return c == i.c;
00100     }
00101 
00102     bool operator!= (const const_iterator& i) const {
00103       return !(*this == i);
00104     }
00105   };
00106 
00107   static const_iterator begin() { return const_iterator(lower_bound); }
00108   static const_iterator end() { return const_iterator(upper_bound + 1); }
00109 
00110   static bool lt(const char_type &x, const char_type &y) {
00111     return x < y;
00112   }
00113 
00114   static bool eq(const char_type &x, const char_type &y) {
00115     return x == y;
00116   }
00117 
00118   // These are not standard requirements (backward compatibility purpose):
00119   typedef const_iterator iterator;
00120   typedef char_type Alphabet;
00121   static unsigned long map(const char_type &x) {
00122     return (unsigned long) to_int_type(x);
00123   }
00124   static char_type unmap(unsigned long x) {
00125     return to_char_type((int_type) x);
00126   }
00127 };
00128 
00129 template <typename T, T lower_bound, T upper_bound> const size_t
00130 range<T, lower_bound, upper_bound>::size = (size_t) upper_bound - lower_bound + 1;
00131 
00132 typedef range<char, (char) -128, (char) 127> plain;
00133 typedef range<unsigned char, (unsigned char) 0, (unsigned char) 255> uplain;
00134 typedef range<char, (char) 0, (char) 127>    ASCII;
00135 typedef std::char_traits<wchar_t> unicode;
00136 
00137 class strings : public CHAR_TRAITS<string>
00138 {
00139 public:
00140   typedef string      char_type;
00141   typedef long        int_type;
00142   static const size_t size;
00143 
00144   static bool eq(const char_type &x, const char_type &y) {
00145     return x == y;
00146   }
00147 
00148   static bool lt(const char_type &x, const char_type &y) {
00149     return x < y;
00150   }
00151 };
00152 
00153 class ucs2
00154 {
00155 public:
00156   typedef unsigned short char_type;
00157   typedef unsigned short int_type;
00158   static const size_t size = 65536;
00159 
00160   static bool eq(const char_type &x, const char_type &y) {
00161     return x == y;
00162   }
00163 
00164   static bool lt(const char_type &x, const char_type &y) {
00165     return x < y;
00166   }
00167 };
00168 
00169 #ifdef _MSC_VER
00170 // VC++ 6.0 does not compile the previous generic 'size' initialization:
00171 //  const size_t plain::size   = 256;
00172 //  const size_t ASCII::size   = 128;
00173 //  const size_t range<char, (char) -128, (char) 127>::size   = 256;
00174 //  const size_t range<char, (char) 0, (char) 127>::size   = 128;
00175 #endif
00176 
00177 } // namespace astl
00178 
00179 #endif // ASTL_ALPHABET_H
00180 
00181 
00182 
00183 
00184 
00185 
00186 

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