1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Daniel K. O. 2005.
4 // (C) Copyright Ion Gaztanaga 2007.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // See http://www.boost.org/libs/intrusive for documentation.
12 /////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
15 #define BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
17 #include <boost/intrusive/detail/config_begin.hpp>
20 #include <boost/intrusive/intrusive_fwd.hpp>
22 #include <boost/intrusive/detail/assert.hpp>
23 #include <boost/intrusive/detail/utilities.hpp>
24 #include <boost/intrusive/detail/tree_algorithms.hpp>
30 //! avltree_algorithms is configured with a NodeTraits class, which encapsulates the
31 //! information about the node to be manipulated. NodeTraits must support the
32 //! following interface:
36 //! <tt>node</tt>: The type of the node that forms the circular list
38 //! <tt>node_ptr</tt>: A pointer to a node
40 //! <tt>const_node_ptr</tt>: A pointer to a const node
42 //! <tt>balance</tt>: The type of the balance factor
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>
58 //! <tt>static balance get_balance(const_node_ptr n);</tt>
60 //! <tt>static void set_balance(node_ptr n, balance b);</tt>
62 //! <tt>static balance negative();</tt>
64 //! <tt>static balance zero();</tt>
66 //! <tt>static balance positive();</tt>
67 template<class NodeTraits
>
68 class avltree_algorithms
71 typedef typename
NodeTraits::node node
;
72 typedef NodeTraits node_traits
;
73 typedef typename
NodeTraits::node_ptr node_ptr
;
74 typedef typename
NodeTraits::const_node_ptr const_node_ptr
;
75 typedef typename
NodeTraits::balance balance
;
79 typedef detail::tree_algorithms
<NodeTraits
> tree_algorithms
;
82 struct avltree_node_cloner
83 : private detail::ebo_functor_holder
<F
>
85 typedef detail::ebo_functor_holder
<F
> base_t
;
87 avltree_node_cloner(F f
)
91 node_ptr
operator()(node_ptr p
)
93 node_ptr n
= base_t::get()(p
);
94 NodeTraits::set_balance(n
, NodeTraits::get_balance(p
));
99 struct avltree_erase_fixup
101 void operator()(node_ptr to_erase
, node_ptr successor
)
102 { NodeTraits::set_balance(successor
, NodeTraits::get_balance(to_erase
)); }
105 static node_ptr
uncast(const_node_ptr ptr
)
107 return node_ptr(const_cast<node
*>(::boost::intrusive::detail::get_pointer(ptr
)));
112 static node_ptr
begin_node(const_node_ptr header
)
113 { return tree_algorithms::begin_node(header
); }
115 static node_ptr
end_node(const_node_ptr header
)
116 { return tree_algorithms::end_node(header
); }
118 //! This type is the information that will be
119 //! filled by insert_unique_check
120 typedef typename
tree_algorithms::insert_commit_data insert_commit_data
;
122 //! <b>Requires</b>: header1 and header2 must be the header nodes
125 //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
126 //! links to the second tree and header2 will have links to the first tree.
128 //! <b>Complexity</b>: Constant.
130 //! <b>Throws</b>: Nothing.
131 static void swap_tree(node_ptr header1
, node_ptr header2
)
132 { return tree_algorithms::swap_tree(header1
, header2
); }
134 //! <b>Requires</b>: node1 and node2 can't be header nodes
137 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
138 //! in the position node2 before the function. node2 will be inserted in the
139 //! position node1 had before the function.
141 //! <b>Complexity</b>: Logarithmic.
143 //! <b>Throws</b>: Nothing.
145 //! <b>Note</b>: This function will break container ordering invariants if
146 //! node1 and node2 are not equivalent according to the ordering rules.
148 //!Experimental function
149 static void swap_nodes(node_ptr node1
, node_ptr node2
)
154 node_ptr
header1(tree_algorithms::get_header(node1
)), header2(tree_algorithms::get_header(node2
));
155 swap_nodes(node1
, header1
, node2
, header2
);
158 //! <b>Requires</b>: node1 and node2 can't be header nodes
159 //! of two trees with header header1 and header2.
161 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
162 //! in the position node2 before the function. node2 will be inserted in the
163 //! position node1 had before the function.
165 //! <b>Complexity</b>: Constant.
167 //! <b>Throws</b>: Nothing.
169 //! <b>Note</b>: This function will break container ordering invariants if
170 //! node1 and node2 are not equivalent according to the ordering rules.
172 //!Experimental function
173 static void swap_nodes(node_ptr node1
, node_ptr header1
, node_ptr node2
, node_ptr header2
)
175 if(node1
== node2
) return;
177 tree_algorithms::swap_nodes(node1
, header1
, node2
, header2
);
179 balance c
= NodeTraits::get_balance(node1
);
180 NodeTraits::set_balance(node1
, NodeTraits::get_balance(node2
));
181 NodeTraits::set_balance(node2
, c
);
184 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
185 //! and new_node must not be inserted in a tree.
187 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
188 //! tree with new_node. The tree does not need to be rebalanced
190 //! <b>Complexity</b>: Logarithmic.
192 //! <b>Throws</b>: Nothing.
194 //! <b>Note</b>: This function will break container ordering invariants if
195 //! new_node is not equivalent to node_to_be_replaced according to the
196 //! ordering rules. This function is faster than erasing and inserting
197 //! the node, since no rebalancing and comparison is needed.
199 //!Experimental function
200 static void replace_node(node_ptr node_to_be_replaced
, node_ptr new_node
)
202 if(node_to_be_replaced
== new_node
)
204 replace_node(node_to_be_replaced
, tree_algorithms::get_header(node_to_be_replaced
), new_node
);
207 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
208 //! with header "header" and new_node must not be inserted in a tree.
210 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
211 //! tree with new_node. The tree does not need to be rebalanced
213 //! <b>Complexity</b>: Constant.
215 //! <b>Throws</b>: Nothing.
217 //! <b>Note</b>: This function will break container ordering invariants if
218 //! new_node is not equivalent to node_to_be_replaced according to the
219 //! ordering rules. This function is faster than erasing and inserting
220 //! the node, since no rebalancing or comparison is needed.
222 //!Experimental function
223 static void replace_node(node_ptr node_to_be_replaced
, node_ptr header
, node_ptr new_node
)
225 tree_algorithms::replace_node(node_to_be_replaced
, header
, new_node
);
226 NodeTraits::set_balance(new_node
, NodeTraits::get_balance(node_to_be_replaced
));
229 //! <b>Requires</b>: node is a tree node but not the header.
231 //! <b>Effects</b>: Unlinks the node and rebalances the tree.
233 //! <b>Complexity</b>: Average complexity is constant time.
235 //! <b>Throws</b>: Nothing.
236 static void unlink(node_ptr node
)
238 node_ptr x
= NodeTraits::get_parent(node
);
241 x
= NodeTraits::get_parent(x
);
246 //! <b>Requires</b>: header is the header of a tree.
248 //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
249 //! updates the header link to the new leftmost node.
251 //! <b>Complexity</b>: Average complexity is constant time.
253 //! <b>Throws</b>: Nothing.
255 //! <b>Notes</b>: This function breaks the tree and the tree can
256 //! only be used for more unlink_leftmost_without_rebalance calls.
257 //! This function is normally used to achieve a step by step
258 //! controlled destruction of the tree.
259 static node_ptr
unlink_leftmost_without_rebalance(node_ptr header
)
260 { return tree_algorithms::unlink_leftmost_without_rebalance(header
); }
262 //! <b>Requires</b>: node is a node of the tree or an node initialized
265 //! <b>Effects</b>: Returns true if the node is initialized by init().
267 //! <b>Complexity</b>: Constant time.
269 //! <b>Throws</b>: Nothing.
270 static bool unique(const_node_ptr node
)
271 { return tree_algorithms::unique(node
); }
273 //! <b>Requires</b>: node is a node of the tree but it's not the header.
275 //! <b>Effects</b>: Returns the number of nodes of the subtree.
277 //! <b>Complexity</b>: Linear time.
279 //! <b>Throws</b>: Nothing.
280 static std::size_t count(const_node_ptr node
)
281 { return tree_algorithms::count(node
); }
283 //! <b>Requires</b>: header is the header node of the tree.
285 //! <b>Effects</b>: Returns the number of nodes above the header.
287 //! <b>Complexity</b>: Linear time.
289 //! <b>Throws</b>: Nothing.
290 static std::size_t size(const_node_ptr header
)
291 { return tree_algorithms::size(header
); }
293 //! <b>Requires</b>: p is a node from the tree except the header.
295 //! <b>Effects</b>: Returns the next node of the tree.
297 //! <b>Complexity</b>: Average constant time.
299 //! <b>Throws</b>: Nothing.
300 static node_ptr
next_node(node_ptr p
)
301 { return tree_algorithms::next_node(p
); }
303 //! <b>Requires</b>: p is a node from the tree except the leftmost node.
305 //! <b>Effects</b>: Returns the previous node of the tree.
307 //! <b>Complexity</b>: Average constant time.
309 //! <b>Throws</b>: Nothing.
310 static node_ptr
prev_node(node_ptr p
)
311 { return tree_algorithms::prev_node(p
); }
313 //! <b>Requires</b>: node must not be part of any tree.
315 //! <b>Effects</b>: After the function unique(node) == true.
317 //! <b>Complexity</b>: Constant.
319 //! <b>Throws</b>: Nothing.
321 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
322 static void init(node_ptr node
)
323 { tree_algorithms::init(node
); }
325 //! <b>Requires</b>: node must not be part of any tree.
327 //! <b>Effects</b>: Initializes the header to represent an empty tree.
328 //! unique(header) == true.
330 //! <b>Complexity</b>: Constant.
332 //! <b>Throws</b>: Nothing.
334 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
335 static void init_header(node_ptr header
)
337 tree_algorithms::init_header(header
);
338 NodeTraits::set_balance(header
, NodeTraits::zero());
341 //! <b>Requires</b>: header must be the header of a tree, z a node
342 //! of that tree and z != header.
344 //! <b>Effects</b>: Erases node "z" from the tree with header "header".
346 //! <b>Complexity</b>: Amortized constant time.
348 //! <b>Throws</b>: Nothing.
349 static node_ptr
erase(node_ptr header
, node_ptr z
)
351 typename
tree_algorithms::data_for_rebalance info
;
352 tree_algorithms::erase(header
, z
, avltree_erase_fixup(), info
);
354 node_ptr x_parent
= info
.x_parent
;
357 rebalance_after_erasure(header
, x
, x_parent
);
361 //! <b>Requires</b>: "cloner" must be a function
362 //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
363 //! take a node_ptr and shouldn't throw.
365 //! <b>Effects</b>: First empties target tree calling
366 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
367 //! except the header.
369 //! Then, duplicates the entire tree pointed by "source_header" cloning each
370 //! source node with <tt>node_ptr Cloner::operator()(node_ptr)</tt> to obtain
371 //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
372 //! are disposed using <tt>void disposer(node_ptr)</tt>.
374 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
375 //! number of elements of tree target tree when calling this function.
377 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
378 template <class Cloner
, class Disposer
>
380 (const_node_ptr source_header
, node_ptr target_header
, Cloner cloner
, Disposer disposer
)
382 avltree_node_cloner
<Cloner
> new_cloner(cloner
);
383 tree_algorithms::clone(source_header
, target_header
, new_cloner
, disposer
);
386 //! <b>Requires</b>: "disposer" must be an object function
387 //! taking a node_ptr parameter and shouldn't throw.
389 //! <b>Effects</b>: Empties the target tree calling
390 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
391 //! except the header.
393 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
394 //! number of elements of tree target tree when calling this function.
396 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
397 template<class Disposer
>
398 static void clear_and_dispose(node_ptr header
, Disposer disposer
)
399 { tree_algorithms::clear_and_dispose(header
, disposer
); }
401 //! <b>Requires</b>: "header" must be the header node of a tree.
402 //! KeyNodePtrCompare is a function object that induces a strict weak
403 //! ordering compatible with the strict weak ordering used to create the
404 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
406 //! <b>Effects</b>: Returns an node_ptr to the first element that is
407 //! not less than "key" according to "comp" or "header" if that element does
410 //! <b>Complexity</b>: Logarithmic.
412 //! <b>Throws</b>: If "comp" throws.
413 template<class KeyType
, class KeyNodePtrCompare
>
414 static node_ptr lower_bound
415 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
416 { return tree_algorithms::lower_bound(header
, key
, comp
); }
418 //! <b>Requires</b>: "header" must be the header node of a tree.
419 //! KeyNodePtrCompare is a function object that induces a strict weak
420 //! ordering compatible with the strict weak ordering used to create the
421 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
423 //! <b>Effects</b>: Returns an node_ptr to the first element that is greater
424 //! than "key" according to "comp" or "header" if that element does not exist.
426 //! <b>Complexity</b>: Logarithmic.
428 //! <b>Throws</b>: If "comp" throws.
429 template<class KeyType
, class KeyNodePtrCompare
>
430 static node_ptr upper_bound
431 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
432 { return tree_algorithms::upper_bound(header
, key
, comp
); }
434 //! <b>Requires</b>: "header" must be the header node of a tree.
435 //! KeyNodePtrCompare is a function object that induces a strict weak
436 //! ordering compatible with the strict weak ordering used to create the
437 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
439 //! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
440 //! "key" according to "comp" or "header" if that element does not exist.
442 //! <b>Complexity</b>: Logarithmic.
444 //! <b>Throws</b>: If "comp" throws.
445 template<class KeyType
, class KeyNodePtrCompare
>
447 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
448 { return tree_algorithms::find(header
, key
, comp
); }
450 //! <b>Requires</b>: "header" must be the header node of a tree.
451 //! KeyNodePtrCompare is a function object that induces a strict weak
452 //! ordering compatible with the strict weak ordering used to create the
453 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
455 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
456 //! all elements that are equivalent to "key" according to "comp" or an
457 //! empty range that indicates the position where those elements would be
458 //! if they there are no equivalent elements.
460 //! <b>Complexity</b>: Logarithmic.
462 //! <b>Throws</b>: If "comp" throws.
463 template<class KeyType
, class KeyNodePtrCompare
>
464 static std::pair
<node_ptr
, node_ptr
> equal_range
465 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
466 { return tree_algorithms::equal_range(header
, key
, comp
); }
468 //! <b>Requires</b>: "h" must be the header node of a tree.
469 //! NodePtrCompare is a function object that induces a strict weak
470 //! ordering compatible with the strict weak ordering used to create the
471 //! the tree. NodePtrCompare compares two node_ptrs.
473 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
474 //! according to "comp".
476 //! <b>Complexity</b>: Average complexity for insert element is at
477 //! most logarithmic.
479 //! <b>Throws</b>: If "comp" throws.
480 template<class NodePtrCompare
>
481 static node_ptr insert_equal_upper_bound
482 (node_ptr h
, node_ptr new_node
, NodePtrCompare comp
)
484 tree_algorithms::insert_equal_upper_bound(h
, new_node
, comp
);
485 rebalance_after_insertion(h
, new_node
);
489 //! <b>Requires</b>: "h" must be the header node of a tree.
490 //! NodePtrCompare is a function object that induces a strict weak
491 //! ordering compatible with the strict weak ordering used to create the
492 //! the tree. NodePtrCompare compares two node_ptrs.
494 //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
495 //! according to "comp".
497 //! <b>Complexity</b>: Average complexity for insert element is at
498 //! most logarithmic.
500 //! <b>Throws</b>: If "comp" throws.
501 template<class NodePtrCompare
>
502 static node_ptr insert_equal_lower_bound
503 (node_ptr h
, node_ptr new_node
, NodePtrCompare comp
)
505 tree_algorithms::insert_equal_lower_bound(h
, new_node
, comp
);
506 rebalance_after_insertion(h
, new_node
);
510 //! <b>Requires</b>: "header" must be the header node of a tree.
511 //! NodePtrCompare is a function object that induces a strict weak
512 //! ordering compatible with the strict weak ordering used to create the
513 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
514 //! the "header"'s tree.
516 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
517 //! where it will be inserted. If "hint" is the upper_bound
518 //! the insertion takes constant time (two comparisons in the worst case).
520 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
521 //! constant time if new_node is inserted immediately before "hint".
523 //! <b>Throws</b>: If "comp" throws.
524 template<class NodePtrCompare
>
525 static node_ptr insert_equal
526 (node_ptr header
, node_ptr hint
, node_ptr new_node
, NodePtrCompare comp
)
528 tree_algorithms::insert_equal(header
, hint
, new_node
, comp
);
529 rebalance_after_insertion(header
, new_node
);
533 //! <b>Requires</b>: "header" must be the header node of a tree.
534 //! KeyNodePtrCompare is a function object that induces a strict weak
535 //! ordering compatible with the strict weak ordering used to create the
536 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
538 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
539 //! tree according to "comp" and obtains the needed information to realize
540 //! a constant-time node insertion if there is no equivalent node.
542 //! <b>Returns</b>: If there is an equivalent value
543 //! returns a pair containing a node_ptr to the already present node
544 //! and false. If there is not equivalent key can be inserted returns true
545 //! in the returned pair's boolean and fills "commit_data" that is meant to
546 //! be used with the "insert_commit" function to achieve a constant-time
547 //! insertion function.
549 //! <b>Complexity</b>: Average complexity is at most logarithmic.
551 //! <b>Throws</b>: If "comp" throws.
553 //! <b>Notes</b>: This function is used to improve performance when constructing
554 //! a node is expensive and the user does not want to have two equivalent nodes
555 //! in the tree: if there is an equivalent value
556 //! the constructed object must be discarded. Many times, the part of the
557 //! node that is used to impose the order is much cheaper to construct
558 //! than the node and this function offers the possibility to use that part
559 //! to check if the insertion will be successful.
561 //! If the check is successful, the user can construct the node and use
562 //! "insert_commit" to insert the node in constant-time. This gives a total
563 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
565 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
566 //! if no more objects are inserted or erased from the set.
567 template<class KeyType
, class KeyNodePtrCompare
>
568 static std::pair
<node_ptr
, bool> insert_unique_check
569 (const_node_ptr header
, const KeyType
&key
570 ,KeyNodePtrCompare comp
, insert_commit_data
&commit_data
)
571 { return tree_algorithms::insert_unique_check(header
, key
, comp
, commit_data
); }
573 //! <b>Requires</b>: "header" must be the header node of a tree.
574 //! KeyNodePtrCompare is a function object that induces a strict weak
575 //! ordering compatible with the strict weak ordering used to create the
576 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
577 //! "hint" is node from the "header"'s tree.
579 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
580 //! tree according to "comp" using "hint" as a hint to where it should be
581 //! inserted and obtains the needed information to realize
582 //! a constant-time node insertion if there is no equivalent node.
583 //! If "hint" is the upper_bound the function has constant time
584 //! complexity (two comparisons in the worst case).
586 //! <b>Returns</b>: If there is an equivalent value
587 //! returns a pair containing a node_ptr to the already present node
588 //! and false. If there is not equivalent key can be inserted returns true
589 //! in the returned pair's boolean and fills "commit_data" that is meant to
590 //! be used with the "insert_commit" function to achieve a constant-time
591 //! insertion function.
593 //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
594 //! amortized constant time if new_node should be inserted immediately before "hint".
596 //! <b>Throws</b>: If "comp" throws.
598 //! <b>Notes</b>: This function is used to improve performance when constructing
599 //! a node is expensive and the user does not want to have two equivalent nodes
600 //! in the tree: if there is an equivalent value
601 //! the constructed object must be discarded. Many times, the part of the
602 //! node that is used to impose the order is much cheaper to construct
603 //! than the node and this function offers the possibility to use that part
604 //! to check if the insertion will be successful.
606 //! If the check is successful, the user can construct the node and use
607 //! "insert_commit" to insert the node in constant-time. This gives a total
608 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
610 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
611 //! if no more objects are inserted or erased from the set.
612 template<class KeyType
, class KeyNodePtrCompare
>
613 static std::pair
<node_ptr
, bool> insert_unique_check
614 (const_node_ptr header
, node_ptr hint
, const KeyType
&key
615 ,KeyNodePtrCompare comp
, insert_commit_data
&commit_data
)
616 { return tree_algorithms::insert_unique_check(header
, hint
, key
, comp
, commit_data
); }
618 //! <b>Requires</b>: "header" must be the header node of a tree.
619 //! "commit_data" must have been obtained from a previous call to
620 //! "insert_unique_check". No objects should have been inserted or erased
621 //! from the set between the "insert_unique_check" that filled "commit_data"
622 //! and the call to "insert_commit".
625 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
626 //! from the "commit_data" that a previous "insert_check" filled.
628 //! <b>Complexity</b>: Constant time.
630 //! <b>Throws</b>: Nothing.
632 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
633 //! previously executed to fill "commit_data". No value should be inserted or
634 //! erased between the "insert_check" and "insert_commit" calls.
635 static void insert_unique_commit
636 (node_ptr header
, node_ptr new_value
, const insert_commit_data
&commit_data
)
638 tree_algorithms::insert_unique_commit(header
, new_value
, commit_data
);
639 rebalance_after_insertion(header
, new_value
);
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 NodeTraits::get_balance(p
) == NodeTraits::zero() && tree_algorithms::is_header(p
); }
665 static void rebalance_after_erasure(node_ptr header
, node_ptr x
, node_ptr x_parent
)
667 node_ptr root
= NodeTraits::get_parent(header
);
669 const balance x_parent_balance
= NodeTraits::get_balance(x_parent
);
670 if(x_parent_balance
== NodeTraits::zero()){
671 NodeTraits::set_balance(x_parent
,
672 (x
== NodeTraits::get_right(x_parent
) ? NodeTraits::negative() : NodeTraits::positive()));
673 break; // the height didn't change, let's stop here
675 else if(x_parent_balance
== NodeTraits::negative()){
676 if (x
== NodeTraits::get_left(x_parent
)) {
677 NodeTraits::set_balance(x_parent
, NodeTraits::zero()); // balanced
679 x_parent
= NodeTraits::get_parent(x_parent
);
684 node_ptr a
= NodeTraits::get_left(x_parent
);
685 BOOST_INTRUSIVE_INVARIANT_ASSERT(a
);
686 if (NodeTraits::get_balance(a
) == NodeTraits::positive()) {
687 // a MUST have a right child
688 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(a
));
689 rotate_left_right(x_parent
, root
);
691 x
= NodeTraits::get_parent(x_parent
);
692 x_parent
= NodeTraits::get_parent(x
);
695 rotate_right(x_parent
, root
);
696 x
= NodeTraits::get_parent(x_parent
);
697 x_parent
= NodeTraits::get_parent(x
);
701 // if changed from negative to NodeTraits::positive(), no need to check above
702 if (NodeTraits::get_balance(x
) == NodeTraits::positive()){
707 else if(x_parent_balance
== NodeTraits::positive()){
708 if (x
== NodeTraits::get_right(x_parent
)) {
709 NodeTraits::set_balance(x_parent
, NodeTraits::zero()); // balanced
711 x_parent
= NodeTraits::get_parent(x_parent
);
716 node_ptr a
= NodeTraits::get_right(x_parent
);
717 BOOST_INTRUSIVE_INVARIANT_ASSERT(a
);
718 if (NodeTraits::get_balance(a
) == NodeTraits::negative()) {
719 // a MUST have then a left child
720 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(a
));
721 rotate_right_left(x_parent
, root
);
723 x
= NodeTraits::get_parent(x_parent
);
724 x_parent
= NodeTraits::get_parent(x
);
727 rotate_left(x_parent
, root
);
728 x
= NodeTraits::get_parent(x_parent
);
729 x_parent
= NodeTraits::get_parent(x
);
731 // if changed from NodeTraits::positive() to negative, no need to check above
732 if (NodeTraits::get_balance(x
) == NodeTraits::negative()){
738 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
741 NodeTraits::set_parent(header
, root
);
745 static void rebalance_after_insertion(node_ptr header
, node_ptr x
)
747 node_ptr root
= NodeTraits::get_parent(header
);
748 NodeTraits::set_balance(x
, NodeTraits::zero());
752 const balance x_parent_balance
= NodeTraits::get_balance(NodeTraits::get_parent(x
));
754 if(x_parent_balance
== NodeTraits::zero()){
755 // if x is left, parent will have parent->bal_factor = negative
756 // else, parent->bal_factor = NodeTraits::positive()
757 NodeTraits::set_balance( NodeTraits::get_parent(x
)
758 , x
== NodeTraits::get_left(NodeTraits::get_parent(x
))
759 ? NodeTraits::negative() : NodeTraits::positive() );
760 x
= NodeTraits::get_parent(x
);
762 else if(x_parent_balance
== NodeTraits::positive()){
763 // if x is a left child, parent->bal_factor = zero
764 if (x
== NodeTraits::get_left(NodeTraits::get_parent(x
)))
765 NodeTraits::set_balance(NodeTraits::get_parent(x
), NodeTraits::zero());
766 else{ // x is a right child, needs rebalancing
767 if (NodeTraits::get_balance(x
) == NodeTraits::negative())
768 rotate_right_left(NodeTraits::get_parent(x
), root
);
770 rotate_left(NodeTraits::get_parent(x
), root
);
774 else if(x_parent_balance
== NodeTraits::negative()){
775 // if x is a left child, needs rebalancing
776 if (x
== NodeTraits::get_left(NodeTraits::get_parent(x
))) {
777 if (NodeTraits::get_balance(x
) == NodeTraits::positive())
778 rotate_left_right(NodeTraits::get_parent(x
), root
);
780 rotate_right(NodeTraits::get_parent(x
), root
);
783 NodeTraits::set_balance(NodeTraits::get_parent(x
), NodeTraits::zero());
787 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
790 NodeTraits::set_parent(header
, root
);
793 static void rotate_left_right(node_ptr a
, node_ptr
&root
)
801 // [d] c [d] e f [g] //
804 node_ptr b
= NodeTraits::get_left(a
), c
= NodeTraits::get_right(b
);
807 NodeTraits::set_left(a
, NodeTraits::get_right(c
));
808 NodeTraits::set_right(b
, NodeTraits::get_left(c
));
810 NodeTraits::set_right(c
, a
);
811 NodeTraits::set_left(c
, b
);
814 NodeTraits::set_parent(c
, NodeTraits::get_parent(a
));
815 NodeTraits::set_parent(a
, c
);
816 NodeTraits::set_parent(b
, c
);
818 if (NodeTraits::get_left(a
)) // do we have f?
819 NodeTraits::set_parent(NodeTraits::get_left(a
), a
);
820 if (NodeTraits::get_right(b
)) // do we have e?
821 NodeTraits::set_parent(NodeTraits::get_right(b
), b
);
823 if (a
==root
) root
= c
;
824 else // a had a parent, his child is now c
825 if (a
== NodeTraits::get_left(NodeTraits::get_parent(c
)))
826 NodeTraits::set_left(NodeTraits::get_parent(c
), c
);
828 NodeTraits::set_right(NodeTraits::get_parent(c
), c
);
831 const balance c_balance
= NodeTraits::get_balance(c
);
832 if(c_balance
== NodeTraits::negative()){
833 NodeTraits::set_balance(a
, NodeTraits::positive());
834 NodeTraits::set_balance(b
, NodeTraits::zero());
836 else if(c_balance
== NodeTraits::zero()){
837 NodeTraits::set_balance(a
, NodeTraits::zero());
838 NodeTraits::set_balance(b
, NodeTraits::zero());
840 else if(c_balance
== NodeTraits::positive()){
841 NodeTraits::set_balance(a
, NodeTraits::zero());
842 NodeTraits::set_balance(b
, NodeTraits::negative());
845 BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
847 NodeTraits::set_balance(c
, NodeTraits::zero());
850 static void rotate_right_left(node_ptr a
, node_ptr
&root
)
856 // [d] b(neg) ==> a b //
858 // c [g] [d] e f [g] //
861 node_ptr b
= NodeTraits::get_right(a
), c
= NodeTraits::get_left(b
);
864 NodeTraits::set_right(a
, NodeTraits::get_left(c
));
865 NodeTraits::set_left(b
, NodeTraits::get_right(c
));
867 NodeTraits::set_left(c
, a
);
868 NodeTraits::set_right(c
, b
);
871 NodeTraits::set_parent(c
, NodeTraits::get_parent(a
));
872 NodeTraits::set_parent(a
, c
);
873 NodeTraits::set_parent(b
, c
);
875 if (NodeTraits::get_right(a
)) // do we have e?
876 NodeTraits::set_parent(NodeTraits::get_right(a
), a
);
877 if (NodeTraits::get_left(b
)) // do we have f?
878 NodeTraits::set_parent(NodeTraits::get_left(b
), b
);
880 if (a
==root
) root
= c
;
881 else // a had a parent, his child is now c
882 if (a
== NodeTraits::get_left(NodeTraits::get_parent(c
)))
883 NodeTraits::set_left(NodeTraits::get_parent(c
), c
);
885 NodeTraits::set_right(NodeTraits::get_parent(c
), c
);
888 const balance c_balance
= NodeTraits::get_balance(c
);
889 if(c_balance
== NodeTraits::negative()){
890 NodeTraits::set_balance(a
, NodeTraits::zero());
891 NodeTraits::set_balance(b
, NodeTraits::positive());
893 else if(c_balance
== NodeTraits::zero()){
894 NodeTraits::set_balance(a
, NodeTraits::zero());
895 NodeTraits::set_balance(b
, NodeTraits::zero());
897 else if(c_balance
== NodeTraits::positive()){
898 NodeTraits::set_balance(a
, NodeTraits::negative());
899 NodeTraits::set_balance(b
, NodeTraits::zero());
902 BOOST_INTRUSIVE_INVARIANT_ASSERT(false);
904 NodeTraits::set_balance(c
, NodeTraits::zero());
907 static void rotate_left(node_ptr x
, node_ptr
& root
)
912 // n[a] y(1)n+2 n+1(0)x [c]n+1 //
914 // n[b] [c]n+1 n[a] [b]n //
915 node_ptr y
= NodeTraits::get_right(x
);
918 NodeTraits::set_right(x
, NodeTraits::get_left(y
));
919 NodeTraits::set_left(y
, x
);
922 NodeTraits::set_parent(y
, NodeTraits::get_parent(x
));
923 NodeTraits::set_parent(x
, y
);
926 if (NodeTraits::get_right(x
))
927 NodeTraits::set_parent(NodeTraits::get_right(x
), x
);
932 // need to reparent y
933 if (NodeTraits::get_left(NodeTraits::get_parent(y
)) == x
)
934 NodeTraits::set_left(NodeTraits::get_parent(y
), y
);
936 NodeTraits::set_right(NodeTraits::get_parent(y
), y
);
938 // reset the balancing factor
939 if (NodeTraits::get_balance(y
) == NodeTraits::positive()) {
940 NodeTraits::set_balance(x
, NodeTraits::zero());
941 NodeTraits::set_balance(y
, NodeTraits::zero());
943 else { // this doesn't happen during insertions
944 NodeTraits::set_balance(x
, NodeTraits::positive());
945 NodeTraits::set_balance(y
, NodeTraits::negative());
949 static void rotate_right(node_ptr x
, node_ptr
&root
)
951 node_ptr y
= NodeTraits::get_left(x
);
954 NodeTraits::set_left(x
, NodeTraits::get_right(y
));
955 NodeTraits::set_right(y
, x
);
958 NodeTraits::set_parent(y
, NodeTraits::get_parent(x
));
959 NodeTraits::set_parent(x
, y
);
962 if (NodeTraits::get_left(x
))
963 NodeTraits::set_parent(NodeTraits::get_left(x
), x
);
968 // need to reparent y
969 if (NodeTraits::get_left(NodeTraits::get_parent(y
)) == x
)
970 NodeTraits::set_left(NodeTraits::get_parent(y
), y
);
972 NodeTraits::set_right(NodeTraits::get_parent(y
), y
);
974 // reset the balancing factor
975 if (NodeTraits::get_balance(y
) == NodeTraits::negative()) {
976 NodeTraits::set_balance(x
, NodeTraits::zero());
977 NodeTraits::set_balance(y
, NodeTraits::zero());
979 else { // this doesn't happen during insertions
980 NodeTraits::set_balance(x
, NodeTraits::negative());
981 NodeTraits::set_balance(y
, NodeTraits::positive());
988 } //namespace intrusive
991 #include <boost/intrusive/detail/config_end.hpp>
993 #endif //BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP