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

Generated on Sat Jul 29 17:13:10 2006 for Vaucanson by  doxygen 1.4.6