Vaucanson  1.4.1
aut_projection.hxx
1 // aut_projection.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 
18 #ifndef VCSN_ALGORITHMS_AUT_PROJECTION_HXX
19 # define VCSN_ALGORITHMS_AUT_PROJECTION_HXX
20 
22 
23 namespace vcsn
24 {
25  template <typename S, typename T>
26  void
27  do_first_projection(const Element<S, T>& src,
28  typename projection_traits<S, T>::
29  first_projection_t& dst)
30  {
31  BENCH_TASK_SCOPED("first_projection");
32 
33  // Type helpers.
34  typedef Element<S, T> automaton_t;
35  typedef projection_traits<S, T> projection_traits_t;
36  typedef typename projection_traits_t::first_projection_t res_t;
37  AUTOMATON_TYPES(automaton_t);
38  AUTOMATON_TYPES_(res_t, res_);
39 
40  // Helper map.
41  std::map<hstate_t, res_hstate_t> m;
42 
43  // Create destination states.
44  for_all_const_states(p, src)
45  m[*p] = dst.add_state();
46 
47  // Setup initial weights.
48  for_all_const_initial_states(i, src)
49  {
50  dst.set_initial(m[*i],
51  projection_traits_t::
52  series_first_projection(dst.series(),
53  src.get_initial(*i)));
54  }
55 
56  // Setup normal transitions.
57  for_all_const_transitions(e, src)
58  {
59  dst.add_series_transition(m[src.src_of(*e)],
60  m[src.dst_of(*e)],
61  projection_traits_t::
62  series_first_projection(dst.series(),
63  src.series_of(*e)));
64  }
65 
66  // Setup final weights.
67  for_all_const_final_states(f, src)
68  {
69  dst.set_final(m[*f],
70  projection_traits_t::
71  series_first_projection(dst.series(),
72  src.get_final(*f)));
73  }
74  }
75 
76  template <typename S, typename T>
77  void
78  do_second_projection(const Element<S, T>& src,
79  typename projection_traits<S, T>::
80  second_projection_t& dst)
81  {
82  BENCH_TASK_SCOPED("second_projection");
83 
84  // Type helpers.
85  typedef Element<S, T> automaton_t;
86  typedef projection_traits<S, T> projection_traits_t;
87  typedef typename projection_traits_t::second_projection_t 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  // Create destination states.
95  for_all_const_states(p, src)
96  m[*p] = dst.add_state();
97 
98  // Setup initial weights.
99  for_all_const_initial_states(i, src)
100  {
101  dst.set_initial(m[*i],
102  projection_traits_t::
103  series_second_projection(dst.series(),
104  src.get_initial(*i)));
105  }
106 
107  // Setup normal transitions.
108  for_all_const_transitions(e, src)
109  {
110  dst.add_series_transition(m[src.src_of(*e)],
111  m[src.dst_of(*e)],
112  projection_traits_t::
113  series_second_projection(dst.series(),
114  src.series_of(*e)));
115  }
116 
117  // Setup final weights.
118  for_all_const_final_states(f, src)
119  {
120  dst.set_final(m[*f],
121  projection_traits_t::
122  series_second_projection(dst.series(),
123  src.get_final(*f)));
124  }
125  }
126 
127  /*------------------.
128  | Function Facades. |
129  `------------------*/
130 
131  //
132  // First projection
133  //
134 
135  template <typename S, typename T>
136  void
137  first_projection(const Element<S, T>& src,
138  typename projection_traits<S, T>::first_projection_t& dst)
139  {
140  do_first_projection(src, dst);
141  }
142 
143  template <typename S, typename T>
144  typename projection_traits<S, T>::first_projection_t
145  first_projection(const Element<S, T>& src)
146  {
147  typename projection_traits<S, T>::first_projection_t
148  dst = projection_traits<S, T>::first_projection(src);
149 
150  do_first_projection(src, dst);
151 
152  return dst;
153  }
154 
155  //
156  // Second projection
157  //
158 
159  template <typename S, typename T>
160  void
161  second_projection(const Element<S, T>& src,
162  typename projection_traits<S, T>::
163  second_projection_t& dst)
164  {
165  do_second_projection(src, dst);
166  }
167 
168  template <typename S, typename T>
169  typename projection_traits<S, T>::second_projection_t
170  second_projection(const Element<S, T>& src)
171  {
172  typename projection_traits<S, T>::second_projection_t
173  dst = projection_traits<S, T>::second_projection(src);
174 
175  do_second_projection(src, dst);
176 
177  return dst;
178  }
179 
180 } // ! vcsn
181 
182 #endif // ! VCSN_ALGORITHMS_AUT_PROJECTION_HXX