Commit 73c483aa authored by Nathan Bronson's avatar Nathan Bronson Committed by Facebook Github Bot

clang-format in preparation for other changes

Summary:
This diff clang-formats a couple of files in preparation for
additional changes.

Reviewed By: yfeldblum, shixiao

Differential Revision: D7197983

fbshipit-source-id: 8995bdaca29bcc44cd5379fc64c76cee89635ac1
parent 92dbb6a8
...@@ -33,7 +33,8 @@ ...@@ -33,7 +33,8 @@
* Various hashing functions. * Various hashing functions.
*/ */
namespace folly { namespace hash { namespace folly {
namespace hash {
// This is a general-purpose way to create a single hash from multiple // This is a general-purpose way to create a single hash from multiple
// hashable objects. hash_combine_generic takes a class Hasher implementing // hashable objects. hash_combine_generic takes a class Hasher implementing
...@@ -41,7 +42,6 @@ namespace folly { namespace hash { ...@@ -41,7 +42,6 @@ namespace folly { namespace hash {
// hash_combine_generic hashes each argument and combines those hashes in // hash_combine_generic hashes each argument and combines those hashes in
// an order-dependent way to yield a new hash. // an order-dependent way to yield a new hash.
// This is the Hash128to64 function from Google's cityhash (available // This is the Hash128to64 function from Google's cityhash (available
// under the MIT License). We use it to reduce multiple 64 bit hashes // under the MIT License). We use it to reduce multiple 64 bit hashes
// into a single hash. // into a single hash.
...@@ -65,10 +65,8 @@ inline size_t hash_combine_generic() { ...@@ -65,10 +65,8 @@ inline size_t hash_combine_generic() {
template < template <
class Iter, class Iter,
class Hash = std::hash<typename std::iterator_traits<Iter>::value_type>> class Hash = std::hash<typename std::iterator_traits<Iter>::value_type>>
uint64_t hash_range(Iter begin, uint64_t
Iter end, hash_range(Iter begin, Iter end, uint64_t hash = 0, Hash hasher = Hash()) {
uint64_t hash = 0,
Hash hasher = Hash()) {
for (; begin != end; ++begin) { for (; begin != end; ++begin) {
hash = hash_128_to_64(hash, hasher(*begin)); hash = hash_128_to_64(hash, hasher(*begin));
} }
...@@ -155,7 +153,7 @@ inline uint32_t twang_32from64(uint64_t key) { ...@@ -155,7 +153,7 @@ inline uint32_t twang_32from64(uint64_t key) {
key = key ^ (key >> 11); key = key ^ (key >> 11);
key = key + (key << 6); key = key + (key << 6);
key = key ^ (key >> 22); key = key ^ (key >> 22);
return (uint32_t) key; return (uint32_t)key;
} }
/* /*
...@@ -194,11 +192,9 @@ inline uint32_t jenkins_rev_unmix32(uint32_t key) { ...@@ -194,11 +192,9 @@ inline uint32_t jenkins_rev_unmix32(uint32_t key) {
// for (int i = n; i < 32; i += n) { // for (int i = n; i < 32; i += n) {
// b ^= (a >> i); // b ^= (a >> i);
// } // }
key ^= key ^= (key >> 2) ^ (key >> 4) ^ (key >> 6) ^ (key >> 8) ^ (key >> 10) ^
(key >> 2) ^ (key >> 4) ^ (key >> 6) ^ (key >> 8) ^ (key >> 12) ^ (key >> 14) ^ (key >> 16) ^ (key >> 18) ^ (key >> 20) ^
(key >> 10) ^ (key >> 12) ^ (key >> 14) ^ (key >> 16) ^ (key >> 22) ^ (key >> 24) ^ (key >> 26) ^ (key >> 28) ^ (key >> 30);
(key >> 18) ^ (key >> 20) ^ (key >> 22) ^ (key >> 24) ^
(key >> 26) ^ (key >> 28) ^ (key >> 30);
key *= 3222273025U; key *= 3222273025U;
key ^= (key >> 9) ^ (key >> 18) ^ (key >> 27); key ^= (key >> 9) ^ (key >> 18) ^ (key >> 27);
key *= 4042322161U; key *= 4042322161U;
...@@ -221,29 +217,29 @@ inline uint32_t fnv32(const char* buf, uint32_t hash = FNV_32_HASH_START) { ...@@ -221,29 +217,29 @@ inline uint32_t fnv32(const char* buf, uint32_t hash = FNV_32_HASH_START) {
const signed char* s = reinterpret_cast<const signed char*>(buf); const signed char* s = reinterpret_cast<const signed char*>(buf);
for (; *s; ++s) { for (; *s; ++s) {
hash += (hash << 1) + (hash << 4) + (hash << 7) + hash +=
(hash << 8) + (hash << 24); (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
hash ^= *s; hash ^= *s;
} }
return hash; return hash;
} }
inline uint32_t fnv32_buf(const void* buf, inline uint32_t
size_t n, fnv32_buf(const void* buf, size_t n, uint32_t hash = FNV_32_HASH_START) {
uint32_t hash = FNV_32_HASH_START) {
// forcing signed char, since other platforms can use unsigned // forcing signed char, since other platforms can use unsigned
const signed char* char_buf = reinterpret_cast<const signed char*>(buf); const signed char* char_buf = reinterpret_cast<const signed char*>(buf);
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
hash += (hash << 1) + (hash << 4) + (hash << 7) + hash +=
(hash << 8) + (hash << 24); (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
hash ^= char_buf[i]; hash ^= char_buf[i];
} }
return hash; return hash;
} }
inline uint32_t fnv32(const std::string& str, inline uint32_t fnv32(
const std::string& str,
uint32_t hash = FNV_32_HASH_START) { uint32_t hash = FNV_32_HASH_START) {
return fnv32_buf(str.data(), str.size(), hash); return fnv32_buf(str.data(), str.size(), hash);
} }
...@@ -260,9 +256,8 @@ inline uint64_t fnv64(const char* buf, uint64_t hash = FNV_64_HASH_START) { ...@@ -260,9 +256,8 @@ inline uint64_t fnv64(const char* buf, uint64_t hash = FNV_64_HASH_START) {
return hash; return hash;
} }
inline uint64_t fnv64_buf(const void* buf, inline uint64_t
size_t n, fnv64_buf(const void* buf, size_t n, uint64_t hash = FNV_64_HASH_START) {
uint64_t hash = FNV_64_HASH_START) {
// forcing signed char, since other platforms can use unsigned // forcing signed char, since other platforms can use unsigned
const signed char* char_buf = reinterpret_cast<const signed char*>(buf); const signed char* char_buf = reinterpret_cast<const signed char*>(buf);
...@@ -274,14 +269,14 @@ inline uint64_t fnv64_buf(const void* buf, ...@@ -274,14 +269,14 @@ inline uint64_t fnv64_buf(const void* buf,
return hash; return hash;
} }
inline uint64_t fnv64(const std::string& str, inline uint64_t fnv64(
const std::string& str,
uint64_t hash = FNV_64_HASH_START) { uint64_t hash = FNV_64_HASH_START) {
return fnv64_buf(str.data(), str.size(), hash); return fnv64_buf(str.data(), str.size(), hash);
} }
inline uint64_t fnva64_buf(const void* buf, inline uint64_t
size_t n, fnva64_buf(const void* buf, size_t n, uint64_t hash = FNVA_64_HASH_START) {
uint64_t hash = FNVA_64_HASH_START) {
const uint8_t* char_buf = reinterpret_cast<const uint8_t*>(buf); const uint8_t* char_buf = reinterpret_cast<const uint8_t*>(buf);
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
...@@ -292,7 +287,8 @@ inline uint64_t fnva64_buf(const void* buf, ...@@ -292,7 +287,8 @@ inline uint64_t fnva64_buf(const void* buf,
return hash; return hash;
} }
inline uint64_t fnva64(const std::string& str, inline uint64_t fnva64(
const std::string& str,
uint64_t hash = FNVA_64_HASH_START) { uint64_t hash = FNVA_64_HASH_START) {
return fnva64_buf(str.data(), str.size(), hash); return fnva64_buf(str.data(), str.size(), hash);
} }
...@@ -318,11 +314,11 @@ inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) { ...@@ -318,11 +314,11 @@ inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) {
len >>= 2; len >>= 2;
/* Main loop */ /* Main loop */
for (;len > 0; len--) { for (; len > 0; len--) {
hash += get16bits (s); hash += get16bits(s);
tmp = (get16bits (s+2) << 11) ^ hash; tmp = (get16bits(s + 2) << 11) ^ hash;
hash = (hash << 16) ^ tmp; hash = (hash << 16) ^ tmp;
s += 2*sizeof (uint16_t); s += 2 * sizeof(uint16_t);
hash += hash >> 11; hash += hash >> 11;
} }
...@@ -331,7 +327,7 @@ inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) { ...@@ -331,7 +327,7 @@ inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) {
case 3: case 3:
hash += get16bits(s); hash += get16bits(s);
hash ^= hash << 16; hash ^= hash << 16;
hash ^= s[sizeof (uint16_t)] << 18; hash ^= s[sizeof(uint16_t)] << 18;
hash += hash >> 11; hash += hash >> 11;
break; break;
case 2: case 2:
...@@ -487,7 +483,8 @@ struct hasher<float> : detail::float_hasher<float> {}; ...@@ -487,7 +483,8 @@ struct hasher<float> : detail::float_hasher<float> {};
template <> template <>
struct hasher<double> : detail::float_hasher<double> {}; struct hasher<double> : detail::float_hasher<double> {};
template <> struct hasher<std::string> { template <>
struct hasher<std::string> {
size_t operator()(const std::string& key) const { size_t operator()(const std::string& key) const {
return static_cast<size_t>( return static_cast<size_t>(
hash::SpookyHashV2::Hash64(key.data(), key.size(), 0)); hash::SpookyHashV2::Hash64(key.data(), key.size(), 0));
...@@ -510,7 +507,7 @@ struct hasher<std::pair<T1, T2>> { ...@@ -510,7 +507,7 @@ struct hasher<std::pair<T1, T2>> {
template <typename... Ts> template <typename... Ts>
struct hasher<std::tuple<Ts...>> { struct hasher<std::tuple<Ts...>> {
size_t operator() (const std::tuple<Ts...>& key) const { size_t operator()(const std::tuple<Ts...>& key) const {
return applyTuple(Hash(), key); return applyTuple(Hash(), key);
} }
}; };
...@@ -520,8 +517,7 @@ template <size_t index, typename... Ts> ...@@ -520,8 +517,7 @@ template <size_t index, typename... Ts>
struct TupleHasher { struct TupleHasher {
size_t operator()(std::tuple<Ts...> const& key) const { size_t operator()(std::tuple<Ts...> const& key) const {
return hash::hash_combine( return hash::hash_combine(
TupleHasher<index - 1, Ts...>()(key), TupleHasher<index - 1, Ts...>()(key), std::get<index>(key));
std::get<index>(key));
} }
}; };
...@@ -551,7 +547,7 @@ struct hash<unsigned __int128> ...@@ -551,7 +547,7 @@ struct hash<unsigned __int128>
// Hash function for pairs. Requires default hash functions for both // Hash function for pairs. Requires default hash functions for both
// items in the pair. // items in the pair.
template <typename T1, typename T2> template <typename T1, typename T2>
struct hash<std::pair<T1, T2> > { struct hash<std::pair<T1, T2>> {
public: public:
size_t operator()(const std::pair<T1, T2>& x) const { size_t operator()(const std::pair<T1, T2>& x) const {
return folly::hash::hash_combine(x.first, x.second); return folly::hash::hash_combine(x.first, x.second);
...@@ -564,7 +560,8 @@ struct hash<std::tuple<Ts...>> { ...@@ -564,7 +560,8 @@ struct hash<std::tuple<Ts...>> {
size_t operator()(std::tuple<Ts...> const& key) const { size_t operator()(std::tuple<Ts...> const& key) const {
folly::TupleHasher< folly::TupleHasher<
std::tuple_size<std::tuple<Ts...>>::value - 1, // start index std::tuple_size<std::tuple<Ts...>>::value - 1, // start index
Ts...> hasher; Ts...>
hasher;
return hasher(key); return hasher(key);
} }
......
...@@ -15,13 +15,16 @@ ...@@ -15,13 +15,16 @@
*/ */
#include <folly/hash/Hash.h> #include <folly/hash/Hash.h>
#include <folly/MapUtil.h>
#include <folly/portability/GTest.h>
#include <stdint.h> #include <stdint.h>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
#include <folly/MapUtil.h>
#include <folly/portability/GTest.h>
using namespace folly::hash; using namespace folly::hash;
TEST(Hash, Fnv32) { TEST(Hash, Fnv32) {
...@@ -76,17 +79,12 @@ TEST(Hash, Fnv64) { ...@@ -76,17 +79,12 @@ TEST(Hash, Fnv64) {
int32_t t4_c = 0xAB12CD34; int32_t t4_c = 0xAB12CD34;
const char* t4_d = "Unum"; const char* t4_d = "Unum";
uint64_t t4_res = 15571330457339273965ULL; uint64_t t4_res = 15571330457339273965ULL;
uint64_t t4_hash1 = fnv64_buf(t4_a, uint64_t t4_hash1 = fnv64_buf(t4_a, strlen(t4_a));
strlen(t4_a)); uint64_t t4_hash2 =
uint64_t t4_hash2 = fnv64_buf(reinterpret_cast<void*>(&t4_b), fnv64_buf(reinterpret_cast<void*>(&t4_b), sizeof(int64_t), t4_hash1);
sizeof(int64_t), uint64_t t4_hash3 =
t4_hash1); fnv64_buf(reinterpret_cast<void*>(&t4_c), sizeof(int32_t), t4_hash2);
uint64_t t4_hash3 = fnv64_buf(reinterpret_cast<void*>(&t4_c), uint64_t t4_hash4 = fnv64_buf(t4_d, strlen(t4_d), t4_hash3);
sizeof(int32_t),
t4_hash2);
uint64_t t4_hash4 = fnv64_buf(t4_d,
strlen(t4_d),
t4_hash3);
EXPECT_EQ(t4_hash4, t4_res); EXPECT_EQ(t4_hash4, t4_res);
// note: These are probabalistic, not determinate, but c'mon. // note: These are probabalistic, not determinate, but c'mon.
// These hash values should be different, or something's not // These hash values should be different, or something's not
...@@ -181,7 +179,7 @@ TEST(Hash, Jenkins_Rev_Unmix32) { ...@@ -181,7 +179,7 @@ TEST(Hash, Jenkins_Rev_Unmix32) {
TEST(Hash, hasher) { TEST(Hash, hasher) {
// Basically just confirms that things compile ok. // Basically just confirms that things compile ok.
std::unordered_map<int32_t,int32_t,folly::hasher<int32_t>> m; std::unordered_map<int32_t, int32_t, folly::hasher<int32_t>> m;
m.insert(std::make_pair(4, 5)); m.insert(std::make_pair(4, 5));
EXPECT_EQ(get_default(m, 4), 5); EXPECT_EQ(get_default(m, 4), 5);
} }
...@@ -280,38 +278,27 @@ TEST(Hash, pair) { ...@@ -280,38 +278,27 @@ TEST(Hash, pair) {
auto b = std::make_pair(3, 4); auto b = std::make_pair(3, 4);
auto c = std::make_pair(1, 2); auto c = std::make_pair(1, 2);
auto d = std::make_pair(2, 1); auto d = std::make_pair(2, 1);
EXPECT_EQ(hash_combine(a), EXPECT_EQ(hash_combine(a), hash_combine(c));
hash_combine(c)); EXPECT_NE(hash_combine(b), hash_combine(c));
EXPECT_NE(hash_combine(b), EXPECT_NE(hash_combine(d), hash_combine(c));
hash_combine(c));
EXPECT_NE(hash_combine(d),
hash_combine(c));
// With composition // With composition
EXPECT_EQ(hash_combine(a, b), EXPECT_EQ(hash_combine(a, b), hash_combine(c, b));
hash_combine(c, b));
// Test order dependence // Test order dependence
EXPECT_NE(hash_combine(a, b), EXPECT_NE(hash_combine(a, b), hash_combine(b, a));
hash_combine(b, a));
// Test with custom hasher // Test with custom hasher
EXPECT_EQ(hash_combine_test(a), EXPECT_EQ(hash_combine_test(a), hash_combine_test(c));
hash_combine_test(c));
// 3 + 4 != 1 + 2 // 3 + 4 != 1 + 2
EXPECT_NE(hash_combine_test(b), EXPECT_NE(hash_combine_test(b), hash_combine_test(c));
hash_combine_test(c));
// This time, thanks to a terrible hash function, these are equal // This time, thanks to a terrible hash function, these are equal
EXPECT_EQ(hash_combine_test(d), EXPECT_EQ(hash_combine_test(d), hash_combine_test(c));
hash_combine_test(c));
// With composition // With composition
EXPECT_EQ(hash_combine_test(a, b), EXPECT_EQ(hash_combine_test(a, b), hash_combine_test(c, b));
hash_combine_test(c, b));
// Test order dependence // Test order dependence
EXPECT_NE(hash_combine_test(a, b), EXPECT_NE(hash_combine_test(a, b), hash_combine_test(b, a));
hash_combine_test(b, a));
// Again, 1 + 2 == 2 + 1 // Again, 1 + 2 == 2 + 1
EXPECT_EQ(hash_combine_test(a, b), EXPECT_EQ(hash_combine_test(a, b), hash_combine_test(d, b));
hash_combine_test(d, b));
} }
TEST(Hash, hash_combine) { TEST(Hash, hash_combine) {
...@@ -419,18 +406,15 @@ TEST(Hash, std_tuple_different_hash) { ...@@ -419,18 +406,15 @@ TEST(Hash, std_tuple_different_hash) {
tuple3 t2(9, "bar", 3); tuple3 t2(9, "bar", 3);
tuple3 t3(42, "foo", 3); tuple3 t3(42, "foo", 3);
EXPECT_NE(std::hash<tuple3>()(t1), EXPECT_NE(std::hash<tuple3>()(t1), std::hash<tuple3>()(t2));
std::hash<tuple3>()(t2)); EXPECT_NE(std::hash<tuple3>()(t1), std::hash<tuple3>()(t3));
EXPECT_NE(std::hash<tuple3>()(t1),
std::hash<tuple3>()(t3));
} }
TEST(Hash, Strings) { TEST(Hash, Strings) {
using namespace folly; using namespace folly;
StringPiece a1 = "10050517", b1 = "51107032", StringPiece a1 = "10050517", b1 = "51107032", a2 = "10050518",
a2 = "10050518", b2 = "51107033", b2 = "51107033", a3 = "10050519", b3 = "51107034",
a3 = "10050519", b3 = "51107034",
a4 = "10050525", b4 = "51107040"; a4 = "10050525", b4 = "51107040";
Range<const wchar_t*> w1 = range(L"10050517"), w2 = range(L"51107032"), Range<const wchar_t*> w1 = range(L"10050517"), w2 = range(L"51107032"),
w3 = range(L"10050518"), w4 = range(L"51107033"); w3 = range(L"10050518"), w4 = range(L"51107033");
...@@ -462,8 +446,8 @@ struct FNVTestParam { ...@@ -462,8 +446,8 @@ struct FNVTestParam {
class FNVTest : public ::testing::TestWithParam<FNVTestParam> {}; class FNVTest : public ::testing::TestWithParam<FNVTestParam> {};
TEST_P(FNVTest, Fnva64Buf) { TEST_P(FNVTest, Fnva64Buf) {
EXPECT_EQ(GetParam().out, EXPECT_EQ(
fnva64_buf(GetParam().in.data(), GetParam().in.size())); GetParam().out, fnva64_buf(GetParam().in.data(), GetParam().in.size()));
} }
TEST_P(FNVTest, Fnva64) { TEST_P(FNVTest, Fnva64) {
...@@ -474,7 +458,8 @@ TEST_P(FNVTest, Fnva64Partial) { ...@@ -474,7 +458,8 @@ TEST_P(FNVTest, Fnva64Partial) {
size_t partialLen = GetParam().in.size() / 2; size_t partialLen = GetParam().in.size() / 2;
auto data = GetParam().in.data(); auto data = GetParam().in.data();
auto partial = fnva64_buf(data, partialLen); auto partial = fnva64_buf(data, partialLen);
EXPECT_EQ(GetParam().out, EXPECT_EQ(
GetParam().out,
fnva64_buf( fnva64_buf(
data + partialLen, GetParam().in.size() - partialLen, partial)); data + partialLen, GetParam().in.size() - partialLen, partial));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment