extension.hxx

00001 // extension.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 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_EXTENSION_HXX
00018 # define VCSN_ALGORITHMS_EXTENSION_HXX
00019 
00020 # include <vaucanson/algorithms/extension.hh>
00021 
00022 # include <vaucanson/misc/usual_macros.hh>
00023 
00024 using namespace std;
00025 
00026 namespace vcsn {
00027 
00028   template <typename S, typename T, typename Auto_t>
00029   typename identity_transducer_helper<S, T>::ret
00030   do_extension(const AutomataBase<S>& s,
00031                const Auto_t& a)
00032   {
00033     AUTOMATON_TYPES(Auto_t);
00034     // ret_t is the type of the transducer returned.
00035     typedef typename identity_transducer_helper<S, T>::ret    ret_t;
00036 
00037     AUTOMATON_TYPES_(ret_t, t_);
00038     typedef typename ret_t::set_t                set_t;
00039     typedef typename set_t::series_set_t         o_series_set_t;
00040     typedef typename ret_t::series_set_elt_t     output_series_set_elt_t;
00041     typedef typename series_set_elt_t::support_t support_t;
00042 
00043     set_t
00044       ts(o_series_set_t (a.structure().series(),
00045                          a.structure().series().monoid()));
00046     ret_t          t_ret(ts);
00047 
00048     monoid_elt_t   neutre   = a.series().monoid().empty_;
00049     monoid_elt_t        t_neutre = t_ret.series().monoid().
00050       identity(SELECT(typename t_monoid_elt_t::value_t));
00051 
00052     vector<hstate_t>    conv(a.states().size());
00053 
00054     for_all_states(s, a)
00055       conv[t_ret.add_state()] = *s;
00056 
00057     for_all_transitions(e, a)
00058     {
00059       series_set_elt_t t = a.series_of(*e);
00060       series_set_elt_t s(t);
00061       output_series_set_elt_t os(t_ret.structure().series());
00062       support_t supp = s.supp();
00063       for_all_const_(support_t, m, supp)
00064       {
00065         series_set_elt_t tmp(a.structure().series());
00066         // try to associate the neutral monoid element with a weight
00067         // to create a series which will be a weight in the series os
00068         tmp.assoc(neutre, s.get(*m));
00069         os.assoc(*m, tmp);
00070       }
00071       htransition_t f = t_ret.add_series_transition(conv[a.src_of(*e)],
00072                                                     conv[a.dst_of(*e)],
00073                                                     os);
00074     }
00075 
00076     for_all_initial_states(i, a)
00077     {
00078       series_set_elt_t a_series = a.get_initial(*i);
00079       t_series_set_elt_t s;
00080       s.set(t_neutre, a_series);
00081       t_ret.set_initial(conv[*i], s);
00082     }
00083 
00084     for_all_final_states(f, a)
00085     {
00086       series_set_elt_t a_series = a.get_final(*f);
00087       t_series_set_elt_t s;
00088       s.value_set(t_neutre, a_series);
00089       t_ret.set_final(conv[*f], s);
00090     }
00091 
00092     return t_ret;
00093   }
00094 
00095   template<typename S, typename T>
00096   typename identity_transducer_helper<S, T>::ret extension(const Element<S, T>& a)
00097   {
00098     return do_extension(a.structure(), a);
00099   }
00100 
00102 
00103   template<typename SA, typename ST, typename Auto_t, typename Trans_t>
00104   Trans_t do_extension(const AutomataBase<SA>&,
00105                        const TransducerBase<ST>&,
00106                        const Auto_t& a,
00107                        const Trans_t& t)
00108   {
00109     AUTOMATON_TYPES_(Trans_t, t_);
00110     AUTOMATON_TYPES_(Auto_t, a_);
00111     typedef typename Trans_t::series_set_elt_t  t_output_series_set_elt_t;
00112     typedef typename Auto_t::series_set_elt_t::support_t a_support_t;
00113     typedef typename Trans_t::semiring_elt_t    t_weight_t;
00114 
00115     Trans_t                   tt(t.structure());
00116     map<hstate_t, hstate_t>   conv;
00117 
00118     a_monoid_elt_t a_neutre =
00119       a.series().monoid().identity(SELECT(typename a_monoid_elt_t::value_t));
00120     t_monoid_elt_t t_neutre =
00121       t.series().monoid().identity(SELECT(typename t_monoid_elt_t::value_t));
00122 
00123     for(a_state_iterator p = a.states().begin(); p != a.states().end(); ++p)
00124       conv[*p] = tt.add_state();
00125 
00126     // convert transitions
00127     for(a_transition_iterator e = a.transitions().begin();
00128         e != a.transitions().end(); ++e)
00129     {
00130       a_series_set_elt_t s_ = a.series_of(*e);
00131       a_series_set_elt_t s(s_);
00132 
00133       t_output_series_set_elt_t os(t.structure().series());
00134 
00135       a_support_t supp = s.supp();
00136       for(typename a_support_t::const_iterator m = supp.begin();
00137           m != supp.end(); ++m)
00138       {
00139         t_weight_t tmp(t.structure().series().semiring());
00140         tmp.assoc(a_neutre, s.get(*m));
00141         os.assoc(a_monoid_elt_t (a.structure().series().monoid(), *m), tmp);
00142       }
00143 
00144       tt.add_series_transition(conv[a.src_of(*e)], conv[a.dst_of(*e)], os);
00145     }
00146 
00147     for(a_initial_iterator p = a.initial().begin();
00148         p != a.initial().end();
00149         ++p)
00150     {
00151       a_series_set_elt_t a_series = a.get_initial(*p);
00152       t_series_set_elt_t s (t.structure().series());
00153       s.assoc(t_neutre, a_series);
00154       tt.set_initial(conv[*p], s);
00155     }
00156 
00157     for(a_final_iterator p = a.final().begin();
00158         p != a.final().end();
00159         ++p)
00160     {
00161       a_series_set_elt_t a_series = a.get_final(*p);
00162       t_series_set_elt_t s (t.structure().series());
00163       s.assoc(t_neutre, a_series);
00164       tt.set_final(conv[*p], s);
00165     }
00166 
00167     return tt;
00168   }
00169 
00170   template<typename SA, typename TA, typename ST, typename TT>
00171   Element<ST, TT> extension(const Element<SA, TA>& a, const Element<ST, TT>& t)
00172   {
00173     return do_extension(a.structure(), t.structure(), a, t);
00174   }
00175 
00176 }
00177 
00178 #endif // ! VCSN_ALGORITHMS_EXTENSION_HXX

Generated on Sat Jul 29 17:12:59 2006 for Vaucanson by  doxygen 1.4.6