forked from redpanda-data/redpanda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hashing: New utils for hashing multiple values
- Loading branch information
1 parent
7a65698
commit a1b4ffb
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 Redpanda Data, Inc. | ||
* | ||
* Use of this software is governed by the Business Source License | ||
* included in the file licenses/BSL.md | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <absl/hash/hash.h> | ||
#include <boost/functional/hash.hpp> | ||
|
||
#include <functional> | ||
|
||
namespace hash { | ||
namespace impl { | ||
/// Pulled from utils/functional.h to prevent making v::utils a dependency of | ||
/// v::hashing | ||
template<typename> | ||
inline constexpr bool always_false_v = false; | ||
|
||
template<typename T> | ||
concept is_absl_hashable = requires(T const& t) { | ||
{ absl::Hash<T>{}(t) } -> std::convertible_to<std::size_t>; | ||
}; | ||
|
||
template<typename T> | ||
concept is_std_hashable = requires(T const& t) { | ||
{ std::hash<T>{}(t) } -> std::convertible_to<std::size_t>; | ||
}; | ||
|
||
template<typename T> | ||
concept is_boost_hashable = requires(T const& t) { | ||
{ boost::hash<T>{}(t) } -> std::convertible_to<std::size_t>; | ||
}; | ||
|
||
template<typename T> | ||
size_t combine(size_t& seed, T const& t) { | ||
if constexpr (is_std_hashable<T>) { | ||
boost::hash_combine(seed, std::hash<T>{}(t)); | ||
} else if constexpr (is_absl_hashable<T>) { | ||
boost::hash_combine(seed, absl::Hash<T>{}(t)); | ||
} else if constexpr (is_boost_hashable<T>) { | ||
boost::hash_combine(seed, boost::hash<T>{}(t)); | ||
} else { | ||
static_assert(always_false_v<T>, "No hasher found for T"); | ||
} | ||
return seed; | ||
} | ||
|
||
} // namespace impl | ||
|
||
template<typename... Args> | ||
size_t combine(size_t& seed, Args&&... args) { | ||
return (impl::combine(seed, std::forward<Args>(args)), ...); | ||
} | ||
|
||
} // namespace hash |