1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2013
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
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/treap_set.hpp>
20 #include <boost/intrusive/detail/mpl.hpp>
21 #include <boost/intrusive/pointer_traits.hpp>
22 #include <boost/static_assert.hpp>
23 #include "smart_ptr.hpp"
26 using namespace boost::intrusive;
31 typedef make_bs_set_base_hook
32 < void_pointer<smart_ptr<void> >, link_mode<normal_link>
33 , tag<my_tag> >::type TreapHook;
34 typedef make_bs_set_base_hook
35 < void_pointer<smart_ptr<void> >, link_mode<normal_link>
36 , tag<my_tag2> >::type SplayHook;
39 : public make_list_base_hook
40 < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
41 , public make_slist_base_hook
42 < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
43 , public make_set_base_hook
44 < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
45 , public make_unordered_set_base_hook
46 < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
47 , public make_avl_set_base_hook
48 < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
49 , public make_bs_set_base_hook
50 < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
61 friend bool operator<(const MyClass &l, const MyClass &r)
62 { return l.int_ < r.int_; }
64 friend bool operator==(const MyClass &l, const MyClass &r)
65 { return l.int_ == r.int_; }
67 friend std::size_t hash_value(const MyClass &v)
68 { return boost::hash_value(v.int_); }
70 friend bool priority_order(const MyClass &l, const MyClass &r)
71 { return l.int_ < r.int_; }
74 //Define a list that will store MyClass using the public base hook
75 typedef make_list<MyClass>::type List;
76 typedef make_slist<MyClass>::type Slist;
77 typedef make_set<MyClass>::type Set;
78 typedef make_unordered_set<MyClass>::type USet;
80 typedef make_avl_set<MyClass>::type AvlSet;
81 typedef make_sg_set<MyClass>::type SgSet;
82 typedef make_treap_set<MyClass
83 , base_hook<TreapHook> >::type TreapSet;
84 typedef make_splay_set<MyClass
85 , base_hook<SplayHook> >::type SplaySet;
89 typedef std::vector<MyClass>::iterator VectIt;
90 typedef std::vector<MyClass>::reverse_iterator VectRit;
92 //Create several MyClass objects, each one with a different value
93 std::vector<MyClass> values;
94 for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
96 USet::bucket_type buckets[100];
101 USet my_uset(USet::bucket_traits
102 (pointer_traits<USet::bucket_ptr>::pointer_to(buckets[0]), 100));
105 SplaySet my_splayset;
107 TreapSet my_treapset;
109 //Now insert them in containers
110 for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
111 my_list.push_front(*it);
112 my_slist.push_front(*it);
115 my_avlset.insert(*it);
116 my_splayset.insert(*it);
117 my_sgset.insert(*it);
118 my_treapset.insert(*it);
123 List::const_iterator list_it(my_list.cbegin());
124 Slist::const_iterator slist_it(my_slist.cbegin());
125 Set::const_reverse_iterator set_rit(my_set.crbegin());
127 AvlSet::const_reverse_iterator avlset_rit(my_avlset.crbegin());
128 SplaySet::const_reverse_iterator splayset_rit(my_splayset.crbegin());
129 SgSet::const_reverse_iterator sgset_rit(my_sgset.crbegin());
130 TreapSet::const_reverse_iterator treapset_rit(my_treapset.crbegin());
132 VectRit vect_it(values.rbegin()), vect_itend(values.rend());
134 //Test the objects inserted in the base hook list
135 for( ; vect_it != vect_itend
136 ; ++vect_it, ++list_it, ++slist_it, ++set_rit
137 , ++avlset_rit, ++splayset_rit, ++sgset_rit, ++treapset_rit
139 if(&*list_it != &*vect_it) return 1;
140 if(&*slist_it != &*vect_it) return 1;
141 if(&*set_rit != &*vect_it) return 1;
142 if(my_uset.find(*set_rit) == my_uset.cend()) return 1;
143 if(&*avlset_rit != &*vect_it) return 1;
144 if(&*splayset_rit != &*vect_it) return 1;
145 if(&*sgset_rit != &*vect_it) return 1;
146 if(&*treapset_rit != &*vect_it) return 1;
150 //Check defined types and implicitly defined types are equal
151 BOOST_STATIC_ASSERT((detail::is_same<make_list_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
152 ,make_list_base_hook<>::type
155 BOOST_STATIC_ASSERT((detail::is_same<make_slist_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
156 ,make_slist_base_hook<>::type
159 BOOST_STATIC_ASSERT((detail::is_same<make_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
160 ,make_set_base_hook<>::type
163 BOOST_STATIC_ASSERT((detail::is_same<make_unordered_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
164 ,make_unordered_set_base_hook<>::type
167 BOOST_STATIC_ASSERT((detail::is_same<make_avl_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
168 ,make_avl_set_base_hook<>::type
171 BOOST_STATIC_ASSERT((detail::is_same<make_bs_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
172 ,make_bs_set_base_hook<>::type
175 //Check defined types and implicitly defined types are unequal
176 BOOST_STATIC_ASSERT(!(detail::is_same<make_list_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
177 ,make_list_base_hook<>::type
180 BOOST_STATIC_ASSERT(!(detail::is_same<make_slist_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
181 ,make_slist_base_hook<>::type
184 BOOST_STATIC_ASSERT(!(detail::is_same<make_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
185 ,make_set_base_hook<>::type
188 BOOST_STATIC_ASSERT(!(detail::is_same<make_unordered_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
189 ,make_unordered_set_base_hook<>::type
192 BOOST_STATIC_ASSERT(!(detail::is_same<make_avl_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
193 ,make_avl_set_base_hook<>::type
196 BOOST_STATIC_ASSERT(!(detail::is_same<make_bs_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
197 ,make_bs_set_base_hook<>::type