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 /////////////////////////////////////////////////////////////////////////////
13 #include <boost/intrusive/sg_set.hpp>
18 using namespace boost::intrusive
;
20 class MyClass
: public bs_set_base_hook
<>
25 //This is a member hook
26 bs_set_member_hook
<> member_hook_
;
31 friend bool operator< (const MyClass
&a
, const MyClass
&b
)
32 { return a
.int_
< b
.int_
; }
33 friend bool operator> (const MyClass
&a
, const MyClass
&b
)
34 { return a
.int_
> b
.int_
; }
35 friend bool operator== (const MyClass
&a
, const MyClass
&b
)
36 { return a
.int_
== b
.int_
; }
39 //Define an sg_set using the base hook that will store values in reverse order
40 //and won't execute floating point operations.
42 < MyClass
, compare
<std::greater
<MyClass
> >, floating_point
<false> > BaseSet
;
44 //Define an multiset using the member hook
45 typedef member_hook
<MyClass
, bs_set_member_hook
<>, &MyClass::member_hook_
> MemberOption
;
46 typedef sg_multiset
< MyClass
, MemberOption
> MemberMultiset
;
50 typedef std::vector
<MyClass
>::iterator VectIt
;
52 //Create several MyClass objects, each one with a different value
53 std::vector
<MyClass
> values
;
54 for(int i
= 0; i
< 100; ++i
) values
.push_back(MyClass(i
));
57 MemberMultiset membermultiset
;
59 //Now insert them in the reverse order in the base hook sg_set
60 for(VectIt
it(values
.begin()), itend(values
.end()); it
!= itend
; ++it
){
62 membermultiset
.insert(*it
);
65 //Change balance factor
66 membermultiset
.balance_factor(0.9f
);
70 BaseSet::reverse_iterator
rbit(baseset
.rbegin());
71 MemberMultiset::iterator
mit(membermultiset
.begin());
72 VectIt
it(values
.begin()), itend(values
.end());
74 //Test the objects inserted in the base hook sg_set
75 for(; it
!= itend
; ++it
, ++rbit
)
76 if(&*rbit
!= &*it
) return 1;
78 //Test the objects inserted in the member hook sg_set
79 for(it
= values
.begin(); it
!= itend
; ++it
, ++mit
)
80 if(&*mit
!= &*it
) return 1;