Skip to content

Commit

Permalink
Improve hash combination
Browse files Browse the repository at this point in the history
  • Loading branch information
hsutter committed Sep 30, 2024
1 parent 4641ad2 commit baa6954
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 431 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10166087914140344613
6440649391998984779
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ pure2-hashable.cpp2:1:7: note: candidates are: ‘constexpr base& base::operator
pure2-hashable.cpp2:1:7: note: ‘constexpr base& base::operator=(const base&)’
pure2-hashable.cpp2:6:14: note: ‘template<class auto:97> base& base::operator=(auto:97&&)’
pure2-hashable.cpp2:1:7: note: ‘class base’ defined here
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&>)’
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&>)’
pure2-hashable.cpp2:5:7: note: candidates are: ‘mystruct::mystruct(mystruct&&)’
pure2-hashable.cpp2:5:7: note: ‘mystruct::mystruct(const mystruct&)’
pure2-hashable.cpp2:10:13: note: ‘template<class auto:98, class auto:99, class auto:100> mystruct::mystruct(auto:98&&, auto:99&&, auto:100&&)’
pure2-hashable.cpp2:5:7: note: ‘class mystruct’ defined here
pure2-hashable.cpp2:14:104: error: mixing declarations and function-definitions is forbidden
pure2-hashable.cpp2:14:107: error: expected constructor, destructor, or type conversion before ‘{’ token
pure2-hashable.cpp2:15:104: error: expected unqualified-id before ‘,’ token
pure2-hashable.cpp2:15:107: error: expected constructor, destructor, or type conversion before ‘{’ token
pure2-hashable.cpp2:16:104: error: expected unqualified-id before ‘,’ token
pure2-hashable.cpp2:16:107: error: expected constructor, destructor, or type conversion before ‘{’ token
pure2-hashable.cpp2:16:127: error: expected unqualified-id before ‘{’ token
pure2-hashable.cpp2:20:104: error: mixing declarations and function-definitions is forbidden
pure2-hashable.cpp2:20:107: error: expected constructor, destructor, or type conversion before ‘{’ token
pure2-hashable.cpp2:21:104: error: expected unqualified-id before ‘,’ token
pure2-hashable.cpp2:21:107: error: expected constructor, destructor, or type conversion before ‘{’ token
pure2-hashable.cpp2:22:104: error: expected unqualified-id before ‘,’ token
pure2-hashable.cpp2:22:107: error: expected constructor, destructor, or type conversion before ‘{’ token
pure2-hashable.cpp2:22:127: error: expected unqualified-id before ‘{’ token
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10166087914140344613
6440649391998984779
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13273923326899311054
13958440649017415620
18 changes: 16 additions & 2 deletions regression-tests/test-results/pure2-hashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,28 @@ auto base::operator=(auto&& h_) -> base&
requires (std::is_convertible_v<CPP2_TYPEOF(h_), std::add_const_t<cpp2::i32>&>) {
h = CPP2_FORWARD(h_);
return *this;}
[[nodiscard]] auto base::hash() const& -> size_t { return std::hash<cpp2::i32>()(h); }
[[nodiscard]] auto base::hash() const& -> size_t{

size_t ret {0};
ret ^= std::hash<cpp2::i32>()(h) + (ret << 6) + (ret >> 2);
return ret;
}

mystruct::mystruct(auto&& i_, auto&& j_, auto&& k_)
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>&>)
: base{ (1) }
, i{ CPP2_FORWARD(i_) }
, j{ CPP2_FORWARD(j_) }
, k{ CPP2_FORWARD(k_) }{}
[[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); }
[[nodiscard]] auto mystruct::hash() const& -> size_t{

size_t ret {0};
ret ^= base::hash() + (ret << 6) + (ret >> 2);
ret ^= std::hash<cpp2::i32>()(i) + (ret << 6) + (ret >> 2);
ret ^= std::hash<std::string>()(j) + (ret << 6) + (ret >> 2);
ret ^= std::hash<cpp2::u64>()(k) + (ret << 6) + (ret >> 2);
return ret;
}
#line 12 "pure2-hashable.cpp2"
auto main() -> int{
mystruct x {2, "three", 4u};
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.7.4 Build 9930:1028
cppfront compiler v0.7.4 Build 9930:1349
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"9930:1028"
"9930:1349"
Loading

0 comments on commit baa6954

Please sign in to comment.