]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_iterator_from_value | |
13 | #include <boost/intrusive/list.hpp> | |
14 | #include <boost/intrusive/unordered_set.hpp> | |
1e59de90 | 15 | #include <boost/container_hash/hash.hpp> |
7c673cae FG |
16 | #include <vector> |
17 | ||
18 | using namespace boost::intrusive; | |
19 | ||
20 | class intrusive_data | |
21 | { | |
22 | int data_id_; | |
23 | public: | |
24 | ||
25 | void set(int id) { data_id_ = id; } | |
26 | ||
27 | //This class can be inserted in an intrusive list | |
28 | list_member_hook<> list_hook_; | |
29 | ||
30 | //This class can be inserted in an intrusive unordered_set | |
31 | unordered_set_member_hook<> unordered_set_hook_; | |
32 | ||
33 | //Comparison operators | |
34 | friend bool operator==(const intrusive_data &a, const intrusive_data &b) | |
35 | { return a.data_id_ == b.data_id_; } | |
36 | ||
37 | friend bool operator!=(const intrusive_data &a, const intrusive_data &b) | |
38 | { return a.data_id_ != b.data_id_; } | |
39 | ||
40 | //The hash function | |
41 | friend std::size_t hash_value(const intrusive_data &i) | |
42 | { return boost::hash<int>()(i.data_id_); } | |
43 | }; | |
44 | ||
45 | //Definition of the intrusive list that will hold intrusive_data | |
46 | typedef member_hook<intrusive_data, list_member_hook<> | |
47 | , &intrusive_data::list_hook_> MemberListOption; | |
48 | typedef list<intrusive_data, MemberListOption> list_t; | |
49 | ||
50 | //Definition of the intrusive unordered_set that will hold intrusive_data | |
51 | typedef member_hook | |
52 | < intrusive_data, unordered_set_member_hook<> | |
53 | , &intrusive_data::unordered_set_hook_> MemberUsetOption; | |
54 | typedef boost::intrusive::unordered_set | |
55 | < intrusive_data, MemberUsetOption> unordered_set_t; | |
56 | ||
57 | int main() | |
58 | { | |
59 | //Create MaxElem objects | |
1e59de90 | 60 | const std::size_t MaxElem = 100; |
7c673cae FG |
61 | std::vector<intrusive_data> nodes(MaxElem); |
62 | ||
63 | //Declare the intrusive containers | |
64 | list_t list; | |
65 | unordered_set_t::bucket_type buckets[MaxElem]; | |
66 | unordered_set_t unordered_set | |
67 | (unordered_set_t::bucket_traits(buckets, MaxElem)); | |
68 | ||
69 | //Initialize all the nodes | |
1e59de90 | 70 | for(std::size_t i = 0; i < MaxElem; ++i) nodes[i].set((int)i); |
7c673cae FG |
71 | |
72 | //Now insert them in both intrusive containers | |
73 | list.insert(list.end(), nodes.begin(), nodes.end()); | |
74 | unordered_set.insert(nodes.begin(), nodes.end()); | |
75 | ||
76 | //Now check the iterator_to function | |
77 | list_t::iterator list_it(list.begin()); | |
1e59de90 | 78 | for(std::size_t i = 0; i < MaxElem; ++i, ++list_it) |
7c673cae FG |
79 | if(list.iterator_to(nodes[i]) != list_it || |
80 | list_t::s_iterator_to(nodes[i]) != list_it) | |
81 | return 1; | |
82 | ||
83 | //Now check unordered_set::s_iterator_to (which is a member function) | |
84 | //and unordered_set::s_local_iterator_to (which is an static member function) | |
85 | unordered_set_t::iterator unordered_set_it(unordered_set.begin()); | |
1e59de90 | 86 | for(std::size_t i = 0; i < MaxElem; ++i){ |
7c673cae FG |
87 | unordered_set_it = unordered_set.find(nodes[i]); |
88 | if(unordered_set.iterator_to(nodes[i]) != unordered_set_it) | |
89 | return 1; | |
90 | if(*unordered_set.local_iterator_to(nodes[i]) != *unordered_set_it || | |
91 | *unordered_set_t::s_local_iterator_to(nodes[i]) != *unordered_set_it ) | |
92 | return 1; | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | //] |