Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 91 additions & 59 deletions include/iris/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#include <functional>
#include <ranges>
#include <utility>
#include <concepts>

#include <cstddef>
#include <cstdint>

namespace iris {

Expand Down Expand Up @@ -54,22 +56,11 @@ template<class Key>
constexpr bool is_hash_enabled_v = is_hash_enabled<Key>::value;


template<class T, class Enabled = void>
struct is_nothrow_hashable : std::false_type
{
static_assert(
!std::is_const_v<T> && !std::is_volatile_v<T> && !std::is_reference_v<T>,
"Although the standard has no restriction on hashing potentially "
"cv-qualified and/or reference types, we intentionally disallow "
"them in our metafunction to prevent error-prone instantiations."
);

static_assert(is_hash_enabled_v<T>, "is_nothrow_hashable must be used only after proper overload resolution in SFINAE-friendly context");
};

template<class T>
struct is_nothrow_hashable<T, std::void_t<decltype(std::hash<T>{}(std::declval<T const&>()))>>
: std::bool_constant<noexcept(std::hash<T>{}(std::declval<T const&>()))>
struct is_nothrow_hashable
: std::bool_constant<requires(T const& x) {
{ std::hash<T>{}(x) } noexcept;
}>
{
static_assert(
!std::is_const_v<T> && !std::is_volatile_v<T> && !std::is_reference_v<T>,
Expand All @@ -91,17 +82,7 @@ constexpr std::size_t hash_mix(std::size_t) noexcept = delete;

// https://jonkagstrom.com/mx3/mx3_rev2.html

template<>
[[nodiscard]] constexpr std::size_t hash_mix<4>(std::size_t x) noexcept
{
x ^= x >> 16;
x *= 0x21f0aaaduz;
x ^= x >> 15;
x *= 0x735a2d97uz;
x ^= x >> 15;
return x;
}

#if SIZE_MAX >= 0xFFFFFFFFFFFFFFFFULL
template<>
[[nodiscard]] constexpr std::size_t hash_mix<8>(std::size_t x) noexcept
{
Expand All @@ -112,55 +93,106 @@ template<>
x ^= x >> 28;
return x;
}
#endif

} // detail

template<class T>
[[nodiscard]] constexpr std::size_t hash_combine(std::size_t const seed, T const& v) noexcept;

template<class T>
[[nodiscard]] constexpr std::size_t hash_value(T const& var) noexcept
template<>
[[nodiscard]] constexpr std::size_t hash_mix<4>(std::size_t x) noexcept
{
static_assert(is_hash_enabled_v<T>);
return std::hash<T>{}(var);
x ^= x >> 16;
x *= 0x21f0aaaduz;
x ^= x >> 15;
x *= 0x735a2d97uz;
x ^= x >> 15;
return x;
}

template<class R>
requires std::ranges::input_range<R const>
[[nodiscard]] constexpr std::size_t hash_value(R const& r)
noexcept(
noexcept(std::ranges::begin(r)) &&
noexcept(std::ranges::end(r)) &&
noexcept(++std::declval<std::ranges::iterator_t<R const>&>()) &&
noexcept(std::declval<std::ranges::iterator_t<R const>&>() != std::declval<std::ranges::sentinel_t<R const>&>()) &&
noexcept(iris::hash_value(*std::declval<std::ranges::iterator_t<R const>&>()))
)
{
std::size_t seed = 0;
auto it = std::ranges::begin(r);
auto const se = std::ranges::end(r);
for (; it != se; ++it) {
seed = iris::hash_combine(seed, iris::hash_value(*it));
}
return seed;
}
} // detail

// https://github.com/boostorg/container_hash/blob/5d8b8ac2b9d9d7cb3818f88fd7e6372e5f072ff5/include/boost/container_hash/hash.hpp#L472C53-L472C63
// https://softwareengineering.stackexchange.com/questions/402542/where-do-magic-hashing-constants-like-0x9e3779b9-and-0x9e3779b1-come-from

template<class T>
[[nodiscard]] constexpr std::size_t hash_combine(std::size_t const seed, T const& v) noexcept
[[nodiscard]] constexpr std::size_t hash_combine(
std::same_as<std::size_t> auto const seed, std::same_as<std::size_t> auto const additional_hash
) noexcept
{
#if SIZE_MAX >= 0xFFFFFFFFFFFFFFFFULL
return detail::hash_mix<sizeof(std::size_t)>(
seed + 0x9e3779b97f4a7c55uz + additional_hash
);
#else
return detail::hash_mix<sizeof(std::size_t)>(
seed + 0x9e3779b97f4a7c55uz + iris::hash_value(v)
seed + 0x9e3779b9uz + additional_hash
);
#endif
}

namespace detail {

namespace adl_hashable_detail {

void hash_value() = delete; // poison pill

template<class T>
concept adl_hashable = requires(T const& x) {
{ hash_value(x) } -> std::convertible_to<std::size_t>;
};

} // adl_hashable_detail

using adl_hashable_detail::adl_hashable;

// This must be CPO to find nested `hash_value` (not solvable with free function)
struct hash_value_fn
{
template<class T>
requires adl_hashable<T>
[[nodiscard]] static constexpr std::size_t operator()(T const& x)
noexcept(noexcept(hash_value(x)))
{
return hash_value(x);
}

template<class T>
requires (!adl_hashable<T>) && is_hash_enabled_v<T>
[[nodiscard]] static constexpr std::size_t operator()(T const& x)
noexcept(is_nothrow_hashable_v<T>)
{
return std::hash<T>{}(x);
}

template<class R>
requires (!adl_hashable<R>) && (!is_hash_enabled_v<R>) && std::ranges::input_range<R const>
[[nodiscard]] static constexpr std::size_t operator()(R const& r)
noexcept(
noexcept(std::ranges::begin(r)) &&
noexcept(std::ranges::end(r)) &&
std::is_nothrow_move_constructible_v<std::ranges::iterator_t<R const>> &&
std::is_nothrow_move_constructible_v<std::ranges::sentinel_t<R const>> &&
noexcept(++std::declval<std::ranges::iterator_t<R const>&>()) &&
noexcept(std::declval<std::ranges::iterator_t<R const>&>() != std::declval<std::ranges::sentinel_t<R const> const&>()) &&
noexcept(std::declval<hash_value_fn const&>()(*std::declval<std::ranges::iterator_t<R const>&>()))
)
{
std::size_t seed = 0;
auto it = std::ranges::begin(r);
auto const se = std::ranges::end(r);
for (; it != se; ++it) {
seed = iris::hash_combine(seed, hash_value_fn{}(*it));
}
return seed;
}
};

} // detail

inline constexpr detail::hash_value_fn hash_value{};

template<class T, class... Ts>
[[nodiscard]] constexpr std::size_t hash_all(T const& first, Ts const&... rest) noexcept
[[nodiscard]] constexpr std::size_t hash_all(T const& first, Ts const&... rest)
noexcept(noexcept(iris::hash_value(first)) && (noexcept(iris::hash_value(rest)) && ...))
{
std::size_t seed = iris::hash_value(first);
((seed = iris::hash_combine(seed, rest)), ...);
((seed = iris::hash_combine(seed, iris::hash_value(rest))), ...);
return seed;
}

Expand Down
15 changes: 9 additions & 6 deletions include/iris/hash/FNV_hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>

#include <cstddef> // IWYU pragma: keep
#include <cstdint>

// https://datatracker.ietf.org/doc/html/draft-eastlake-fnv-03#section-2
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59406
Expand All @@ -20,18 +21,20 @@ namespace detail {
template<std::size_t Base>
struct FNV_hash_numbers;

#if SIZE_MAX >= 0xFFFFFFFFFFFFFFFFULL
template<>
struct FNV_hash_numbers<4>
struct FNV_hash_numbers<8>
{
static constexpr std::size_t FNV_offset_basis = 2166136261uz;
static constexpr std::size_t FNV_prime = 16777619uz;
static constexpr std::size_t FNV_offset_basis = 14695981039346656037uz;
static constexpr std::size_t FNV_prime = 1099511628211uz;
};
#endif

template<>
struct FNV_hash_numbers<8>
struct FNV_hash_numbers<4>
{
static constexpr std::size_t FNV_offset_basis = 14695981039346656037uz;
static constexpr std::size_t FNV_prime = 1099511628211uz;
static constexpr std::size_t FNV_offset_basis = 2166136261uz;
static constexpr std::size_t FNV_prime = 16777619uz;
};

template<std::size_t Base>
Expand Down
31 changes: 7 additions & 24 deletions include/iris/rvariant/rvariant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1809,20 +1809,18 @@ operator<=>(rvariant<Ts...> const& v, rvariant<Ts...> const& w)
} // iris


namespace std {

// https://eel.is/c++draft/variant.hash
template<class... Ts>
requires (::iris::is_hash_enabled_v<std::remove_const_t<Ts>> && ...)
struct hash<::iris::rvariant<Ts...>> // NOLINT(cert-dcl58-cpp)
requires (iris::is_hash_enabled_v<std::remove_const_t<Ts>> && ...)
struct std::hash<iris::rvariant<Ts...>> // NOLINT(cert-dcl58-cpp)
{
[[nodiscard]] static /* constexpr */ std::size_t operator()(::iris::rvariant<Ts...> const& v)
noexcept(std::conjunction_v<::iris::is_nothrow_hashable<std::remove_const_t<Ts>>...>)
[[nodiscard]] static /* constexpr */ std::size_t operator()(iris::rvariant<Ts...> const& v)
noexcept(std::conjunction_v<iris::is_nothrow_hashable<std::remove_const_t<Ts>>...>)
{
return ::iris::detail::raw_visit(v, []<std::size_t i, class T>(std::in_place_index_t<i>, T const& t) static
return iris::detail::raw_visit(v, []<std::size_t i, class T>(std::in_place_index_t<i>, T const& t) static
noexcept(std::disjunction_v<
std::bool_constant<i == std::variant_npos>,
::iris::is_nothrow_hashable<T>
iris::is_nothrow_hashable<T>
>)
{
if constexpr (i == std::variant_npos) {
Expand Down Expand Up @@ -1877,28 +1875,13 @@ struct hash<::iris::rvariant<Ts...>> // NOLINT(cert-dcl58-cpp)
// We assume `hash_combine` is unnecessary here, since the collision
// is very unlikely to occur as long as the `index_hash` is NOT
// evaluated as the re-interpreted bit representation.
constexpr std::size_t index_hash = ::iris::FNV_hash<>::hash(i);
constexpr std::size_t index_hash = iris::FNV_hash<>::hash(i);
return index_hash + std::hash<T>{}(t);
}
});
}
};

} // std


namespace iris {

template<class... Ts>
requires (is_hash_enabled_v<std::remove_const_t<Ts>> && ...)
[[nodiscard]] std::size_t hash_value(rvariant<Ts...> const& v)
noexcept(std::conjunction_v<is_nothrow_hashable<std::remove_const_t<Ts>>...>)
{
return std::hash<rvariant<Ts...>>{}(v);
}

} // iris

#undef IRIS_RVARIANT_DISABLE_UNINITIALIZED_WARNING_BEGIN
#undef IRIS_RVARIANT_DISABLE_UNINITIALIZED_WARNING_END

Expand Down
34 changes: 30 additions & 4 deletions test/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,29 @@ TEST_CASE("throwf")
);
}

} // unit_test

namespace unit_test {

struct HashSpecializedRange
{
int const* begin() const { return nullptr; }
int const* end() const { return nullptr; }
};

} // unit_test

template<>
struct std::hash<unit_test::HashSpecializedRange>
{
static std::size_t operator()(unit_test::HashSpecializedRange const&)
{
return 114514uz;
}
};

namespace unit_test {

TEST_CASE("hash")
{
static_assert(iris::hash_all(std::vector<int>{}) == 0);
Expand All @@ -572,13 +595,16 @@ TEST_CASE("hash")
}
{
std::vector<int> const vec{1};
CHECK(iris::hash_all(vec) == iris::hash_combine(0, iris::hash_value(1)));
CHECK(iris::hash_all(vec) == iris::hash_combine(0, std::hash<int>{}(1)));
CHECK(iris::hash_all(vec) == iris::hash_combine(0uz, iris::hash_value(1)));
}
{
std::vector<int> const vec{1, 2};
CHECK(iris::hash_all(vec) == iris::hash_combine(iris::hash_combine(0, iris::hash_value(1)), iris::hash_value(2)));
CHECK(iris::hash_all(vec) == iris::hash_combine(iris::hash_combine(0, std::hash<int>{}(1)), std::hash<int>{}(2)));
CHECK(iris::hash_all(vec) == iris::hash_combine(iris::hash_combine(0uz, iris::hash_value(1)), iris::hash_value(2)));
}

{
static_assert(std::ranges::input_range<HashSpecializedRange const>);
CHECK(iris::hash_value(HashSpecializedRange{}) == 114514uz); // NOT `0`
}
}

Expand Down
16 changes: 0 additions & 16 deletions test/rvariant/iris_rvariant_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,8 @@ template<class... Ts, class... Args>
throw std::logic_error{"[BUG] failed to create valueless instance; exception was never thrown"};
}

template<class T>
struct HashForwarded
{
T value;
};

} // unit_test


template<class T>
struct std::hash<::unit_test::HashForwarded<T>> // NOLINT(bugprone-std-namespace-modification)
{
std::size_t operator()(::unit_test::HashForwarded<T> const& v) const
{
return std::hash<T>{}(v.value);
}
};

template<class charT>
struct std::formatter<::unit_test::MC_Thrower, charT> // NOLINT(cert-dcl58-cpp)
{
Expand Down
Loading
Loading