18 #ifndef VCSN_ALGORITHMS_INVERT_HXX
19 # define VCSN_ALGORITHMS_INVERT_HXX
23 # include <vaucanson/automata/concept/automata_base.hh>
24 # include <vaucanson/automata/concept/transducer.hh>
25 # include <vaucanson/algebra/concept/freemonoid_product.hh>
26 # include <vaucanson/algebra/implementation/series/series.hh>
28 # include <vaucanson/misc/usual_macros.hh>
40 template<
typename S,
typename T>
42 do_invert_rw(Element<S, T>& t,
45 typedef Element<S, T> Trans_t;
46 typedef typename output_projection_helper<S, T>::ret Auto_t;
47 AUTOMATON_TYPES(Trans_t);
51 std::map<hstate_t, hstate_t> map_t_u;
52 for_all_const_states (p, t)
53 map_t_u[*p] = u.add_state ();
55 initial_iterator i_it;
56 for (initial_iterator next = t.initial().begin(), next_end = t.initial().end();
65 u.set_initial (map_t_u[*i_it]);
69 for (final_iterator next = t.final().begin(), next_end = t.final().end();
78 u.set_final (map_t_u[*f_it]);
81 for_all_const_transitions (e, t)
83 semiring_elt_t exp = t.output_of(*e);
85 typename Auto_t::set_t auto_structure
86 (
typename Auto_t::set_t::series_set_t
87 (t.structure().series().semiring()));
88 Auto_t auto_tmp(auto_structure);
95 std::map<hstate_t, hstate_t> map_auto_u;
97 for_all_const_states (p, auto_tmp)
98 if (auto_tmp.is_initial (*p))
99 map_auto_u[*p] = map_t_u[t.src_of(*e)];
101 map_auto_u[*p] = u.add_state();
103 typename semiring_elt_t::semiring_elt_t
104 o_sm_zero (u.structure().series().semiring().semiring());
105 monoid_elt_t monoid_identity = u.series().monoid().VCSN_EMPTY_;
107 monoid_elt_t a (u.structure().series().monoid());
108 semiring_elt_t sm (u.structure().series().semiring());
109 series_set_elt_t s (u.structure().series());
111 for (typename Auto_t::transition_iterator f =
112 auto_tmp.transitions().begin();
113 f != auto_tmp.transitions().end();
116 a = auto_tmp.word_of (*f);
118 s.assoc (a, algebra::identity_as<semiring_elt_value_t>
119 ::of(u.series().semiring()));
121 u.add_series_transition (map_auto_u[auto_tmp.src_of(*f)],
122 map_auto_u[auto_tmp.dst_of(*f)],
127 for (
typename Auto_t::final_iterator q = auto_tmp.final().begin();
128 q != auto_tmp.final().end();
131 typedef typename semiring_elt_t::semiring_elt_t output_sm_t;
132 typedef typename output_sm_t::value_t output_sm_value_t;
134 sm.assoc (a, algebra::identity_as<output_sm_value_t>
135 ::of(u.series().semiring().semiring()));
136 s.assoc(monoid_identity, sm);
137 u.add_series_transition(map_auto_u[*q],
138 map_t_u[t.dst_of(*e)],
155 template<
typename S,
typename T,
156 typename SS,
typename TT>
157 static void invert_label(
const Element<S, T>& label,
158 Element<SS, TT>& res)
160 typedef Element<S, T> series_set_elt_t;
161 typedef typename series_set_elt_t::monoid_elt_t::value_t
162 res_monoid_elt_value_t;
165 for_all_const_(series_set_elt_t::support_t, w, label.supp())
167 res.assoc(res_monoid_elt_value_t((*w).second, (*w).first),
172 template<
typename S,
typename T,
173 typename SS,
typename TT>
175 do_invert_tdc(
const Element<S, T>& t,
178 typedef Element<S, T> Trans_t;
179 typedef Element<SS, TT> res_t;
180 AUTOMATON_TYPES(Trans_t);
181 AUTOMATON_TYPES_(res_t, res_);
183 std::map<hstate_t, hstate_t> map_t_u;
184 for_all_const_states (p, t)
185 map_t_u[*p] = u.add_state ();
188 for_all_const_initial_states (p, t)
190 res_series_set_elt_t s (u.structure().series());
191 invert_label(t.get_initial(*p), s);
192 u.set_initial (map_t_u[*p], s);
195 for_all_const_final_states (p, t)
197 res_series_set_elt_t s (u.structure().series());
198 invert_label(t.get_final(*p), s);
199 u.set_final (map_t_u[*p], s);
203 for_all_const_transitions (e, t)
205 res_series_set_elt_t s (u.structure().series());
206 res_monoid_elt_t a (u.structure().series().monoid());
209 series_set_elt_t label(t.structure().series());
210 label = t.series_of(*e);
212 invert_label(label, s);
214 u.add_series_transition(map_t_u[t.src_of(*e)],
215 map_t_u[t.dst_of(*e)],
228 template<
typename S,
typename T,
229 typename M1,
typename M2>
231 do_invert_fmp(
const algebra::FreeMonoidProduct<M1, M2>&,
232 const Element<S, T>& t)
235 typedef Element<S, T> trans_t;
236 AUTOMATON_TYPES(trans_t);
238 typedef algebra::FreeMonoidProduct<
239 typename trans_t::series_set_t::monoid_t::second_monoid_t,
240 typename trans_t::series_set_t::monoid_t::first_monoid_t>
243 typedef algebra::Series<
typename trans_t::series_set_t::semiring_t,
247 res_monoid_t monoid(t.structure().series().monoid().second_monoid(),
248 t.structure().series().monoid().first_monoid());
251 res_series_set_t series(t.structure().series().semiring(), monoid);
253 Automata<series_set_t, kind_t> trans_set(series);
255 typedef Element< Automata<series_set_t, kind_t>, T> res_trans_t;
256 res_trans_t* res =
new res_trans_t(trans_set);
258 return do_invert_tdc(t, *res);
262 template<
typename S,
typename T,
263 typename SS,
typename TT,
264 typename M1,
typename M2>
266 do_invert_fmp(
const algebra::FreeMonoidProduct<M1, M2>&,
267 const Element<S, T>& t,
268 Element<SS, TT>& res)
270 return do_invert_tdc(t, res);
274 template<
typename S,
typename T>
276 do_invert(
const AutomataBase<S>&,
277 const Element<S, T>& t)
279 return do_invert_fmp(t.structure().series().monoid(), t);
282 template<
typename S,
typename T>
284 do_invert(
const AutomataBase<S>&,
285 const Element<S, T>& t,
288 return do_invert_fmp(t.structure().series().monoid(), t, res);
296 template<
typename S,
typename T>
298 do_invert(
const TransducerBase<S>&,
299 const Element<S, T>& t)
302 typedef Element<S, T> Trans_t;
303 AUTOMATON_TYPES(Trans_t);
305 monoid_t o_mon_structure
306 (t.structure().series().monoid());
307 typename semiring_t::semiring_t sm_sm_structure
308 (t.structure().series().semiring().semiring());
309 typename semiring_t::monoid_t i_mon_structure
310 (t.structure().series().semiring().monoid());
312 semiring_t sm_structure (sm_sm_structure, o_mon_structure);
313 series_set_t ss_structure(sm_structure, i_mon_structure);
315 automata_set_t auto_structure(ss_structure);
317 Trans_t* res =
new Trans_t(auto_structure);
320 do_invert_rw(src, *res);
325 template<
typename S,
typename T>
327 do_invert(
const TransducerBase<S>&,
328 const Element<S, T>& t,
331 Element<S, T> src(t);
332 return do_invert_rw(src, res);
342 template<
typename S,
typename T>
344 invert(
const Element<S, T>& t)
346 BENCH_TASK_SCOPED(
"invert");
347 return do_invert(t.structure(), t);
350 template<
typename S,
typename T>
352 invert(
const Element<S, T>& t,
355 BENCH_TASK_SCOPED(
"invert");
356 do_invert(t.structure(), t, res);
360 #endif // !VCSN_ALGORITHMS_INVERT_HXX