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