Chris@16
|
1 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2007-2013
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 //
|
Chris@16
|
9 // See http://www.boost.org/libs/intrusive for documentation.
|
Chris@16
|
10 //
|
Chris@16
|
11 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
12
|
Chris@16
|
13 #ifndef BOOST_INTRUSIVE_AVLTREE_NODE_HPP
|
Chris@16
|
14 #define BOOST_INTRUSIVE_AVLTREE_NODE_HPP
|
Chris@16
|
15
|
Chris@101
|
16 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
17 # include <boost/config.hpp>
|
Chris@101
|
18 #endif
|
Chris@101
|
19
|
Chris@101
|
20 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
21 # pragma once
|
Chris@101
|
22 #endif
|
Chris@101
|
23
|
Chris@16
|
24 #include <boost/intrusive/detail/config_begin.hpp>
|
Chris@101
|
25 #include <boost/intrusive/pointer_rebind.hpp>
|
Chris@16
|
26 #include <boost/intrusive/avltree_algorithms.hpp>
|
Chris@16
|
27 #include <boost/intrusive/pointer_plus_bits.hpp>
|
Chris@16
|
28 #include <boost/intrusive/detail/mpl.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31 namespace intrusive {
|
Chris@16
|
32
|
Chris@16
|
33 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
34 // //
|
Chris@16
|
35 // Generic node_traits for any pointer type //
|
Chris@16
|
36 // //
|
Chris@16
|
37 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
38
|
Chris@16
|
39 //This is the compact representation: 3 pointers
|
Chris@16
|
40 template<class VoidPointer>
|
Chris@16
|
41 struct compact_avltree_node
|
Chris@16
|
42 {
|
Chris@101
|
43 typedef typename pointer_rebind<VoidPointer, compact_avltree_node<VoidPointer> >::type node_ptr;
|
Chris@101
|
44 typedef typename pointer_rebind<VoidPointer, const compact_avltree_node<VoidPointer> >::type const_node_ptr;
|
Chris@16
|
45 enum balance { negative_t, zero_t, positive_t };
|
Chris@16
|
46 node_ptr parent_, left_, right_;
|
Chris@16
|
47 };
|
Chris@16
|
48
|
Chris@16
|
49 //This is the normal representation: 3 pointers + enum
|
Chris@16
|
50 template<class VoidPointer>
|
Chris@16
|
51 struct avltree_node
|
Chris@16
|
52 {
|
Chris@101
|
53 typedef typename pointer_rebind<VoidPointer, avltree_node<VoidPointer> >::type node_ptr;
|
Chris@101
|
54 typedef typename pointer_rebind<VoidPointer, const avltree_node<VoidPointer> >::type const_node_ptr;
|
Chris@16
|
55 enum balance { negative_t, zero_t, positive_t };
|
Chris@16
|
56 node_ptr parent_, left_, right_;
|
Chris@16
|
57 balance balance_;
|
Chris@16
|
58 };
|
Chris@16
|
59
|
Chris@16
|
60 //This is the default node traits implementation
|
Chris@16
|
61 //using a node with 3 generic pointers plus an enum
|
Chris@16
|
62 template<class VoidPointer>
|
Chris@16
|
63 struct default_avltree_node_traits_impl
|
Chris@16
|
64 {
|
Chris@16
|
65 typedef avltree_node<VoidPointer> node;
|
Chris@101
|
66 typedef typename node::node_ptr node_ptr;
|
Chris@101
|
67 typedef typename node::const_node_ptr const_node_ptr;
|
Chris@16
|
68
|
Chris@16
|
69 typedef typename node::balance balance;
|
Chris@16
|
70
|
Chris@16
|
71 static node_ptr get_parent(const const_node_ptr & n)
|
Chris@16
|
72 { return n->parent_; }
|
Chris@16
|
73
|
Chris@16
|
74 static node_ptr get_parent(const node_ptr & n)
|
Chris@16
|
75 { return n->parent_; }
|
Chris@16
|
76
|
Chris@16
|
77 static void set_parent(const node_ptr & n, const node_ptr & p)
|
Chris@16
|
78 { n->parent_ = p; }
|
Chris@16
|
79
|
Chris@16
|
80 static node_ptr get_left(const const_node_ptr & n)
|
Chris@16
|
81 { return n->left_; }
|
Chris@16
|
82
|
Chris@16
|
83 static node_ptr get_left(const node_ptr & n)
|
Chris@16
|
84 { return n->left_; }
|
Chris@16
|
85
|
Chris@16
|
86 static void set_left(const node_ptr & n, const node_ptr & l)
|
Chris@16
|
87 { n->left_ = l; }
|
Chris@16
|
88
|
Chris@16
|
89 static node_ptr get_right(const const_node_ptr & n)
|
Chris@16
|
90 { return n->right_; }
|
Chris@16
|
91
|
Chris@16
|
92 static node_ptr get_right(const node_ptr & n)
|
Chris@16
|
93 { return n->right_; }
|
Chris@16
|
94
|
Chris@16
|
95 static void set_right(const node_ptr & n, const node_ptr & r)
|
Chris@16
|
96 { n->right_ = r; }
|
Chris@16
|
97
|
Chris@16
|
98 static balance get_balance(const const_node_ptr & n)
|
Chris@16
|
99 { return n->balance_; }
|
Chris@16
|
100
|
Chris@16
|
101 static balance get_balance(const node_ptr & n)
|
Chris@16
|
102 { return n->balance_; }
|
Chris@16
|
103
|
Chris@16
|
104 static void set_balance(const node_ptr & n, balance b)
|
Chris@16
|
105 { n->balance_ = b; }
|
Chris@16
|
106
|
Chris@16
|
107 static balance negative()
|
Chris@16
|
108 { return node::negative_t; }
|
Chris@16
|
109
|
Chris@16
|
110 static balance zero()
|
Chris@16
|
111 { return node::zero_t; }
|
Chris@16
|
112
|
Chris@16
|
113 static balance positive()
|
Chris@16
|
114 { return node::positive_t; }
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 //This is the compact node traits implementation
|
Chris@16
|
118 //using a node with 3 generic pointers
|
Chris@16
|
119 template<class VoidPointer>
|
Chris@16
|
120 struct compact_avltree_node_traits_impl
|
Chris@16
|
121 {
|
Chris@16
|
122 typedef compact_avltree_node<VoidPointer> node;
|
Chris@101
|
123 typedef typename node::node_ptr node_ptr;
|
Chris@101
|
124 typedef typename node::const_node_ptr const_node_ptr;
|
Chris@16
|
125 typedef typename node::balance balance;
|
Chris@16
|
126
|
Chris@16
|
127 typedef pointer_plus_bits<node_ptr, 2> ptr_bit;
|
Chris@16
|
128
|
Chris@16
|
129 static node_ptr get_parent(const const_node_ptr & n)
|
Chris@16
|
130 { return ptr_bit::get_pointer(n->parent_); }
|
Chris@16
|
131
|
Chris@16
|
132 static void set_parent(const node_ptr & n, const node_ptr & p)
|
Chris@16
|
133 { ptr_bit::set_pointer(n->parent_, p); }
|
Chris@16
|
134
|
Chris@16
|
135 static node_ptr get_left(const const_node_ptr & n)
|
Chris@16
|
136 { return n->left_; }
|
Chris@16
|
137
|
Chris@16
|
138 static void set_left(const node_ptr & n, const node_ptr & l)
|
Chris@16
|
139 { n->left_ = l; }
|
Chris@16
|
140
|
Chris@16
|
141 static node_ptr get_right(const const_node_ptr & n)
|
Chris@16
|
142 { return n->right_; }
|
Chris@16
|
143
|
Chris@16
|
144 static void set_right(const node_ptr & n, const node_ptr & r)
|
Chris@16
|
145 { n->right_ = r; }
|
Chris@16
|
146
|
Chris@16
|
147 static balance get_balance(const const_node_ptr & n)
|
Chris@16
|
148 { return (balance)ptr_bit::get_bits(n->parent_); }
|
Chris@16
|
149
|
Chris@16
|
150 static void set_balance(const node_ptr & n, balance b)
|
Chris@16
|
151 { ptr_bit::set_bits(n->parent_, (std::size_t)b); }
|
Chris@16
|
152
|
Chris@16
|
153 static balance negative()
|
Chris@16
|
154 { return node::negative_t; }
|
Chris@16
|
155
|
Chris@16
|
156 static balance zero()
|
Chris@16
|
157 { return node::zero_t; }
|
Chris@16
|
158
|
Chris@16
|
159 static balance positive()
|
Chris@16
|
160 { return node::positive_t; }
|
Chris@16
|
161 };
|
Chris@16
|
162
|
Chris@16
|
163 //Dispatches the implementation based on the boolean
|
Chris@16
|
164 template<class VoidPointer, bool Compact>
|
Chris@16
|
165 struct avltree_node_traits_dispatch
|
Chris@16
|
166 : public default_avltree_node_traits_impl<VoidPointer>
|
Chris@16
|
167 {};
|
Chris@16
|
168
|
Chris@16
|
169 template<class VoidPointer>
|
Chris@16
|
170 struct avltree_node_traits_dispatch<VoidPointer, true>
|
Chris@16
|
171 : public compact_avltree_node_traits_impl<VoidPointer>
|
Chris@16
|
172 {};
|
Chris@16
|
173
|
Chris@101
|
174 //Inherit from rbtree_node_traits_dispatch depending on the embedding capabilities
|
Chris@16
|
175 template<class VoidPointer, bool OptimizeSize = false>
|
Chris@16
|
176 struct avltree_node_traits
|
Chris@16
|
177 : public avltree_node_traits_dispatch
|
Chris@16
|
178 < VoidPointer
|
Chris@16
|
179 , OptimizeSize &&
|
Chris@16
|
180 max_pointer_plus_bits
|
Chris@16
|
181 < VoidPointer
|
Chris@16
|
182 , detail::alignment_of<compact_avltree_node<VoidPointer> >::value
|
Chris@16
|
183 >::value >= 2u
|
Chris@16
|
184 >
|
Chris@16
|
185 {};
|
Chris@16
|
186
|
Chris@16
|
187 } //namespace intrusive
|
Chris@16
|
188 } //namespace boost
|
Chris@16
|
189
|
Chris@16
|
190 #include <boost/intrusive/detail/config_end.hpp>
|
Chris@16
|
191
|
Chris@16
|
192 #endif //BOOST_INTRUSIVE_AVLTREE_NODE_HPP
|