functional.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (c) 2016-2024 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PFR_DETAIL_FUNCTIONAL_HPP
  6. #define BOOST_PFR_DETAIL_FUNCTIONAL_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #include <functional>
  10. #include <cstdint>
  11. #include <boost/pfr/detail/sequence_tuple.hpp>
  12. namespace boost { namespace pfr { namespace detail {
  13. template <std::size_t I, std::size_t N>
  14. struct equal_impl {
  15. template <class T, class U>
  16. constexpr static bool cmp(const T& v1, const U& v2) noexcept {
  17. return ::boost::pfr::detail::sequence_tuple::get<I>(v1) == ::boost::pfr::detail::sequence_tuple::get<I>(v2)
  18. && equal_impl<I + 1, N>::cmp(v1, v2);
  19. }
  20. };
  21. template <std::size_t N>
  22. struct equal_impl<N, N> {
  23. template <class T, class U>
  24. constexpr static bool cmp(const T&, const U&) noexcept {
  25. return T::size_v == U::size_v;
  26. }
  27. };
  28. template <std::size_t I, std::size_t N>
  29. struct not_equal_impl {
  30. template <class T, class U>
  31. constexpr static bool cmp(const T& v1, const U& v2) noexcept {
  32. return ::boost::pfr::detail::sequence_tuple::get<I>(v1) != ::boost::pfr::detail::sequence_tuple::get<I>(v2)
  33. || not_equal_impl<I + 1, N>::cmp(v1, v2);
  34. }
  35. };
  36. template <std::size_t N>
  37. struct not_equal_impl<N, N> {
  38. template <class T, class U>
  39. constexpr static bool cmp(const T&, const U&) noexcept {
  40. return T::size_v != U::size_v;
  41. }
  42. };
  43. template <std::size_t I, std::size_t N>
  44. struct less_impl {
  45. template <class T, class U>
  46. constexpr static bool cmp(const T& v1, const U& v2) noexcept {
  47. return sequence_tuple::get<I>(v1) < sequence_tuple::get<I>(v2)
  48. || (sequence_tuple::get<I>(v1) == sequence_tuple::get<I>(v2) && less_impl<I + 1, N>::cmp(v1, v2));
  49. }
  50. };
  51. template <std::size_t N>
  52. struct less_impl<N, N> {
  53. template <class T, class U>
  54. constexpr static bool cmp(const T&, const U&) noexcept {
  55. return T::size_v < U::size_v;
  56. }
  57. };
  58. template <std::size_t I, std::size_t N>
  59. struct less_equal_impl {
  60. template <class T, class U>
  61. constexpr static bool cmp(const T& v1, const U& v2) noexcept {
  62. return sequence_tuple::get<I>(v1) < sequence_tuple::get<I>(v2)
  63. || (sequence_tuple::get<I>(v1) == sequence_tuple::get<I>(v2) && less_equal_impl<I + 1, N>::cmp(v1, v2));
  64. }
  65. };
  66. template <std::size_t N>
  67. struct less_equal_impl<N, N> {
  68. template <class T, class U>
  69. constexpr static bool cmp(const T&, const U&) noexcept {
  70. return T::size_v <= U::size_v;
  71. }
  72. };
  73. template <std::size_t I, std::size_t N>
  74. struct greater_impl {
  75. template <class T, class U>
  76. constexpr static bool cmp(const T& v1, const U& v2) noexcept {
  77. return sequence_tuple::get<I>(v1) > sequence_tuple::get<I>(v2)
  78. || (sequence_tuple::get<I>(v1) == sequence_tuple::get<I>(v2) && greater_impl<I + 1, N>::cmp(v1, v2));
  79. }
  80. };
  81. template <std::size_t N>
  82. struct greater_impl<N, N> {
  83. template <class T, class U>
  84. constexpr static bool cmp(const T&, const U&) noexcept {
  85. return T::size_v > U::size_v;
  86. }
  87. };
  88. template <std::size_t I, std::size_t N>
  89. struct greater_equal_impl {
  90. template <class T, class U>
  91. constexpr static bool cmp(const T& v1, const U& v2) noexcept {
  92. return sequence_tuple::get<I>(v1) > sequence_tuple::get<I>(v2)
  93. || (sequence_tuple::get<I>(v1) == sequence_tuple::get<I>(v2) && greater_equal_impl<I + 1, N>::cmp(v1, v2));
  94. }
  95. };
  96. template <std::size_t N>
  97. struct greater_equal_impl<N, N> {
  98. template <class T, class U>
  99. constexpr static bool cmp(const T&, const U&) noexcept {
  100. return T::size_v >= U::size_v;
  101. }
  102. };
  103. // Hash combine functions copied from Boost.ContainerHash
  104. // https://github.com/boostorg/container_hash/blob/171c012d4723c5e93cc7cffe42919afdf8b27dfa/include/boost/container_hash/hash.hpp#L311
  105. // that is based on Peter Dimov's proposal
  106. // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf
  107. // issue 6.18.
  108. //
  109. // This also contains public domain code from MurmurHash. From the
  110. // MurmurHash header:
  111. //
  112. // MurmurHash3 was written by Austin Appleby, and is placed in the public
  113. // domain. The author hereby disclaims copyright to this source code.
  114. template <typename SizeT>
  115. constexpr void hash_combine(SizeT& seed, SizeT value) noexcept {
  116. seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
  117. }
  118. constexpr auto rotl(std::uint32_t x, std::uint32_t r) noexcept {
  119. return (x << r) | (x >> (32 - r));
  120. }
  121. constexpr void hash_combine(std::uint32_t& h1, std::uint32_t k1) noexcept {
  122. const std::uint32_t c1 = 0xcc9e2d51;
  123. const std::uint32_t c2 = 0x1b873593;
  124. k1 *= c1;
  125. k1 = detail::rotl(k1,15);
  126. k1 *= c2;
  127. h1 ^= k1;
  128. h1 = detail::rotl(h1,13);
  129. h1 = h1*5+0xe6546b64;
  130. }
  131. #if defined(INT64_MIN) && defined(UINT64_MAX)
  132. constexpr void hash_combine(std::uint64_t& h, std::uint64_t k) noexcept {
  133. const std::uint64_t m = 0xc6a4a7935bd1e995ULL;
  134. const int r = 47;
  135. k *= m;
  136. k ^= k >> r;
  137. k *= m;
  138. h ^= k;
  139. h *= m;
  140. // Completely arbitrary number, to prevent 0's
  141. // from hashing to 0.
  142. h += 0xe6546b64;
  143. }
  144. #endif
  145. template <typename T>
  146. auto compute_hash(const T& value, long /*priority*/)
  147. -> decltype(std::hash<T>()(value))
  148. {
  149. return std::hash<T>()(value);
  150. }
  151. template <typename T>
  152. std::size_t compute_hash(const T& /*value*/, int /*priority*/) {
  153. static_assert(sizeof(T) && false, "====================> Boost.PFR: std::hash not specialized for type T");
  154. return 0;
  155. }
  156. template <std::size_t I, std::size_t N>
  157. struct hash_impl {
  158. template <class T>
  159. constexpr static std::size_t compute(const T& val) noexcept {
  160. std::size_t h = detail::compute_hash( ::boost::pfr::detail::sequence_tuple::get<I>(val), 1L );
  161. detail::hash_combine(h, hash_impl<I + 1, N>::compute(val) );
  162. return h;
  163. }
  164. };
  165. template <std::size_t N>
  166. struct hash_impl<N, N> {
  167. template <class T>
  168. constexpr static std::size_t compute(const T&) noexcept {
  169. return 0;
  170. }
  171. };
  172. ///////////////////// Define min_element and to avoid inclusion of <algorithm>
  173. constexpr std::size_t min_size(std::size_t x, std::size_t y) noexcept {
  174. return x < y ? x : y;
  175. }
  176. template <template <std::size_t, std::size_t> class Visitor, class T, class U>
  177. constexpr bool binary_visit(const T& x, const U& y) {
  178. constexpr std::size_t fields_count_lhs = detail::fields_count<std::remove_reference_t<T>>();
  179. constexpr std::size_t fields_count_rhs = detail::fields_count<std::remove_reference_t<U>>();
  180. constexpr std::size_t fields_count_min = detail::min_size(fields_count_lhs, fields_count_rhs);
  181. typedef Visitor<0, fields_count_min> visitor_t;
  182. #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
  183. return visitor_t::cmp(detail::tie_as_tuple(x), detail::tie_as_tuple(y));
  184. #else
  185. bool result = true;
  186. ::boost::pfr::detail::for_each_field_dispatcher(
  187. x,
  188. [&result, &y](const auto& lhs) {
  189. constexpr std::size_t fields_count_rhs_ = detail::fields_count<std::remove_reference_t<U>>();
  190. ::boost::pfr::detail::for_each_field_dispatcher(
  191. y,
  192. [&result, &lhs](const auto& rhs) {
  193. result = visitor_t::cmp(lhs, rhs);
  194. },
  195. detail::make_index_sequence<fields_count_rhs_>{}
  196. );
  197. },
  198. detail::make_index_sequence<fields_count_lhs>{}
  199. );
  200. return result;
  201. #endif
  202. }
  203. }}} // namespace boost::pfr::detail
  204. #endif // BOOST_PFR_DETAIL_FUNCTIONAL_HPP