Vaucanson  1.4.1
pair_to_fmp.hxx
1 // pair_to_fmp.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2008 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_PAIR_TO_FMP_HXX
18 # define VCSN_ALGORITHMS_PAIR_TO_FMP_HXX
19 
21 
22 namespace vcsn
23 {
24 // Helper to write lighter code.
25 # define MUTE_TRAITS mute_pair_to_fmp<S, T>
26 
27  template <typename S, typename T>
28  inline typename MUTE_TRAITS::ret
29  MUTE_TRAITS::
30  make_automaton(const typename MUTE_TRAITS::ret_first_alphabet_t& A,
31  const typename MUTE_TRAITS::ret_second_alphabet_t& B)
32  {
33  semiring_t semiring;
34  first_monoid_t fM(A);
35  second_monoid_t sM(B);
36  ret_monoid_t freemonoidproduct(fM, sM);
37  typename ret::series_set_t series(semiring, freemonoidproduct);
38 
39  return ret(Automata<typename ret::series_set_t, typename ret::kind_t>(series));
40  }
41 
42  template <typename S, typename T>
43  inline typename MUTE_TRAITS::ret
44  MUTE_TRAITS::
45  make_automaton(const Element<S, T>& aut)
46  {
47  ret_first_alphabet_t fA = alphabet_traits_t::first_projection(aut.
48  series().monoid().alphabet());
49  ret_second_alphabet_t sA = alphabet_traits_t::second_projection(aut.
50  series().monoid().alphabet());
51 
52  return make_automaton(fA, sA);
53  }
54 
55  template <typename S, typename T>
56  inline typename MUTE_TRAITS::ret::series_set_elt_t
57  MUTE_TRAITS::
58  series_convert(const typename MUTE_TRAITS::ret_series_set_t& series,
59  const typename MUTE_TRAITS::automaton_t::
60  series_set_elt_t& ss)
61  {
62  typedef typename MUTE_TRAITS::automaton_t::series_set_elt_t
63  series_set_elt_t;
64 
65  typename ret::series_set_elt_t R(series);
66 
67  for_all_const_(series_set_elt_t::support_t, it, ss.supp())
68  {
69  R.assoc(std::make_pair(word_traits_t::first_projection(*it),
70  word_traits_t::second_projection(*it)),
71  ss.get(*it));
72  }
73 
74  return R;
75  }
76 
77  template <typename S, typename T>
78  void
79  do_pair_to_fmp(const Element<S, T>& src,
80  typename MUTE_TRAITS::ret& res)
81  {
82  BENCH_TASK_SCOPED("pair_to_fmp");
83 
84  // Type helpers.
85  typedef typename MUTE_TRAITS::automaton_t automaton_t;
86  typedef MUTE_TRAITS fmp_traits_t;
87  typedef typename fmp_traits_t::ret res_t;
88  AUTOMATON_TYPES(automaton_t);
89  AUTOMATON_TYPES_(res_t, res_);
90 
91  // Helper map.
92  std::map<hstate_t, res_hstate_t> m;
93 
94  for_all_const_states(p, src)
95  m[*p] = res.add_state();
96 
97  // Setup initial transitions.
98  for_all_const_initial_states(i, src)
99  {
100  res.set_initial(m[*i],
101  fmp_traits_t::
102  series_convert(res.structure().series(),
103  src.get_initial(*i)));
104  }
105 
106  // Setup normal transitions.
107  for_all_const_transitions(e, src)
108  {
109  res.add_series_transition(m[src.src_of(*e)],
110  m[src.dst_of(*e)],
111  fmp_traits_t::
112  series_convert(res.structure().series(),
113  src.series_of(*e)));
114  }
115 
116  // Setup final transitions.
117  for_all_const_final_states(f, src)
118  {
119  res.set_final(m[*f],
120  fmp_traits_t::
121  series_convert(res.structure().series(),
122  src.get_final(*f)));
123  }
124  }
125 
126  //
127  // Facade Functions
128  //
129 
130  template <typename S, typename T>
131  void
132  pair_to_fmp(const Element<S, T>& aut,
133  typename MUTE_TRAITS::ret& res)
134  {
135  do_pair_to_fmp(aut, res);
136  }
137 
138  template <typename S, typename T>
139  typename MUTE_TRAITS::ret
140  pair_to_fmp(const Element<S, T>& aut)
141  {
142  typename MUTE_TRAITS::ret res = MUTE_TRAITS::make_automaton(aut);
143 
144  do_pair_to_fmp(aut, res);
145 
146  return res;
147  }
148 
149 # undef MUTE_TRAITS
150 
151 } // ! vcsn
152 
153 #endif // ! VCSN_ALGORITHMS_PAIR_TO_FMP_HXX