nlib
Hash.h
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_HASH_H_
4 #define INCLUDE_NN_NLIB_HASH_H_
5 
6 #include <functional>
7 #include "nn/nlib/Config.h"
8 
9 #if defined(NLIB_CXX11_TEMPLATE_ALIAS)
10 
11 template<class T> using Hash = std::hash<T>;
12 
13 #define NLIB_DEFINE_STD_HASH_BEGIN1(ns1) namespace std { // NOLINT
14 #define NLIB_DEFINE_STD_HASH_BEGIN2(ns1, ns2) namespace std { // NOLINT
15 #define NLIB_DEFINE_STD_HASH_BEGIN3(ns1, ns2, ns3) namespace std { // NOLINT
16 #define NLIB_DEFINE_STD_HASH_BEGIN4(ns1, ns2, ns3, ns4) namespace std { // NOLINT
17 #define NLIB_DEFINE_STD_HASH_END1(ns1) } // NOLINT
18 #define NLIB_DEFINE_STD_HASH_END2(ns1, ns2) } // NOLINT
19 #define NLIB_DEFINE_STD_HASH_END3(ns1, ns2, ns3) } // NOLINT
20 #define NLIB_DEFINE_STD_HASH_END4(ns1, ns2, ns3, ns4) } // NOLINT
21 #define NLIB_DEFINE_STD_HASH_T(tp) \
22 template<> struct hash<tp> { \
23  size_t operator()(const tp& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
24 };
25 #define NLIB_DEFINE_STD_HASH_T2(targ1, targ2, tp) \
26 template<class targ1, class targ2> struct hash<tp<targ1, targ2> > { \
27  size_t operator()(const tp<targ1, targ2>& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
28 };
29 
30 #else
31 NLIB_NAMESPACE_BEGIN
32 
33 namespace detail {
34 inline size_t Times33(const unsigned char* p, size_t n) NLIB_NOEXCEPT {
35  size_t hash = 0;
36  for (; n != 0; --n, ++p) { hash = hash * 33 + *p; }
37  return hash;
38 }
39 } // namespace detail
40 
41 template<class T>
42 struct SimpleHash : public std::unary_function<T, size_t> {
43  size_t operator()(const T& val) const NLIB_NOEXCEPT {
44  return detail::Times33(
45  reinterpret_cast<const unsigned char*>(&val), sizeof(val));
46  }
47 };
48 
49 template<class T> struct Hash : public SimpleHash<T> {};
50 
51 template<> struct Hash<bool> : public SimpleHash<bool> {};
52 template<> struct Hash<char> : public SimpleHash<char> {};
53 template<> struct Hash<signed char> : public SimpleHash<signed char> {};
54 template<> struct Hash<unsigned char> : public SimpleHash<unsigned char> {};
55 template<> struct Hash<wchar_t> : public SimpleHash<wchar_t> {};
56 template<> struct Hash<short> : public SimpleHash<short> {}; // NOLINT
57 template<> struct Hash<unsigned short> : public SimpleHash<unsigned short> {}; // NOLINT
58 template<> struct Hash<int> : public SimpleHash<int> {};
59 template<> struct Hash<unsigned int> : public SimpleHash<unsigned int> {};
60 template<> struct Hash<long> : public SimpleHash<long> {}; // NOLINT
61 template<> struct Hash<unsigned long> : public SimpleHash<unsigned long> {}; // NOLINT
62 template<> struct Hash<long long> : public SimpleHash<long long> {}; // NOLINT
63 template<> struct Hash<unsigned long long> : public SimpleHash<unsigned long long> {}; // NOLINT
64 template<> struct Hash<float> : public SimpleHash<float> {};
65 template<> struct Hash<double> : public SimpleHash<double> {};
66 template<class T> struct Hash<T*> : public SimpleHash<T*> {};
67 
68 // NOTE:
69 // This file defines the subset of C++11 std::hash.
70 // No definitions for std::string, std::wstring for now
71 #define NLIB_DEFINE_STD_HASH_BEGIN1(ns1) namespace ns1 { // NOLINT
72 #define NLIB_DEFINE_STD_HASH_BEGIN2(ns1, ns2) namespace ns1 { namespace ns2 { // NOLINT
73 #define NLIB_DEFINE_STD_HASH_BEGIN3(ns1, ns2, ns3) \
74  namespace ns1 { namespace ns2 { namespace ns3 { // NOLINT
75 #define NLIB_DEFINE_STD_HASH_BEGIN4(ns1, ns2, ns3, ns4) \
76  namespace ns1 { namespace ns2 { namespace ns3 { namespace ns4 { // NOLINT
77 #define NLIB_DEFINE_STD_HASH_END1(ns1) } // NOLINT
78 #define NLIB_DEFINE_STD_HASH_END2(ns1, ns2) }} // NOLINT
79 #define NLIB_DEFINE_STD_HASH_END3(ns1, ns2, ns3) }}} // NOLINT
80 #define NLIB_DEFINE_STD_HASH_END4(ns1, ns2, ns3, ns4) }}}} // NOLINT
81 
82 #define NLIB_DEFINE_STD_HASH_T(tp) \
83 template<> struct Hash<tp> { \
84  size_t operator()(const tp& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
85 };
86 #define NLIB_DEFINE_STD_HASH_T2(targ1, targ2, tp) \
87 template<class targ1, class targ2> struct Hash<tp<targ1, targ2> > { \
88  size_t operator()(const tp<targ1, targ2>& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
89 };
90 
91 
92 NLIB_NAMESPACE_END
93 #endif
94 
95 
96 
97 #endif // INCLUDE_NN_NLIB_HASH_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
A file that contains the configuration information for each development environment.