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 /////////////////////////////////////////////////////////////////////////////
12 // The implementation of splay trees is based on the article and code published
13 // in C++ Users Journal "Implementing Splay Trees in C++" (September 1, 2005).
15 // The code has been modified and (supposely) improved by Ion Gaztanaga.
16 // Here is the header of the file used as base code:
18 // splay_tree.h -- implementation of a STL complatible splay tree.
20 // Copyright (c) 2004 Ralf Mattethat
22 // Permission to copy, use, modify, sell and distribute this software
23 // is granted provided this copyright notice appears in all copies.
24 // This software is provided "as is" without express or implied
25 // warranty, and with no claim as to its suitability for any purpose.
27 // Please send questions, comments, complaints, performance data, etc to
28 // ralf.mattethat@teknologisk.dk
30 // Requirements for element type
31 // * must be copy-constructible
32 // * destructor must not throw exception
34 // Methods marked with note A only throws an exception if the evaluation of the
35 // predicate throws an exception. If an exception is thrown the call has no
36 // effect on the containers state
38 // Methods marked with note B only throws an exception if the coppy constructor
39 // or assignment operator of the predicate throws an exception. If an exception
40 // is thrown the call has no effect on the containers state
42 // iterators are only invalidated, if the element pointed to by the iterator
43 // is deleted. The same goes for element references
46 #ifndef BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
47 #define BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
49 #include <boost/intrusive/detail/config_begin.hpp>
50 #include <boost/intrusive/detail/assert.hpp>
51 #include <boost/intrusive/intrusive_fwd.hpp>
53 #include <boost/intrusive/detail/utilities.hpp>
54 #include <boost/intrusive/detail/tree_algorithms.hpp>
62 template<class NodeTraits
>
63 struct splaydown_rollback
65 typedef typename
NodeTraits::node_ptr node_ptr
;
66 splaydown_rollback( const node_ptr
*pcur_subtree
, node_ptr header
67 , node_ptr leftmost
, node_ptr rightmost
)
68 : pcur_subtree_(pcur_subtree
) , header_(header
)
69 , leftmost_(leftmost
) , rightmost_(rightmost
)
73 { pcur_subtree_
= 0; }
78 //Exception can only be thrown by comp, but
79 //tree invariants still hold. *pcur_subtree is the current root
80 //so link it to the header.
81 NodeTraits::set_parent(*pcur_subtree_
, header_
);
82 NodeTraits::set_parent(header_
, *pcur_subtree_
);
83 //Recover leftmost/rightmost pointers
84 NodeTraits::set_left (header_
, leftmost_
);
85 NodeTraits::set_right(header_
, rightmost_
);
88 const node_ptr
*pcur_subtree_
;
89 node_ptr header_
, leftmost_
, rightmost_
;
92 } //namespace detail {
95 //! A splay tree is an implementation of a binary search tree. The tree is
96 //! self balancing using the splay algorithm as described in
98 //! "Self-Adjusting Binary Search Trees
99 //! by Daniel Dominic Sleator and Robert Endre Tarjan
100 //! AT&T Bell Laboratories, Murray Hill, NJ
101 //! Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686
103 //! splaytree_algorithms is configured with a NodeTraits class, which encapsulates the
104 //! information about the node to be manipulated. NodeTraits must support the
105 //! following interface:
109 //! <tt>node</tt>: The type of the node that forms the circular list
111 //! <tt>node_ptr</tt>: A pointer to a node
113 //! <tt>const_node_ptr</tt>: A pointer to a const node
115 //! <b>Static functions</b>:
117 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
119 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
121 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
123 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
125 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
127 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
128 template<class NodeTraits
>
129 class splaytree_algorithms
133 typedef detail::tree_algorithms
<NodeTraits
> tree_algorithms
;
137 typedef typename
NodeTraits::node node
;
138 typedef NodeTraits node_traits
;
139 typedef typename
NodeTraits::node_ptr node_ptr
;
140 typedef typename
NodeTraits::const_node_ptr const_node_ptr
;
142 //! This type is the information that will be
143 //! filled by insert_unique_check
144 typedef typename
tree_algorithms::insert_commit_data insert_commit_data
;
148 static node_ptr
uncast(const_node_ptr ptr
)
150 return node_ptr(const_cast<node
*>(::boost::intrusive::detail::get_pointer(ptr
)));
155 static node_ptr
begin_node(const_node_ptr header
)
156 { return tree_algorithms::begin_node(header
); }
158 static node_ptr
end_node(const_node_ptr header
)
159 { return tree_algorithms::end_node(header
); }
161 //! <b>Requires</b>: node is a node of the tree or an node initialized
164 //! <b>Effects</b>: Returns true if the node is initialized by init().
166 //! <b>Complexity</b>: Constant time.
168 //! <b>Throws</b>: Nothing.
169 static bool unique(const_node_ptr node
)
170 { return tree_algorithms::unique(node
); }
172 static void unlink(node_ptr node
)
173 { tree_algorithms::unlink(node
); }
175 //! <b>Requires</b>: node1 and node2 can't be header nodes
178 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
179 //! in the position node2 before the function. node2 will be inserted in the
180 //! position node1 had before the function.
182 //! <b>Complexity</b>: Logarithmic.
184 //! <b>Throws</b>: Nothing.
186 //! <b>Note</b>: This function will break container ordering invariants if
187 //! node1 and node2 are not equivalent according to the ordering rules.
189 //!Experimental function
190 static void swap_nodes(node_ptr node1
, node_ptr node2
)
195 node_ptr
header1(tree_algorithms::get_header(node1
)), header2(tree_algorithms::get_header(node2
));
196 swap_nodes(node1
, header1
, node2
, header2
);
199 //! <b>Requires</b>: node1 and node2 can't be header nodes
200 //! of two trees with header header1 and header2.
202 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
203 //! in the position node2 before the function. node2 will be inserted in the
204 //! position node1 had before the function.
206 //! <b>Complexity</b>: Constant.
208 //! <b>Throws</b>: Nothing.
210 //! <b>Note</b>: This function will break container ordering invariants if
211 //! node1 and node2 are not equivalent according to the ordering rules.
213 //!Experimental function
214 static void swap_nodes(node_ptr node1
, node_ptr header1
, node_ptr node2
, node_ptr header2
)
215 { tree_algorithms::swap_nodes(node1
, header1
, node2
, header2
); }
217 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
218 //! and new_node must not be inserted in a tree.
220 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
221 //! tree with new_node. The tree does not need to be rebalanced
223 //! <b>Complexity</b>: Logarithmic.
225 //! <b>Throws</b>: Nothing.
227 //! <b>Note</b>: This function will break container ordering invariants if
228 //! new_node is not equivalent to node_to_be_replaced according to the
229 //! ordering rules. This function is faster than erasing and inserting
230 //! the node, since no rebalancing and comparison is needed.
232 //!Experimental function
233 static void replace_node(node_ptr node_to_be_replaced
, node_ptr new_node
)
235 if(node_to_be_replaced
== new_node
)
237 replace_node(node_to_be_replaced
, tree_algorithms::get_header(node_to_be_replaced
), new_node
);
240 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
241 //! with header "header" and new_node must not be inserted in a tree.
243 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
244 //! tree with new_node. The tree does not need to be rebalanced
246 //! <b>Complexity</b>: Constant.
248 //! <b>Throws</b>: Nothing.
250 //! <b>Note</b>: This function will break container ordering invariants if
251 //! new_node is not equivalent to node_to_be_replaced according to the
252 //! ordering rules. This function is faster than erasing and inserting
253 //! the node, since no rebalancing or comparison is needed.
255 //!Experimental function
256 static void replace_node(node_ptr node_to_be_replaced
, node_ptr header
, node_ptr new_node
)
257 { tree_algorithms::replace_node(node_to_be_replaced
, header
, new_node
); }
259 //! <b>Requires</b>: p is a node from the tree except the header.
261 //! <b>Effects</b>: Returns the next node of the tree.
263 //! <b>Complexity</b>: Average constant time.
265 //! <b>Throws</b>: Nothing.
266 static node_ptr
next_node(node_ptr p
)
267 { return tree_algorithms::next_node(p
); }
269 //! <b>Requires</b>: p is a node from the tree except the leftmost node.
271 //! <b>Effects</b>: Returns the previous node of the tree.
273 //! <b>Complexity</b>: Average constant time.
275 //! <b>Throws</b>: Nothing.
276 static node_ptr
prev_node(node_ptr p
)
277 { return tree_algorithms::prev_node(p
); }
279 //! <b>Requires</b>: node must not be part of any tree.
281 //! <b>Effects</b>: After the function unique(node) == true.
283 //! <b>Complexity</b>: Constant.
285 //! <b>Throws</b>: Nothing.
287 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
288 static void init(node_ptr node
)
289 { tree_algorithms::init(node
); }
291 //! <b>Requires</b>: node must not be part of any tree.
293 //! <b>Effects</b>: Initializes the header to represent an empty tree.
294 //! unique(header) == true.
296 //! <b>Complexity</b>: Constant.
298 //! <b>Throws</b>: Nothing.
300 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
301 static void init_header(node_ptr header
)
302 { tree_algorithms::init_header(header
); }
304 //! <b>Requires</b>: "disposer" must be an object function
305 //! taking a node_ptr parameter and shouldn't throw.
307 //! <b>Effects</b>: Empties the target tree calling
308 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
309 //! except the header.
311 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
312 //! number of elements of tree target tree when calling this function.
314 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
315 template<class Disposer
>
316 static void clear_and_dispose(node_ptr header
, Disposer disposer
)
317 { tree_algorithms::clear_and_dispose(header
, disposer
); }
319 //! <b>Requires</b>: node is a node of the tree but it's not the header.
321 //! <b>Effects</b>: Returns the number of nodes of the subtree.
323 //! <b>Complexity</b>: Linear time.
325 //! <b>Throws</b>: Nothing.
326 static std::size_t count(const_node_ptr node
)
327 { return tree_algorithms::count(node
); }
329 //! <b>Requires</b>: header is the header node of the tree.
331 //! <b>Effects</b>: Returns the number of nodes above the header.
333 //! <b>Complexity</b>: Linear time.
335 //! <b>Throws</b>: Nothing.
336 static std::size_t size(const_node_ptr header
)
337 { return tree_algorithms::size(header
); }
339 //! <b>Requires</b>: header1 and header2 must be the header nodes
342 //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
343 //! links to the second tree and header2 will have links to the first tree.
345 //! <b>Complexity</b>: Constant.
347 //! <b>Throws</b>: Nothing.
348 static void swap_tree(node_ptr header1
, node_ptr header2
)
349 { return tree_algorithms::swap_tree(header1
, header2
); }
351 //! <b>Requires</b>: "header" must be the header node of a tree.
352 //! "commit_data" must have been obtained from a previous call to
353 //! "insert_unique_check". No objects should have been inserted or erased
354 //! from the set between the "insert_unique_check" that filled "commit_data"
355 //! and the call to "insert_commit".
358 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
359 //! from the "commit_data" that a previous "insert_check" filled.
361 //! <b>Complexity</b>: Constant time.
363 //! <b>Throws</b>: Nothing.
365 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
366 //! previously executed to fill "commit_data". No value should be inserted or
367 //! erased between the "insert_check" and "insert_commit" calls.
368 static void insert_unique_commit
369 (node_ptr header
, node_ptr new_value
, const insert_commit_data
&commit_data
)
370 { tree_algorithms::insert_unique_commit(header
, new_value
, commit_data
); }
372 //! <b>Requires</b>: "header" must be the header node of a tree.
373 //! KeyNodePtrCompare is a function object that induces a strict weak
374 //! ordering compatible with the strict weak ordering used to create the
375 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
377 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
378 //! tree according to "comp" and obtains the needed information to realize
379 //! a constant-time node insertion if there is no equivalent node.
381 //! <b>Returns</b>: If there is an equivalent value
382 //! returns a pair containing a node_ptr to the already present node
383 //! and false. If there is not equivalent key can be inserted returns true
384 //! in the returned pair's boolean and fills "commit_data" that is meant to
385 //! be used with the "insert_commit" function to achieve a constant-time
386 //! insertion function.
388 //! <b>Complexity</b>: Average complexity is at most logarithmic.
390 //! <b>Throws</b>: If "comp" throws.
392 //! <b>Notes</b>: This function is used to improve performance when constructing
393 //! a node is expensive and the user does not want to have two equivalent nodes
394 //! in the tree: if there is an equivalent value
395 //! the constructed object must be discarded. Many times, the part of the
396 //! node that is used to impose the order is much cheaper to construct
397 //! than the node and this function offers the possibility to use that part
398 //! to check if the insertion will be successful.
400 //! If the check is successful, the user can construct the node and use
401 //! "insert_commit" to insert the node in constant-time. This gives a total
402 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
404 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
405 //! if no more objects are inserted or erased from the set.
406 template<class KeyType
, class KeyNodePtrCompare
>
407 static std::pair
<node_ptr
, bool> insert_unique_check
408 (node_ptr header
, const KeyType
&key
409 ,KeyNodePtrCompare comp
, insert_commit_data
&commit_data
)
411 splay_down(header
, key
, comp
);
412 return tree_algorithms::insert_unique_check(header
, key
, comp
, commit_data
);
415 template<class KeyType
, class KeyNodePtrCompare
>
416 static std::pair
<node_ptr
, bool> insert_unique_check
417 (node_ptr header
, node_ptr hint
, const KeyType
&key
418 ,KeyNodePtrCompare comp
, insert_commit_data
&commit_data
)
420 splay_down(header
, key
, comp
);
421 return tree_algorithms::insert_unique_check(header
, hint
, key
, comp
, commit_data
);
424 static bool is_header(const_node_ptr p
)
425 { return tree_algorithms::is_header(p
); }
427 //! <b>Requires</b>: "header" must be the header node of a tree.
428 //! KeyNodePtrCompare is a function object that induces a strict weak
429 //! ordering compatible with the strict weak ordering used to create the
430 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
432 //! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
433 //! "key" according to "comp" or "header" if that element does not exist.
435 //! <b>Complexity</b>: Logarithmic.
437 //! <b>Throws</b>: If "comp" throws.
438 template<class KeyType
, class KeyNodePtrCompare
>
440 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
, bool splay
= true)
443 splay_down(uncast(header
), key
, comp
);
444 node_ptr end
= uncast(header
);
445 node_ptr y
= lower_bound(header
, key
, comp
, false);
446 node_ptr r
= (y
== end
|| comp(key
, y
)) ? end
: y
;
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
, bool splay
= true)
468 //splay_down(uncast(header), key, comp);
469 std::pair
<node_ptr
, node_ptr
> ret
=
470 tree_algorithms::equal_range(header
, key
, comp
);
473 splay_up(ret
.first
, uncast(header
));
477 //! <b>Requires</b>: "header" must be the header node of a tree.
478 //! KeyNodePtrCompare is a function object that induces a strict weak
479 //! ordering compatible with the strict weak ordering used to create the
480 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
482 //! <b>Effects</b>: Returns an node_ptr to the first element that is
483 //! not less than "key" according to "comp" or "header" if that element does
486 //! <b>Complexity</b>: Logarithmic.
488 //! <b>Throws</b>: If "comp" throws.
489 template<class KeyType
, class KeyNodePtrCompare
>
490 static node_ptr lower_bound
491 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
, bool splay
= true)
494 //splay_down(uncast(header), key, comp);
495 node_ptr y
= tree_algorithms::lower_bound(header
, key
, comp
);
497 splay_up(y
, uncast(header
));
501 //! <b>Requires</b>: "header" must be the header node of a tree.
502 //! KeyNodePtrCompare is a function object that induces a strict weak
503 //! ordering compatible with the strict weak ordering used to create the
504 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
506 //! <b>Effects</b>: Returns an node_ptr to the first element that is greater
507 //! than "key" according to "comp" or "header" if that element does not exist.
509 //! <b>Complexity</b>: Logarithmic.
511 //! <b>Throws</b>: If "comp" throws.
512 template<class KeyType
, class KeyNodePtrCompare
>
513 static node_ptr upper_bound
514 (const_node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
, bool splay
= true)
517 //splay_down(uncast(header), key, comp);
518 node_ptr y
= tree_algorithms::upper_bound(header
, key
, comp
);
520 splay_up(y
, uncast(header
));
524 //! <b>Requires</b>: "header" must be the header node of a tree.
525 //! NodePtrCompare is a function object that induces a strict weak
526 //! ordering compatible with the strict weak ordering used to create the
527 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
528 //! the "header"'s tree.
530 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
531 //! where it will be inserted. If "hint" is the upper_bound
532 //! the insertion takes constant time (two comparisons in the worst case).
534 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
535 //! constant time if new_node is inserted immediately before "hint".
537 //! <b>Throws</b>: If "comp" throws.
538 template<class NodePtrCompare
>
539 static node_ptr insert_equal
540 (node_ptr header
, node_ptr hint
, node_ptr new_node
, NodePtrCompare comp
)
542 splay_down(header
, new_node
, comp
);
543 return tree_algorithms::insert_equal(header
, hint
, new_node
, comp
);
546 template<class NodePtrCompare
>
547 static node_ptr insert_equal_upper_bound
548 (node_ptr header
, node_ptr new_node
, NodePtrCompare comp
)
550 splay_down(header
, new_node
, comp
);
551 return tree_algorithms::insert_equal_upper_bound(header
, new_node
, comp
);
554 template<class NodePtrCompare
>
555 static node_ptr insert_equal_lower_bound
556 (node_ptr header
, node_ptr new_node
, NodePtrCompare comp
)
558 splay_down(header
, new_node
, comp
);
559 return tree_algorithms::insert_equal_lower_bound(header
, new_node
, comp
);
562 //! <b>Requires</b>: "cloner" must be a function
563 //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
564 //! take a node_ptr and shouldn't throw.
566 //! <b>Effects</b>: First empties target tree calling
567 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
568 //! except the header.
570 //! Then, duplicates the entire tree pointed by "source_header" cloning each
571 //! source node with <tt>node_ptr Cloner::operator()(node_ptr)</tt> to obtain
572 //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
573 //! are disposed using <tt>void disposer(node_ptr)</tt>.
575 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
576 //! number of elements of tree target tree when calling this function.
578 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
579 template <class Cloner
, class Disposer
>
581 (const_node_ptr source_header
, node_ptr target_header
, Cloner cloner
, Disposer disposer
)
582 { tree_algorithms::clone(source_header
, target_header
, cloner
, disposer
); }
584 // delete node | complexity : constant | exception : nothrow
585 static void erase(node_ptr header
, node_ptr z
, bool splay
= true)
587 // node_base* n = t->right;
588 // if( t->left != 0 ){
589 // node_base* l = t->previous();
590 // splay_up( l , t );
592 // n->right = t->right;
593 // if( n->right != 0 )
594 // n->right->parent = n;
598 // n->parent = t->parent;
600 // if( t->parent->left == t )
601 // t->parent->left = n;
602 // else // must be ( t->parent->right == t )
603 // t->parent->right = n;
605 // if( data_->parent == t )
606 // data_->parent = find_leftmost();
608 if(splay
&& NodeTraits::get_left(z
) != 0 ){
609 node_ptr l
= prev_node(z
);
614 if(splay && NodeTraits::get_left(z) != 0 ){
615 node_ptr l = NodeTraits::get_left(z);
618 if(splay && NodeTraits::get_left(z) != 0 ){
619 node_ptr l = prev_node(z);
629 //splay_up(z, header);
630 tree_algorithms::erase(header
, z
);
633 // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow
634 static void splay_up(node_ptr n
, node_ptr header
)
636 if(n
== header
){ // do a splay for the right most node instead
637 // this is to boost performance of equal_range/count on equivalent containers in the case
638 // where there are many equal elements at the end
639 n
= NodeTraits::get_right(header
);
647 node_ptr p
= NodeTraits::get_parent(n
);
648 node_ptr g
= NodeTraits::get_parent(p
);
656 else if ((NodeTraits::get_left(p
) == n
&& NodeTraits::get_left(g
) == p
) ||
657 (NodeTraits::get_right(p
) == n
&& NodeTraits::get_right(g
) == p
) ){
670 // top-down splay | complexity : logarithmic | exception : strong, note A
671 template<class KeyType
, class KeyNodePtrCompare
>
672 static node_ptr
splay_down(node_ptr header
, const KeyType
&key
, KeyNodePtrCompare comp
)
674 if(!NodeTraits::get_parent(header
))
676 //Most splay tree implementations use a dummy/null node to implement.
677 //this function. This has some problems for a generic library like Intrusive:
679 // * The node might not have a default constructor.
680 // * The default constructor could throw.
682 //We already have a header node. Leftmost and rightmost nodes of the tree
683 //are not changed when splaying (because the invariants of the tree don't
684 //change) We can back up them, use the header as the null node and
685 //reassign old values after the function has been completed.
686 node_ptr t
= NodeTraits::get_parent(header
);
687 //Check if tree has a single node
688 if(!NodeTraits::get_left(t
) && !NodeTraits::get_right(t
))
690 //Backup leftmost/rightmost
691 node_ptr leftmost
= NodeTraits::get_left(header
);
692 node_ptr rightmost
= NodeTraits::get_right(header
);
695 detail::splaydown_rollback
<NodeTraits
> rollback(&t
, header
, leftmost
, rightmost
);
696 node_ptr null
= header
;
702 if(NodeTraits::get_left(t
) == 0 )
704 if(comp(key
, NodeTraits::get_left(t
))){
705 t
= tree_algorithms::rotate_right(t
);
707 if(NodeTraits::get_left(t
) == 0)
711 else if(comp(NodeTraits::get_left(t
), key
)){
714 if(NodeTraits::get_right(t
) == 0 )
722 else if(comp(t
, key
)){
723 if(NodeTraits::get_right(t
) == 0 )
726 if(comp(NodeTraits::get_right(t
), key
)){
727 t
= tree_algorithms::rotate_left( t
);
729 if(NodeTraits::get_right(t
) == 0 )
733 else if(comp(key
, NodeTraits::get_right(t
))){
736 if(NodeTraits::get_left(t
) == 0)
750 assemble(t
, l
, r
, null
);
754 //t is the current root
755 NodeTraits::set_parent(header
, t
);
756 NodeTraits::set_parent(t
, header
);
757 //Recover leftmost/rightmost pointers
758 NodeTraits::set_left (header
, leftmost
);
759 NodeTraits::set_right(header
, rightmost
);
763 //! <b>Requires</b>: header must be the header of a tree.
765 //! <b>Effects</b>: Rebalances the tree.
767 //! <b>Throws</b>: Nothing.
769 //! <b>Complexity</b>: Linear.
770 static void rebalance(node_ptr header
)
771 { tree_algorithms::rebalance(header
); }
773 //! <b>Requires</b>: old_root is a node of a tree.
775 //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
777 //! <b>Returns</b>: The new root of the subtree.
779 //! <b>Throws</b>: Nothing.
781 //! <b>Complexity</b>: Linear.
782 static node_ptr
rebalance_subtree(node_ptr old_root
)
783 { return tree_algorithms::rebalance_subtree(old_root
); }
786 //! <b>Requires</b>: "n" must be a node inserted in a tree.
788 //! <b>Effects</b>: Returns a pointer to the header node of the tree.
790 //! <b>Complexity</b>: Logarithmic.
792 //! <b>Throws</b>: Nothing.
793 static node_ptr
get_header(node_ptr n
)
794 { return tree_algorithms::get_header(n
); }
800 // assemble the three sub-trees into new tree pointed to by t | complexity : constant | exception : nothrow
801 static void assemble( node_ptr t
, node_ptr l
, node_ptr r
, const_node_ptr null_node
)
803 NodeTraits::set_right(l
, NodeTraits::get_left(t
));
804 NodeTraits::set_left(r
, NodeTraits::get_right(t
));
806 if(NodeTraits::get_right(l
) != 0){
807 NodeTraits::set_parent(NodeTraits::get_right(l
), l
);
810 if(NodeTraits::get_left(r
) != 0){
811 NodeTraits::set_parent(NodeTraits::get_left(r
), r
);
814 NodeTraits::set_left (t
, NodeTraits::get_right(null_node
));
815 NodeTraits::set_right(t
, NodeTraits::get_left(null_node
));
817 if( NodeTraits::get_left(t
) != 0 ){
818 NodeTraits::set_parent(NodeTraits::get_left(t
), t
);
821 if( NodeTraits::get_right(t
) ){
822 NodeTraits::set_parent(NodeTraits::get_right(t
), t
);
826 // break link to left child node and attach it to left tree pointed to by l | complexity : constant | exception : nothrow
827 static void link_left(node_ptr
& t
, node_ptr
& l
)
829 NodeTraits::set_right(l
, t
);
830 NodeTraits::set_parent(t
, l
);
832 t
= NodeTraits::get_right(t
);
835 // break link to right child node and attach it to right tree pointed to by r | complexity : constant | exception : nothrow
836 static void link_right(node_ptr
& t
, node_ptr
& r
)
838 NodeTraits::set_left(r
, t
);
839 NodeTraits::set_parent(t
, r
);
841 t
= NodeTraits::get_left(t
);
844 // rotate n with its parent | complexity : constant | exception : nothrow
845 static void rotate(node_ptr n
)
847 node_ptr p
= NodeTraits::get_parent(n
);
848 node_ptr g
= NodeTraits::get_parent(p
);
849 //Test if g is header before breaking tree
850 //invariants that would make is_header invalid
851 bool g_is_header
= is_header(g
);
853 if(NodeTraits::get_left(p
) == n
){
854 NodeTraits::set_left(p
, NodeTraits::get_right(n
));
855 if(NodeTraits::get_left(p
) != 0)
856 NodeTraits::set_parent(NodeTraits::get_left(p
), p
);
857 NodeTraits::set_right(n
, p
);
859 else{ // must be ( p->right == n )
860 NodeTraits::set_right(p
, NodeTraits::get_left(n
));
861 if(NodeTraits::get_right(p
) != 0)
862 NodeTraits::set_parent(NodeTraits::get_right(p
), p
);
863 NodeTraits::set_left(n
, p
);
866 NodeTraits::set_parent(p
, n
);
867 NodeTraits::set_parent(n
, g
);
870 if(NodeTraits::get_parent(g
) == p
)
871 NodeTraits::set_parent(g
, n
);
872 else{//must be ( g->right == p )
873 BOOST_INTRUSIVE_INVARIANT_ASSERT(0);
874 NodeTraits::set_right(g
, n
);
878 if(NodeTraits::get_left(g
) == p
)
879 NodeTraits::set_left(g
, n
);
880 else //must be ( g->right == p )
881 NodeTraits::set_right(g
, n
);
888 } //namespace intrusive
891 #include <boost/intrusive/detail/config_end.hpp>
893 #endif //BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP