freemonoid_base.hxx

00001 // freemonoid_base.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_ALGEBRA_CONCEPT_FREEMONOID_BASE_HXX
00018 # define VCSN_ALGEBRA_CONCEPT_FREEMONOID_BASE_HXX
00019 
00020 # include <vaucanson/algebra/concept/freemonoid_base.hh>
00021 # include <vaucanson/misc/escaper.hh>
00022 
00023 namespace vcsn {
00024 
00025   namespace algebra {
00026 
00027     /*---------------------.
00028     | FreeMonoidBase<Self> |
00029     `---------------------*/
00030     template <class Self>
00031     typename FreeMonoidBase<Self>::alphabet_t&
00032     FreeMonoidBase<Self>::alphabet()
00033     {
00034       return this->self().alphabet();
00035     }
00036 
00037     template <class Self>
00038     const typename FreeMonoidBase<Self>::alphabet_t&
00039     FreeMonoidBase<Self>::alphabet() const
00040     {
00041       return this->self().alphabet();
00042     }
00043 
00044     template <class Self>
00045     FreeMonoidBase<Self>::FreeMonoidBase()
00046     {}
00047 
00048     template <class Self>
00049     FreeMonoidBase<Self>::FreeMonoidBase(const FreeMonoidBase& m) :
00050       MonoidBase<Self>(m)
00051     {}
00052 
00053   } // algebra
00054 
00055   /*-------------------------------------.
00056   | MetaElement<FreeMonoidBase<Self>, T> |
00057   `-------------------------------------*/
00058   template <class Self, typename T>
00059   size_t
00060   MetaElement<algebra::FreeMonoidBase<Self>, T>::length() const
00061   {
00062     return op_size(this->structure(), this->value());
00063   }
00064 
00065   template <class Self, typename T>
00066   void
00067   MetaElement<algebra::FreeMonoidBase<Self>, T>::mirror()
00068   {
00069     op_in_mirror(this->structure(), this->value());
00070   }
00071 
00072   template <class Self, typename T>
00073   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::iterator
00074   MetaElement<algebra::FreeMonoidBase<Self>, T>::begin()
00075   {
00076     return op_begin(this->structure(), this->value());
00077   }
00078 
00079   template <class Self, typename T>
00080   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_iterator
00081   MetaElement<algebra::FreeMonoidBase<Self>, T>::begin() const
00082   {
00083     return op_begin_const(this->structure(), this->value());
00084   }
00085 
00086   template <class Self, typename T>
00087   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::reverse_iterator
00088   MetaElement<algebra::FreeMonoidBase<Self>, T>::rbegin()
00089   {
00090     return op_rbegin(this->structure(), this->value());
00091   }
00092 
00093   template <class Self, typename T>
00094   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_reverse_iterator
00095   MetaElement<algebra::FreeMonoidBase<Self>, T>::rbegin() const
00096   {
00097     return op_rbegin_const(this->structure(), this->value());
00098   }
00099 
00100   template <class Self, typename T>
00101   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::iterator
00102   MetaElement<algebra::FreeMonoidBase<Self>, T>::end()
00103   {
00104     return op_end(this->structure(), this->value());
00105   }
00106 
00107   template <class Self, typename T>
00108   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_iterator
00109   MetaElement<algebra::FreeMonoidBase<Self>, T>::end() const
00110   {
00111     return op_end_const(this->structure(), this->value());
00112   }
00113 
00114   template <class Self, typename T>
00115   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::reverse_iterator
00116   MetaElement<algebra::FreeMonoidBase<Self>, T>::rend()
00117   {
00118     return op_rend(this->structure(), this->value());
00119   }
00120 
00121   template <class Self, typename T>
00122   typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_reverse_iterator
00123   MetaElement<algebra::FreeMonoidBase<Self>, T>::rend() const
00124   {
00125     return op_rend_const(this->structure(), this->value());
00126   }
00127 
00128   template <class Self, typename T>
00129   MetaElement<algebra::FreeMonoidBase<Self>, T>::MetaElement() :
00130     MetaElement<algebra::MonoidBase<Self>, T>()
00131   {}
00132 
00133   template <class Self, typename T>
00134   MetaElement<algebra::FreeMonoidBase<Self>, T>::MetaElement(const MetaElement& o)  :
00135     MetaElement<algebra::MonoidBase<Self>, T>(o)
00136   {}
00137 
00138   /*-------------------.
00139   | External functions |
00140   `-------------------*/
00141 
00142   template <typename S, typename T>
00143   Element<S, T>
00144   mirror(const Element<S, T>& e)
00145   {
00146     Element<S, T>       ret(e);
00147     ret.mirror();
00148     return ret;
00149   }
00150 
00151   template <typename S, typename T, typename CharContainer>
00152   bool
00153   parse_word(Element<S, T>& dest,
00154              const std::string& s,
00155              typename std::string::const_iterator& i,
00156              const CharContainer& escaped)
00157   {
00158     return op_parse(dest.structure(), dest.value(), s, i, escaped);
00159   }
00160 
00161   template<typename S, typename T>
00162   const std::basic_string<T>&
00163   op_convert(const algebra::FreeMonoidBase<S>& s,
00164              SELECTOR(std::basic_string<T>),
00165              const std::basic_string<T>& from_data)
00166   {
00167 # ifndef VCSN_NDEBUG
00168     for (typename std::basic_string<T>::const_iterator it = from_data.begin();
00169          it != from_data.end();
00170          ++it)
00171       precondition_ (s.alphabet().contains(*it),
00172                      "The letter " + *it + " is not in the given alphabet");
00173 # else // ! VCSN_NDEBUG
00174     (void) s;
00175 # endif // ! VCSN_NDEBUG
00176     return from_data;
00177   }
00178 
00179   /*----------.
00180   | Operators |
00181   `----------*/
00182 
00183   // default implementation of word parsing assumes the fact that
00184   // an alphabet letter can be constructed from a 'char'.
00185   // specialized this function according to your alphabet if this
00186   // is not the case.
00187   template <typename S, typename T, typename CharContainer>
00188   bool op_parse(const algebra::FreeMonoidBase<S>& set, T& v,
00189                 const std::string& s,
00190                 typename std::string::const_iterator& i,
00191                 const CharContainer& escaped)
00192   {
00193     typename std::string::const_iterator j = i;
00194     typename std::string::const_iterator k;
00195 
00196     while ((i != s.end()) &&
00197            ((*i == '\\') || (set.alphabet().contains(*i) &&
00198            (std::find(escaped.begin(), escaped.end(), *i) == escaped.end()))))
00199       if (*i == '\\')
00200         {
00201           k = i;
00202           ++k;
00203           if (k != s.end())
00204             i = k;
00205           if (not set.alphabet().contains(*i))
00206             return (i != j);
00207           v += *i;
00208           ++i;
00209         }
00210       else
00211         {
00212           v += *i;
00213           ++i;
00214         }
00215     return (i != j);
00216   }
00217 
00218   template <typename Self, typename T>
00219   void
00220   op_in_mirror(const algebra::FreeMonoidBase<Self>& s, T& v)
00221   {
00222     const T             new_v(v);
00223     typename MetaElement<algebra::FreeMonoidBase<Self>, T>::iterator  it
00224       = op_begin(s.self(), v);
00225 
00226     for (typename MetaElement<algebra::FreeMonoidBase<Self>, T>::
00227            const_reverse_iterator i = op_rbegin_const(s.self(), new_v);
00228          i != op_rend_const(s.self(), new_v);
00229          ++i)
00230       *it++ = *i;
00231   }
00232 
00233   template <typename Self, typename T>
00234   bool
00235   op_contains(const algebra::FreeMonoidBase<Self>& s, const T& v)
00236   {
00237     typedef typename op_begin_traits<Self, T>::const_ret_t const_iterator;
00238 
00239     for (const_iterator i = op_begin_const(s.self(), v);
00240          i != op_end_const(s.self(), v);
00241          ++i)
00242       if (! s.alphabet().contains(*i))
00243         return false;
00244     return true;
00245   }
00246 
00247   template <typename Self, typename St, typename T>
00248   St&
00249   op_rout(const algebra::FreeMonoidBase<Self>& s,
00250           St& st,
00251           const T& v)
00252   {
00253     typedef typename op_begin_traits<Self, T>::const_ret_t const_iterator;
00254 
00255     bool is_empty = true;
00256     for (const_iterator i = op_begin_const(s.self(), v);
00257          i != op_end_const(s.self(), v);
00258          ++i, is_empty = false)
00259       st << utility::make_escaper(*i);
00260 
00261     if (is_empty)
00262       st << "1";
00263     return st;
00264   }
00265 
00266 } // vcsn
00267 
00268 #endif // ! VCSN_ALGEBRA_CONCEPT_FREEMONOID_BASE_HXX

Generated on Fri Jul 28 12:18:32 2006 for Vaucanson by  doxygen 1.4.6