00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00030 # define INSUM_EVENT "in place sum "
00031
00032
00033
00034
00035
00036 template <typename A, typename lhs_t, typename rhs_t>
00037 void
00038 do_sum(const AutomataBase<A>&, lhs_t& lhs, const rhs_t& rhs)
00039 {
00040 typedef typename rhs_t::state_iterator state_iterator;
00041
00042
00043
00044
00045 std::map<typename rhs_t::hstate_t, typename lhs_t::hstate_t> states_map;
00046
00047 for_all_const_states(i, rhs)
00048 {
00049 typename lhs_t::hstate_t new_state = lhs.add_state();
00050 states_map[*i] = new_state;
00051
00052
00053
00054 lhs.set_final(new_state, rhs.get_final(*i));
00055 lhs.set_initial(new_state, rhs.get_initial(*i));
00056 }
00057
00058
00059
00060
00061
00062 typedef std::list<typename rhs_t::htransition_t> dst_t;
00063 dst_t dst;
00064
00065 for_all_const_states(i, rhs)
00066 {
00067 dst.clear();
00068 rhs.deltac(dst, *i, delta_kind::transitions());
00069 for (typename dst_t::const_iterator d = dst.begin();
00070 d != dst.end();
00071 ++d)
00072 {
00073 lhs.add_transition(states_map[rhs.src_of(*d)],
00074 states_map[rhs.dst_of(*d)],
00075 rhs.label_of(*d));
00076
00077
00078
00079 }
00080 }
00081 }
00082
00083
00084 template<typename A, typename T, typename U>
00085 void
00086 sum_here(Element<A, T>& lhs, const Element<A, U>& rhs)
00087 {
00088
00089 do_sum(lhs.structure(), lhs, rhs);
00090 }
00091
00092 template<typename A, typename T, typename U>
00093 Element<A, T>
00094 sum(const Element<A, T>& lhs, const Element<A, U>& rhs)
00095 {
00096
00097 Element<A, T> ret(lhs);
00098
00099 do_sum(ret.structure(), ret, rhs);
00100 return ret;
00101 }
00102
00103 }
00104
00105 #endif // ! VCSN_ALGORITHMS_SUM_HXX