Vaucanson 1.4
is_ambiguous.hxx
00001 // is_ambiguous.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 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 
00018 #ifndef VCSN_ALGORITHMS_IS_AMBIGUOUS_HXX
00019 # define VCSN_ALGORITHMS_IS_AMBIGUOUS_HXX
00020 
00021 # include <vaucanson/algorithms/is_ambiguous.hh>
00022 # include <vaucanson/algorithms/trim.hh>
00023 # include <vaucanson/algorithms/product.hh>
00024 # include <vaucanson/misc/usual_macros.hh>
00025 
00026 # include <map>
00027 # include <set>
00028 
00029 namespace vcsn {
00030 
00031   /*-------------.
00032   | is_ambiguous |
00033   `-------------*/
00034   template <typename A, typename AI>
00035   bool
00036   do_is_ambiguous(const AutomataBase<A>&,
00037                   const Element<A, AI>& aut)
00038   {
00039     typedef typename Element<A, AI>::hstate_t hstate_t;
00040 
00041     // This map is used to know which state is created by which two ones.
00042     std::map<hstate_t, std::pair<hstate_t, hstate_t> >  m;
00043     std::map<hstate_t, std::pair<hstate_t, hstate_t> >  aut_p_m;
00044     Element<A, AI> aut_p = product (aut, aut, m);
00045     for (typename std::map<hstate_t, std::pair<hstate_t, hstate_t> >::iterator i = m.begin();
00046          i != m.end(); ++i)
00047       aut_p_m.insert(std::make_pair(aut_p.get_state(size_t(i->first)), i->second));
00048 
00049     const std::set<hstate_t>& s = useful_states (aut_p);
00050     for_all_const_ (std::set<hstate_t>, i, s)
00051       if (aut_p_m[*i].first != aut_p_m[*i].second)
00052         return true;
00053     return false;
00054   }
00055 
00056   template<typename A, typename AI>
00057   bool
00058   is_ambiguous(const Element<A, AI>& aut)
00059   {
00060     BENCH_TASK_SCOPED("is_ambiguous");
00061     return do_is_ambiguous(aut.structure(), aut);
00062   }
00063 
00064 } // End of namespace vcsn.
00065 
00066 #endif // ! VCSN_ALGORITHMS_IS_AMBIGUOUS_HXX