ATLAS Offline Software
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  */
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 
32 namespace CxxUtils {
33 
34 
35 uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed );
36 uint64_t MurmurHash64A ( const void * key, int len, uint64_t seed );
37 uint64_t MurmurHash64B ( const void * key, int len, uint64_t seed );
38 uint32_t MurmurHash2A ( const void * key, int len, uint32_t seed );
39 uint32_t MurmurHashNeutral2 ( const void * key, int len, uint32_t seed );
40 uint32_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 {
61 public:
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 = *(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 
102 private:
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 
129 };
130 
131 
132 } // namespace CxxUtils
133 
134 
135 #endif // not CXXUTILS_MURMURHASH2_H
CxxUtils::MurmurHash64A
uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
Definition: MurmurHash2.cxx:103
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
CxxUtils::CMurmurHash2A
Definition: MurmurHash2.h:60
CxxUtils::CMurmurHash2A::Begin
void Begin(uint32_t seed=0)
Definition: MurmurHash2.h:63
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
MurmurHash_mmix
#define MurmurHash_mmix(h, k)
Definition: MurmurHash2.h:29
CxxUtils::MurmurHashAligned2
uint32_t MurmurHashAligned2(const void *key, int len, uint32_t seed)
Definition: MurmurHash2.cxx:324
CxxUtils::CMurmurHash2A::MixTail
void MixTail(const unsigned char *&data, int &len)
Definition: MurmurHash2.h:107
CxxUtils::MurmurHash2
uint32_t MurmurHash2(const void *key, int len, uint32_t seed)
Definition: MurmurHash2.cxx:42
CxxUtils::MurmurHash64B
uint64_t MurmurHash64B(const void *key, int len, uint64_t seed)
Definition: MurmurHash2.cxx:155
CxxUtils::CMurmurHash2A::m_size
uint32_t m_size
Definition: MurmurHash2.h:128
CxxUtils::CMurmurHash2A::R
static constexpr int R
Definition: MurmurHash2.h:105
CxxUtils::MurmurHashNeutral2
uint32_t MurmurHashNeutral2(const void *key, int len, uint32_t seed)
Definition: MurmurHash2.cxx:267
CxxUtils
Definition: aligned_vector.h:29
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
CxxUtils::CMurmurHash2A::m_hash
uint32_t m_hash
Definition: MurmurHash2.h:125
CxxUtils::CMurmurHash2A::Add
void Add(const unsigned char *data, int len)
Definition: MurmurHash2.h:71
CxxUtils::CMurmurHash2A::m_tail
uint32_t m_tail
Definition: MurmurHash2.h:126
CxxUtils::CMurmurHash2A::End
uint32_t End(void)
Definition: MurmurHash2.h:90
CxxUtils::MurmurHash2A
uint32_t MurmurHash2A(const void *key, int len, uint32_t seed)
Definition: MurmurHash2.cxx:220
CxxUtils::CMurmurHash2A::M
static constexpr uint32_t M
Definition: MurmurHash2.h:104
CxxUtils::CMurmurHash2A::m_count
uint32_t m_count
Definition: MurmurHash2.h:127
fitman.k
k
Definition: fitman.py:528
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37