fix doc example typo
[boost.git] / boost / intrusive / sgtree_algorithms.hpp
blobc9af84f5efa9d27d993d1560d3c40441e6216667
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007.
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 // Scapegoat tree algorithms are taken from the paper titled:
14 // "Scapegoat Trees" by Igal Galperin Ronald L. Rivest.
16 /////////////////////////////////////////////////////////////////////////////
17 #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
18 #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
20 #include <boost/intrusive/detail/config_begin.hpp>
22 #include <cstddef>
23 #include <boost/intrusive/intrusive_fwd.hpp>
24 #include <boost/intrusive/detail/assert.hpp>
25 #include <boost/intrusive/detail/utilities.hpp>
26 #include <boost/intrusive/detail/tree_algorithms.hpp>
29 namespace boost {
30 namespace intrusive {
32 //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
33 //! information about the node to be manipulated. NodeTraits must support the
34 //! following interface:
35 //!
36 //! <b>Typedefs</b>:
37 //!
38 //! <tt>node</tt>: The type of the node that forms the circular list
39 //!
40 //! <tt>node_ptr</tt>: A pointer to a node
41 //!
42 //! <tt>const_node_ptr</tt>: A pointer to a const node
43 //!
44 //! <b>Static functions</b>:
45 //!
46 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
47 //!
48 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
49 //!
50 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
51 //!
52 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
53 //!
54 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
55 //!
56 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
57 template<class NodeTraits>
58 class sgtree_algorithms
60 public:
61 typedef typename NodeTraits::node node;
62 typedef NodeTraits node_traits;
63 typedef typename NodeTraits::node_ptr node_ptr;
64 typedef typename NodeTraits::const_node_ptr const_node_ptr;
66 /// @cond
67 private:
69 typedef detail::tree_algorithms<NodeTraits> tree_algorithms;
71 static node_ptr uncast(const_node_ptr ptr)
73 return node_ptr(const_cast<node*>(::boost::intrusive::detail::get_pointer(ptr)));
75 /// @endcond
77 public:
78 static node_ptr begin_node(const_node_ptr header)
79 { return tree_algorithms::begin_node(header); }
81 static node_ptr end_node(const_node_ptr header)
82 { return tree_algorithms::end_node(header); }
84 //! This type is the information that will be
85 //! filled by insert_unique_check
86 struct insert_commit_data
87 : tree_algorithms::insert_commit_data
89 std::size_t depth;
92 //! <b>Requires</b>: header1 and header2 must be the header nodes
93 //! of two trees.
94 //!
95 //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
96 //! links to the second tree and header2 will have links to the first tree.
97 //!
98 //! <b>Complexity</b>: Constant.
99 //!
100 //! <b>Throws</b>: Nothing.
101 static void swap_tree(node_ptr header1, node_ptr header2)
102 { return tree_algorithms::swap_tree(header1, header2); }
104 //! <b>Requires</b>: node1 and node2 can't be header nodes
105 //! of two trees.
106 //!
107 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
108 //! in the position node2 before the function. node2 will be inserted in the
109 //! position node1 had before the function.
110 //!
111 //! <b>Complexity</b>: Logarithmic.
112 //!
113 //! <b>Throws</b>: Nothing.
114 //!
115 //! <b>Note</b>: This function will break container ordering invariants if
116 //! node1 and node2 are not equivalent according to the ordering rules.
118 //!Experimental function
119 static void swap_nodes(node_ptr node1, node_ptr node2)
121 if(node1 == node2)
122 return;
124 node_ptr header1(tree_algorithms::get_header(node1)), header2(tree_algorithms::get_header(node2));
125 swap_nodes(node1, header1, node2, header2);
128 //! <b>Requires</b>: node1 and node2 can't be header nodes
129 //! of two trees with header header1 and header2.
130 //!
131 //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
132 //! in the position node2 before the function. node2 will be inserted in the
133 //! position node1 had before the function.
134 //!
135 //! <b>Complexity</b>: Constant.
136 //!
137 //! <b>Throws</b>: Nothing.
138 //!
139 //! <b>Note</b>: This function will break container ordering invariants if
140 //! node1 and node2 are not equivalent according to the ordering rules.
142 //!Experimental function
143 static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2)
144 { tree_algorithms::swap_nodes(node1, header1, node2, header2); }
146 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
147 //! and new_node must not be inserted in a tree.
148 //!
149 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
150 //! tree with new_node. The tree does not need to be rebalanced
151 //!
152 //! <b>Complexity</b>: Logarithmic.
153 //!
154 //! <b>Throws</b>: Nothing.
155 //!
156 //! <b>Note</b>: This function will break container ordering invariants if
157 //! new_node is not equivalent to node_to_be_replaced according to the
158 //! ordering rules. This function is faster than erasing and inserting
159 //! the node, since no rebalancing and comparison is needed.
161 //!Experimental function
162 static void replace_node(node_ptr node_to_be_replaced, node_ptr new_node)
164 if(node_to_be_replaced == new_node)
165 return;
166 replace_node(node_to_be_replaced, tree_algorithms::get_header(node_to_be_replaced), new_node);
169 //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
170 //! with header "header" and new_node must not be inserted in a tree.
171 //!
172 //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
173 //! tree with new_node. The tree does not need to be rebalanced
174 //!
175 //! <b>Complexity</b>: Constant.
176 //!
177 //! <b>Throws</b>: Nothing.
178 //!
179 //! <b>Note</b>: This function will break container ordering invariants if
180 //! new_node is not equivalent to node_to_be_replaced according to the
181 //! ordering rules. This function is faster than erasing and inserting
182 //! the node, since no rebalancing or comparison is needed.
184 //!Experimental function
185 static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node)
186 { tree_algorithms::replace_node(node_to_be_replaced, header, new_node); }
188 //! <b>Requires</b>: node is a tree node but not the header.
189 //!
190 //! <b>Effects</b>: Unlinks the node and rebalances the tree.
191 //!
192 //! <b>Complexity</b>: Average complexity is constant time.
193 //!
194 //! <b>Throws</b>: Nothing.
195 static void unlink(node_ptr node)
197 node_ptr x = NodeTraits::get_parent(node);
198 if(x){
199 while(!is_header(x))
200 x = NodeTraits::get_parent(x);
201 tree_algorithms::erase(x, node);
205 //! <b>Requires</b>: header is the header of a tree.
206 //!
207 //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
208 //! updates the header link to the new leftmost node.
209 //!
210 //! <b>Complexity</b>: Average complexity is constant time.
211 //!
212 //! <b>Throws</b>: Nothing.
213 //!
214 //! <b>Notes</b>: This function breaks the tree and the tree can
215 //! only be used for more unlink_leftmost_without_rebalance calls.
216 //! This function is normally used to achieve a step by step
217 //! controlled destruction of the tree.
218 static node_ptr unlink_leftmost_without_rebalance(node_ptr header)
219 { return tree_algorithms::unlink_leftmost_without_rebalance(header); }
221 //! <b>Requires</b>: node is a node of the tree or an node initialized
222 //! by init(...).
223 //!
224 //! <b>Effects</b>: Returns true if the node is initialized by init().
225 //!
226 //! <b>Complexity</b>: Constant time.
227 //!
228 //! <b>Throws</b>: Nothing.
229 static bool unique(const_node_ptr node)
230 { return tree_algorithms::unique(node); }
232 //! <b>Requires</b>: node is a node of the tree but it's not the header.
233 //!
234 //! <b>Effects</b>: Returns the number of nodes of the subtree.
235 //!
236 //! <b>Complexity</b>: Linear time.
237 //!
238 //! <b>Throws</b>: Nothing.
239 static std::size_t count(const_node_ptr node)
240 { return tree_algorithms::count(node); }
242 //! <b>Requires</b>: header is the header node of the tree.
243 //!
244 //! <b>Effects</b>: Returns the number of nodes above the header.
245 //!
246 //! <b>Complexity</b>: Linear time.
247 //!
248 //! <b>Throws</b>: Nothing.
249 static std::size_t size(const_node_ptr header)
250 { return tree_algorithms::size(header); }
252 //! <b>Requires</b>: p is a node from the tree except the header.
253 //!
254 //! <b>Effects</b>: Returns the next node of the tree.
255 //!
256 //! <b>Complexity</b>: Average constant time.
257 //!
258 //! <b>Throws</b>: Nothing.
259 static node_ptr next_node(node_ptr p)
260 { return tree_algorithms::next_node(p); }
262 //! <b>Requires</b>: p is a node from the tree except the leftmost node.
263 //!
264 //! <b>Effects</b>: Returns the previous node of the tree.
265 //!
266 //! <b>Complexity</b>: Average constant time.
267 //!
268 //! <b>Throws</b>: Nothing.
269 static node_ptr prev_node(node_ptr p)
270 { return tree_algorithms::prev_node(p); }
272 //! <b>Requires</b>: node must not be part of any tree.
274 //! <b>Effects</b>: After the function unique(node) == true.
275 //!
276 //! <b>Complexity</b>: Constant.
277 //!
278 //! <b>Throws</b>: Nothing.
280 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
281 static void init(node_ptr node)
282 { tree_algorithms::init(node); }
284 //! <b>Requires</b>: node must not be part of any tree.
286 //! <b>Effects</b>: Initializes the header to represent an empty tree.
287 //! unique(header) == true.
288 //!
289 //! <b>Complexity</b>: Constant.
290 //!
291 //! <b>Throws</b>: Nothing.
293 //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
294 static void init_header(node_ptr header)
295 { tree_algorithms::init_header(header); }
297 //! <b>Requires</b>: header must be the header of a tree, z a node
298 //! of that tree and z != header.
300 //! <b>Effects</b>: Erases node "z" from the tree with header "header".
301 //!
302 //! <b>Complexity</b>: Amortized constant time.
303 //!
304 //! <b>Throws</b>: Nothing.
305 template<class AlphaByMaxSize>
306 static node_ptr erase(node_ptr header, node_ptr z, std::size_t tree_size, std::size_t &max_tree_size, AlphaByMaxSize alpha_by_maxsize)
308 //typename tree_algorithms::data_for_rebalance info;
309 tree_algorithms::erase(header, z);
310 --tree_size;
311 if (tree_size > 0 &&
312 tree_size < alpha_by_maxsize(max_tree_size)){
313 tree_algorithms::rebalance(header);
314 max_tree_size = tree_size;
316 return z;
319 //! <b>Requires</b>: "cloner" must be a function
320 //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
321 //! take a node_ptr and shouldn't throw.
323 //! <b>Effects</b>: First empties target tree calling
324 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
325 //! except the header.
326 //!
327 //! Then, duplicates the entire tree pointed by "source_header" cloning each
328 //! source node with <tt>node_ptr Cloner::operator()(node_ptr)</tt> to obtain
329 //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
330 //! are disposed using <tt>void disposer(node_ptr)</tt>.
331 //!
332 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
333 //! number of elements of tree target tree when calling this function.
334 //!
335 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
336 template <class Cloner, class Disposer>
337 static void clone
338 (const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer)
340 tree_algorithms::clone(source_header, target_header, cloner, disposer);
343 //! <b>Requires</b>: "disposer" must be an object function
344 //! taking a node_ptr parameter and shouldn't throw.
346 //! <b>Effects</b>: Empties the target tree calling
347 //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the tree
348 //! except the header.
349 //!
350 //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
351 //! number of elements of tree target tree when calling this function.
352 //!
353 //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
354 template<class Disposer>
355 static void clear_and_dispose(node_ptr header, Disposer disposer)
356 { tree_algorithms::clear_and_dispose(header, disposer); }
358 //! <b>Requires</b>: "header" must be the header node of a tree.
359 //! KeyNodePtrCompare is a function object that induces a strict weak
360 //! ordering compatible with the strict weak ordering used to create the
361 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
363 //! <b>Effects</b>: Returns an node_ptr to the first element that is
364 //! not less than "key" according to "comp" or "header" if that element does
365 //! not exist.
367 //! <b>Complexity</b>: Logarithmic.
368 //!
369 //! <b>Throws</b>: If "comp" throws.
370 template<class KeyType, class KeyNodePtrCompare>
371 static node_ptr lower_bound
372 (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
373 { return tree_algorithms::lower_bound(header, key, comp); }
375 //! <b>Requires</b>: "header" must be the header node of a tree.
376 //! KeyNodePtrCompare is a function object that induces a strict weak
377 //! ordering compatible with the strict weak ordering used to create the
378 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
380 //! <b>Effects</b>: Returns an node_ptr to the first element that is greater
381 //! than "key" according to "comp" or "header" if that element does not exist.
383 //! <b>Complexity</b>: Logarithmic.
384 //!
385 //! <b>Throws</b>: If "comp" throws.
386 template<class KeyType, class KeyNodePtrCompare>
387 static node_ptr upper_bound
388 (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
389 { return tree_algorithms::upper_bound(header, key, comp); }
391 //! <b>Requires</b>: "header" must be the header node of a tree.
392 //! KeyNodePtrCompare is a function object that induces a strict weak
393 //! ordering compatible with the strict weak ordering used to create the
394 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
396 //! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
397 //! "key" according to "comp" or "header" if that element does not exist.
399 //! <b>Complexity</b>: Logarithmic.
400 //!
401 //! <b>Throws</b>: If "comp" throws.
402 template<class KeyType, class KeyNodePtrCompare>
403 static node_ptr find
404 (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
405 { return tree_algorithms::find(header, key, comp); }
407 //! <b>Requires</b>: "header" must be the header node of a tree.
408 //! KeyNodePtrCompare is a function object that induces a strict weak
409 //! ordering compatible with the strict weak ordering used to create the
410 //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
412 //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
413 //! all elements that are equivalent to "key" according to "comp" or an
414 //! empty range that indicates the position where those elements would be
415 //! if they there are no equivalent elements.
417 //! <b>Complexity</b>: Logarithmic.
418 //!
419 //! <b>Throws</b>: If "comp" throws.
420 template<class KeyType, class KeyNodePtrCompare>
421 static std::pair<node_ptr, node_ptr> equal_range
422 (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
423 { return tree_algorithms::equal_range(header, key, comp); }
425 //! <b>Requires</b>: "h" must be the header node of a tree.
426 //! NodePtrCompare is a function object that induces a strict weak
427 //! ordering compatible with the strict weak ordering used to create the
428 //! the tree. NodePtrCompare compares two node_ptrs.
430 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
431 //! according to "comp".
432 //!
433 //! <b>Complexity</b>: Average complexity for insert element is at
434 //! most logarithmic.
435 //!
436 //! <b>Throws</b>: If "comp" throws.
437 template<class NodePtrCompare, class H_Alpha>
438 static node_ptr insert_equal_upper_bound
439 (node_ptr h, node_ptr new_node, NodePtrCompare comp
440 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
442 std::size_t depth;
443 tree_algorithms::insert_equal_upper_bound(h, new_node, comp, &depth);
444 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
445 return new_node;
448 //! <b>Requires</b>: "h" must be the header node of a tree.
449 //! NodePtrCompare is a function object that induces a strict weak
450 //! ordering compatible with the strict weak ordering used to create the
451 //! the tree. NodePtrCompare compares two node_ptrs.
453 //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
454 //! according to "comp".
455 //!
456 //! <b>Complexity</b>: Average complexity for insert element is at
457 //! most logarithmic.
458 //!
459 //! <b>Throws</b>: If "comp" throws.
460 template<class NodePtrCompare, class H_Alpha>
461 static node_ptr insert_equal_lower_bound
462 (node_ptr h, node_ptr new_node, NodePtrCompare comp
463 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
465 std::size_t depth;
466 tree_algorithms::insert_equal_lower_bound(h, new_node, comp, &depth);
467 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
468 return new_node;
471 //! <b>Requires</b>: "header" must be the header node of a tree.
472 //! NodePtrCompare is a function object that induces a strict weak
473 //! ordering compatible with the strict weak ordering used to create the
474 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
475 //! the "header"'s tree.
476 //!
477 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
478 //! where it will be inserted. If "hint" is the upper_bound
479 //! the insertion takes constant time (two comparisons in the worst case).
481 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
482 //! constant time if new_node is inserted immediately before "hint".
483 //!
484 //! <b>Throws</b>: If "comp" throws.
485 template<class NodePtrCompare, class H_Alpha>
486 static node_ptr insert_equal
487 (node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp
488 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
490 std::size_t depth;
491 tree_algorithms::insert_equal(header, hint, new_node, comp, &depth);
492 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
493 return new_node;
496 //! <b>Requires</b>: "header" must be the header node of a tree.
497 //! KeyNodePtrCompare is a function object that induces a strict weak
498 //! ordering compatible with the strict weak ordering used to create the
499 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
500 //!
501 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
502 //! tree according to "comp" and obtains the needed information to realize
503 //! a constant-time node insertion if there is no equivalent node.
505 //! <b>Returns</b>: If there is an equivalent value
506 //! returns a pair containing a node_ptr to the already present node
507 //! and false. If there is not equivalent key can be inserted returns true
508 //! in the returned pair's boolean and fills "commit_data" that is meant to
509 //! be used with the "insert_commit" function to achieve a constant-time
510 //! insertion function.
511 //!
512 //! <b>Complexity</b>: Average complexity is at most logarithmic.
514 //! <b>Throws</b>: If "comp" throws.
515 //!
516 //! <b>Notes</b>: This function is used to improve performance when constructing
517 //! a node is expensive and the user does not want to have two equivalent nodes
518 //! in the tree: if there is an equivalent value
519 //! the constructed object must be discarded. Many times, the part of the
520 //! node that is used to impose the order is much cheaper to construct
521 //! than the node and this function offers the possibility to use that part
522 //! to check if the insertion will be successful.
524 //! If the check is successful, the user can construct the node and use
525 //! "insert_commit" to insert the node in constant-time. This gives a total
526 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
528 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
529 //! if no more objects are inserted or erased from the set.
530 template<class KeyType, class KeyNodePtrCompare>
531 static std::pair<node_ptr, bool> insert_unique_check
532 (const_node_ptr header, const KeyType &key
533 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
535 std::size_t depth;
536 std::pair<node_ptr, bool> ret =
537 tree_algorithms::insert_unique_check(header, key, comp, commit_data, &depth);
538 commit_data.depth = depth;
539 return ret;
542 //! <b>Requires</b>: "header" must be the header node of a tree.
543 //! KeyNodePtrCompare is a function object that induces a strict weak
544 //! ordering compatible with the strict weak ordering used to create the
545 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
546 //! "hint" is node from the "header"'s tree.
547 //!
548 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
549 //! tree according to "comp" using "hint" as a hint to where it should be
550 //! inserted and obtains the needed information to realize
551 //! a constant-time node insertion if there is no equivalent node.
552 //! If "hint" is the upper_bound the function has constant time
553 //! complexity (two comparisons in the worst case).
555 //! <b>Returns</b>: If there is an equivalent value
556 //! returns a pair containing a node_ptr to the already present node
557 //! and false. If there is not equivalent key can be inserted returns true
558 //! in the returned pair's boolean and fills "commit_data" that is meant to
559 //! be used with the "insert_commit" function to achieve a constant-time
560 //! insertion function.
561 //!
562 //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
563 //! amortized constant time if new_node should be inserted immediately before "hint".
565 //! <b>Throws</b>: If "comp" throws.
566 //!
567 //! <b>Notes</b>: This function is used to improve performance when constructing
568 //! a node is expensive and the user does not want to have two equivalent nodes
569 //! in the tree: if there is an equivalent value
570 //! the constructed object must be discarded. Many times, the part of the
571 //! node that is used to impose the order is much cheaper to construct
572 //! than the node and this function offers the possibility to use that part
573 //! to check if the insertion will be successful.
575 //! If the check is successful, the user can construct the node and use
576 //! "insert_commit" to insert the node in constant-time. This gives a total
577 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
579 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
580 //! if no more objects are inserted or erased from the set.
581 template<class KeyType, class KeyNodePtrCompare>
582 static std::pair<node_ptr, bool> insert_unique_check
583 (const_node_ptr header, node_ptr hint, const KeyType &key
584 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
586 std::size_t depth;
587 std::pair<node_ptr, bool> ret =
588 tree_algorithms::insert_unique_check
589 (header, hint, key, comp, commit_data, &depth);
590 commit_data.depth = depth;
591 return ret;
594 //! <b>Requires</b>: "header" must be the header node of a tree.
595 //! "commit_data" must have been obtained from a previous call to
596 //! "insert_unique_check". No objects should have been inserted or erased
597 //! from the set between the "insert_unique_check" that filled "commit_data"
598 //! and the call to "insert_commit".
599 //!
600 //!
601 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
602 //! from the "commit_data" that a previous "insert_check" filled.
604 //! <b>Complexity</b>: Constant time.
606 //! <b>Throws</b>: Nothing.
607 //!
608 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
609 //! previously executed to fill "commit_data". No value should be inserted or
610 //! erased between the "insert_check" and "insert_commit" calls.
611 template<class H_Alpha>
612 static void insert_unique_commit
613 (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data
614 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
616 tree_algorithms::insert_unique_commit(header, new_value, commit_data);
617 rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size);
620 //! <b>Requires</b>: header must be the header of a tree.
621 //!
622 //! <b>Effects</b>: Rebalances the tree.
623 //!
624 //! <b>Throws</b>: Nothing.
625 //!
626 //! <b>Complexity</b>: Linear.
627 static void rebalance(node_ptr header)
628 { tree_algorithms::rebalance(header); }
630 //! <b>Requires</b>: old_root is a node of a tree.
631 //!
632 //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
634 //! <b>Returns</b>: The new root of the subtree.
636 //! <b>Throws</b>: Nothing.
637 //!
638 //! <b>Complexity</b>: Linear.
639 static node_ptr rebalance_subtree(node_ptr old_root)
640 { return tree_algorithms::rebalance_subtree(old_root); }
642 //! <b>Requires</b>: "n" must be a node inserted in a tree.
644 //! <b>Effects</b>: Returns a pointer to the header node of the tree.
646 //! <b>Complexity</b>: Logarithmic.
647 //!
648 //! <b>Throws</b>: Nothing.
649 static node_ptr get_header(node_ptr n)
650 { return tree_algorithms::get_header(n); }
652 /// @cond
653 private:
655 //! <b>Requires</b>: p is a node of a tree.
656 //!
657 //! <b>Effects</b>: Returns true if p is the header of the tree.
658 //!
659 //! <b>Complexity</b>: Constant.
660 //!
661 //! <b>Throws</b>: Nothing.
662 static bool is_header(const_node_ptr p)
663 { return tree_algorithms::is_header(p); }
665 template<class H_Alpha>
666 static void rebalance_after_insertion
667 ( node_ptr x, std::size_t depth
668 , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
670 if(tree_size > max_tree_size)
671 max_tree_size = tree_size;
673 if(tree_size != 1 && depth > h_alpha(tree_size)){
674 //Find the first non height-balanced node
675 //as described in the section 4.2 of the paper.
676 //This method is the alternative method described
677 //in the paper. Authors claim that this method
678 //may tend to yield more balanced trees on the average
679 //than the weight balanced method.
680 node_ptr s = x;
681 std::size_t size = 1;
683 for(std::size_t i = 1; true; ++i){
684 bool rebalance = false;
685 if(i == depth){
686 BOOST_INTRUSIVE_INVARIANT_ASSERT(tree_size == count(s));
687 rebalance = true;
689 else if(i > h_alpha(size)){
690 node_ptr s_parent = NodeTraits::get_parent(s);
691 node_ptr s_parent_left = NodeTraits::get_left(s_parent);
692 size += 1 + tree_algorithms::count
693 ( s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left );
694 s = s_parent;
695 rebalance = true;
697 if(rebalance){
698 rebalance_subtree(s);
699 break;
705 /// @endcond
708 } //namespace intrusive
709 } //namespace boost
711 #include <boost/intrusive/detail/config_end.hpp>
713 #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP