1
/////////////////////////////////////////////////////////////////////////////
3
// (C) Copyright Daniel K. O. 2005.
4
// (C) Copyright Ion Gaztanaga 2007.
6
// Distributed under the Boost Software License, Version 1.0.
7
// (See accompanying file LICENSE_1_0.txt or copy at
8
// http://www.boost.org/LICENSE_1_0.txt)
10
// See http://www.boost.org/libs/intrusive for documentation.
12
/////////////////////////////////////////////////////////////////////////////
14
#ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
15
#define BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
17
#include <boost/intrusive/detail/config_begin.hpp>
20
#include <boost/intrusive/intrusive_fwd.hpp>
22
#include <boost/intrusive/detail/assert.hpp>
23
#include <boost/intrusive/detail/utilities.hpp>
24
#include <boost/intrusive/detail/tree_algorithms.hpp>
25
#include <boost/intrusive/pointer_traits.hpp>
31
//! avltree_algorithms is configured with a NodeTraits class, which encapsulates the
32
//! information about the node to be manipulated. NodeTraits must support the
33
//! following interface:
37
//! <tt>node</tt>: The type of the node that forms the circular list
39
//! <tt>node_ptr</tt>: A pointer to a node
41
//! <tt>const_node_ptr</tt>: A pointer to a const node
43
//! <tt>balance</tt>: The type of the balance factor
45
//! <b>Static functions</b>:
47
//! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
49
//! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
51
//! <tt>static node_ptr get_left(const_node_ptr n);</tt>
53
//! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
55
//! <tt>static node_ptr get_right(const_node_ptr n);</tt>
57
//! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
59
//! <tt>static balance get_balance(const_node_ptr n);</tt>
61
//! <tt>static void set_balance(node_ptr n, balance b);</tt>
63
//! <tt>static balance negative();</tt>
65
//! <tt>static balance zero();</tt>
67
//! <tt>static balance positive();</tt>
68
template<class NodeTraits>
69
class avltree_algorithms
72
typedef typename NodeTraits::node node;
73
typedef NodeTraits node_traits;
74
typedef typename NodeTraits::node_ptr node_ptr;
75
typedef typename NodeTraits::const_node_ptr const_node_ptr;
76
typedef typename NodeTraits::balance balance;
80
typedef detail::tree_algorithms<NodeTraits> tree_algorithms;
83
struct avltree_node_cloner
84
: private detail::ebo_functor_holder<F>
86
typedef detail::ebo_functor_holder<F> base_t;
88
avltree_node_cloner(F f)
92
node_ptr operator()(const node_ptr &p)
94
node_ptr n = base_t::get()(p);
95
NodeTraits::set_balance(n, NodeTraits::get_balance(p));
100
struct avltree_erase_fixup
102
void operator()(const node_ptr &to_erase, const node_ptr &successor)
103
{ NodeTraits::set_balance(successor, NodeTraits::get_balance(to_erase)); }
106
static node_ptr uncast(const const_node_ptr & ptr)
107
{ return pointer_traits<node_ptr>::const_cast_from(ptr); }
111
static node_ptr begin_node(const const_node_ptr & header)
112
{ return tree_algorithms::begin_node(header); }
114
static node_ptr end_node(const const_node_ptr & header)
115
{ return tree_algorithms::end_node(header); }
117
//! This type is the information that will be
118
//! filled by insert_unique_check
119
typedef typename tree_algorithms::insert_commit_data insert_commit_data;
121
//! <b>Requires</b>: header1 and header2 must be the header nodes
124
//! <b>Effects</b>: Swaps two trees. After the function header1 will contain
125
//! links to the second tree and header2 will have links to the first tree.
127
//! <b>Complexity</b>: Constant.
129
//! <b>Throws</b>: Nothing.
130
static void swap_tree(const node_ptr & header1, const node_ptr & header2)
131
{ return tree_algorithms::swap_tree(header1, header2); }
133
//! <b>Requires</b>: node1 and node2 can't be header nodes
136
//! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
137
//! in the position node2 before the function. node2 will be inserted in the
138
//! position node1 had before the function.
140
//! <b>Complexity</b>: Logarithmic.
142
//! <b>Throws</b>: Nothing.
144
//! <b>Note</b>: This function will break container ordering invariants if
145
//! node1 and node2 are not equivalent according to the ordering rules.
147
//!Experimental function
148
static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
153
node_ptr header1(tree_algorithms::get_header(node1)), header2(tree_algorithms::get_header(node2));
154
swap_nodes(node1, header1, node2, header2);
157
//! <b>Requires</b>: node1 and node2 can't be header nodes
158
//! of two trees with header header1 and header2.
160
//! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
161
//! in the position node2 before the function. node2 will be inserted in the
162
//! position node1 had before the function.
164
//! <b>Complexity</b>: Constant.
166
//! <b>Throws</b>: Nothing.
168
//! <b>Note</b>: This function will break container ordering invariants if
169
//! node1 and node2 are not equivalent according to the ordering rules.
171
//!Experimental function
172
static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
174
if(node1 == node2) return;
176
tree_algorithms::swap_nodes(node1, header1, node2, header2);
178
balance c = NodeTraits::get_balance(node1);
179
NodeTraits::set_balance(node1, NodeTraits::get_balance(node2));
180
NodeTraits::set_balance(node2, c);
183
//! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
184
//! and new_node must not be inserted in a tree.
186
//! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
187
//! tree with new_node. The tree does not need to be rebalanced
189
//! <b>Complexity</b>: Logarithmic.
191
//! <b>Throws</b>: Nothing.
193
//! <b>Note</b>: This function will break container ordering invariants if
194
//! new_node is not equivalent to node_to_be_replaced according to the
195
//! ordering rules. This function is faster than erasing and inserting
196
//! the node, since no rebalancing and comparison is needed.
198
//!Experimental function
199
static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
201
if(node_to_be_replaced == new_node)
203
replace_node(node_to_be_replaced, tree_algorithms::get_header(node_to_be_replaced), new_node);
206
//! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
207
//! with header "header" and new_node must not be inserted in a tree.
209
//! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
210
//! tree with new_node. The tree does not need to be rebalanced
212
//! <b>Complexity</b>: Constant.
214
//! <b>Throws</b>: Nothing.
216
//! <b>Note</b>: This function will break container ordering invariants if
217
//! new_node is not equivalent to node_to_be_replaced according to the
218
//! ordering rules. This function is faster than erasing and inserting
219
//! the node, since no rebalancing or comparison is needed.
221
//!Experimental function
222
static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
224
tree_algorithms::replace_node(node_to_be_replaced, header, new_node);
225
NodeTraits::set_balance(new_node, NodeTraits::get_balance(node_to_be_replaced));
228
//! <b>Requires</b>: node is a tree node but not the header.
230
//! <b>Effects</b>: Unlinks the node and rebalances the tree.
232
//! <b>Complexity</b>: Average complexity is constant time.
234
//! <b>Throws</b>: Nothing.
235
static void unlink(const node_ptr & node)
237
node_ptr x = NodeTraits::get_parent(node);
240
x = NodeTraits::get_parent(x);
245
//! <b>Requires</b>: header is the header of a tree.
247
//! <b>Effects</b>: Unlinks the leftmost node from the tree, and
248
//! updates the header link to the new leftmost node.
250
//! <b>Complexity</b>: Average complexity is constant time.
252
//! <b>Throws</b>: Nothing.
254
//! <b>Notes</b>: This function breaks the tree and the tree can
255
//! only be used for more unlink_leftmost_without_rebalance calls.
256
//! This function is normally used to achieve a step by step
257
//! controlled destruction of the tree.
258
static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header)
259
{ return tree_algorithms::unlink_leftmost_without_rebalance(header); }
261
//! <b>Requires</b>: node is a node of the tree or an node initialized
264
//! <b>Effects</b>: Returns true if the node is initialized by init().
266
//! <b>Complexity</b>: Constant time.
268
//! <b>Throws</b>: Nothing.
269
static bool unique(const const_node_ptr & node)
270
{ return tree_algorithms::unique(node); }
272
//! <b>Requires</b>: node is a node of the tree but it's not the header.
274
//! <b>Effects</b>: Returns the number of nodes of the subtree.
276
//! <b>Complexity</b>: Linear time.
278
//! <b>Throws</b>: Nothing.
279
static std::size_t count(const const_node_ptr & node)
280
{ return tree_algorithms::count(node); }
282
//! <b>Requires</b>: header is the header node of the tree.
284
//! <b>Effects</b>: Returns the number of nodes above the header.
286
//! <b>Complexity</b>: Linear time.
288
//! <b>Throws</b>: Nothing.
289
static std::size_t size(const const_node_ptr & header)
290
{ return tree_algorithms::size(header); }
292
//! <b>Requires</b>: p is a node from the tree except the header.
294
//! <b>Effects</b>: Returns the next node of the tree.
296
//! <b>Complexity</b>: Average constant time.
298
//! <b>Throws</b>: Nothing.
299
static node_ptr next_node(const node_ptr & p)
300
{ return tree_algorithms::next_node(p); }
302
//! <b>Requires</b>: p is a node from the tree except the leftmost node.
304
//! <b>Effects</b>: Returns the previous node of the tree.
306
//! <b>Complexity</b>: Average constant time.
308
//! <b>Throws</b>: Nothing.
309
static node_ptr prev_node(const node_ptr & p)
310
{ return tree_algorithms::prev_node(p); }
312
//! <b>Requires</b>: node must not be part of any tree.
314
//! <b>Effects</b>: After the function unique(node) == true.
316
//! <b>Complexity</b>: Constant.
318
//! <b>Throws</b>: Nothing.
320
//! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
321
static void init(const node_ptr & node)
322
{ tree_algorithms::init(node); }
324
//! <b>Requires</b>: node must not be part of any tree.
326
//! <b>Effects</b>: Initializes the header to represent an empty tree.
327
//! unique(header) == true.
329
//! <b>Complexity</b>: Constant.
331
//! <b>Throws</b>: Nothing.
333
//! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
334
static void init_header(const node_ptr & header)
336
tree_algorithms::init_header(header);
337
NodeTraits::set_balance(header, NodeTraits::zero());
340
//! <b>Requires</b>: header must be the header of a tree, z a node
341
//! of that tree and z != header.
343
//! <b>Effects</b>: Erases node "z" from the tree with header "header".
345
//! <b>Complexity</b>: Amortized constant time.
347
//! <b>Throws</b>: Nothing.
348
static node_ptr erase(const node_ptr & header, const node_ptr & z)
350
typename tree_algorithms::data_for_rebalance info;
351
tree_algorithms::erase(header, z, avltree_erase_fixup(), info);
353
node_ptr x_parent = info.x_parent;
356
rebalance_after_erasure(header, x, x_parent);
360
//! <b>Requires</b>: "cloner" must be a function
361
//! object taking a node_ptr and returning a new cloned node of it. "disposer" must
362
//! take a node_ptr and shouldn't throw.
364
//! <b>Effects</b>: First empties target tree calling
365
//! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
366
//! except the header.
368
//! Then, duplicates the entire tree pointed by "source_header" cloning each
369
//! source node with <tt>node_ptr Cloner::operator()(const node_ptr &)</tt> to obtain
370
//! the nodes of the target tree. If "cloner" throws, the cloned target nodes
371
//! are disposed using <tt>void disposer(const node_ptr &)</tt>.
373
//! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
374
//! number of elements of tree target tree when calling this function.
376
//! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
377
template <class Cloner, class Disposer>
379
(const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
381
avltree_node_cloner<Cloner> new_cloner(cloner);
382
tree_algorithms::clone(source_header, target_header, new_cloner, disposer);
385
//! <b>Requires</b>: "disposer" must be an object function
386
//! taking a node_ptr parameter and shouldn't throw.
388
//! <b>Effects</b>: Empties the target tree calling
389
//! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
390
//! except the header.
392
//! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
393
//! number of elements of tree target tree when calling this function.
395
//! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
396
template<class Disposer>
397
static void clear_and_dispose(const node_ptr & header, Disposer disposer)
398
{ tree_algorithms::clear_and_dispose(header, disposer); }
400
//! <b>Requires</b>: "header" must be the header node of a tree.
401
//! KeyNodePtrCompare is a function object that induces a strict weak
402
//! ordering compatible with the strict weak ordering used to create the
403
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
405
//! <b>Effects</b>: Returns an node_ptr to the first element that is
406
//! not less than "key" according to "comp" or "header" if that element does
409
//! <b>Complexity</b>: Logarithmic.
411
//! <b>Throws</b>: If "comp" throws.
412
template<class KeyType, class KeyNodePtrCompare>
413
static node_ptr lower_bound
414
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
415
{ return tree_algorithms::lower_bound(header, key, comp); }
417
//! <b>Requires</b>: "header" must be the header node of a tree.
418
//! KeyNodePtrCompare is a function object that induces a strict weak
419
//! ordering compatible with the strict weak ordering used to create the
420
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
422
//! <b>Effects</b>: Returns an node_ptr to the first element that is greater
423
//! than "key" according to "comp" or "header" if that element does not exist.
425
//! <b>Complexity</b>: Logarithmic.
427
//! <b>Throws</b>: If "comp" throws.
428
template<class KeyType, class KeyNodePtrCompare>
429
static node_ptr upper_bound
430
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
431
{ return tree_algorithms::upper_bound(header, key, comp); }
433
//! <b>Requires</b>: "header" must be the header node of a tree.
434
//! KeyNodePtrCompare is a function object that induces a strict weak
435
//! ordering compatible with the strict weak ordering used to create the
436
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
438
//! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
439
//! "key" according to "comp" or "header" if that element does not exist.
441
//! <b>Complexity</b>: Logarithmic.
443
//! <b>Throws</b>: If "comp" throws.
444
template<class KeyType, class KeyNodePtrCompare>
446
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
447
{ return tree_algorithms::find(header, key, comp); }
449
//! <b>Requires</b>: "header" must be the header node of a tree.
450
//! KeyNodePtrCompare is a function object that induces a strict weak
451
//! ordering compatible with the strict weak ordering used to create the
452
//! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
454
//! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
455
//! all elements that are equivalent to "key" according to "comp" or an
456
//! empty range that indicates the position where those elements would be
457
//! if they there are no equivalent elements.
459
//! <b>Complexity</b>: Logarithmic.
461
//! <b>Throws</b>: If "comp" throws.
462
template<class KeyType, class KeyNodePtrCompare>
463
static std::pair<node_ptr, node_ptr> equal_range
464
(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
465
{ return tree_algorithms::equal_range(header, key, comp); }
467
//! <b>Requires</b>: "h" must be the header node of a tree.
468
//! NodePtrCompare is a function object that induces a strict weak
469
//! ordering compatible with the strict weak ordering used to create the
470
//! the tree. NodePtrCompare compares two node_ptrs.
472
//! <b>Effects</b>: Inserts new_node into the tree before the upper bound
473
//! according to "comp".
475
//! <b>Complexity</b>: Average complexity for insert element is at
476
//! most logarithmic.
478
//! <b>Throws</b>: If "comp" throws.
479
template<class NodePtrCompare>
480
static node_ptr insert_equal_upper_bound
481
(const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
483
tree_algorithms::insert_equal_upper_bound(h, new_node, comp);
484
rebalance_after_insertion(h, new_node);
488
//! <b>Requires</b>: "h" must be the header node of a tree.
489
//! NodePtrCompare is a function object that induces a strict weak
490
//! ordering compatible with the strict weak ordering used to create the
491
//! the tree. NodePtrCompare compares two node_ptrs.
493
//! <b>Effects</b>: Inserts new_node into the tree before the lower bound
494
//! according to "comp".
496
//! <b>Complexity</b>: Average complexity for insert element is at
497
//! most logarithmic.
499
//! <b>Throws</b>: If "comp" throws.
500
template<class NodePtrCompare>
501
static node_ptr insert_equal_lower_bound
502
(const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
504
tree_algorithms::insert_equal_lower_bound(h, new_node, comp);
505
rebalance_after_insertion(h, new_node);
509
//! <b>Requires</b>: "header" must be the header node of a tree.
510
//! NodePtrCompare is a function object that induces a strict weak
511
//! ordering compatible with the strict weak ordering used to create the
512
//! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
513
//! the "header"'s tree.
515
//! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
516
//! where it will be inserted. If "hint" is the upper_bound
517
//! the insertion takes constant time (two comparisons in the worst case).
519
//! <b>Complexity</b>: Logarithmic in general, but it is amortized
520
//! constant time if new_node is inserted immediately before "hint".
522
//! <b>Throws</b>: If "comp" throws.
523
template<class NodePtrCompare>
524
static node_ptr insert_equal
525
(const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp)
527
tree_algorithms::insert_equal(header, hint, new_node, comp);
528
rebalance_after_insertion(header, new_node);
532
//! <b>Requires</b>: "header" must be the header node of a tree.
533
//! "pos" must be a valid iterator or header (end) node.
534
//! "pos" must be an iterator pointing to the successor to "new_node"
535
//! once inserted according to the order of already inserted nodes. This function does not
536
//! check "pos" and this precondition must be guaranteed by the caller.
538
//! <b>Effects</b>: Inserts new_node into the tree before "pos".
540
//! <b>Complexity</b>: Constant-time.
542
//! <b>Throws</b>: Nothing.
544
//! <b>Note</b>: If "pos" is not the successor of the newly inserted "new_node"
545
//! tree invariants might be broken.
546
static node_ptr insert_before
547
(const node_ptr & header, const node_ptr & pos, const node_ptr & new_node)
549
tree_algorithms::insert_before(header, pos, new_node);
550
rebalance_after_insertion(header, new_node);
554
//! <b>Requires</b>: "header" must be the header node of a tree.
555
//! "new_node" must be, according to the used ordering no less than the
556
//! greatest inserted key.
558
//! <b>Effects</b>: Inserts new_node into the tree before "pos".
560
//! <b>Complexity</b>: Constant-time.
562
//! <b>Throws</b>: Nothing.
564
//! <b>Note</b>: If "new_node" is less than the greatest inserted key
565
//! tree invariants are broken. This function is slightly faster than
566
//! using "insert_before".
567
static void push_back(const node_ptr & header, const node_ptr & new_node)
569
tree_algorithms::push_back(header, new_node);
570
rebalance_after_insertion(header, new_node);
573
//! <b>Requires</b>: "header" must be the header node of a tree.
574
//! "new_node" must be, according to the used ordering, no greater than the
575
//! lowest inserted key.
577
//! <b>Effects</b>: Inserts new_node into the tree before "pos".
579
//! <b>Complexity</b>: Constant-time.
581
//! <b>Throws</b>: Nothing.
583
//! <b>Note</b>: If "new_node" is greater than the lowest inserted key
584
//! tree invariants are broken. This function is slightly faster than
585
//! using "insert_before".
586
static void push_front(const node_ptr & header, const node_ptr & new_node)
588
tree_algorithms::push_front(header, new_node);
589
rebalance_after_insertion(header, new_node);
592
//! <b>Requires</b>: "header" must be the header node of a tree.
593
//! KeyNodePtrCompare is a function object that induces a strict weak
594
//! ordering compatible with the strict weak ordering used to create the
595
//! the tree. NodePtrCompare compares KeyType with a node_ptr.
597
//! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
598
//! tree according to "comp" and obtains the needed information to realize
599
//! a constant-time node insertion if there is no equivalent node.
601
//! <b>Returns</b>: If there is an equivalent value
602
//! returns a pair containing a node_ptr to the already present node
603
//! and false. If there is not equivalent key can be inserted returns true
604
//! in the returned pair's boolean and fills "commit_data" that is meant to
605
//! be used with the "insert_commit" function to achieve a constant-time
606
//! insertion function.
608
//! <b>Complexity</b>: Average complexity is at most logarithmic.
610
//! <b>Throws</b>: If "comp" throws.
612
//! <b>Notes</b>: This function is used to improve performance when constructing
613
//! a node is expensive and the user does not want to have two equivalent nodes
614
//! in the tree: if there is an equivalent value
615
//! the constructed object must be discarded. Many times, the part of the
616
//! node that is used to impose the order is much cheaper to construct
617
//! than the node and this function offers the possibility to use that part
618
//! to check if the insertion will be successful.
620
//! If the check is successful, the user can construct the node and use
621
//! "insert_commit" to insert the node in constant-time. This gives a total
622
//! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
624
//! "commit_data" remains valid for a subsequent "insert_unique_commit" only
625
//! if no more objects are inserted or erased from the set.
626
template<class KeyType, class KeyNodePtrCompare>
627
static std::pair<node_ptr, bool> insert_unique_check
628
(const const_node_ptr & header, const KeyType &key
629
,KeyNodePtrCompare comp, insert_commit_data &commit_data)
630
{ return tree_algorithms::insert_unique_check(header, key, comp, commit_data); }
632
//! <b>Requires</b>: "header" must be the header node of a tree.
633
//! KeyNodePtrCompare is a function object that induces a strict weak
634
//! ordering compatible with the strict weak ordering used to create the
635
//! the tree. NodePtrCompare compares KeyType with a node_ptr.
636
//! "hint" is node from the "header"'s tree.
638
//! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
639
//! tree according to "comp" using "hint" as a hint to where it should be
640
//! inserted and obtains the needed information to realize
641
//! a constant-time node insertion if there is no equivalent node.
642
//! If "hint" is the upper_bound the function has constant time
643
//! complexity (two comparisons in the worst case).
645
//! <b>Returns</b>: If there is an equivalent value
646
//! returns a pair containing a node_ptr to the already present node
647
//! and false. If there is not equivalent key can be inserted returns true
648
//! in the returned pair's boolean and fills "commit_data" that is meant to
649
//! be used with the "insert_commit" function to achieve a constant-time
650
//! insertion function.
652
//! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
653
//! amortized constant time if new_node should be inserted immediately before "hint".
655
//! <b>Throws</b>: If "comp" throws.
657
//! <b>Notes</b>: This function is used to improve performance when constructing
658
//! a node is expensive and the user does not want to have two equivalent nodes
659
//! in the tree: if there is an equivalent value
660
//! the constructed object must be discarded. Many times, the part of the
661
//! node that is used to impose the order is much cheaper to construct
662
//! than the node and this function offers the possibility to use that part
663
//! to check if the insertion will be successful.
665
//! If the check is successful, the user can construct the node and use
666
//! "insert_commit" to insert the node in constant-time. This gives a total
667
//! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
669
//! "commit_data" remains valid for a subsequent "insert_unique_commit" only
670
//! if no more objects are inserted or erased from the set.
671
template<class KeyType, class KeyNodePtrCompare>
672
static std::pair<node_ptr, bool> insert_unique_check
673
(const const_node_ptr & header, const node_ptr &hint, const KeyType &key
674
,KeyNodePtrCompare comp, insert_commit_data &commit_data)
675
{ return tree_algorithms::insert_unique_check(header, hint, key, comp, commit_data); }
677
//! <b>Requires</b>: "header" must be the header node of a tree.
678
//! "commit_data" must have been obtained from a previous call to
679
//! "insert_unique_check". No objects should have been inserted or erased
680
//! from the set between the "insert_unique_check" that filled "commit_data"
681
//! and the call to "insert_commit".
684
//! <b>Effects</b>: Inserts new_node in the set using the information obtained
685
//! from the "commit_data" that a previous "insert_check" filled.
687
//! <b>Complexity</b>: Constant time.
689
//! <b>Throws</b>: Nothing.
691
//! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
692
//! previously executed to fill "commit_data". No value should be inserted or
693
//! erased between the "insert_check" and "insert_commit" calls.
694
static void insert_unique_commit
695
(const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
697
tree_algorithms::insert_unique_commit(header, new_value, commit_data);
698
rebalance_after_insertion(header, new_value);
701
//! <b>Requires</b>: "n" must be a node inserted in a tree.
703
//! <b>Effects</b>: Returns a pointer to the header node of the tree.
705
//! <b>Complexity</b>: Logarithmic.
707
//! <b>Throws</b>: Nothing.
708
static node_ptr get_header(const node_ptr & n)
709
{ return tree_algorithms::get_header(n); }
714
//! <b>Requires</b>: p is a node of a tree.
716
//! <b>Effects</b>: Returns true if p is the header of the tree.
718
//! <b>Complexity</b>: Constant.
720
//! <b>Throws</b>: Nothing.
721
static bool is_header(const const_node_ptr & p)
722
{ return NodeTraits::get_balance(p) == NodeTraits::zero() && tree_algorithms::is_header(p); }
724
static void rebalance_after_erasure(const node_ptr & header, const node_ptr & xnode, const node_ptr & xnode_parent)
726
node_ptr x(xnode), x_parent(xnode_parent);
727
for (node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)) {
728
const balance x_parent_balance = NodeTraits::get_balance(x_parent);
729
if(x_parent_balance == NodeTraits::zero()){
730
NodeTraits::set_balance(x_parent,
731
(x == NodeTraits::get_right(x_parent) ? NodeTraits::negative() : NodeTraits::positive()));
732
break; // the height didn't change, let's stop here
734
else if(x_parent_balance == NodeTraits::negative()){
735
if (x == NodeTraits::get_left(x_parent)) {
736
NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
738
x_parent = NodeTraits::get_parent(x_parent);
743
node_ptr a = NodeTraits::get_left(x_parent);
744
BOOST_INTRUSIVE_INVARIANT_ASSERT(a);
745
if (NodeTraits::get_balance(a) == NodeTraits::positive()) {
746
// a MUST have a right child
747
BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(a));
748
rotate_left_right(x_parent, header);
749
x = NodeTraits::get_parent(x_parent);
750
x_parent = NodeTraits::get_parent(x);
753
rotate_right(x_parent, header);
754
x = NodeTraits::get_parent(x_parent);
755
x_parent = NodeTraits::get_parent(x);
758
// if changed from negative to NodeTraits::positive(), no need to check above
759
if (NodeTraits::get_balance(x) == NodeTraits::positive()){
764
else if(x_parent_balance == NodeTraits::positive()){
765
if (x == NodeTraits::get_right(x_parent)) {
766
NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
768
x_parent = NodeTraits::get_parent(x_parent);
773
node_ptr a = NodeTraits::get_right(x_parent);
774
BOOST_INTRUSIVE_INVARIANT_ASSERT(a);
775
if (NodeTraits::get_balance(a) == NodeTraits::negative()) {
776
// a MUST have then a left child
777
BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(a));
778
rotate_right_left(x_parent, header);
780
x = NodeTraits::get_parent(x_parent);
781
x_parent = NodeTraits::get_parent(x);
784
rotate_left(x_parent, header);
785
x = NodeTraits::get_parent(x_parent);
786
x_parent = NodeTraits::get_parent(x);
788
// if changed from NodeTraits::positive() to negative, no need to check above
789
if (NodeTraits::get_balance(x) == NodeTraits::negative()){
795
BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
800
static void rebalance_after_insertion(const node_ptr & header, const node_ptr & xnode)
803
NodeTraits::set_balance(x, NodeTraits::zero());
805
for(node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)){
806
const balance x_parent_balance = NodeTraits::get_balance(NodeTraits::get_parent(x));
808
if(x_parent_balance == NodeTraits::zero()){
809
// if x is left, parent will have parent->bal_factor = negative
810
// else, parent->bal_factor = NodeTraits::positive()
811
NodeTraits::set_balance( NodeTraits::get_parent(x)
812
, x == NodeTraits::get_left(NodeTraits::get_parent(x))
813
? NodeTraits::negative() : NodeTraits::positive() );
814
x = NodeTraits::get_parent(x);
816
else if(x_parent_balance == NodeTraits::positive()){
817
// if x is a left child, parent->bal_factor = zero
818
if (x == NodeTraits::get_left(NodeTraits::get_parent(x)))
819
NodeTraits::set_balance(NodeTraits::get_parent(x), NodeTraits::zero());
820
else{ // x is a right child, needs rebalancing
821
if (NodeTraits::get_balance(x) == NodeTraits::negative())
822
rotate_right_left(NodeTraits::get_parent(x), header);
824
rotate_left(NodeTraits::get_parent(x), header);
828
else if(x_parent_balance == NodeTraits::negative()){
829
// if x is a left child, needs rebalancing
830
if (x == NodeTraits::get_left(NodeTraits::get_parent(x))) {
831
if (NodeTraits::get_balance(x) == NodeTraits::positive())
832
rotate_left_right(NodeTraits::get_parent(x), header);
834
rotate_right(NodeTraits::get_parent(x), header);
837
NodeTraits::set_balance(NodeTraits::get_parent(x), NodeTraits::zero());
841
BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
846
static void left_right_balancing(const node_ptr & a, const node_ptr & b, const node_ptr & c)
849
const balance c_balance = NodeTraits::get_balance(c);
850
const balance zero_balance = NodeTraits::zero();
851
NodeTraits::set_balance(c, zero_balance);
852
if(c_balance == NodeTraits::negative()){
853
NodeTraits::set_balance(a, NodeTraits::positive());
854
NodeTraits::set_balance(b, zero_balance);
856
else if(c_balance == zero_balance){
857
NodeTraits::set_balance(a, zero_balance);
858
NodeTraits::set_balance(b, zero_balance);
860
else if(c_balance == NodeTraits::positive()){
861
NodeTraits::set_balance(a, zero_balance);
862
NodeTraits::set_balance(b, NodeTraits::negative());
865
BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
869
static void rotate_left_right(const node_ptr a, const node_ptr & hdr)
877
// [d] c [d] e f [g] //
880
node_ptr b = NodeTraits::get_left(a), c = NodeTraits::get_right(b);
881
tree_algorithms::rotate_left(b, hdr);
882
tree_algorithms::rotate_right(a, hdr);
883
left_right_balancing(a, b, c);
886
static void rotate_right_left(const node_ptr a, const node_ptr & hdr)
892
// [d] b(neg) ==> a b //
894
// c [g] [d] e f [g] //
897
node_ptr b = NodeTraits::get_right(a), c = NodeTraits::get_left(b);
898
tree_algorithms::rotate_right(b, hdr);
899
tree_algorithms::rotate_left(a, hdr);
900
left_right_balancing(b, a, c);
903
static void rotate_left(const node_ptr x, const node_ptr & hdr)
905
const node_ptr y = NodeTraits::get_right(x);
906
tree_algorithms::rotate_left(x, hdr);
908
// reset the balancing factor
909
if (NodeTraits::get_balance(y) == NodeTraits::positive()) {
910
NodeTraits::set_balance(x, NodeTraits::zero());
911
NodeTraits::set_balance(y, NodeTraits::zero());
913
else { // this doesn't happen during insertions
914
NodeTraits::set_balance(x, NodeTraits::positive());
915
NodeTraits::set_balance(y, NodeTraits::negative());
919
static void rotate_right(const node_ptr x, const node_ptr & hdr)
921
const node_ptr y = NodeTraits::get_left(x);
922
tree_algorithms::rotate_right(x, hdr);
924
// reset the balancing factor
925
if (NodeTraits::get_balance(y) == NodeTraits::negative()) {
926
NodeTraits::set_balance(x, NodeTraits::zero());
927
NodeTraits::set_balance(y, NodeTraits::zero());
929
else { // this doesn't happen during insertions
930
NodeTraits::set_balance(x, NodeTraits::negative());
931
NodeTraits::set_balance(y, NodeTraits::positive());
938
} //namespace intrusive
941
#include <boost/intrusive/detail/config_end.hpp>
943
#endif //BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP