Vaucanson 1.4
transducer.hxx
00001 // transducer.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_AUTOMATA_CONCEPT_TRANSDUCER_HXX
00018 # define VCSN_AUTOMATA_CONCEPT_TRANSDUCER_HXX
00019 
00020 # include <vaucanson/automata/concept/transducer_base.hh>
00021 
00022 namespace vcsn {
00023 
00024   template <typename Series, typename Kind>
00025   Transducer<Series, Kind>::Transducer(const series_set_t& s):
00026     SetSlot<Series>(s)
00027   {}
00028 
00029   template <typename Series, typename Kind>
00030   const typename Transducer<Series, Kind>::series_set_t&
00031   Transducer<Series, Kind>::series() const
00032   {
00033     return this->_structure_get();
00034   }
00035 
00036   template <typename Series, typename Kind>
00037   bool
00038   operator==(const Transducer<Series, Kind>& lhs,
00039              const Transducer<Series, Kind>& rhs)
00040   {
00041     return & lhs.series() == & rhs.series();
00042   }
00043 
00044   //
00045   // Projections for RW transducers
00046   //
00047 
00048   template <typename S, typename K, typename T>
00049   inline typename input_projection_helper<Transducer<S, K>, T>::ret
00050   input_projection_helper<Transducer<S, K>, T>::
00051   make_input_projection_automaton(const Element<Transducer<S, K>, T>& t)
00052   {
00053     // Type helpers.
00054     typedef typename ret::set_t set_t;
00055     typedef typename set_t::series_set_t series_set_t;
00056 
00057     set_t
00058     auto_set(series_set_t(t.structure().series().
00059                           semiring().semiring(), t.structure().
00060                           series().monoid()));
00061 
00062     return ret(auto_set);
00063   }
00064 
00065   template <typename S, typename K, typename T>
00066   inline typename output_projection_helper<Transducer<S, K>, T>::ret
00067   output_projection_helper<Transducer<S, K>, T>::
00068   make_output_projection_automaton(const Element<Transducer<S, K>, T>& t)
00069   {
00070     // Type helpers.
00071     typedef typename ret::set_t set_t;
00072     typedef typename set_t::series_set_t series_set_t;
00073 
00074     set_t
00075     auto_set(series_set_t(t.structure().series().semiring()));
00076 
00077     return ret(auto_set);
00078   }
00079 
00080   //
00081   // Projections for FMP transducers
00082   //
00083 
00084   template <typename S, typename K, typename T>
00085   inline typename input_projection_helper<Automata<S, K>, T>::ret
00086   input_projection_helper<Automata<S, K>, T>::
00087   make_input_projection_automaton(const Element<Automata<S, K>, T>& t)
00088   {
00089     // Type helpers.
00090     typedef typename ret::set_t set_t;
00091     typedef typename set_t::series_set_t series_set_t;
00092 
00093     set_t
00094     auto_set(series_set_t(t.structure().series().
00095                           semiring(), t.structure().series().
00096                           monoid().first_monoid()));
00097 
00098     return ret(auto_set);
00099   }
00100 
00101   template <typename S, typename K, typename T>
00102   inline typename output_projection_helper<Automata<S, K>, T>::ret
00103   output_projection_helper<Automata<S, K>, T>::
00104   make_output_projection_automaton(const Element<Automata<S, K>, T>& t)
00105   {
00106     // Type helpers.
00107     typedef typename ret::set_t set_t;
00108     typedef typename set_t::series_set_t series_set_t;
00109 
00110     set_t
00111     auto_set(series_set_t(t.structure().series().
00112                           semiring(), t.structure().series().
00113                           monoid().second_monoid()));
00114 
00115     return ret(auto_set);
00116   }
00117 
00118   template <typename S, typename K, typename T>
00119   typename identity_transducer_helper<S, K, T>::ret
00120   partial_identity(const Element<S, T>& a)
00121   {
00122     typedef Element<S, T> automaton_t;
00123     AUTOMATON_TYPES(automaton_t);
00124     typedef typename identity_transducer_helper<S, K, T>::ret  ret_t;
00125     typedef typename ret_t::series_set_elt_t output_series_set_elt_t;
00126     typedef typename series_set_elt_t::support_t support_t;
00127     typedef typename ret_t::set_t set_t;
00128     typedef typename set_t::series_set_t o_series_set_t;
00129     set_t s (o_series_set_t (a.structure().series(),
00130                              a.structure().series().monoid()));
00131     ret_t ret(s);
00132     std::vector<hstate_t> conv(a.states().size());
00133 
00134     for_all_states(s, a)
00135       conv[ret.add_state()] = *s;
00136     for_all_transitions(e, a)
00137     {
00138       series_set_elt_t t = a.series_of(*e);
00139       series_set_elt_t s(t);
00140       output_series_set_elt_t os(ret.structure().series());
00141       support_t supp = s.supp();
00142       for_all_const_(support_t, m, supp)
00143       {
00144         series_set_elt_t tmp(a.structure().series());
00145         tmp.assoc(*m, s.get(*m));
00146         os.assoc(*m, tmp);
00147       }
00148       htransition_t f = ret.add_series_transition(conv[a.src_of(*e)],
00149                                                   conv[a.dst_of(*e)],
00150                                                   os);
00151     }
00152     // FIXME: set initial/final weights.
00153     for_all_initial_states(i, a)
00154       ret.set_initial(conv[*i], a.get_initial(*i));
00155     for_all_final_states(f, a)
00156       ret.set_final(conv[*f], a.get_final(*f));
00157     return ret;
00158   }
00159 
00160 } // vcsn
00161 
00162 #endif // ! VCSN_AUTOMATA_CONCEPT_TRANSDUCER_HXX