17 #ifndef VCSN_ALGORITHMS_KRAT_EXP_EXPAND_HXX
18 # define VCSN_ALGORITHMS_KRAT_EXP_EXPAND_HXX
25 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
33 template <
typename S,
typename T>
34 struct pseudo_exp_list
36 typedef Element<S, T> exp_t;
37 typedef std::list<exp_t> exp_list_t;
38 typedef typename exp_t::semiring_elt_t semiring_elt_t;
39 typedef std::map<exp_list_t, semiring_elt_t> ret_t;
44 template <
class Series,
class T,
class Dispatch>
45 struct KRatExpExpander : algebra::KRatExpMatcher<
46 KRatExpExpander<Series, T, Dispatch>,
48 typename pseudo_exp_list<Series, T>::ret_t,
53 typedef pseudo_exp_list<Series, T> trait;
54 typedef typename trait::exp_t exp_t;
55 typedef typename trait::ret_t return_type;
56 typedef typename trait::exp_list_t exp_list_t;
57 typedef KRatExpExpander<Series, T, Dispatch> self_t;
58 typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
59 typedef typename semiring_elt_t::value_t semiring_elt_value_t;
60 typedef typename Element<Series, T>::monoid_elt_t monoid_elt_t;
61 typedef typename monoid_elt_t::set_t monoid_t;
62 typedef typename monoid_t::alphabet_t alphabet_t;
63 typedef typename alphabet_t::letter_t letter_t;
64 INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
66 KRatExpExpander(
const exp_t& exp) :
73 exp_t convert(
const return_type& l)
75 typedef typename return_type::const_iterator iterator;
76 typedef typename exp_list_t::const_iterator sub_iterator;
78 exp_t result = exp_.structure().zero(
SELECT(T));
80 for (iterator i = l.begin(); i != l.end(); ++i)
82 exp_list_t l = i->first;
84 for (sub_iterator j = ++l.begin(); j != l.end(); ++j)
86 result += i->second * e;
93 return_type convert(
const exp_t& e)
96 exp_list.push_front(e);
100 exp_.structure().semiring().identity(
SELECT(semiring_elt_value_t));
107 return_type list_union(
const return_type& left,
108 const return_type& right)
110 typedef typename return_type::const_iterator const_iterator;
111 typedef typename return_type::iterator iterator;
113 return_type result(left);
114 for (const_iterator i = right.begin(); i != right.end(); ++i)
116 iterator j = result.find(i->first);
118 if (j != result.end())
119 j->second += i->second;
128 exp_list_t list_concat(
const exp_list_t& left,
const exp_list_t& right)
131 typename T::node_t* lnode = left.back().value().base();
132 typename T::node_t* rnode = right.front().value().base();
135 if (dynamic_cast<typename T::n_one_t*>(lnode))
137 if (dynamic_cast<typename T::n_one_t*>(rnode))
139 if (dynamic_cast<typename T::n_zero_t*>(lnode))
141 if (dynamic_cast<typename T::n_zero_t*>(rnode))
144 typedef typename exp_list_t::const_iterator const_iterator;
145 std::list<exp_t> result(left);
149 typename T::n_const_t* left_node =
150 dynamic_cast<typename T::n_const_t*
>(lnode);
151 typename T::n_const_t* right_node =
152 dynamic_cast<typename T::n_const_t*
>(rnode);
153 if (left_node and right_node)
155 result.back() = exp_t(exp_.structure(),
156 left_node->value_ + right_node->value_);
157 for (const_iterator i = ++right.begin(); i != right.end(); ++i)
158 result.push_back(*i);
161 for (const_iterator i = right.begin(); i != right.end(); ++i)
162 result.push_back(*i);
171 return_type l = this->
match(exp_.value());
176 MATCH__(Product, lhs, rhs)
178 typedef typename return_type::iterator iterator;
180 return_type llist = this->
match(lhs);
181 return_type rlist = this->
match(rhs);
184 for (iterator i = llist.begin(); i != llist.end(); ++i)
185 for (iterator j = rlist.begin(); j != rlist.end(); ++j)
187 exp_list_t p = list_concat(i->first, j->first);
189 iterator pi = result.find(p);
190 if (pi != result.end())
191 pi->second += i->second * j->second;
193 result[p] = i->second * j->second;
199 MATCH__(Sum, lhs, rhs)
201 return list_union(this->
match(lhs), this->
match(rhs));
207 return_type l = this->
match(e);
208 return convert(convert(l).
star());
212 MATCH__(LeftWeight, w, e)
214 typedef typename return_type::iterator iterator;
216 return_type l = this->
match(e);
219 for (iterator i = l.begin(); i != l.end(); ++i)
220 result[i->first] = semiring_elt_t(w) * i->second;
225 MATCH__(RightWeight, e, w)
227 typedef typename return_type::iterator iterator;
229 return_type l = this->
match(e);
232 for (iterator i = l.begin(); i != l.end(); ++i)
233 result[i->first] = i->second * semiring_elt_t(w);
240 return convert(exp_t(exp_.structure(), m));
246 return convert(exp_.structure().zero(
SELECT(T)));
252 return convert(exp_.structure().identity(
SELECT(T)));
257 Element<Series, T> exp_;
262 template <
class S,
class E>
263 E do_expand(
const algebra::SeriesBase<S>&,
const E& exp)
265 typedef typename E::value_t T;
267 algebra::KRatExpExpander< S, T, algebra::DispatchFunction<T> > matcher(exp);
268 return matcher.get();
271 template <
class Series,
class T>
273 expand(
const Element<Series, T>& exp)
275 return do_expand(exp.structure(), exp);
280 #endif // ! VCSN_ALGORITHMS_KRAT_EXP_EXPAND_HXX