Skip to content

Commit

Permalink
hashing: New utils for hashing multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
graphcareful committed Jan 17, 2024
1 parent 7a65698 commit a1b4ffb
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/v/hashing/utils.h
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

0 comments on commit a1b4ffb

Please sign in to comment.