Vaucanson  1.4.1
krat_exp_linearize.hxx
1 // krat_exp_linearize.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_LINEARIZE_HXX
18 # define VCSN_ALGORITHMS_KRAT_EXP_LINEARIZE_HXX
19 
20 // LINEAR_INDEX_START must be != 0
21 # define LINEAR_INDEX_START 1
22 
24 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
25 
26 namespace vcsn {
27 
28  // The Matcher building the linearized expression
29  template <class Series, class T, class Dispatch>
30  struct KRatExpLinearize : algebra::KRatExpMatcher<
31  KRatExpLinearize<Series, T, Dispatch>,
32  T,
33  typename linearize_element<Series, T>::element_t,
34  Dispatch
35  >
36  {
37  // Types of the resulting expression
38  typedef typename linearize_element<Series, T>::index_t index_t;
39  typedef typename linearize_element<Series, T>::element_t return_type;
40  typedef typename return_type::value_t exp_impl_t;
41  typedef typename return_type::monoid_elt_value_t l_monoid_elt_value_t;
42  typedef typename return_type::set_t l_series_set_elt_t;
43  typedef typename l_series_set_elt_t::monoid_t l_monoid_t;
44  typedef typename l_series_set_elt_t::semiring_t l_semiring_t;
45  typedef typename l_monoid_t::alphabet_t l_alphabet_t;
46  typedef typename l_monoid_t::letter_t l_letter_t;
47  typedef typename return_type::monoid_elt_t l_monoid_elt_t;
48  typedef typename return_type::semiring_elt_t l_semiring_elt_t;
49  // Types of the source expression
50  typedef KRatExpLinearize<Series, T, Dispatch> self_t;
51  typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
52  typedef typename Element<Series, T>::monoid_elt_t monoid_elt_t;
53  typedef typename monoid_elt_t::value_t monoid_elt_value_t;
54  INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
55 
56  KRatExpLinearize(const Element<Series, T>& exp) :
57  index_(LINEAR_INDEX_START),
58  exp_(exp),
59  l_alpha_(),
60  l_series_(l_semiring_t (), l_monoid_t (l_alpha_))
61  {
62  }
63 
64  return_type linearize()
65  {
66  // Build a Element with the correct alphabet.
67  return_type result = this->match(exp_.value());
68  l_monoid_t l_monoid(l_alpha_);
69  l_semiring_t l_semiring;
70  return return_type (l_series_set_elt_t (l_semiring, l_monoid),
71  result.value());
72  }
73 
74  MATCH__(Product, lhs, rhs)
75  {
76  return this->match(lhs) * this->match(rhs);
77  }
78  END
79 
80  MATCH__(Sum, lhs, rhs)
81  {
82  return this->match(lhs) + this->match(rhs);
83  }
84  END
85 
86  MATCH_(Star, e)
87  {
88  return this->match(e).star();
89  }
90  END
91 
92  MATCH__(LeftWeight, w, e)
93  {
94  return l_semiring_elt_t(w) * this->match(e);
95  }
96  END
97 
98  MATCH__(RightWeight, e, w)
99  {
100  return this->match(e) * l_semiring_elt_t(w);
101  }
102  END
103 
104  MATCH_(Constant, m)
105  {
106  // Build new constant and update alphabet
107  l_monoid_elt_value_t res;
108  typedef typename monoid_elt_value_t::const_iterator const_iterator;
109  for (const_iterator i = m.begin(); i != m.end(); ++i)
110  {
111  l_alpha_.insert(l_letter_t(*i, index_));
112  res.push_back(l_letter_t(*i, index_));
113  index_++;
114  }
115  // Transform it in the good type
116  return return_type(l_series_, res);
117  }
118  END
119 
120  MATCH(Zero)
121  {
122  return l_series_.zero(SELECT(exp_impl_t));
123  }
124  END
125 
126  MATCH(One)
127  {
128  return l_series_.identity(SELECT(exp_impl_t));
129  }
130  END
131 
132  private:
133  index_t index_;
134  Element<Series, T> exp_;
135  l_alphabet_t l_alpha_;
136  l_series_set_elt_t l_series_;
137  };
138 
139  template <class Series, class T>
140  typename linearize_element<Series, T>::element_t
142  {
143  KRatExpLinearize<Series, T, algebra::DispatchFunction<T> >
144  matcher(exp);
145  return matcher.linearize();
146  }
147 
148 } // vcsn
149 
150 #endif // ! VCSN_ALGORITHMS_KRAT_EXP_LINEARIZE_HXX