From 8b4297b1db387a6109a4450ef3eadc6462ad3ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sun, 31 Dec 2023 23:45:45 +0100 Subject: [PATCH] Use custom hash functions as Boost.ContainerHash requires C++11. --- example/doc_assoc_optimized_code.cpp | 13 +++++++--- example/doc_bucket_traits.cpp | 5 ++-- example/doc_iterator_from_value.cpp | 5 ++-- example/doc_unordered_set.cpp | 1 - test/any_test.cpp | 4 +-- test/custom_bucket_traits_test.cpp | 3 +-- test/default_hook_test.cpp | 2 +- test/function_hook_test.cpp | 1 - test/int_holder.hpp | 7 +---- test/itestvalue.hpp | 39 +--------------------------- test/make_functions_test.cpp | 2 +- test/scary_iterators_test.cpp | 2 +- test/stateful_value_traits_test.cpp | 3 +-- 13 files changed, 25 insertions(+), 62 deletions(-) diff --git a/example/doc_assoc_optimized_code.cpp b/example/doc_assoc_optimized_code.cpp index 530ad79..3a596cd 100644 --- a/example/doc_assoc_optimized_code.cpp +++ b/example/doc_assoc_optimized_code.cpp @@ -13,20 +13,27 @@ #include #include #include - -using namespace boost::intrusive; +#include // Hash function for strings struct StrHasher { std::size_t operator()(const char *str) const { + //Simple example, use favorite hash function (like boost::hash_combine) std::size_t seed = 0; - for(; *str; ++str) boost::hash_combine(seed, *str); + for(; *str; ++str){ + std::size_t x = std::size_t(*str); + x = ((x >> 16) ^ x) * 0x45d9f3b; + x = (x >> 16) ^ x; + seed += x; + } return seed; } }; +using namespace boost::intrusive; + class Expensive : public set_base_hook<>, public unordered_set_base_hook<> { std::string key_; diff --git a/example/doc_bucket_traits.cpp b/example/doc_bucket_traits.cpp index 9494ed9..235537e 100644 --- a/example/doc_bucket_traits.cpp +++ b/example/doc_bucket_traits.cpp @@ -11,7 +11,6 @@ ///////////////////////////////////////////////////////////////////////////// //[doc_bucket_traits #include -#include #include using namespace boost::intrusive; @@ -28,7 +27,9 @@ class MyClass : public unordered_set_base_hook<> friend bool operator==(const MyClass &l, const MyClass &r) { return l.int_ == r.int_; } friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } + { //Use your favorite hash function, like boost::hash or std::hash + return std::size_t(v.int_); + } }; //Define the base hook option diff --git a/example/doc_iterator_from_value.cpp b/example/doc_iterator_from_value.cpp index c33d7dd..bfe2e55 100644 --- a/example/doc_iterator_from_value.cpp +++ b/example/doc_iterator_from_value.cpp @@ -12,7 +12,6 @@ //[doc_iterator_from_value #include #include -#include #include using namespace boost::intrusive; @@ -39,7 +38,9 @@ class intrusive_data //The hash function friend std::size_t hash_value(const intrusive_data &i) - { return boost::hash()(i.data_id_); } + { //Use your favorite hash function, like boost::hash or std::hash + return std::size_t(i.data_id_); + } }; //Definition of the intrusive list that will hold intrusive_data diff --git a/example/doc_unordered_set.cpp b/example/doc_unordered_set.cpp index e793f87..12e49fc 100644 --- a/example/doc_unordered_set.cpp +++ b/example/doc_unordered_set.cpp @@ -13,7 +13,6 @@ #include #include #include -#include using namespace boost::intrusive; diff --git a/test/any_test.cpp b/test/any_test.cpp index 7552899..4b3fd76 100644 --- a/test/any_test.cpp +++ b/test/any_test.cpp @@ -19,7 +19,7 @@ #include #include #include -#include + #include //std::vector #include //std::size_t @@ -47,7 +47,7 @@ class MyClass : public any_base_hook<> { return l.int_ == r.int_; } friend std::size_t hash_value(const MyClass &o) - { return boost::hash()(o.get()); } + { return std::size_t(o.get()); } friend bool priority_order(const MyClass &a, const MyClass &b) { return a.int_ < b.int_; } diff --git a/test/custom_bucket_traits_test.cpp b/test/custom_bucket_traits_test.cpp index 84968b2..5dddaec 100644 --- a/test/custom_bucket_traits_test.cpp +++ b/test/custom_bucket_traits_test.cpp @@ -11,7 +11,6 @@ ///////////////////////////////////////////////////////////////////////////// #include #include -#include #include #include @@ -30,7 +29,7 @@ class MyClass : public unordered_set_base_hook<> { return l.int_ == r.int_; } friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } + { return std::size_t(v.int_); } }; struct uset_value_traits diff --git a/test/default_hook_test.cpp b/test/default_hook_test.cpp index a8efa99..19a1f55 100644 --- a/test/default_hook_test.cpp +++ b/test/default_hook_test.cpp @@ -53,7 +53,7 @@ class MyClass { return l.int_ == r.int_; } friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } + { return std::size_t(v.int_); } friend bool priority_order(const MyClass &l, const MyClass &r) { return l.int_ < r.int_; } diff --git a/test/function_hook_test.cpp b/test/function_hook_test.cpp index 8b1c6ac..c8bfdfd 100644 --- a/test/function_hook_test.cpp +++ b/test/function_hook_test.cpp @@ -14,7 +14,6 @@ #include #include #include -#include using namespace boost::intrusive; diff --git a/test/int_holder.hpp b/test/int_holder.hpp index 8f276c9..38a1ffc 100644 --- a/test/int_holder.hpp +++ b/test/int_holder.hpp @@ -12,8 +12,6 @@ #ifndef BOOST_INTRUSIVE_DETAIL_INT_HOLDER_HPP #define BOOST_INTRUSIVE_DETAIL_INT_HOLDER_HPP -#include - //GCC has some false array_bounds warnings starting in GCC 12 #if defined(BOOST_CLANG) || (defined(BOOST_GCC) && (BOOST_GCC >= 120000)) #pragma GCC diagnostic push @@ -88,10 +86,7 @@ struct int_holder { return int_ != i; } friend std::size_t hash_value(const int_holder &t) - { - boost::hash hasher; - return hasher((&t)->int_value()); - } + { return std::size_t((&t)->int_value()); } int int_; }; diff --git a/test/itestvalue.hpp b/test/itestvalue.hpp index c9e39e4..cdd59ce 100644 --- a/test/itestvalue.hpp +++ b/test/itestvalue.hpp @@ -15,7 +15,6 @@ #include #include -#include #include #include "nonhook_node.hpp" #include "int_holder.hpp" @@ -127,33 +126,8 @@ struct testvalue { return other1.value_.int_ != other2; } friend std::size_t hash_value(const testvalue&t) - { - boost::hash hasher; - return hasher((&t)->int_value()); - } - /* - static std::size_t priority_hash(const testvalue &t) - { return boost::hash()((&t)->int_value()); } + { return hash_value(t.value_); } - static std::size_t priority_hash(int i) - { return boost::hash()(i); } - - template - static bool priority_order_impl(const T& t1, const U& t2) - { - std::size_t hash1 = (priority_hash)(t1); - boost::hash_combine(hash1, -hash1); - std::size_t hash2 = (priority_hash)(t2); - boost::hash_combine(hash2, -hash2); - return hash1 < hash2; - } - - friend bool priority_order(const testvalue &t1, int t2) - { return (priority_order_impl)(t1, t2); } - - friend bool priority_order(int t1, const testvalue &t2) - { return (priority_order_impl)(t1, t2); } -*/ template < typename Node_Algorithms > friend void swap_nodes(testvalue& lhs, testvalue& rhs) { lhs.swap_nodes(rhs); } @@ -163,17 +137,6 @@ struct testvalue { return s << t.value_.int_value(); } }; -/* -bool priority_order(int t1, int t2) -{ - std::size_t hash1 = boost::hash()(t1); - boost::hash_combine(hash1, &t1); - std::size_t hash2 = boost::hash()(t2); - boost::hash_combine(hash2, &t2); - return hash1 < hash2; -} -*/ - struct even_odd { template < typename key_type_1, typename key_type_2 > diff --git a/test/make_functions_test.cpp b/test/make_functions_test.cpp index 8d0fafb..9907832 100644 --- a/test/make_functions_test.cpp +++ b/test/make_functions_test.cpp @@ -71,7 +71,7 @@ class MyClass { return l.int_ == r.int_; } friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } + { return std::size_t(v.int_); } friend bool priority_order(const MyClass &l, const MyClass &r) { return l.int_ < r.int_; } diff --git a/test/scary_iterators_test.cpp b/test/scary_iterators_test.cpp index 6c4b426..69cd8b1 100644 --- a/test/scary_iterators_test.cpp +++ b/test/scary_iterators_test.cpp @@ -55,7 +55,7 @@ class MyClass { return l.int_ == r.int_; } friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } + { return std::size_t(v.int_); } friend bool priority_order(const MyClass &l, const MyClass &r) { return l.int_ < r.int_; } diff --git a/test/stateful_value_traits_test.cpp b/test/stateful_value_traits_test.cpp index a9e8bfb..b5d974d 100644 --- a/test/stateful_value_traits_test.cpp +++ b/test/stateful_value_traits_test.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -35,7 +34,7 @@ class MyClass { return l.int_ == r.int_; } friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } + { return std::size_t(v.int_); } }; template