accessible.hxx

00001 // accessible.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, 2008 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_ALGORITHMS_ACCESSIBLE_HXX
00018 # define VCSN_ALGORITHMS_ACCESSIBLE_HXX
00019 
00020 # include <vaucanson/algorithms/accessible.hh>
00021 
00022 # include <vaucanson/automata/concept/automata_base.hh>
00023 # include <vaucanson/automata/implementation/transpose_view.hh>
00024 # include <vaucanson/algorithms/sub_automaton.hh>
00025 # include <vaucanson/misc/usual_macros.hh>
00026 
00027 # include <queue>
00028 # include <set>
00029 
00030 
00031 namespace vcsn {
00032 
00033   /*-----------------------.
00034   | do_accessible_states.  |
00035   `-----------------------*/
00036 
00037   template <class A, typename AI>
00038   std::set<typename Element<A, AI>::hstate_t>
00039   do_accessible_states(const AutomataBase<A>&,
00040                        const Element<A, AI>&   a)
00041   {
00042     typedef Element<A, AI> auto_t;
00043     AUTOMATON_TYPES(auto_t);
00044     typedef std::set<hstate_t> state_set_t;
00045 
00046     std::queue<hstate_t>      queue;
00047     state_set_t               reachable_states;
00048 
00049     /*---------------.
00050     | Initialization |
00051     `---------------*/
00052     for_all_const_initial_states(i, a)
00053     {
00054       queue.push(*i);
00055       reachable_states.insert(*i);
00056     }
00057 
00058     /*----------.
00059     | Main loop |
00060     `----------*/
00061     while (!queue.empty())
00062     {
00063       hstate_t state = queue.front();
00064       queue.pop();
00065       state_set_t delta_ret;
00066       a.deltac(delta_ret, state, delta_kind::states());
00067       for_all_const_(state_set_t, j, delta_ret)
00068       {
00069         state = *j;
00070         if (reachable_states.find(state) == reachable_states.end())
00071         {
00072           reachable_states.insert(state);
00073           queue.push(state);
00074         }
00075       }
00076     }
00077     return reachable_states;
00078   }
00079 
00080   template<typename A, typename AI>
00081   std::set<typename Element<A, AI>::hstate_t>
00082   accessible_states(const Element<A, AI>& a)
00083   {
00084     TIMER_SCOPED ("accessible_states");
00085     return do_accessible_states(a.structure(), a);
00086   }
00087 
00088   template<typename A, typename AI>
00089   void
00090   accessible_here(Element<A, AI>& a)
00091   {
00092     sub_automaton_here(a, accessible_states(a));
00093   }
00094 
00095   template<typename A, typename AI>
00096   Element<A, AI>
00097   accessible(const Element<A, AI>& a)
00098   {
00099     return sub_automaton(a, accessible_states(a));
00100   }
00101 
00102   /*-----------------------.
00103   | do_coaccessible_states |
00104   `-----------------------*/
00105 
00106   template <class A, typename AI>
00107   std::set<typename Element<A, AI>::hstate_t>
00108   do_coaccessible_states(const AutomataBase<A>&,
00109                          const Element<A, AI>&  a)
00110   {
00111     return accessible_states(transpose_view(a));
00112   }
00113 
00114   template<typename A, typename AI>
00115   std::set<typename Element<A, AI>::hstate_t>
00116   coaccessible_states(const Element<A, AI>& a)
00117   {
00118     return do_coaccessible_states(a.structure(), a);
00119   }
00120 
00121   template<typename A, typename AI>
00122   Element<A, AI>
00123   coaccessible(const Element<A, AI>& a)
00124   {
00125     return sub_automaton(a, coaccessible_states(a));
00126   }
00127 
00128   template<typename A, typename AI>
00129   void
00130   coaccessible_here(Element<A, AI>& a)
00131   {
00132     sub_automaton_here(a, coaccessible_states(a));
00133   }
00134 
00135 } // vcsn
00136 
00137 #endif // ! VCSN_ALGORITHMS_ACCESSIBLE_HXX

Generated on Thu Oct 9 20:22:33 2008 for Vaucanson by  doxygen 1.5.1