ATLAS Offline Software
Loading...
Searching...
No Matches
MurmurHash2.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Public domain; see below.
4 */
13
14
15#ifndef CXXUTILS_MURMURHASH2_H
16#define CXXUTILS_MURMURHASH2_H
17
18
19//-----------------------------------------------------------------------------
20// MurmurHash2 was written by Austin Appleby, and is placed in the public
21// domain. The author hereby disclaims copyright to this source code.
22
23//-----------------------------------------------------------------------------
24// Platform-specific functions and macros
25
26#include <cstdint>
27
28
29#define MurmurHash_mmix(h,k) { k *= M; k ^= k >> R; k *= M; h *= M; h ^= k; }
30
31
32namespace CxxUtils {
33
34
35uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed );
36uint64_t MurmurHash64A ( const void * key, int len, uint64_t seed );
37uint64_t MurmurHash64B ( const void * key, int len, uint64_t seed );
38uint32_t MurmurHash2A ( const void * key, int len, uint32_t seed );
39uint32_t MurmurHashNeutral2 ( const void * key, int len, uint32_t seed );
40uint32_t MurmurHashAligned2 ( const void * key, int len, uint32_t seed );
41
42
43//-----------------------------------------------------------------------------
44// CMurmurHash2A, by Austin Appleby
45
46// This is a sample implementation of MurmurHash2A designed to work
47// incrementally.
48
49// Usage -
50
51// CMurmurHash2A hasher
52// hasher.Begin(seed);
53// hasher.Add(data1,size1);
54// hasher.Add(data2,size2);
55// ...
56// hasher.Add(dataN,sizeN);
57// uint32_t hash = hasher.End()
58
60{
61public:
62
63 void Begin ( uint32_t seed = 0 )
64 {
65 m_hash = seed;
66 m_tail = 0;
67 m_count = 0;
68 m_size = 0;
69 }
70
71 void Add ( const unsigned char * data, int len )
72 {
73 m_size += len;
74
75 MixTail(data,len);
76
77 while(len >= 4)
78 {
79 uint32_t k = *reinterpret_cast<const uint32_t*>(data);
80
82
83 data += 4;
84 len -= 4;
85 }
86
87 MixTail(data,len);
88 }
89
90 uint32_t End ( void )
91 {
94
95 m_hash ^= m_hash >> 13;
96 m_hash *= M;
97 m_hash ^= m_hash >> 15;
98
99 return m_hash;
100 }
101
102private:
103
104 static constexpr uint32_t M = 0x5bd1e995;
105 static constexpr int R = 24;
106
107 void MixTail ( const unsigned char * & data, int & len )
108 {
109 while( len && ((len<4) || m_count) )
110 {
111 m_tail |= (*data++) << (m_count * 8);
112
113 m_count++;
114 len--;
115
116 if(m_count == 4)
117 {
119 m_tail = 0;
120 m_count = 0;
121 }
122 }
123 }
124
125 uint32_t m_hash;
126 uint32_t m_tail;
127 uint32_t m_count;
128 uint32_t m_size;
129};
130
131
132} // namespace CxxUtils
133
134
135#endif // not CXXUTILS_MURMURHASH2_H
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
#define MurmurHash_mmix(h, k)
Definition MurmurHash2.h:29
static constexpr int R
void Add(const unsigned char *data, int len)
Definition MurmurHash2.h:71
static constexpr uint32_t M
void MixTail(const unsigned char *&data, int &len)
void Begin(uint32_t seed=0)
Definition MurmurHash2.h:63
uint32_t MurmurHash2A(const void *key, int len, uint32_t seed)
uint32_t MurmurHash2(const void *key, int len, uint32_t seed)
uint64_t MurmurHash64B(const void *key, int len, uint64_t seed)
uint32_t MurmurHashAligned2(const void *key, int len, uint32_t seed)
uint32_t MurmurHashNeutral2(const void *key, int len, uint32_t seed)
uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)