1 | #include <boost/intrusive/list.hpp>
|
---|
2 |
|
---|
3 | using namespace boost::intrusive;
|
---|
4 |
|
---|
5 | //This type is not modifiable so we can't store hooks or custom nodes
|
---|
6 | typedef int identifier_t;
|
---|
7 |
|
---|
8 | //This value traits will associate elements from an array of identifiers with
|
---|
9 | //elements of an array of nodes. The element i of the value array will use the
|
---|
10 | //node i of the node array:
|
---|
11 | struct stateful_value_traits
|
---|
12 | {
|
---|
13 | typedef list_node_traits<void*> node_traits;
|
---|
14 | typedef node_traits::node node;
|
---|
15 | typedef node * node_ptr;
|
---|
16 | typedef const node * const_node_ptr;
|
---|
17 | typedef identifier_t value_type;
|
---|
18 | typedef identifier_t * pointer;
|
---|
19 | typedef const identifier_t * const_pointer;
|
---|
20 | static const link_mode_type link_mode = normal_link;
|
---|
21 |
|
---|
22 | stateful_value_traits(pointer ids, node_ptr node_array)
|
---|
23 | : ids_(ids), nodes_(node_array)
|
---|
24 | {}
|
---|
25 |
|
---|
26 | ///Note: non static functions!
|
---|
27 | node_ptr to_node_ptr (value_type &value)
|
---|
28 | { return this->nodes_ + (&value - this->ids_); }
|
---|
29 | const_node_ptr to_node_ptr (const value_type &value) const
|
---|
30 | { return this->nodes_ + (&value - this->ids_); }
|
---|
31 | pointer to_value_ptr(node_ptr n)
|
---|
32 | { return this->ids_ + (n - this->nodes_); }
|
---|
33 | const_pointer to_value_ptr(const_node_ptr n) const
|
---|
34 | { return this->ids_ + (n - this->nodes_); }
|
---|
35 |
|
---|
36 | private:
|
---|
37 | pointer ids_;
|
---|
38 | node_ptr nodes_;
|
---|
39 | };
|
---|
40 |
|
---|
41 | int main()
|
---|
42 | {
|
---|
43 | const int NumElements = 100;
|
---|
44 |
|
---|
45 | //This is an array of ids that we want to "store"
|
---|
46 | identifier_t ids [NumElements];
|
---|
47 |
|
---|
48 | //This is an array of nodes that is necessary to form the linked list
|
---|
49 | list_node_traits<void*>::node nodes [NumElements];
|
---|
50 |
|
---|
51 | //Initialize id objects, each one with a different number
|
---|
52 | for(int i = 0; i != NumElements; ++i) ids[i] = i;
|
---|
53 |
|
---|
54 | //Define a list that will "link" identifiers using external nodes
|
---|
55 | typedef list<identifier_t, value_traits<stateful_value_traits> > List;
|
---|
56 |
|
---|
57 | //This list will store ids without modifying identifier_t instances
|
---|
58 | //Stateful value traits must be explicitly passed in the constructor.
|
---|
59 | List my_list (stateful_value_traits (ids, nodes));
|
---|
60 |
|
---|
61 | //Insert ids in reverse order in the list
|
---|
62 | for(identifier_t * it(&ids[0]), *itend(&ids[NumElements]); it != itend; ++it)
|
---|
63 | my_list.push_front(*it);
|
---|
64 |
|
---|
65 | //Now test lists
|
---|
66 | List::const_iterator list_it (my_list.cbegin());
|
---|
67 | List::const_iterator tmp = my_list.iterator_to(*list_it);
|
---|
68 | identifier_t *it_val(&ids[NumElements-1]), *it_rbeg_val(&ids[0]-1);
|
---|
69 |
|
---|
70 | //Test the objects inserted in the base hook list
|
---|
71 | for(; it_val != it_rbeg_val; --it_val, ++list_it)
|
---|
72 | if(&*list_it != &*it_val) return 1;
|
---|
73 |
|
---|
74 | return 0;
|
---|
75 | }
|
---|