Chris@16
|
1 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@101
|
3 // (C) Copyright Ion Gaztanaga 2007-2014
|
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 // Scapegoat tree algorithms are taken from the paper titled:
|
Chris@16
|
14 // "Scapegoat Trees" by Igal Galperin Ronald L. Rivest.
|
Chris@16
|
15 //
|
Chris@16
|
16 /////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
17 #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
|
Chris@16
|
18 #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/intrusive/detail/config_begin.hpp>
|
Chris@101
|
21 #include <boost/intrusive/intrusive_fwd.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #include <cstddef>
|
Chris@101
|
24 #include <boost/intrusive/detail/algo_type.hpp>
|
Chris@16
|
25 #include <boost/intrusive/bstree_algorithms.hpp>
|
Chris@16
|
26
|
Chris@101
|
27 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
28 # pragma once
|
Chris@101
|
29 #endif
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32 namespace intrusive {
|
Chris@16
|
33
|
Chris@16
|
34 //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
|
Chris@16
|
35 //! information about the node to be manipulated. NodeTraits must support the
|
Chris@16
|
36 //! following interface:
|
Chris@16
|
37 //!
|
Chris@16
|
38 //! <b>Typedefs</b>:
|
Chris@16
|
39 //!
|
Chris@16
|
40 //! <tt>node</tt>: The type of the node that forms the binary search tree
|
Chris@16
|
41 //!
|
Chris@16
|
42 //! <tt>node_ptr</tt>: A pointer to a node
|
Chris@16
|
43 //!
|
Chris@16
|
44 //! <tt>const_node_ptr</tt>: A pointer to a const node
|
Chris@16
|
45 //!
|
Chris@16
|
46 //! <b>Static functions</b>:
|
Chris@16
|
47 //!
|
Chris@16
|
48 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
|
Chris@16
|
49 //!
|
Chris@16
|
50 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
|
Chris@16
|
51 //!
|
Chris@16
|
52 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
|
Chris@16
|
53 //!
|
Chris@16
|
54 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
|
Chris@16
|
55 //!
|
Chris@16
|
56 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
|
Chris@16
|
57 //!
|
Chris@16
|
58 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
|
Chris@16
|
59 template<class NodeTraits>
|
Chris@16
|
60 class sgtree_algorithms
|
Chris@16
|
61 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
62 : public bstree_algorithms<NodeTraits>
|
Chris@16
|
63 #endif
|
Chris@16
|
64 {
|
Chris@16
|
65 public:
|
Chris@16
|
66 typedef typename NodeTraits::node node;
|
Chris@16
|
67 typedef NodeTraits node_traits;
|
Chris@16
|
68 typedef typename NodeTraits::node_ptr node_ptr;
|
Chris@16
|
69 typedef typename NodeTraits::const_node_ptr const_node_ptr;
|
Chris@16
|
70
|
Chris@16
|
71 /// @cond
|
Chris@16
|
72 private:
|
Chris@16
|
73
|
Chris@16
|
74 typedef bstree_algorithms<NodeTraits> bstree_algo;
|
Chris@16
|
75
|
Chris@16
|
76 /// @endcond
|
Chris@16
|
77
|
Chris@16
|
78 public:
|
Chris@16
|
79 //! This type is the information that will be
|
Chris@16
|
80 //! filled by insert_unique_check
|
Chris@16
|
81 struct insert_commit_data
|
Chris@16
|
82 : bstree_algo::insert_commit_data
|
Chris@16
|
83 {
|
Chris@16
|
84 std::size_t depth;
|
Chris@16
|
85 };
|
Chris@16
|
86
|
Chris@16
|
87 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
88 //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
|
Chris@16
|
89 static node_ptr get_header(const const_node_ptr & n);
|
Chris@16
|
90
|
Chris@16
|
91 //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
|
Chris@16
|
92 static node_ptr begin_node(const const_node_ptr & header);
|
Chris@16
|
93
|
Chris@16
|
94 //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
|
Chris@16
|
95 static node_ptr end_node(const const_node_ptr & header);
|
Chris@16
|
96
|
Chris@16
|
97 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
|
Chris@16
|
98 static void swap_tree(const node_ptr & header1, const node_ptr & header2);
|
Chris@16
|
99
|
Chris@16
|
100 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
|
Chris@16
|
101 static void swap_nodes(const node_ptr & node1, const node_ptr & node2);
|
Chris@16
|
102
|
Chris@16
|
103 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
|
Chris@16
|
104 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2);
|
Chris@16
|
105
|
Chris@16
|
106 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
|
Chris@16
|
107 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node);
|
Chris@16
|
108
|
Chris@16
|
109 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
|
Chris@16
|
110 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node);
|
Chris@16
|
111
|
Chris@16
|
112 //Unlink is not possible since tree metadata is needed to update the tree
|
Chris@16
|
113 //!static void unlink(const node_ptr & node);
|
Chris@16
|
114
|
Chris@16
|
115 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
|
Chris@16
|
116 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
|
Chris@16
|
117
|
Chris@16
|
118 //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
|
Chris@16
|
119 static bool unique(const const_node_ptr & node);
|
Chris@16
|
120
|
Chris@16
|
121 //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
|
Chris@16
|
122 static std::size_t size(const const_node_ptr & header);
|
Chris@16
|
123
|
Chris@16
|
124 //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
|
Chris@16
|
125 static node_ptr next_node(const node_ptr & node);
|
Chris@16
|
126
|
Chris@16
|
127 //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
|
Chris@16
|
128 static node_ptr prev_node(const node_ptr & node);
|
Chris@16
|
129
|
Chris@16
|
130 //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
|
Chris@16
|
131 static void init(const node_ptr & node);
|
Chris@16
|
132
|
Chris@16
|
133 //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
|
Chris@16
|
134 static void init_header(const node_ptr & header);
|
Chris@16
|
135 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
136
|
Chris@16
|
137 //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
|
Chris@16
|
138 template<class AlphaByMaxSize>
|
Chris@16
|
139 static node_ptr erase(const node_ptr & header, const node_ptr & z, std::size_t tree_size, std::size_t &max_tree_size, AlphaByMaxSize alpha_by_maxsize)
|
Chris@16
|
140 {
|
Chris@16
|
141 //typename bstree_algo::data_for_rebalance info;
|
Chris@16
|
142 bstree_algo::erase(header, z);
|
Chris@16
|
143 --tree_size;
|
Chris@16
|
144 if (tree_size > 0 &&
|
Chris@16
|
145 tree_size < alpha_by_maxsize(max_tree_size)){
|
Chris@16
|
146 bstree_algo::rebalance(header);
|
Chris@16
|
147 max_tree_size = tree_size;
|
Chris@16
|
148 }
|
Chris@16
|
149 return z;
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
153 //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
|
Chris@16
|
154 template <class Cloner, class Disposer>
|
Chris@16
|
155 static void clone
|
Chris@16
|
156 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
|
Chris@16
|
157
|
Chris@16
|
158 //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
|
Chris@16
|
159 template<class Disposer>
|
Chris@16
|
160 static void clear_and_dispose(const node_ptr & header, Disposer disposer);
|
Chris@16
|
161
|
Chris@16
|
162 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
163 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
164 static node_ptr lower_bound
|
Chris@16
|
165 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
166
|
Chris@16
|
167 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
168 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
169 static node_ptr upper_bound
|
Chris@16
|
170 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
171
|
Chris@16
|
172 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
173 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
174 static node_ptr find
|
Chris@16
|
175 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
176
|
Chris@16
|
177 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
178 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
179 static std::pair<node_ptr, node_ptr> equal_range
|
Chris@16
|
180 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@16
|
181
|
Chris@16
|
182 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
|
Chris@16
|
183 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
184 static std::pair<node_ptr, node_ptr> bounded_range
|
Chris@16
|
185 (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
|
Chris@16
|
186 , bool left_closed, bool right_closed);
|
Chris@16
|
187
|
Chris@16
|
188 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
|
Chris@16
|
189 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
190 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
|
Chris@101
|
191
|
Chris@16
|
192 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
193
|
Chris@16
|
194 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
|
Chris@16
|
195 template<class NodePtrCompare, class H_Alpha>
|
Chris@16
|
196 static node_ptr insert_equal_upper_bound
|
Chris@16
|
197 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
|
Chris@16
|
198 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
199 {
|
Chris@16
|
200 std::size_t depth;
|
Chris@16
|
201 bstree_algo::insert_equal_upper_bound(h, new_node, comp, &depth);
|
Chris@16
|
202 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
|
Chris@16
|
203 return new_node;
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
|
Chris@16
|
207 template<class NodePtrCompare, class H_Alpha>
|
Chris@16
|
208 static node_ptr insert_equal_lower_bound
|
Chris@16
|
209 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
|
Chris@16
|
210 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
211 {
|
Chris@16
|
212 std::size_t depth;
|
Chris@16
|
213 bstree_algo::insert_equal_lower_bound(h, new_node, comp, &depth);
|
Chris@16
|
214 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
|
Chris@16
|
215 return new_node;
|
Chris@16
|
216 }
|
Chris@16
|
217
|
Chris@16
|
218 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
|
Chris@16
|
219 template<class NodePtrCompare, class H_Alpha>
|
Chris@16
|
220 static node_ptr insert_equal
|
Chris@16
|
221 (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
|
Chris@16
|
222 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
223 {
|
Chris@16
|
224 std::size_t depth;
|
Chris@16
|
225 bstree_algo::insert_equal(header, hint, new_node, comp, &depth);
|
Chris@16
|
226 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
|
Chris@16
|
227 return new_node;
|
Chris@16
|
228 }
|
Chris@16
|
229
|
Chris@16
|
230 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
|
Chris@16
|
231 template<class H_Alpha>
|
Chris@16
|
232 static node_ptr insert_before
|
Chris@16
|
233 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node
|
Chris@16
|
234 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
235 {
|
Chris@16
|
236 std::size_t depth;
|
Chris@16
|
237 bstree_algo::insert_before(header, pos, new_node, &depth);
|
Chris@16
|
238 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
|
Chris@16
|
239 return new_node;
|
Chris@16
|
240 }
|
Chris@16
|
241
|
Chris@16
|
242 //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
|
Chris@16
|
243 template<class H_Alpha>
|
Chris@16
|
244 static void push_back(const node_ptr & header, const node_ptr & new_node
|
Chris@16
|
245 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
246 {
|
Chris@16
|
247 std::size_t depth;
|
Chris@16
|
248 bstree_algo::push_back(header, new_node, &depth);
|
Chris@16
|
249 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
|
Chris@16
|
250 }
|
Chris@16
|
251
|
Chris@16
|
252 //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
|
Chris@16
|
253 template<class H_Alpha>
|
Chris@16
|
254 static void push_front(const node_ptr & header, const node_ptr & new_node
|
Chris@16
|
255 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
256 {
|
Chris@16
|
257 std::size_t depth;
|
Chris@16
|
258 bstree_algo::push_front(header, new_node, &depth);
|
Chris@16
|
259 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
|
Chris@16
|
260 }
|
Chris@16
|
261
|
Chris@16
|
262 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
|
Chris@16
|
263 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
264 static std::pair<node_ptr, bool> insert_unique_check
|
Chris@16
|
265 (const const_node_ptr & header, const KeyType &key
|
Chris@16
|
266 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
|
Chris@16
|
267 {
|
Chris@16
|
268 std::size_t depth;
|
Chris@16
|
269 std::pair<node_ptr, bool> ret =
|
Chris@16
|
270 bstree_algo::insert_unique_check(header, key, comp, commit_data, &depth);
|
Chris@16
|
271 commit_data.depth = depth;
|
Chris@16
|
272 return ret;
|
Chris@16
|
273 }
|
Chris@16
|
274
|
Chris@16
|
275 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
|
Chris@16
|
276 template<class KeyType, class KeyNodePtrCompare>
|
Chris@16
|
277 static std::pair<node_ptr, bool> insert_unique_check
|
Chris@16
|
278 (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
|
Chris@16
|
279 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
|
Chris@16
|
280 {
|
Chris@16
|
281 std::size_t depth;
|
Chris@16
|
282 std::pair<node_ptr, bool> ret =
|
Chris@16
|
283 bstree_algo::insert_unique_check
|
Chris@16
|
284 (header, hint, key, comp, commit_data, &depth);
|
Chris@16
|
285 commit_data.depth = depth;
|
Chris@16
|
286 return ret;
|
Chris@16
|
287 }
|
Chris@16
|
288
|
Chris@16
|
289 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&)
|
Chris@16
|
290 template<class H_Alpha>
|
Chris@16
|
291 static void insert_unique_commit
|
Chris@16
|
292 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data
|
Chris@16
|
293 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
294 {
|
Chris@16
|
295 bstree_algo::insert_unique_commit(header, new_value, commit_data);
|
Chris@16
|
296 rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size);
|
Chris@16
|
297 }
|
Chris@16
|
298
|
Chris@16
|
299 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
300 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
|
Chris@16
|
301 static bool is_header(const const_node_ptr & p);
|
Chris@16
|
302
|
Chris@16
|
303 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
|
Chris@16
|
304 static void rebalance(const node_ptr & header);
|
Chris@16
|
305
|
Chris@16
|
306 //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
|
Chris@16
|
307 static node_ptr rebalance_subtree(const node_ptr & old_root)
|
Chris@16
|
308 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
|
Chris@16
|
309
|
Chris@16
|
310 /// @cond
|
Chris@16
|
311 private:
|
Chris@16
|
312
|
Chris@16
|
313 template<class H_Alpha>
|
Chris@16
|
314 static void rebalance_after_insertion
|
Chris@16
|
315 (const node_ptr &x, std::size_t depth
|
Chris@16
|
316 , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
|
Chris@16
|
317 {
|
Chris@16
|
318 if(tree_size > max_tree_size)
|
Chris@16
|
319 max_tree_size = tree_size;
|
Chris@16
|
320
|
Chris@101
|
321 if(tree_size > 2 && //Nothing to do with only the root
|
Chris@16
|
322 //Check if the root node is unbalanced
|
Chris@16
|
323 //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1,
|
Chris@16
|
324 //but since "depth" is the depth of the ancestor of x, i == depth
|
Chris@16
|
325 depth > h_alpha(tree_size)){
|
Chris@101
|
326
|
Chris@16
|
327 //Find the first non height-balanced node
|
Chris@16
|
328 //as described in the section 4.2 of the paper.
|
Chris@16
|
329 //This method is the alternative method described
|
Chris@16
|
330 //in the paper. Authors claim that this method
|
Chris@16
|
331 //may tend to yield more balanced trees on the average
|
Chris@16
|
332 //than the weight balanced method.
|
Chris@16
|
333 node_ptr s = x;
|
Chris@16
|
334 std::size_t size = 1;
|
Chris@101
|
335 for(std::size_t ancestor = 1; ancestor != depth; ++ancestor){
|
Chris@101
|
336 const node_ptr s_parent = NodeTraits::get_parent(s);
|
Chris@101
|
337 const node_ptr s_parent_left = NodeTraits::get_left(s_parent);
|
Chris@101
|
338 //Obtain parent's size (previous size + parent + sibling tree)
|
Chris@101
|
339 const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
|
Chris@101
|
340 size += 1 + bstree_algo::subtree_size(s_sibling);
|
Chris@101
|
341 s = s_parent;
|
Chris@101
|
342 if(ancestor > h_alpha(size)){ //is 's' scapegoat?
|
Chris@101
|
343 bstree_algo::rebalance_subtree(s);
|
Chris@101
|
344 return;
|
Chris@16
|
345 }
|
Chris@16
|
346 }
|
Chris@101
|
347 //The whole tree must be rebuilt
|
Chris@101
|
348 max_tree_size = tree_size;
|
Chris@101
|
349 bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
|
Chris@16
|
350 }
|
Chris@16
|
351 }
|
Chris@16
|
352 /// @endcond
|
Chris@16
|
353 };
|
Chris@16
|
354
|
Chris@16
|
355 /// @cond
|
Chris@16
|
356
|
Chris@16
|
357 template<class NodeTraits>
|
Chris@16
|
358 struct get_algo<SgTreeAlgorithms, NodeTraits>
|
Chris@16
|
359 {
|
Chris@16
|
360 typedef sgtree_algorithms<NodeTraits> type;
|
Chris@16
|
361 };
|
Chris@16
|
362
|
Chris@101
|
363 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
|
Chris@101
|
364 struct get_node_checker<SgTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
|
Chris@101
|
365 {
|
Chris@101
|
366 typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
|
Chris@101
|
367 };
|
Chris@101
|
368
|
Chris@16
|
369 /// @endcond
|
Chris@16
|
370
|
Chris@16
|
371 } //namespace intrusive
|
Chris@16
|
372 } //namespace boost
|
Chris@16
|
373
|
Chris@16
|
374 #include <boost/intrusive/detail/config_end.hpp>
|
Chris@16
|
375
|
Chris@16
|
376 #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
|