nlib
Hash.h
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_HASH_H_
17 #define INCLUDE_NN_NLIB_HASH_H_
18 
19 #include <functional>
20 #include "nn/nlib/Config.h"
21 
22 #if defined(__cpp_alias_templates)
23 
24 template<class T>
25 using Hash = std::hash<T>;
26 
27 #define NLIB_DEFINE_STD_HASH_BEGIN2(ns1, ns2) namespace std {
28 #define NLIB_DEFINE_STD_HASH_BEGIN3(ns1, ns2, ns3) namespace std {
29 #define NLIB_DEFINE_STD_HASH_BEGIN4(ns1, ns2, ns3, ns4) namespace std {
30 #define NLIB_DEFINE_STD_HASH_END2(ns1, ns2) }
31 #define NLIB_DEFINE_STD_HASH_END3(ns1, ns2, ns3) }
32 #define NLIB_DEFINE_STD_HASH_END4(ns1, ns2, ns3, ns4) }
33 #define NLIB_DEFINE_STD_HASH_T(tp) \
34  template<> \
35  struct hash<tp> { \
36  size_t operator()(const tp& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
37  };
38 #define NLIB_DEFINE_STD_HASH_T2(targ1, targ2, tp) \
39  template<class targ1, class targ2> \
40  struct hash<tp<targ1, targ2> > { \
41  size_t operator()(const tp<targ1, targ2>& val) const NLIB_NOEXCEPT { \
42  return val.GetHash(); \
43  } \
44  };
45 
46 #else
47 NLIB_NAMESPACE_BEGIN
48 
49 namespace detail {
50 inline size_t Times33(const unsigned char* p, size_t n) NLIB_NOEXCEPT {
51  size_t hash = 0;
52  for (; n != 0; --n, ++p) {
53  hash = hash * 33 + *p;
54  }
55  return hash;
56 }
57 } // namespace detail
58 
59 template<class T>
60 struct SimpleHash : public std::unary_function<T, size_t> {
61  size_t operator()(const T& val) const NLIB_NOEXCEPT {
62  return detail::Times33(reinterpret_cast<const unsigned char*>(&val), sizeof(val));
63  }
64 };
65 
66 template<class T>
67 struct Hash : public SimpleHash<T> {};
68 
69 template<>
70 struct Hash<bool> : public SimpleHash<bool> {};
71 template<>
72 struct Hash<char> : public SimpleHash<char> {};
73 template<>
74 struct Hash<signed char> : public SimpleHash<signed char> {};
75 template<>
76 struct Hash<unsigned char> : public SimpleHash<unsigned char> {};
77 template<>
78 struct Hash<wchar_t> : public SimpleHash<wchar_t> {};
79 template<>
80 struct Hash<short> : public SimpleHash<short> {};
81 template<>
82 struct Hash<unsigned short> : public SimpleHash<unsigned short> {};
83 template<>
84 struct Hash<int> : public SimpleHash<int> {};
85 template<>
86 struct Hash<unsigned int> : public SimpleHash<unsigned int> {};
87 template<>
88 struct Hash<long> : public SimpleHash<long> {};
89 template<>
90 struct Hash<unsigned long> : public SimpleHash<unsigned long> {};
91 template<>
92 struct Hash<long long> : public SimpleHash<long long> {};
93 template<>
94 struct Hash<unsigned long long> : public SimpleHash<unsigned long long> {};
95 template<>
96 struct Hash<float> : public SimpleHash<float> {};
97 template<>
98 struct Hash<double> : public SimpleHash<double> {};
99 template<class T>
100 struct Hash<T*> : public SimpleHash<T*> {};
101 
102 // NOTE:
103 // This file defines the subset of C++11 std::hash.
104 // No definitions for std::string, std::wstring for now
105 #define NLIB_DEFINE_STD_HASH_BEGIN2(ns1, ns2) NLIB_NAMESPACE_BEGIN
106 #define NLIB_DEFINE_STD_HASH_BEGIN3(ns1, ns2, ns3) NLIB_NAMESPACE_BEGIN namespace ns3 {
107 #define NLIB_DEFINE_STD_HASH_BEGIN4(ns1, ns2, ns3, ns4) \
108  NLIB_NAMESPACE_BEGIN namespace ns3 { \
109  namespace ns4 {
110 #define NLIB_DEFINE_STD_HASH_END2(ns1, ns2) NLIB_NAMESPACE_END
111 #define NLIB_DEFINE_STD_HASH_END3(ns1, ns2, ns3) \
112  } \
113  NLIB_NAMESPACE_END
114 #define NLIB_DEFINE_STD_HASH_END4(ns1, ns2, ns3, ns4) \
115  } \
116  } \
117  NLIB_NAMESPACE_END
118 
119 #define NLIB_DEFINE_STD_HASH_T(tp) \
120  template<> \
121  struct Hash<tp> { \
122  size_t operator()(const tp& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
123  };
124 #define NLIB_DEFINE_STD_HASH_T2(targ1, targ2, tp) \
125  template<class targ1, class targ2> \
126  struct Hash<tp<targ1, targ2> > { \
127  size_t operator()(const tp<targ1, targ2>& val) const NLIB_NOEXCEPT { \
128  return val.GetHash(); \
129  } \
130  };
131 
132 NLIB_NAMESPACE_END
133 #endif
134 
135 #endif // INCLUDE_NN_NLIB_HASH_H_
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
開発環境別の設定が書かれるファイルです。