make_functions_test.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/avl_set.hpp>
  17. #include <boost/intrusive/sg_set.hpp>
  18. #include <boost/intrusive/splay_set.hpp>
  19. #include <boost/intrusive/bs_set.hpp>
  20. #include <boost/intrusive/treap_set.hpp>
  21. #include <boost/intrusive/detail/mpl.hpp>
  22. #include <boost/intrusive/pointer_traits.hpp>
  23. #include <boost/static_assert.hpp>
  24. #include "smart_ptr.hpp"
  25. #include <vector>
  26. using namespace boost::intrusive;
  27. struct my_tag;
  28. struct my_tag2;
  29. struct my_tag3;
  30. typedef make_bs_set_base_hook
  31. < void_pointer<smart_ptr<void> >, link_mode<normal_link>
  32. , tag<my_tag> >::type TreapHook;
  33. typedef make_bs_set_base_hook
  34. < void_pointer<smart_ptr<void> >, link_mode<normal_link>
  35. , tag<my_tag2> >::type SplayHook;
  36. typedef make_bs_set_base_hook
  37. < void_pointer<smart_ptr<void> >, link_mode<normal_link>
  38. , tag<my_tag3> >::type BsHook;
  39. class MyClass
  40. : public make_list_base_hook
  41. < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
  42. , public make_slist_base_hook
  43. < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
  44. , public make_set_base_hook
  45. < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
  46. , public make_unordered_set_base_hook
  47. < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
  48. , public make_avl_set_base_hook
  49. < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
  50. , public make_bs_set_base_hook
  51. < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
  52. , public TreapHook
  53. , public SplayHook
  54. , public BsHook
  55. {
  56. int int_;
  57. public:
  58. MyClass(int i)
  59. : int_(i)
  60. {}
  61. friend bool operator<(const MyClass &l, const MyClass &r)
  62. { return l.int_ < r.int_; }
  63. friend bool operator==(const MyClass &l, const MyClass &r)
  64. { return l.int_ == r.int_; }
  65. friend std::size_t hash_value(const MyClass &v)
  66. { return boost::hash_value(v.int_); }
  67. friend bool priority_order(const MyClass &l, const MyClass &r)
  68. { return l.int_ < r.int_; }
  69. };
  70. //Define a list that will store MyClass using the public base hook
  71. typedef make_list<MyClass>::type List;
  72. typedef make_slist<MyClass>::type Slist;
  73. typedef make_set<MyClass>::type Set;
  74. typedef make_unordered_set<MyClass>::type USet;
  75. typedef make_avl_set<MyClass>::type AvlSet;
  76. typedef make_sg_set<MyClass>::type SgSet;
  77. typedef make_treap_set<MyClass
  78. , base_hook<TreapHook> >::type TreapSet;
  79. typedef make_splay_set<MyClass
  80. , base_hook<SplayHook> >::type SplaySet;
  81. typedef make_bs_set<MyClass
  82. , base_hook<BsHook> >::type BsSet;
  83. int main()
  84. {
  85. typedef std::vector<MyClass>::iterator VectIt;
  86. typedef std::vector<MyClass>::reverse_iterator VectRit;
  87. //Create several MyClass objects, each one with a different value
  88. std::vector<MyClass> values;
  89. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
  90. USet::bucket_type buckets[100];
  91. List my_list;
  92. Slist my_slist;
  93. Set my_set;
  94. USet my_uset(USet::bucket_traits
  95. (pointer_traits<USet::bucket_ptr>::pointer_to(buckets[0]), 100));
  96. AvlSet my_avlset;
  97. SplaySet my_splayset;
  98. BsSet my_bsset;
  99. SgSet my_sgset;
  100. TreapSet my_treapset;
  101. //Now insert them in containers
  102. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
  103. my_list.push_front(*it);
  104. my_slist.push_front(*it);
  105. my_set.insert(*it);
  106. my_uset.insert(*it);
  107. my_avlset.insert(*it);
  108. my_splayset.insert(*it);
  109. my_bsset.insert(*it);
  110. my_sgset.insert(*it);
  111. my_treapset.insert(*it);
  112. }
  113. //Now test lists
  114. {
  115. List::const_iterator list_it(my_list.cbegin());
  116. Slist::const_iterator slist_it(my_slist.cbegin());
  117. Set::const_reverse_iterator set_rit(my_set.crbegin());
  118. AvlSet::const_reverse_iterator avlset_rit(my_avlset.crbegin());
  119. SplaySet::const_reverse_iterator splayset_rit(my_splayset.crbegin());
  120. BsSet::const_reverse_iterator bsset_rit(my_bsset.crbegin());
  121. SgSet::const_reverse_iterator sgset_rit(my_sgset.crbegin());
  122. TreapSet::const_reverse_iterator treapset_rit(my_treapset.crbegin());
  123. VectRit vect_it(values.rbegin()), vect_itend(values.rend());
  124. //Test the objects inserted in the base hook list
  125. for( ; vect_it != vect_itend
  126. ; ++vect_it, ++list_it, ++slist_it, ++set_rit
  127. , ++avlset_rit, ++splayset_rit, ++bsset_rit, ++sgset_rit, ++treapset_rit
  128. ){
  129. if(&*list_it != &*vect_it) return 1;
  130. if(&*slist_it != &*vect_it) return 1;
  131. if(&*set_rit != &*vect_it) return 1;
  132. if(my_uset.find(*set_rit) == my_uset.cend()) return 1;
  133. if(&*avlset_rit != &*vect_it) return 1;
  134. if(&*splayset_rit != &*vect_it) return 1;
  135. if(&*bsset_rit != &*vect_it) return 1;
  136. if(&*sgset_rit != &*vect_it) return 1;
  137. if(&*treapset_rit != &*vect_it) return 1;
  138. }
  139. }
  140. //Check defined types and implicitly defined types are equal
  141. BOOST_STATIC_ASSERT((detail::is_same<make_list_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
  142. ,make_list_base_hook<>::type
  143. >::value));
  144. BOOST_STATIC_ASSERT((detail::is_same<make_slist_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
  145. ,make_slist_base_hook<>::type
  146. >::value));
  147. BOOST_STATIC_ASSERT((detail::is_same<make_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
  148. ,make_set_base_hook<>::type
  149. >::value));
  150. BOOST_STATIC_ASSERT((detail::is_same<make_unordered_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
  151. ,make_unordered_set_base_hook<>::type
  152. >::value));
  153. BOOST_STATIC_ASSERT((detail::is_same<make_avl_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
  154. ,make_avl_set_base_hook<>::type
  155. >::value));
  156. BOOST_STATIC_ASSERT((detail::is_same<make_bs_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
  157. ,make_bs_set_base_hook<>::type
  158. >::value));
  159. //Check defined types and implicitly defined types are unequal
  160. BOOST_STATIC_ASSERT(!(detail::is_same<make_list_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
  161. ,make_list_base_hook<>::type
  162. >::value));
  163. BOOST_STATIC_ASSERT(!(detail::is_same<make_slist_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
  164. ,make_slist_base_hook<>::type
  165. >::value));
  166. BOOST_STATIC_ASSERT(!(detail::is_same<make_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
  167. ,make_set_base_hook<>::type
  168. >::value));
  169. BOOST_STATIC_ASSERT(!(detail::is_same<make_unordered_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
  170. ,make_unordered_set_base_hook<>::type
  171. >::value));
  172. BOOST_STATIC_ASSERT(!(detail::is_same<make_avl_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
  173. ,make_avl_set_base_hook<>::type
  174. >::value));
  175. BOOST_STATIC_ASSERT(!(detail::is_same<make_bs_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
  176. ,make_bs_set_base_hook<>::type
  177. >::value));
  178. return 0;
  179. }