sum.hxx

00001 // sum.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_SUM_HXX
00018 # define VCSN_ALGORITHMS_SUM_HXX
00019 
00020 # include <vaucanson/algorithms/sum.hh>
00021 
00022 # include <vaucanson/automata/concept/automata_base.hh>
00023 
00024 # include <set>
00025 # include <map>
00026 
00027 namespace vcsn {
00028 
00029   // FIXME : this should be defined differently :
00030 # define INSUM_EVENT    "in place sum "
00031 
00032   /*------.
00033   |  sum  |
00034   `------*/
00035 
00036   template <typename A, typename lhs_t, typename rhs_t>
00037   void do_sum(const AutomataBase<A>& ,
00038               lhs_t& lhs,
00039               const rhs_t& rhs)
00040   {
00041     /*--------------.
00042     | Sum of states |
00043     `--------------*/
00044     std::map<hstate_t, hstate_t> states_map;
00045 
00046     for (typename rhs_t::state_iterator i = rhs.states().begin();
00047          i != rhs.states().end();
00048          ++i)
00049     {
00050       hstate_t new_state = lhs.add_state();
00051       states_map[*i] = new_state;
00052 
00053       //  lhs.history().set_state_event_about(INSUM_EVENT, new_state, *i);
00054 
00055       lhs.set_final(new_state, rhs.get_final(*i));
00056       lhs.set_initial(new_state, rhs.get_initial(*i));
00057     }
00058 
00059     /*-------------------.
00060     | Sum of transitions |
00061     `-------------------*/
00062 
00063     typedef std::set<htransition_t> aim_t;
00064     aim_t aim;
00065 
00066     for (typename rhs_t::state_iterator i = rhs.states().begin();
00067          i != rhs.states().end();
00068          ++i)
00069     {
00070       aim.clear();
00071       rhs.deltac(aim, *i, delta_kind::transitions());
00072       for (typename aim_t::const_iterator d = aim.begin();
00073            d != aim.end();
00074            ++d)
00075       {
00076         lhs.add_transition(states_map[rhs.src_of(*d)],
00077                            states_map[rhs.dst_of(*d)],
00078                            rhs.label_of(*d));
00079 
00080         //  lhs.history().set_transition_event_about(INSUM_EVENT,
00081         //                                           new_transition, *d);
00082       }
00083     }
00084   }
00085 
00086   // wrappers
00087   template<typename A, typename T, typename U>
00088   void sum_here(Element<A, T>& lhs, const Element<A, U>& rhs)
00089   {
00090     // assertion(lhs.structure() == rhs.structure())
00091     do_sum(lhs.structure(), lhs, rhs);
00092   }
00093 
00094   template<typename A, typename T, typename U>
00095   Element<A, T>
00096   sum(const Element<A, T>& lhs, const Element<A, U>& rhs)
00097   {
00098     // assertion(lhs.structure() == rhs.structure())
00099     Element<A, T> ret(lhs);
00100     //    ret.history().set_auto_event_about(INSUM_EVENT, lhs, rhs);
00101     do_sum(ret.structure(), ret, rhs);
00102     return ret;
00103   }
00104 
00105 } // vcsn
00106 
00107 #endif // ! VCSN_ALGORITHMS_SUM_HXX

Generated on Sat Jul 29 17:13:11 2006 for Vaucanson by  doxygen 1.4.6