Vaucanson  1.4.1
letter_to_letter_composition.hxx
1 // letter_to_letter_composition.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGORITHMS_LETTER_TO_LETTER_COMPOSITION_HXX
18 # define VCSN_ALGORITHMS_LETTER_TO_LETTER_COMPOSITION_HXX
19 
20 # include <vaucanson/automata/concept/transducer_base.hh>
21 # include <vaucanson/misc/usual_macros.hh>
22 
23 # include <set>
24 # include <map>
25 
26 namespace vcsn {
27 
28 
29  template <class Self, class T>
30  Element<Self, T>
31  do_letter_to_letter_composition(const TransducerBase<Self>& s,
32  const Element<Self, T>& f,
33  const Element<Self, T>& g)
34  {
35  typedef Element<Self, T> transducer_t;
36  AUTOMATON_TYPES(transducer_t);
37  typedef std::map<std::pair<hstate_t, hstate_t>, hstate_t> assoc_t;
38  typedef std::list<htransition_t> delta_ret_t;
39 
40  semiring_t output_series(f.series().semiring().semiring(),
41  f.series().semiring().monoid());
42  series_set_t series(output_series, g.series().monoid());
43  automata_set_t structure(series);
44  transducer_t output(s);
45  assoc_t conv;
46  series_set_elt_t zero =
47  algebra::zero_as<series_set_elt_value_t>::of(output.series());
48 
49  for_all_const_states(s, f)
50  for_all_const_states(t, g)
51  {
52  hstate_t ns = output.add_state();
53  conv[std::make_pair(*s, *t)] = ns;
54  output.set_initial(ns,
55  f.get_initial(*s) * g.get_initial(*t));
56  output.set_final(ns,
57  f.get_final(*s) * g.get_final(*t));
58 
59  }
60 
61  for_all_const_states(s, f)
62  for_all_const_states(t, g)
63  {
64  for (delta_iterator lhs_e(f.value(), *s); ! lhs_e.done(); lhs_e.next())
65  {
66  series_set_elt_t l = f.series_of(*lhs_e);
67  for (delta_iterator rhs_e(g.value(), *s); ! rhs_e.done(); rhs_e.next())
68  {
69  series_set_elt_t l_ = g.series_of(*rhs_e);
70  series_set_elt_t l__(series);
71  typedef typename series_set_t::support_t support_t;
72  for_all_const_(support_t, supp, l.supp())
73  {
74  semiring_elt_t ol = l.get(*supp);
75  typedef typename semiring_elt_t::support_t wsupport_t;
76  wsupport_t wsupp = ol.supp();
77  series_set_t ts(series, monoid_elt_t(*supp));
78  for_all_const_(wsupport_t, ss, wsupp)
79  l__ += ts * l_.get(*ss);
80  }
81  if (l__ != zero)
82  {
83  output.add_series_transition(conv[std::make_pair(*s, *t)],
84  conv[std::make_pair(f.dst_of(*lhs_e),
85  g.dst_of(*rhs_e))
86  ],
87  l__);
88  }
89  }
90  }
91  }
92  return output;
93  }
94 
95  template <class S, class T>
96  Element<S, T>
98  const Element<S, T>& rhs)
99  {
100  BENCH_TASK_SCOPED("letter_to_letter_composition");
101  return do_letter_to_letter_composition(lhs.structure(), lhs, rhs);
102  }
103 
104 } // vcsn
105 
106 #endif // ! VCSN_ALGORITHMS_LETTER_TO_LETTER_COMPOSITION_HXX