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
40 changes: 36 additions & 4 deletions include/iris/hash.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef IRIS_HASH_HPP
#ifndef IRIS_HASH_HPP
#define IRIS_HASH_HPP

// SPDX-License-Identifier: MIT
Expand All @@ -7,7 +7,7 @@
#include <iris/type_traits.hpp>

#include <functional>
#include <bit>
#include <ranges>

#include <cstddef>

Expand Down Expand Up @@ -111,18 +111,50 @@ template<>

} // 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
{
static_assert(is_hash_enabled_v<T>);
return std::hash<T>{}(var);
}

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

// 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
{
static_assert(is_hash_enabled_v<T>);
return detail::hash_mix<sizeof(std::size_t)>(
seed + 0x9e3779b97f4a7c55uz + std::hash<T>{}(v)
seed + 0x9e3779b97f4a7c55uz + iris::hash_value(v)
);
}

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

} // iris

#endif
22 changes: 22 additions & 0 deletions test/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include <iris/compare.hpp>
#include <iris/fixed_string.hpp>
#include <iris/exception.hpp>
#include <iris/hash.hpp>

#include <concepts>
#include <utility>
#include <type_traits>
#include <ranges>
#include <vector>

namespace unit_test {

Expand Down Expand Up @@ -522,4 +524,24 @@ TEST_CASE("throwf")
);
}

TEST_CASE("hash")
{
static_assert(iris::hash_all(std::vector<int>{}) == 0);
{
int const value = 0;
CHECK(iris::hash_all(value) == iris::hash_value(value));
CHECK(iris::hash_all(value) == std::hash<int>{}(value));
}
{
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)));
}
{
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)));
}
}

} // unit_test