1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007.
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 // Scapegoat tree algorithms are taken from the paper titled:
14 // "Scapegoat Trees" by Igal Galperin Ronald L. Rivest.
16 /////////////////////////////////////////////////////////////////////////////
17 #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
18 #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
20 #include <boost/intrusive/detail/config_begin.hpp>
23 #include <boost/intrusive/intrusive_fwd.hpp>
24 #include <boost/intrusive/detail/assert.hpp>
25 #include <boost/intrusive/detail/utilities.hpp>
26 #include <boost/intrusive/detail/tree_algorithms.hpp>
32 //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
33 //! information about the node to be manipulated. NodeTraits must support the
34 //! following interface:
38 //! <tt>node</tt>: The type of the node that forms the circular list
40 //! <tt>node_ptr</tt>: A pointer to a node
42 //! <tt>const_node_ptr</tt>: A pointer to a const node
44 //! <b>Static functions</b>:
46 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
48 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
50 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
52 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
54 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
56 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
57 template<class NodeTraits
>
58 class sgtree_algorithms
61 typedef typename
NodeTraits::node node
;
62 typedef NodeTraits node_traits
;
63 typedef typename
NodeTraits::node_ptr node_ptr
;
64 typedef typename
NodeTraits::const_node_ptr const_node_ptr
;
69 typedef detail::tree_algorithms
<NodeTraits
> tree_algorithms
;
71 static node_ptr
uncast(const_node_ptr ptr
)
73 return node_ptr(const_cast<node
*>(::boost::intrusive::detail::get_pointer(ptr
)));
78 static node_ptr
begin_node(const_node_ptr header
)
79 { return tree_algorithms::begin_node(header
); }
81 static node_ptr
end_node(const_node_ptr header
)
82 { return tree_algorithms::end_node(header
); }
84 //! This type is the information that will be
85 //! filled by insert_unique_check
86 struct insert_commit_data
87 : tree_algorithms::insert_commit_data
92 //! <b>Requires</b>: header1 and header2 must be the header nodes
95 //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
96 //! links to the second tree and header2 will have links to the first tree.
98 //! <b>Complexity</b>: Constant.
100 //! <b>Throws</b>: Nothing.
101 static void swap_tree(node_ptr header1
, node_ptr header2
)
102 { return tree_algorithms::swap_tree(header1
, header2
); }
104 //! <b>Requires</b>: node1 and node2 can't be header nodes
107 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
108 //! in the position node2 before the function. node2 will be inserted in the
109 //! position node1 had before the function.
111 //! <b>Complexity</b>: Logarithmic.
113 //! <b>Throws</b>: Nothing.
115 //! <b>Note</b>: This function will break container ordering invariants if
116 //! node1 and node2 are not equivalent according to the ordering rules.
118 //!Experimental function
119 static void swap_nodes(node_ptr node1
, node_ptr node2
)
124 node_ptr
header1(tree_algorithms::get_header(node1
)), header2(tree_algorithms::get_header(node2
));
125 swap_nodes(node1
, header1
, node2
, header2
);
128 //! <b>Requires</b>: node1 and node2 can't be header nodes
129 //! of two trees with header header1 and header2.
131 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
132 //! in the position node2 before the function. node2 will be inserted in the
133 //! position node1 had before the function.
135 //! <b>Complexity</b>: Constant.
137 //! <b>Throws</b>: Nothing.
139 //! <b>Note</b>: This function will break container ordering invariants if
140 //! node1 and node2 are not equivalent according to the ordering rules.
142 //!Experimental function
143 static void swap_nodes(node_ptr node1
, node_ptr header1
, node_ptr node2
, node_ptr header2
)
144 { tree_algorithms::swap_nodes(node1
, header1
, node2
, header2
); }
146 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
147 //! and new_node must not be inserted in a tree.
149 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
150 //! tree with new_node. The tree does not need to be rebalanced
152 //! <b>Complexity</b>: Logarithmic.
154 //! <b>Throws</b>: Nothing.
156 //! <b>Note</b>: This function will break container ordering invariants if
157 //! new_node is not equivalent to node_to_be_replaced according to the
158 //! ordering rules. This function is faster than erasing and inserting
159 //! the node, since no rebalancing and comparison is needed.
161 //!Experimental function
162 static void replace_node(node_ptr node_to_be_replaced
, node_ptr new_node
)
164 if(node_to_be_replaced
== new_node
)
166 replace_node(node_to_be_replaced
, tree_algorithms::get_header(node_to_be_replaced
), new_node
);
169 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
170 //! with header "header" and new_node must not be inserted in a tree.
172 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
173 //! tree with new_node. The tree does not need to be rebalanced
175 //! <b>Complexity</b>: Constant.
177 //! <b>Throws</b>: Nothing.
179 //! <b>Note</b>: This function will break container ordering invariants if
180 //! new_node is not equivalent to node_to_be_replaced according to the
181 //! ordering rules. This function is faster than erasing and inserting
182 //! the node, since no rebalancing or comparison is needed.
184 //!Experimental function
185 static void replace_node(node_ptr node_to_be_replaced
, node_ptr header
, node_ptr new_node
)
186 { tree_algorithms::replace_node(node_to_be_replaced
, header
, new_node
); }
188 //! <b>Requires</b>: node is a tree node but not the header.
190 //! <b>Effects</b>: Unlinks the node and rebalances the tree.
192 //! <b>Complexity</b>: Average complexity is constant time.
194 //! <b>Throws</b>: Nothing.
195 static void unlink(node_ptr node
)
197 node_ptr x
= NodeTraits::get_parent(node
);
200 x
= NodeTraits::get_parent(x
);
201 tree_algorithms::erase(x
, node
);
205 //! <b>Requires</b>: header is the header of a tree.
207 //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
208 //! updates the header link to the new leftmost node.
210 //! <b>Complexity</b>: Average complexity is constant time.
212 //! <b>Throws</b>: Nothing.
214 //! <b>Notes</b>: This function breaks the tree and the tree can
215 //! only be used for more unlink_leftmost_without_rebalance calls.
216 //! This function is normally used to achieve a step by step
217 //! controlled destruction of the tree.
218 static node_ptr
unlink_leftmost_without_rebalance(node_ptr header
)
219 { return tree_algorithms::unlink_leftmost_without_rebalance(header
); }
221 //! <b>Requires</b>: node is a node of the tree or an node initialized
224 //! <b>Effects</b>: Returns true if the node is initialized by init().
226 //! <b>Complexity</b>: Constant time.
228 //! <b>Throws</b>: Nothing.
229 static bool unique(const_node_ptr node
)
230 { return tree_algorithms::unique(node
); }
232 //! <b>Requires</b>: node is a node of the tree but it's not the header.
234 //! <b>Effects</b>: Returns the number of nodes of the subtree.
236 //! <b>Complexity</b>: Linear time.
238 //! <b>Throws</b>: Nothing.
239 static std::size_t count(const_node_ptr node
)
240 { return tree_algorithms::count(node
); }
242 //! <b>Requires</b>: header is the header node of the tree.
244 //! <b>Effects</b>: Returns the number of nodes above the header.
246 //! <b>Complexity</b>: Linear time.
248 //! <b>Throws</b>: Nothing.
249 static std::size_t size(const_node_ptr header
)
250 { return tree_algorithms::size(header
); }
252 //! <b>Requires</b>: p is a node from the tree except the header.
254 //! <b>Effects</b>: Returns the next node of the tree.
256 //! <b>Complexity</b>: Average constant time.
258 //! <b>Throws</b>: Nothing.
259 static node_ptr
next_node(node_ptr p
)
260 { return tree_algorithms::next_node(p
); }
262 //! <b>Requires</b>: p is a node from the tree except the leftmost node.
264 //! <b>Effects</b>: Returns the previous node of the tree.
266 //! <b>Complexity</b>: Average constant time.
268 //! <b>Throws</b>: Nothing.
269 static node_ptr
prev_node(node_ptr p
)
270 { return tree_algorithms::prev_node(p
); }
272 //! <b>Requires</b>: node must not be part of any tree.
274 //! <b>Effects</b>: After the function unique(node) == true.
276 //! <b>Complexity</b>: Constant.
278 //! <b>Throws</b>: Nothing.
280 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
281 static void init(node_ptr node
)
282 { tree_algorithms::init(node
); }
284 //! <b>Requires</b>: node must not be part of any tree.
286 //! <b>Effects</b>: Initializes the header to represent an empty tree.
287 //! unique(header) == true.
289 //! <b>Complexity</b>: Constant.
291 //! <b>Throws</b>: Nothing.
293 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
294 static void init_header(node_ptr header
)
295 { tree_algorithms::init_header(header
); }
297 //! <b>Requires</b>: header must be the header of a tree, z a node
298 //! of that tree and z != header.
300 //! <b>Effects</b>: Erases node "z" from the tree with header "header".
302 //! <b>Complexity</b>: Amortized constant time.
304 //! <b>Throws</b>: Nothing.
305 template<class AlphaByMaxSize
>
306 static node_ptr
erase(node_ptr header
, node_ptr z
, std::size_t tree_size
, std::size_t &max_tree_size
, AlphaByMaxSize alpha_by_maxsize
)
308 //typename tree_algorithms::data_for_rebalance info;
309 tree_algorithms::erase(header
, z
);
312 tree_size
< alpha_by_maxsize(max_tree_size
)){
313 tree_algorithms::rebalance(header
);
314 max_tree_size
= tree_size
;
319 //! <b>Requires</b>: "cloner" must be a function
320 //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
321 //! take a node_ptr and shouldn't throw.
323 //! <b>Effects</b>: First empties target tree calling
324 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
325 //! except the header.
327 //! Then, duplicates the entire tree pointed by "source_header" cloning each
328 //! source node with <tt>node_ptr Cloner::operator()(node_ptr)</tt> to obtain
329 //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
330 //! are disposed using <tt>void disposer(node_ptr)</tt>.
332 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
333 //! number of elements of tree target tree when calling this function.
335 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
336 template <class Cloner
, class Disposer
>
338 (const_node_ptr source_header
, node_ptr target_header
, Cloner cloner
, Disposer disposer
)
340 tree_algorithms::clone(source_header
, target_header
, cloner
, disposer
);
343 //! <b>Requires</b>: "disposer" must be an object function
344 //! taking a node_ptr parameter and shouldn't throw.
346 //! <b>Effects</b>: Empties the target tree calling
347 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
348 //! except the header.
350 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
351 //! number of elements of tree target tree when calling this function.
353 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
354 template<class Disposer
>
355 static void clear_and_dispose(node_ptr header
, Disposer disposer
)
356 { tree_algorithms::clear_and_dispose(header
, disposer
); }
358 //! <b>Requires</b>: "header" must be the header node of a tree.
359 //! KeyNodePtrCompare is a function object that induces a strict weak
360 //! ordering compatible with the strict weak ordering used to create the
361 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
363 //! <b>Effects</b>: Returns an node_ptr to the first element that is
364 //! not less than "key" according to "comp" or "header" if that element does
367 //! <b>Complexity</b>: Logarithmic.
369 //! <b>Throws</b>: If "comp" throws.
370 template<class KeyType
, class KeyNodePtrCompare
>
371 static node_ptr lower_bound
372 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
373 { return tree_algorithms::lower_bound(header
, key
, comp
); }
375 //! <b>Requires</b>: "header" must be the header node of a tree.
376 //! KeyNodePtrCompare is a function object that induces a strict weak
377 //! ordering compatible with the strict weak ordering used to create the
378 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
380 //! <b>Effects</b>: Returns an node_ptr to the first element that is greater
381 //! than "key" according to "comp" or "header" if that element does not exist.
383 //! <b>Complexity</b>: Logarithmic.
385 //! <b>Throws</b>: If "comp" throws.
386 template<class KeyType
, class KeyNodePtrCompare
>
387 static node_ptr upper_bound
388 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
389 { return tree_algorithms::upper_bound(header
, key
, comp
); }
391 //! <b>Requires</b>: "header" must be the header node of a tree.
392 //! KeyNodePtrCompare is a function object that induces a strict weak
393 //! ordering compatible with the strict weak ordering used to create the
394 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
396 //! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
397 //! "key" according to "comp" or "header" if that element does not exist.
399 //! <b>Complexity</b>: Logarithmic.
401 //! <b>Throws</b>: If "comp" throws.
402 template<class KeyType
, class KeyNodePtrCompare
>
404 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
405 { return tree_algorithms::find(header
, key
, comp
); }
407 //! <b>Requires</b>: "header" must be the header node of a tree.
408 //! KeyNodePtrCompare is a function object that induces a strict weak
409 //! ordering compatible with the strict weak ordering used to create the
410 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
412 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
413 //! all elements that are equivalent to "key" according to "comp" or an
414 //! empty range that indicates the position where those elements would be
415 //! if they there are no equivalent elements.
417 //! <b>Complexity</b>: Logarithmic.
419 //! <b>Throws</b>: If "comp" throws.
420 template<class KeyType
, class KeyNodePtrCompare
>
421 static std::pair
<node_ptr
, node_ptr
> equal_range
422 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
423 { return tree_algorithms::equal_range(header
, key
, comp
); }
425 //! <b>Requires</b>: "h" must be the header node of a tree.
426 //! NodePtrCompare is a function object that induces a strict weak
427 //! ordering compatible with the strict weak ordering used to create the
428 //! the tree. NodePtrCompare compares two node_ptrs.
430 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
431 //! according to "comp".
433 //! <b>Complexity</b>: Average complexity for insert element is at
434 //! most logarithmic.
436 //! <b>Throws</b>: If "comp" throws.
437 template<class NodePtrCompare
, class H_Alpha
>
438 static node_ptr insert_equal_upper_bound
439 (node_ptr h
, node_ptr new_node
, NodePtrCompare comp
440 ,std::size_t tree_size
, H_Alpha h_alpha
, std::size_t &max_tree_size
)
443 tree_algorithms::insert_equal_upper_bound(h
, new_node
, comp
, &depth
);
444 rebalance_after_insertion(new_node
, depth
, tree_size
+1, h_alpha
, max_tree_size
);
448 //! <b>Requires</b>: "h" must be the header node of a tree.
449 //! NodePtrCompare is a function object that induces a strict weak
450 //! ordering compatible with the strict weak ordering used to create the
451 //! the tree. NodePtrCompare compares two node_ptrs.
453 //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
454 //! according to "comp".
456 //! <b>Complexity</b>: Average complexity for insert element is at
457 //! most logarithmic.
459 //! <b>Throws</b>: If "comp" throws.
460 template<class NodePtrCompare
, class H_Alpha
>
461 static node_ptr insert_equal_lower_bound
462 (node_ptr h
, node_ptr new_node
, NodePtrCompare comp
463 ,std::size_t tree_size
, H_Alpha h_alpha
, std::size_t &max_tree_size
)
466 tree_algorithms::insert_equal_lower_bound(h
, new_node
, comp
, &depth
);
467 rebalance_after_insertion(new_node
, depth
, tree_size
+1, h_alpha
, max_tree_size
);
471 //! <b>Requires</b>: "header" must be the header node of a tree.
472 //! NodePtrCompare is a function object that induces a strict weak
473 //! ordering compatible with the strict weak ordering used to create the
474 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
475 //! the "header"'s tree.
477 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
478 //! where it will be inserted. If "hint" is the upper_bound
479 //! the insertion takes constant time (two comparisons in the worst case).
481 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
482 //! constant time if new_node is inserted immediately before "hint".
484 //! <b>Throws</b>: If "comp" throws.
485 template<class NodePtrCompare
, class H_Alpha
>
486 static node_ptr insert_equal
487 (node_ptr header
, node_ptr hint
, node_ptr new_node
, NodePtrCompare comp
488 ,std::size_t tree_size
, H_Alpha h_alpha
, std::size_t &max_tree_size
)
491 tree_algorithms::insert_equal(header
, hint
, new_node
, comp
, &depth
);
492 rebalance_after_insertion(new_node
, depth
, tree_size
+1, h_alpha
, max_tree_size
);
496 //! <b>Requires</b>: "header" must be the header node of a tree.
497 //! KeyNodePtrCompare is a function object that induces a strict weak
498 //! ordering compatible with the strict weak ordering used to create the
499 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
501 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
502 //! tree according to "comp" and obtains the needed information to realize
503 //! a constant-time node insertion if there is no equivalent node.
505 //! <b>Returns</b>: If there is an equivalent value
506 //! returns a pair containing a node_ptr to the already present node
507 //! and false. If there is not equivalent key can be inserted returns true
508 //! in the returned pair's boolean and fills "commit_data" that is meant to
509 //! be used with the "insert_commit" function to achieve a constant-time
510 //! insertion function.
512 //! <b>Complexity</b>: Average complexity is at most logarithmic.
514 //! <b>Throws</b>: If "comp" throws.
516 //! <b>Notes</b>: This function is used to improve performance when constructing
517 //! a node is expensive and the user does not want to have two equivalent nodes
518 //! in the tree: if there is an equivalent value
519 //! the constructed object must be discarded. Many times, the part of the
520 //! node that is used to impose the order is much cheaper to construct
521 //! than the node and this function offers the possibility to use that part
522 //! to check if the insertion will be successful.
524 //! If the check is successful, the user can construct the node and use
525 //! "insert_commit" to insert the node in constant-time. This gives a total
526 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
528 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
529 //! if no more objects are inserted or erased from the set.
530 template<class KeyType
, class KeyNodePtrCompare
>
531 static std::pair
<node_ptr
, bool> insert_unique_check
532 (const_node_ptr header
, const KeyType
&key
533 ,KeyNodePtrCompare comp
, insert_commit_data
&commit_data
)
536 std::pair
<node_ptr
, bool> ret
=
537 tree_algorithms::insert_unique_check(header
, key
, comp
, commit_data
, &depth
);
538 commit_data
.depth
= depth
;
542 //! <b>Requires</b>: "header" must be the header node of a tree.
543 //! KeyNodePtrCompare is a function object that induces a strict weak
544 //! ordering compatible with the strict weak ordering used to create the
545 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
546 //! "hint" is node from the "header"'s tree.
548 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
549 //! tree according to "comp" using "hint" as a hint to where it should be
550 //! inserted and obtains the needed information to realize
551 //! a constant-time node insertion if there is no equivalent node.
552 //! If "hint" is the upper_bound the function has constant time
553 //! complexity (two comparisons in the worst case).
555 //! <b>Returns</b>: If there is an equivalent value
556 //! returns a pair containing a node_ptr to the already present node
557 //! and false. If there is not equivalent key can be inserted returns true
558 //! in the returned pair's boolean and fills "commit_data" that is meant to
559 //! be used with the "insert_commit" function to achieve a constant-time
560 //! insertion function.
562 //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
563 //! amortized constant time if new_node should be inserted immediately before "hint".
565 //! <b>Throws</b>: If "comp" throws.
567 //! <b>Notes</b>: This function is used to improve performance when constructing
568 //! a node is expensive and the user does not want to have two equivalent nodes
569 //! in the tree: if there is an equivalent value
570 //! the constructed object must be discarded. Many times, the part of the
571 //! node that is used to impose the order is much cheaper to construct
572 //! than the node and this function offers the possibility to use that part
573 //! to check if the insertion will be successful.
575 //! If the check is successful, the user can construct the node and use
576 //! "insert_commit" to insert the node in constant-time. This gives a total
577 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
579 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
580 //! if no more objects are inserted or erased from the set.
581 template<class KeyType
, class KeyNodePtrCompare
>
582 static std::pair
<node_ptr
, bool> insert_unique_check
583 (const_node_ptr header
, node_ptr hint
, const KeyType
&key
584 ,KeyNodePtrCompare comp
, insert_commit_data
&commit_data
)
587 std::pair
<node_ptr
, bool> ret
=
588 tree_algorithms::insert_unique_check
589 (header
, hint
, key
, comp
, commit_data
, &depth
);
590 commit_data
.depth
= depth
;
594 //! <b>Requires</b>: "header" must be the header node of a tree.
595 //! "commit_data" must have been obtained from a previous call to
596 //! "insert_unique_check". No objects should have been inserted or erased
597 //! from the set between the "insert_unique_check" that filled "commit_data"
598 //! and the call to "insert_commit".
601 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
602 //! from the "commit_data" that a previous "insert_check" filled.
604 //! <b>Complexity</b>: Constant time.
606 //! <b>Throws</b>: Nothing.
608 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
609 //! previously executed to fill "commit_data". No value should be inserted or
610 //! erased between the "insert_check" and "insert_commit" calls.
611 template<class H_Alpha
>
612 static void insert_unique_commit
613 (node_ptr header
, node_ptr new_value
, const insert_commit_data
&commit_data
614 ,std::size_t tree_size
, H_Alpha h_alpha
, std::size_t &max_tree_size
)
616 tree_algorithms::insert_unique_commit(header
, new_value
, commit_data
);
617 rebalance_after_insertion(new_value
, commit_data
.depth
, tree_size
+1, h_alpha
, max_tree_size
);
620 //! <b>Requires</b>: header must be the header of a tree.
622 //! <b>Effects</b>: Rebalances the tree.
624 //! <b>Throws</b>: Nothing.
626 //! <b>Complexity</b>: Linear.
627 static void rebalance(node_ptr header
)
628 { tree_algorithms::rebalance(header
); }
630 //! <b>Requires</b>: old_root is a node of a tree.
632 //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
634 //! <b>Returns</b>: The new root of the subtree.
636 //! <b>Throws</b>: Nothing.
638 //! <b>Complexity</b>: Linear.
639 static node_ptr
rebalance_subtree(node_ptr old_root
)
640 { return tree_algorithms::rebalance_subtree(old_root
); }
642 //! <b>Requires</b>: "n" must be a node inserted in a tree.
644 //! <b>Effects</b>: Returns a pointer to the header node of the tree.
646 //! <b>Complexity</b>: Logarithmic.
648 //! <b>Throws</b>: Nothing.
649 static node_ptr
get_header(node_ptr n
)
650 { return tree_algorithms::get_header(n
); }
655 //! <b>Requires</b>: p is a node of a tree.
657 //! <b>Effects</b>: Returns true if p is the header of the tree.
659 //! <b>Complexity</b>: Constant.
661 //! <b>Throws</b>: Nothing.
662 static bool is_header(const_node_ptr p
)
663 { return tree_algorithms::is_header(p
); }
665 template<class H_Alpha
>
666 static void rebalance_after_insertion
667 ( node_ptr x
, std::size_t depth
668 , std::size_t tree_size
, H_Alpha h_alpha
, std::size_t &max_tree_size
)
670 if(tree_size
> max_tree_size
)
671 max_tree_size
= tree_size
;
673 if(tree_size
!= 1 && depth
> h_alpha(tree_size
)){
674 //Find the first non height-balanced node
675 //as described in the section 4.2 of the paper.
676 //This method is the alternative method described
677 //in the paper. Authors claim that this method
678 //may tend to yield more balanced trees on the average
679 //than the weight balanced method.
681 std::size_t size
= 1;
683 for(std::size_t i
= 1; true; ++i
){
684 bool rebalance
= false;
686 BOOST_INTRUSIVE_INVARIANT_ASSERT(tree_size
== count(s
));
689 else if(i
> h_alpha(size
)){
690 node_ptr s_parent
= NodeTraits::get_parent(s
);
691 node_ptr s_parent_left
= NodeTraits::get_left(s_parent
);
692 size
+= 1 + tree_algorithms::count
693 ( s_parent_left
== s
? NodeTraits::get_right(s_parent
) : s_parent_left
);
698 rebalance_subtree(s
);
708 } //namespace intrusive
711 #include <boost/intrusive/detail/config_end.hpp>
713 #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP