Vaucanson  1.4.1
transducer.hxx
1 // transducer.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, 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_AUTOMATA_CONCEPT_TRANSDUCER_HXX
18 # define VCSN_AUTOMATA_CONCEPT_TRANSDUCER_HXX
19 
20 # include <vaucanson/automata/concept/transducer_base.hh>
21 
22 namespace vcsn {
23 
24  template <typename Series, typename Kind>
25  Transducer<Series, Kind>::Transducer(const series_set_t& s):
26  SetSlot<Series>(s)
27  {}
28 
29  template <typename Series, typename Kind>
30  const typename Transducer<Series, Kind>::series_set_t&
32  {
33  return this->_structure_get();
34  }
35 
36  template <typename Series, typename Kind>
37  bool
38  operator==(const Transducer<Series, Kind>& lhs,
39  const Transducer<Series, Kind>& rhs)
40  {
41  return & lhs.series() == & rhs.series();
42  }
43 
44  //
45  // Projections for RW transducers
46  //
47 
48  template <typename S, typename K, typename T>
49  inline typename input_projection_helper<Transducer<S, K>, T>::ret
50  input_projection_helper<Transducer<S, K>, T>::
51  make_input_projection_automaton(const Element<Transducer<S, K>, T>& t)
52  {
53  // Type helpers.
54  typedef typename ret::set_t set_t;
55  typedef typename set_t::series_set_t series_set_t;
56 
57  set_t
58  auto_set(series_set_t(t.structure().series().
59  semiring().semiring(), t.structure().
60  series().monoid()));
61 
62  return ret(auto_set);
63  }
64 
65  template <typename S, typename K, typename T>
66  inline typename output_projection_helper<Transducer<S, K>, T>::ret
67  output_projection_helper<Transducer<S, K>, T>::
68  make_output_projection_automaton(const Element<Transducer<S, K>, T>& t)
69  {
70  // Type helpers.
71  typedef typename ret::set_t set_t;
72  typedef typename set_t::series_set_t series_set_t;
73 
74  set_t
75  auto_set(series_set_t(t.structure().series().semiring()));
76 
77  return ret(auto_set);
78  }
79 
80  //
81  // Projections for FMP transducers
82  //
83 
84  template <typename S, typename K, typename T>
85  inline typename input_projection_helper<Automata<S, K>, T>::ret
86  input_projection_helper<Automata<S, K>, T>::
87  make_input_projection_automaton(const Element<Automata<S, K>, T>& t)
88  {
89  // Type helpers.
90  typedef typename ret::set_t set_t;
91  typedef typename set_t::series_set_t series_set_t;
92 
93  set_t
94  auto_set(series_set_t(t.structure().series().
95  semiring(), t.structure().series().
96  monoid().first_monoid()));
97 
98  return ret(auto_set);
99  }
100 
101  template <typename S, typename K, typename T>
102  inline typename output_projection_helper<Automata<S, K>, T>::ret
103  output_projection_helper<Automata<S, K>, T>::
104  make_output_projection_automaton(const Element<Automata<S, K>, T>& t)
105  {
106  // Type helpers.
107  typedef typename ret::set_t set_t;
108  typedef typename set_t::series_set_t series_set_t;
109 
110  set_t
111  auto_set(series_set_t(t.structure().series().
112  semiring(), t.structure().series().
113  monoid().second_monoid()));
114 
115  return ret(auto_set);
116  }
117 
118  template <typename S, typename K, typename T>
119  typename identity_transducer_helper<S, K, T>::ret
120  partial_identity(const Element<S, T>& a)
121  {
122  typedef Element<S, T> automaton_t;
123  AUTOMATON_TYPES(automaton_t);
124  typedef typename identity_transducer_helper<S, K, T>::ret ret_t;
125  typedef typename ret_t::series_set_elt_t output_series_set_elt_t;
126  typedef typename series_set_elt_t::support_t support_t;
127  typedef typename ret_t::set_t set_t;
128  typedef typename set_t::series_set_t o_series_set_t;
129  set_t s (o_series_set_t (a.structure().series(),
130  a.structure().series().monoid()));
131  ret_t ret(s);
132  std::vector<hstate_t> conv(a.states().size());
133 
134  for_all_states(s, a)
135  conv[ret.add_state()] = *s;
136  for_all_transitions(e, a)
137  {
138  series_set_elt_t t = a.series_of(*e);
139  series_set_elt_t s(t);
140  output_series_set_elt_t os(ret.structure().series());
141  support_t supp = s.supp();
142  for_all_const_(support_t, m, supp)
143  {
144  series_set_elt_t tmp(a.structure().series());
145  tmp.assoc(*m, s.get(*m));
146  os.assoc(*m, tmp);
147  }
148  htransition_t f = ret.add_series_transition(conv[a.src_of(*e)],
149  conv[a.dst_of(*e)],
150  os);
151  }
152  // FIXME: set initial/final weights.
153  for_all_initial_states(i, a)
154  ret.set_initial(conv[*i], a.get_initial(*i));
155  for_all_final_states(f, a)
156  ret.set_final(conv[*f], a.get_final(*f));
157  return ret;
158  }
159 
160 } // vcsn
161 
162 #endif // ! VCSN_AUTOMATA_CONCEPT_TRANSDUCER_HXX