-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_funcs.h
217 lines (193 loc) · 4.99 KB
/
hash_funcs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* hash_funcs.h
*
* ** THOR INTERNAL FILE - NOT FOR APPLICATION USE **
*
* This file defines STL-compatible hash functors
*/
#ifndef THOR_HASH_FUNCS_H
#define THOR_HASH_FUNCS_H
#pragma once
#ifndef THOR_BASETYPES_H
#include "basetypes.h"
#endif
namespace thor
{
// Hash functions
// Default (no implementation)
template <class T> struct hash
{
};
// Pointer specializations
template <class T> struct hash<T*>
{
size_type operator () (const T* p) const
{
size_type s = (size_type)(void*)p;
if (THOR_SUPPRESS_WARNING(sizeof(p) == 4))
{
return (s >> 2);
}
else if (THOR_SUPPRESS_WARNING(sizeof(p) == 8))
{
return (s >> 3);
}
THOR_ASSERT(0); // Unknown pointer size
return s;
}
};
#if 0
template <class T> struct hash<const T*>
{
size_type operator () (const T* p) const
{
size_type s = (size_t)(void*)p;
if (THOR_SUPPRESS_WARNING(sizeof(p) == 4))
{
return (s >> 2);
}
else if (THOR_SUPPRESS_WARNING(sizeof(p) == 8))
{
return (s >> 3);
}
THOR_ASSERT(0); // Unknown pointer size
return s;
}
};
#endif
// Intrinsic specializations
#define INTRINSIC_HASH(__type) \
template <> struct hash< __type > \
{ \
size_type operator () (__type t) const { return (size_type)t; } \
}
INTRINSIC_HASH(char);
INTRINSIC_HASH(unsigned char);
INTRINSIC_HASH(short);
INTRINSIC_HASH(unsigned short);
INTRINSIC_HASH(int);
INTRINSIC_HASH(unsigned int);
INTRINSIC_HASH(long);
INTRINSIC_HASH(unsigned long);
#ifdef _NATIVE_WCHAR_T_DEFINED
INTRINSIC_HASH(wchar_t);
#endif
#undef INTRINSIC_HASH
template <class T> size_type __hashstring(T *s)
{
// Jenkins One-at-a-time hash function
size_type hash = 0;
while (*s != 0)
{
hash += *s;
hash += (hash << 10);
hash ^= (hash >> 6);
s++;
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
template <class T> size_type __hashstring(T *s, size_type len)
{
// Jenkins One-at-a-time hash function
size_type hash = 0;
const T* end = s + len;
while (s < end)
{
hash += *s;
hash += (hash << 10);
hash ^= (hash >> 6);
s++;
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
// String types specialization
template <> struct hash<char*>
{
size_type operator () (const char* p) const { return __hashstring(p); }
};
template <> struct hash<const char*>
{
size_type operator () (const char* p) const { return __hashstring(p); }
};
template <> struct hash<wchar_t*>
{
size_type operator () (const wchar_t* p) const { return __hashstring(p); }
};
template <> struct hash<const wchar_t*>
{
size_type operator () (const wchar_t* p) const { return __hashstring(p); }
};
// 64-bit POD type specializations
template <> struct hash< long long >
{
size_type operator () (long long t) const
{
THOR_COMPILETIME_ASSERT(sizeof(t) == 8, InvalidSizeAssumption);
THOR_COMPILETIME_ASSERT((sizeof(t)/sizeof(size_type)) <= 2, InvalidSizeAssumption2);
if (THOR_SUPPRESS_WARNING(sizeof(size_type) == 4))
{
// 64-bit t, 32-bit size_t
return (size_type(t) & size_type(-1)) ^ size_type(t >> 32);
}
else
{
// Both t and size_t are 64-bit
THOR_ASSERT(sizeof(size_type) == sizeof(t));
return size_type(t);
}
}
};
template <> struct hash< unsigned long long >
{
size_type operator () (unsigned long long t) const
{
THOR_COMPILETIME_ASSERT(sizeof(t) == 8, InvalidSizeAssumption);
THOR_COMPILETIME_ASSERT((sizeof(t)/sizeof(size_type)) <= 2, InvalidSizeAssumption2);
if (THOR_SUPPRESS_WARNING(sizeof(size_type) == 4))
{
// 64-bit t, 32-bit size_t
return (size_type(t) & size_type(-1)) ^ size_type(t >> 32);
}
else
{
// Both t and size_t are 64-bit
THOR_ASSERT(sizeof(size_type) == sizeof(t));
return (size_type)t;
}
}
};
// floating point specializations
template <> struct hash<float>
{
size_type operator () (float f) const
{
THOR_COMPILETIME_ASSERT(sizeof(f) == sizeof(unsigned), InvalidSizeAssumption);
return hash<unsigned>() (*(unsigned*)&f);
}
};
template <> struct hash<double>
{
size_type operator () (double d) const
{
THOR_COMPILETIME_ASSERT(sizeof(d) == sizeof(unsigned long long), InvalidSizeAssumption);
return hash<unsigned long long>() (*(unsigned long long*)&d);
}
};
template <> struct hash<long double>
{
size_type operator () (long double d) const
{
THOR_COMPILETIME_ASSERT(sizeof(d) == sizeof(unsigned long long), InvalidSizeAssumption);
return hash<unsigned long long>() (*(unsigned long long*)&d);
}
};
}; // namespace thor
#endif