annotate DEPENDENCIES/generic/include/boost/intrusive/sgtree_algorithms.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 /////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2007-2013
Chris@16 4 //
Chris@16 5 // Distributed under the Boost Software License, Version 1.0.
Chris@16 6 // (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 //
Chris@16 9 // See http://www.boost.org/libs/intrusive for documentation.
Chris@16 10 //
Chris@16 11 /////////////////////////////////////////////////////////////////////////////
Chris@16 12 //
Chris@16 13 // Scapegoat tree algorithms are taken from the paper titled:
Chris@16 14 // "Scapegoat Trees" by Igal Galperin Ronald L. Rivest.
Chris@16 15 //
Chris@16 16 /////////////////////////////////////////////////////////////////////////////
Chris@16 17 #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
Chris@16 18 #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
Chris@16 19
Chris@16 20 #include <boost/intrusive/detail/config_begin.hpp>
Chris@16 21
Chris@16 22 #include <cstddef>
Chris@16 23 #include <boost/intrusive/intrusive_fwd.hpp>
Chris@16 24 #include <boost/intrusive/detail/assert.hpp>
Chris@16 25 #include <boost/intrusive/detail/utilities.hpp>
Chris@16 26 #include <boost/intrusive/bstree_algorithms.hpp>
Chris@16 27 #include <boost/intrusive/pointer_traits.hpp>
Chris@16 28
Chris@16 29
Chris@16 30 namespace boost {
Chris@16 31 namespace intrusive {
Chris@16 32
Chris@16 33 //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
Chris@16 34 //! information about the node to be manipulated. NodeTraits must support the
Chris@16 35 //! following interface:
Chris@16 36 //!
Chris@16 37 //! <b>Typedefs</b>:
Chris@16 38 //!
Chris@16 39 //! <tt>node</tt>: The type of the node that forms the binary search tree
Chris@16 40 //!
Chris@16 41 //! <tt>node_ptr</tt>: A pointer to a node
Chris@16 42 //!
Chris@16 43 //! <tt>const_node_ptr</tt>: A pointer to a const node
Chris@16 44 //!
Chris@16 45 //! <b>Static functions</b>:
Chris@16 46 //!
Chris@16 47 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
Chris@16 48 //!
Chris@16 49 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
Chris@16 50 //!
Chris@16 51 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
Chris@16 52 //!
Chris@16 53 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
Chris@16 54 //!
Chris@16 55 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
Chris@16 56 //!
Chris@16 57 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
Chris@16 58 template<class NodeTraits>
Chris@16 59 class sgtree_algorithms
Chris@16 60 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 61 : public bstree_algorithms<NodeTraits>
Chris@16 62 #endif
Chris@16 63 {
Chris@16 64 public:
Chris@16 65 typedef typename NodeTraits::node node;
Chris@16 66 typedef NodeTraits node_traits;
Chris@16 67 typedef typename NodeTraits::node_ptr node_ptr;
Chris@16 68 typedef typename NodeTraits::const_node_ptr const_node_ptr;
Chris@16 69
Chris@16 70 /// @cond
Chris@16 71 private:
Chris@16 72
Chris@16 73 typedef bstree_algorithms<NodeTraits> bstree_algo;
Chris@16 74
Chris@16 75 /// @endcond
Chris@16 76
Chris@16 77 public:
Chris@16 78 //! This type is the information that will be
Chris@16 79 //! filled by insert_unique_check
Chris@16 80 struct insert_commit_data
Chris@16 81 : bstree_algo::insert_commit_data
Chris@16 82 {
Chris@16 83 std::size_t depth;
Chris@16 84 };
Chris@16 85
Chris@16 86 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 87 //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
Chris@16 88 static node_ptr get_header(const const_node_ptr & n);
Chris@16 89
Chris@16 90 //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
Chris@16 91 static node_ptr begin_node(const const_node_ptr & header);
Chris@16 92
Chris@16 93 //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
Chris@16 94 static node_ptr end_node(const const_node_ptr & header);
Chris@16 95
Chris@16 96 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
Chris@16 97 static void swap_tree(const node_ptr & header1, const node_ptr & header2);
Chris@16 98
Chris@16 99 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
Chris@16 100 static void swap_nodes(const node_ptr & node1, const node_ptr & node2);
Chris@16 101
Chris@16 102 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 103 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2);
Chris@16 104
Chris@16 105 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
Chris@16 106 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node);
Chris@16 107
Chris@16 108 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 109 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node);
Chris@16 110
Chris@16 111 //Unlink is not possible since tree metadata is needed to update the tree
Chris@16 112 //!static void unlink(const node_ptr & node);
Chris@16 113
Chris@16 114 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
Chris@16 115 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
Chris@16 116
Chris@16 117 //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
Chris@16 118 static bool unique(const const_node_ptr & node);
Chris@16 119
Chris@16 120 //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
Chris@16 121 static std::size_t size(const const_node_ptr & header);
Chris@16 122
Chris@16 123 //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
Chris@16 124 static node_ptr next_node(const node_ptr & node);
Chris@16 125
Chris@16 126 //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
Chris@16 127 static node_ptr prev_node(const node_ptr & node);
Chris@16 128
Chris@16 129 //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
Chris@16 130 static void init(const node_ptr & node);
Chris@16 131
Chris@16 132 //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
Chris@16 133 static void init_header(const node_ptr & header);
Chris@16 134 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 135
Chris@16 136 //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
Chris@16 137 template<class AlphaByMaxSize>
Chris@16 138 static node_ptr erase(const node_ptr & header, const node_ptr & z, std::size_t tree_size, std::size_t &max_tree_size, AlphaByMaxSize alpha_by_maxsize)
Chris@16 139 {
Chris@16 140 //typename bstree_algo::data_for_rebalance info;
Chris@16 141 bstree_algo::erase(header, z);
Chris@16 142 --tree_size;
Chris@16 143 if (tree_size > 0 &&
Chris@16 144 tree_size < alpha_by_maxsize(max_tree_size)){
Chris@16 145 bstree_algo::rebalance(header);
Chris@16 146 max_tree_size = tree_size;
Chris@16 147 }
Chris@16 148 return z;
Chris@16 149 }
Chris@16 150
Chris@16 151 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 152 //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
Chris@16 153 template <class Cloner, class Disposer>
Chris@16 154 static void clone
Chris@16 155 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
Chris@16 156
Chris@16 157 //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
Chris@16 158 template<class Disposer>
Chris@16 159 static void clear_and_dispose(const node_ptr & header, Disposer disposer);
Chris@16 160
Chris@16 161 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 162 template<class KeyType, class KeyNodePtrCompare>
Chris@16 163 static node_ptr lower_bound
Chris@16 164 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 165
Chris@16 166 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 167 template<class KeyType, class KeyNodePtrCompare>
Chris@16 168 static node_ptr upper_bound
Chris@16 169 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 170
Chris@16 171 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
Chris@16 172 template<class KeyType, class KeyNodePtrCompare>
Chris@16 173 static node_ptr find
Chris@16 174 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 175
Chris@16 176 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 177 template<class KeyType, class KeyNodePtrCompare>
Chris@16 178 static std::pair<node_ptr, node_ptr> equal_range
Chris@16 179 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 180
Chris@16 181 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
Chris@16 182 template<class KeyType, class KeyNodePtrCompare>
Chris@16 183 static std::pair<node_ptr, node_ptr> bounded_range
Chris@16 184 (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
Chris@16 185 , bool left_closed, bool right_closed);
Chris@16 186
Chris@16 187 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 188 template<class KeyType, class KeyNodePtrCompare>
Chris@16 189 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 190 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 191
Chris@16 192 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
Chris@16 193 template<class NodePtrCompare, class H_Alpha>
Chris@16 194 static node_ptr insert_equal_upper_bound
Chris@16 195 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
Chris@16 196 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 197 {
Chris@16 198 std::size_t depth;
Chris@16 199 bstree_algo::insert_equal_upper_bound(h, new_node, comp, &depth);
Chris@16 200 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
Chris@16 201 return new_node;
Chris@16 202 }
Chris@16 203
Chris@16 204 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
Chris@16 205 template<class NodePtrCompare, class H_Alpha>
Chris@16 206 static node_ptr insert_equal_lower_bound
Chris@16 207 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
Chris@16 208 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 209 {
Chris@16 210 std::size_t depth;
Chris@16 211 bstree_algo::insert_equal_lower_bound(h, new_node, comp, &depth);
Chris@16 212 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
Chris@16 213 return new_node;
Chris@16 214 }
Chris@16 215
Chris@16 216 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
Chris@16 217 template<class NodePtrCompare, class H_Alpha>
Chris@16 218 static node_ptr insert_equal
Chris@16 219 (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
Chris@16 220 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 221 {
Chris@16 222 std::size_t depth;
Chris@16 223 bstree_algo::insert_equal(header, hint, new_node, comp, &depth);
Chris@16 224 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
Chris@16 225 return new_node;
Chris@16 226 }
Chris@16 227
Chris@16 228 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 229 template<class H_Alpha>
Chris@16 230 static node_ptr insert_before
Chris@16 231 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node
Chris@16 232 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 233 {
Chris@16 234 std::size_t depth;
Chris@16 235 bstree_algo::insert_before(header, pos, new_node, &depth);
Chris@16 236 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
Chris@16 237 return new_node;
Chris@16 238 }
Chris@16 239
Chris@16 240 //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
Chris@16 241 template<class H_Alpha>
Chris@16 242 static void push_back(const node_ptr & header, const node_ptr & new_node
Chris@16 243 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 244 {
Chris@16 245 std::size_t depth;
Chris@16 246 bstree_algo::push_back(header, new_node, &depth);
Chris@16 247 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
Chris@16 248 }
Chris@16 249
Chris@16 250 //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
Chris@16 251 template<class H_Alpha>
Chris@16 252 static void push_front(const node_ptr & header, const node_ptr & new_node
Chris@16 253 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 254 {
Chris@16 255 std::size_t depth;
Chris@16 256 bstree_algo::push_front(header, new_node, &depth);
Chris@16 257 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
Chris@16 258 }
Chris@16 259
Chris@16 260 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
Chris@16 261 template<class KeyType, class KeyNodePtrCompare>
Chris@16 262 static std::pair<node_ptr, bool> insert_unique_check
Chris@16 263 (const const_node_ptr & header, const KeyType &key
Chris@16 264 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
Chris@16 265 {
Chris@16 266 std::size_t depth;
Chris@16 267 std::pair<node_ptr, bool> ret =
Chris@16 268 bstree_algo::insert_unique_check(header, key, comp, commit_data, &depth);
Chris@16 269 commit_data.depth = depth;
Chris@16 270 return ret;
Chris@16 271 }
Chris@16 272
Chris@16 273 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
Chris@16 274 template<class KeyType, class KeyNodePtrCompare>
Chris@16 275 static std::pair<node_ptr, bool> insert_unique_check
Chris@16 276 (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
Chris@16 277 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
Chris@16 278 {
Chris@16 279 std::size_t depth;
Chris@16 280 std::pair<node_ptr, bool> ret =
Chris@16 281 bstree_algo::insert_unique_check
Chris@16 282 (header, hint, key, comp, commit_data, &depth);
Chris@16 283 commit_data.depth = depth;
Chris@16 284 return ret;
Chris@16 285 }
Chris@16 286
Chris@16 287 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&)
Chris@16 288 template<class H_Alpha>
Chris@16 289 static void insert_unique_commit
Chris@16 290 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data
Chris@16 291 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 292 {
Chris@16 293 bstree_algo::insert_unique_commit(header, new_value, commit_data);
Chris@16 294 rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size);
Chris@16 295 }
Chris@16 296
Chris@16 297 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 298 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
Chris@16 299 static bool is_header(const const_node_ptr & p);
Chris@16 300
Chris@16 301 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
Chris@16 302 static void rebalance(const node_ptr & header);
Chris@16 303
Chris@16 304 //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
Chris@16 305 static node_ptr rebalance_subtree(const node_ptr & old_root)
Chris@16 306 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 307
Chris@16 308 /// @cond
Chris@16 309 private:
Chris@16 310
Chris@16 311 template<class H_Alpha>
Chris@16 312 static void rebalance_after_insertion
Chris@16 313 (const node_ptr &x, std::size_t depth
Chris@16 314 , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
Chris@16 315 {
Chris@16 316 if(tree_size > max_tree_size)
Chris@16 317 max_tree_size = tree_size;
Chris@16 318
Chris@16 319 if(tree_size > 2 && //Nothing to do with only the root
Chris@16 320 //Check if the root node is unbalanced
Chris@16 321 //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1,
Chris@16 322 //but since "depth" is the depth of the ancestor of x, i == depth
Chris@16 323 depth > h_alpha(tree_size)){
Chris@16 324
Chris@16 325 //Find the first non height-balanced node
Chris@16 326 //as described in the section 4.2 of the paper.
Chris@16 327 //This method is the alternative method described
Chris@16 328 //in the paper. Authors claim that this method
Chris@16 329 //may tend to yield more balanced trees on the average
Chris@16 330 //than the weight balanced method.
Chris@16 331 node_ptr s = x;
Chris@16 332 std::size_t size = 1;
Chris@16 333 for(std::size_t ancestor = 1; true; ++ancestor){
Chris@16 334 if(ancestor == depth){ //Check if whole tree must be rebuilt
Chris@16 335 max_tree_size = tree_size;
Chris@16 336 bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
Chris@16 337 break;
Chris@16 338 }
Chris@16 339 else{ //Go to the next scapegoat candidate
Chris@16 340 const node_ptr s_parent = NodeTraits::get_parent(s);
Chris@16 341 const node_ptr s_parent_left = NodeTraits::get_left(s_parent);
Chris@16 342 //Obtain parent's size (previous size + parent + sibling tree)
Chris@16 343 const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
Chris@16 344 size += 1 + bstree_algo::subtree_size(s_sibling);
Chris@16 345 s = s_parent;
Chris@16 346 if(ancestor > h_alpha(size)){ //is 's' scapegoat?
Chris@16 347 bstree_algo::rebalance_subtree(s);
Chris@16 348 break;
Chris@16 349 }
Chris@16 350 }
Chris@16 351 }
Chris@16 352 }
Chris@16 353 }
Chris@16 354 /// @endcond
Chris@16 355 };
Chris@16 356
Chris@16 357 /// @cond
Chris@16 358
Chris@16 359 template<class NodeTraits>
Chris@16 360 struct get_algo<SgTreeAlgorithms, NodeTraits>
Chris@16 361 {
Chris@16 362 typedef sgtree_algorithms<NodeTraits> type;
Chris@16 363 };
Chris@16 364
Chris@16 365 /// @endcond
Chris@16 366
Chris@16 367 } //namespace intrusive
Chris@16 368 } //namespace boost
Chris@16 369
Chris@16 370 #include <boost/intrusive/detail/config_end.hpp>
Chris@16 371
Chris@16 372 #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP