18 #ifndef VCSN_ALGORITHMS_IMAGE_HXX
19 # define VCSN_ALGORITHMS_IMAGE_HXX
27 template <
typename auto_t,
typename trans_t>
29 do_fmp_image(
const trans_t& input, auto_t& res,
bool weighted)
31 BENCH_TASK_SCOPED(
"image");
32 AUTOMATON_TYPES_(trans_t, trans_);
33 AUTOMATON_TYPES(auto_t);
35 trans_t fmp_trans =
cut_up(input);
37 typedef typename trans_series_set_elt_t::support_t trans_support_t;
38 std::map<trans_hstate_t, hstate_t> stmap;
40 const series_set_t& series = res.structure().series();
41 const monoid_t& monoid = res.structure().series().monoid();
42 const semiring_elt_t& unit = res.structure().series().semiring().wone_;
43 const trans_monoid_t& trans_monoid =
44 fmp_trans.structure().series().monoid();
46 for_all_const_states(fmp_s, fmp_trans)
48 hstate_t s = res.add_state();
51 if (fmp_trans.is_initial(*fmp_s))
53 const trans_series_set_elt_t trans_series_elt =
54 fmp_trans.get_initial(*fmp_s);
55 trans_support_t trans_supp = trans_series_elt.supp();
56 const trans_monoid_elt_t trans_monoid_elt(trans_monoid,
57 *(trans_supp.begin()));
59 const monoid_elt_value_t word(trans_monoid_elt.value().second);
61 series_set_elt_t series_elt(series);
63 series_elt.assoc(monoid_elt_t(monoid, word),
65 trans_series_elt.get(trans_monoid_elt) : unit);
67 res.set_initial(s, series_elt);
69 if (fmp_trans.is_final(*fmp_s))
71 const trans_series_set_elt_t trans_series_elt =
72 fmp_trans.get_final(*fmp_s);
73 trans_support_t trans_supp = trans_series_elt.supp();
74 const trans_monoid_elt_t trans_monoid_elt(trans_monoid,
75 *(trans_supp.begin()));
77 const monoid_elt_value_t word(trans_monoid_elt.value().second);
79 series_set_elt_t series_elt(series);
81 series_elt.assoc(monoid_elt_t(monoid, word),
82 weighted ? trans_series_elt.get(trans_monoid_elt)
84 res.set_final(s, series_elt);
88 for_all_const_transitions_(trans_, fmp_e, fmp_trans)
90 const trans_series_set_elt_t trans_series_elt =
91 fmp_trans.series_of(*fmp_e);
92 trans_support_t trans_supp = trans_series_elt.supp();
93 for_all_const_(trans_support_t, trans_value, trans_supp)
95 const trans_monoid_elt_t trans_monoid_elt(trans_monoid,
98 const monoid_elt_value_t word(trans_monoid_elt.value().second);
100 series_set_elt_t series_elt(series);
102 series_elt.assoc(monoid_elt_t(monoid, word),
103 weighted ? trans_series_elt.get(trans_monoid_elt)
106 res.add_series_transition(stmap[fmp_trans.src_of(*fmp_e)],
107 stmap[fmp_trans.dst_of(*fmp_e)], series_elt);
113 template<
typename Trans_t,
typename Auto_t>
115 do_rw_image(
const Trans_t& t,
118 BENCH_TASK_SCOPED(
"image (transducer to automaton)");
119 AUTOMATON_TYPES(Trans_t);
120 AUTOMATON_TYPES_(Auto_t, auto_);
121 std::map<hstate_t, auto_hstate_t> m;
123 for_all_const_states(p, t)
124 m[*p] = ret.add_state();
127 algebra::identity_as<monoid_elt_value_t>::of(t.series().monoid());
128 typename Trans_t::series_set_elt_t id_series(t.structure().series());
129 id_series = vcsn::algebra::
130 identity_as<typename Trans_t::series_set_elt_value_t>::
131 of(t.structure().series());
133 for_all_const_initial_states(p, t)
135 if (t.get_initial(*p) != id_series)
137 auto_hstate_t tmp = ret.add_state();
138 ret.set_initial(tmp);
139 ret.add_series_transition(tmp, m[*p], t.get_initial(*p).get(empty));
142 ret.set_initial(m[*p], t.get_initial(*p).get(empty));
145 for_all_const_final_states(p, t)
147 if (t.get_final(*p) != id_series)
149 auto_hstate_t tmp = ret.add_state();
151 ret.add_series_transition(m[*p], tmp, t.get_final(*p).get(empty));
154 ret.set_final(m[*p], t.get_final(*p).get(empty));
157 for_all_const_transitions(e, t)
159 ret.add_series_transition(m[t.src_of(*e)],
166 template <
typename S,
typename T,
typename Auto_t>
168 typename output_projection_helper<S, T>::ret
169 do_rw_image(
const Element<S, T>& t,
171 std::map<typename Auto_t::hstate_t, typename T::hstate_t>& m_)
173 BENCH_TASK_SCOPED(
"image (transducer to automaton)");
174 typedef Element<S, T> Trans_t;
175 AUTOMATON_TYPES(Trans_t);
177 monoid_elt_t empty = t.series().monoid().VCSN_EMPTY_;
178 std::map<hstate_t, hstate_t> m;
180 for_all_const_states(p, t)
182 m[*p] = ret.add_state();
186 for_all_const_initial_states(p, t)
187 ret.set_initial(m[*p], t.get_initial(*p).
get(empty));
189 for_all_const_final_states(p, t)
190 ret.set_final(m[*p], t.get_final(*p).
get(empty));
192 for_all_const_transitions(e, t)
193 ret.add_series_transition(m[t.src_of(*e)], m[t.dst_of(*e)],
206 template <typename S, typename S2,
207 typename T, typename T2,
208 typename ST, typename M1>
210 image_dispatch(const Element<S,T>& src,
211 const TransducerBase<ST>&,
212 const algebra::FreeMonoidBase<M1>&,
213 Element<S2, T2>& dst,
bool)
215 do_rw_image(src, dst);
218 template <
typename S,
typename S2,
219 typename T,
typename T2,
220 typename ST,
typename M1>
222 image_dispatch(
const Element<S,T>& src,
223 const TransducerBase<ST>&,
224 const algebra::FreeMonoidBase<M1>&,
225 Element<S2, T2>& dst,
226 std::map<typename T::hstate_t, typename T2::hstate_t>& m)
228 do_rw_image(src, dst, m);
233 template <
typename S,
typename T,
typename ST>
235 typename output_projection_helper<S, T>::ret
236 image_dispatch2(
const Element<S,T>& src,
237 const TransducerBase<ST>&,
238 std::map<typename T::hstate_t, typename T::hstate_t>& m)
240 typename output_projection_helper<S, T>::ret dst =
241 output_projection_helper<S, T>::make_output_projection_automaton(src);
243 image_dispatch(src, src.structure(),
244 src.structure().series().monoid(), dst, m);
249 template <
typename S,
typename T,
typename ST>
251 typename output_projection_helper<S, T>::ret
252 image_dispatch2(
const Element<S,T>& src,
253 const TransducerBase<ST>&)
255 typename output_projection_helper<S, T>::ret dst =
256 output_projection_helper<S, T>::make_output_projection_automaton(src);
258 image_dispatch(src, src.structure(),
259 src.structure().series().monoid(), dst,
true);
264 template <
typename S,
typename S2,
265 typename T,
typename T2,
266 typename ST,
typename M1>
268 image_dispatch(
const Element<S,T>& src,
269 const AutomataBase<ST>&,
270 const algebra::FreeMonoidProductBase<M1>&,
271 Element<S2, T2>& dst,
bool weighted)
273 do_fmp_image(src, dst, weighted);
280 template <
typename S,
typename T>
282 image(
const Element<S, T>& src,
283 typename output_projection_helper<S, T>::ret& dst,
286 image_dispatch(src, src.structure(),
287 src.structure().series().monoid(),
291 template <
typename S,
typename T>
292 typename output_projection_helper<S, T>::ret
295 return image_dispatch2(src, src.
structure());
298 template <
typename S,
typename T>
299 typename output_projection_helper<S, T>::ret
300 image(
const Element<S, T>& src,
301 std::map<typename T::hstate_t, typename T::hstate_t>& m)
303 return image_dispatch2(src, src.structure(), m);