Vaucanson  1.4.1
krat_coefficient.hxx
1 // krat_coefficient.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_COEFFICIENT_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_COEFFICIENT_HXX
19 
20 # include <utility>
22 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
23 
24 namespace vcsn {
25 
26  namespace algebra {
27 
28  template <class Series, class T, class Dispatch>
29  class CoefficientEval : public algebra::KRatExpMatcher<
30  CoefficientEval<Series, T, Dispatch>,
31  T,
32  std::pair<typename Element<Series, T>::semiring_elt_t,
33  Element<Series, T> >,
34  Dispatch
35  >
36  {
37  public:
38  typedef ConstantTermEval<Series, T, Dispatch> self_t;
39  typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
40  typedef std::pair<semiring_elt_t, Element<Series, T> > return_type;
41  typedef typename semiring_elt_t::value_t semiring_elt_value_t;
42  INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
43 
44  CoefficientEval(const Element<Series, T>& exp) :
45  exp_(exp)
46  {}
47 
48  semiring_elt_t one()
49  {
50  return
51  exp_.structure().semiring().identity(SELECT(semiring_elt_value_t));
52  }
53 
54  semiring_elt_t zero()
55  {
56  return
57  exp_.structure().semiring().zero(SELECT(semiring_elt_value_t));
58  }
59 
60  MATCH__(Product, lhs, rhs)
61  {
62  return return_type(one(), lhs * rhs);
63  }
64  END
65 
66  MATCH__(Sum, lhs, rhs)
67  {
68  return return_type(one(), lhs + rhs);
69  }
70  END
71 
72  MATCH_(Star, node)
73  {
74  return return_type(one(), node.star());
75  }
76  END
77 
78  MATCH__(LeftWeight, w, node)
79  {
80  return return_type(w, node);
81  }
82  END
83 
84  MATCH__(RightWeight, node, w)
85  {
86  return return_type(w, node);
87  }
88  END
89 
90  MATCH_(Constant, m)
91  {
92  return return_type(one(), m);
93  }
94  END
95 
96  MATCH(Zero)
97  {
98  return return_type(zero(), zero_as<T>::of(exp_.structure()));
99  }
100  END
101 
102  MATCH(One)
103  {
104  return return_type(one(), identity_as<T>::of(exp_.structure()));
105  }
106  END
107 
108  private:
109  Element<Series, T> exp_;
110  };
111 
112  template <class Series, class T>
113  std::pair<typename Element<Series, T>::semiring_elt_t, Element<Series, T> >
114  coefficient(const Element<Series, T>& exp)
115  {
116  CoefficientEval<Series, T, algebra::DispatchFunction<T> > matcher(exp);
117  return matcher.match(exp.value());
118  }
119 
120  } // algebra
121 
122 } // vcsn
123 
124 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_COEFFICIENT_HXX