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