Skip to content

Commit 36cd4d3

Browse files
committed
std::hash example
1 parent ac3180a commit 36cd4d3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

VS2022/dsga.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
<ClInclude Include="..\examples\basic.hxx" />
177177
<ClInclude Include="..\examples\bezier.hxx" />
178178
<ClInclude Include="..\examples\format_output.hxx" />
179+
<ClInclude Include="..\examples\hash.hxx" />
179180
<ClInclude Include="..\examples\ostream_output.hxx" />
180181
<ClInclude Include="..\examples\span_convert.hxx" />
181182
<ClInclude Include="..\examples\stl.hxx" />

VS2022/dsga.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<ClInclude Include="..\examples\basic.hxx">
4646
<Filter>Header Files</Filter>
4747
</ClInclude>
48+
<ClInclude Include="..\examples\hash.hxx">
49+
<Filter>Header Files</Filter>
50+
</ClInclude>
4851
</ItemGroup>
4952
<ItemGroup>
5053
<None Include="..\README.md" />

examples/hash.hxx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
// Copyright David Browne 2020-2023.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// https://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include "dsga.hxx"
8+
#include <functional>
9+
10+
11+
// based on https://www.boost.org/doc/libs/1_83_0/libs/container_hash/doc/html/hash.html#notes_hash_combine
12+
template <typename T>
13+
constexpr std::size_t hash_combine(std::size_t seed, const T &t) noexcept
14+
{
15+
std::size_t x = seed + 0x9e3779b9 + std::hash<T>{}(t);
16+
17+
x ^= x >> 32;
18+
x *= 0xe9846af9b1a615d;
19+
x ^= x >> 32;
20+
x *= 0xe9846af9b1a615d;
21+
x ^= x >> 28;
22+
23+
return x;
24+
}
25+
26+
template <dsga::dimensional_scalar T, std::size_t S>
27+
struct std::hash<dsga::basic_vector<T, S>>
28+
{
29+
std::size_t operator()(const dsga::basic_vector<T, S> &v) const noexcept
30+
{
31+
std::size_t seed = 0;
32+
33+
[&] <std::size_t ...Js>(std::index_sequence<Js ...>)
34+
{
35+
((seed = hash_combine(seed, v[Js])), ...);
36+
}(std::make_index_sequence<S>{});
37+
38+
return seed;
39+
}
40+
};
41+
42+
template <dsga::floating_point_scalar T, std::size_t C, std::size_t R>
43+
struct std::hash<dsga::basic_matrix<T, C, R>>
44+
{
45+
std::size_t operator()(const dsga::basic_matrix<T, C, R> &m) const noexcept
46+
{
47+
std::size_t seed = 0;
48+
49+
[&] <std::size_t ...Js>(std::index_sequence<Js ...>)
50+
{
51+
((seed = hash_combine(seed, m[Js])), ...);
52+
}(std::make_index_sequence<C>{});
53+
54+
return seed;
55+
}
56+
};

0 commit comments

Comments
 (0)