forked from ValveSoftware/GameNetworkingSockets
-
Notifications
You must be signed in to change notification settings - Fork 15
/
generichash.h
150 lines (125 loc) · 3.5 KB
/
generichash.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
//======= Copyright (c) 2005-2011, Valve Corporation, All rights reserved. =========
//
// Public domain MurmurHash3 by Austin Appleby is a very solid general-purpose
// hash with a 32-bit output. References:
// http://code.google.com/p/smhasher/ (home of MurmurHash3)
// https://sites.google.com/site/murmurhash/avalanche
// http://www.strchr.com/hash_functions
//
// Variant Pearson Hash general purpose hashing algorithm described
// by Cargill in C++ Report 1994. Generates a 16-bit result.
// Now relegated to PearsonHash namespace, not recommended for use;
// still around in case someone needs value compatibility with old code.
//
//=============================================================================
#pragma once
#ifndef GENERICHASH_H
#define GENERICHASH_H
#include "miniutl.h"
uint32_t MurmurHash3_32( const void *key, size_t len, uint32_t seed, bool bCaselessStringVariant = false );
void MurmurHash3_128( const void * key, const int len, const uint32_t seed, void * out );
inline uint32_t HashString( const char *pszKey, size_t len )
{
return MurmurHash3_32( pszKey, len, 1047 /*anything will do for a seed*/, false );
}
inline uint32_t HashStringCaseless( const char *pszKey, size_t len )
{
return MurmurHash3_32( pszKey, len, 1047 /*anything will do for a seed*/, true );
}
inline uint32_t HashString( const char *pszKey )
{
return HashString( pszKey, strlen( pszKey ) );
}
inline uint32_t HashStringCaseless( const char *pszKey )
{
return HashStringCaseless( pszKey, strlen( pszKey ) );
}
inline uint32_t HashInt64( uint64_t h )
{
// roughly equivalent to MurmurHash3_32( &lower32, sizeof(uint32), upper32_as_seed )...
// theory being that most of the entropy is in the lower 32 bits and we still mix
// everything together at the end, so not fully shuffling upper32 is not a big deal
uint32_t h1 = static_cast<uint32_t>( h>>32 );
uint32_t k1 = (uint32_t)h;
k1 *= 0xcc9e2d51;
k1 = (k1 << 15) | (k1 >> 17);
k1 *= 0x1b873593;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >> 19);
h1 = h1*5+0xe6546b64;
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >> 16;
return h1;
}
inline uint32_t HashInt( uint32_t h )
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
template <typename T>
inline uint32_t HashItemAsBytes( const T&item )
{
if ( sizeof( item ) == sizeof( uint32_t ))
return HashInt( *(uint32_t*)&item );
if ( sizeof( item ) == sizeof( uint64_t ))
return HashInt64( *(uint64_t*)&item );
return MurmurHash3_32( &item, sizeof(item), 1047 );
}
template <typename T>
inline uint32_t HashItem( const T &item )
{
return HashItemAsBytes( item );
}
template<typename T>
struct HashFunctor
{
typedef uint32_t TargetType;
TargetType operator()(const T &key) const
{
return HashItem( key );
}
};
template<>
struct HashFunctor<char *>
{
typedef uint32_t TargetType;
TargetType operator()(const char *key) const
{
return HashString( key );
}
};
template<>
struct HashFunctor<char const *>
{
typedef uint32_t TargetType;
TargetType operator()(const char *key) const
{
return HashString( key );
}
};
struct HashFunctorStringCaseless
{
typedef uint32_t TargetType;
TargetType operator()(const char *key) const
{
return HashStringCaseless( key );
}
};
template<class T>
struct HashFunctorUnpaddedStructure
{
typedef uint32_t TargetType;
TargetType operator()(const T &key) const
{
return HashItemAsBytes( key );
}
};
//-----------------------------------------------------------------------------
#endif /* !GENERICHASH_H */