]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/intrusive/include/boost/intrusive/bstree_algorithms.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / intrusive / include / boost / intrusive / bstree_algorithms.hpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-2014
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.
10 //
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
14 #define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
15
16 #include <cstddef>
17 #include <boost/intrusive/detail/config_begin.hpp>
18 #include <boost/intrusive/intrusive_fwd.hpp>
19 #include <boost/intrusive/detail/bstree_algorithms_base.hpp>
20 #include <boost/intrusive/detail/assert.hpp>
21 #include <boost/intrusive/detail/uncast.hpp>
22 #include <boost/intrusive/detail/math.hpp>
23 #include <boost/intrusive/detail/algo_type.hpp>
24
25 #include <boost/intrusive/detail/minimal_pair_header.hpp>
26
27 #if defined(BOOST_HAS_PRAGMA_ONCE)
28 # pragma once
29 #endif
30
31 namespace boost {
32 namespace intrusive {
33
34 /// @cond
35
36 //! This type is the information that will be filled by insert_unique_check
37 template <class NodePtr>
38 struct insert_commit_data_t
39 {
40 bool link_left;
41 NodePtr node;
42 };
43
44 template <class NodePtr>
45 struct data_for_rebalance_t
46 {
47 NodePtr x;
48 NodePtr x_parent;
49 NodePtr y;
50 };
51
52 namespace detail {
53
54 template<class ValueTraits, class NodePtrCompare, class ExtraChecker>
55 struct bstree_node_checker
56 : public ExtraChecker
57 {
58 typedef ExtraChecker base_checker_t;
59 typedef ValueTraits value_traits;
60 typedef typename value_traits::node_traits node_traits;
61 typedef typename node_traits::const_node_ptr const_node_ptr;
62
63 struct return_type
64 : public base_checker_t::return_type
65 {
66 BOOST_INTRUSIVE_FORCEINLINE return_type()
67 : min_key_node_ptr(const_node_ptr()), max_key_node_ptr(const_node_ptr()), node_count(0)
68 {}
69
70 const_node_ptr min_key_node_ptr;
71 const_node_ptr max_key_node_ptr;
72 size_t node_count;
73 };
74
75 BOOST_INTRUSIVE_FORCEINLINE bstree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker)
76 : base_checker_t(extra_checker), comp_(comp)
77 {}
78
79 void operator () (const const_node_ptr& p,
80 const return_type& check_return_left, const return_type& check_return_right,
81 return_type& check_return)
82 {
83 if (check_return_left.max_key_node_ptr)
84 BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(p, check_return_left.max_key_node_ptr));
85 if (check_return_right.min_key_node_ptr)
86 BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(check_return_right.min_key_node_ptr, p));
87 check_return.min_key_node_ptr = node_traits::get_left(p)? check_return_left.min_key_node_ptr : p;
88 check_return.max_key_node_ptr = node_traits::get_right(p)? check_return_right.max_key_node_ptr : p;
89 check_return.node_count = check_return_left.node_count + check_return_right.node_count + 1;
90 base_checker_t::operator()(p, check_return_left, check_return_right, check_return);
91 }
92
93 const NodePtrCompare comp_;
94 };
95
96 } // namespace detail
97
98 /// @endcond
99
100
101
102 //! This is an implementation of a binary search tree.
103 //! A node in the search tree has references to its children and its parent. This
104 //! is to allow traversal of the whole tree from a given node making the
105 //! implementation of iterator a pointer to a node.
106 //! At the top of the tree a node is used specially. This node's parent pointer
107 //! is pointing to the root of the tree. Its left pointer points to the
108 //! leftmost node in the tree and the right pointer to the rightmost one.
109 //! This node is used to represent the end-iterator.
110 //!
111 //! +---------+
112 //! header------------------------------>| |
113 //! | |
114 //! +----------(left)--------| |--------(right)---------+
115 //! | +---------+ |
116 //! | | |
117 //! | | (parent) |
118 //! | | |
119 //! | | |
120 //! | +---------+ |
121 //! root of tree ..|......................> | | |
122 //! | | D | |
123 //! | | | |
124 //! | +-------+---------+-------+ |
125 //! | | | |
126 //! | | | |
127 //! | | | |
128 //! | | | |
129 //! | | | |
130 //! | +---------+ +---------+ |
131 //! | | | | | |
132 //! | | B | | F | |
133 //! | | | | | |
134 //! | +--+---------+--+ +--+---------+--+ |
135 //! | | | | | |
136 //! | | | | | |
137 //! | | | | | |
138 //! | +---+-----+ +-----+---+ +---+-----+ +-----+---+ |
139 //! +-->| | | | | | | |<--+
140 //! | A | | C | | E | | G |
141 //! | | | | | | | |
142 //! +---------+ +---------+ +---------+ +---------+
143 //!
144 //! bstree_algorithms is configured with a NodeTraits class, which encapsulates the
145 //! information about the node to be manipulated. NodeTraits must support the
146 //! following interface:
147 //!
148 //! <b>Typedefs</b>:
149 //!
150 //! <tt>node</tt>: The type of the node that forms the binary search tree
151 //!
152 //! <tt>node_ptr</tt>: A pointer to a node
153 //!
154 //! <tt>const_node_ptr</tt>: A pointer to a const node
155 //!
156 //! <b>Static functions</b>:
157 //!
158 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
159 //!
160 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
161 //!
162 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
163 //!
164 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
165 //!
166 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
167 //!
168 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
169 template<class NodeTraits>
170 class bstree_algorithms : public bstree_algorithms_base<NodeTraits>
171 {
172 public:
173 typedef typename NodeTraits::node node;
174 typedef NodeTraits node_traits;
175 typedef typename NodeTraits::node_ptr node_ptr;
176 typedef typename NodeTraits::const_node_ptr const_node_ptr;
177 typedef insert_commit_data_t<node_ptr> insert_commit_data;
178 typedef data_for_rebalance_t<node_ptr> data_for_rebalance;
179
180 /// @cond
181 typedef bstree_algorithms<NodeTraits> this_type;
182 typedef bstree_algorithms_base<NodeTraits> base_type;
183 private:
184 template<class Disposer>
185 struct dispose_subtree_disposer
186 {
187 BOOST_INTRUSIVE_FORCEINLINE dispose_subtree_disposer(Disposer &disp, const node_ptr & subtree)
188 : disposer_(&disp), subtree_(subtree)
189 {}
190
191 BOOST_INTRUSIVE_FORCEINLINE void release()
192 { disposer_ = 0; }
193
194 BOOST_INTRUSIVE_FORCEINLINE ~dispose_subtree_disposer()
195 {
196 if(disposer_){
197 dispose_subtree(subtree_, *disposer_);
198 }
199 }
200 Disposer *disposer_;
201 const node_ptr subtree_;
202 };
203
204 /// @endcond
205
206 public:
207 //! <b>Requires</b>: 'header' is the header node of a tree.
208 //!
209 //! <b>Effects</b>: Returns the first node of the tree, the header if the tree is empty.
210 //!
211 //! <b>Complexity</b>: Constant time.
212 //!
213 //! <b>Throws</b>: Nothing.
214 BOOST_INTRUSIVE_FORCEINLINE static node_ptr begin_node(const const_node_ptr & header)
215 { return node_traits::get_left(header); }
216
217 //! <b>Requires</b>: 'header' is the header node of a tree.
218 //!
219 //! <b>Effects</b>: Returns the header of the tree.
220 //!
221 //! <b>Complexity</b>: Constant time.
222 //!
223 //! <b>Throws</b>: Nothing.
224 BOOST_INTRUSIVE_FORCEINLINE static node_ptr end_node(const const_node_ptr & header)
225 { return detail::uncast(header); }
226
227 //! <b>Requires</b>: 'header' is the header node of a tree.
228 //!
229 //! <b>Effects</b>: Returns the root of the tree if any, header otherwise
230 //!
231 //! <b>Complexity</b>: Constant time.
232 //!
233 //! <b>Throws</b>: Nothing.
234 BOOST_INTRUSIVE_FORCEINLINE static node_ptr root_node(const const_node_ptr & header)
235 {
236 node_ptr p = node_traits::get_parent(header);
237 return p ? p : detail::uncast(header);
238 }
239
240 //! <b>Requires</b>: 'node' is a node of the tree or a node initialized
241 //! by init(...) or init_node.
242 //!
243 //! <b>Effects</b>: Returns true if the node is initialized by init() or init_node().
244 //!
245 //! <b>Complexity</b>: Constant time.
246 //!
247 //! <b>Throws</b>: Nothing.
248 BOOST_INTRUSIVE_FORCEINLINE static bool unique(const const_node_ptr & node)
249 { return !NodeTraits::get_parent(node); }
250
251 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
252 //! <b>Requires</b>: 'node' is a node of the tree or a header node.
253 //!
254 //! <b>Effects</b>: Returns the header of the tree.
255 //!
256 //! <b>Complexity</b>: Logarithmic.
257 //!
258 //! <b>Throws</b>: Nothing.
259 static node_ptr get_header(const const_node_ptr & node);
260 #endif
261
262 //! <b>Requires</b>: node1 and node2 can't be header nodes
263 //! of two trees.
264 //!
265 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
266 //! in the position node2 before the function. node2 will be inserted in the
267 //! position node1 had before the function.
268 //!
269 //! <b>Complexity</b>: Logarithmic.
270 //!
271 //! <b>Throws</b>: Nothing.
272 //!
273 //! <b>Note</b>: This function will break container ordering invariants if
274 //! node1 and node2 are not equivalent according to the ordering rules.
275 //!
276 //!Experimental function
277 static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
278 {
279 if(node1 == node2)
280 return;
281
282 node_ptr header1(base_type::get_header(node1)), header2(base_type::get_header(node2));
283 swap_nodes(node1, header1, node2, header2);
284 }
285
286 //! <b>Requires</b>: node1 and node2 can't be header nodes
287 //! of two trees with header header1 and header2.
288 //!
289 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
290 //! in the position node2 before the function. node2 will be inserted in the
291 //! position node1 had before the function.
292 //!
293 //! <b>Complexity</b>: Constant.
294 //!
295 //! <b>Throws</b>: Nothing.
296 //!
297 //! <b>Note</b>: This function will break container ordering invariants if
298 //! node1 and node2 are not equivalent according to the ordering rules.
299 //!
300 //!Experimental function
301 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
302 {
303 if(node1 == node2)
304 return;
305
306 //node1 and node2 must not be header nodes
307 //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2));
308 if(header1 != header2){
309 //Update header1 if necessary
310 if(node1 == NodeTraits::get_left(header1)){
311 NodeTraits::set_left(header1, node2);
312 }
313
314 if(node1 == NodeTraits::get_right(header1)){
315 NodeTraits::set_right(header1, node2);
316 }
317
318 if(node1 == NodeTraits::get_parent(header1)){
319 NodeTraits::set_parent(header1, node2);
320 }
321
322 //Update header2 if necessary
323 if(node2 == NodeTraits::get_left(header2)){
324 NodeTraits::set_left(header2, node1);
325 }
326
327 if(node2 == NodeTraits::get_right(header2)){
328 NodeTraits::set_right(header2, node1);
329 }
330
331 if(node2 == NodeTraits::get_parent(header2)){
332 NodeTraits::set_parent(header2, node1);
333 }
334 }
335 else{
336 //If both nodes are from the same tree
337 //Update header if necessary
338 if(node1 == NodeTraits::get_left(header1)){
339 NodeTraits::set_left(header1, node2);
340 }
341 else if(node2 == NodeTraits::get_left(header2)){
342 NodeTraits::set_left(header2, node1);
343 }
344
345 if(node1 == NodeTraits::get_right(header1)){
346 NodeTraits::set_right(header1, node2);
347 }
348 else if(node2 == NodeTraits::get_right(header2)){
349 NodeTraits::set_right(header2, node1);
350 }
351
352 if(node1 == NodeTraits::get_parent(header1)){
353 NodeTraits::set_parent(header1, node2);
354 }
355 else if(node2 == NodeTraits::get_parent(header2)){
356 NodeTraits::set_parent(header2, node1);
357 }
358
359 //Adjust data in nodes to be swapped
360 //so that final link swap works as expected
361 if(node1 == NodeTraits::get_parent(node2)){
362 NodeTraits::set_parent(node2, node2);
363
364 if(node2 == NodeTraits::get_right(node1)){
365 NodeTraits::set_right(node1, node1);
366 }
367 else{
368 NodeTraits::set_left(node1, node1);
369 }
370 }
371 else if(node2 == NodeTraits::get_parent(node1)){
372 NodeTraits::set_parent(node1, node1);
373
374 if(node1 == NodeTraits::get_right(node2)){
375 NodeTraits::set_right(node2, node2);
376 }
377 else{
378 NodeTraits::set_left(node2, node2);
379 }
380 }
381 }
382
383 //Now swap all the links
384 node_ptr temp;
385 //swap left link
386 temp = NodeTraits::get_left(node1);
387 NodeTraits::set_left(node1, NodeTraits::get_left(node2));
388 NodeTraits::set_left(node2, temp);
389 //swap right link
390 temp = NodeTraits::get_right(node1);
391 NodeTraits::set_right(node1, NodeTraits::get_right(node2));
392 NodeTraits::set_right(node2, temp);
393 //swap parent link
394 temp = NodeTraits::get_parent(node1);
395 NodeTraits::set_parent(node1, NodeTraits::get_parent(node2));
396 NodeTraits::set_parent(node2, temp);
397
398 //Now adjust adjacent nodes for newly inserted node 1
399 if((temp = NodeTraits::get_left(node1))){
400 NodeTraits::set_parent(temp, node1);
401 }
402 if((temp = NodeTraits::get_right(node1))){
403 NodeTraits::set_parent(temp, node1);
404 }
405 if((temp = NodeTraits::get_parent(node1)) &&
406 //The header has been already updated so avoid it
407 temp != header2){
408 if(NodeTraits::get_left(temp) == node2){
409 NodeTraits::set_left(temp, node1);
410 }
411 if(NodeTraits::get_right(temp) == node2){
412 NodeTraits::set_right(temp, node1);
413 }
414 }
415 //Now adjust adjacent nodes for newly inserted node 2
416 if((temp = NodeTraits::get_left(node2))){
417 NodeTraits::set_parent(temp, node2);
418 }
419 if((temp = NodeTraits::get_right(node2))){
420 NodeTraits::set_parent(temp, node2);
421 }
422 if((temp = NodeTraits::get_parent(node2)) &&
423 //The header has been already updated so avoid it
424 temp != header1){
425 if(NodeTraits::get_left(temp) == node1){
426 NodeTraits::set_left(temp, node2);
427 }
428 if(NodeTraits::get_right(temp) == node1){
429 NodeTraits::set_right(temp, node2);
430 }
431 }
432 }
433
434 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
435 //! and new_node must not be inserted in a tree.
436 //!
437 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
438 //! tree with new_node. The tree does not need to be rebalanced
439 //!
440 //! <b>Complexity</b>: Logarithmic.
441 //!
442 //! <b>Throws</b>: Nothing.
443 //!
444 //! <b>Note</b>: This function will break container ordering invariants if
445 //! new_node is not equivalent to node_to_be_replaced according to the
446 //! ordering rules. This function is faster than erasing and inserting
447 //! the node, since no rebalancing and comparison is needed. Experimental function
448 BOOST_INTRUSIVE_FORCEINLINE static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
449 {
450 if(node_to_be_replaced == new_node)
451 return;
452 replace_node(node_to_be_replaced, base_type::get_header(node_to_be_replaced), new_node);
453 }
454
455 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
456 //! with header "header" and new_node must not be inserted in a tree.
457 //!
458 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
459 //! tree with new_node. The tree does not need to be rebalanced
460 //!
461 //! <b>Complexity</b>: Constant.
462 //!
463 //! <b>Throws</b>: Nothing.
464 //!
465 //! <b>Note</b>: This function will break container ordering invariants if
466 //! new_node is not equivalent to node_to_be_replaced according to the
467 //! ordering rules. This function is faster than erasing and inserting
468 //! the node, since no rebalancing or comparison is needed. Experimental function
469 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
470 {
471 if(node_to_be_replaced == new_node)
472 return;
473
474 //Update header if necessary
475 if(node_to_be_replaced == NodeTraits::get_left(header)){
476 NodeTraits::set_left(header, new_node);
477 }
478
479 if(node_to_be_replaced == NodeTraits::get_right(header)){
480 NodeTraits::set_right(header, new_node);
481 }
482
483 if(node_to_be_replaced == NodeTraits::get_parent(header)){
484 NodeTraits::set_parent(header, new_node);
485 }
486
487 //Now set data from the original node
488 node_ptr temp;
489 NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced));
490 NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced));
491 NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced));
492
493 //Now adjust adjacent nodes for newly inserted node
494 if((temp = NodeTraits::get_left(new_node))){
495 NodeTraits::set_parent(temp, new_node);
496 }
497 if((temp = NodeTraits::get_right(new_node))){
498 NodeTraits::set_parent(temp, new_node);
499 }
500 if((temp = NodeTraits::get_parent(new_node)) &&
501 //The header has been already updated so avoid it
502 temp != header){
503 if(NodeTraits::get_left(temp) == node_to_be_replaced){
504 NodeTraits::set_left(temp, new_node);
505 }
506 if(NodeTraits::get_right(temp) == node_to_be_replaced){
507 NodeTraits::set_right(temp, new_node);
508 }
509 }
510 }
511
512 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
513 //! <b>Requires</b>: 'node' is a node from the tree except the header.
514 //!
515 //! <b>Effects</b>: Returns the next node of the tree.
516 //!
517 //! <b>Complexity</b>: Average constant time.
518 //!
519 //! <b>Throws</b>: Nothing.
520 static node_ptr next_node(const node_ptr & node);
521
522 //! <b>Requires</b>: 'node' is a node from the tree except the leftmost node.
523 //!
524 //! <b>Effects</b>: Returns the previous node of the tree.
525 //!
526 //! <b>Complexity</b>: Average constant time.
527 //!
528 //! <b>Throws</b>: Nothing.
529 static node_ptr prev_node(const node_ptr & node);
530
531 //! <b>Requires</b>: 'node' is a node of a tree but not the header.
532 //!
533 //! <b>Effects</b>: Returns the minimum node of the subtree starting at p.
534 //!
535 //! <b>Complexity</b>: Logarithmic to the size of the subtree.
536 //!
537 //! <b>Throws</b>: Nothing.
538 static node_ptr minimum(node_ptr node);
539
540 //! <b>Requires</b>: 'node' is a node of a tree but not the header.
541 //!
542 //! <b>Effects</b>: Returns the maximum node of the subtree starting at p.
543 //!
544 //! <b>Complexity</b>: Logarithmic to the size of the subtree.
545 //!
546 //! <b>Throws</b>: Nothing.
547 static node_ptr maximum(node_ptr node);
548 #endif
549
550 //! <b>Requires</b>: 'node' must not be part of any tree.
551 //!
552 //! <b>Effects</b>: After the function unique(node) == true.
553 //!
554 //! <b>Complexity</b>: Constant.
555 //!
556 //! <b>Throws</b>: Nothing.
557 //!
558 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
559 BOOST_INTRUSIVE_FORCEINLINE static void init(const node_ptr & node)
560 {
561 NodeTraits::set_parent(node, node_ptr());
562 NodeTraits::set_left(node, node_ptr());
563 NodeTraits::set_right(node, node_ptr());
564 };
565
566 //! <b>Effects</b>: Returns true if node is in the same state as if called init(node)
567 //!
568 //! <b>Complexity</b>: Constant.
569 //!
570 //! <b>Throws</b>: Nothing.
571 BOOST_INTRUSIVE_FORCEINLINE static bool inited(const const_node_ptr & node)
572 {
573 return !NodeTraits::get_parent(node) &&
574 !NodeTraits::get_left(node) &&
575 !NodeTraits::get_right(node) ;
576 };
577
578 //! <b>Requires</b>: node must not be part of any tree.
579 //!
580 //! <b>Effects</b>: Initializes the header to represent an empty tree.
581 //! unique(header) == true.
582 //!
583 //! <b>Complexity</b>: Constant.
584 //!
585 //! <b>Throws</b>: Nothing.
586 //!
587 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
588 BOOST_INTRUSIVE_FORCEINLINE static void init_header(const node_ptr & header)
589 {
590 NodeTraits::set_parent(header, node_ptr());
591 NodeTraits::set_left(header, header);
592 NodeTraits::set_right(header, header);
593 }
594
595 //! <b>Requires</b>: "disposer" must be an object function
596 //! taking a node_ptr parameter and shouldn't throw.
597 //!
598 //! <b>Effects</b>: Empties the target tree calling
599 //! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
600 //! except the header.
601 //!
602 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
603 //! number of elements of tree target tree when calling this function.
604 //!
605 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
606 template<class Disposer>
607 static void clear_and_dispose(const node_ptr & header, Disposer disposer)
608 {
609 node_ptr source_root = NodeTraits::get_parent(header);
610 if(!source_root)
611 return;
612 dispose_subtree(source_root, disposer);
613 init_header(header);
614 }
615
616 //! <b>Requires</b>: header is the header of a tree.
617 //!
618 //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
619 //! updates the header link to the new leftmost node.
620 //!
621 //! <b>Complexity</b>: Average complexity is constant time.
622 //!
623 //! <b>Throws</b>: Nothing.
624 //!
625 //! <b>Notes</b>: This function breaks the tree and the tree can
626 //! only be used for more unlink_leftmost_without_rebalance calls.
627 //! This function is normally used to achieve a step by step
628 //! controlled destruction of the tree.
629 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header)
630 {
631 node_ptr leftmost = NodeTraits::get_left(header);
632 if (leftmost == header)
633 return node_ptr();
634 node_ptr leftmost_parent(NodeTraits::get_parent(leftmost));
635 node_ptr leftmost_right (NodeTraits::get_right(leftmost));
636 bool is_root = leftmost_parent == header;
637
638 if (leftmost_right){
639 NodeTraits::set_parent(leftmost_right, leftmost_parent);
640 NodeTraits::set_left(header, base_type::minimum(leftmost_right));
641
642 if (is_root)
643 NodeTraits::set_parent(header, leftmost_right);
644 else
645 NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right);
646 }
647 else if (is_root){
648 NodeTraits::set_parent(header, node_ptr());
649 NodeTraits::set_left(header, header);
650 NodeTraits::set_right(header, header);
651 }
652 else{
653 NodeTraits::set_left(leftmost_parent, node_ptr());
654 NodeTraits::set_left(header, leftmost_parent);
655 }
656 return leftmost;
657 }
658
659 //! <b>Requires</b>: node is a node of the tree but it's not the header.
660 //!
661 //! <b>Effects</b>: Returns the number of nodes of the subtree.
662 //!
663 //! <b>Complexity</b>: Linear time.
664 //!
665 //! <b>Throws</b>: Nothing.
666 static std::size_t size(const const_node_ptr & header)
667 {
668 node_ptr beg(begin_node(header));
669 node_ptr end(end_node(header));
670 std::size_t i = 0;
671 for(;beg != end; beg = base_type::next_node(beg)) ++i;
672 return i;
673 }
674
675 //! <b>Requires</b>: header1 and header2 must be the header nodes
676 //! of two trees.
677 //!
678 //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
679 //! links to the second tree and header2 will have links to the first tree.
680 //!
681 //! <b>Complexity</b>: Constant.
682 //!
683 //! <b>Throws</b>: Nothing.
684 static void swap_tree(const node_ptr & header1, const node_ptr & header2)
685 {
686 if(header1 == header2)
687 return;
688
689 node_ptr tmp;
690
691 //Parent swap
692 tmp = NodeTraits::get_parent(header1);
693 NodeTraits::set_parent(header1, NodeTraits::get_parent(header2));
694 NodeTraits::set_parent(header2, tmp);
695 //Left swap
696 tmp = NodeTraits::get_left(header1);
697 NodeTraits::set_left(header1, NodeTraits::get_left(header2));
698 NodeTraits::set_left(header2, tmp);
699 //Right swap
700 tmp = NodeTraits::get_right(header1);
701 NodeTraits::set_right(header1, NodeTraits::get_right(header2));
702 NodeTraits::set_right(header2, tmp);
703
704 //Now test parent
705 node_ptr h1_parent(NodeTraits::get_parent(header1));
706 if(h1_parent){
707 NodeTraits::set_parent(h1_parent, header1);
708 }
709 else{
710 NodeTraits::set_left(header1, header1);
711 NodeTraits::set_right(header1, header1);
712 }
713
714 node_ptr h2_parent(NodeTraits::get_parent(header2));
715 if(h2_parent){
716 NodeTraits::set_parent(h2_parent, header2);
717 }
718 else{
719 NodeTraits::set_left(header2, header2);
720 NodeTraits::set_right(header2, header2);
721 }
722 }
723
724 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
725 //! <b>Requires</b>: p is a node of a tree.
726 //!
727 //! <b>Effects</b>: Returns true if p is the header of the tree.
728 //!
729 //! <b>Complexity</b>: Constant.
730 //!
731 //! <b>Throws</b>: Nothing.
732 static bool is_header(const const_node_ptr & p);
733 #endif
734
735 //! <b>Requires</b>: "header" must be the header node of a tree.
736 //! KeyNodePtrCompare is a function object that induces a strict weak
737 //! ordering compatible with the strict weak ordering used to create the
738 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
739 //!
740 //! <b>Effects</b>: Returns a node_ptr to the first element that is equivalent to
741 //! "key" according to "comp" or "header" if that element does not exist.
742 //!
743 //! <b>Complexity</b>: Logarithmic.
744 //!
745 //! <b>Throws</b>: If "comp" throws.
746 template<class KeyType, class KeyNodePtrCompare>
747 static node_ptr find
748 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
749 {
750 node_ptr end = detail::uncast(header);
751 node_ptr y = lower_bound(header, key, comp);
752 return (y == end || comp(key, y)) ? end : y;
753 }
754
755 //! <b>Requires</b>: "header" must be the header node of a tree.
756 //! KeyNodePtrCompare is a function object that induces a strict weak
757 //! ordering compatible with the strict weak ordering used to create the
758 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
759 //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If
760 //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be true.
761 //!
762 //! <b>Effects</b>: Returns an a pair with the following criteria:
763 //!
764 //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise
765 //!
766 //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise
767 //!
768 //! <b>Complexity</b>: Logarithmic.
769 //!
770 //! <b>Throws</b>: If "comp" throws.
771 //!
772 //! <b>Note</b>: This function can be more efficient than calling upper_bound
773 //! and lower_bound for lower_key and upper_key.
774 //!
775 //! <b>Note</b>: Experimental function, the interface might change.
776 template< class KeyType, class KeyNodePtrCompare>
777 static std::pair<node_ptr, node_ptr> bounded_range
778 ( const const_node_ptr & header
779 , const KeyType &lower_key
780 , const KeyType &upper_key
781 , KeyNodePtrCompare comp
782 , bool left_closed
783 , bool right_closed)
784 {
785 node_ptr y = detail::uncast(header);
786 node_ptr x = NodeTraits::get_parent(header);
787
788 while(x){
789 //If x is less than lower_key the target
790 //range is on the right part
791 if(comp(x, lower_key)){
792 //Check for invalid input range
793 BOOST_INTRUSIVE_INVARIANT_ASSERT(comp(x, upper_key));
794 x = NodeTraits::get_right(x);
795 }
796 //If the upper_key is less than x, the target
797 //range is on the left part
798 else if(comp(upper_key, x)){
799 y = x;
800 x = NodeTraits::get_left(x);
801 }
802 else{
803 //x is inside the bounded range(lower_key <= x <= upper_key),
804 //so we must split lower and upper searches
805 //
806 //Sanity check: if lower_key and upper_key are equal, then both left_closed and right_closed can't be false
807 BOOST_INTRUSIVE_INVARIANT_ASSERT(left_closed || right_closed || comp(lower_key, x) || comp(x, upper_key));
808 return std::pair<node_ptr,node_ptr>(
809 left_closed
810 //If left_closed, then comp(x, lower_key) is already the lower_bound
811 //condition so we save one comparison and go to the next level
812 //following traditional lower_bound algo
813 ? lower_bound_loop(NodeTraits::get_left(x), x, lower_key, comp)
814 //If left-open, comp(x, lower_key) is not the upper_bound algo
815 //condition so we must recheck current 'x' node with upper_bound algo
816 : upper_bound_loop(x, y, lower_key, comp)
817 ,
818 right_closed
819 //If right_closed, then comp(upper_key, x) is already the upper_bound
820 //condition so we can save one comparison and go to the next level
821 //following lower_bound algo
822 ? upper_bound_loop(NodeTraits::get_right(x), y, upper_key, comp)
823 //If right-open, comp(upper_key, x) is not the lower_bound algo
824 //condition so we must recheck current 'x' node with lower_bound algo
825 : lower_bound_loop(x, y, upper_key, comp)
826 );
827 }
828 }
829 return std::pair<node_ptr,node_ptr> (y, y);
830 }
831
832 //! <b>Requires</b>: "header" must be the header node of a tree.
833 //! KeyNodePtrCompare is a function object that induces a strict weak
834 //! ordering compatible with the strict weak ordering used to create the
835 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
836 //!
837 //! <b>Effects</b>: Returns the number of elements with a key equivalent to "key"
838 //! according to "comp".
839 //!
840 //! <b>Complexity</b>: Logarithmic.
841 //!
842 //! <b>Throws</b>: If "comp" throws.
843 template<class KeyType, class KeyNodePtrCompare>
844 static std::size_t count
845 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
846 {
847 std::pair<node_ptr, node_ptr> ret = equal_range(header, key, comp);
848 std::size_t n = 0;
849 while(ret.first != ret.second){
850 ++n;
851 ret.first = base_type::next_node(ret.first);
852 }
853 return n;
854 }
855
856 //! <b>Requires</b>: "header" must be the header node of a tree.
857 //! KeyNodePtrCompare is a function object that induces a strict weak
858 //! ordering compatible with the strict weak ordering used to create the
859 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
860 //!
861 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
862 //! all elements that are equivalent to "key" according to "comp" or an
863 //! empty range that indicates the position where those elements would be
864 //! if there are no equivalent elements.
865 //!
866 //! <b>Complexity</b>: Logarithmic.
867 //!
868 //! <b>Throws</b>: If "comp" throws.
869 template<class KeyType, class KeyNodePtrCompare>
870 BOOST_INTRUSIVE_FORCEINLINE static std::pair<node_ptr, node_ptr> equal_range
871 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
872 {
873 return bounded_range(header, key, key, comp, true, true);
874 }
875
876 //! <b>Requires</b>: "header" must be the header node of a tree.
877 //! KeyNodePtrCompare is a function object that induces a strict weak
878 //! ordering compatible with the strict weak ordering used to create the
879 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
880 //!
881 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
882 //! the first element that is equivalent to "key" according to "comp" or an
883 //! empty range that indicates the position where that element would be
884 //! if there are no equivalent elements.
885 //!
886 //! <b>Complexity</b>: Logarithmic.
887 //!
888 //! <b>Throws</b>: If "comp" throws.
889 template<class KeyType, class KeyNodePtrCompare>
890 static std::pair<node_ptr, node_ptr> lower_bound_range
891 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
892 {
893 node_ptr const lb(lower_bound(header, key, comp));
894 std::pair<node_ptr, node_ptr> ret_ii(lb, lb);
895 if(lb != header && !comp(key, lb)){
896 ret_ii.second = base_type::next_node(ret_ii.second);
897 }
898 return ret_ii;
899 }
900
901 //! <b>Requires</b>: "header" must be the header node of a tree.
902 //! KeyNodePtrCompare is a function object that induces a strict weak
903 //! ordering compatible with the strict weak ordering used to create the
904 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
905 //!
906 //! <b>Effects</b>: Returns a node_ptr to the first element that is
907 //! not less than "key" according to "comp" or "header" if that element does
908 //! not exist.
909 //!
910 //! <b>Complexity</b>: Logarithmic.
911 //!
912 //! <b>Throws</b>: If "comp" throws.
913 template<class KeyType, class KeyNodePtrCompare>
914 BOOST_INTRUSIVE_FORCEINLINE static node_ptr lower_bound
915 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
916 {
917 return lower_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
918 }
919
920 //! <b>Requires</b>: "header" must be the header node of a tree.
921 //! KeyNodePtrCompare is a function object that induces a strict weak
922 //! ordering compatible with the strict weak ordering used to create the
923 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
924 //!
925 //! <b>Effects</b>: Returns a node_ptr to the first element that is greater
926 //! than "key" according to "comp" or "header" if that element does not exist.
927 //!
928 //! <b>Complexity</b>: Logarithmic.
929 //!
930 //! <b>Throws</b>: If "comp" throws.
931 template<class KeyType, class KeyNodePtrCompare>
932 BOOST_INTRUSIVE_FORCEINLINE static node_ptr upper_bound
933 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
934 {
935 return upper_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
936 }
937
938 //! <b>Requires</b>: "header" must be the header node of a tree.
939 //! "commit_data" must have been obtained from a previous call to
940 //! "insert_unique_check". No objects should have been inserted or erased
941 //! from the set between the "insert_unique_check" that filled "commit_data"
942 //! and the call to "insert_commit".
943 //!
944 //!
945 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
946 //! from the "commit_data" that a previous "insert_check" filled.
947 //!
948 //! <b>Complexity</b>: Constant time.
949 //!
950 //! <b>Throws</b>: Nothing.
951 //!
952 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
953 //! previously executed to fill "commit_data". No value should be inserted or
954 //! erased between the "insert_check" and "insert_commit" calls.
955 BOOST_INTRUSIVE_FORCEINLINE static void insert_unique_commit
956 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
957 { return insert_commit(header, new_value, commit_data); }
958
959 //! <b>Requires</b>: "header" must be the header node of a tree.
960 //! KeyNodePtrCompare is a function object that induces a strict weak
961 //! ordering compatible with the strict weak ordering used to create the
962 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
963 //!
964 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
965 //! tree according to "comp" and obtains the needed information to realize
966 //! a constant-time node insertion if there is no equivalent node.
967 //!
968 //! <b>Returns</b>: If there is an equivalent value
969 //! returns a pair containing a node_ptr to the already present node
970 //! and false. If there is not equivalent key can be inserted returns true
971 //! in the returned pair's boolean and fills "commit_data" that is meant to
972 //! be used with the "insert_commit" function to achieve a constant-time
973 //! insertion function.
974 //!
975 //! <b>Complexity</b>: Average complexity is at most logarithmic.
976 //!
977 //! <b>Throws</b>: If "comp" throws.
978 //!
979 //! <b>Notes</b>: This function is used to improve performance when constructing
980 //! a node is expensive and the user does not want to have two equivalent nodes
981 //! in the tree: if there is an equivalent value
982 //! the constructed object must be discarded. Many times, the part of the
983 //! node that is used to impose the order is much cheaper to construct
984 //! than the node and this function offers the possibility to use that part
985 //! to check if the insertion will be successful.
986 //!
987 //! If the check is successful, the user can construct the node and use
988 //! "insert_commit" to insert the node in constant-time. This gives a total
989 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
990 //!
991 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
992 //! if no more objects are inserted or erased from the set.
993 template<class KeyType, class KeyNodePtrCompare>
994 static std::pair<node_ptr, bool> insert_unique_check
995 (const const_node_ptr & header, const KeyType &key
996 ,KeyNodePtrCompare comp, insert_commit_data &commit_data
997 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
998 , std::size_t *pdepth = 0
999 #endif
1000 )
1001 {
1002 std::size_t depth = 0;
1003 node_ptr h(detail::uncast(header));
1004 node_ptr y(h);
1005 node_ptr x(NodeTraits::get_parent(y));
1006 node_ptr prev = node_ptr();
1007
1008 //Find the upper bound, cache the previous value and if we should
1009 //store it in the left or right node
1010 bool left_child = true;
1011 while(x){
1012 ++depth;
1013 y = x;
1014 x = (left_child = comp(key, x)) ?
1015 NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x));
1016 }
1017
1018 if(pdepth) *pdepth = depth;
1019
1020 //Since we've found the upper bound there is no other value with the same key if:
1021 // - There is no previous node
1022 // - The previous node is less than the key
1023 const bool not_present = !prev || comp(prev, key);
1024 if(not_present){
1025 commit_data.link_left = left_child;
1026 commit_data.node = y;
1027 }
1028 return std::pair<node_ptr, bool>(prev, not_present);
1029 }
1030
1031 //! <b>Requires</b>: "header" must be the header node of a tree.
1032 //! KeyNodePtrCompare is a function object that induces a strict weak
1033 //! ordering compatible with the strict weak ordering used to create the
1034 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
1035 //! "hint" is node from the "header"'s tree.
1036 //!
1037 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
1038 //! tree according to "comp" using "hint" as a hint to where it should be
1039 //! inserted and obtains the needed information to realize
1040 //! a constant-time node insertion if there is no equivalent node.
1041 //! If "hint" is the upper_bound the function has constant time
1042 //! complexity (two comparisons in the worst case).
1043 //!
1044 //! <b>Returns</b>: If there is an equivalent value
1045 //! returns a pair containing a node_ptr to the already present node
1046 //! and false. If there is not equivalent key can be inserted returns true
1047 //! in the returned pair's boolean and fills "commit_data" that is meant to
1048 //! be used with the "insert_commit" function to achieve a constant-time
1049 //! insertion function.
1050 //!
1051 //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
1052 //! amortized constant time if new_node should be inserted immediately before "hint".
1053 //!
1054 //! <b>Throws</b>: If "comp" throws.
1055 //!
1056 //! <b>Notes</b>: This function is used to improve performance when constructing
1057 //! a node is expensive and the user does not want to have two equivalent nodes
1058 //! in the tree: if there is an equivalent value
1059 //! the constructed object must be discarded. Many times, the part of the
1060 //! node that is used to impose the order is much cheaper to construct
1061 //! than the node and this function offers the possibility to use that part
1062 //! to check if the insertion will be successful.
1063 //!
1064 //! If the check is successful, the user can construct the node and use
1065 //! "insert_commit" to insert the node in constant-time. This gives a total
1066 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
1067 //!
1068 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
1069 //! if no more objects are inserted or erased from the set.
1070 template<class KeyType, class KeyNodePtrCompare>
1071 static std::pair<node_ptr, bool> insert_unique_check
1072 (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
1073 ,KeyNodePtrCompare comp, insert_commit_data &commit_data
1074 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1075 , std::size_t *pdepth = 0
1076 #endif
1077 )
1078 {
1079 //hint must be bigger than the key
1080 if(hint == header || comp(key, hint)){
1081 node_ptr prev(hint);
1082 //Previous value should be less than the key
1083 if(hint == begin_node(header) || comp((prev = base_type::prev_node(hint)), key)){
1084 commit_data.link_left = unique(header) || !NodeTraits::get_left(hint);
1085 commit_data.node = commit_data.link_left ? hint : prev;
1086 if(pdepth){
1087 *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
1088 }
1089 return std::pair<node_ptr, bool>(node_ptr(), true);
1090 }
1091 }
1092 //Hint was wrong, use hintless insertion
1093 return insert_unique_check(header, key, comp, commit_data, pdepth);
1094 }
1095
1096 //! <b>Requires</b>: "header" must be the header node of a tree.
1097 //! NodePtrCompare is a function object that induces a strict weak
1098 //! ordering compatible with the strict weak ordering used to create the
1099 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
1100 //! the "header"'s tree.
1101 //!
1102 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
1103 //! where it will be inserted. If "hint" is the upper_bound
1104 //! the insertion takes constant time (two comparisons in the worst case).
1105 //!
1106 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
1107 //! constant time if new_node is inserted immediately before "hint".
1108 //!
1109 //! <b>Throws</b>: If "comp" throws.
1110 template<class NodePtrCompare>
1111 static node_ptr insert_equal
1112 (const node_ptr & h, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
1113 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1114 , std::size_t *pdepth = 0
1115 #endif
1116 )
1117 {
1118 insert_commit_data commit_data;
1119 insert_equal_check(h, hint, new_node, comp, commit_data, pdepth);
1120 insert_commit(h, new_node, commit_data);
1121 return new_node;
1122 }
1123
1124 //! <b>Requires</b>: "h" must be the header node of a tree.
1125 //! NodePtrCompare is a function object that induces a strict weak
1126 //! ordering compatible with the strict weak ordering used to create the
1127 //! the tree. NodePtrCompare compares two node_ptrs.
1128 //!
1129 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
1130 //! according to "comp".
1131 //!
1132 //! <b>Complexity</b>: Average complexity for insert element is at
1133 //! most logarithmic.
1134 //!
1135 //! <b>Throws</b>: If "comp" throws.
1136 template<class NodePtrCompare>
1137 static node_ptr insert_equal_upper_bound
1138 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
1139 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1140 , std::size_t *pdepth = 0
1141 #endif
1142 )
1143 {
1144 insert_commit_data commit_data;
1145 insert_equal_upper_bound_check(h, new_node, comp, commit_data, pdepth);
1146 insert_commit(h, new_node, commit_data);
1147 return new_node;
1148 }
1149
1150 //! <b>Requires</b>: "h" must be the header node of a tree.
1151 //! NodePtrCompare is a function object that induces a strict weak
1152 //! ordering compatible with the strict weak ordering used to create the
1153 //! the tree. NodePtrCompare compares two node_ptrs.
1154 //!
1155 //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
1156 //! according to "comp".
1157 //!
1158 //! <b>Complexity</b>: Average complexity for insert element is at
1159 //! most logarithmic.
1160 //!
1161 //! <b>Throws</b>: If "comp" throws.
1162 template<class NodePtrCompare>
1163 static node_ptr insert_equal_lower_bound
1164 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
1165 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1166 , std::size_t *pdepth = 0
1167 #endif
1168 )
1169 {
1170 insert_commit_data commit_data;
1171 insert_equal_lower_bound_check(h, new_node, comp, commit_data, pdepth);
1172 insert_commit(h, new_node, commit_data);
1173 return new_node;
1174 }
1175
1176 //! <b>Requires</b>: "header" must be the header node of a tree.
1177 //! "pos" must be a valid iterator or header (end) node.
1178 //! "pos" must be an iterator pointing to the successor to "new_node"
1179 //! once inserted according to the order of already inserted nodes. This function does not
1180 //! check "pos" and this precondition must be guaranteed by the caller.
1181 //!
1182 //! <b>Effects</b>: Inserts new_node into the tree before "pos".
1183 //!
1184 //! <b>Complexity</b>: Constant-time.
1185 //!
1186 //! <b>Throws</b>: Nothing.
1187 //!
1188 //! <b>Note</b>: If "pos" is not the successor of the newly inserted "new_node"
1189 //! tree invariants might be broken.
1190 static node_ptr insert_before
1191 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node
1192 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1193 , std::size_t *pdepth = 0
1194 #endif
1195 )
1196 {
1197 insert_commit_data commit_data;
1198 insert_before_check(header, pos, commit_data, pdepth);
1199 insert_commit(header, new_node, commit_data);
1200 return new_node;
1201 }
1202
1203 //! <b>Requires</b>: "header" must be the header node of a tree.
1204 //! "new_node" must be, according to the used ordering no less than the
1205 //! greatest inserted key.
1206 //!
1207 //! <b>Effects</b>: Inserts new_node into the tree before "pos".
1208 //!
1209 //! <b>Complexity</b>: Constant-time.
1210 //!
1211 //! <b>Throws</b>: Nothing.
1212 //!
1213 //! <b>Note</b>: If "new_node" is less than the greatest inserted key
1214 //! tree invariants are broken. This function is slightly faster than
1215 //! using "insert_before".
1216 static void push_back
1217 (const node_ptr & header, const node_ptr & new_node
1218 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1219 , std::size_t *pdepth = 0
1220 #endif
1221 )
1222 {
1223 insert_commit_data commit_data;
1224 push_back_check(header, commit_data, pdepth);
1225 insert_commit(header, new_node, commit_data);
1226 }
1227
1228 //! <b>Requires</b>: "header" must be the header node of a tree.
1229 //! "new_node" must be, according to the used ordering, no greater than the
1230 //! lowest inserted key.
1231 //!
1232 //! <b>Effects</b>: Inserts new_node into the tree before "pos".
1233 //!
1234 //! <b>Complexity</b>: Constant-time.
1235 //!
1236 //! <b>Throws</b>: Nothing.
1237 //!
1238 //! <b>Note</b>: If "new_node" is greater than the lowest inserted key
1239 //! tree invariants are broken. This function is slightly faster than
1240 //! using "insert_before".
1241 static void push_front
1242 (const node_ptr & header, const node_ptr & new_node
1243 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1244 , std::size_t *pdepth = 0
1245 #endif
1246 )
1247 {
1248 insert_commit_data commit_data;
1249 push_front_check(header, commit_data, pdepth);
1250 insert_commit(header, new_node, commit_data);
1251 }
1252
1253 //! <b>Requires</b>: 'node' can't be a header node.
1254 //!
1255 //! <b>Effects</b>: Calculates the depth of a node: the depth of a
1256 //! node is the length (number of edges) of the path from the root
1257 //! to that node. (The root node is at depth 0.)
1258 //!
1259 //! <b>Complexity</b>: Logarithmic to the number of nodes in the tree.
1260 //!
1261 //! <b>Throws</b>: Nothing.
1262 static std::size_t depth(const_node_ptr node)
1263 {
1264 std::size_t depth = 0;
1265 node_ptr p_parent;
1266 while(node != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(node))){
1267 ++depth;
1268 node = p_parent;
1269 }
1270 return depth;
1271 }
1272
1273 //! <b>Requires</b>: "cloner" must be a function
1274 //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
1275 //! take a node_ptr and shouldn't throw.
1276 //!
1277 //! <b>Effects</b>: First empties target tree calling
1278 //! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
1279 //! except the header.
1280 //!
1281 //! Then, duplicates the entire tree pointed by "source_header" cloning each
1282 //! source node with <tt>node_ptr Cloner::operator()(const node_ptr &)</tt> to obtain
1283 //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
1284 //! are disposed using <tt>void disposer(const node_ptr &)</tt>.
1285 //!
1286 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the
1287 //! number of elements of tree target tree when calling this function.
1288 //!
1289 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
1290 template <class Cloner, class Disposer>
1291 static void clone
1292 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
1293 {
1294 if(!unique(target_header)){
1295 clear_and_dispose(target_header, disposer);
1296 }
1297
1298 node_ptr leftmost, rightmost;
1299 node_ptr new_root = clone_subtree
1300 (source_header, target_header, cloner, disposer, leftmost, rightmost);
1301
1302 //Now update header node
1303 NodeTraits::set_parent(target_header, new_root);
1304 NodeTraits::set_left (target_header, leftmost);
1305 NodeTraits::set_right (target_header, rightmost);
1306 }
1307
1308 //! <b>Requires</b>: header must be the header of a tree, z a node
1309 //! of that tree and z != header.
1310 //!
1311 //! <b>Effects</b>: Erases node "z" from the tree with header "header".
1312 //!
1313 //! <b>Complexity</b>: Amortized constant time.
1314 //!
1315 //! <b>Throws</b>: Nothing.
1316 BOOST_INTRUSIVE_FORCEINLINE static void erase(const node_ptr & header, const node_ptr & z)
1317 {
1318 data_for_rebalance ignored;
1319 erase(header, z, ignored);
1320 }
1321
1322 //! <b>Requires</b>: header1 and header2 must be the headers of trees tree1 and tree2
1323 //! respectively, z a non-header node of tree1. NodePtrCompare is the comparison
1324 //! function of tree1..
1325 //!
1326 //! <b>Effects</b>: Transfers node "z" from tree1 to tree2 if tree1 does not contain
1327 //! a node that is equivalent to z.
1328 //!
1329 //! <b>Returns</b>: True if the node was trasferred, false otherwise.
1330 //!
1331 //! <b>Complexity</b>: Logarithmic.
1332 //!
1333 //! <b>Throws</b>: If the comparison throws.
1334 template<class NodePtrCompare>
1335 BOOST_INTRUSIVE_FORCEINLINE static bool transfer_unique
1336 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z)
1337 {
1338 data_for_rebalance ignored;
1339 return transfer_unique(header1, comp, header2, z, ignored);
1340 }
1341
1342 //! <b>Requires</b>: header1 and header2 must be the headers of trees tree1 and tree2
1343 //! respectively, z a non-header node of tree1. NodePtrCompare is the comparison
1344 //! function of tree1..
1345 //!
1346 //! <b>Effects</b>: Transfers node "z" from tree1 to tree2.
1347 //!
1348 //! <b>Complexity</b>: Logarithmic.
1349 //!
1350 //! <b>Throws</b>: If the comparison throws.
1351 template<class NodePtrCompare>
1352 BOOST_INTRUSIVE_FORCEINLINE static void transfer_equal
1353 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z)
1354 {
1355 data_for_rebalance ignored;
1356 transfer_equal(header1, comp, header2, z, ignored);
1357 }
1358
1359 //! <b>Requires</b>: node is a tree node but not the header.
1360 //!
1361 //! <b>Effects</b>: Unlinks the node and rebalances the tree.
1362 //!
1363 //! <b>Complexity</b>: Average complexity is constant time.
1364 //!
1365 //! <b>Throws</b>: Nothing.
1366 static void unlink(const node_ptr & node)
1367 {
1368 node_ptr x = NodeTraits::get_parent(node);
1369 if(x){
1370 while(!base_type::is_header(x))
1371 x = NodeTraits::get_parent(x);
1372 erase(x, node);
1373 }
1374 }
1375
1376 //! <b>Requires</b>: header must be the header of a tree.
1377 //!
1378 //! <b>Effects</b>: Rebalances the tree.
1379 //!
1380 //! <b>Throws</b>: Nothing.
1381 //!
1382 //! <b>Complexity</b>: Linear.
1383 static void rebalance(const node_ptr & header)
1384 {
1385 node_ptr root = NodeTraits::get_parent(header);
1386 if(root){
1387 rebalance_subtree(root);
1388 }
1389 }
1390
1391 //! <b>Requires</b>: old_root is a node of a tree. It shall not be null.
1392 //!
1393 //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
1394 //!
1395 //! <b>Returns</b>: The new root of the subtree.
1396 //!
1397 //! <b>Throws</b>: Nothing.
1398 //!
1399 //! <b>Complexity</b>: Linear.
1400 static node_ptr rebalance_subtree(const node_ptr & old_root)
1401 {
1402 //Taken from:
1403 //"Tree rebalancing in optimal time and space"
1404 //Quentin F. Stout and Bette L. Warren
1405
1406 //To avoid irregularities in the algorithm (old_root can be a
1407 //left or right child or even the root of the tree) just put the
1408 //root as the right child of its parent. Before doing this backup
1409 //information to restore the original relationship after
1410 //the algorithm is applied.
1411 node_ptr super_root = NodeTraits::get_parent(old_root);
1412 BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root);
1413
1414 //Get root info
1415 node_ptr super_root_right_backup = NodeTraits::get_right(super_root);
1416 bool super_root_is_header = NodeTraits::get_parent(super_root) == old_root;
1417 bool old_root_is_right = is_right_child(old_root);
1418 NodeTraits::set_right(super_root, old_root);
1419
1420 std::size_t size;
1421 subtree_to_vine(super_root, size);
1422 vine_to_subtree(super_root, size);
1423 node_ptr new_root = NodeTraits::get_right(super_root);
1424
1425 //Recover root
1426 if(super_root_is_header){
1427 NodeTraits::set_right(super_root, super_root_right_backup);
1428 NodeTraits::set_parent(super_root, new_root);
1429 }
1430 else if(old_root_is_right){
1431 NodeTraits::set_right(super_root, new_root);
1432 }
1433 else{
1434 NodeTraits::set_right(super_root, super_root_right_backup);
1435 NodeTraits::set_left(super_root, new_root);
1436 }
1437 return new_root;
1438 }
1439
1440 //! <b>Effects</b>: Asserts the integrity of the container with additional checks provided by the user.
1441 //!
1442 //! <b>Requires</b>: header must be the header of a tree.
1443 //!
1444 //! <b>Complexity</b>: Linear time.
1445 //!
1446 //! <b>Note</b>: The method might not have effect when asserts are turned off (e.g., with NDEBUG).
1447 //! Experimental function, interface might change in future versions.
1448 template<class Checker>
1449 static void check(const const_node_ptr& header, Checker checker, typename Checker::return_type& checker_return)
1450 {
1451 const_node_ptr root_node_ptr = NodeTraits::get_parent(header);
1452 if (!root_node_ptr){
1453 // check left&right header pointers
1454 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == header);
1455 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == header);
1456 }
1457 else{
1458 // check parent pointer of root node
1459 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(root_node_ptr) == header);
1460 // check subtree from root
1461 check_subtree(root_node_ptr, checker, checker_return);
1462 // check left&right header pointers
1463 const_node_ptr p = root_node_ptr;
1464 while (NodeTraits::get_left(p)) { p = NodeTraits::get_left(p); }
1465 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == p);
1466 p = root_node_ptr;
1467 while (NodeTraits::get_right(p)) { p = NodeTraits::get_right(p); }
1468 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == p);
1469 }
1470 }
1471
1472 protected:
1473
1474 template<class NodePtrCompare>
1475 static bool transfer_unique
1476 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z, data_for_rebalance &info)
1477 {
1478 insert_commit_data commit_data;
1479 bool const transferable = insert_unique_check(header1, z, comp, commit_data).second;
1480 if(transferable){
1481 erase(header2, z, info);
1482 insert_commit(header1, z, commit_data);
1483 }
1484 return transferable;
1485 }
1486
1487 template<class NodePtrCompare>
1488 static void transfer_equal
1489 (const node_ptr & header1, NodePtrCompare comp, const node_ptr &header2, const node_ptr & z, data_for_rebalance &info)
1490 {
1491 insert_commit_data commit_data;
1492 insert_equal_upper_bound_check(header1, z, comp, commit_data);
1493 erase(header2, z, info);
1494 insert_commit(header1, z, commit_data);
1495 }
1496
1497 static void erase(const node_ptr & header, const node_ptr & z, data_for_rebalance &info)
1498 {
1499 node_ptr y(z);
1500 node_ptr x;
1501 const node_ptr z_left(NodeTraits::get_left(z));
1502 const node_ptr z_right(NodeTraits::get_right(z));
1503
1504 if(!z_left){
1505 x = z_right; // x might be null.
1506 }
1507 else if(!z_right){ // z has exactly one non-null child. y == z.
1508 x = z_left; // x is not null.
1509 BOOST_ASSERT(x);
1510 }
1511 else{ //make y != z
1512 // y = find z's successor
1513 y = base_type::minimum(z_right);
1514 x = NodeTraits::get_right(y); // x might be null.
1515 }
1516
1517 node_ptr x_parent;
1518 const node_ptr z_parent(NodeTraits::get_parent(z));
1519 const bool z_is_leftchild(NodeTraits::get_left(z_parent) == z);
1520
1521 if(y != z){ //has two children and y is the minimum of z
1522 //y is z's successor and it has a null left child.
1523 //x is the right child of y (it can be null)
1524 //Relink y in place of z and link x with y's old parent
1525 NodeTraits::set_parent(z_left, y);
1526 NodeTraits::set_left(y, z_left);
1527 if(y != z_right){
1528 //Link y with the right tree of z
1529 NodeTraits::set_right(y, z_right);
1530 NodeTraits::set_parent(z_right, y);
1531 //Link x with y's old parent (y must be a left child)
1532 x_parent = NodeTraits::get_parent(y);
1533 BOOST_ASSERT(NodeTraits::get_left(x_parent) == y);
1534 if(x)
1535 NodeTraits::set_parent(x, x_parent);
1536 //Since y was the successor and not the right child of z, it must be a left child
1537 NodeTraits::set_left(x_parent, x);
1538 }
1539 else{ //y was the right child of y so no need to fix x's position
1540 x_parent = y;
1541 }
1542 NodeTraits::set_parent(y, z_parent);
1543 this_type::set_child(header, y, z_parent, z_is_leftchild);
1544 }
1545 else { // z has zero or one child, x is one child (it can be null)
1546 //Just link x to z's parent
1547 x_parent = z_parent;
1548 if(x)
1549 NodeTraits::set_parent(x, z_parent);
1550 this_type::set_child(header, x, z_parent, z_is_leftchild);
1551
1552 //Now update leftmost/rightmost in case z was one of them
1553 if(NodeTraits::get_left(header) == z){
1554 //z_left must be null because z is the leftmost
1555 BOOST_ASSERT(!z_left);
1556 NodeTraits::set_left(header, !z_right ?
1557 z_parent : // makes leftmost == header if z == root
1558 base_type::minimum(z_right));
1559 }
1560 if(NodeTraits::get_right(header) == z){
1561 //z_right must be null because z is the rightmost
1562 BOOST_ASSERT(!z_right);
1563 NodeTraits::set_right(header, !z_left ?
1564 z_parent : // makes rightmost == header if z == root
1565 base_type::maximum(z_left));
1566 }
1567 }
1568
1569 //If z had 0/1 child, y == z and one of its children (and maybe null)
1570 //If z had 2 children, y is the successor of z and x is the right child of y
1571 info.x = x;
1572 info.y = y;
1573 //If z had 0/1 child, x_parent is the new parent of the old right child of y (z's successor)
1574 //If z had 2 children, x_parent is the new parent of y (z_parent)
1575 BOOST_ASSERT(!x || NodeTraits::get_parent(x) == x_parent);
1576 info.x_parent = x_parent;
1577 }
1578
1579 //! <b>Requires</b>: node is a node of the tree but it's not the header.
1580 //!
1581 //! <b>Effects</b>: Returns the number of nodes of the subtree.
1582 //!
1583 //! <b>Complexity</b>: Linear time.
1584 //!
1585 //! <b>Throws</b>: Nothing.
1586 static std::size_t subtree_size(const const_node_ptr & subtree)
1587 {
1588 std::size_t count = 0;
1589 if (subtree){
1590 node_ptr n = detail::uncast(subtree);
1591 node_ptr m = NodeTraits::get_left(n);
1592 while(m){
1593 n = m;
1594 m = NodeTraits::get_left(n);
1595 }
1596
1597 while(1){
1598 ++count;
1599 node_ptr n_right(NodeTraits::get_right(n));
1600 if(n_right){
1601 n = n_right;
1602 m = NodeTraits::get_left(n);
1603 while(m){
1604 n = m;
1605 m = NodeTraits::get_left(n);
1606 }
1607 }
1608 else {
1609 do{
1610 if (n == subtree){
1611 return count;
1612 }
1613 m = n;
1614 n = NodeTraits::get_parent(n);
1615 }while(NodeTraits::get_left(n) != m);
1616 }
1617 }
1618 }
1619 return count;
1620 }
1621
1622 //! <b>Requires</b>: p is a node of a tree.
1623 //!
1624 //! <b>Effects</b>: Returns true if p is a left child.
1625 //!
1626 //! <b>Complexity</b>: Constant.
1627 //!
1628 //! <b>Throws</b>: Nothing.
1629 BOOST_INTRUSIVE_FORCEINLINE static bool is_left_child(const node_ptr & p)
1630 { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; }
1631
1632 //! <b>Requires</b>: p is a node of a tree.
1633 //!
1634 //! <b>Effects</b>: Returns true if p is a right child.
1635 //!
1636 //! <b>Complexity</b>: Constant.
1637 //!
1638 //! <b>Throws</b>: Nothing.
1639 BOOST_INTRUSIVE_FORCEINLINE static bool is_right_child(const node_ptr & p)
1640 { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; }
1641
1642 static void insert_before_check
1643 (const node_ptr &header, const node_ptr & pos
1644 , insert_commit_data &commit_data
1645 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1646 , std::size_t *pdepth = 0
1647 #endif
1648 )
1649 {
1650 node_ptr prev(pos);
1651 if(pos != NodeTraits::get_left(header))
1652 prev = base_type::prev_node(pos);
1653 bool link_left = unique(header) || !NodeTraits::get_left(pos);
1654 commit_data.link_left = link_left;
1655 commit_data.node = link_left ? pos : prev;
1656 if(pdepth){
1657 *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
1658 }
1659 }
1660
1661 static void push_back_check
1662 (const node_ptr & header, insert_commit_data &commit_data
1663 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1664 , std::size_t *pdepth = 0
1665 #endif
1666 )
1667 {
1668 node_ptr prev(NodeTraits::get_right(header));
1669 if(pdepth){
1670 *pdepth = prev == header ? 0 : depth(prev) + 1;
1671 }
1672 commit_data.link_left = false;
1673 commit_data.node = prev;
1674 }
1675
1676 static void push_front_check
1677 (const node_ptr & header, insert_commit_data &commit_data
1678 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1679 , std::size_t *pdepth = 0
1680 #endif
1681 )
1682 {
1683 node_ptr pos(NodeTraits::get_left(header));
1684 if(pdepth){
1685 *pdepth = pos == header ? 0 : depth(pos) + 1;
1686 }
1687 commit_data.link_left = true;
1688 commit_data.node = pos;
1689 }
1690
1691 template<class NodePtrCompare>
1692 static void insert_equal_check
1693 (const node_ptr &header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
1694 , insert_commit_data &commit_data
1695 /// @cond
1696 , std::size_t *pdepth = 0
1697 /// @endcond
1698 )
1699 {
1700 if(hint == header || !comp(hint, new_node)){
1701 node_ptr prev(hint);
1702 if(hint == NodeTraits::get_left(header) ||
1703 !comp(new_node, (prev = base_type::prev_node(hint)))){
1704 bool link_left = unique(header) || !NodeTraits::get_left(hint);
1705 commit_data.link_left = link_left;
1706 commit_data.node = link_left ? hint : prev;
1707 if(pdepth){
1708 *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
1709 }
1710 }
1711 else{
1712 insert_equal_upper_bound_check(header, new_node, comp, commit_data, pdepth);
1713 }
1714 }
1715 else{
1716 insert_equal_lower_bound_check(header, new_node, comp, commit_data, pdepth);
1717 }
1718 }
1719
1720 template<class NodePtrCompare>
1721 static void insert_equal_upper_bound_check
1722 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
1723 {
1724 std::size_t depth = 0;
1725 node_ptr y(h);
1726 node_ptr x(NodeTraits::get_parent(y));
1727
1728 while(x){
1729 ++depth;
1730 y = x;
1731 x = comp(new_node, x) ?
1732 NodeTraits::get_left(x) : NodeTraits::get_right(x);
1733 }
1734 if(pdepth) *pdepth = depth;
1735 commit_data.link_left = (y == h) || comp(new_node, y);
1736 commit_data.node = y;
1737 }
1738
1739 template<class NodePtrCompare>
1740 static void insert_equal_lower_bound_check
1741 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
1742 {
1743 std::size_t depth = 0;
1744 node_ptr y(h);
1745 node_ptr x(NodeTraits::get_parent(y));
1746
1747 while(x){
1748 ++depth;
1749 y = x;
1750 x = !comp(x, new_node) ?
1751 NodeTraits::get_left(x) : NodeTraits::get_right(x);
1752 }
1753 if(pdepth) *pdepth = depth;
1754 commit_data.link_left = (y == h) || !comp(y, new_node);
1755 commit_data.node = y;
1756 }
1757
1758 static void insert_commit
1759 (const node_ptr & header, const node_ptr & new_node, const insert_commit_data &commit_data)
1760 {
1761 //Check if commit_data has not been initialized by a insert_unique_check call.
1762 BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != node_ptr());
1763 node_ptr parent_node(commit_data.node);
1764 if(parent_node == header){
1765 NodeTraits::set_parent(header, new_node);
1766 NodeTraits::set_right(header, new_node);
1767 NodeTraits::set_left(header, new_node);
1768 }
1769 else if(commit_data.link_left){
1770 NodeTraits::set_left(parent_node, new_node);
1771 if(parent_node == NodeTraits::get_left(header))
1772 NodeTraits::set_left(header, new_node);
1773 }
1774 else{
1775 NodeTraits::set_right(parent_node, new_node);
1776 if(parent_node == NodeTraits::get_right(header))
1777 NodeTraits::set_right(header, new_node);
1778 }
1779 NodeTraits::set_parent(new_node, parent_node);
1780 NodeTraits::set_right(new_node, node_ptr());
1781 NodeTraits::set_left(new_node, node_ptr());
1782 }
1783
1784 //Fix header and own's parent data when replacing x with own, providing own's old data with parent
1785 static void set_child(const node_ptr & header, const node_ptr & new_child, const node_ptr & new_parent, const bool link_left)
1786 {
1787 if(new_parent == header)
1788 NodeTraits::set_parent(header, new_child);
1789 else if(link_left)
1790 NodeTraits::set_left(new_parent, new_child);
1791 else
1792 NodeTraits::set_right(new_parent, new_child);
1793 }
1794
1795 // rotate p to left (no header and p's parent fixup)
1796 static void rotate_left_no_parent_fix(const node_ptr & p, const node_ptr &p_right)
1797 {
1798 node_ptr p_right_left(NodeTraits::get_left(p_right));
1799 NodeTraits::set_right(p, p_right_left);
1800 if(p_right_left){
1801 NodeTraits::set_parent(p_right_left, p);
1802 }
1803 NodeTraits::set_left(p_right, p);
1804 NodeTraits::set_parent(p, p_right);
1805 }
1806
1807 // rotate p to left (with header and p's parent fixup)
1808 static void rotate_left(const node_ptr & p, const node_ptr & p_right, const node_ptr & p_parent, const node_ptr & header)
1809 {
1810 const bool p_was_left(NodeTraits::get_left(p_parent) == p);
1811 rotate_left_no_parent_fix(p, p_right);
1812 NodeTraits::set_parent(p_right, p_parent);
1813 set_child(header, p_right, p_parent, p_was_left);
1814 }
1815
1816 // rotate p to right (no header and p's parent fixup)
1817 static void rotate_right_no_parent_fix(const node_ptr & p, const node_ptr &p_left)
1818 {
1819 node_ptr p_left_right(NodeTraits::get_right(p_left));
1820 NodeTraits::set_left(p, p_left_right);
1821 if(p_left_right){
1822 NodeTraits::set_parent(p_left_right, p);
1823 }
1824 NodeTraits::set_right(p_left, p);
1825 NodeTraits::set_parent(p, p_left);
1826 }
1827
1828 // rotate p to right (with header and p's parent fixup)
1829 static void rotate_right(const node_ptr & p, const node_ptr & p_left, const node_ptr & p_parent, const node_ptr & header)
1830 {
1831 const bool p_was_left(NodeTraits::get_left(p_parent) == p);
1832 rotate_right_no_parent_fix(p, p_left);
1833 NodeTraits::set_parent(p_left, p_parent);
1834 set_child(header, p_left, p_parent, p_was_left);
1835 }
1836
1837 private:
1838
1839 static void subtree_to_vine(node_ptr vine_tail, std::size_t &size)
1840 {
1841 //Inspired by LibAVL:
1842 //It uses a clever optimization for trees with parent pointers.
1843 //No parent pointer is updated when transforming a tree to a vine as
1844 //most of them will be overriten during compression rotations.
1845 //A final pass must be made after the rebalancing to updated those
1846 //pointers not updated by tree_to_vine + compression calls
1847 std::size_t len = 0;
1848 node_ptr remainder = NodeTraits::get_right(vine_tail);
1849 while(remainder){
1850 node_ptr tempptr = NodeTraits::get_left(remainder);
1851 if(!tempptr){ //move vine-tail down one
1852 vine_tail = remainder;
1853 remainder = NodeTraits::get_right(remainder);
1854 ++len;
1855 }
1856 else{ //rotate
1857 NodeTraits::set_left(remainder, NodeTraits::get_right(tempptr));
1858 NodeTraits::set_right(tempptr, remainder);
1859 remainder = tempptr;
1860 NodeTraits::set_right(vine_tail, tempptr);
1861 }
1862 }
1863 size = len;
1864 }
1865
1866 static void compress_subtree(node_ptr scanner, std::size_t count)
1867 {
1868 while(count--){ //compress "count" spine nodes in the tree with pseudo-root scanner
1869 node_ptr child = NodeTraits::get_right(scanner);
1870 node_ptr child_right = NodeTraits::get_right(child);
1871 NodeTraits::set_right(scanner, child_right);
1872 //Avoid setting the parent of child_right
1873 scanner = child_right;
1874 node_ptr scanner_left = NodeTraits::get_left(scanner);
1875 NodeTraits::set_right(child, scanner_left);
1876 if(scanner_left)
1877 NodeTraits::set_parent(scanner_left, child);
1878 NodeTraits::set_left(scanner, child);
1879 NodeTraits::set_parent(child, scanner);
1880 }
1881 }
1882
1883 static void vine_to_subtree(const node_ptr & super_root, std::size_t count)
1884 {
1885 const std::size_t one_szt = 1u;
1886 std::size_t leaf_nodes = count + one_szt - std::size_t(one_szt << detail::floor_log2(count + one_szt));
1887 compress_subtree(super_root, leaf_nodes); //create deepest leaves
1888 std::size_t vine_nodes = count - leaf_nodes;
1889 while(vine_nodes > 1){
1890 vine_nodes /= 2;
1891 compress_subtree(super_root, vine_nodes);
1892 }
1893
1894 //Update parents of nodes still in the in the original vine line
1895 //as those have not been updated by subtree_to_vine or compress_subtree
1896 for ( node_ptr q = super_root, p = NodeTraits::get_right(super_root)
1897 ; p
1898 ; q = p, p = NodeTraits::get_right(p)){
1899 NodeTraits::set_parent(p, q);
1900 }
1901 }
1902
1903 //! <b>Requires</b>: "n" must be a node inserted in a tree.
1904 //!
1905 //! <b>Effects</b>: Returns a pointer to the header node of the tree.
1906 //!
1907 //! <b>Complexity</b>: Logarithmic.
1908 //!
1909 //! <b>Throws</b>: Nothing.
1910 static node_ptr get_root(const node_ptr & node)
1911 {
1912 BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(node)));
1913 node_ptr x = NodeTraits::get_parent(node);
1914 if(x){
1915 while(!base_type::is_header(x)){
1916 x = NodeTraits::get_parent(x);
1917 }
1918 return x;
1919 }
1920 else{
1921 return node;
1922 }
1923 }
1924
1925 template <class Cloner, class Disposer>
1926 static node_ptr clone_subtree
1927 (const const_node_ptr &source_parent, const node_ptr &target_parent
1928 , Cloner cloner, Disposer disposer
1929 , node_ptr &leftmost_out, node_ptr &rightmost_out
1930 )
1931 {
1932 node_ptr target_sub_root = target_parent;
1933 node_ptr source_root = NodeTraits::get_parent(source_parent);
1934 if(!source_root){
1935 leftmost_out = rightmost_out = source_root;
1936 }
1937 else{
1938 //We'll calculate leftmost and rightmost nodes while iterating
1939 node_ptr current = source_root;
1940 node_ptr insertion_point = target_sub_root = cloner(current);
1941
1942 //We'll calculate leftmost and rightmost nodes while iterating
1943 node_ptr leftmost = target_sub_root;
1944 node_ptr rightmost = target_sub_root;
1945
1946 //First set the subroot
1947 NodeTraits::set_left(target_sub_root, node_ptr());
1948 NodeTraits::set_right(target_sub_root, node_ptr());
1949 NodeTraits::set_parent(target_sub_root, target_parent);
1950
1951 dispose_subtree_disposer<Disposer> rollback(disposer, target_sub_root);
1952 while(true) {
1953 //First clone left nodes
1954 if( NodeTraits::get_left(current) &&
1955 !NodeTraits::get_left(insertion_point)) {
1956 current = NodeTraits::get_left(current);
1957 node_ptr temp = insertion_point;
1958 //Clone and mark as leaf
1959 insertion_point = cloner(current);
1960 NodeTraits::set_left (insertion_point, node_ptr());
1961 NodeTraits::set_right (insertion_point, node_ptr());
1962 //Insert left
1963 NodeTraits::set_parent(insertion_point, temp);
1964 NodeTraits::set_left (temp, insertion_point);
1965 //Update leftmost
1966 if(rightmost == target_sub_root)
1967 leftmost = insertion_point;
1968 }
1969 //Then clone right nodes
1970 else if( NodeTraits::get_right(current) &&
1971 !NodeTraits::get_right(insertion_point)){
1972 current = NodeTraits::get_right(current);
1973 node_ptr temp = insertion_point;
1974 //Clone and mark as leaf
1975 insertion_point = cloner(current);
1976 NodeTraits::set_left (insertion_point, node_ptr());
1977 NodeTraits::set_right (insertion_point, node_ptr());
1978 //Insert right
1979 NodeTraits::set_parent(insertion_point, temp);
1980 NodeTraits::set_right (temp, insertion_point);
1981 //Update rightmost
1982 rightmost = insertion_point;
1983 }
1984 //If not, go up
1985 else if(current == source_root){
1986 break;
1987 }
1988 else{
1989 //Branch completed, go up searching more nodes to clone
1990 current = NodeTraits::get_parent(current);
1991 insertion_point = NodeTraits::get_parent(insertion_point);
1992 }
1993 }
1994 rollback.release();
1995 leftmost_out = leftmost;
1996 rightmost_out = rightmost;
1997 }
1998 return target_sub_root;
1999 }
2000
2001 template<class Disposer>
2002 static void dispose_subtree(node_ptr x, Disposer disposer)
2003 {
2004 while (x){
2005 node_ptr save(NodeTraits::get_left(x));
2006 if (save) {
2007 // Right rotation
2008 NodeTraits::set_left(x, NodeTraits::get_right(save));
2009 NodeTraits::set_right(save, x);
2010 }
2011 else {
2012 save = NodeTraits::get_right(x);
2013 init(x);
2014 disposer(x);
2015 }
2016 x = save;
2017 }
2018 }
2019
2020 template<class KeyType, class KeyNodePtrCompare>
2021 static node_ptr lower_bound_loop
2022 (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
2023 {
2024 while(x){
2025 if(comp(x, key)){
2026 x = NodeTraits::get_right(x);
2027 }
2028 else{
2029 y = x;
2030 x = NodeTraits::get_left(x);
2031 }
2032 }
2033 return y;
2034 }
2035
2036 template<class KeyType, class KeyNodePtrCompare>
2037 static node_ptr upper_bound_loop
2038 (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
2039 {
2040 while(x){
2041 if(comp(key, x)){
2042 y = x;
2043 x = NodeTraits::get_left(x);
2044 }
2045 else{
2046 x = NodeTraits::get_right(x);
2047 }
2048 }
2049 return y;
2050 }
2051
2052 template<class Checker>
2053 static void check_subtree(const const_node_ptr& node, Checker checker, typename Checker::return_type& check_return)
2054 {
2055 const_node_ptr left = NodeTraits::get_left(node);
2056 const_node_ptr right = NodeTraits::get_right(node);
2057 typename Checker::return_type check_return_left;
2058 typename Checker::return_type check_return_right;
2059 if (left)
2060 {
2061 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(left) == node);
2062 check_subtree(left, checker, check_return_left);
2063 }
2064 if (right)
2065 {
2066 BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(right) == node);
2067 check_subtree(right, checker, check_return_right);
2068 }
2069 checker(node, check_return_left, check_return_right, check_return);
2070 }
2071 };
2072
2073 /// @cond
2074
2075 template<class NodeTraits>
2076 struct get_algo<BsTreeAlgorithms, NodeTraits>
2077 {
2078 typedef bstree_algorithms<NodeTraits> type;
2079 };
2080
2081 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
2082 struct get_node_checker<BsTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
2083 {
2084 typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
2085 };
2086
2087 /// @endcond
2088
2089 } //namespace intrusive
2090 } //namespace boost
2091
2092 #include <boost/intrusive/detail/config_end.hpp>
2093
2094 #endif //BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP