Vaucanson  1.4.1
cut_up.hxx
Go to the documentation of this file.
1 // cut_up.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 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_ALGORITHMS_CUT_UP_HXX
18 # define VCSN_ALGORITHMS_CUT_UP_HXX
19 
29 namespace vcsn {
30 
31 
32  /*----------------------------------.
33  | Check if an automaton is cut up. |
34  `----------------------------------*/
35 
36  template <typename A, typename AI>
37  bool is_cut_up(const Element<A, AI>& a)
38  {
39  BENCH_TASK_SCOPED("is_cut_up");
40  typedef Element<A, AI> automaton_t;
41  AUTOMATON_TYPES(automaton_t);
42 
43  for_all_const_transitions(e, a)
44  if (! a.series_of(*e).is_finite_app() ||
45  a.series_of(*e).supp().size() > 1)
46  return false;
47 
48  return true;
49  }
50 
51 
52  /*------------------------------------------------.
53  | Specialization for label with rational series. |
54  `------------------------------------------------*/
55 
56  template <typename S, typename T, typename TT, typename Auto, typename Ret>
57  void do_cut_up(const AutomataBase<S>&,
58  const rat::exp<T, TT>&,
59  const Auto& a,
60  Ret& res)
61  {
62  AUTOMATON_TYPES_(Ret, ret_);
63  typedef typename generalized_traits<Ret>::automaton_t gen_automaton_t;
64  AUTOMATON_TYPES(gen_automaton_t);
65 
66  auto_copy(res, a);
67 
68  std::map<hstate_t, ret_hstate_t> statemap;
69 
70  ret_transitions_t transitions = res.transitions();
71 
72  for_all_(ret_transitions_t, e, transitions)
73  {
74  if (! res.series_of(*e).is_finite_app() ||
75  res.series_of(*e).supp().size() > 1)
76  {
77  gen_automaton_t tmp(res.structure());
78  standard_of(tmp, res.series_of(*e).value());
79 
80  for_all_states(s, tmp)
81  statemap[*s] = res.add_state();
82 
83  for_all_initial_states(i, tmp)
84  res.add_series_transition(res.src_of(*e),
85  statemap[*i],
86  tmp.get_initial(*i));
87 
88  for_all_transitions(ed, tmp)
89  res.add_transition(statemap[tmp.src_of(*ed)],
90  statemap[tmp.dst_of(*ed)],
91  tmp.label_of(*ed));
92 
93  for_all_final_states(f, tmp)
94  res.add_series_transition(statemap[*f], res.dst_of(*e),
95  tmp.get_final(*f));
96 
97  res.del_transition(*e);
98  }
99  }
100  }
101 
102 
103  /*--------------------------------------------------.
104  | Specialization for label with polynomial series. |
105  `--------------------------------------------------*/
106 
107  template <typename S, typename T, typename TT, typename Auto, typename Ret>
108  void do_cut_up(const S&,
109  const algebra::polynom<T, TT>&,
110  const Auto& a,
111  Ret& res)
112  {
113  AUTOMATON_TYPES(Ret);
114  typedef typename Ret::series_set_elt_t::support_t support_t;
115  int size;
116 
117  auto_copy(res, a);
118 
119  transitions_t transitions = res.transitions();
120 
121  for_all_(transitions_t, e, transitions)
122  {
123  series_set_elt_t label(res.structure().series());
124  label = res.series_of(*e);
125 
126  if ((size = label.supp().size()) > 1)
127  {
128  typename support_t::const_iterator m = label.supp().begin();
129  for (int i = 0; i < size; ++i, ++m)
130  {
131  series_set_elt_t series(res.structure().series());
132  series.assoc(*m, label.get(*m));
133  res.add_series_transition(res.src_of(*e),
134  res.dst_of(*e),
135  series);
136  }
137 
138  res.del_transition(*e);
139  }
140  }
141  }
142 
143 
144  /*---------.
145  | Wrappers |
146  `---------*/
147 
148  template <typename A, typename AI>
149  void
151  {
152  BENCH_TASK_SCOPED("cut_up");
153  typedef typename Element<A, AI>::series_set_elt_t::value_t series_impl_t;
154  do_cut_up(a.structure(), SELECT(series_impl_t), a, res);
155  }
156 
157 
158  template <typename A, typename AI>
159  Element<A, AI>
161  {
162  Element<A, AI> res(a.structure());
163  cut_up(a, res);
164  return res;
165  }
166 
167 
168  template <typename A, typename AI>
169  void
171  {
172  a = cut_up(a);
173  }
174 
175 
176 } // ! vcsn
177 
178 
179 #endif // ! VCSN_ALGORITHMS_CUT_UP_HXX