Vaucanson  1.4.1
domain.hxx
1 // domain.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2006, 2011 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_DOMAIN_HXX
18 # define VCSN_ALGORITHMS_DOMAIN_HXX
19 
22 
23 namespace vcsn
24 {
25 
26  /*-----------.
27  | FMP_Domain |
28  `-----------*/
29 
30  template <typename src_t, typename dst_t>
31  void
32  do_fmp_domain(const src_t& input, dst_t& dst, bool weighted)
33  {
34  BENCH_TASK_SCOPED("fmp_domain");
35  AUTOMATON_TYPES_(src_t, trans_);
36  AUTOMATON_TYPES(dst_t);
37 
38  src_t src = cut_up(input);
39 
40  typedef typename trans_series_set_elt_t::support_t trans_support_t;
41  std::map<trans_hstate_t, hstate_t> stmap;
42 
43  const series_set_t& series = dst.structure().series();
44  const monoid_t& monoid = dst.structure().series().monoid();
45  const trans_monoid_t& trans_monoid = src.structure().series().monoid();
46  const semiring_elt_t& unit = src.structure().series().semiring().wone_;
47 
48  for_all_const_states(fmp_s, src)
49  {
50  hstate_t s = dst.add_state();
51  stmap[*fmp_s] = s;
52 
53  if (src.is_initial(*fmp_s))
54  {
55  const trans_series_set_elt_t trans_series_elt =
56  src.get_initial(*fmp_s);
57  trans_support_t trans_supp = trans_series_elt.supp();
58  const trans_monoid_elt_t trans_monoid_elt(trans_monoid,
59  *(trans_supp.begin()));
60 
61  const monoid_elt_value_t word(trans_monoid_elt.value().first);
62 
63  series_set_elt_t series_elt(series);
64 
65  series_elt.assoc(monoid_elt_t(monoid, word),
66  weighted ?
67  trans_series_elt.get(trans_monoid_elt) : unit);
68  dst.set_initial(s, series_elt);
69  }
70  if (src.is_final(*fmp_s))
71  {
72  const trans_series_set_elt_t trans_series_elt =
73  src.get_final(*fmp_s);
74  trans_support_t trans_supp = trans_series_elt.supp();
75  const trans_monoid_elt_t trans_monoid_elt(trans_monoid,
76  *(trans_supp.begin()));
77 
78  const monoid_elt_value_t word(trans_monoid_elt.value().first);
79 
80  series_set_elt_t series_elt(series);
81 
82  series_elt.assoc(monoid_elt_t(monoid, word),
83  weighted ?
84  trans_series_elt.get(trans_monoid_elt) : unit);
85 
86  dst.set_final(s, series_elt);
87  }
88  }
89 
90  for_all_const_transitions_(trans_, fmp_e, src)
91  {
92  const trans_series_set_elt_t trans_series_elt = src.series_of(*fmp_e);
93  trans_support_t trans_supp = trans_series_elt.supp();
94  for_all_const_(trans_support_t, trans_value, trans_supp)
95  {
96  const trans_monoid_elt_t trans_monoid_elt(trans_monoid,
97  *trans_value);
98  const monoid_elt_value_t word(trans_monoid_elt.value().first);
99 
100  series_set_elt_t series_elt(series);
101 
102  series_elt.assoc(monoid_elt_t(monoid, word),
103  weighted ? trans_series_elt.get(trans_monoid_elt)
104  : unit);
105 
106  dst.add_series_transition(stmap[src.src_of(*fmp_e)],
107  stmap[src.dst_of(*fmp_e)], series_elt);
108  }
109  }
110  }
111 
112 
113  /*----------.
114  | RW_Domain |
115  `----------*/
116 
117  template <typename src_t, typename dst_t>
118  void
119  do_rw_domain(const src_t& src, dst_t& dst)
120  {
121  BENCH_TASK_SCOPED("rw_domain");
122  std::map<typename src_t::hstate_t, typename dst_t::hstate_t> m;
123  AUTOMATON_TYPES(src_t);
124 
125  for_all_const_states(p, src)
126  {
127  m[*p] = dst.add_state();
128  }
129 
130  for_all_const_initial_states(p, src)
131  dst.set_initial(m[*p]);
132 
133  for_all_const_final_states(p, src)
134  dst.set_final(m[*p]);
135 
136  for_all_const_transitions(e, src)
137  {
138  dst.add_series_transition(m[src.src_of(*e)],
139  m[src.dst_of(*e)],
140  src.input_of(*e));
141  }
142  }
143 
144 // FIXME: non void version
145 
146 
147  /*-----------------.
148  | Dispatch methods |
149  `-----------------*/
150 
151  template <typename S, typename S2, typename T, typename T2>
152  void
153  domain_dispatch(const AutomataBase<S>&, const Element<S,T>& src, Element<S2, T2>& dst, bool weighted)
154  {
155  do_fmp_domain(src, dst, weighted);
156  }
157 
158  template <typename S, typename S2, typename T, typename T2>
159  void
160  domain_dispatch(const TransducerBase<S>&, const Element<S,T>& src, Element<S2, T2>& dst, bool)
161  {
162  do_rw_domain(src, dst);
163  }
164 
165  template <typename S, typename S2, typename T, typename T2>
166  void
167  domain(const Element<S,T>& src, Element<S2, T2>& dst, bool weighted)
168  {
169  domain_dispatch(src.structure(), src, dst, weighted);
170  }
171 
172 } // End of namespace vcsn.
173 
174 #endif // ! VCSN_ALGORITHMS_DOMAIN_HXX