-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFnvHash.h
More file actions
73 lines (62 loc) · 2.08 KB
/
FnvHash.h
File metadata and controls
73 lines (62 loc) · 2.08 KB
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
/*
* FnvHash.h
* Geometry
*
* Created by Christian Brunschen on 22/08/2009.
* Copyright 2009 Christian Brunschen. All rights reserved.
*
*/
#ifndef __FnvHash_h__
#define __FnvHash_h__
#define DEBUG_HASH 0
#if DEBUG_HASH
#define DH(x) do { x; } while(0)
#else
#define DH(x)
#endif
#include <cstddef>
template<size_t = sizeof(size_t)>
struct fnv_hash {
static const size_t init = static_cast<size_t>(0);
static size_t hash(const char *first, size_t length, size_t result = init) {
DH(ios_base::fmtflags flags = cerr.flags(); cerr << "fnv_hash(" << hex << static_cast<const void *>(first) << dec << ", " << length << ", result)" << endl << flush; cerr.flags(flags));
for (; length > 0; --length)
result = (result * 131) + *first++;
return result;
}
};
template<>
struct fnv_hash<4>
{
static const size_t init = static_cast<size_t>(2166136261UL);
static size_t hash(const char* first, size_t length, size_t result = init) {
DH(ios_base::fmtflags flags = cerr.flags(); cerr << "fnv_hash<4>(" << hex << static_cast<const void *>(first) << dec << ", " << length << ", result)" << endl << flush; cerr.flags(flags));
for (; length > 0; --length) {
result ^= static_cast<size_t>(*first++);
result *= static_cast<size_t>(16777619UL);
}
return result;
}
};
template<>
struct fnv_hash<8>
{
static const size_t init = static_cast<size_t>(14695981039346656037ULL);
static size_t hash(const char* first, size_t length, size_t result = init) {
DH(ios_base::fmtflags flags = cerr.flags(); cerr << "fnv_hash<8>(" << hex << static_cast<const void *>(first) << dec << ", " << length << ", result)" << endl << flush; cerr.flags(flags));
for (; length > 0; --length)
{
result ^= static_cast<size_t>(*first++);
result *= static_cast<size_t>(1099511628211ULL);
}
return result;
}
};
template<typename T>
struct continue_hash {
static size_t hash (const T &t, size_t hash = fnv_hash<sizeof(T)>::init) {
return fnv_hash<sizeof(T)>::hash(reinterpret_cast<const char *> (&t), sizeof(T), hash);
}
};
#undef DH
#endif // __FnvHash_h__