From f081388afea591903c5902aabcb1650d70eb2f53 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Mon, 15 Jan 2018 01:27:30 -0800 Subject: [PATCH] Common: Adapt CityHash code to match our codebase better - Use #pragma once instead of guards - Move header typedefs to implementation file - Enclose in Common namespace --- src/common/cityhash.cpp | 8 ++++++ src/common/cityhash.h | 55 +++++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/common/cityhash.cpp b/src/common/cityhash.cpp index 84f044ed0..ab8db7774 100644 --- a/src/common/cityhash.cpp +++ b/src/common/cityhash.cpp @@ -42,6 +42,12 @@ using namespace std; +typedef uint8_t uint8; +typedef uint32_t uint32; +typedef uint64_t uint64; + +namespace Common { + static uint64 UNALIGNED_LOAD64(const char* p) { uint64 result; memcpy(&result, p, sizeof(result)); @@ -648,3 +654,5 @@ uint128 CityHashCrc128(const char* s, size_t len) { } #endif + +} // namespace Common diff --git a/src/common/cityhash.h b/src/common/cityhash.h index 94499ce41..e6baf6a37 100644 --- a/src/common/cityhash.h +++ b/src/common/cityhash.h @@ -59,54 +59,55 @@ // of a+b is easily derived from the hashes of a and b. This property // doesn't hold for any hash functions in this file. -#ifndef CITY_HASH_H_ -#define CITY_HASH_H_ +#pragma once -#include // for size_t. -#include #include +#include +#include // for size_t. -typedef uint8_t uint8; -typedef uint32_t uint32; -typedef uint64_t uint64; -typedef std::pair uint128; +namespace Common { -inline uint64 Uint128Low64(const uint128& x) { return x.first; } -inline uint64 Uint128High64(const uint128& x) { return x.second; } +typedef std::pair uint128; + +inline uint64_t Uint128Low64(const uint128& x) { + return x.first; +} +inline uint64_t Uint128High64(const uint128& x) { + return x.second; +} // Hash function for a byte array. -uint64 CityHash64(const char *buf, size_t len); +uint64_t CityHash64(const char* buf, size_t len); // Hash function for a byte array. For convenience, a 64-bit seed is also // hashed into the result. -uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed); +uint64_t CityHash64WithSeed(const char* buf, size_t len, uint64_t seed); // Hash function for a byte array. For convenience, two seeds are also // hashed into the result. -uint64 CityHash64WithSeeds(const char *buf, size_t len, - uint64 seed0, uint64 seed1); +uint64_t CityHash64WithSeeds(const char* buf, size_t len, uint64_t seed0, uint64_t seed1); // Hash function for a byte array. -uint128 CityHash128(const char *s, size_t len); +uint128 CityHash128(const char* s, size_t len); // Hash function for a byte array. For convenience, a 128-bit seed is also // hashed into the result. -uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed); +uint128 CityHash128WithSeed(const char* s, size_t len, uint128 seed); // Hash function for a byte array. Most useful in 32-bit binaries. -uint32 CityHash32(const char *buf, size_t len); +uint32_t CityHash32(const char* buf, size_t len); // Hash 128 input bits down to 64 bits of output. // This is intended to be a reasonably good hash function. -inline uint64 Hash128to64(const uint128& x) { - // Murmur-inspired hashing. - const uint64 kMul = 0x9ddfea08eb382d69ULL; - uint64 a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; - a ^= (a >> 47); - uint64 b = (Uint128High64(x) ^ a) * kMul; - b ^= (b >> 47); - b *= kMul; - return b; +inline uint64_t Hash128to64(const uint128& x) { + // Murmur-inspired hashing. + const uint64_t kMul = 0x9ddfea08eb382d69ULL; + uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; + a ^= (a >> 47); + uint64_t b = (Uint128High64(x) ^ a) * kMul; + b ^= (b >> 47); + b *= kMul; + return b; } -#endif // CITY_HASH_H_ +} // namespace Common