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> using Hash = std::hash<T>;
25 
26 #define NLIB_DEFINE_STD_HASH_BEGIN2(ns1, ns2) namespace std { // NOLINT
27 #define NLIB_DEFINE_STD_HASH_BEGIN3(ns1, ns2, ns3) namespace std { // NOLINT
28 #define NLIB_DEFINE_STD_HASH_BEGIN4(ns1, ns2, ns3, ns4) namespace std { // NOLINT
29 #define NLIB_DEFINE_STD_HASH_END2(ns1, ns2) } // NOLINT
30 #define NLIB_DEFINE_STD_HASH_END3(ns1, ns2, ns3) } // NOLINT
31 #define NLIB_DEFINE_STD_HASH_END4(ns1, ns2, ns3, ns4) } // NOLINT
32 #define NLIB_DEFINE_STD_HASH_T(tp) \
33 template<> struct hash<tp> { \
34  size_t operator()(const tp& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
35 };
36 #define NLIB_DEFINE_STD_HASH_T2(targ1, targ2, tp) \
37 template<class targ1, class targ2> struct hash<tp<targ1, targ2> > { \
38  size_t operator()(const tp<targ1, targ2>& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
39 };
40 
41 #else
42 NLIB_NAMESPACE_BEGIN
43 
44 namespace detail {
45 inline size_t Times33(const unsigned char* p, size_t n) NLIB_NOEXCEPT {
46  size_t hash = 0;
47  for (; n != 0; --n, ++p) { hash = hash * 33 + *p; }
48  return hash;
49 }
50 } // namespace detail
51 
52 template<class T>
53 struct SimpleHash : public std::unary_function<T, size_t> {
54  size_t operator()(const T& val) const NLIB_NOEXCEPT {
55  return detail::Times33(
56  reinterpret_cast<const unsigned char*>(&val), sizeof(val));
57  }
58 };
59 
60 template<class T> struct Hash : public SimpleHash<T> {};
61 
62 template<> struct Hash<bool> : public SimpleHash<bool> {};
63 template<> struct Hash<char> : public SimpleHash<char> {};
64 template<> struct Hash<signed char> : public SimpleHash<signed char> {};
65 template<> struct Hash<unsigned char> : public SimpleHash<unsigned char> {};
66 template<> struct Hash<wchar_t> : public SimpleHash<wchar_t> {};
67 template<> struct Hash<short> : public SimpleHash<short> {}; // NOLINT
68 template<> struct Hash<unsigned short> : public SimpleHash<unsigned short> {}; // NOLINT
69 template<> struct Hash<int> : public SimpleHash<int> {};
70 template<> struct Hash<unsigned int> : public SimpleHash<unsigned int> {};
71 template<> struct Hash<long> : public SimpleHash<long> {}; // NOLINT
72 template<> struct Hash<unsigned long> : public SimpleHash<unsigned long> {}; // NOLINT
73 template<> struct Hash<long long> : public SimpleHash<long long> {}; // NOLINT
74 template<> struct Hash<unsigned long long> : public SimpleHash<unsigned long long> {}; // NOLINT
75 template<> struct Hash<float> : public SimpleHash<float> {};
76 template<> struct Hash<double> : public SimpleHash<double> {};
77 template<class T> struct Hash<T*> : public SimpleHash<T*> {};
78 
79 // NOTE:
80 // This file defines the subset of C++11 std::hash.
81 // No definitions for std::string, std::wstring for now
82 #define NLIB_DEFINE_STD_HASH_BEGIN2(ns1, ns2) NLIB_NAMESPACE_BEGIN // NOLINT
83 #define NLIB_DEFINE_STD_HASH_BEGIN3(ns1, ns2, ns3) \
84  NLIB_NAMESPACE_BEGIN namespace ns3 { // NOLINT
85 #define NLIB_DEFINE_STD_HASH_BEGIN4(ns1, ns2, ns3, ns4) \
86  NLIB_NAMESPACE_BEGIN namespace ns3 { namespace ns4 { // NOLINT
87 #define NLIB_DEFINE_STD_HASH_END2(ns1, ns2) NLIB_NAMESPACE_END // NOLINT
88 #define NLIB_DEFINE_STD_HASH_END3(ns1, ns2, ns3) } NLIB_NAMESPACE_END // NOLINT
89 #define NLIB_DEFINE_STD_HASH_END4(ns1, ns2, ns3, ns4) }} NLIB_NAMESPACE_END // NOLINT
90 
91 #define NLIB_DEFINE_STD_HASH_T(tp) \
92 template<> struct Hash<tp> { \
93  size_t operator()(const tp& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
94 };
95 #define NLIB_DEFINE_STD_HASH_T2(targ1, targ2, tp) \
96 template<class targ1, class targ2> struct Hash<tp<targ1, targ2> > { \
97  size_t operator()(const tp<targ1, targ2>& val) const NLIB_NOEXCEPT { return val.GetHash(); } \
98 };
99 
100 
101 NLIB_NAMESPACE_END
102 #endif
103 
104 
105 
106 #endif // INCLUDE_NN_NLIB_HASH_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
A file that contains the configuration information for each development environment.