fmp_to_realtime.hxx

00001 // fmp_to_realtime.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006 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_FMP_TO_REALTIME_HXX
00018 # define VCSN_ALGORITHMS_FMP_TO_REALTIME_HXX
00019 
00020 # include <vaucanson/automata/concept/automata.hh>
00021 # include <vaucanson/automata/concept/transducer.hh>
00022 # include <vaucanson/algebra/concept/freemonoid_product.hh>
00023 
00024 # include <map>
00025 
00026 namespace vcsn
00027 {
00028 
00029   template<typename S, typename T,
00030            typename SS, typename TT,
00031            typename Self>
00032   void
00033   do_fmp_to_realtime(const vcsn::AutomataBase<S>&,
00034                      const vcsn::TransducerBase<SS>&,
00035                      const vcsn::algebra::FreeMonoidProductBase<Self>&,
00036                      const vcsn::Element<S, T>& fmp,
00037                      vcsn::Element<SS, TT>& res)
00038   {
00039     // Map source automaton's states with result's states
00040     std::map<vcsn::hstate_t, vcsn::hstate_t> m;
00041 
00042     // Input FMP type
00043     typedef vcsn::Element<S, T> FMP_t;
00044 
00045     // Output transducer type
00046     typedef vcsn::Element<SS, TT> Trans_t;
00047 
00048     /*-------------------------.
00049     | Creating the transducer. |
00050     `-------------------------*/
00051 
00052     // Adding states
00053     for (typename FMP_t::state_iterator St = fmp.states().begin();
00054          St != fmp.states().end();
00055          ++St)
00056       m[*St] = res.add_state();
00057 
00058 
00059     /*-------------------------.
00060     | Setting initial states.  |
00061     `-------------------------*/
00062 
00063     for (typename FMP_t::initial_iterator St, next = fmp.initial().begin();
00064          next != fmp.initial().end();)
00065     {
00066       //We need to store the next iterator before using the current one
00067       //to avoid an invalid iterator after having called set_final.
00068       //Indeed, set_final can delete the iterator if its second parameter
00069       //is the zero of the serie.
00070       St = next;
00071       next++;
00072       //Series to be created
00073       typename Trans_t::series_set_elt_t s(res.structure().series());
00074 
00075       typename FMP_t::series_set_elt_t s_elt = fmp.get_initial(*St);
00076       for_all_const_(FMP_t::series_set_elt_t::support_t, i, s_elt.supp())
00077       {
00078         typename Trans_t::semiring_elt_value_t::monoid_elt_value_t
00079           output_monoid_value;
00080         typename Trans_t::semiring_elt_t::semiring_elt_t weight;
00081 
00082         typename Trans_t::monoid_elt_value_t
00083           input_monoid_value = (*i).first;
00084         typename Trans_t::monoid_elt_t
00085           input_monoid(res.structure().series().monoid(),
00086                        input_monoid_value);
00087 
00088         typename Trans_t::semiring_elt_t::monoid_elt_t
00089           output_monoid(res.structure().series().semiring().monoid(),
00090                         (*i).second);
00091         weight = s_elt.get(*i);
00092 
00093         //Creating the element multiplicity
00094         typename Trans_t::semiring_elt_t
00095           out_mult(res.structure().series().semiring());
00096         out_mult.assoc(output_monoid, weight);
00097 
00098         //Associating it to the input monoid
00099         s.assoc(input_monoid, out_mult);
00100       }
00101       res.set_initial(m[*St], s);
00102     }
00103 
00104 
00105     /*------------------------.
00106     | Setting final states.   |
00107     `------------------------*/
00108 
00109     for (typename FMP_t::final_iterator St, next = fmp.final().begin();
00110          next != fmp.final().end();)
00111     {
00112       //We need to store the next iterator before using the current one
00113       //to avoid an invalid iterator after having called set_final.
00114       //Indeed, set_final can delete the iterator if its second parameter
00115       //is the zero of the serie.
00116       St = next;
00117       next++;
00118       //Series to be created
00119       typename Trans_t::series_set_elt_t s(res.structure().series());
00120 
00121       typename FMP_t::series_set_elt_t s_elt = fmp.get_final(*St);
00122       for_all_const_(FMP_t::series_set_elt_t::support_t, i, s_elt.supp())
00123       {
00124         typename Trans_t::semiring_elt_value_t::monoid_elt_value_t
00125           output_monoid_value;
00126         typename Trans_t::semiring_elt_t::semiring_elt_t weight;
00127 
00128         typename Trans_t::monoid_elt_value_t input_monoid_value =
00129           (*i).first;
00130         typename Trans_t::monoid_elt_t
00131           input_monoid(res.structure().series().monoid(),
00132                        input_monoid_value);
00133         typename Trans_t::semiring_elt_t::monoid_elt_t
00134           output_monoid(res.structure().series().semiring().monoid(),
00135                         (*i).second);
00136         weight = s_elt.get(*i);
00137 
00138         //Creating the element multiplicity
00139         typename Trans_t::semiring_elt_t
00140           out_mult(res.structure().series().semiring());
00141         out_mult.assoc(output_monoid, weight);
00142 
00143         //Associating it to the input monoid
00144         s.assoc(input_monoid, out_mult);
00145       }
00146       res.set_final(m[*St], s);
00147     }
00148 
00149 
00150     /*-----------------------.
00151     | Creating transitions.  |
00152     `-----------------------*/
00153 
00154     for (typename FMP_t::transition_iterator Ed = fmp.transitions().begin();
00155          Ed != fmp.transitions().end();
00156          ++Ed)
00157     {
00158       // FIXME
00159       // No Special Treatment is done for 0 weighted
00160       typename Trans_t::series_set_elt_t
00161         transition_value(res.structure().series());
00162 
00163       typename Trans_t::monoid_elt_value_t input_monoid_value;
00164       typename Trans_t::semiring_elt_value_t::monoid_elt_value_t
00165         output_monoid_value;
00166 
00167       typename FMP_t::series_set_elt_t series_fmp(fmp.structure().series());
00168       typename Trans_t::semiring_elt_t
00169         out_mult(res.structure().series().semiring());
00170       series_fmp = fmp.series_of(*Ed);
00171 
00172       for_all_const_(FMP_t::series_set_elt_t::support_t,
00173                      i,
00174                      series_fmp.supp())
00175       {
00176         input_monoid_value = (*i).first;
00177         output_monoid_value = (*i).second;
00178 
00179         //Creating the transition's semiring value
00180         typename Trans_t::semiring_elt_t::semiring_elt_t
00181           transition_weight(res.structure().series().semiring().semiring(),
00182                             series_fmp.get(*i));
00183         typename Trans_t::semiring_elt_t
00184           out_mult(res.structure().series().semiring());
00185         out_mult.assoc(typename Trans_t::semiring_elt_t::monoid_elt_t
00186                        (res.structure().series().semiring().monoid(),
00187                         output_monoid_value),
00188                        transition_weight);
00189 
00190         //Creating the transition's monoid value
00191         typename Trans_t::monoid_elt_t
00192           input(res.structure().series().monoid(),
00193                 input_monoid_value);
00194         transition_value.assoc(input, out_mult);
00195         res.add_series_transition(m[fmp.src_of(*Ed)],
00196                                   m[fmp.dst_of(*Ed)],
00197                                   transition_value);
00198       }
00199     }
00200   }
00201 
00202   template<typename S, typename T,
00203            typename SS, typename TT>
00204   vcsn::Element<SS, TT>&
00205   fmp_to_realtime(const vcsn::Element<S, T>& fmp,
00206                   vcsn::Element<SS, TT>& res)
00207   {
00208     TIMER_SCOPED("fmp_to_realtime");
00209     do_fmp_to_realtime(fmp.structure(), res.structure(),
00210                        fmp.structure().series().monoid(),
00211                        fmp, res);
00212     return res;
00213   }
00214 }
00215 #endif // ! VCSN_ALGORITHMS_FMP_TO_REALTIME_HXX

Generated on Sun Jul 29 19:35:19 2007 for Vaucanson by  doxygen 1.5.2