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, 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_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 AI1, typename AI2>
00037   void
00038   do_sum(const AutomataBase<A>&, Element<A, AI1>& lhs, const Element<A, AI2>& rhs)
00039   {
00040     typedef Element<A, AI1> lhs_t;
00041     typedef Element<A, AI2> rhs_t;
00042     typedef typename rhs_t::state_iterator state_iterator;
00043 
00044     /*--------------.
00045     | Sum of states |
00046     `--------------*/
00047     std::map<typename rhs_t::hstate_t, typename lhs_t::hstate_t> states_map;
00048 
00049     for_all_const_states(i, rhs)
00050     {
00051       typename lhs_t::hstate_t new_state = lhs.add_state();
00052       states_map[*i] = new_state;
00053 
00054       //  lhs.history().set_state_event_about(INSUM_EVENT, new_state, *i);
00055 
00056       lhs.set_final(new_state, rhs.get_final(*i));
00057       lhs.set_initial(new_state, rhs.get_initial(*i));
00058     }
00059 
00060     /*---------------------.
00061     | Sum of transitions.  |
00062     `---------------------*/
00063 
00064     typedef std::list<typename rhs_t::htransition_t> dst_t;
00065     dst_t dst;
00066 
00067     for_all_const_states(i, rhs)
00068     {
00069       dst.clear();
00070       rhs.deltac(dst, *i, delta_kind::transitions());
00071       for (typename dst_t::const_iterator d = dst.begin();
00072            d != dst.end();
00073            ++d)
00074       {
00075         lhs.add_transition(states_map[rhs.src_of(*d)],
00076                            states_map[rhs.dst_of(*d)],
00077                            rhs.label_of(*d));
00078 
00079         //  lhs.history().set_transition_event_about(INSUM_EVENT,
00080         //                                           new_transition, *d);
00081       }
00082     }
00083   }
00084 
00085   // wrappers
00086   template<typename A, typename AI1, typename AI2>
00087   void
00088   sum_here(Element<A, AI1>& lhs, const Element<A, AI2>& rhs)
00089   {
00090     do_sum(lhs.structure(), lhs, rhs);
00091   }
00092 
00093   template<typename A, typename AI1, typename AI2>
00094   Element<A, AI1>
00095   sum(const Element<A, AI1>& lhs, const Element<A, AI2>& rhs)
00096   {
00097     Element<A, AI1> ret(lhs);
00098     //    ret.history().set_auto_event_about(INSUM_EVENT, lhs, rhs);
00099     do_sum(ret.structure(), ret, rhs);
00100     return ret;
00101   }
00102 
00103 } // vcsn
00104 
00105 #endif // ! VCSN_ALGORITHMS_SUM_HXX

Generated on Thu Oct 9 20:22:41 2008 for Vaucanson by  doxygen 1.5.1