• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

image.hxx

00001 // image.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2006, 2008 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 
00018 #ifndef VCSN_ALGORITHMS_IMAGE_HXX
00019 # define VCSN_ALGORITHMS_IMAGE_HXX
00020 
00021 # include <vaucanson/algorithms/image.hh>
00022 # include <vaucanson/algorithms/projection.hh>
00023 
00024 namespace vcsn
00025 {
00026   template <typename auto_t, typename trans_t>
00027   static void
00028   do_fmp_image(const trans_t& fmp_trans, auto_t& res)
00029   {
00030     BENCH_TASK_SCOPED("image");
00031     AUTOMATON_TYPES_(trans_t, trans_);
00032     AUTOMATON_TYPES(auto_t);
00033 
00034     typedef typename trans_series_set_elt_t::support_t  trans_support_t;
00035     std::map<trans_hstate_t, hstate_t>  stmap;
00036 
00037     const series_set_t&         series = res.structure().series();
00038     const monoid_t&             monoid = res.structure().series().monoid();
00039     const trans_monoid_t&       trans_monoid =
00040       fmp_trans.structure().series().monoid();
00041 
00042    for_all_const_states(fmp_s, fmp_trans)
00043     {
00044       hstate_t s = res.add_state();
00045       stmap[*fmp_s] = s;
00046 
00047       if (fmp_trans.is_initial(*fmp_s))
00048       {
00049       const trans_series_set_elt_t      trans_series_elt =
00050         fmp_trans.get_initial(*fmp_s);
00051       trans_support_t                   trans_supp = trans_series_elt.supp();
00052                 const trans_monoid_elt_t        trans_monoid_elt
00053              (trans_monoid, *(trans_supp.begin()));
00054 
00055         const monoid_elt_value_t        word(trans_monoid_elt.value().second);
00056 
00057         series_set_elt_t                series_elt(series);
00058 
00059         series_elt.assoc(monoid_elt_t(monoid, word),
00060                        trans_series_elt.get(trans_monoid_elt));
00061 
00062             res.set_initial(s, series_elt);
00063       }
00064       if (fmp_trans.is_final(*fmp_s))
00065       {
00066       const trans_series_set_elt_t      trans_series_elt =
00067         fmp_trans.get_final(*fmp_s);
00068       trans_support_t                   trans_supp = trans_series_elt.supp();
00069         const trans_monoid_elt_t        trans_monoid_elt
00070              (trans_monoid, *(trans_supp.begin()));
00071 
00072         const monoid_elt_value_t        word(trans_monoid_elt.value().second);
00073 
00074         series_set_elt_t                series_elt(series);
00075 
00076         series_elt.assoc(monoid_elt_t(monoid, word),
00077                        trans_series_elt.get(trans_monoid_elt));
00078 
00079             res.set_final(s, series_elt);
00080       }
00081     }
00082 
00083     for_all_const_transitions_(trans_, fmp_e, fmp_trans)
00084     {
00085       const trans_series_set_elt_t      trans_series_elt =
00086         fmp_trans.series_of(*fmp_e);
00087       trans_support_t                   trans_supp = trans_series_elt.supp();
00088       const trans_monoid_elt_t  trans_monoid_elt
00089         (trans_monoid, *(trans_supp.begin()));
00090 
00091       const monoid_elt_value_t  word(trans_monoid_elt.value().second);
00092 
00093       series_set_elt_t          series_elt(series);
00094 
00095       series_elt.assoc(monoid_elt_t(monoid, word),
00096                        trans_series_elt.get(trans_monoid_elt));
00097 
00098       res.add_series_transition(stmap[fmp_trans.src_of(*fmp_e)],
00099                                 stmap[fmp_trans.dst_of(*fmp_e)], series_elt);
00100     }
00101   }
00102 
00103 
00104 
00105   template<typename Trans_t, typename Auto_t>
00106   static void
00107   do_rw_image(const Trans_t& t,
00108               Auto_t& ret)
00109   {
00110     BENCH_TASK_SCOPED("image (transducer to automaton)");
00111     AUTOMATON_TYPES(Trans_t);
00112     AUTOMATON_TYPES_(Auto_t, auto_);
00113     std::map<hstate_t, auto_hstate_t> m;
00114 
00115     for_all_const_states(p, t)
00116       m[*p] = ret.add_state();
00117 
00118     monoid_elt_t empty =
00119       algebra::identity_as<monoid_elt_value_t>::of(t.series().monoid());
00120     typename Trans_t::series_set_elt_t id_series(t.structure().series());
00121     id_series = vcsn::algebra::
00122       identity_as<typename Trans_t::series_set_elt_value_t>::
00123       of(t.structure().series());
00124 
00125     for_all_const_initial_states(p, t)
00126     {
00127       if (t.get_initial(*p) != id_series)
00128       {
00129         auto_hstate_t tmp = ret.add_state();
00130         ret.set_initial(tmp);
00131         ret.add_series_transition(tmp, m[*p], t.get_initial(*p).get(empty));
00132       }
00133       else
00134         ret.set_initial(m[*p], t.get_initial(*p).get(empty));
00135     }
00136 
00137     for_all_const_final_states(p, t)
00138     {
00139       if (t.get_final(*p) != id_series)
00140       {
00141         auto_hstate_t tmp = ret.add_state();
00142         ret.set_final(tmp);
00143         ret.add_series_transition(m[*p], tmp, t.get_final(*p).get(empty));
00144       }
00145       else
00146         ret.set_final(m[*p], t.get_final(*p).get(empty));
00147     }
00148 
00149     for_all_const_transitions(e, t)
00150     {
00151       ret.add_series_transition(m[t.src_of(*e)],
00152                                 m[t.dst_of(*e)],
00153                                 t.output_of(*e));
00154     }
00155   }
00156 
00157 
00158   template <typename S, typename T, typename Auto_t>
00159   static
00160   typename output_projection_helper<S, T>::ret
00161   do_rw_image(const Element<S, T>& t,
00162               Auto_t& ret,
00163               std::map<typename Auto_t::hstate_t, typename T::hstate_t>& m_)
00164   {
00165     BENCH_TASK_SCOPED("image (transducer to automaton)");
00166     typedef Element<S, T>  Trans_t;
00167     AUTOMATON_TYPES(Trans_t);
00168 
00169     monoid_elt_t empty = t.series().monoid().VCSN_EMPTY_;
00170     std::map<hstate_t, hstate_t> m;
00171 
00172     for_all_const_states(p, t)
00173     {
00174       m[*p] = ret.add_state();
00175       m_[m[*p]] = *p;
00176     }
00177 
00178     for_all_const_initial_states(p, t)
00179       ret.set_initial(m[*p], t.get_initial(*p).get(empty));
00180 
00181     for_all_const_final_states(p, t)
00182       ret.set_final(m[*p], t.get_final(*p).get(empty));
00183 
00184     for_all_const_transitions(e, t)
00185       ret.add_series_transition(m[t.src_of(*e)], m[t.dst_of(*e)],
00186                                 t.output_of(*e));
00187 
00188     return ret;
00189   }
00190 
00191 
00192 
00193   /*-------------.
00194   | DISPATCHERS. |
00195   `-------------*/
00196 
00197   // Dispatchers for RW transducers.
00198   template <typename S, typename S2,
00199             typename T, typename T2,
00200             typename ST, typename M1>
00201   static void
00202   image_dispatch(const Element<S,T>& src,
00203                  const TransducerBase<ST>&,
00204                  const algebra::FreeMonoidBase<M1>&,
00205                  Element<S2, T2>& dst)
00206   {
00207     do_rw_image(src, dst);
00208   }
00209 
00210   template <typename S, typename S2,
00211             typename T, typename T2,
00212             typename ST, typename M1>
00213   static void
00214   image_dispatch(const Element<S,T>& src,
00215                  const TransducerBase<ST>&,
00216                  const algebra::FreeMonoidBase<M1>&,
00217                  Element<S2, T2>& dst,
00218                  std::map<typename T::hstate_t, typename T2::hstate_t>& m)
00219   {
00220     do_rw_image(src, dst, m);
00221   }
00222 
00223   // Dispatch and build returned object for RW transducers. A map between
00224   // states of the resulting automaton and the tranducer is filled.
00225   template <typename S, typename T, typename ST>
00226   static
00227   typename output_projection_helper<S, T>::ret
00228   image_dispatch2(const Element<S,T>& src,
00229                   const TransducerBase<ST>&,
00230                   std::map<typename T::hstate_t, typename T::hstate_t>& m)
00231   {
00232     typename output_projection_helper<S, T>::ret dst =
00233         output_projection_helper<S, T>::make_output_projection_automaton(src);
00234 
00235     image_dispatch(src, src.structure(),
00236                    src.structure().series().monoid(), dst, m);
00237     return dst;
00238   }
00239 
00240   // Dispatch and build returned object for RW transducers
00241   template <typename S, typename T, typename ST>
00242   static
00243   typename output_projection_helper<S, T>::ret
00244   image_dispatch2(const Element<S,T>& src,
00245                   const TransducerBase<ST>&)
00246   {
00247     typename output_projection_helper<S, T>::ret dst =
00248         output_projection_helper<S, T>::make_output_projection_automaton(src);
00249 
00250     image_dispatch(src, src.structure(),
00251                    src.structure().series().monoid(), dst);
00252     return dst;
00253   }
00254 
00255   // Dispatcher for transducers
00256   template <typename S, typename S2,
00257             typename T, typename T2,
00258             typename ST, typename M1>
00259   static void
00260   image_dispatch(const Element<S,T>& src,
00261                  const AutomataBase<ST>&,
00262                  const algebra::FreeMonoidProductBase<M1>&,
00263                  Element<S2, T2>& dst)
00264   {
00265     do_fmp_image(src, dst);
00266   }
00267 
00268 
00269 
00270   /*---------------.
00271   | IMAGE FACADES. |
00272   `---------------*/
00273 
00274   template <typename S, typename T>
00275   void
00276   image(const Element<S, T>& src, typename output_projection_helper<S, T>::ret& dst)
00277   {
00278     image_dispatch(src, src.structure(),
00279                    src.structure().series().monoid(),
00280                    dst);
00281   }
00282 
00283   template <typename S, typename T>
00284   typename output_projection_helper<S, T>::ret
00285   image(const Element<S, T>& src)
00286   {
00287     return image_dispatch2(src, src.structure());
00288   }
00289 
00290   template <typename S, typename T>
00291   typename output_projection_helper<S, T>::ret
00292   image(const Element<S, T>& src,
00293         std::map<typename T::hstate_t, typename T::hstate_t>& m)
00294   {
00295     return image_dispatch2(src, src.structure(), m);
00296   }
00297 
00298 } // End of namespace vcsn.
00299 
00300 #endif      /* !VCSN_ALGORITHMS_IMAGE_HXX */

Generated on Fri Jul 8 2011 22:06:59 for Vaucanson by  doxygen 1.7.1