1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2008.
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 #ifndef BOOST_INTRUSIVE_TRIE_ALGORITHMS_HPP
14 #define BOOST_INTRUSIVE_TRIE_ALGORITHMS_HPP
16 #include <boost/intrusive/detail/config_begin.hpp>
19 #include <boost/intrusive/intrusive_fwd.hpp>
21 #include <boost/intrusive/detail/assert.hpp>
22 #include <boost/intrusive/detail/utilities.hpp>
23 #include <boost/intrusive/detail/tree_algorithms.hpp>
30 //! treap_algorithms provides basic algorithms to manipulate
31 //! nodes forming a treap.
33 //! (1) the header node is maintained with links not only to the root
34 //! but also to the leftmost node of the tree, to enable constant time
35 //! begin(), and to the rightmost node of the tree, to enable linear time
36 //! performance when used with the generic set algorithms (set_union,
39 //! (2) when a node being deleted has two children its successor node is
40 //! relinked into its place, rather than copied, so that the only
41 //! pointers invalidated are those referring to the deleted node.
43 //! treap_algorithms is configured with a NodeTraits class, which encapsulates the
44 //! information about the node to be manipulated. NodeTraits must support the
45 //! following interface:
49 //! <tt>node</tt>: The type of the node that forms the circular list
51 //! <tt>node_ptr</tt>: A pointer to a node
53 //! <tt>const_node_ptr</tt>: A pointer to a const node
55 //! <b>Static functions</b>:
57 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
59 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
61 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
63 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
65 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
67 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
68 template<class NodeTraits
>
69 class treap_algorithms
72 typedef NodeTraits node_traits
;
73 typedef typename
NodeTraits::node node
;
74 typedef typename
NodeTraits::node_ptr node_ptr
;
75 typedef typename
NodeTraits::const_node_ptr const_node_ptr
;
80 class remove_on_destroy
82 remove_on_destroy(const remove_on_destroy
&);
83 remove_on_destroy
& operator=(const remove_on_destroy
&);
85 remove_on_destroy(node_ptr header
, node_ptr z
)
86 : header_(header
), z_(z
), remove_it_(true)
91 tree_algorithms::erase(header_
, z_
);
96 { remove_it_
= false; }
98 const node_ptr header_
;
103 class rerotate_on_destroy
105 rerotate_on_destroy(const remove_on_destroy
&);
106 rerotate_on_destroy
& operator=(const rerotate_on_destroy
&);
109 rerotate_on_destroy(node_ptr header
, node_ptr p
, std::size_t &n
)
110 : header_(header
), p_(p
), n_(n
), remove_it_(true)
113 ~rerotate_on_destroy()
116 rotate_up_n(header_
, p_
, n_
);
121 { remove_it_
= false; }
123 const node_ptr header_
;
129 static void rotate_up_n(const node_ptr header
, const node_ptr p
, std::size_t n
)
131 for( node_ptr p_parent
= NodeTraits::get_parent(p
)
133 ; p_parent
= NodeTraits::get_parent(p
)){
134 //Check if left child
135 if(p
== NodeTraits::get_left(p_parent
)){
136 tree_algorithms::rotate_right(p_parent
, header
);
139 tree_algorithms::rotate_left(p_parent
, header
);
144 typedef detail::tree_algorithms
<NodeTraits
> tree_algorithms
;
146 static node_ptr
uncast(const_node_ptr ptr
)
148 return node_ptr(const_cast<node
*>(::boost::intrusive::detail::get_pointer(ptr
)));
153 static node_ptr
begin_node(const_node_ptr header
)
154 { return tree_algorithms::begin_node(header
); }
156 static node_ptr
end_node(const_node_ptr header
)
157 { return tree_algorithms::end_node(header
); }
159 //! This type is the information that will be
160 //! filled by insert_unique_check
161 struct insert_commit_data
163 : public tree_algorithms::insert_commit_data
167 std::size_t rotations
;
171 //! <b>Requires</b>: header1 and header2 must be the header nodes
174 //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
175 //! links to the second tree and header2 will have links to the first tree.
177 //! <b>Complexity</b>: Constant.
179 //! <b>Throws</b>: Nothing.
180 static void swap_tree(node_ptr header1
, node_ptr header2
)
181 { return tree_algorithms::swap_tree(header1
, header2
); }
183 //! <b>Requires</b>: node1 and node2 can't be header nodes
186 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
187 //! in the position node2 before the function. node2 will be inserted in the
188 //! position node1 had before the function.
190 //! <b>Complexity</b>: Logarithmic.
192 //! <b>Throws</b>: Nothing.
194 //! <b>Note</b>: This function will break container ordering invariants if
195 //! node1 and node2 are not equivalent according to the ordering rules.
197 //!Experimental function
198 static void swap_nodes(node_ptr node1
, node_ptr node2
)
203 node_ptr
header1(tree_algorithms::get_header(node1
)), header2(tree_algorithms::get_header(node2
));
204 swap_nodes(node1
, header1
, node2
, header2
);
207 //! <b>Requires</b>: node1 and node2 can't be header nodes
208 //! of two trees with header header1 and header2.
210 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
211 //! in the position node2 before the function. node2 will be inserted in the
212 //! position node1 had before the function.
214 //! <b>Complexity</b>: Constant.
216 //! <b>Throws</b>: Nothing.
218 //! <b>Note</b>: This function will break container ordering invariants if
219 //! node1 and node2 are not equivalent according to the ordering rules.
221 //!Experimental function
222 static void swap_nodes(node_ptr node1
, node_ptr header1
, node_ptr node2
, node_ptr header2
)
223 { tree_algorithms::swap_nodes(node1
, header1
, node2
, header2
); }
225 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
226 //! and new_node must not be inserted in a tree.
228 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
229 //! tree with new_node. The tree does not need to be rebalanced
231 //! <b>Complexity</b>: Logarithmic.
233 //! <b>Throws</b>: Nothing.
235 //! <b>Note</b>: This function will break container ordering invariants if
236 //! new_node is not equivalent to node_to_be_replaced according to the
237 //! ordering rules. This function is faster than erasing and inserting
238 //! the node, since no rebalancing and comparison is needed.
240 //!Experimental function
241 static void replace_node(node_ptr node_to_be_replaced
, node_ptr new_node
)
243 if(node_to_be_replaced
== new_node
)
245 replace_node(node_to_be_replaced
, tree_algorithms::get_header(node_to_be_replaced
), new_node
);
248 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
249 //! with header "header" and new_node must not be inserted in a tree.
251 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
252 //! tree with new_node. The tree does not need to be rebalanced
254 //! <b>Complexity</b>: Constant.
256 //! <b>Throws</b>: Nothing.
258 //! <b>Note</b>: This function will break container ordering invariants if
259 //! new_node is not equivalent to node_to_be_replaced according to the
260 //! ordering rules. This function is faster than erasing and inserting
261 //! the node, since no rebalancing or comparison is needed.
263 //!Experimental function
264 static void replace_node(node_ptr node_to_be_replaced
, node_ptr header
, node_ptr new_node
)
265 { tree_algorithms::replace_node(node_to_be_replaced
, header
, new_node
); }
267 //! <b>Requires</b>: node is a tree node but not the header.
269 //! <b>Effects</b>: Unlinks the node and rebalances the tree.
271 //! <b>Complexity</b>: Average complexity is constant time.
273 //! <b>Throws</b>: Nothing.
274 template<class NodePriorityCompare
>
275 static void unlink(node_ptr node
, NodePriorityCompare prio
)
277 node_ptr x
= NodeTraits::get_parent(node
);
280 x
= NodeTraits::get_parent(x
);
281 erase(x
, node
, prio
);
285 //! <b>Requires</b>: header is the header of a tree.
287 //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
288 //! updates the header link to the new leftmost node.
290 //! <b>Complexity</b>: Average complexity is constant time.
292 //! <b>Throws</b>: Nothing.
294 //! <b>Notes</b>: This function breaks the tree and the tree can
295 //! only be used for more unlink_leftmost_without_rebalance calls.
296 //! This function is normally used to achieve a step by step
297 //! controlled destruction of the tree.
298 static node_ptr
unlink_leftmost_without_rebalance(node_ptr header
)
299 { return tree_algorithms::unlink_leftmost_without_rebalance(header
); }
301 //! <b>Requires</b>: node is a node of the tree or an node initialized
304 //! <b>Effects</b>: Returns true if the node is initialized by init().
306 //! <b>Complexity</b>: Constant time.
308 //! <b>Throws</b>: Nothing.
309 static bool unique(const_node_ptr node
)
310 { return tree_algorithms::unique(node
); }
312 //! <b>Requires</b>: node is a node of the tree but it's not the header.
314 //! <b>Effects</b>: Returns the number of nodes of the subtree.
316 //! <b>Complexity</b>: Linear time.
318 //! <b>Throws</b>: Nothing.
319 static std::size_t count(const_node_ptr node
)
320 { return tree_algorithms::count(node
); }
322 //! <b>Requires</b>: header is the header node of the tree.
324 //! <b>Effects</b>: Returns the number of nodes above the header.
326 //! <b>Complexity</b>: Linear time.
328 //! <b>Throws</b>: Nothing.
329 static std::size_t size(const_node_ptr header
)
330 { return tree_algorithms::size(header
); }
332 //! <b>Requires</b>: p is a node from the tree except the header.
334 //! <b>Effects</b>: Returns the next node of the tree.
336 //! <b>Complexity</b>: Average constant time.
338 //! <b>Throws</b>: Nothing.
339 static node_ptr
next_node(node_ptr p
)
340 { return tree_algorithms::next_node(p
); }
342 //! <b>Requires</b>: p is a node from the tree except the leftmost node.
344 //! <b>Effects</b>: Returns the previous node of the tree.
346 //! <b>Complexity</b>: Average constant time.
348 //! <b>Throws</b>: Nothing.
349 static node_ptr
prev_node(node_ptr p
)
350 { return tree_algorithms::prev_node(p
); }
352 //! <b>Requires</b>: node must not be part of any tree.
354 //! <b>Effects</b>: After the function unique(node) == true.
356 //! <b>Complexity</b>: Constant.
358 //! <b>Throws</b>: Nothing.
360 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
361 static void init(node_ptr node
)
362 { tree_algorithms::init(node
); }
364 //! <b>Requires</b>: node must not be part of any tree.
366 //! <b>Effects</b>: Initializes the header to represent an empty tree.
367 //! unique(header) == true.
369 //! <b>Complexity</b>: Constant.
371 //! <b>Throws</b>: Nothing.
373 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
374 static void init_header(node_ptr header
)
376 tree_algorithms::init_header(header
);
379 //! <b>Requires</b>: header must be the header of a tree, z a node
380 //! of that tree and z != header.
382 //! <b>Effects</b>: Erases node "z" from the tree with header "header".
384 //! <b>Complexity</b>: Amortized constant time.
386 //! <b>Throws</b>: Nothing.
387 template<class NodePriorityCompare
>
388 static node_ptr
erase(node_ptr header
, node_ptr z
, NodePriorityCompare pcomp
)
390 rebalance_for_erasure(header
, z
, pcomp
);
391 tree_algorithms::erase(header
, z
);
392 // assert(check_invariant(header, pcomp));
396 //! <b>Requires</b>: "cloner" must be a function
397 //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
398 //! take a node_ptr and shouldn't throw.
400 //! <b>Effects</b>: First empties target tree calling
401 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
402 //! except the header.
404 //! Then, duplicates the entire tree pointed by "source_header" cloning each
405 //! source node with <tt>node_ptr Cloner::operator()(node_ptr)</tt> to obtain
406 //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
407 //! are disposed using <tt>void disposer(node_ptr)</tt>.
409 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
410 //! number of elements of tree target tree when calling this function.
412 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
413 template <class Cloner
, class Disposer
>
415 (const_node_ptr source_header
, node_ptr target_header
, Cloner cloner
, Disposer disposer
)
417 tree_algorithms::clone(source_header
, target_header
, cloner
, disposer
);
420 //! <b>Requires</b>: "disposer" must be an object function
421 //! taking a node_ptr parameter and shouldn't throw.
423 //! <b>Effects</b>: Empties the target tree calling
424 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
425 //! except the header.
427 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
428 //! number of elements of tree target tree when calling this function.
430 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
431 template<class Disposer
>
432 static void clear_and_dispose(node_ptr header
, Disposer disposer
)
433 { tree_algorithms::clear_and_dispose(header
, disposer
); }
435 //! <b>Requires</b>: "header" must be the header node of a tree.
436 //! KeyNodePtrCompare is a function object that induces a strict weak
437 //! ordering compatible with the strict weak ordering used to create the
438 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
440 //! <b>Effects</b>: Returns an node_ptr to the first element that is
441 //! not less than "key" according to "comp" or "header" if that element does
444 //! <b>Complexity</b>: Logarithmic.
446 //! <b>Throws</b>: If "comp" throws.
447 template<class KeyType
, class KeyNodePtrCompare
>
448 static node_ptr lower_bound
449 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
450 { return tree_algorithms::lower_bound(header
, key
, comp
); }
452 //! <b>Requires</b>: "header" must be the header node of a tree.
453 //! KeyNodePtrCompare is a function object that induces a strict weak
454 //! ordering compatible with the strict weak ordering used to create the
455 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
457 //! <b>Effects</b>: Returns an node_ptr to the first element that is greater
458 //! than "key" according to "comp" or "header" if that element does not exist.
460 //! <b>Complexity</b>: Logarithmic.
462 //! <b>Throws</b>: If "comp" throws.
463 template<class KeyType
, class KeyNodePtrCompare
>
464 static node_ptr upper_bound
465 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
466 { return tree_algorithms::upper_bound(header
, key
, comp
); }
468 //! <b>Requires</b>: "header" must be the header node of a tree.
469 //! KeyNodePtrCompare is a function object that induces a strict weak
470 //! ordering compatible with the strict weak ordering used to create the
471 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
473 //! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
474 //! "key" according to "comp" or "header" if that element does not exist.
476 //! <b>Complexity</b>: Logarithmic.
478 //! <b>Throws</b>: If "comp" throws.
479 template<class KeyType
, class KeyNodePtrCompare
>
481 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
482 { return tree_algorithms::find(header
, key
, comp
); }
484 //! <b>Requires</b>: "header" must be the header node of a tree.
485 //! KeyNodePtrCompare is a function object that induces a strict weak
486 //! ordering compatible with the strict weak ordering used to create the
487 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
489 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
490 //! all elements that are equivalent to "key" according to "comp" or an
491 //! empty range that indicates the position where those elements would be
492 //! if they there are no equivalent elements.
494 //! <b>Complexity</b>: Logarithmic.
496 //! <b>Throws</b>: If "comp" throws.
497 template<class KeyType
, class KeyNodePtrCompare
>
498 static std::pair
<node_ptr
, node_ptr
> equal_range
499 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
500 { return tree_algorithms::equal_range(header
, key
, comp
); }
502 //! <b>Requires</b>: "h" must be the header node of a tree.
503 //! NodePtrCompare is a function object that induces a strict weak
504 //! ordering compatible with the strict weak ordering used to create the
505 //! the tree. NodePtrCompare compares two node_ptrs.
507 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
508 //! according to "comp".
510 //! <b>Complexity</b>: Average complexity for insert element is at
511 //! most logarithmic.
513 //! <b>Throws</b>: If "comp" throws.
514 template<class NodePtrCompare
, class PriorityNodeCompare
>
515 static node_ptr insert_equal_upper_bound
516 (node_ptr h
, node_ptr new_node
, NodePtrCompare comp
, PriorityNodeCompare pcomp
)
518 insert_commit_data commit_data
;
519 tree_algorithms::insert_equal_upper_bound_check(h
, new_node
, comp
, commit_data
);
520 rebalance_after_insertion_check(h
, commit_data
.node
, new_node
, pcomp
, commit_data
.rotations
);
521 tree_algorithms::insert_unique_commit(h
, new_node
, commit_data
);
522 rebalance_after_insertion_commit(h
, new_node
, commit_data
.rotations
);
526 //! <b>Requires</b>: "h" must be the header node of a tree.
527 //! NodePtrCompare is a function object that induces a strict weak
528 //! ordering compatible with the strict weak ordering used to create the
529 //! the tree. NodePtrCompare compares two node_ptrs.
531 //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
532 //! according to "comp".
534 //! <b>Complexity</b>: Average complexity for insert element is at
535 //! most logarithmic.
537 //! <b>Throws</b>: If "comp" throws.
538 template<class NodePtrCompare
, class NodePriorityCompare
>
539 static node_ptr insert_equal_lower_bound
540 (node_ptr h
, node_ptr new_node
, NodePtrCompare comp
, NodePriorityCompare pcomp
)
542 insert_commit_data commit_data
;
543 tree_algorithms::insert_equal_lower_bound_check(h
, new_node
, comp
, commit_data
);
544 rebalance_after_insertion_check(h
, commit_data
.node
, new_node
, pcomp
, commit_data
.rotations
);
545 tree_algorithms::insert_unique_commit(h
, new_node
, commit_data
);
546 rebalance_after_insertion_commit(h
, new_node
, commit_data
.rotations
);
550 //! <b>Requires</b>: "header" must be the header node of a tree.
551 //! NodePtrCompare is a function object that induces a strict weak
552 //! ordering compatible with the strict weak ordering used to create the
553 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
554 //! the "header"'s tree.
556 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
557 //! where it will be inserted. If "hint" is the upper_bound
558 //! the insertion takes constant time (two comparisons in the worst case).
560 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
561 //! constant time if new_node is inserted immediately before "hint".
563 //! <b>Throws</b>: If "comp" throws.
564 template<class NodePtrCompare
, class NodePriorityCompare
>
565 static node_ptr insert_equal
566 (node_ptr h
, node_ptr hint
, node_ptr new_node
, NodePtrCompare comp
, NodePriorityCompare pcomp
)
568 insert_commit_data commit_data
;
569 tree_algorithms::insert_equal_check(h
, hint
, new_node
, comp
, commit_data
);
570 rebalance_after_insertion_check(h
, commit_data
.node
, new_node
, pcomp
, commit_data
.rotations
);
571 tree_algorithms::insert_unique_commit(h
, new_node
, commit_data
);
572 rebalance_after_insertion_commit(h
, new_node
, commit_data
.rotations
);
576 //! <b>Requires</b>: "header" must be the header node of a tree.
577 //! KeyNodePtrCompare is a function object that induces a strict weak
578 //! ordering compatible with the strict weak ordering used to create the
579 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
581 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
582 //! tree according to "comp" and obtains the needed information to realize
583 //! a constant-time node insertion if there is no equivalent node.
585 //! <b>Returns</b>: If there is an equivalent value
586 //! returns a pair containing a node_ptr to the already present node
587 //! and false. If there is not equivalent key can be inserted returns true
588 //! in the returned pair's boolean and fills "commit_data" that is meant to
589 //! be used with the "insert_commit" function to achieve a constant-time
590 //! insertion function.
592 //! <b>Complexity</b>: Average complexity is at most logarithmic.
594 //! <b>Throws</b>: If "comp" throws.
596 //! <b>Notes</b>: This function is used to improve performance when constructing
597 //! a node is expensive and the user does not want to have two equivalent nodes
598 //! in the tree: if there is an equivalent value
599 //! the constructed object must be discarded. Many times, the part of the
600 //! node that is used to impose the order is much cheaper to construct
601 //! than the node and this function offers the possibility to use that part
602 //! to check if the insertion will be successful.
604 //! If the check is successful, the user can construct the node and use
605 //! "insert_commit" to insert the node in constant-time. This gives a total
606 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
608 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
609 //! if no more objects are inserted or erased from the set.
610 template<class KeyType
, class KeyNodePtrCompare
, class KeyNodePtrPrioCompare
>
611 static std::pair
<node_ptr
, bool> insert_unique_check
612 (const_node_ptr header
, const KeyType
&key
613 ,KeyNodePtrCompare comp
, KeyNodePtrPrioCompare pcomp
614 ,insert_commit_data
&commit_data
)
616 std::pair
<node_ptr
, bool> ret
=
617 tree_algorithms::insert_unique_check(header
, key
, comp
, commit_data
);
619 rebalance_after_insertion_check(header
, commit_data
.node
, key
, pcomp
, commit_data
.rotations
);
623 //! <b>Requires</b>: "header" must be the header node of a tree.
624 //! KeyNodePtrCompare is a function object that induces a strict weak
625 //! ordering compatible with the strict weak ordering used to create the
626 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
627 //! "hint" is node from the "header"'s tree.
629 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
630 //! tree according to "comp" using "hint" as a hint to where it should be
631 //! inserted and obtains the needed information to realize
632 //! a constant-time node insertion if there is no equivalent node.
633 //! If "hint" is the upper_bound the function has constant time
634 //! complexity (two comparisons in the worst case).
636 //! <b>Returns</b>: If there is an equivalent value
637 //! returns a pair containing a node_ptr to the already present node
638 //! and false. If there is not equivalent key can be inserted returns true
639 //! in the returned pair's boolean and fills "commit_data" that is meant to
640 //! be used with the "insert_commit" function to achieve a constant-time
641 //! insertion function.
643 //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
644 //! amortized constant time if new_node should be inserted immediately before "hint".
646 //! <b>Throws</b>: If "comp" throws.
648 //! <b>Notes</b>: This function is used to improve performance when constructing
649 //! a node is expensive and the user does not want to have two equivalent nodes
650 //! in the tree: if there is an equivalent value
651 //! the constructed object must be discarded. Many times, the part of the
652 //! node that is used to impose the order is much cheaper to construct
653 //! than the node and this function offers the possibility to use that part
654 //! to check if the insertion will be successful.
656 //! If the check is successful, the user can construct the node and use
657 //! "insert_commit" to insert the node in constant-time. This gives a total
658 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
660 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
661 //! if no more objects are inserted or erased from the set.
662 template<class KeyType
, class KeyNodePtrCompare
, class KeyNodePtrPrioCompare
>
663 static std::pair
<node_ptr
, bool> insert_unique_check
664 (const_node_ptr header
, node_ptr hint
, const KeyType
&key
665 ,KeyNodePtrCompare comp
, KeyNodePtrPrioCompare pcomp
, insert_commit_data
&commit_data
)
667 std::pair
<node_ptr
, bool> ret
=
668 tree_algorithms::insert_unique_check(header
, hint
, key
, comp
, commit_data
);
670 rebalance_after_insertion_check(header
, commit_data
.node
, key
, pcomp
, commit_data
.rotations
);
674 //! <b>Requires</b>: "header" must be the header node of a tree.
675 //! "commit_data" must have been obtained from a previous call to
676 //! "insert_unique_check". No objects should have been inserted or erased
677 //! from the set between the "insert_unique_check" that filled "commit_data"
678 //! and the call to "insert_commit".
681 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
682 //! from the "commit_data" that a previous "insert_check" filled.
684 //! <b>Complexity</b>: Constant time.
686 //! <b>Throws</b>: Nothing.
688 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
689 //! previously executed to fill "commit_data". No value should be inserted or
690 //! erased between the "insert_check" and "insert_commit" calls.
691 static void insert_unique_commit
692 (node_ptr header
, node_ptr new_node
, const insert_commit_data
&commit_data
)
694 tree_algorithms::insert_unique_commit(header
, new_node
, commit_data
);
695 rebalance_after_insertion_commit(header
, new_node
, commit_data
.rotations
);
698 //! <b>Requires</b>: "n" must be a node inserted in a tree.
700 //! <b>Effects</b>: Returns a pointer to the header node of the tree.
702 //! <b>Complexity</b>: Logarithmic.
704 //! <b>Throws</b>: Nothing.
705 static node_ptr
get_header(node_ptr n
)
706 { return tree_algorithms::get_header(n
); }
711 //! <b>Requires</b>: p is a node of a tree.
713 //! <b>Effects</b>: Returns true if p is the header of the tree.
715 //! <b>Complexity</b>: Constant.
717 //! <b>Throws</b>: Nothing.
718 static bool is_header(const_node_ptr p
)
720 return tree_algorithms::is_header(p
);
723 template<class NodePriorityCompare
>
724 static void rebalance_for_erasure(node_ptr header
, node_ptr z
, NodePriorityCompare pcomp
)
727 rerotate_on_destroy
rb(header
, z
, n
);
729 node_ptr z_left
= NodeTraits::get_left(z
);
730 node_ptr z_right
= NodeTraits::get_right(z
);
731 while(z_left
|| z_right
){
732 if(!z_right
|| (z_left
&& pcomp(z_left
, z_right
))){
733 tree_algorithms::rotate_right(z
, header
);
736 tree_algorithms::rotate_left(z
, header
);
739 z_left
= NodeTraits::get_left(z
);
740 z_right
= NodeTraits::get_right(z
);
745 template<class Key
, class KeyNodePriorityCompare
>
746 static void rebalance_after_insertion_check
747 ( const_node_ptr header
, const_node_ptr upnode
, const Key
&k
748 , KeyNodePriorityCompare pcomp
, std::size_t &num_rotations
)
750 //First check rotations since pcomp can throw
753 while(upnode
!= header
&& pcomp(k
, upnode
)){
755 upnode
= NodeTraits::get_parent(upnode
);
760 static void rebalance_after_insertion_commit(node_ptr header
, node_ptr p
, std::size_t n
)
762 // Now to n rotations
763 for( node_ptr p_parent
= NodeTraits::get_parent(p
)
765 ; p_parent
= NodeTraits::get_parent(p
)){
766 //Check if left child
767 if(p
== NodeTraits::get_left(p_parent
)){
768 tree_algorithms::rotate_right(p_parent
, header
);
771 tree_algorithms::rotate_left(p_parent
, header
);
776 template<class NodePriorityCompare
>
777 static bool check_invariant(const_node_ptr header
, NodePriorityCompare pcomp
)
779 node_ptr beg
= begin_node(header
);
780 node_ptr end
= end_node(header
);
783 node_ptr p
= NodeTraits::get_parent(beg
);
788 beg
= next_node(beg
);
796 } //namespace intrusive
799 #include <boost/intrusive/detail/config_end.hpp>
801 #endif //BOOST_INTRUSIVE_TRIE_ALGORITHMS_HPP