/Users/deen/code/yugabyte-db/src/yb/gutil/hash/string_hash.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2011 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // The following only applies to changes made to this file as part of YugaByte development. |
4 | | // |
5 | | // Portions Copyright (c) YugaByte, Inc. |
6 | | // |
7 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
8 | | // in compliance with the License. You may obtain a copy of the License at |
9 | | // |
10 | | // http://www.apache.org/licenses/LICENSE-2.0 |
11 | | // |
12 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
13 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
14 | | // or implied. See the License for the specific language governing permissions and limitations |
15 | | // under the License. |
16 | | // |
17 | | // These are the core hashing routines which operate on strings. We define |
18 | | // strings loosely as a sequence of bytes, and these routines are designed to |
19 | | // work with the most fundamental representations of a string of bytes. |
20 | | // |
21 | | // These routines provide "good" hash functions in terms of both quality and |
22 | | // speed. Their values can and will change as their implementations change and |
23 | | // evolve. |
24 | | |
25 | | #ifndef UTIL_HASH_STRING_HASH_H_ |
26 | | #define UTIL_HASH_STRING_HASH_H_ |
27 | | |
28 | | #include <stddef.h> |
29 | | |
30 | | #include "yb/gutil/port.h" |
31 | | #include "yb/gutil/integral_types.h" |
32 | | #include "yb/gutil/hash/city.h" |
33 | | #include "yb/gutil/hash/jenkins.h" |
34 | | #include "yb/gutil/hash/jenkins_lookup2.h" |
35 | | |
36 | | namespace hash_internal { |
37 | | |
38 | | // We have some special cases for 64-bit hardware and x86-64 in particular. |
39 | | // Instead of sprinkling ifdefs through the file, we have one ugly ifdef here. |
40 | | // Later code can then use "if" instead of "ifdef". |
41 | | #if defined(__x86_64__) |
42 | | enum { x86_64 = true, sixty_four_bit = true }; |
43 | | #elif defined(_LP64) |
44 | | enum { x86_64 = false, sixty_four_bit = true }; |
45 | | #else |
46 | | enum { x86_64 = false, sixty_four_bit = false }; |
47 | | #endif |
48 | | |
49 | | // Arbitrary mix constants (pi). |
50 | | static const uint32 kMix32 = 0x12b9b0a1UL; |
51 | | static const uint64 kMix64 = GG_ULONGLONG(0x2b992ddfa23249d6); |
52 | | |
53 | | } // namespace hash_internal |
54 | | |
55 | | inline size_t HashStringThoroughlyWithSeed(const char* s, size_t len, |
56 | 0 | size_t seed) { |
57 | 0 | if (hash_internal::x86_64) |
58 | 0 | return static_cast<size_t>(util_hash::CityHash64WithSeed(s, len, seed)); |
59 | 0 |
|
60 | 0 | if (hash_internal::sixty_four_bit) |
61 | 0 | return Hash64StringWithSeed(s, static_cast<uint32>(len), seed); |
62 | 0 |
|
63 | 0 | return static_cast<size_t>(Hash32StringWithSeed(s, static_cast<uint32>(len), |
64 | 0 | static_cast<uint32>(seed))); |
65 | 0 | } |
66 | | |
67 | 5.66M | inline size_t HashStringThoroughly(const char* s, size_t len) { |
68 | 5.66M | if (hash_internal::x86_64) |
69 | 0 | return static_cast<size_t>(util_hash::CityHash64(s, len)); |
70 | | |
71 | 5.66M | if (hash_internal::sixty_four_bit) |
72 | 5.66M | return Hash64StringWithSeed(s, static_cast<uint32>(len), |
73 | 5.66M | hash_internal::kMix64); |
74 | | |
75 | 18.4E | return static_cast<size_t>(Hash32StringWithSeed(s, static_cast<uint32>(len), |
76 | 18.4E | hash_internal::kMix32)); |
77 | 18.4E | } |
78 | | |
79 | | inline size_t HashStringThoroughlyWithSeeds(const char* s, size_t len, |
80 | 0 | size_t seed0, size_t seed1) { |
81 | 0 | if (hash_internal::x86_64) |
82 | 0 | return util_hash::CityHash64WithSeeds(s, len, seed0, seed1); |
83 | 0 |
|
84 | 0 | if (hash_internal::sixty_four_bit) { |
85 | 0 | uint64 a = seed0; |
86 | 0 | uint64 b = seed1; |
87 | 0 | uint64 c = HashStringThoroughly(s, len); |
88 | 0 | mix(a, b, c); |
89 | 0 | return c; |
90 | 0 | } |
91 | 0 |
|
92 | 0 | uint32 a = static_cast<uint32>(seed0); |
93 | 0 | uint32 b = static_cast<uint32>(seed1); |
94 | 0 | uint32 c = static_cast<uint32>(HashStringThoroughly(s, len)); |
95 | 0 | mix(a, b, c); |
96 | 0 | return c; |
97 | 0 | } |
98 | | |
99 | | #endif // UTIL_HASH_STRING_HASH_H_ |