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 
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   namespace algebra {
00129 
00130     // default implementations:
00131 
00132     template <typename S, typename T>
00133     bool
00134     op_can_choose_non_starable(const algebra::SemiringBase<S>& set,
00135                                SELECTOR(T))
00136     {
00137       return false;
00138     }
00139 
00140 
00141     template <typename S, typename T>
00142     Element<S, T>
00143     op_choose_starable(const algebra::SemiringBase<S>& set, SELECTOR(T))
00144     {
00145       std::cerr << "WARNING: default implementation of op_choose_starable "
00146         "called." << std::endl;
00147       std::cerr << "RESULT IS NOT RANDOM." << std::endl;
00148       // Zero is always starable.
00149       return T();
00150     }
00151 
00152     template <typename S, typename T>
00153     Element<S, T>
00154     op_choose_non_starable(const algebra::SemiringBase<S>& set, SELECTOR(T))
00155     {
00156       pure_service_call ("default implementation of op_choose_non_starable()");
00157       return T();
00158     }
00159 
00160     template <typename S, typename T>
00161     bool
00162     op_parse(const algebra::SemiringBase<S>&,
00163              T&                                  w,
00164              const std::string&                  s,
00165              typename std::string::const_iterator& i)
00166     {
00167       if (*i != '-' && (*i < '0' || *i > '9'))
00168         return false;
00169       T res;
00170       std::stringstream ret;
00171       ret << std::string(i, s.end());
00172       int init = ret.tellg();
00173       ret >> std::dec >> res;
00174       if (ret.tellg() < 0)
00175         return false;
00176       for (int cur = ret.tellg();
00177            (cur - init - 1) && i != s.end(); ++i, ++init)
00178         ;
00179       if (*i != '.')
00180         ++i;
00181       w = res;
00182       return true;
00183     }
00184 
00185     template <typename Self, typename T>
00186     bool op_starable(const algebra::SemiringBase<Self>& s, const T& v)
00187     {
00188       return op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T)));
00189     }
00190 
00191     template <typename Self, typename T>
00192     void op_in_star(const algebra::SemiringBase<Self>&, T& v)
00193     {
00194       assertion(op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T))));
00195       v = identity_value(SELECT(Self), SELECT(T));
00196     }
00197 
00198     template <typename Self, typename T>
00199     T op_default(SELECTOR(algebra::SemiringBase<Self>), SELECTOR(T))
00200     {
00201       return zero_value(SELECT(Self), SELECT(T));
00202     }
00203 
00204   } // algebra
00205 
00206 } // vcsn
00207 
00208 #endif // ! VCSN_ALGEBRA_CONCEPT_SEMIRING_BASE_HXX

Generated on Thu Dec 13 16:03:01 2007 for Vaucanson by  doxygen 1.5.4