Skip to content

Commit 6896dbf

Browse files
committed
Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear).
1 parent 96f2119 commit 6896dbf

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/hash.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,34 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
1717
{
1818
// The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
1919
uint32_t h1 = nHashSeed;
20-
if (vDataToHash.size() > 0)
21-
{
22-
const uint32_t c1 = 0xcc9e2d51;
23-
const uint32_t c2 = 0x1b873593;
20+
const uint32_t c1 = 0xcc9e2d51;
21+
const uint32_t c2 = 0x1b873593;
2422

25-
const int nblocks = vDataToHash.size() / 4;
23+
const int nblocks = vDataToHash.size() / 4;
2624

27-
//----------
28-
// body
29-
const uint8_t* blocks = &vDataToHash[0] + nblocks * 4;
25+
//----------
26+
// body
27+
const uint8_t* blocks = vDataToHash.data();
3028

31-
for (int i = -nblocks; i; i++) {
32-
uint32_t k1 = ReadLE32(blocks + i*4);
29+
for (int i = 0; i < nblocks; ++i) {
30+
uint32_t k1 = ReadLE32(blocks + i*4);
3331

34-
k1 *= c1;
35-
k1 = ROTL32(k1, 15);
36-
k1 *= c2;
32+
k1 *= c1;
33+
k1 = ROTL32(k1, 15);
34+
k1 *= c2;
3735

38-
h1 ^= k1;
39-
h1 = ROTL32(h1, 13);
40-
h1 = h1 * 5 + 0xe6546b64;
41-
}
36+
h1 ^= k1;
37+
h1 = ROTL32(h1, 13);
38+
h1 = h1 * 5 + 0xe6546b64;
39+
}
4240

43-
//----------
44-
// tail
45-
const uint8_t* tail = (const uint8_t*)(&vDataToHash[0] + nblocks * 4);
41+
//----------
42+
// tail
43+
const uint8_t* tail = vDataToHash.data() + nblocks * 4;
4644

47-
uint32_t k1 = 0;
45+
uint32_t k1 = 0;
4846

49-
switch (vDataToHash.size() & 3) {
47+
switch (vDataToHash.size() & 3) {
5048
case 3:
5149
k1 ^= tail[2] << 16;
5250
case 2:
@@ -57,7 +55,6 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
5755
k1 = ROTL32(k1, 15);
5856
k1 *= c2;
5957
h1 ^= k1;
60-
}
6158
}
6259

6360
//----------

0 commit comments

Comments
 (0)