• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_unordered_set_code
13 #include <boost/intrusive/unordered_set.hpp>
14 #include <vector>
15 #include <functional>
16 #include <boost/functional/hash.hpp>
17 
18 using namespace boost::intrusive;
19 
20 class MyClass : public unordered_set_base_hook<>
21 {               //This is a derivation hook
22    int int_;
23 
24    public:
25    unordered_set_member_hook<> member_hook_; //This is a member hook
26 
MyClass(int i)27    MyClass(int i)
28       :  int_(i)
29    {}
30 
operator ==(const MyClass & a,const MyClass & b)31    friend bool operator== (const MyClass &a, const MyClass &b)
32    {  return a.int_ == b.int_;  }
33 
hash_value(const MyClass & value)34    friend std::size_t hash_value(const MyClass &value)
35    {  return std::size_t(value.int_); }
36 };
37 
38 //Define an unordered_set that will store MyClass objects using the base hook
39 typedef unordered_set<MyClass>    BaseSet;
40 
41 //Define an unordered_multiset that will store MyClass using the member hook
42 typedef member_hook<MyClass, unordered_set_member_hook<>, &MyClass::member_hook_>
43    MemberOption;
44 typedef unordered_multiset< MyClass, MemberOption>  MemberMultiSet;
45 
main()46 int main()
47 {
48    typedef std::vector<MyClass>::iterator VectIt;
49 
50    //Create a vector with 100 different MyClass objects
51    std::vector<MyClass> values;
52    for(int i = 0; i < 100; ++i)  values.push_back(MyClass(i));
53 
54    //Create a copy of the vector
55    std::vector<MyClass> values2(values);
56 
57    //Create a bucket array for base_set
58    BaseSet::bucket_type base_buckets[100];
59 
60    //Create a bucket array for member_multi_set
61    MemberMultiSet::bucket_type member_buckets[200];
62 
63    //Create unordered containers taking buckets as arguments
64    BaseSet base_set(BaseSet::bucket_traits(base_buckets, 100));
65    MemberMultiSet member_multi_set
66       (MemberMultiSet::bucket_traits(member_buckets, 200));
67 
68    //Now insert values's elements in the unordered_set
69    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
70       base_set.insert(*it);
71 
72    //Now insert values's and values2's elements in the unordered_multiset
73    for(VectIt it(values.begin()), itend(values.end()), it2(values2.begin())
74       ; it != itend; ++it, ++it2){
75       member_multi_set.insert(*it);
76       member_multi_set.insert(*it2);
77    }
78 
79    //Now find every element
80    {
81       VectIt it(values.begin()), itend(values.end());
82 
83       for(; it != itend; ++it){
84          //base_set should contain one element for each key
85          if(base_set.count(*it) != 1)           return 1;
86          //member_multi_set should contain two elements for each key
87          if(member_multi_set.count(*it) != 2)   return 1;
88       }
89    }
90    return 0;
91 }
92 //]
93