fix doc example typo
[boost.git] / boost / intrusive / splaytree_algorithms.hpp
blobe727ab6d13054a1825f3464b91a784356c086053
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007.
4 //
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)
8 //
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.
19 //
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>
52 #include <cstddef>
53 #include <boost/intrusive/detail/utilities.hpp>
54 #include <boost/intrusive/detail/tree_algorithms.hpp>
56 namespace boost {
57 namespace intrusive {
59 /// @cond
60 namespace detail {
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)
72 void release()
73 { pcur_subtree_ = 0; }
75 ~splaydown_rollback()
77 if(pcur_subtree_){
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 {
93 /// @endcond
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
97 //!
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:
107 //! <b>Typedefs</b>:
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>
118 //!
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>
122 //!
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>
126 //!
127 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
128 template<class NodeTraits>
129 class splaytree_algorithms
131 /// @cond
132 private:
133 typedef detail::tree_algorithms<NodeTraits> tree_algorithms;
134 /// @endcond
136 public:
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;
146 /// @cond
147 private:
148 static node_ptr uncast(const_node_ptr ptr)
150 return node_ptr(const_cast<node*>(::boost::intrusive::detail::get_pointer(ptr)));
152 /// @endcond
154 public:
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
162 //! by init(...).
163 //!
164 //! <b>Effects</b>: Returns true if the node is initialized by init().
165 //!
166 //! <b>Complexity</b>: Constant time.
167 //!
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
176 //! of two trees.
177 //!
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.
181 //!
182 //! <b>Complexity</b>: Logarithmic.
183 //!
184 //! <b>Throws</b>: Nothing.
185 //!
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)
192 if(node1 == node2)
193 return;
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.
201 //!
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.
205 //!
206 //! <b>Complexity</b>: Constant.
207 //!
208 //! <b>Throws</b>: Nothing.
209 //!
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.
219 //!
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
222 //!
223 //! <b>Complexity</b>: Logarithmic.
224 //!
225 //! <b>Throws</b>: Nothing.
226 //!
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)
236 return;
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.
242 //!
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
245 //!
246 //! <b>Complexity</b>: Constant.
247 //!
248 //! <b>Throws</b>: Nothing.
249 //!
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.
260 //!
261 //! <b>Effects</b>: Returns the next node of the tree.
262 //!
263 //! <b>Complexity</b>: Average constant time.
264 //!
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.
270 //!
271 //! <b>Effects</b>: Returns the previous node of the tree.
272 //!
273 //! <b>Complexity</b>: Average constant time.
274 //!
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.
282 //!
283 //! <b>Complexity</b>: Constant.
284 //!
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.
295 //!
296 //! <b>Complexity</b>: Constant.
297 //!
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.
310 //!
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.
313 //!
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.
320 //!
321 //! <b>Effects</b>: Returns the number of nodes of the subtree.
322 //!
323 //! <b>Complexity</b>: Linear time.
324 //!
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.
330 //!
331 //! <b>Effects</b>: Returns the number of nodes above the header.
332 //!
333 //! <b>Complexity</b>: Linear time.
334 //!
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
340 //! of two trees.
341 //!
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.
344 //!
345 //! <b>Complexity</b>: Constant.
346 //!
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".
356 //!
357 //!
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.
364 //!
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.
376 //!
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.
387 //!
388 //! <b>Complexity</b>: Average complexity is at most logarithmic.
390 //! <b>Throws</b>: If "comp" throws.
391 //!
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.
436 //!
437 //! <b>Throws</b>: If "comp" throws.
438 template<class KeyType, class KeyNodePtrCompare>
439 static node_ptr find
440 (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true)
442 if(splay)
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;
447 return r;
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.
461 //!
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)
467 //if(splay)
468 //splay_down(uncast(header), key, comp);
469 std::pair<node_ptr, node_ptr> ret =
470 tree_algorithms::equal_range(header, key, comp);
472 if(splay)
473 splay_up(ret.first, uncast(header));
474 return ret;
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
484 //! not exist.
486 //! <b>Complexity</b>: Logarithmic.
487 //!
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)
493 //if(splay)
494 //splay_down(uncast(header), key, comp);
495 node_ptr y = tree_algorithms::lower_bound(header, key, comp);
496 if(splay)
497 splay_up(y, uncast(header));
498 return y;
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.
510 //!
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)
516 //if(splay)
517 //splay_down(uncast(header), key, comp);
518 node_ptr y = tree_algorithms::upper_bound(header, key, comp);
519 if(splay)
520 splay_up(y, uncast(header));
521 return y;
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.
529 //!
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".
536 //!
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.
569 //!
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>.
574 //!
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.
577 //!
578 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
579 template <class Cloner, class Disposer>
580 static void clone
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 );
591 // n = t->left;
592 // n->right = t->right;
593 // if( n->right != 0 )
594 // n->right->parent = n;
595 // }
597 // if( n != 0 )
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();
607 //posibility 1
608 if(splay && NodeTraits::get_left(z) != 0 ){
609 node_ptr l = prev_node(z);
610 splay_up(l, header);
613 //possibility 2
614 if(splay && NodeTraits::get_left(z) != 0 ){
615 node_ptr l = NodeTraits::get_left(z);
616 splay_up(l, header);
617 }*//*
618 if(splay && NodeTraits::get_left(z) != 0 ){
619 node_ptr l = prev_node(z);
620 splay_up_impl(l, z);
623 //possibility 4
624 if(splay){
625 splay_up(z, header);
628 //if(splay)
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);
642 node_ptr t = header;
644 if( n == t ) return;
646 for( ;; ){
647 node_ptr p = NodeTraits::get_parent(n);
648 node_ptr g = NodeTraits::get_parent(p);
650 if( p == t ) break;
652 if( g == t ){
653 // zig
654 rotate(n);
656 else if ((NodeTraits::get_left(p) == n && NodeTraits::get_left(g) == p) ||
657 (NodeTraits::get_right(p) == n && NodeTraits::get_right(g) == p) ){
658 // zig-zig
659 rotate(p);
660 rotate(n);
662 else{
663 // zig-zag
664 rotate(n);
665 rotate(n);
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))
675 return 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))
689 return 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;
697 node_ptr l = null;
698 node_ptr r = null;
700 for( ;; ){
701 if(comp(key, t)){
702 if(NodeTraits::get_left(t) == 0 )
703 break;
704 if(comp(key, NodeTraits::get_left(t))){
705 t = tree_algorithms::rotate_right(t);
707 if(NodeTraits::get_left(t) == 0)
708 break;
709 link_right(t, r);
711 else if(comp(NodeTraits::get_left(t), key)){
712 link_right(t, r);
714 if(NodeTraits::get_right(t) == 0 )
715 break;
716 link_left(t, l);
718 else{
719 link_right(t, r);
722 else if(comp(t, key)){
723 if(NodeTraits::get_right(t) == 0 )
724 break;
726 if(comp(NodeTraits::get_right(t), key)){
727 t = tree_algorithms::rotate_left( t );
729 if(NodeTraits::get_right(t) == 0 )
730 break;
731 link_left(t, l);
733 else if(comp(key, NodeTraits::get_right(t))){
734 link_left(t, l);
736 if(NodeTraits::get_left(t) == 0)
737 break;
739 link_right(t, r);
741 else{
742 link_left(t, l);
745 else{
746 break;
750 assemble(t, l, r, null);
751 rollback.release();
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);
760 return t;
763 //! <b>Requires</b>: header must be the header of a tree.
764 //!
765 //! <b>Effects</b>: Rebalances the tree.
766 //!
767 //! <b>Throws</b>: Nothing.
768 //!
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.
774 //!
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.
780 //!
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.
791 //!
792 //! <b>Throws</b>: Nothing.
793 static node_ptr get_header(node_ptr n)
794 { return tree_algorithms::get_header(n); }
796 private:
798 /// @cond
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);
831 l = t;
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);
840 r = t;
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);
869 if(g_is_header){
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);
877 else{
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);
885 /// @endcond
888 } //namespace intrusive
889 } //namespace boost
891 #include <boost/intrusive/detail/config_end.hpp>
893 #endif //BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP