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