krat_coefficient.hxx

00001 // krat_coefficient.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 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_IMPLEMENTATION_SERIES_KRAT_COEFFICIENT_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_COEFFICIENT_HXX
00019 
00020 # include <utility>
00021 # include <vaucanson/algorithms/krat_exp_constant_term.hh>
00022 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
00023 
00024 namespace vcsn {
00025 
00026   namespace algebra {
00027 
00028     template <class Series, class T, class Dispatch>
00029     class CoefficientEval : public algebra::KRatExpMatcher<
00030       CoefficientEval<Series, T, Dispatch>,
00031       T,
00032       std::pair<typename Element<Series, T>::semiring_elt_t,
00033                 Element<Series, T> >,
00034       Dispatch
00035       >
00036     {
00037     public:
00038       typedef ConstantTermEval<Series, T, Dispatch>     self_t;
00039       typedef typename Element<Series, T>::semiring_elt_t     semiring_elt_t;
00040       typedef std::pair<semiring_elt_t, Element<Series, T> >  return_type;
00041       typedef typename semiring_elt_t::value_t          semiring_elt_value_t;
00042       INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
00043 
00044       CoefficientEval(const Element<Series, T>& exp) :
00045         exp_(exp)
00046       {}
00047 
00048       semiring_elt_t    one()
00049       {
00050         return
00051           exp_.structure().semiring().identity(SELECT(semiring_elt_value_t));
00052       }
00053 
00054       semiring_elt_t zero()
00055       {
00056         return
00057           exp_.structure().semiring().zero(SELECT(semiring_elt_value_t));
00058       }
00059 
00060       MATCH__(Product, lhs, rhs)
00061       {
00062         return return_type(one(), lhs * rhs);
00063       }
00064       END
00065 
00066       MATCH__(Sum, lhs, rhs)
00067       {
00068         return return_type(one(), lhs + rhs);
00069       }
00070       END
00071 
00072       MATCH_(Star, node)
00073       {
00074         return return_type(one(), node.star());
00075       }
00076       END
00077 
00078       MATCH__(LeftWeight, w, node)
00079       {
00080         return return_type(w, node);
00081       }
00082       END
00083 
00084       MATCH__(RightWeight, node, w)
00085       {
00086         return return_type(w, node);
00087       }
00088       END
00089 
00090       MATCH_(Constant, m)
00091       {
00092         return return_type(one(), m);
00093       }
00094       END
00095 
00096       MATCH(Zero)
00097       {
00098         return return_type(zero(), zero_as<T>::of(exp_.structure()));
00099       }
00100       END
00101 
00102       MATCH(One)
00103       {
00104         return return_type(one(), identity_as<T>::of(exp_.structure()));
00105       }
00106       END
00107 
00108     private:
00109       Element<Series, T> exp_;
00110     };
00111 
00112     template <class Series, class T>
00113     std::pair<typename Element<Series, T>::semiring_elt_t, Element<Series, T> >
00114     coefficient(const Element<Series, T>& exp)
00115     {
00116       CoefficientEval<Series, T, algebra::DispatchFunction<T> > matcher(exp);
00117       return matcher.match(exp.value());
00118     }
00119 
00120   } // algebra
00121 
00122 } // vcsn
00123 
00124 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_COEFFICIENT_HXX

Generated on Sun Jul 29 19:35:20 2007 for Vaucanson by  doxygen 1.5.2