Vaucanson  1.4.1
hash.hxx
1 // hash_label.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2007, 2008 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 
18 #ifndef VCSN_MISC_HASH_HXX_
19 # define VCSN_MISC_HASH_HXX_
20 
21 # include <utility>
22 # include <boost/functional/hash/hash.hpp>
23 # include <vaucanson/algebra/implementation/series/rat/hash_visitor.hh>
24 
25 namespace vcsn
26 {
27  namespace misc
28  {
29 
30  inline
31  std::size_t hash_label<char>::operator()(const char c) const
32  {
33  return c;
34  }
35 
36  inline
37  std::size_t hash_label<int>::operator()(const int c) const
38  {
39  return c;
40  }
41 
42  template <typename U, typename V>
43  inline
44  std::size_t hash_label<std::pair<U, V> >::operator()(const std::pair<U, V>& p) const
45  {
46  std::size_t seed (0);
47 
48  // FIXME: boost should have hash for pairs OOB
49  ::boost::hash_combine(seed, p.first);
50  ::boost::hash_combine(seed, p.second);
51 
52  return seed;
53  }
54 
55  template <typename Word, typename Weight>
56  std::size_t
57  hash_label<algebra::polynom<Word, Weight> >::operator() (const algebra::polynom<Word, Weight>& l) const
58  {
59  std::size_t seed (0);
60 
61  for (typename algebra::polynom<Word, Weight>::const_iterator i = l.begin ();
62  i != l.end ();
63  ++i)
64  {
65  // FIXME: it might not be correct yet
66  ::boost::hash_combine (seed, i->first); // std::string
67 // ::boost::hash_combine (seed, i->second); It does not make sense to hash the weight
68  }
69  return seed;
70  }
71 
72 # if 0
73  template <typename Word, typename LetterT, typename WeightT>
74  std::size_t
75  hash_label<algebra::polynom<Word, rat::exp<LetterT, WeightT> > >::operator() (
76  const algebra::polynom<Word, rat::exp<LetterT, WeightT> >& l) const
77  {
78  std::size_t seed (0);
79  hash_label hash;
80 
81  for (typename algebra::polynom<Word, rat::exp<LetterT,
82  WeightT> >::const_iterator i = l.begin ();
83  i != l.end ();
84  ++i)
85  {
86  ::boost::hash_combine (seed, hash(i->first));
87  ::boost::hash_combine (seed, i->second);
88  }
89  return seed;
90  }
91 #endif
92  template <typename Word, typename Word2, typename WeightT>
93  std::size_t
94  hash_label<algebra::polynom<Word, rat::exp<Word2, WeightT> > >::operator() (
95  const rat::exp<Word2, WeightT>& l) const
96  {
97  rat::HashVisitor<Word2, WeightT> visitor;
98  l.accept(visitor);
99  return visitor.hash_value();
100  }
101 
102 
103  template <typename Word, typename Word2, typename WeightT>
104  std::size_t
105  hash_label<algebra::polynom<Word, rat::exp<Word2, WeightT> > >::operator() (
106  const algebra::polynom<Word, rat::exp<Word2, WeightT> >& l) const
107  {
108  std::size_t seed (0);
109  hash_label hash;
110 
111  for (typename algebra::polynom<Word, rat::exp<Word2,
112  WeightT> >::const_iterator i = l.begin ();
113  i != l.end ();
114  ++i)
115  {
116  ::boost::hash_combine (seed, i->first);
117  ::boost::hash_combine (seed, hash(i->second));
118  }
119  return seed;
120  }
121 
122  template <typename Weight, typename T, typename U>
123  std::size_t
124  hash_label<algebra::polynom<std::pair<T, U>, Weight> >::operator() (
125  const algebra::polynom<std::pair<T, U>, Weight >& l) const
126  {
127  std::size_t seed (0);
128 
129  for (typename algebra::polynom<std::pair<T, U>, Weight>::const_iterator i = l.begin ();
130  i != l.end ();
131  ++i)
132  {
133  ::boost::hash_combine (seed, i->first.first);
134  ::boost::hash_combine (seed, i->first.second);
135  ::boost::hash_combine (seed, i->second);
136  }
137  return seed;
138  }
139 
140  template <typename Word, typename Weight>
141  std::size_t
142  hash_label<rat::exp<Word, Weight> >::operator() (const rat::exp<Word, Weight>& l) const
143  {
144  rat::HashVisitor<Word, Weight> visitor;
145  l.accept(visitor);
146  return visitor.hash_value();
147  }
148 
149  template <typename Kind, typename Type>
150  std::size_t
151  hash_handler<handler<Kind, Type> >::operator() (const handler<Kind, Type>& h) const
152  {
153  return ::boost::hash_value (h.value());
154  }
155 
156  inline
157  std::size_t
158  hash_handler<char>::operator() (const char c) const
159  {
160  return ::boost::hash_value (c);
161  }
162 
163  }
164 }
165 
166 #endif // ! VCSN_MISC_HASH_HXX_ //