- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

# 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 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   namespace algebra {
    00180
    00181     /*----------.
    00182     | Operators |
    00183     `----------*/
    00184
    00185     // default implementation of word parsing assumes the fact that
    00186     // an alphabet letter can be constructed from a 'char'.
    00187     // specialized this function according to your alphabet if this
    00188     // is not the case.
    00189     template <typename S, typename T, typename CharContainer>
    00190     bool op_parse(const algebra::FreeMonoidBase<S>& set, T& v,
    00191                   const std::string& s,
    00192                   typename std::string::const_iterator& i,
    00193                   const CharContainer& escaped)
    00194     {
    00195       typename std::string::const_iterator j = i;
    00196       typename std::string::const_iterator k;
    00197
    00198       while ((i != s.end()) &&
    00199              ((*i == '\\') || (set.alphabet().contains(*i) &&
    00200              (std::find(escaped.begin(), escaped.end(), *i) == escaped.end()))))
    00201         if (*i == '\\')
    00202           {
    00203             k = i;
    00204             ++k;
    00205             if (k != s.end())
    00206               i = k;
    00207             if (not set.alphabet().contains(*i))
    00208               return (i != j);
    00209             v += *i;
    00210             ++i;
    00211           }
    00212         else
    00213           {
    00214             v += *i;
    00215             ++i;
    00216           }
    00217       return (i != j);
    00218     }
    00219
    00220     template <typename Self, typename T>
    00221     void
    00222     op_in_mirror(const algebra::FreeMonoidBase<Self>& s, T& v)
    00223     {
    00224       const T           new_v(v);
    00225       typename MetaElement<algebra::FreeMonoidBase<Self>, T>::iterator  it
    00226         = op_begin(s.self(), v);
    00227
    00228       for (typename MetaElement<algebra::FreeMonoidBase<Self>, T>::
    00229              const_reverse_iterator i = op_rbegin_const(s.self(), new_v);
    00230            i != op_rend_const(s.self(), new_v);
    00231            ++i)
    00232         *it++ = *i;
    00233     }
    00234
    00235     template <typename Self, typename T>
    00236     bool
    00237     op_contains(const algebra::FreeMonoidBase<Self>& s, const T& v)
    00238     {
    00239       typedef typename op_begin_traits<Self, T>::const_ret_t const_iterator;
    00240
    00241       for (const_iterator i = op_begin_const(s.self(), v);
    00242            i != op_end_const(s.self(), v);
    00243            ++i)
    00244         if (! s.alphabet().contains(*i))
    00245           return false;
    00246       return true;
    00247     }
    00248
    00249     template <typename Self, typename St, typename T>
    00250     St&
    00251     op_rout(const algebra::FreeMonoidBase<Self>& s,
    00252             St& st,
    00253             const T& v)
    00254     {
    00255       typedef typename op_begin_traits<Self, T>::const_ret_t const_iterator;
    00256
    00257       bool is_empty = true;
    00258       for (const_iterator i = op_begin_const(s.self(), v);
    00259            i != op_end_const(s.self(), v);
    00260            ++i, is_empty = false)
    00261         st << misc::make_escaper(*i);
    00262
    00263       if (is_empty)
    00264         st << "1";
    00265       return st;
    00266     }
    00267
    00268   } // algebra
    00269
    00270 } // vcsn
    00271
    00272 #endif // ! VCSN_ALGEBRA_CONCEPT_FREEMONOID_BASE_HXX