Vaucanson 1.4
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, 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_SEMIRING_BASE_HXX
00018 # define VCSN_ALGEBRA_CONCEPT_SEMIRING_BASE_HXX
00019 
00020 # include <vaucanson/algebra/concept/semiring_base.hh>
00021 # include <vaucanson/algebra/implementation/semiring/q_number.hh>
00022 
00023 # include <sstream>
00024 # include <string>
00025 # include <iostream>
00026 
00027 namespace vcsn {
00028 
00029   namespace algebra {
00030 
00031     /*-------------.
00032     | SemiringBase |
00033     `-------------*/
00034 
00035     template <class Self>
00036     SemiringBase<Self>::SemiringBase()
00037     {}
00038 
00039     template <class Self>
00040     SemiringBase<Self>::SemiringBase(const SemiringBase& other) :
00041       MonoidBase<Self>(other)
00042     {}
00043 
00044     template <class Self>
00045     template <class T>
00046     bool
00047     SemiringBase<Self>::can_choose_non_starable(SELECTOR(T)) const
00048     {
00049       return op_can_choose_non_starable(this->self(), SELECT(T));
00050     }
00051 
00052     template <class Self>
00053     template <class T>
00054     Element<Self, T>
00055     SemiringBase<Self>::choose_starable(SELECTOR(T)) const
00056     {
00057       return op_choose_starable(this->self(), SELECT(T));
00058     }
00059 
00060 
00061     template <class Self>
00062     template <class T>
00063     Element<Self, T>
00064     SemiringBase<Self>::choose_non_starable(SELECTOR(T)) const
00065     {
00066       return op_choose_non_starable(this->self(), SELECT(T));
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   namespace algebra {
00128 
00129     // default implementations:
00130 
00131     template <typename S, typename T>
00132     bool
00133     op_can_choose_non_starable(const algebra::SemiringBase<S>& set,
00134                                SELECTOR(T))
00135     {
00136       return false;
00137     }
00138 
00139 
00140     template <typename S, typename T>
00141     Element<S, T>
00142     op_choose_starable(const algebra::SemiringBase<S>& set, SELECTOR(T))
00143     {
00144       std::cerr << "WARNING: default implementation of op_choose_starable "
00145         "called." << std::endl;
00146       std::cerr << "RESULT IS NOT RANDOM." << std::endl;
00147       // Zero is always starable.
00148       return T();
00149     }
00150 
00151     template <typename S, typename T>
00152     Element<S, T>
00153     op_choose_non_starable(const algebra::SemiringBase<S>& set, SELECTOR(T))
00154     {
00155       pure_service_call ("default implementation of op_choose_non_starable()");
00156       return T();
00157     }
00158 
00159     template <typename S, typename T>
00160     bool
00161     op_parse(const algebra::SemiringBase<S>&,
00162              T&                                  w,
00163              const std::string&                  s,
00164              typename std::string::const_iterator& i)
00165     {
00166       if (*i != '-' && (*i < '0' || *i > '9'))
00167         return false;
00168       T res;
00169       std::stringstream ret;
00170       ret << std::string(i, s.end());
00171       int init = ret.tellg();
00172       ret >> std::dec >> res;
00173       if (ret.eof())
00174         {
00175           i = s.end();
00176         }
00177       else
00178         {
00179           int cur = ret.tellg();
00180           if (cur < 0)
00181             return false;
00182           i += cur - init;
00183         }
00184       w = res;
00185       return true;
00186     }
00187 
00188     template <typename Self, typename T>
00189     bool op_starable(const algebra::SemiringBase<Self>& s, const T& v)
00190     {
00191       return op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T)));
00192     }
00193 
00194     template <typename Self, typename T>
00195     void op_in_star(const algebra::SemiringBase<Self>&, T& v)
00196     {
00197       assertion(op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T))));
00198       v = identity_value(SELECT(Self), SELECT(T));
00199     }
00200 
00201     template <typename Self, typename T>
00202     T op_default(SELECTOR(algebra::SemiringBase<Self>), SELECTOR(T))
00203     {
00204       return zero_value(SELECT(Self), SELECT(T));
00205     }
00206 
00207   } // algebra
00208 
00209 } // vcsn
00210 
00211 #endif // ! VCSN_ALGEBRA_CONCEPT_SEMIRING_BASE_HXX