comparison DEPENDENCIES/generic/include/boost/intrusive/sgtree_algorithms.hpp @ 101:c530137014c0

Update Boost headers (1.58.0)
author Chris Cannam
date Mon, 07 Sep 2015 11:12:49 +0100
parents 2665513ce2d3
children
comparison
equal deleted inserted replaced
100:793467b5e61c 101:c530137014c0
1 ///////////////////////////////////////////////////////////////////////////// 1 /////////////////////////////////////////////////////////////////////////////
2 // 2 //
3 // (C) Copyright Ion Gaztanaga 2007-2013 3 // (C) Copyright Ion Gaztanaga 2007-2014
4 // 4 //
5 // Distributed under the Boost Software License, Version 1.0. 5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at 6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt) 7 // http://www.boost.org/LICENSE_1_0.txt)
8 // 8 //
16 ///////////////////////////////////////////////////////////////////////////// 16 /////////////////////////////////////////////////////////////////////////////
17 #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP 17 #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
18 #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP 18 #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
19 19
20 #include <boost/intrusive/detail/config_begin.hpp> 20 #include <boost/intrusive/detail/config_begin.hpp>
21 #include <boost/intrusive/intrusive_fwd.hpp>
21 22
22 #include <cstddef> 23 #include <cstddef>
23 #include <boost/intrusive/intrusive_fwd.hpp> 24 #include <boost/intrusive/detail/algo_type.hpp>
24 #include <boost/intrusive/detail/assert.hpp>
25 #include <boost/intrusive/detail/utilities.hpp>
26 #include <boost/intrusive/bstree_algorithms.hpp> 25 #include <boost/intrusive/bstree_algorithms.hpp>
27 #include <boost/intrusive/pointer_traits.hpp> 26
28 27 #if defined(BOOST_HAS_PRAGMA_ONCE)
28 # pragma once
29 #endif
29 30
30 namespace boost { 31 namespace boost {
31 namespace intrusive { 32 namespace intrusive {
32 33
33 //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the 34 //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
185 , bool left_closed, bool right_closed); 186 , bool left_closed, bool right_closed);
186 187
187 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare) 188 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
188 template<class KeyType, class KeyNodePtrCompare> 189 template<class KeyType, class KeyNodePtrCompare>
189 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp); 190 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
191
190 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED 192 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
191 193
192 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare) 194 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
193 template<class NodePtrCompare, class H_Alpha> 195 template<class NodePtrCompare, class H_Alpha>
194 static node_ptr insert_equal_upper_bound 196 static node_ptr insert_equal_upper_bound
314 , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size) 316 , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
315 { 317 {
316 if(tree_size > max_tree_size) 318 if(tree_size > max_tree_size)
317 max_tree_size = tree_size; 319 max_tree_size = tree_size;
318 320
319 if(tree_size > 2 && //Nothing to do with only the root 321 if(tree_size > 2 && //Nothing to do with only the root
320 //Check if the root node is unbalanced 322 //Check if the root node is unbalanced
321 //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1, 323 //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1,
322 //but since "depth" is the depth of the ancestor of x, i == depth 324 //but since "depth" is the depth of the ancestor of x, i == depth
323 depth > h_alpha(tree_size)){ 325 depth > h_alpha(tree_size)){
324 326
325 //Find the first non height-balanced node 327 //Find the first non height-balanced node
326 //as described in the section 4.2 of the paper. 328 //as described in the section 4.2 of the paper.
327 //This method is the alternative method described 329 //This method is the alternative method described
328 //in the paper. Authors claim that this method 330 //in the paper. Authors claim that this method
329 //may tend to yield more balanced trees on the average 331 //may tend to yield more balanced trees on the average
330 //than the weight balanced method. 332 //than the weight balanced method.
331 node_ptr s = x; 333 node_ptr s = x;
332 std::size_t size = 1; 334 std::size_t size = 1;
333 for(std::size_t ancestor = 1; true; ++ancestor){ 335 for(std::size_t ancestor = 1; ancestor != depth; ++ancestor){
334 if(ancestor == depth){ //Check if whole tree must be rebuilt 336 const node_ptr s_parent = NodeTraits::get_parent(s);
335 max_tree_size = tree_size; 337 const node_ptr s_parent_left = NodeTraits::get_left(s_parent);
336 bstree_algo::rebalance_subtree(NodeTraits::get_parent(s)); 338 //Obtain parent's size (previous size + parent + sibling tree)
337 break; 339 const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
338 } 340 size += 1 + bstree_algo::subtree_size(s_sibling);
339 else{ //Go to the next scapegoat candidate 341 s = s_parent;
340 const node_ptr s_parent = NodeTraits::get_parent(s); 342 if(ancestor > h_alpha(size)){ //is 's' scapegoat?
341 const node_ptr s_parent_left = NodeTraits::get_left(s_parent); 343 bstree_algo::rebalance_subtree(s);
342 //Obtain parent's size (previous size + parent + sibling tree) 344 return;
343 const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
344 size += 1 + bstree_algo::subtree_size(s_sibling);
345 s = s_parent;
346 if(ancestor > h_alpha(size)){ //is 's' scapegoat?
347 bstree_algo::rebalance_subtree(s);
348 break;
349 }
350 } 345 }
351 } 346 }
347 //The whole tree must be rebuilt
348 max_tree_size = tree_size;
349 bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
352 } 350 }
353 } 351 }
354 /// @endcond 352 /// @endcond
355 }; 353 };
356 354
360 struct get_algo<SgTreeAlgorithms, NodeTraits> 358 struct get_algo<SgTreeAlgorithms, NodeTraits>
361 { 359 {
362 typedef sgtree_algorithms<NodeTraits> type; 360 typedef sgtree_algorithms<NodeTraits> type;
363 }; 361 };
364 362
363 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
364 struct get_node_checker<SgTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
365 {
366 typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
367 };
368
365 /// @endcond 369 /// @endcond
366 370
367 } //namespace intrusive 371 } //namespace intrusive
368 } //namespace boost 372 } //namespace boost
369 373