Vaucanson 1.4
pair_to_fmp.hxx
00001 // pair_to_fmp.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 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 #ifndef VCSN_ALGORITHMS_PAIR_TO_FMP_HXX
00018 # define VCSN_ALGORITHMS_PAIR_TO_FMP_HXX
00019 
00020 # include <vaucanson/algorithms/pair_to_fmp.hh>
00021 
00022 namespace vcsn
00023 {
00024 // Helper to write lighter code.
00025 # define MUTE_TRAITS mute_pair_to_fmp<S, T>
00026 
00027   template <typename S, typename T>
00028   inline typename MUTE_TRAITS::ret
00029   MUTE_TRAITS::
00030   make_automaton(const typename MUTE_TRAITS::ret_first_alphabet_t& A,
00031                  const typename MUTE_TRAITS::ret_second_alphabet_t& B)
00032   {
00033     semiring_t semiring;
00034     first_monoid_t fM(A);
00035     second_monoid_t sM(B);
00036     ret_monoid_t freemonoidproduct(fM, sM);
00037     typename ret::series_set_t series(semiring, freemonoidproduct);
00038 
00039     return ret(Automata<typename ret::series_set_t, typename ret::kind_t>(series));
00040   }
00041 
00042   template <typename S, typename T>
00043   inline typename MUTE_TRAITS::ret
00044   MUTE_TRAITS::
00045   make_automaton(const Element<S, T>& aut)
00046   {
00047     ret_first_alphabet_t fA = alphabet_traits_t::first_projection(aut.
00048         series().monoid().alphabet());
00049     ret_second_alphabet_t sA = alphabet_traits_t::second_projection(aut.
00050         series().monoid().alphabet());
00051 
00052     return make_automaton(fA, sA);
00053   }
00054 
00055   template <typename S, typename T>
00056   inline typename MUTE_TRAITS::ret::series_set_elt_t
00057   MUTE_TRAITS::
00058   series_convert(const typename MUTE_TRAITS::ret_series_set_t& series,
00059                  const typename MUTE_TRAITS::automaton_t::
00060                  series_set_elt_t& ss)
00061   {
00062     typedef typename MUTE_TRAITS::automaton_t::series_set_elt_t
00063         series_set_elt_t;
00064 
00065     typename ret::series_set_elt_t R(series);
00066 
00067     for_all_const_(series_set_elt_t::support_t, it, ss.supp())
00068     {
00069       R.assoc(std::make_pair(word_traits_t::first_projection(*it),
00070                              word_traits_t::second_projection(*it)),
00071               ss.get(*it));
00072     }
00073 
00074     return R;
00075   }
00076 
00077   template <typename S, typename T>
00078   void
00079   do_pair_to_fmp(const Element<S, T>& src,
00080                  typename MUTE_TRAITS::ret& res)
00081   {
00082     BENCH_TASK_SCOPED("pair_to_fmp");
00083 
00084     // Type helpers.
00085     typedef typename MUTE_TRAITS::automaton_t automaton_t;
00086     typedef MUTE_TRAITS fmp_traits_t;
00087     typedef typename fmp_traits_t::ret res_t;
00088     AUTOMATON_TYPES(automaton_t);
00089     AUTOMATON_TYPES_(res_t, res_);
00090 
00091     // Helper map.
00092     std::map<hstate_t, res_hstate_t> m;
00093 
00094     for_all_const_states(p, src)
00095       m[*p] = res.add_state();
00096 
00097     // Setup initial transitions.
00098     for_all_const_initial_states(i, src)
00099     {
00100       res.set_initial(m[*i],
00101                       fmp_traits_t::
00102                       series_convert(res.structure().series(),
00103                                      src.get_initial(*i)));
00104     }
00105 
00106     // Setup normal transitions.
00107     for_all_const_transitions(e, src)
00108     {
00109       res.add_series_transition(m[src.src_of(*e)],
00110                                 m[src.dst_of(*e)],
00111                                 fmp_traits_t::
00112                                 series_convert(res.structure().series(),
00113                                                src.series_of(*e)));
00114     }
00115 
00116     // Setup final transitions.
00117     for_all_const_final_states(f, src)
00118     {
00119       res.set_final(m[*f],
00120                     fmp_traits_t::
00121                     series_convert(res.structure().series(),
00122                                    src.get_final(*f)));
00123     }
00124   }
00125 
00126   //
00127   // Facade Functions
00128   //
00129 
00130   template <typename S, typename T>
00131   void
00132   pair_to_fmp(const Element<S, T>& aut,
00133               typename MUTE_TRAITS::ret& res)
00134   {
00135     do_pair_to_fmp(aut, res);
00136   }
00137 
00138   template <typename S, typename T>
00139   typename MUTE_TRAITS::ret
00140   pair_to_fmp(const Element<S, T>& aut)
00141   {
00142     typename MUTE_TRAITS::ret res = MUTE_TRAITS::make_automaton(aut);
00143 
00144     do_pair_to_fmp(aut, res);
00145 
00146     return res;
00147   }
00148 
00149 # undef MUTE_TRAITS
00150 
00151 } // ! vcsn
00152 
00153 #endif // ! VCSN_ALGORITHMS_PAIR_TO_FMP_HXX