| 1 |
|
| // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
| 2 |
|
| // Use of this source code is governed by a BSD-style license that can be |
| 3 |
|
| // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 |
|
| |
| 5 |
|
| #include <string.h> |
| 6 |
|
| #include "util/coding.h" |
| 7 |
|
| #include "util/hash.h" |
| 8 |
|
| |
| 9 |
|
| namespace leveldb { |
| 10 |
|
| |
| 11 |
5 |
| uint32_t Hash(const char* data, size_t n, uint32_t seed) { |
| 12 |
|
| // Similar to murmur hash |
| 13 |
5 |
| const uint32_t m = 0xc6a4a793; |
| 14 |
5 |
| const uint32_t r = 24; |
| 15 |
5 |
| const char* limit = data + n; |
| 16 |
5 |
| uint32_t h = seed ^ (n * m); |
| 17 |
|
| |
| 18 |
|
| // Pick up four bytes at a time |
| 19 |
19 |
▶ | while (data + 4 <= limit) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (data + 4 <= limit) { |
14 |
5 |
19 |
|
| 20 |
14 |
| uint32_t w = DecodeFixed32(data); |
| 21 |
14 |
| data += 4; |
| 22 |
14 |
| h += w; |
| 23 |
14 |
| h *= m; |
| 24 |
14 |
| h ^= (h >> 16); |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
|
| // Pick up remaining bytes |
| 28 |
5 |
| switch (limit - data) { |
| 29 |
0 |
| case 3: |
| 30 |
0 |
| h += data[2] << 16; |
| 31 |
|
| // fall through |
| 32 |
0 |
| case 2: |
| 33 |
0 |
| h += data[1] << 8; |
| 34 |
|
| // fall through |
| 35 |
0 |
| case 1: |
| 36 |
0 |
| h += data[0]; |
| 37 |
0 |
| h *= m; |
| 38 |
0 |
| h ^= (h >> r); |
| 39 |
0 |
| break; |
| 40 |
|
| } |
| 41 |
5 |
| return h; |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
|
| |
| 45 |
|
| } |