Skip to content

Commit baa6954

Browse files
committed
Improve hash combination
Per 4641ad2#r147400088
1 parent 4641ad2 commit baa6954

File tree

9 files changed

+433
-431
lines changed

9 files changed

+433
-431
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10166087914140344613
1+
6440649391998984779

regression-tests/test-results/gcc-10-c++20/pure2-hashable.cpp.output

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ pure2-hashable.cpp2:1:7: note: candidates are: ‘constexpr base& base::operator
2121
pure2-hashable.cpp2:1:7: note: ‘constexpr base& base::operator=(const base&)’
2222
pure2-hashable.cpp2:6:14: note: ‘template<class auto:97> base& base::operator=(auto:97&&)’
2323
pure2-hashable.cpp2:1:7: note: ‘class base’ defined here
24-
pure2-hashable.cpp2:11:1: error: no declaration matches ‘mystruct::mystruct(auto:103&&, auto:104&&, auto:105&&) requires (is_convertible_v<typename std::remove_cv<typename std::remove_reference<decltype(mystruct::__ct ::i_)>::type>::type, const std::add_const_t&>) && (is_convertible_v<typename std::remove_cv<typename std::remove_reference<decltype(mystruct::__ct ::j_)>::type>::type, const std::__cxx11::add_const_t<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&>) && (is_convertible_v<typename std::remove_cv<typename std::remove_reference<decltype(mystruct::__ct ::k_)>::type>::type, const std::add_const_t&>)’
24+
pure2-hashable.cpp2:17:1: error: no declaration matches ‘mystruct::mystruct(auto:103&&, auto:104&&, auto:105&&) requires (is_convertible_v<typename std::remove_cv<typename std::remove_reference<decltype(mystruct::__ct ::i_)>::type>::type, const std::add_const_t&>) && (is_convertible_v<typename std::remove_cv<typename std::remove_reference<decltype(mystruct::__ct ::j_)>::type>::type, const std::__cxx11::add_const_t<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&>) && (is_convertible_v<typename std::remove_cv<typename std::remove_reference<decltype(mystruct::__ct ::k_)>::type>::type, const std::add_const_t&>)’
2525
pure2-hashable.cpp2:5:7: note: candidates are: ‘mystruct::mystruct(mystruct&&)’
2626
pure2-hashable.cpp2:5:7: note: ‘mystruct::mystruct(const mystruct&)’
2727
pure2-hashable.cpp2:10:13: note: ‘template<class auto:98, class auto:99, class auto:100> mystruct::mystruct(auto:98&&, auto:99&&, auto:100&&)’
2828
pure2-hashable.cpp2:5:7: note: ‘class mystruct’ defined here
29-
pure2-hashable.cpp2:14:104: error: mixing declarations and function-definitions is forbidden
30-
pure2-hashable.cpp2:14:107: error: expected constructor, destructor, or type conversion before ‘{’ token
31-
pure2-hashable.cpp2:15:104: error: expected unqualified-id before ‘,’ token
32-
pure2-hashable.cpp2:15:107: error: expected constructor, destructor, or type conversion before ‘{’ token
33-
pure2-hashable.cpp2:16:104: error: expected unqualified-id before ‘,’ token
34-
pure2-hashable.cpp2:16:107: error: expected constructor, destructor, or type conversion before ‘{’ token
35-
pure2-hashable.cpp2:16:127: error: expected unqualified-id before ‘{’ token
29+
pure2-hashable.cpp2:20:104: error: mixing declarations and function-definitions is forbidden
30+
pure2-hashable.cpp2:20:107: error: expected constructor, destructor, or type conversion before ‘{’ token
31+
pure2-hashable.cpp2:21:104: error: expected unqualified-id before ‘,’ token
32+
pure2-hashable.cpp2:21:107: error: expected constructor, destructor, or type conversion before ‘{’ token
33+
pure2-hashable.cpp2:22:104: error: expected unqualified-id before ‘,’ token
34+
pure2-hashable.cpp2:22:107: error: expected constructor, destructor, or type conversion before ‘{’ token
35+
pure2-hashable.cpp2:22:127: error: expected unqualified-id before ‘{’ token
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10166087914140344613
1+
6440649391998984779
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13273923326899311054
1+
13958440649017415620

regression-tests/test-results/pure2-hashable.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,28 @@ auto base::operator=(auto&& h_) -> base&
5757
requires (std::is_convertible_v<CPP2_TYPEOF(h_), std::add_const_t<cpp2::i32>&>) {
5858
h = CPP2_FORWARD(h_);
5959
return *this;}
60-
[[nodiscard]] auto base::hash() const& -> size_t { return std::hash<cpp2::i32>()(h); }
60+
[[nodiscard]] auto base::hash() const& -> size_t{
61+
62+
size_t ret {0};
63+
ret ^= std::hash<cpp2::i32>()(h) + (ret << 6) + (ret >> 2);
64+
return ret;
65+
}
66+
6167
mystruct::mystruct(auto&& i_, auto&& j_, auto&& k_)
6268
requires (std::is_convertible_v<CPP2_TYPEOF(i_), std::add_const_t<cpp2::i32>&> && std::is_convertible_v<CPP2_TYPEOF(j_), std::add_const_t<std::string>&> && std::is_convertible_v<CPP2_TYPEOF(k_), std::add_const_t<cpp2::u64>&>)
6369
: base{ (1) }
6470
, i{ CPP2_FORWARD(i_) }
6571
, j{ CPP2_FORWARD(j_) }
6672
, k{ CPP2_FORWARD(k_) }{}
67-
[[nodiscard]] auto mystruct::hash() const& -> size_t { return base::hash() ^ (std::hash<cpp2::i32>()(i) << 1) ^ (std::hash<std::string>()(j) << 2) ^ (std::hash<cpp2::u64>()(k) << 3); }
73+
[[nodiscard]] auto mystruct::hash() const& -> size_t{
74+
75+
size_t ret {0};
76+
ret ^= base::hash() + (ret << 6) + (ret >> 2);
77+
ret ^= std::hash<cpp2::i32>()(i) + (ret << 6) + (ret >> 2);
78+
ret ^= std::hash<std::string>()(j) + (ret << 6) + (ret >> 2);
79+
ret ^= std::hash<cpp2::u64>()(k) + (ret << 6) + (ret >> 2);
80+
return ret;
81+
}
6882
#line 12 "pure2-hashable.cpp2"
6983
auto main() -> int{
7084
mystruct x {2, "three", 4u};

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.7.4 Build 9930:1028
2+
cppfront compiler v0.7.4 Build 9930:1349
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"9930:1028"
1+
"9930:1349"

0 commit comments

Comments
 (0)