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

Generated on Wed Jun 13 17:00:29 2007 for Vaucanson by  doxygen 1.5.1