Vaucanson  1.4.1
initial_derivation.hxx
1 // initial_derivation.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2005, 2006 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_INITIAL_DERIVATION_HXX
18 # define VCSN_ALGORITHMS_INITIAL_DERIVATION_HXX
19 
20 # include <vaucanson/algebra/concept/series_base.hh>
21 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
22 
23 # include <list>
24 
25 namespace vcsn
26 {
27 
35  template <class Series, class T, class Dispatch>
36  struct KRatExpInitialDerivation : algebra::KRatExpMatcher<
37  KRatExpInitialDerivation<Series, T, Dispatch>,
38  T,
39  std::list< Element<Series, T> >,
40  Dispatch
41  >
42  {
44  typedef Element<Series, T> exp_t;
45  typedef std::list<exp_t> list_t;
46  typedef std::list<exp_t> return_type;
47  typedef typename list_t::iterator iterator_t;
48  IMPORT_TYPEDEF_(exp_t, semiring_elt_t);
49  IMPORT_TYPEDEF_(exp_t, monoid_elt_t);
50  typedef typename semiring_elt_t::value_t semiring_elt_value_t;
51  INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
52 
53  KRatExpInitialDerivation(const exp_t& exp) :
54  exp_(exp)
55  {}
56 
57  // Useful functions:
58  private:
59  list_t apply_sum(list_t explist)
60  {
61  if (explist.size() != 1)
62  {
63  iterator_t i = explist.begin();
64  exp_t res = *i;
65  for (i++; i != explist.end(); ++i)
66  {
67  res += *i;
68  }
69  explist.clear();
70  explist.push_back(res);
71  }
72  return explist;
73  }
74 
75  list_t& put_in(list_t& s, exp_t e)
76  {
77  s.clear();
78  s.push_back(e);
79  return s;
80  }
81 
82  public:
83 
84  // Matches:
85  MATCH__(Product, lhs, rhs)
86  {
87  list_t llist = match(lhs);
88  exp_t s (exp_.structure(), rhs);
89  list_t res;
90  for (typename list_t::const_iterator it = llist.begin();
91  it != llist.end(); ++it)
92  res.push_back(*it * s);
93  return res;
94  }
95  END
96 
97  MATCH__(Sum, lhs, rhs)
98  {
99  list_t llist = match(lhs);
100  list_t rlist = match(rhs);
101  list_t res;
102  merge(llist.begin(), llist.end(),
103  rlist.begin(), rlist.end(),
104  inserter(res, res.begin()));
105  return res;
106  }
107  END
108 
109  MATCH_(Star, e)
110  {
111  list_t res;
112  exp_t s (exp_.structure(), e.star());
113  res.push_back(s);
114  return res;
115  }
116  END
117 
118  MATCH__(LeftWeight, w, e)
119  {
120  list_t clist = apply_sum(match(e));
121  put_in(clist, semiring_elt_t(w) * *clist.begin());
122  return clist;
123  }
124  END
125 
126  MATCH__(RightWeight, e, w)
127  {
128  list_t clist = apply_sum(match(e));
129  put_in(clist, *clist.begin() * semiring_elt_t(w));
130  return clist;
131  }
132  END
133 
134  MATCH_(Constant, m)
135  {
136  list_t res;
137  exp_t s (exp_.structure(), m);
138  res.push_back(s);
139  return res;
140  }
141  END
142 
143  MATCH(Zero)
144  {
145  list_t res;
146  res.push_back(exp_.structure().zero(SELECT(T)));
147  return res;
148  }
149  END
150 
151  MATCH(One)
152  {
153  list_t res;
154  res.push_back(exp_.structure().identity(SELECT(T)));
155  return res;
156  }
157  END
158 
159  private:
160  exp_t exp_;
161  };
162 
163 }
164 
165 #endif // ! VCSN_ALGORITHMS_INITIAL_DERIVATION_HXX