Vaucanson  1.4.1
krat_exp_constant_term.hxx
1 // krat_exp_constant_term.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, 2011 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_ALGORITHMS_KRAT_EXP_CONSTANT_TERM_HXX
18 # define VCSN_ALGORITHMS_KRAT_EXP_CONSTANT_TERM_HXX
19 
21 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
22 
23 namespace vcsn {
24 
33  template <class Series, class T, class Dispatch>
34  class ConstantTermEval : public algebra::KRatExpMatcher<
35  ConstantTermEval<Series, T, Dispatch>,
36  T,
37  typename Element<Series, T>::semiring_elt_t,
38  Dispatch
39  >
40  {
41  public:
43  typedef typename Element<Series, T>::semiring_elt_t return_type;
44  typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
45  typedef typename semiring_elt_t::value_t semiring_elt_value_t;
46  INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
47 
49  undefined(false),
50  exp_(exp)
51  {}
52 
53  MATCH__(Product, lhs, rhs)
54  {
55  return this->match(lhs) * this->match(rhs);
56  }
57  END
58 
59  MATCH__(Sum, lhs, rhs)
60  {
61  return this->match(lhs) + this->match(rhs);
62  }
63  END
64 
65  MATCH_(Star, node)
66  {
67  semiring_elt_t ret = this->match(node);
68  if (ret.starable())
69  return star(ret);
70  undefined = true;
71  return ret;
72  }
73  END
74 
75  MATCH__(LeftWeight, w, node)
76  {
77  return w * this->match(node);
78  }
79  END
80 
81  MATCH__(RightWeight, node, w)
82  {
83  return this->match(node) * w;
84  }
85  END
86 
87  MATCH_(Constant, m)
88  {
89  return exp_.structure().semiring().zero(SELECT(semiring_elt_value_t));
90  }
91  END
92 
93  MATCH(Zero)
94  {
95  return exp_.structure().semiring().zero(SELECT(semiring_elt_value_t));
96  }
97  END
98 
99  MATCH(One)
100  {
101  return exp_.structure().semiring().identity(SELECT(semiring_elt_value_t));
102  }
103  END
104 
105  bool undefined;
106 
107  private:
108  Element<Series, T> exp_;
109  };
110 
111  template <class Series, class T>
112  std::pair<typename Element<Series, T>::semiring_elt_t, bool>
113  constant_term(const Element<Series, T>& exp)
114  {
115  typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
117  semiring_elt_t v = matcher.match(exp.value());
118  if (matcher.undefined)
119  return std::make_pair(v, false);
120  return std::make_pair(v, true);
121  }
122 
123 } // vcsn
124 
125 #endif // ! VCSN_ALGORITHMS_KRAT_EXP_CONSTANT_TERM_HXX