Vaucanson  1.4.1
krat_exp_cderivation.hxx
1 // krat_exp_cderivation.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_ALGORITHMS_KRAT_EXP_CDERIVATION_HXX
18 # define VCSN_ALGORITHMS_KRAT_EXP_CDERIVATION_HXX
19 
21 
22 namespace vcsn {
23 
24  namespace algebra {
25 
26  template <class Series, class T, class Dispatch>
27  struct KRatExpCDerivation : algebra::KRatExpMatcher<
28  KRatExpCDerivation<Series, T, Dispatch>,
29  T,
30  Element<Series, T>,
31  Dispatch
32  >
33  {
34  typedef KRatExpCDerivation<Series, T, Dispatch> self_t;
35  typedef Element<Series, T> return_type;
36  typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
37  typedef typename semiring_elt_t::value_t semiring_elt_value_t;
38  typedef typename Element<Series, T>::monoid_elt_t monoid_elt_t;
39  typedef typename monoid_elt_t::value_t monoid_elt_value_t;
40  typedef typename monoid_elt_t::set_t monoid_t;
41  typedef typename monoid_t::alphabet_t alphabet_t;
42  typedef typename alphabet_t::letter_t letter_t;
43  INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
44 
45  KRatExpCDerivation(const Element<Series, T>& exp,
46  letter_t a) :
47  exp_(exp),
48  a_(a)
49  {}
50 
51  Element<Series, T> series(const T& e)
52  {
53  return Element<Series, T>(exp_.structure(), e);
54  }
55 
56  MATCH__(Product, lhs, rhs)
57  {
58  return_type match_lhs = match(lhs);
59 
60  // FIXME: Following code only valid for series over Boolean semirings.
61  if (match_lhs != zero_as<T>::of(exp_.structure()))
62  return match_lhs * rhs;
63  else
64  {
65  std::pair<semiring_elt_t, bool> ret = constant_term(series(lhs));
66  return ret.first * match(rhs);
67  }
68  }
69  END
70 
71  MATCH__(Sum, lhs, rhs)
72  {
73  return_type match_lhs = match(lhs);
74 
75  // FIXME: Following code only valid for series over Boolean semirings.
76  if (match_lhs != zero_as<T>::of(exp_.structure()))
77  return match_lhs;
78  else
79  return match(rhs);
80  }
81  END
82 
83  MATCH_(Star, e)
84  {
85  // FIXME: Following code only valid for series over Boolean semirings.
86  return match(e) * e.clone().star();
87  }
88  END
89 
90  MATCH__(LeftWeight, w, e)
91  {
92  return semiring_elt_t(w) * match(e);
93  }
94  END
95 
96  MATCH__(RightWeight, e, w)
97  {
98  return match(e) * semiring_elt_t(w);
99  }
100  END
101 
102  MATCH_(Constant, m)
103  {
104  if (m[0] == a_)
105  {
106  if (m.length() == 1)
107  return identity_as<T>::of(exp_.structure());
108  else
109  return Element<Series, T> (exp_.structure(), m.substr(1));
110  }
111  else
112  return zero_as<T>::of(exp_.structure());
113  }
114  END
115 
116  MATCH(Zero)
117  {
118  return zero_as<T>::of(exp_.structure());
119  }
120  END
121 
122  MATCH(One)
123  {
124  return zero_as<T>::of(exp_.structure());
125  }
126  END
127 
128  private:
129  Element<Series, T> exp_;
130  letter_t a_;
131  };
132 
133  } // algebra
134 
135  template <class Series, class T, class Letter>
136  Element<Series, T>
138  Letter a)
139  {
140  algebra::KRatExpCDerivation<Series, T, algebra::DispatchFunction<T> >
141  matcher(exp, a);
142  return matcher.match(exp.value());
143  }
144 
145  template <class Series, class T, class Word>
146  Element<Series, T>
148  Word w)
149  {
150  Element<Series, T> ret(exp);
151  for (typename Word::reverse_iterator a = w.rbegin();
152  a != w.rend(); ++a)
153  {
154  algebra::KRatExpCDerivation<Series, T, algebra::DispatchFunction<T> >
155  matcher(exp, *a);
156  ret = matcher.match(ret.value());
157  }
158  return ret;
159  }
160 
161 } // vcsn
162 
163 #endif // ! VCSN_ALGORITHMS_KRAT_EXP_CDERIVATION_HXX