Vaucanson 1.4
projection.hxx
00001 // projection.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 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 #ifndef VCSN_ALGORITHMS_PROJECTION_HXX
00018 # define VCSN_ALGORITHMS_PROJECTION_HXX
00019 
00020 # include <map>
00021 
00022 # include <vaucanson/algorithms/projection.hh>
00023 
00024 namespace vcsn
00025 {
00026   template <typename auto_t, typename trans_t>
00027   void
00028   set_states(const trans_t& fmp_trans, auto_t& res,
00029              std::map<typename trans_t::hstate_t, typename auto_t::hstate_t>&
00030               stmap)
00031   {
00032     AUTOMATON_TYPES_(trans_t, trans_);
00033     AUTOMATON_TYPES(auto_t);
00034 
00035     typedef typename trans_series_set_elt_t::support_t  trans_support_t;
00036 
00037     const series_set_t&         series = res.structure().series();
00038     const monoid_t&             monoid = res.structure().series().monoid();
00039 
00040     for_all_const_states(fmp_s, fmp_trans)
00041     {
00042       hstate_t s = res.add_state();
00043       stmap[*fmp_s] = s;
00044 
00045       if (fmp_trans.is_initial(*fmp_s))
00046       {
00047         trans_series_set_elt_t  in = fmp_trans.get_initial(*fmp_s);
00048         trans_support_t         supp = in.supp();
00049 
00050         semiring_elt_t          in_semi_elt = in.get(*(supp.begin()));
00051         series_set_elt_t        series_elt(series);
00052 
00053         series_elt.assoc(monoid_elt_t(monoid,
00054                                       algebra::identity_as<
00055                                       monoid_elt_value_t>::
00056                                       of(monoid).value()),
00057                          in_semi_elt);
00058         res.set_initial(s, series_elt);
00059       }
00060 
00061       if (fmp_trans.is_final(*fmp_s))
00062       {
00063         trans_series_set_elt_t  out = fmp_trans.get_final(*fmp_s);
00064         trans_support_t         supp = out.supp();
00065 
00066         semiring_elt_t          out_semi_elt = out.get(*(supp.begin()));
00067         series_set_elt_t        series_elt(series);
00068 
00069         series_elt.assoc(monoid_elt_t(monoid,
00070                                       algebra::identity_as<
00071                                       monoid_elt_value_t>::
00072                                       of(monoid).value()),
00073                          out_semi_elt);
00074         res.set_final(s, series_elt);
00075       }
00076     }
00077   }
00078 
00079   /*---------.
00080   | Identity |
00081   `---------*/
00082 
00083   template <typename S1, typename S2, typename M1, typename M2,
00084             typename auto_t, typename trans_t>
00085   void
00086   do_identity(const AutomataBase<S1>&,
00087               const algebra::FreeMonoidBase<M1>&,
00088               const AutomataBase<S2>&,
00089               const algebra::FreeMonoidProduct<M1,M2>&,
00090               const auto_t& aut, trans_t& res)
00091   {
00092     BENCH_TASK_SCOPED("identity");
00093     AUTOMATON_TYPES_(auto_t, aut_);
00094     AUTOMATON_TYPES(trans_t);
00095 
00096     std::map<hstate_t, aut_hstate_t>    stmap;
00097     typedef typename aut_series_set_elt_t::support_t    aut_support_t;
00098 
00099     const series_set_t&         series = res.structure().series();
00100     const monoid_t&             monoid = res.structure().series().monoid();
00101     const aut_monoid_t&         aut_monoid = aut.structure().series().monoid();
00102 
00103     set_states(aut, res, stmap);
00104 
00105     for_all_const_transitions_(aut_, aut_e, aut)
00106     {
00107       const aut_series_set_elt_t aut_series_elt = aut.series_of(*aut_e);
00108 
00109       // If the transition is labeled by a+bc, we want to output
00110       // two transitions labeled by (a,a) and (bc,bc).
00111       aut_support_t aut_supp = aut_series_elt.supp();
00112       for_all_const_(aut_support_t, label, aut_supp)
00113         {
00114           const aut_monoid_elt_t aut_monoid_elt(aut_monoid, *label);
00115           const monoid_elt_value_t word(aut_monoid_elt.value(),
00116                                         aut_monoid_elt.value());
00117 
00118           series_set_elt_t series_elt(series);
00119           series_elt.assoc(monoid_elt_t(monoid, word),
00120                            aut_series_elt.get(aut_monoid_elt));
00121 
00122           res.add_series_transition(stmap[aut.src_of(*aut_e)],
00123                                     stmap[aut.dst_of(*aut_e)], series_elt);
00124         }
00125     }
00126   }
00127 
00128   template <typename S, typename S2, typename T, typename T2>
00129   void
00130   identity(const Element<S,T>& aut, Element<S2, T2>& res)
00131   {
00132     do_identity(aut.structure(), aut.structure().series().monoid(),
00133                 res.structure(), res.structure().series().monoid(), aut, res);
00134   }
00135 
00136 } // ! vcsn
00137 
00138 #endif // ! VCSN_ALGORITHMS_PROJECTION_HXX