semiring_base.hxx

00001 // semiring_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 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_SEMIRING_BASE_HXX
00018 # define VCSN_ALGEBRA_CONCEPT_SEMIRING_BASE_HXX
00019 
00020 # include <vaucanson/algebra/concept/semiring_base.hh>
00021 
00022 # include <sstream>
00023 # include <string>
00024 
00025 namespace vcsn {
00026 
00027   namespace algebra {
00028 
00029     /*-------------.
00030     | SemiringBase |
00031     `-------------*/
00032 
00033     template <class Self>
00034     SemiringBase<Self>::SemiringBase()
00035     {}
00036 
00037     template <class Self>
00038     SemiringBase<Self>::SemiringBase(const SemiringBase& other) :
00039       MonoidBase<Self>(other)
00040     {}
00041 
00042 # define VCSN_CHOOSE_SEMIRING(CanArg, NonArg, RetType...)                     \
00043     template <class Self>                                                     \
00044     template <class T>                                                        \
00045     RetType                                                                   \
00046     SemiringBase<Self>::CanArg ## choose_ ## NonArg ## starable(              \
00047     SELECTOR(T)) const                                                        \
00048     {                                                                         \
00049       return op_ ## CanArg ## choose_ ## NonArg ## starable(this->self(),     \
00050                                                             SELECT(T));       \
00051     }
00052     VCSN_CHOOSE_SEMIRING(can_,non_,bool)
00053     VCSN_CHOOSE_SEMIRING(,,Element<Self, T>)
00054     VCSN_CHOOSE_SEMIRING(,non_,Element<Self, T>)
00055 # undef VCSN_CHOOSE_SEMIRING
00056 
00057 
00058   } // algebra
00059 
00060   /*---------------------.
00061   | MetaElement<Self, T> |
00062   `---------------------*/
00063 
00064   template <class Self, class T>
00065   Element<Self, T>&
00066   MetaElement<algebra::SemiringBase<Self>, T>::star()
00067   {
00068     op_in_star(this->structure(), this->value());
00069     return this->self();
00070   }
00071 
00072   template <class Self, class T>
00073   bool
00074   MetaElement<algebra::SemiringBase<Self>, T>::starable() const
00075   {
00076     return op_starable(this->structure(), this->value());
00077   }
00078 
00079   template <class Self, class T>
00080   MetaElement<algebra::SemiringBase<Self>, T>::MetaElement()
00081   {}
00082 
00083   template <class Self, class T>
00084   MetaElement<algebra::SemiringBase<Self>, T>::MetaElement
00085   (const MetaElement& other) :
00086     MetaElement<algebra::MonoidBase<Self>, T>(other)
00087   {}
00088 
00089   /*-----.
00090   | star |
00091   `-----*/
00092 
00093   template <typename S, typename T>
00094   typename op_star_traits<S, T>::ret_t
00095   star(const Element<S, T>& e)
00096   {
00097     typename op_star_traits<S, T>::ret_t res(e);
00098     return res.star();
00099   }
00100 
00101   template <typename S, typename T>
00102   bool starable(const Element<S, T>& elt)
00103   {
00104     return op_starable(elt.structure(), elt.value());
00105   }
00106 
00107   template <typename S, typename T>
00108   bool
00109   parse_weight(Element<S, T>& w, const std::string&s ,
00110                typename std::string::const_iterator& i)
00111   {
00112     return op_parse(w.structure(), w.value(), s, i);
00113   }
00114 
00115 
00116   // default implementations:
00117 
00118   template <typename S, typename T>
00119   bool
00120   op_can_choose_non_starable(const algebra::SemiringBase<S>& set, SELECTOR(T))
00121   {
00122     return false;
00123   }
00124 
00125 
00126   template <typename S, typename T>
00127   Element<S, T>
00128   op_choose_starable(const algebra::SemiringBase<S>& set, SELECTOR(T))
00129   {
00130     std::cerr << "WARNING: default implementation of op_choose_starable "
00131       "called." << std::endl;
00132     std::cerr << "RESULT IS NOT RANDOM." << std::endl;
00133     // Zero is always starable.
00134     return T();
00135   }
00136 
00137   template <typename S, typename T>
00138   Element<S, T>
00139   op_choose_non_starable(const algebra::SemiringBase<S>& set, SELECTOR(T))
00140   {
00141     pure_service_call ("default implementation of op_choose_non_starable()");
00142     return T();
00143   }
00144 
00145   template <typename S, typename T>
00146   bool
00147   op_parse(const algebra::SemiringBase<S>&,
00148            T&                                    w,
00149            const std::string&                    s,
00150            typename std::string::const_iterator& i)
00151   {
00152     if (*i != '-' && (*i < '0' || *i > '9'))
00153       return false;
00154     T res;
00155     std::stringstream ret;
00156     ret << std::string(i, s.end());
00157     int init = ret.tellg();
00158     ret >> std::dec >> res;
00159     if (ret.tellg() < 0)
00160       return false;
00161     for (int cur = ret.tellg(); (cur - init - 1) && i != s.end(); ++i, ++init)
00162       ;
00163     if (*i != '.')
00164       ++i;
00165     w = res;
00166     return true;
00167   }
00168 
00169   template <typename Self, typename T>
00170   bool op_starable(const algebra::SemiringBase<Self>& s, const T& v)
00171   {
00172     return op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T)));
00173   }
00174 
00175   template <typename Self, typename T>
00176   void op_in_star(const algebra::SemiringBase<Self>&, T& v)
00177   {
00178     assertion(op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T))));
00179     v = identity_value(SELECT(Self), SELECT(T));
00180   }
00181 
00182   template <typename Self, typename T>
00183   T op_default(SELECTOR(algebra::SemiringBase<Self>), SELECTOR(T))
00184   {
00185     return zero_value(SELECT(Self), SELECT(T));
00186   }
00187 
00188 } // vcsn
00189 
00190 #endif // ! VCSN_ALGEBRA_CONCEPT_SEMIRING_BASE_HXX

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