annotate DEPENDENCIES/generic/include/boost/intrusive/splaytree_algorithms.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 /////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@101 3 // (C) Copyright Ion Gaztanaga 2007-2014
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 // The implementation of splay trees is based on the article and code published
Chris@16 13 // in C++ Users Journal "Implementing Splay Trees in C++" (September 1, 2005).
Chris@16 14 //
Chris@16 15 // The splay code has been modified and (supposedly) improved by Ion Gaztanaga.
Chris@16 16 //
Chris@16 17 // Here is the copyright notice of the original file containing the splay code:
Chris@16 18 //
Chris@16 19 // splay_tree.h -- implementation of a STL compatible splay tree.
Chris@16 20 //
Chris@16 21 // Copyright (c) 2004 Ralf Mattethat
Chris@16 22 //
Chris@16 23 // Permission to copy, use, modify, sell and distribute this software
Chris@16 24 // is granted provided this copyright notice appears in all copies.
Chris@16 25 // This software is provided "as is" without express or implied
Chris@16 26 // warranty, and with no claim as to its suitability for any purpose.
Chris@16 27 //
Chris@16 28 /////////////////////////////////////////////////////////////////////////////
Chris@16 29
Chris@16 30 #ifndef BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
Chris@16 31 #define BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
Chris@16 32
Chris@16 33 #include <boost/intrusive/detail/config_begin.hpp>
Chris@101 34 #include <boost/intrusive/intrusive_fwd.hpp>
Chris@16 35 #include <boost/intrusive/detail/assert.hpp>
Chris@101 36 #include <boost/intrusive/detail/algo_type.hpp>
Chris@101 37 #include <boost/intrusive/detail/uncast.hpp>
Chris@101 38 #include <boost/intrusive/bstree_algorithms.hpp>
Chris@101 39
Chris@16 40 #include <cstddef>
Chris@101 41
Chris@101 42 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@101 43 # pragma once
Chris@101 44 #endif
Chris@16 45
Chris@16 46 namespace boost {
Chris@16 47 namespace intrusive {
Chris@16 48
Chris@16 49 /// @cond
Chris@16 50 namespace detail {
Chris@16 51
Chris@16 52 template<class NodeTraits>
Chris@101 53 struct splaydown_assemble_and_fix_header
Chris@16 54 {
Chris@16 55 typedef typename NodeTraits::node_ptr node_ptr;
Chris@101 56
Chris@101 57 splaydown_assemble_and_fix_header(const node_ptr & t, const node_ptr & header, const node_ptr &leftmost, const node_ptr &rightmost)
Chris@101 58 : t_(t)
Chris@101 59 , null_node_(header)
Chris@101 60 , l_(null_node_)
Chris@101 61 , r_(null_node_)
Chris@101 62 , leftmost_(leftmost)
Chris@101 63 , rightmost_(rightmost)
Chris@16 64 {}
Chris@16 65
Chris@101 66 ~splaydown_assemble_and_fix_header()
Chris@101 67 {
Chris@101 68 this->assemble();
Chris@16 69
Chris@101 70 //Now recover the original header except for the
Chris@101 71 //splayed root node.
Chris@101 72 //"t_" is the current root and "null_node_" is the header node
Chris@101 73 NodeTraits::set_parent(null_node_, t_);
Chris@101 74 NodeTraits::set_parent(t_, null_node_);
Chris@101 75 //Recover leftmost/rightmost pointers
Chris@101 76 NodeTraits::set_left (null_node_, leftmost_);
Chris@101 77 NodeTraits::set_right(null_node_, rightmost_);
Chris@101 78 }
Chris@101 79
Chris@101 80 private:
Chris@101 81
Chris@101 82 void assemble()
Chris@16 83 {
Chris@101 84 //procedure assemble;
Chris@101 85 // left(r), right(l) := right(t), left(t);
Chris@101 86 // left(t), right(t) := right(null), left(null);
Chris@101 87 //end assemble;
Chris@101 88 { // left(r), right(l) := right(t), left(t);
Chris@101 89
Chris@101 90 node_ptr const old_t_left = NodeTraits::get_left(t_);
Chris@101 91 node_ptr const old_t_right = NodeTraits::get_right(t_);
Chris@101 92 NodeTraits::set_right(l_, old_t_left);
Chris@101 93 NodeTraits::set_left (r_, old_t_right);
Chris@101 94 if(old_t_left){
Chris@101 95 NodeTraits::set_parent(old_t_left, l_);
Chris@101 96 }
Chris@101 97 if(old_t_right){
Chris@101 98 NodeTraits::set_parent(old_t_right, r_);
Chris@101 99 }
Chris@101 100 }
Chris@101 101 { // left(t), right(t) := right(null), left(null);
Chris@101 102 node_ptr const null_right = NodeTraits::get_right(null_node_);
Chris@101 103 node_ptr const null_left = NodeTraits::get_left(null_node_);
Chris@101 104 NodeTraits::set_left (t_, null_right);
Chris@101 105 NodeTraits::set_right(t_, null_left);
Chris@101 106 if(null_right){
Chris@101 107 NodeTraits::set_parent(null_right, t_);
Chris@101 108 }
Chris@101 109 if(null_left){
Chris@101 110 NodeTraits::set_parent(null_left, t_);
Chris@101 111 }
Chris@16 112 }
Chris@16 113 }
Chris@101 114
Chris@101 115 public:
Chris@101 116 node_ptr t_, null_node_, l_, r_, leftmost_, rightmost_;
Chris@16 117 };
Chris@16 118
Chris@16 119 } //namespace detail {
Chris@16 120 /// @endcond
Chris@16 121
Chris@16 122 //! A splay tree is an implementation of a binary search tree. The tree is
Chris@16 123 //! self balancing using the splay algorithm as described in
Chris@16 124 //!
Chris@16 125 //! "Self-Adjusting Binary Search Trees
Chris@16 126 //! by Daniel Dominic Sleator and Robert Endre Tarjan
Chris@16 127 //! AT&T Bell Laboratories, Murray Hill, NJ
Chris@16 128 //! Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686
Chris@16 129 //!
Chris@16 130 //! splaytree_algorithms is configured with a NodeTraits class, which encapsulates the
Chris@16 131 //! information about the node to be manipulated. NodeTraits must support the
Chris@16 132 //! following interface:
Chris@16 133 //!
Chris@16 134 //! <b>Typedefs</b>:
Chris@16 135 //!
Chris@16 136 //! <tt>node</tt>: The type of the node that forms the binary search tree
Chris@16 137 //!
Chris@16 138 //! <tt>node_ptr</tt>: A pointer to a node
Chris@16 139 //!
Chris@16 140 //! <tt>const_node_ptr</tt>: A pointer to a const node
Chris@16 141 //!
Chris@16 142 //! <b>Static functions</b>:
Chris@16 143 //!
Chris@16 144 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
Chris@16 145 //!
Chris@16 146 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
Chris@16 147 //!
Chris@16 148 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
Chris@16 149 //!
Chris@16 150 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
Chris@16 151 //!
Chris@16 152 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
Chris@16 153 //!
Chris@16 154 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
Chris@16 155 template<class NodeTraits>
Chris@16 156 class splaytree_algorithms
Chris@16 157 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 158 : public bstree_algorithms<NodeTraits>
Chris@16 159 #endif
Chris@16 160 {
Chris@16 161 /// @cond
Chris@16 162 private:
Chris@16 163 typedef bstree_algorithms<NodeTraits> bstree_algo;
Chris@16 164 /// @endcond
Chris@16 165
Chris@16 166 public:
Chris@16 167 typedef typename NodeTraits::node node;
Chris@16 168 typedef NodeTraits node_traits;
Chris@16 169 typedef typename NodeTraits::node_ptr node_ptr;
Chris@16 170 typedef typename NodeTraits::const_node_ptr const_node_ptr;
Chris@16 171
Chris@16 172 //! This type is the information that will be
Chris@16 173 //! filled by insert_unique_check
Chris@16 174 typedef typename bstree_algo::insert_commit_data insert_commit_data;
Chris@16 175
Chris@16 176 public:
Chris@16 177 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 178 //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
Chris@16 179 static node_ptr get_header(const const_node_ptr & n);
Chris@16 180
Chris@16 181 //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
Chris@16 182 static node_ptr begin_node(const const_node_ptr & header);
Chris@16 183
Chris@16 184 //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
Chris@16 185 static node_ptr end_node(const const_node_ptr & header);
Chris@16 186
Chris@16 187 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
Chris@16 188 static void swap_tree(const node_ptr & header1, const node_ptr & header2);
Chris@16 189
Chris@16 190 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
Chris@16 191 static void swap_nodes(const node_ptr & node1, const node_ptr & node2);
Chris@16 192
Chris@16 193 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 194 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2);
Chris@16 195
Chris@16 196 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
Chris@16 197 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node);
Chris@16 198
Chris@16 199 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 200 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node);
Chris@16 201
Chris@16 202 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&)
Chris@16 203 static void unlink(const node_ptr & node);
Chris@16 204
Chris@16 205 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
Chris@16 206 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
Chris@16 207
Chris@16 208 //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
Chris@16 209 static bool unique(const const_node_ptr & node);
Chris@16 210
Chris@16 211 //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
Chris@16 212 static std::size_t size(const const_node_ptr & header);
Chris@16 213
Chris@16 214 //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
Chris@16 215 static node_ptr next_node(const node_ptr & node);
Chris@16 216
Chris@16 217 //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
Chris@16 218 static node_ptr prev_node(const node_ptr & node);
Chris@16 219
Chris@16 220 //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
Chris@16 221 static void init(const node_ptr & node);
Chris@16 222
Chris@16 223 //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
Chris@16 224 static void init_header(const node_ptr & header);
Chris@101 225
Chris@16 226 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 227
Chris@16 228 //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
Chris@101 229 //! Additional notes: the previous node of z is splayed to speed up range deletions.
Chris@101 230 static void erase(const node_ptr & header, const node_ptr & z)
Chris@16 231 {
Chris@16 232 //posibility 1
Chris@101 233 if(NodeTraits::get_left(z)){
Chris@16 234 splay_up(bstree_algo::prev_node(z), header);
Chris@16 235 }
Chris@101 236
Chris@16 237 //possibility 2
Chris@101 238 //if(NodeTraits::get_left(z)){
Chris@101 239 // node_ptr l = NodeTraits::get_left(z);
Chris@101 240 // splay_up(l, header);
Chris@101 241 //}
Chris@101 242
Chris@101 243 //if(NodeTraits::get_left(z)){
Chris@101 244 // node_ptr l = bstree_algo::prev_node(z);
Chris@101 245 // splay_up_impl(l, z);
Chris@101 246 //}
Chris@101 247
Chris@16 248 //possibility 4
Chris@101 249 //splay_up(z, header);
Chris@16 250
Chris@16 251 bstree_algo::erase(header, z);
Chris@16 252 }
Chris@16 253
Chris@16 254 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 255 //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
Chris@16 256 template <class Cloner, class Disposer>
Chris@16 257 static void clone
Chris@16 258 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
Chris@16 259
Chris@16 260 //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
Chris@16 261 template<class Disposer>
Chris@16 262 static void clear_and_dispose(const node_ptr & header, Disposer disposer);
Chris@16 263
Chris@16 264 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 265 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@101 266 //! Additional notes: an element with key `key` is splayed.
Chris@16 267 template<class KeyType, class KeyNodePtrCompare>
Chris@16 268 static std::size_t count
Chris@16 269 (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 270 {
Chris@16 271 std::pair<node_ptr, node_ptr> ret = equal_range(header, key, comp);
Chris@16 272 std::size_t n = 0;
Chris@16 273 while(ret.first != ret.second){
Chris@16 274 ++n;
Chris@16 275 ret.first = next_node(ret.first);
Chris@16 276 }
Chris@16 277 return n;
Chris@16 278 }
Chris@16 279
Chris@16 280 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 281 //! Additional note: no splaying is performed
Chris@16 282 template<class KeyType, class KeyNodePtrCompare>
Chris@16 283 static std::size_t count
Chris@16 284 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 285 { return bstree_algo::count(header, key, comp); }
Chris@16 286
Chris@16 287 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@101 288 //! Additional notes: the first node of the range is splayed.
Chris@16 289 template<class KeyType, class KeyNodePtrCompare>
Chris@16 290 static node_ptr lower_bound
Chris@101 291 (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 292 {
Chris@101 293 splay_down(detail::uncast(header), key, comp);
Chris@16 294 node_ptr y = bstree_algo::lower_bound(header, key, comp);
Chris@101 295 //splay_up(y, detail::uncast(header));
Chris@16 296 return y;
Chris@16 297 }
Chris@16 298
Chris@16 299 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 300 //! Additional note: no splaying is performed
Chris@16 301 template<class KeyType, class KeyNodePtrCompare>
Chris@16 302 static node_ptr lower_bound
Chris@16 303 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 304 { return bstree_algo::lower_bound(header, key, comp); }
Chris@16 305
Chris@16 306 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@101 307 //! Additional notes: the first node of the range is splayed.
Chris@16 308 template<class KeyType, class KeyNodePtrCompare>
Chris@16 309 static node_ptr upper_bound
Chris@101 310 (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 311 {
Chris@101 312 splay_down(detail::uncast(header), key, comp);
Chris@16 313 node_ptr y = bstree_algo::upper_bound(header, key, comp);
Chris@101 314 //splay_up(y, detail::uncast(header));
Chris@16 315 return y;
Chris@16 316 }
Chris@16 317
Chris@16 318 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 319 //! Additional note: no splaying is performed
Chris@16 320 template<class KeyType, class KeyNodePtrCompare>
Chris@16 321 static node_ptr upper_bound
Chris@16 322 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 323 { return bstree_algo::upper_bound(header, key, comp); }
Chris@16 324
Chris@16 325 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
Chris@101 326 //! Additional notes: the found node of the lower bound is splayed.
Chris@16 327 template<class KeyType, class KeyNodePtrCompare>
Chris@16 328 static node_ptr find
Chris@101 329 (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 330 {
Chris@101 331 splay_down(detail::uncast(header), key, comp);
Chris@101 332 return bstree_algo::find(header, key, comp);
Chris@16 333 }
Chris@16 334
Chris@16 335 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
Chris@16 336 //! Additional note: no splaying is performed
Chris@16 337 template<class KeyType, class KeyNodePtrCompare>
Chris@16 338 static node_ptr find
Chris@16 339 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 340 { return bstree_algo::find(header, key, comp); }
Chris@16 341
Chris@16 342 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@101 343 //! Additional notes: the first node of the range is splayed.
Chris@16 344 template<class KeyType, class KeyNodePtrCompare>
Chris@16 345 static std::pair<node_ptr, node_ptr> equal_range
Chris@101 346 (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 347 {
Chris@101 348 splay_down(detail::uncast(header), key, comp);
Chris@16 349 std::pair<node_ptr, node_ptr> ret = bstree_algo::equal_range(header, key, comp);
Chris@101 350 //splay_up(ret.first, detail::uncast(header));
Chris@16 351 return ret;
Chris@16 352 }
Chris@16 353
Chris@16 354 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 355 //! Additional note: no splaying is performed
Chris@16 356 template<class KeyType, class KeyNodePtrCompare>
Chris@16 357 static std::pair<node_ptr, node_ptr> equal_range
Chris@16 358 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@16 359 { return bstree_algo::equal_range(header, key, comp); }
Chris@16 360
Chris@101 361 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@101 362 //! Additional notes: the first node of the range is splayed.
Chris@101 363 template<class KeyType, class KeyNodePtrCompare>
Chris@101 364 static std::pair<node_ptr, node_ptr> lower_bound_range
Chris@101 365 (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@101 366 {
Chris@101 367 splay_down(detail::uncast(header), key, comp);
Chris@101 368 std::pair<node_ptr, node_ptr> ret = bstree_algo::lower_bound_range(header, key, comp);
Chris@101 369 //splay_up(ret.first, detail::uncast(header));
Chris@101 370 return ret;
Chris@101 371 }
Chris@101 372
Chris@101 373 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@101 374 //! Additional note: no splaying is performed
Chris@101 375 template<class KeyType, class KeyNodePtrCompare>
Chris@101 376 static std::pair<node_ptr, node_ptr> lower_bound_range
Chris@101 377 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
Chris@101 378 { return bstree_algo::lower_bound_range(header, key, comp); }
Chris@101 379
Chris@16 380 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
Chris@101 381 //! Additional notes: the first node of the range is splayed.
Chris@16 382 template<class KeyType, class KeyNodePtrCompare>
Chris@16 383 static std::pair<node_ptr, node_ptr> bounded_range
Chris@16 384 (const node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
Chris@101 385 , bool left_closed, bool right_closed)
Chris@16 386 {
Chris@101 387 splay_down(detail::uncast(header), lower_key, comp);
Chris@16 388 std::pair<node_ptr, node_ptr> ret =
Chris@16 389 bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed);
Chris@101 390 //splay_up(ret.first, detail::uncast(header));
Chris@16 391 return ret;
Chris@16 392 }
Chris@16 393
Chris@16 394 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
Chris@16 395 //! Additional note: no splaying is performed
Chris@16 396 template<class KeyType, class KeyNodePtrCompare>
Chris@16 397 static std::pair<node_ptr, node_ptr> bounded_range
Chris@16 398 (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
Chris@16 399 , bool left_closed, bool right_closed)
Chris@16 400 { return bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed); }
Chris@16 401
Chris@16 402 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
Chris@16 403 //! Additional note: the inserted node is splayed
Chris@16 404 template<class NodePtrCompare>
Chris@16 405 static node_ptr insert_equal_upper_bound
Chris@16 406 (const node_ptr & header, const node_ptr & new_node, NodePtrCompare comp)
Chris@16 407 {
Chris@16 408 splay_down(header, new_node, comp);
Chris@16 409 return bstree_algo::insert_equal_upper_bound(header, new_node, comp);
Chris@16 410 }
Chris@16 411
Chris@16 412 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
Chris@16 413 //! Additional note: the inserted node is splayed
Chris@16 414 template<class NodePtrCompare>
Chris@16 415 static node_ptr insert_equal_lower_bound
Chris@16 416 (const node_ptr & header, const node_ptr & new_node, NodePtrCompare comp)
Chris@16 417 {
Chris@16 418 splay_down(header, new_node, comp);
Chris@16 419 return bstree_algo::insert_equal_lower_bound(header, new_node, comp);
Chris@16 420 }
Chris@16 421
Chris@16 422 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
Chris@16 423 //! Additional note: the inserted node is splayed
Chris@16 424 template<class NodePtrCompare>
Chris@16 425 static node_ptr insert_equal
Chris@16 426 (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp)
Chris@16 427 {
Chris@16 428 splay_down(header, new_node, comp);
Chris@16 429 return bstree_algo::insert_equal(header, hint, new_node, comp);
Chris@16 430 }
Chris@16 431
Chris@16 432 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 433 //! Additional note: the inserted node is splayed
Chris@16 434 static node_ptr insert_before
Chris@16 435 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node)
Chris@16 436 {
Chris@16 437 bstree_algo::insert_before(header, pos, new_node);
Chris@16 438 splay_up(new_node, header);
Chris@16 439 return new_node;
Chris@16 440 }
Chris@16 441
Chris@16 442 //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
Chris@16 443 //! Additional note: the inserted node is splayed
Chris@16 444 static void push_back(const node_ptr & header, const node_ptr & new_node)
Chris@16 445 {
Chris@16 446 bstree_algo::push_back(header, new_node);
Chris@16 447 splay_up(new_node, header);
Chris@16 448 }
Chris@16 449
Chris@16 450 //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
Chris@16 451 //! Additional note: the inserted node is splayed
Chris@16 452 static void push_front(const node_ptr & header, const node_ptr & new_node)
Chris@16 453 {
Chris@16 454 bstree_algo::push_front(header, new_node);
Chris@16 455 splay_up(new_node, header);
Chris@16 456 }
Chris@16 457
Chris@16 458 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
Chris@16 459 //! Additional note: nodes with the given key are splayed
Chris@16 460 template<class KeyType, class KeyNodePtrCompare>
Chris@16 461 static std::pair<node_ptr, bool> insert_unique_check
Chris@16 462 (const node_ptr & header, const KeyType &key
Chris@16 463 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
Chris@16 464 {
Chris@16 465 splay_down(header, key, comp);
Chris@16 466 return bstree_algo::insert_unique_check(header, key, comp, commit_data);
Chris@16 467 }
Chris@16 468
Chris@16 469 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
Chris@16 470 //! Additional note: nodes with the given key are splayed
Chris@16 471 template<class KeyType, class KeyNodePtrCompare>
Chris@16 472 static std::pair<node_ptr, bool> insert_unique_check
Chris@16 473 (const node_ptr & header, const node_ptr &hint, const KeyType &key
Chris@16 474 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
Chris@16 475 {
Chris@16 476 splay_down(header, key, comp);
Chris@16 477 return bstree_algo::insert_unique_check(header, hint, key, comp, commit_data);
Chris@16 478 }
Chris@16 479
Chris@16 480 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 481 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&)
Chris@16 482 static void insert_unique_commit
Chris@16 483 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data);
Chris@16 484
Chris@16 485 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
Chris@16 486 static bool is_header(const const_node_ptr & p);
Chris@16 487
Chris@16 488 //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance
Chris@16 489 static void rebalance(const node_ptr & header);
Chris@16 490
Chris@16 491 //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
Chris@16 492 static node_ptr rebalance_subtree(const node_ptr & old_root);
Chris@16 493
Chris@16 494 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 495
Chris@16 496 // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow
Chris@16 497 static void splay_up(const node_ptr & node, const node_ptr & header)
Chris@101 498 { priv_splay_up<true>(node, header); }
Chris@101 499
Chris@101 500 // top-down splay | complexity : logarithmic | exception : strong, note A
Chris@101 501 template<class KeyType, class KeyNodePtrCompare>
Chris@101 502 static node_ptr splay_down(const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool *pfound = 0)
Chris@101 503 { return priv_splay_down<true>(header, key, comp, pfound); }
Chris@101 504
Chris@101 505 private:
Chris@101 506
Chris@101 507 /// @cond
Chris@101 508
Chris@101 509 // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow
Chris@101 510 template<bool SimpleSplay>
Chris@101 511 static void priv_splay_up(const node_ptr & node, const node_ptr & header)
Chris@16 512 {
Chris@16 513 // If (node == header) do a splay for the right most node instead
Chris@16 514 // this is to boost performance of equal_range/count on equivalent containers in the case
Chris@16 515 // where there are many equal elements at the end
Chris@16 516 node_ptr n((node == header) ? NodeTraits::get_right(header) : node);
Chris@16 517 node_ptr t(header);
Chris@16 518
Chris@16 519 if( n == t ) return;
Chris@16 520
Chris@16 521 for( ;; ){
Chris@16 522 node_ptr p(NodeTraits::get_parent(n));
Chris@16 523 node_ptr g(NodeTraits::get_parent(p));
Chris@16 524
Chris@16 525 if( p == t ) break;
Chris@16 526
Chris@16 527 if( g == t ){
Chris@16 528 // zig
Chris@16 529 rotate(n);
Chris@16 530 }
Chris@16 531 else if ((NodeTraits::get_left(p) == n && NodeTraits::get_left(g) == p) ||
Chris@16 532 (NodeTraits::get_right(p) == n && NodeTraits::get_right(g) == p) ){
Chris@16 533 // zig-zig
Chris@16 534 rotate(p);
Chris@16 535 rotate(n);
Chris@16 536 }
Chris@101 537 else {
Chris@16 538 // zig-zag
Chris@16 539 rotate(n);
Chris@101 540 if(!SimpleSplay){
Chris@101 541 rotate(n);
Chris@101 542 }
Chris@16 543 }
Chris@16 544 }
Chris@16 545 }
Chris@16 546
Chris@101 547 template<bool SimpleSplay, class KeyType, class KeyNodePtrCompare>
Chris@101 548 static node_ptr priv_splay_down(const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool *pfound = 0)
Chris@16 549 {
Chris@16 550 //Most splay tree implementations use a dummy/null node to implement.
Chris@16 551 //this function. This has some problems for a generic library like Intrusive:
Chris@16 552 //
Chris@16 553 // * The node might not have a default constructor.
Chris@16 554 // * The default constructor could throw.
Chris@16 555 //
Chris@16 556 //We already have a header node. Leftmost and rightmost nodes of the tree
Chris@16 557 //are not changed when splaying (because the invariants of the tree don't
Chris@16 558 //change) We can back up them, use the header as the null node and
Chris@16 559 //reassign old values after the function has been completed.
Chris@101 560 node_ptr const old_root = NodeTraits::get_parent(header);
Chris@101 561 node_ptr const leftmost = NodeTraits::get_left(header);
Chris@101 562 node_ptr const rightmost = NodeTraits::get_right(header);
Chris@101 563 if(leftmost == rightmost){ //Empty or unique node
Chris@101 564 if(pfound){
Chris@101 565 *pfound = old_root && !comp(key, old_root) && !comp(old_root, key);
Chris@101 566 }
Chris@101 567 return old_root ? old_root : header;
Chris@101 568 }
Chris@101 569 else{
Chris@101 570 //Initialize "null node" (the header in our case)
Chris@101 571 NodeTraits::set_left (header, node_ptr());
Chris@101 572 NodeTraits::set_right(header, node_ptr());
Chris@101 573 //Class that will backup leftmost/rightmost from header, commit the assemble(),
Chris@101 574 //and will restore leftmost/rightmost to header even if "comp" throws
Chris@101 575 detail::splaydown_assemble_and_fix_header<NodeTraits> commit(old_root, header, leftmost, rightmost);
Chris@101 576 bool found = false;
Chris@16 577
Chris@16 578 for( ;; ){
Chris@101 579 if(comp(key, commit.t_)){
Chris@101 580 node_ptr const t_left = NodeTraits::get_left(commit.t_);
Chris@101 581 if(!t_left)
Chris@16 582 break;
Chris@101 583 if(comp(key, t_left)){
Chris@101 584 bstree_algo::rotate_right_no_parent_fix(commit.t_, t_left);
Chris@101 585 commit.t_ = t_left;
Chris@101 586 if( !NodeTraits::get_left(commit.t_) )
Chris@16 587 break;
Chris@101 588 link_right(commit.t_, commit.r_);
Chris@16 589 }
Chris@16 590 else{
Chris@101 591 link_right(commit.t_, commit.r_);
Chris@101 592 if(!SimpleSplay && comp(t_left, key)){
Chris@101 593 if( !NodeTraits::get_right(commit.t_) )
Chris@101 594 break;
Chris@101 595 link_left(commit.t_, commit.l_);
Chris@101 596 }
Chris@16 597 }
Chris@16 598 }
Chris@101 599 else if(comp(commit.t_, key)){
Chris@101 600 node_ptr const t_right = NodeTraits::get_right(commit.t_);
Chris@101 601 if(!t_right)
Chris@16 602 break;
Chris@16 603
Chris@101 604 if(comp(t_right, key)){
Chris@101 605 bstree_algo::rotate_left_no_parent_fix(commit.t_, t_right);
Chris@101 606 commit.t_ = t_right;
Chris@101 607 if( !NodeTraits::get_right(commit.t_) )
Chris@16 608 break;
Chris@101 609 link_left(commit.t_, commit.l_);
Chris@16 610 }
Chris@16 611 else{
Chris@101 612 link_left(commit.t_, commit.l_);
Chris@101 613 if(!SimpleSplay && comp(key, t_right)){
Chris@101 614 if( !NodeTraits::get_left(commit.t_) )
Chris@101 615 break;
Chris@101 616 link_right(commit.t_, commit.r_);
Chris@101 617 }
Chris@16 618 }
Chris@16 619 }
Chris@16 620 else{
Chris@101 621 found = true;
Chris@16 622 break;
Chris@16 623 }
Chris@16 624 }
Chris@16 625
Chris@101 626 //commit.~splaydown_assemble_and_fix_header<NodeTraits>() will first
Chris@101 627 //"assemble()" + link the new root & recover header's leftmost & rightmost
Chris@101 628 if(pfound){
Chris@101 629 *pfound = found;
Chris@101 630 }
Chris@101 631 return commit.t_;
Chris@16 632 }
Chris@16 633 }
Chris@16 634
Chris@16 635 // break link to left child node and attach it to left tree pointed to by l | complexity : constant | exception : nothrow
Chris@16 636 static void link_left(node_ptr & t, node_ptr & l)
Chris@16 637 {
Chris@101 638 //procedure link_left;
Chris@101 639 // t, l, right(l) := right(t), t, t
Chris@101 640 //end link_left
Chris@16 641 NodeTraits::set_right(l, t);
Chris@16 642 NodeTraits::set_parent(t, l);
Chris@16 643 l = t;
Chris@16 644 t = NodeTraits::get_right(t);
Chris@16 645 }
Chris@16 646
Chris@16 647 // break link to right child node and attach it to right tree pointed to by r | complexity : constant | exception : nothrow
Chris@16 648 static void link_right(node_ptr & t, node_ptr & r)
Chris@16 649 {
Chris@101 650 //procedure link_right;
Chris@101 651 // t, r, left(r) := left(t), t, t
Chris@101 652 //end link_right;
Chris@16 653 NodeTraits::set_left(r, t);
Chris@16 654 NodeTraits::set_parent(t, r);
Chris@16 655 r = t;
Chris@16 656 t = NodeTraits::get_left(t);
Chris@16 657 }
Chris@16 658
Chris@16 659 // rotate n with its parent | complexity : constant | exception : nothrow
Chris@16 660 static void rotate(const node_ptr & n)
Chris@16 661 {
Chris@101 662 //procedure rotate_left;
Chris@101 663 // t, right(t), left(right(t)) := right(t), left(right(t)), t
Chris@101 664 //end rotate_left;
Chris@16 665 node_ptr p = NodeTraits::get_parent(n);
Chris@16 666 node_ptr g = NodeTraits::get_parent(p);
Chris@16 667 //Test if g is header before breaking tree
Chris@16 668 //invariants that would make is_header invalid
Chris@16 669 bool g_is_header = bstree_algo::is_header(g);
Chris@16 670
Chris@16 671 if(NodeTraits::get_left(p) == n){
Chris@16 672 NodeTraits::set_left(p, NodeTraits::get_right(n));
Chris@101 673 if(NodeTraits::get_left(p))
Chris@16 674 NodeTraits::set_parent(NodeTraits::get_left(p), p);
Chris@16 675 NodeTraits::set_right(n, p);
Chris@16 676 }
Chris@16 677 else{ // must be ( p->right == n )
Chris@16 678 NodeTraits::set_right(p, NodeTraits::get_left(n));
Chris@101 679 if(NodeTraits::get_right(p))
Chris@16 680 NodeTraits::set_parent(NodeTraits::get_right(p), p);
Chris@16 681 NodeTraits::set_left(n, p);
Chris@16 682 }
Chris@16 683
Chris@16 684 NodeTraits::set_parent(p, n);
Chris@16 685 NodeTraits::set_parent(n, g);
Chris@16 686
Chris@16 687 if(g_is_header){
Chris@16 688 if(NodeTraits::get_parent(g) == p)
Chris@16 689 NodeTraits::set_parent(g, n);
Chris@16 690 else{//must be ( g->right == p )
Chris@16 691 BOOST_INTRUSIVE_INVARIANT_ASSERT(false);
Chris@16 692 NodeTraits::set_right(g, n);
Chris@16 693 }
Chris@16 694 }
Chris@16 695 else{
Chris@16 696 if(NodeTraits::get_left(g) == p)
Chris@16 697 NodeTraits::set_left(g, n);
Chris@16 698 else //must be ( g->right == p )
Chris@16 699 NodeTraits::set_right(g, n);
Chris@16 700 }
Chris@16 701 }
Chris@16 702
Chris@16 703 /// @endcond
Chris@16 704 };
Chris@16 705
Chris@16 706 /// @cond
Chris@16 707
Chris@16 708 template<class NodeTraits>
Chris@16 709 struct get_algo<SplayTreeAlgorithms, NodeTraits>
Chris@16 710 {
Chris@16 711 typedef splaytree_algorithms<NodeTraits> type;
Chris@16 712 };
Chris@16 713
Chris@101 714 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
Chris@101 715 struct get_node_checker<SplayTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
Chris@101 716 {
Chris@101 717 typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
Chris@101 718 };
Chris@101 719
Chris@16 720 /// @endcond
Chris@16 721
Chris@16 722 } //namespace intrusive
Chris@16 723 } //namespace boost
Chris@16 724
Chris@16 725 #include <boost/intrusive/detail/config_end.hpp>
Chris@16 726
Chris@16 727 #endif //BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP