Vaucanson  1.4.1
sub_automaton.hxx
1 // sub_automaton.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, 2007, 2008 The
6 // Vaucanson Group.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // The complete GNU General Public Licence Notice can be found as the
14 // `COPYING' file in the root directory.
15 //
16 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
17 //
18 #ifndef VCSN_ALGORITHMS_SUB_AUTOMATON_HXX
19 # define VCSN_ALGORITHMS_SUB_AUTOMATON_HXX
20 
22 
23 # include <vaucanson/automata/concept/automata_base.hh>
24 # include <vaucanson/misc/usual_macros.hh>
25 
26 # include <list>
27 
28 namespace vcsn {
29 
30  /*----------------------------------------.
31  | SubAutomaton defined by a set of states |
32  `----------------------------------------*/
33 
34  template<typename A, typename AI, typename HStatesSet>
35  void
36  do_sub_automaton_here(const AutomataBase<A>&,
37  Element<A, AI>& a,
38  const HStatesSet& selected,
39  bool check_states)
40  {
41  BENCH_TASK_SCOPED("sub_automaton");
42  typedef Element<A, AI> automaton_t;
43  std::list<typename automaton_t::hstate_t> to_be_removed;
44  for (typename automaton_t::state_iterator i = a.states().begin();
45  i != a.states().end(); ++i)
46  if (std::find(selected.begin(), selected.end(), *i) == selected.end())
47  to_be_removed.push_back(*i);
48 
49  for_all_const_(std::list<typename automaton_t::hstate_t>, i, to_be_removed)
50  if (!check_states
51  || a.has_state(*i))
52  a.del_state(*i);
53  }
54 
55 
56  // wrapper:
57  template<typename A, typename AI, typename HStatesSet>
58  Element<A, AI>
59  sub_automaton(const Element<A, AI>& a, const HStatesSet& s, bool check_states)
60  {
61  Element<A, AI> ret(a);
62  std::set<typename Element<A, AI>::hstate_t> ret_s;
63 
64  for_all_iterator(typename HStatesSet::const_iterator, i, s)
65  ret_s.insert(ret.get_state(size_t(*i)));
66  do_sub_automaton_here(ret.structure(), ret, ret_s, check_states);
67  return ret;
68  }
69 
70  template<typename A, typename AI, typename HStatesSet>
71  void
72  sub_automaton_here(Element<A, AI>& a, const HStatesSet& s, bool check_states)
73  {
74  do_sub_automaton_here(a.structure(), a, s, check_states);
75  }
76 
77 } // vcsn
78 
79 #endif // ! VCSN_ALGORITHMS_SUB_AUTOMATON_HXX