18 #ifndef VCSN_ALGORITHMS_NORMALIZED_HXX
19 # define VCSN_ALGORITHMS_NORMALIZED_HXX
22 # include <vaucanson/algorithms/internal/has_neighbour.hh>
24 # include <vaucanson/automata/concept/automata_base.hh>
25 # include <vaucanson/misc/usual_macros.hh>
35 template <
typename A,
typename AI>
37 do_normalize_here(
const AutomataBase<A>&,
40 BENCH_TASK_SCOPED(
"normalize");
41 typedef Element<A, AI> automaton_t;
42 AUTOMATON_TYPES(automaton_t);
44 hstate_t h = a.add_state();
46 for_all_const_initial_states(i, a)
47 a.add_series_transition(h, *i, a.get_initial(*i));
54 for_all_const_final_states(i, a)
55 a.add_series_transition(*i, h, a.get_final(*i));
61 template <typename A, typename AI>
66 do_normalize_here(result.
structure(), result);
70 template<
typename A,
typename AI>
82 template <
typename A,
typename AI1,
typename AI2>
84 do_union_of_normalized_here(
const AutomataBase<A>&,
86 const Element<A, AI2>& rhs)
88 BENCH_TASK_SCOPED(
"union_of_normalized");
89 typedef Element<A, AI1> lhs_t;
90 typedef Element<A, AI2> rhs_t;
91 AUTOMATON_TYPES(lhs_t);
92 monoid_elt_t monoid_identity =
93 lhs.series().monoid().identity(
SELECT(monoid_elt_value_t));
94 hstate_t new_i = lhs.add_state();
95 hstate_t new_f = lhs.add_state();
99 for_all_const_initial_states(i, lhs)
100 lhs.add_spontaneous(new_i, *i, lhs.get_initial(*i).
get(monoid_identity));
103 for_all_const_final_states(f, lhs)
104 lhs.add_spontaneous(*f, new_f, lhs.get_final(*f).
get(monoid_identity));
107 lhs.set_final(new_f);
108 lhs.set_initial(new_i);
111 template<typename A, typename AI1, typename AI2>
116 do_union_of_normalized_here(lhs.structure(), lhs, rhs);
119 template<
typename A,
typename AI1,
typename AI2>
125 do_union_of_normalized_here(ret.
structure(), ret, rhs);
132 template <
typename A,
typename AI>
134 do_is_normalized(
const AutomataBase<A>&,
135 const Element<A, AI>& a)
137 BENCH_TASK_SCOPED(
"is_normalized");
138 typedef Element<A, AI> automaton_t;
139 typedef typename automaton_t::series_set_elt_value_t series_set_elt_value_t;
140 typedef typename automaton_t::series_set_elt_t series_set_elt_t;
142 series_set_elt_t series_identity =
143 a.series().identity(
SELECT(series_set_elt_value_t));
145 if (a.initial().size() != 1
146 || a.final().size() != 1
147 || a.get_initial(*a.initial().begin()) != series_identity
148 || a.get_final(*a.final().begin()) != series_identity)
157 template<
typename A,
typename AI>
161 return do_is_normalized(a.
structure(), a);
167 template <
typename A,
typename AI1,
typename AI2>
169 do_concatenate_of_normalized_here(
const AutomataBase<A>&,
170 Element<A, AI1>& lhs,
171 const Element<A, AI2>& rhs)
173 BENCH_TASK_SCOPED(
"concatenate_of_normalized");
174 typedef Element<A, AI1> lhs_t;
175 typedef Element<A, AI2> rhs_t;
176 AUTOMATON_TYPES(rhs_t);
177 typedef std::map<hstate_t, hstate_t> map_lhs_rhs_t;
178 typedef std::list<htransition_t> delta_ret_t;
180 hstate_t glue_state = *lhs.final().begin();
189 bool merge_lhs_final_and_rhs_initial =
190 (lhs.get_final(*lhs.final().begin()).value() ==
191 lhs.series().identity(
SELECT(series_set_elt_value_t)) &&
192 rhs.get_initial(*rhs.initial().begin()).value() ==
193 rhs.series().identity(
SELECT(series_set_elt_value_t)));
195 for_all_const_states(s, rhs)
199 if (merge_lhs_final_and_rhs_initial)
201 if (rhs.is_initial(*s))
202 new_state = glue_state;
204 new_state = lhs.add_state();
207 new_state = lhs.add_state();
208 map_h[*s] = new_state;
209 lhs.set_final(new_state, rhs.get_final(*s));
215 for_all_const_transitions(t, rhs)
216 lhs.add_transition(map_h[rhs.src_of(*t)],
217 map_h[rhs.dst_of(*t)],
223 if (!merge_lhs_final_and_rhs_initial)
225 monoid_elt_t monoid_identity =
226 rhs.series().monoid().identity(
SELECT(monoid_elt_value_t));
227 hstate_t i = *rhs.initial().begin();
228 hstate_t f = *lhs.final().begin();
230 lhs.add_spontaneous(f, map_h[i],
231 lhs.get_final(f).get(monoid_identity) *
232 rhs.get_initial(i).get(monoid_identity));
234 lhs.unset_initial(map_h[i]);
238 template<
typename A,
typename AI1,
typename AI2>
242 do_concatenate_of_normalized_here(lhs.
structure(), lhs, rhs);
245 template<
typename A,
typename AI1,
typename AI2>
250 do_concatenate_of_normalized_here(ret.
structure(), ret, rhs);
257 template <
typename A,
typename AI>
259 do_star_of_normalized_here(
const AutomataBase<A>&, Element<A, AI>& a)
261 BENCH_TASK_SCOPED(
"star_of_normalized");
262 typedef Element<A, AI> automaton_t;
263 AUTOMATON_TYPES(automaton_t);
264 monoid_elt_t monoid_identity =
265 a.series().monoid().identity(
SELECT(monoid_elt_value_t));
266 hstate_t old_i = *a.initial().begin();
267 hstate_t old_f = *a.final().begin();
268 hstate_t new_i = a.add_state();
269 hstate_t new_f = a.add_state();
271 a.add_spontaneous(old_f, old_i, a.get_initial(old_i).get(monoid_identity));
272 a.add_spontaneous(new_i, old_i, a.get_initial(old_i).get(monoid_identity));
273 a.add_spontaneous(old_f, new_f, a.get_final(old_f).get(monoid_identity));
274 a.add_spontaneous(new_i, new_f);
276 a.unset_final(old_f);
277 a.set_initial(new_i);
278 a.unset_initial(old_i);
281 template<
typename A,
typename AI>
285 do_star_of_normalized_here(a.
structure(), a);
288 template<
typename A,
typename AI>
293 do_star_of_normalized_here(ret.
structure(), ret);
299 #endif // ! VCSN_ALGORITHMS_NORMALIZED_HXX