Vaucanson 1.4
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       for (delta_iterator j(a.value(), state); ! j.done(); j.next())
00067         delta_ret.insert(a.dst_of(*j));
00068       for_all_const_(state_set_t, j, delta_ret)
00069       {
00070         state = *j;
00071         if (reachable_states.find(state) == reachable_states.end())
00072         {
00073           reachable_states.insert(state);
00074           queue.push(state);
00075         }
00076       }
00077     }
00078     return reachable_states;
00079   }
00080 
00081   template<typename A, typename AI>
00082   std::set<typename Element<A, AI>::hstate_t>
00083   accessible_states(const Element<A, AI>& a)
00084   {
00085     BENCH_TASK_SCOPED("accessible_states");
00086     return do_accessible_states(a.structure(), a);
00087   }
00088 
00089   template<typename A, typename AI>
00090   void
00091   accessible_here(Element<A, AI>& a)
00092   {
00093     sub_automaton_here(a, accessible_states(a));
00094   }
00095 
00096   template<typename A, typename AI>
00097   Element<A, AI>
00098   accessible(const Element<A, AI>& a)
00099   {
00100     return sub_automaton(a, accessible_states(a));
00101   }
00102 
00103   /*-----------------------.
00104   | do_coaccessible_states |
00105   `-----------------------*/
00106 
00107   template <class A, typename AI>
00108   std::set<typename Element<A, AI>::hstate_t>
00109   do_coaccessible_states(const AutomataBase<A>&,
00110                          const Element<A, AI>&  a)
00111   {
00112     return accessible_states(transpose_view(a));
00113   }
00114 
00115   template<typename A, typename AI>
00116   std::set<typename Element<A, AI>::hstate_t>
00117   coaccessible_states(const Element<A, AI>& a)
00118   {
00119     return do_coaccessible_states(a.structure(), a);
00120   }
00121 
00122   template<typename A, typename AI>
00123   Element<A, AI>
00124   coaccessible(const Element<A, AI>& a)
00125   {
00126     return sub_automaton(a, coaccessible_states(a));
00127   }
00128 
00129   template<typename A, typename AI>
00130   void
00131   coaccessible_here(Element<A, AI>& a)
00132   {
00133     sub_automaton_here(a, coaccessible_states(a));
00134   }
00135 
00136 } // vcsn
00137 
00138 #endif // ! VCSN_ALGORITHMS_ACCESSIBLE_HXX