Files
scylladb/utils/hash.hh
Rafael Ávila de Espíndola 336d541f58 database: Use a flat_hash_map for _ks_cf_to_uuid
Given that the key is a std::pair, we have to explicitly mark the hash
and eq types as transparent for heterogeneous lookup to work.

With that, pass std::string_view to a few functions that just check if
a value is in the map.

This increases the .text section by 11 KiB (0.03%).

Signed-off-by: Rafael Ávila de Espíndola <espindola@scylladb.com>
2020-06-14 08:18:39 -07:00

87 lines
2.7 KiB
C++

/*
* Copyright (C) 2015 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTILS_HASH_HH_
#define UTILS_HASH_HH_
#include <functional>
namespace utils {
// public for unit testing etc
inline size_t hash_combine(size_t left, size_t right) {
return left + 0x9e3779b9 + (right << 6) + (right >> 2);
}
struct tuple_hash {
private:
// CMH. Add specializations here to handle recursive tuples
template<typename T>
static size_t hash(const T& t) {
return std::hash<T>()(t);
}
template<size_t index, typename...Types>
struct hash_impl {
size_t operator()(const std::tuple<Types...>& t, size_t a) const {
return hash_impl<index-1, Types...>()(t, hash_combine(hash(std::get<index>(t)), a));
}
size_t operator()(const std::tuple<Types...>& t) const {
return hash_impl<index-1, Types...>()(t, hash(std::get<index>(t)));
}
};
template<class...Types>
struct hash_impl<0, Types...> {
size_t operator()(const std::tuple<Types...>& t, size_t a) const {
return hash_combine(hash(std::get<0>(t)), a);
}
size_t operator()(const std::tuple<Types...>& t) const {
return hash(std::get<0>(t));
}
};
public:
// All the operator() implementations are templates, so this is transparent.
using is_transparent = void;
template<typename T1, typename T2>
size_t operator()(const std::pair<T1, T2>& p) const {
return hash_combine(hash(p.first), hash(p.second));
}
template<typename T1, typename T2>
size_t operator()(const T1& t1, const T2& t2) const {
return hash_combine(hash(t1), hash(t2));
}
template<typename... Args>
size_t operator()(const std::tuple<Args...>& v) const;
};
template<typename... Args>
inline size_t tuple_hash::operator()(const std::tuple<Args...>& v) const {
return hash_impl<std::tuple_size<std::tuple<Args...>>::value - 1, Args...>()(v);
}
template<>
inline size_t tuple_hash::operator()(const std::tuple<>& v) const {
return 0;
}
}
#endif /* UTILS_HASH_HH_ */