Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / intrusive / example / doc_splay_set.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2006-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 //[doc_splay_set_code
13 #include <boost/intrusive/splay_set.hpp>
14 #include <vector>
15 #include <algorithm>
16
17 using namespace boost::intrusive;
18
19 class mytag;
20
21 class MyClass
22    : public bs_set_base_hook<>
23 {
24    int int_;
25
26    public:
27    //This is a member hook
28    bs_set_member_hook<> member_hook_;
29
30    MyClass(int i)
31       :  int_(i)
32       {}
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_;  }
37    friend bool operator== (const MyClass &a, const MyClass &b)
38       {  return a.int_ == b.int_;  }
39 };
40
41 //Define a set using the base hook that will store values in reverse order
42 typedef splay_set< MyClass, compare<std::greater<MyClass> > >     BaseSplaySet;
43
44 //Define an multiset using the member hook
45 typedef member_hook<MyClass, bs_set_member_hook<>, &MyClass::member_hook_> MemberOption;
46 typedef splay_multiset< MyClass, MemberOption>   MemberSplayMultiset;
47
48 int main()
49 {
50    typedef std::vector<MyClass>::iterator VectIt;
51
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));
55
56    BaseSplaySet   baseset;
57    MemberSplayMultiset membermultiset;
58
59
60    //Insert values in the container
61    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
62       baseset.insert(*it);
63       membermultiset.insert(*it);
64    }
65
66    //Now test sets
67    {
68       BaseSplaySet::reverse_iterator rbit(baseset.rbegin());
69       MemberSplayMultiset::iterator mit(membermultiset.begin());
70       VectIt it(values.begin()), itend(values.end());
71
72       //Test the objects inserted in the base hook set
73       for(; it != itend; ++it, ++rbit){
74          if(&*rbit != &*it)   return 1;
75       }
76
77       //Test the objects inserted in member and binary search hook sets
78       for(it = values.begin(); it != itend; ++it, ++mit){
79          if(&*mit != &*it)    return 1;
80       }
81    }
82    return 0;
83 }
84 //]