/Users/deen/code/yugabyte-db/src/yb/gutil/hash/legacy_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 | | // This is a library of legacy hashing routines. These routines are still in |
18 | | // use, but are not encouraged for any new code, and may be removed at some |
19 | | // point in the future. |
20 | | // |
21 | | // New code should use one of the targeted libraries that provide hash |
22 | | // interfaces for the types needed. See //util/hash/README for details. |
23 | | |
24 | | #ifndef YB_GUTIL_HASH_LEGACY_HASH_H |
25 | | #define YB_GUTIL_HASH_LEGACY_HASH_H |
26 | | |
27 | | #include "yb/gutil/hash/builtin_type_hash.h" |
28 | | #include "yb/gutil/hash/jenkins.h" |
29 | | #include "yb/gutil/integral_types.h" |
30 | | |
31 | | // Hash8, Hash16 and Hash32 are for legacy use only. |
32 | | typedef uint32 Hash32; |
33 | | typedef uint16 Hash16; |
34 | | typedef uint8 Hash8; |
35 | | |
36 | | const Hash32 kIllegalHash32 = static_cast<Hash32>(0xffffffffUL); |
37 | | const Hash16 kIllegalHash16 = static_cast<Hash16>(0xffff); |
38 | | |
39 | | static const uint32 MIX32 = 0x12b9b0a1UL; // pi; an arbitrary number |
40 | | static const uint64 MIX64 = GG_ULONGLONG(0x2b992ddfa23249d6); // more of pi |
41 | | |
42 | | // ---------------------------------------------------------------------- |
43 | | // HashTo32() |
44 | | // HashTo16() |
45 | | // These functions take various types of input (through operator |
46 | | // overloading) and return 32 or 16 bit quantities, respectively. |
47 | | // The basic rule of our hashing is: always mix(). Thus, even for |
48 | | // char outputs we cast to a uint32 and mix with two arbitrary numbers. |
49 | | // HashTo32 never returns kIllegalHash32, and similary, |
50 | | // HashTo16 never returns kIllegalHash16. |
51 | | // |
52 | | // Note that these methods avoid returning certain reserved values, while |
53 | | // the corresponding HashXXStringWithSeed() methods may return any value. |
54 | | // ---------------------------------------------------------------------- |
55 | | |
56 | | // This macro defines the HashTo32 and HashTo16 versions all in one go. |
57 | | // It takes the argument list and a command that hashes your number. |
58 | | // (For 16 we just mod retval before returning it.) Example: |
59 | | // HASH_TO((char c), Hash32NumWithSeed(c, MIX32_1)) |
60 | | // evaluates to |
61 | | // uint32 retval; |
62 | | // retval = Hash32NumWithSeed(c, MIX32_1); |
63 | | // return retval == kIllegalHash32 ? retval-1 : retval; |
64 | | // |
65 | | |
66 | | #define HASH_TO(arglist, command) \ |
67 | 124M | inline uint32 HashTo32 arglist { \ |
68 | 124M | uint32 retval = command; \ |
69 | 124M | return retval == kIllegalHash32 ? retval-10 : retval; \ |
70 | 124M | } HashTo32(char const*, unsigned long) Line | Count | Source | 67 | 124M | inline uint32 HashTo32 arglist { \ | 68 | 124M | uint32 retval = command; \ | 69 | 124M | return retval == kIllegalHash32 ? retval-10 : retval; \ | 70 | 124M | } |
Unexecuted instantiation: HashTo32(wchar_t const*, unsigned long) Unexecuted instantiation: HashTo32(char) Unexecuted instantiation: HashTo32(signed char) Unexecuted instantiation: HashTo32(unsigned short) Unexecuted instantiation: HashTo32(short) Unexecuted instantiation: HashTo32(unsigned int) Unexecuted instantiation: HashTo32(int) Unexecuted instantiation: HashTo32(unsigned long long) Unexecuted instantiation: HashTo32(long long) |
71 | | |
72 | | // This defines: |
73 | | // HashToXX(char *s, int slen); |
74 | | // HashToXX(char c); |
75 | | // etc |
76 | | |
77 | | HASH_TO((const char *s, size_t slen), Hash32StringWithSeed(s, slen, MIX32)) |
78 | | HASH_TO((const wchar_t *s, size_t slen), |
79 | | Hash32StringWithSeed(reinterpret_cast<const char*>(s), |
80 | | sizeof(wchar_t) * slen, |
81 | | MIX32)) |
82 | | HASH_TO((char c), Hash32NumWithSeed(static_cast<uint32>(c), MIX32)) |
83 | | HASH_TO((schar c), Hash32NumWithSeed(static_cast<uint32>(c), MIX32)) |
84 | | HASH_TO((uint16 c), Hash32NumWithSeed(static_cast<uint32>(c), MIX32)) |
85 | | HASH_TO((int16 c), Hash32NumWithSeed(static_cast<uint32>(c), MIX32)) |
86 | | HASH_TO((uint32 c), Hash32NumWithSeed(static_cast<uint32>(c), MIX32)) |
87 | | HASH_TO((int32 c), Hash32NumWithSeed(static_cast<uint32>(c), MIX32)) |
88 | | HASH_TO((uint64 c), static_cast<uint32>(Hash64NumWithSeed(c, MIX64) >> 32)) |
89 | | HASH_TO((int64 c), static_cast<uint32>(Hash64NumWithSeed(c, MIX64) >> 32)) |
90 | | |
91 | | #undef HASH_TO // clean up the macro space |
92 | | |
93 | 0 | inline uint16 HashTo16(const char *s, uint64 slen) { |
94 | 0 | uint16 retval = Hash32StringWithSeed(s, slen, MIX32) >> 16; |
95 | 0 | return retval == kIllegalHash16 ? static_cast<uint16>(retval-1) : retval; |
96 | 0 | } |
97 | | |
98 | | #endif // YB_GUTIL_HASH_LEGACY_HASH_H |