paludis  Version 2.6.0
hashes.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2008, 2010, 2011 Ciaran McCreesh
5  *
6  * This file is part of the Paludis package manager. Paludis is free software;
7  * you can redistribute it and/or modify it under the terms of the GNU General
8  * Public License version 2, as published by the Free Software Foundation.
9  *
10  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_HASHES_HH
21 #define PALUDIS_GUARD_PALUDIS_UTIL_HASHES_HH 1
22 
24 #include <paludis/util/wrapped_value-fwd.hh>
25 #include <paludis/util/fs_path-fwd.hh>
26 #include <cstddef>
27 #include <tuple>
28 #include <utility>
29 #include <string>
30 #include <type_traits>
31 #include <memory>
32 
33 namespace paludis
34 {
35  namespace hash_internals
36  {
37  template <typename T_, bool is_numeric_>
38  struct DefaultHash
39  {
40  static std::size_t hash(const T_ & t)
41  {
42  return static_cast<std::size_t>(t);
43  }
44  };
45 
46  template <typename T_>
47  struct DefaultHash<T_, false>
48  {
49  static std::size_t hash(const T_ & t)
50  {
51  return t.hash();
52  }
53  };
54  }
55 
56  template <typename T_>
57  class Hash
58  {
59  public:
60  std::size_t operator() (const T_ & t) const
61  {
62  return hash_internals::DefaultHash<T_, std::is_integral<T_>::value || std::is_enum<T_>::value>::hash(t);
63  }
64  };
65 
66  template <>
67  struct PALUDIS_VISIBLE Hash<std::string>
68  {
69  std::size_t operator() (const std::string &) const PALUDIS_ATTRIBUTE((warn_unused_result));
70  };
71 
72  template <>
74  {
75  std::size_t operator() (const FSPath &) const PALUDIS_ATTRIBUTE((warn_unused_result));
76  };
77 
78  template <typename T_, typename U_>
79  struct Hash<std::pair<T_, U_> >
80  {
81  std::size_t operator() (const std::pair<T_, U_> & p) const
82  {
83  return Hash<T_>()(p.first) ^ Hash<U_>()(p.second);
84  }
85  };
86 
87  template <unsigned n_, typename... Keys_>
88  std::size_t single_tuple_hash(const std::tuple<Keys_...> & p)
89  {
90  return Hash<typename std::tuple_element<n_, std::tuple<Keys_...> >::type>()(std::get<n_>(p));
91  }
92 
94  {
95  };
96 
98  {
99  };
100 
101  template <unsigned n_, typename... Keys_>
102  std::size_t accumulate_tuple_hash(const std::tuple<Keys_...> &, std::size_t v, const FinishedHashingTuple &)
103  {
104  return v;
105  }
106 
107  template <unsigned n_, typename... Keys_>
108  std::size_t accumulate_tuple_hash(const std::tuple<Keys_...> & p, std::size_t v, const NotFinishedHashingTuple &)
109  {
110  return accumulate_tuple_hash<n_ + 1, Keys_...>(p, (v << 8) ^ single_tuple_hash<n_>(p),
111  typename std::conditional<std::tuple_size<std::tuple<Keys_...> >::value == n_ + 1,
113  }
114 
115  template <typename... Keys_>
116  struct Hash<std::tuple<Keys_...> >
117  {
118  std::size_t operator() (const std::tuple<Keys_...> & p) const
119  {
120  return accumulate_tuple_hash<0, Keys_...>(p, 0, NotFinishedHashingTuple());
121  }
122  };
123 
124  template <typename Tag_>
125  struct Hash<WrappedValue<Tag_> >
126  {
127  std::size_t operator() (const WrappedValue<Tag_> & v) const
128  {
130  }
131  };
132 
133  template <typename T_>
134  struct Hash<const T_>
135  {
136  std::size_t operator() (const T_ & t) const
137  {
138  return Hash<T_>()(t);
139  }
140  };
141 
142  template <typename T_>
143  struct Hash<T_ &>
144  {
145  std::size_t operator() (const T_ & t) const
146  {
147  return Hash<T_>()(t);
148  }
149  };
150 
151  template <typename T_>
152  struct Hash<T_ *>
153  {
154  std::size_t operator() (const T_ * const t) const
155  {
156  return Hash<T_>(*t);
157  }
158  };
159 
160  template <typename T_>
161  struct Hash<std::shared_ptr<T_> >
162  {
163  std::size_t operator() (const std::shared_ptr<const T_> & t) const
164  {
165  return Hash<T_>()(*t);
166  }
167  };
168 }
169 
170 #endif
Definition: about_metadata-fwd.hh:23
Definition: hashes.hh:97
STL namespace.
Definition: hashes.hh:38
Definition: hashes.hh:93
Definition: hashes.hh:57
Definition: wrapped_value-fwd.hh:29
Definition: fs_path.hh:33
#define PALUDIS_ATTRIBUTE(x)
Definition: attributes.hh:53
#define PALUDIS_VISIBLE
Definition: attributes.hh:59