18 #ifndef VCSN_ALGORITHMS_STANDARD_HXX
19 # define VCSN_ALGORITHMS_STANDARD_HXX
23 # include <vaucanson/algorithms/internal/has_neighbour.hh>
24 # include <vaucanson/algorithms/internal/build_pattern.hh>
27 # include <vaucanson/automata/concept/automata_base.hh>
28 # include <vaucanson/automata/concept/automata.hh>
29 # include <vaucanson/misc/usual_macros.hh>
39 template<
typename HSTATE,
typename WEIGHT>
45 const WEIGHT& weight_;
48 basic_sptr(
const HSTATE& src,
const HSTATE& dst,
const WEIGHT& weight) :
49 src_(src), dst_(dst), weight_(weight) {}
59 WEIGHT weight()
const {
69 template <
typename A,
typename AI1,
typename AI2>
71 do_union_here(
const AutomataBase<A>&,
73 const Element<A, AI2>& rhs)
75 typedef Element<A, AI1> lhs_t;
76 AUTOMATON_TYPES(lhs_t);
77 typedef Element<A, AI2> rhs_t;
78 typedef typename rhs_t::hstate_t r_hstate_t;
80 std::map<r_hstate_t, hstate_t> states_map;
83 for_all_states(it, rhs)
85 hstate_t new_state = lhs.add_state();
86 states_map[*it] = new_state;
88 lhs.set_initial(new_state, rhs.get_initial(*it));
89 lhs.set_final(new_state, rhs.get_final(*it));
93 for_all_transitions(it, rhs)
95 lhs.add_transition(states_map[rhs.src_of(*it)],
96 states_map[rhs.dst_of(*it)],
102 template<
typename A,
typename AI1,
typename AI2>
106 do_union_here(lhs.
structure(), lhs, rhs);
110 template<
typename A,
typename AI1,
typename AI2>
115 do_union_here(ret.
structure(), ret, rhs);
121 template <
typename A,
typename AI1,
typename AI2>
123 marked_union_here(
const AutomataBase<A>&,
124 Element<A, AI1>& lhs,
125 const Element<A, AI2>& rhs,
126 const typename Element<A, AI1>::hstate_t& lhs_stt,
127 typename Element<A, AI1>::hstate_t& mrk_stt,
128 const typename Element<A, AI2>::hstate_t& rhs_stt)
130 typedef Element<A, AI1> lhs_t;
131 AUTOMATON_TYPES(lhs_t);
132 typedef Element<A, AI2> rhs_t;
133 typedef typename rhs_t::hstate_t r_hstate_t;
134 std::map<r_hstate_t, hstate_t> states_map;
137 for_all_states(it, rhs)
139 hstate_t new_state = lhs.add_state();
140 states_map[*it] = new_state;
142 lhs.set_initial(new_state, rhs.get_initial(*it));
143 lhs.set_final(new_state, rhs.get_final(*it));
145 mrk_stt = states_map[rhs_stt];
148 for_all_transitions(it, rhs)
150 lhs.add_transition(states_map[rhs.src_of(*it)],
151 states_map[rhs.dst_of(*it)],
159 template<
typename A,
typename AI>
161 do_is_standard(
const AutomataBase<A>&,
const Element<A, AI>& a)
163 BENCH_TASK_SCOPED(
"is_standard");
164 typedef Element<A, AI> automaton_t;
165 typedef typename automaton_t::series_set_elt_value_t series_set_elt_value_t;
168 if (a.initial().size() != 1)
172 typename automaton_t::hstate_t s = *a.initial().begin();
174 != a.series().identity(
SELECT(series_set_elt_value_t)))
181 template<
typename A,
typename AI>
192 template<
typename S,
typename AI>
194 do_standardize(
const AutomataBase<S>&, Element<S, AI>& a)
196 BENCH_TASK_SCOPED(
"standardize");
197 typedef Element<S, AI> automaton_t;
198 AUTOMATON_TYPES(automaton_t);
199 typedef basic_sptr<hstate_t, series_set_elt_t> sptr_t;
200 typedef typename std::list<hstate_t> stt_lst_t;
203 series_set_elt_t fin_wgt =
204 algebra::zero_as<series_set_elt_value_t>::of(a.series());
208 hstate_t i = a.add_state();
211 for_all_initial_states(it, a) stt_list.push_back(*it);
213 for_all(typename stt_lst_t, it, stt_list)
215 hstate_t ini_stt = *it;
216 series_set_elt_t ini_wgt = a.get_initial(ini_stt);
219 sptr_t t(i, ini_stt, ini_wgt);
222 a.unset_initial(ini_stt);
225 a.del_state(ini_stt);
234 template<
typename A,
typename AI>
245 template <
typename A,
typename AI>
247 sum_of_standard_inside(
const AutomataBase<A>&, Element<A, AI>& aut,
248 const typename Element<A, AI>::hstate_t& aut_stt,
249 typename Element<A, AI>::hstate_t& mrk_stt)
251 typedef Element<A, AI> automaton_t; AUTOMATON_TYPES(automaton_t);
252 typedef series_set_elt_t weight_t;
253 typedef basic_sptr<hstate_t, weight_t> sptr_t;
255 weight_t unit = aut.series().identity(
SELECT(series_set_elt_value_t));
257 sptr_t t(aut_stt, mrk_stt, unit);
259 aut.del_state(mrk_stt);
263 template <
typename A,
typename AI1,
typename AI2>
265 do_sum_of_standard_here(
const AutomataBase<A>&,
266 Element<A, AI1>& lhs,
267 const Element<A, AI2>& rhs)
269 BENCH_TASK_SCOPED(
"sum_of_standard");
271 typedef Element<A, AI1> lhs_t;
272 AUTOMATON_TYPES(lhs_t);
273 typedef Element<A, AI2> rhs_t;
274 typedef typename rhs_t::hstate_t r_hstate_t;
279 hstate_t lhs_stt = *lhs.initial().begin();
280 r_hstate_t rhs_stt = *rhs.initial().begin();
282 marked_union_here(lhs.structure(), lhs, rhs, lhs_stt, mrk_stt, rhs_stt);
284 sum_of_standard_inside(lhs.structure(), lhs, lhs_stt, mrk_stt);
288 template<
typename A,
typename AI1,
typename AI2>
294 do_sum_of_standard_here(lhs.
structure(), lhs, rhs);
297 template<
typename A,
typename AI1,
typename AI2>
313 template <
typename A,
typename AI>
315 concat_of_standard_inside(
const AutomataBase<A>&, Element<A, AI>& aut,
316 typename std::list<
typename Element<A, AI>::hstate_t>& stt_list,
317 typename Element<A, AI>::hstate_t& mrk_stt)
319 typedef Element<A, AI> aut_t;
320 AUTOMATON_TYPES(aut_t);
321 typedef series_set_elt_t weight_t;
322 typedef typename std::list<hstate_t> stt_lst_t;
323 typedef basic_sptr<hstate_t, weight_t> sptr_t;
325 weight_t mrk_wgt = aut.get_final(mrk_stt);
327 for_all(
typename stt_lst_t, it, stt_list)
329 hstate_t fin_stt = *it;
330 weight_t fin_wgt = aut.get_final(fin_stt);
331 aut.unset_final(fin_stt);
332 sptr_t t(fin_stt, mrk_stt, fin_wgt);
335 aut.del_state(mrk_stt);
339 template <
typename A,
typename AI1,
typename AI2>
341 do_concat_of_standard_here(
const AutomataBase<A>&,
342 Element<A, AI1>& lhs,
343 const Element<A, AI2>& rhs)
345 BENCH_TASK_SCOPED(
"concat_of_standard");
347 typedef Element<A, AI1> lhs_t;
348 AUTOMATON_TYPES(lhs_t);
349 typedef std::list<hstate_t> stt_lst_t;
350 typedef Element<A, AI2> rhs_t;
351 typedef typename rhs_t::hstate_t r_hstate_t;
355 for_all_final_states(it, lhs) stt_list.push_back(*it);
359 hstate_t lhs_stt = *lhs.initial().begin();
360 r_hstate_t rhs_stt = *rhs.initial().begin();
361 marked_union_here(lhs.structure(), lhs, rhs, lhs_stt, mrk_stt, rhs_stt);
363 concat_of_standard_inside(lhs.structure(), lhs, stt_list, mrk_stt);
367 template<typename A, typename AI1, typename AI2>
373 do_concat_of_standard_here(lhs.structure(), lhs, rhs);
376 template<
typename A,
typename AI1,
typename AI2>
384 do_concat_of_standard_here(ret.
structure(), ret, rhs);
393 template <
typename A,
typename AI>
395 left_ext_mult_of_standard_inside(
const AutomataBase<A>&,
397 const typename Element<A, AI>::hstate_t& ini_stt,
398 const typename Element<A, AI>::series_set_elt_t& k)
400 typedef Element<A, AI> automaton_t; AUTOMATON_TYPES(automaton_t);
401 typedef series_set_elt_t weight_t;
402 typedef typename std::list<htransition_t> trn_lst_t;
404 weight_t zero(aut.series().zero_);
405 trn_lst_t ini_out_list;
408 for (delta_iterator it(aut.value(), ini_stt); !it.done(); it.next())
409 ini_out_list.push_back(*it);
418 for_all(
typename trn_lst_t, it, ini_out_list)
419 aut.del_transition(*it);
421 aut.unset_final(ini_stt);
426 for_all(
typename trn_lst_t, it, ini_out_list)
428 hstate_t dest = aut.dst_of(*it);
429 weight_t wgt = k * aut.series_of(*it);
430 aut.del_transition(*it);
431 aut.add_series_transition(ini_stt, dest, wgt);
434 aut.set_final(ini_stt, k * aut.get_final(ini_stt));
438 template <
typename A,
typename AI>
444 left_ext_mult_of_standard_inside(aut.
structure(), aut,
445 *aut.initial().begin(), k);
452 template <
typename A,
typename AI>
454 right_ext_mult_of_standard_inside
455 (
const AutomataBase<A>&, Element<A, AI>& aut,
456 typename std::list<
typename Element<A, AI>::hstate_t>& stt_list,
457 const typename Element<A, AI>::series_set_elt_t& k)
459 typedef Element<A, AI> automaton_t; AUTOMATON_TYPES(automaton_t);
460 typedef typename std::list<hstate_t> stt_lst_t;
464 for_all(
typename stt_lst_t, it, stt_list)
465 aut.set_final(*it, aut.get_final(*it) * k);
468 template <typename A, typename AI>
471 const typename
Element<A, AI>::series_set_elt_t& k)
474 AUTOMATON_TYPES(automaton_t);
477 std::list<hstate_t> finals;
478 for_all_final_states(it, aut) finals.push_back(*it);
480 right_ext_mult_of_standard_inside(aut.structure(), aut,
488 template <
typename A,
typename AI>
490 star_of_standard_inside(
const AutomataBase<A>&, Element<A, AI>& aut,
491 const typename Element<A, AI>::hstate_t& ini_stt,
492 typename std::list<
typename Element<A, AI>::hstate_t>& stt_list)
494 typedef Element<A, AI> automaton_t; AUTOMATON_TYPES(automaton_t);
495 typedef series_set_elt_t weight_t;
496 typedef typename std::list<hstate_t> stt_lst_t;
497 typedef basic_sptr<hstate_t, weight_t> sptr_t;
498 weight_t unit = aut.series().identity(
SELECT(series_set_elt_value_t));
502 weight_t fin_wgt = aut.get_final(ini_stt); fin_wgt.star();
505 aut.set_final(ini_stt, unit);
506 left_ext_mult_of_standard_inside(aut.structure(), aut, ini_stt, fin_wgt);
510 for_all(
typename stt_lst_t, it, stt_list)
512 hstate_t crn_stt = *it;
513 weight_t crn_fin_wgt = aut.get_final(crn_stt);
514 aut.unset_final(crn_stt);
516 sptr_t t(crn_stt, ini_stt, crn_fin_wgt);
522 template <
typename A,
typename AI>
524 do_star_of_standard_here(
const AutomataBase<A>&, Element<A, AI>& a)
526 BENCH_TASK_SCOPED(
"star_of_standard");
527 typedef Element<A, AI> automaton_t; AUTOMATON_TYPES(automaton_t);
528 typedef series_set_elt_t weight_t;
529 typedef std::list<hstate_t> stt_lst_t;
531 hstate_t ini_stt = *a.initial().begin();
532 weight_t fin_wgt = a.get_final(ini_stt);
534 precondition(fin_wgt.starable());
537 for_all_final_states(it, a)
539 stt_list.push_back(*it);
541 star_of_standard_inside(a.structure(), a, ini_stt, stt_list);
544 template<typename A, typename AI>
549 do_star_of_standard_here(a.structure(), a);
552 template<
typename A,
typename AI>
558 do_star_of_standard_here(ret.
structure(), ret);
564 #endif // ! VCSN_ALGORITHMS_STANDARD_HXX