Vaucanson 1.4
hash.hxx
00001 // hash_label.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2007, 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_MISC_HASH_HXX_
00019 # define VCSN_MISC_HASH_HXX_
00020 
00021 # include <utility>
00022 # include <boost/functional/hash/hash.hpp>
00023 # include <vaucanson/algebra/implementation/series/rat/hash_visitor.hh>
00024 
00025 namespace vcsn
00026 {
00027   namespace misc
00028   {
00029 
00030     inline
00031     std::size_t hash_label<char>::operator()(const char c) const
00032     {
00033       return c;
00034     }
00035 
00036     inline
00037     std::size_t hash_label<int>::operator()(const int c) const
00038     {
00039       return c;
00040     }
00041 
00042     template <typename U, typename V>
00043     inline
00044     std::size_t hash_label<std::pair<U, V> >::operator()(const std::pair<U, V>& p) const
00045     {
00046       std::size_t seed (0);
00047 
00048       // FIXME: boost should have hash for pairs OOB
00049       ::boost::hash_combine(seed, p.first);
00050       ::boost::hash_combine(seed, p.second);
00051 
00052       return seed;
00053     }
00054 
00055     template <typename Word, typename Weight>
00056     std::size_t
00057     hash_label<algebra::polynom<Word, Weight> >::operator() (const algebra::polynom<Word, Weight>& l) const
00058     {
00059       std::size_t seed (0);
00060 
00061       for (typename algebra::polynom<Word, Weight>::const_iterator i = l.begin ();
00062            i != l.end ();
00063            ++i)
00064       {
00065         // FIXME: it might not be correct yet
00066         ::boost::hash_combine (seed, i->first); // std::string
00067 //      ::boost::hash_combine (seed, i->second); It does not make sense to hash the weight
00068       }
00069       return seed;
00070     }
00071 
00072 # if 0
00073     template <typename Word, typename LetterT, typename WeightT>
00074     std::size_t
00075     hash_label<algebra::polynom<Word, rat::exp<LetterT, WeightT> > >::operator() (
00076           const algebra::polynom<Word, rat::exp<LetterT, WeightT> >& l) const
00077     {
00078       std::size_t seed (0);
00079       hash_label hash;
00080 
00081       for (typename algebra::polynom<Word, rat::exp<LetterT,
00082             WeightT> >::const_iterator i = l.begin ();
00083            i != l.end ();
00084            ++i)
00085       {
00086         ::boost::hash_combine (seed, hash(i->first));
00087         ::boost::hash_combine (seed, i->second);
00088       }
00089       return seed;
00090     }
00091 #endif
00092     template <typename Word, typename Word2, typename WeightT>
00093     std::size_t
00094     hash_label<algebra::polynom<Word, rat::exp<Word2, WeightT> > >::operator() (
00095         const rat::exp<Word2, WeightT>& l) const
00096     {
00097       rat::HashVisitor<Word2, WeightT> visitor;
00098       l.accept(visitor);
00099       return visitor.hash_value();
00100     }
00101 
00102 
00103     template <typename Word, typename Word2, typename WeightT>
00104     std::size_t
00105     hash_label<algebra::polynom<Word, rat::exp<Word2, WeightT> > >::operator() (
00106           const algebra::polynom<Word, rat::exp<Word2, WeightT> >& l) const
00107     {
00108       std::size_t seed (0);
00109       hash_label hash;
00110 
00111       for (typename algebra::polynom<Word, rat::exp<Word2,
00112             WeightT> >::const_iterator i = l.begin ();
00113            i != l.end ();
00114            ++i)
00115       {
00116         ::boost::hash_combine (seed, i->first);
00117         ::boost::hash_combine (seed, hash(i->second));
00118       }
00119       return seed;
00120     }
00121 
00122     template <typename Weight, typename T, typename U>
00123     std::size_t
00124     hash_label<algebra::polynom<std::pair<T, U>, Weight> >::operator() (
00125         const algebra::polynom<std::pair<T, U>, Weight >& l) const
00126     {
00127       std::size_t seed (0);
00128 
00129       for (typename algebra::polynom<std::pair<T, U>, Weight>::const_iterator i = l.begin ();
00130            i != l.end ();
00131            ++i)
00132       {
00133         ::boost::hash_combine (seed, i->first.first);
00134         ::boost::hash_combine (seed, i->first.second);
00135         ::boost::hash_combine (seed, i->second);
00136       }
00137       return seed;
00138     }
00139 
00140     template <typename Word, typename Weight>
00141     std::size_t
00142     hash_label<rat::exp<Word, Weight> >::operator() (const rat::exp<Word, Weight>& l) const
00143     {
00144       rat::HashVisitor<Word, Weight> visitor;
00145       l.accept(visitor);
00146       return visitor.hash_value();
00147     }
00148 
00149     template <typename Kind, typename Type>
00150     std::size_t
00151     hash_handler<handler<Kind, Type> >::operator() (const handler<Kind, Type>& h) const
00152     {
00153       return ::boost::hash_value (h.value());
00154     }
00155 
00156     inline
00157     std::size_t
00158     hash_handler<char>::operator() (const char c) const
00159     {
00160       return ::boost::hash_value (c);
00161     }
00162 
00163   }
00164 }
00165 
00166 #endif // ! VCSN_MISC_HASH_HXX_ //