17 #ifndef VCSN_ALGORITHMS_AUT_TO_EXP_HXX
18 # define VCSN_ALGORITHMS_AUT_TO_EXP_HXX
22 # include <vaucanson/automata/concept/handlers.hh>
24 # include <vaucanson/misc/usual_macros.hh>
28 # include <vaucanson/automata/concept/automata_base.hh>
43 template <
class Auto_>
44 typename Auto_::hstate_t
45 DefaultChooser::operator()(
const Auto_& a)
const
47 assertion(a.states().size() > 0);
48 typename Auto_::state_iterator s = a.states().begin();
49 typename Auto_::state_iterator k = s;
50 while ((k != a.states().end()) &&
51 ((a.is_initial(*k)) || (a.is_final(*k))))
61 template <
class Auto_>
62 typename Auto_::hstate_t
65 assertion(a.states().size() > 0);
69 for (
typename Auto_::state_iterator i = a.states().begin();
70 i != a.states().end();
83 typename Auto_::state_iterator k = a.states().begin();
85 while (kk <= n || k == a.states().end() ||
86 ((a.is_initial(*k)) || (a.is_final(*k))))
88 if (k == a.states().end())
90 k = a.states().begin();
104 template <
class Auto_>
105 typename Auto_::hstate_t
106 HChooser::operator()(
const Auto_& a)
const
108 assertion(a.states().size() > 0);
110 std::list<typename Auto_::htransition_t> delta_in;
111 std::list<typename Auto_::htransition_t> delta_out;
113 typename Auto_::state_iterator s = a.states().begin();
114 unsigned int d_in = 0;
115 unsigned int d_out = 0;
116 unsigned int max = misc::limits<unsigned int>::max();
117 bool has_loop =
false;
118 bool has_loop_old =
false;
120 for (
typename Auto_::state_iterator i = a.states().begin();
121 i != a.states().end();
124 if (a.is_final(*i) || a.is_initial(*i))
128 for (
typename Auto_::delta_iterator j(a.value(), *i);
131 delta_out.push_back(*j);
132 for (
typename Auto_::rdelta_iterator j(a.value(), *i);
135 delta_in.push_back(*j);
136 for (
typename std::list<typename Auto_::htransition_t>::iterator j = delta_out.begin();
137 j != delta_out.end();
139 if (*i == a.dst_of(*j))
144 d_in = delta_in.size() - 1;
146 d_in = delta_in.size();
147 d_out = delta_out.size();
150 if (d_in * d_out < max ||
151 (d_in * d_out == max &&
152 has_loop_old && not has_loop))
156 has_loop_old = has_loop;
170 template <
class Auto_>
171 typename Auto_::hstate_t
174 assertion(a.states().size() > 0);
176 std::list<typename Auto_::htransition_t> delta_in;
177 std::list<typename Auto_::htransition_t> delta_out;
178 typename Auto_::state_iterator s = a.states().begin();
180 int weight_min = misc::limits<int>::max();
181 for (
typename Auto_::state_iterator i = a.states().begin();
182 i != a.states().end();
185 if (a.is_final(*i) || a.is_initial(*i))
187 unsigned int n_loops = 0;
189 unsigned int out = 0;
195 for (
typename Auto_::delta_iterator j(a.value(), *i);
198 delta_out.push_back(*j);
199 for (
typename Auto_::rdelta_iterator j(a.value(), *i);
202 delta_in.push_back(*j);
204 for (
typename std::list<typename Auto_::htransition_t>::iterator j = delta_out.begin();
205 j != delta_out.end();
207 if (*i == a.dst_of(*j))
210 in = delta_in.size() - n_loops;
211 out = delta_out.size() - n_loops;
214 for (
typename std::list<typename Auto_::htransition_t>::iterator j = delta_in.begin();
217 if (*i != a.dst_of(*j))
219 weight += a.series_value_of(*j).length() * (out - 1);
223 for (
typename std::list<typename Auto_::htransition_t>::iterator j = delta_out.begin();
224 j != delta_out.end();
226 if (*i != a.dst_of(*j))
228 weight += a.series_value_of(*j).length() * (in - 1);
232 for (
typename std::list<typename Auto_::htransition_t>::iterator j = delta_out.begin();
233 j != delta_out.end();
235 if (*i == a.dst_of(*j))
237 weight += a.series_value_of(*j).length() * (in * out - 1);
240 if (weight < weight_min)
253 inline ListChooser::ListChooser(
const std::list<unsigned int>& l) :
259 template <
class Auto_>
260 typename Auto_::hstate_t
261 ListChooser::operator() (
const Auto_& a)
263 assertion(pos_ != list_.end());
264 return a.get_state(*pos_++);
271 template <
class A,
typename AI,
typename Chooser>
272 typename Element<A, AI>::series_set_elt_t
273 do_in_aut_to_exp(
const AutomataBase<A>& a_set,
277 typedef Element<A, AI> automaton_t;
278 AUTOMATON_TYPES(automaton_t);
279 typedef typename automaton_t::series_set_t series_set_t;
280 typedef typename automaton_t::series_set_elt_t series_set_elt_t;
282 typedef typename std::list<htransition_t> htransition_set_t;
283 typedef std::map<hstate_t, series_set_elt_t> sums_t;
285 typename htransition_set_t::const_iterator i, j;
288 std::list<htransition_t> transitions_to_remove;
292 while (a.states().size() != 2)
294 series_set_elt_t loop_sum(a_set.series());
295 sums_t in_sums, out_sums;
298 if (a.is_initial(q) || a.is_final(q))
302 for (delta_iterator e(a.value(), q); ! e.done(); e.next())
303 transitions.push_back(*e);
304 for (i = transitions.begin(); i != transitions.end(); i = j)
308 if (a.dst_of(*i) == q)
309 loop_sum += a.series_of(*i);
312 typename sums_t::iterator f = out_sums.find(a.dst_of(*i));
313 if (f == out_sums.end())
315 (std::make_pair(a.dst_of(*i),
316 series_set_elt_t(a_set.series()))).first;
317 f->second += a.series_of(*i);
319 a.del_transition(*i);
322 for (rdelta_iterator e(a.value(), q); ! e.done(); e.next())
323 transitions.push_back(*e);
324 for (i = transitions.begin(); i != transitions.end(); i = j)
328 typename sums_t::iterator f = in_sums.find(a.src_of(*i));
329 if (f == in_sums.end())
331 (std::make_pair(a.src_of(*i),
332 series_set_elt_t(a_set.series()))).first;
334 f->second += a.series_of(*i);
335 a.del_transition(*i);
339 for_all_const_(sums_t, in, in_sums)
340 for_all_const_(sums_t, out, out_sums)
342 series_set_elt_t res = in->second * loop_sum * out->second;
343 a.add_series_transition(in->first, out->first, res);
347 series_set_elt_t
final(a_set.series());
348 for_all_const_transitions(i, a)
349 final += a.label_of(*i);
357 template<typename A, typename AI, typename Chooser>
358 typename Element<A, AI>::series_set_elt_t
361 BENCH_TASK_SCOPED(
"aut_to_exp");
363 return do_in_aut_to_exp(ret.
structure(), ret, c);
366 template<
typename A,
typename AI>
367 typename Element<A, AI>::series_set_elt_t
375 #endif // ! VCSN_ALGORITHMS_AUT_TO_EXP_HXX