Vaucanson 1.4
sub_automaton.hxx
00001 // sub_automaton.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The
00006 // Vaucanson Group.
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // The complete GNU General Public Licence Notice can be found as the
00014 // `COPYING' file in the root directory.
00015 //
00016 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00017 //
00018 #ifndef VCSN_ALGORITHMS_SUB_AUTOMATON_HXX
00019 # define VCSN_ALGORITHMS_SUB_AUTOMATON_HXX
00020 
00021 # include <vaucanson/algorithms/sub_automaton.hh>
00022 
00023 # include <vaucanson/automata/concept/automata_base.hh>
00024 # include <vaucanson/misc/usual_macros.hh>
00025 
00026 # include <list>
00027 
00028 namespace vcsn {
00029 
00030   /*----------------------------------------.
00031   | SubAutomaton defined by a set of states |
00032   `----------------------------------------*/
00033 
00034   template<typename A, typename AI, typename HStatesSet>
00035   void
00036   do_sub_automaton_here(const AutomataBase<A>&,
00037                         Element<A, AI>& a,
00038                         const HStatesSet& selected,
00039                         bool check_states)
00040   {
00041     BENCH_TASK_SCOPED("sub_automaton");
00042     typedef Element<A, AI> automaton_t;
00043     std::list<typename automaton_t::hstate_t> to_be_removed;
00044     for (typename automaton_t::state_iterator i = a.states().begin();
00045          i != a.states().end(); ++i)
00046       if (std::find(selected.begin(), selected.end(), *i) == selected.end())
00047         to_be_removed.push_back(*i);
00048 
00049     for_all_const_(std::list<typename automaton_t::hstate_t>, i, to_be_removed)
00050       if (!check_states
00051           || a.has_state(*i))
00052         a.del_state(*i);
00053   }
00054 
00055 
00056   // wrapper:
00057   template<typename A, typename AI, typename HStatesSet>
00058   Element<A, AI>
00059   sub_automaton(const Element<A, AI>& a, const HStatesSet& s, bool check_states)
00060   {
00061     Element<A, AI> ret(a);
00062     std::set<typename Element<A, AI>::hstate_t> ret_s;
00063 
00064     for_all_iterator(typename HStatesSet::const_iterator, i, s)
00065       ret_s.insert(ret.get_state(size_t(*i)));
00066     do_sub_automaton_here(ret.structure(), ret, ret_s, check_states);
00067     return ret;
00068   }
00069 
00070   template<typename A, typename AI, typename HStatesSet>
00071   void
00072   sub_automaton_here(Element<A, AI>& a, const HStatesSet& s, bool check_states)
00073   {
00074     do_sub_automaton_here(a.structure(), a, s, check_states);
00075   }
00076 
00077 } // vcsn
00078 
00079 #endif // ! VCSN_ALGORITHMS_SUB_AUTOMATON_HXX