default_hook_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/list.hpp>
  13. #include <boost/intrusive/slist.hpp>
  14. #include <boost/intrusive/set.hpp>
  15. #include <boost/intrusive/unordered_set.hpp>
  16. #include <boost/intrusive/splay_set.hpp>
  17. #include <boost/intrusive/avl_set.hpp>
  18. #include <boost/intrusive/sg_set.hpp>
  19. #include <boost/intrusive/treap_set.hpp>
  20. #include <boost/intrusive/bs_set.hpp>
  21. #include <boost/intrusive/pointer_traits.hpp>
  22. #include "smart_ptr.hpp"
  23. #include <vector>
  24. using namespace boost::intrusive;
  25. class MyClass
  26. : public list_base_hook
  27. < void_pointer<smart_ptr<void> >, link_mode<safe_link> >
  28. , public slist_base_hook
  29. < void_pointer<smart_ptr<void> >, link_mode<safe_link> >
  30. , public set_base_hook
  31. < void_pointer<smart_ptr<void> >, link_mode<safe_link> >
  32. , public unordered_set_base_hook
  33. < void_pointer<smart_ptr<void> >, link_mode<safe_link> >
  34. , public avl_set_base_hook
  35. < void_pointer<smart_ptr<void> >, link_mode<safe_link> >
  36. , public bs_set_base_hook
  37. < void_pointer<smart_ptr<void> >, link_mode<safe_link> >
  38. {
  39. int int_;
  40. public:
  41. MyClass(int i)
  42. : int_(i)
  43. {}
  44. friend bool operator<(const MyClass &l, const MyClass &r)
  45. { return l.int_ < r.int_; }
  46. friend bool operator==(const MyClass &l, const MyClass &r)
  47. { return l.int_ == r.int_; }
  48. friend std::size_t hash_value(const MyClass &v)
  49. { return boost::hash_value(v.int_); }
  50. friend bool priority_order(const MyClass &l, const MyClass &r)
  51. { return l.int_ < r.int_; }
  52. };
  53. //Define a list that will store MyClass using the public base hook
  54. typedef list<MyClass> List;
  55. typedef slist<MyClass> Slist;
  56. typedef set<MyClass> Set;
  57. typedef unordered_set<MyClass> USet;
  58. typedef avl_set<MyClass> AvlSet;
  59. typedef splay_set<MyClass> SplaySet;
  60. typedef treap_set<MyClass> TreapSet;
  61. typedef sg_set<MyClass> SgSet;
  62. typedef bs_set<MyClass> BsSet;
  63. int main()
  64. {
  65. typedef std::vector<MyClass>::iterator VectIt;
  66. typedef std::vector<MyClass>::reverse_iterator VectRit;
  67. //Create several MyClass objects, each one with a different value
  68. std::vector<MyClass> values;
  69. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
  70. {
  71. List my_list;
  72. Slist my_slist;
  73. Set my_set;
  74. USet::bucket_type buckets[100];
  75. USet my_uset(USet::bucket_traits(pointer_traits<USet::bucket_ptr>::pointer_to(*buckets), 100));
  76. AvlSet my_avlset;
  77. SplaySet my_splayset;
  78. //Now insert them in the reverse order
  79. //in the base hook intrusive list
  80. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
  81. my_list.push_front(*it);
  82. my_slist.push_front(*it);
  83. my_set.insert(*it);
  84. my_uset.insert(*it);
  85. my_avlset.insert(*it);
  86. my_splayset.insert(*it);
  87. }
  88. //Now test lists
  89. {
  90. List::const_iterator list_it(my_list.cbegin());
  91. Slist::const_iterator slist_it(my_slist.cbegin());
  92. Set::const_reverse_iterator set_rit(my_set.crbegin());
  93. AvlSet::const_reverse_iterator avl_set_rit(my_avlset.crbegin());
  94. SplaySet::const_reverse_iterator splay_set_rit(my_splayset.crbegin());
  95. VectRit vect_it(values.rbegin()), vect_itend(values.rend());
  96. //Test the objects inserted in the base hook list
  97. for(; vect_it != vect_itend
  98. ; ++vect_it, ++list_it
  99. , ++slist_it, ++set_rit
  100. , ++avl_set_rit
  101. , ++splay_set_rit
  102. ){
  103. if(&*list_it != &*vect_it) return 1;
  104. if(&*slist_it != &*vect_it) return 1;
  105. if(&*set_rit != &*vect_it) return 1;
  106. if(&*avl_set_rit != &*vect_it) return 1;
  107. if(&*splay_set_rit != &*vect_it)return 1;
  108. if(my_uset.find(*set_rit) == my_uset.cend()) return 1;
  109. }
  110. }
  111. }
  112. //Since treap_set, sg_set & bs_set reuse the hook, treat them apart
  113. {
  114. TreapSet my_treapset;
  115. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
  116. my_treapset.insert(*it);
  117. }
  118. TreapSet::const_reverse_iterator treap_set_rit(my_treapset.crbegin());
  119. VectRit vect_it(values.rbegin()), vect_itend(values.rend());
  120. for(; vect_it != vect_itend; ++vect_it, ++treap_set_rit){
  121. if(&*treap_set_rit != &*vect_it) return 1;
  122. }
  123. }
  124. {
  125. SgSet my_sgset;
  126. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
  127. my_sgset.insert(*it);
  128. }
  129. SgSet::const_reverse_iterator sg_set_rit(my_sgset.crbegin());
  130. VectRit vect_it(values.rbegin()), vect_itend(values.rend());
  131. for(; vect_it != vect_itend; ++vect_it, ++sg_set_rit){
  132. if(&*sg_set_rit != &*vect_it) return 1;
  133. }
  134. }
  135. {
  136. BsSet my_bsset;
  137. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
  138. my_bsset.insert(*it);
  139. }
  140. BsSet::const_reverse_iterator bs_set_rit(my_bsset.crbegin());
  141. VectRit vect_it(values.rbegin()), vect_itend(values.rend());
  142. for(; vect_it != vect_itend; ++vect_it, ++bs_set_rit){
  143. if(&*bs_set_rit != &*vect_it) return 1;
  144. }
  145. }
  146. return 0;
  147. }