custom_bucket_traits_test.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #include <boost/intrusive/unordered_set.hpp>
  13. #include <boost/intrusive/detail/mpl.hpp>
  14. #include <boost/functional/hash.hpp>
  15. #include <boost/static_assert.hpp>
  16. #include <vector>
  17. using namespace boost::intrusive;
  18. class MyClass : public unordered_set_base_hook<>
  19. {
  20. int int_;
  21. public:
  22. MyClass(int i = 0) : int_(i)
  23. {}
  24. unordered_set_member_hook<> member_hook_;
  25. friend bool operator==(const MyClass &l, const MyClass &r)
  26. { return l.int_ == r.int_; }
  27. friend std::size_t hash_value(const MyClass &v)
  28. { return boost::hash_value(v.int_); }
  29. };
  30. struct uset_value_traits
  31. {
  32. typedef slist_node_traits<void*> node_traits;
  33. typedef node_traits::node_ptr node_ptr;
  34. typedef node_traits::const_node_ptr const_node_ptr;
  35. typedef MyClass value_type;
  36. typedef MyClass * pointer;
  37. typedef const MyClass * const_pointer;
  38. static const link_mode_type link_mode = normal_link;
  39. static node_ptr to_node_ptr (value_type &value)
  40. { return node_ptr(&value); }
  41. static const_node_ptr to_node_ptr (const value_type &value)
  42. { return const_node_ptr(&value); }
  43. static pointer to_value_ptr(node_ptr n)
  44. { return static_cast<value_type*>(n); }
  45. static const_pointer to_value_ptr(const_node_ptr n)
  46. { return static_cast<const value_type*>(n); }
  47. };
  48. //Base
  49. typedef base_hook< unordered_set_base_hook<> > BaseHook;
  50. typedef unordered_bucket<BaseHook>::type BaseBucketType;
  51. typedef unordered_bucket_ptr<BaseHook>::type BaseBucketPtrType;
  52. typedef unordered_set<MyClass, BaseHook> BaseUset;
  53. //Member
  54. typedef member_hook
  55. < MyClass, unordered_set_member_hook<>
  56. , &MyClass::member_hook_ > MemberHook;
  57. typedef unordered_bucket<MemberHook>::type MemberBucketType;
  58. typedef unordered_bucket_ptr<MemberHook>::type MemberBucketPtrType;
  59. typedef unordered_set<MyClass, MemberHook> MemberUset;
  60. //Explicit
  61. typedef value_traits< uset_value_traits > Traits;
  62. typedef unordered_bucket<Traits>::type TraitsBucketType;
  63. typedef unordered_bucket_ptr<Traits>::type TraitsBucketPtrType;
  64. typedef unordered_set<MyClass, Traits> TraitsUset;
  65. struct uset_bucket_traits
  66. {
  67. //Power of two bucket length
  68. static const std::size_t NumBuckets = 128;
  69. uset_bucket_traits(BaseBucketType *buckets)
  70. : buckets_(buckets)
  71. {}
  72. uset_bucket_traits(const uset_bucket_traits &other)
  73. : buckets_(other.buckets_)
  74. {}
  75. BaseBucketType * bucket_begin() const
  76. { return buckets_; }
  77. std::size_t bucket_count() const
  78. { return NumBuckets; }
  79. BaseBucketType *buckets_;
  80. };
  81. typedef unordered_set
  82. <MyClass, bucket_traits<uset_bucket_traits>, power_2_buckets<true> >
  83. BucketTraitsUset;
  84. int main()
  85. {
  86. BOOST_STATIC_ASSERT((detail::is_same<BaseUset::bucket_type, BaseBucketType>::value));
  87. BOOST_STATIC_ASSERT((detail::is_same<MemberUset::bucket_type, MemberBucketType>::value));
  88. BOOST_STATIC_ASSERT((detail::is_same<TraitsUset::bucket_type, TraitsBucketType>::value));
  89. BOOST_STATIC_ASSERT((detail::is_same<BaseBucketType, MemberBucketType>::value));
  90. BOOST_STATIC_ASSERT((detail::is_same<BaseBucketType, TraitsBucketType>::value));
  91. BOOST_STATIC_ASSERT((detail::is_same<BaseBucketPtrType, TraitsBucketPtrType>::value));
  92. BOOST_STATIC_ASSERT((detail::is_same<BaseBucketPtrType, MemberBucketPtrType>::value));
  93. BOOST_STATIC_ASSERT((detail::is_same<BaseBucketPtrType, BaseBucketType*>::value));
  94. typedef std::vector<MyClass>::iterator VectIt;
  95. typedef std::vector<MyClass>::reverse_iterator VectRit;
  96. std::vector<MyClass> values;
  97. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
  98. BaseBucketType buckets[uset_bucket_traits::NumBuckets];
  99. uset_bucket_traits btraits(buckets);
  100. BucketTraitsUset uset(btraits);
  101. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
  102. uset.insert(*it);
  103. for( VectRit it(values.rbegin()), itend(values.rend()); it != itend; ++it){
  104. if(uset.find(*it) == uset.cend()) return 1;
  105. }
  106. return 0;
  107. }