annotate DEPENDENCIES/generic/include/boost/intrusive/treap_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 2006-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
Chris@16 13 #ifndef BOOST_INTRUSIVE_TREAP_ALGORITHMS_HPP
Chris@16 14 #define BOOST_INTRUSIVE_TREAP_ALGORITHMS_HPP
Chris@16 15
Chris@16 16 #include <boost/intrusive/detail/config_begin.hpp>
Chris@101 17 #include <boost/intrusive/intrusive_fwd.hpp>
Chris@16 18
Chris@16 19 #include <cstddef>
Chris@16 20
Chris@16 21 #include <boost/intrusive/detail/assert.hpp>
Chris@101 22 #include <boost/intrusive/detail/algo_type.hpp>
Chris@16 23 #include <boost/intrusive/bstree_algorithms.hpp>
Chris@16 24
Chris@101 25 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@101 26 # pragma once
Chris@101 27 #endif
Chris@16 28
Chris@16 29 namespace boost {
Chris@16 30 namespace intrusive {
Chris@16 31
Chris@101 32 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@101 33
Chris@101 34 namespace detail
Chris@101 35 {
Chris@101 36
Chris@101 37 template<class ValueTraits, class NodePtrPrioCompare, class ExtraChecker>
Chris@101 38 struct treap_node_extra_checker
Chris@101 39 : public ExtraChecker
Chris@101 40 {
Chris@101 41 typedef ExtraChecker base_checker_t;
Chris@101 42 typedef ValueTraits value_traits;
Chris@101 43 typedef typename value_traits::node_traits node_traits;
Chris@101 44 typedef typename node_traits::const_node_ptr const_node_ptr;
Chris@101 45
Chris@101 46 typedef typename base_checker_t::return_type return_type;
Chris@101 47
Chris@101 48 treap_node_extra_checker(const NodePtrPrioCompare& prio_comp, ExtraChecker extra_checker)
Chris@101 49 : base_checker_t(extra_checker), prio_comp_(prio_comp)
Chris@101 50 {}
Chris@101 51
Chris@101 52 void operator () (const const_node_ptr& p,
Chris@101 53 const return_type& check_return_left, const return_type& check_return_right,
Chris@101 54 return_type& check_return)
Chris@101 55 {
Chris@101 56 if (node_traits::get_left(p))
Chris@101 57 BOOST_INTRUSIVE_INVARIANT_ASSERT(!prio_comp_(node_traits::get_left(p), p));
Chris@101 58 if (node_traits::get_right(p))
Chris@101 59 BOOST_INTRUSIVE_INVARIANT_ASSERT(!prio_comp_(node_traits::get_right(p), p));
Chris@101 60 base_checker_t::operator()(p, check_return_left, check_return_right, check_return);
Chris@101 61 }
Chris@101 62
Chris@101 63 const NodePtrPrioCompare prio_comp_;
Chris@101 64 };
Chris@101 65
Chris@101 66 } // namespace detail
Chris@101 67
Chris@101 68 #endif //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@101 69
Chris@16 70 //! treap_algorithms provides basic algorithms to manipulate
Chris@16 71 //! nodes forming a treap.
Chris@16 72 //!
Chris@16 73 //! (1) the header node is maintained with links not only to the root
Chris@16 74 //! but also to the leftmost node of the tree, to enable constant time
Chris@16 75 //! begin(), and to the rightmost node of the tree, to enable linear time
Chris@16 76 //! performance when used with the generic set algorithms (set_union,
Chris@16 77 //! etc.);
Chris@16 78 //!
Chris@16 79 //! (2) when a node being deleted has two children its successor node is
Chris@16 80 //! relinked into its place, rather than copied, so that the only
Chris@16 81 //! pointers invalidated are those referring to the deleted node.
Chris@16 82 //!
Chris@16 83 //! treap_algorithms is configured with a NodeTraits class, which encapsulates the
Chris@16 84 //! information about the node to be manipulated. NodeTraits must support the
Chris@16 85 //! following interface:
Chris@16 86 //!
Chris@16 87 //! <b>Typedefs</b>:
Chris@16 88 //!
Chris@16 89 //! <tt>node</tt>: The type of the node that forms the treap
Chris@16 90 //!
Chris@16 91 //! <tt>node_ptr</tt>: A pointer to a node
Chris@16 92 //!
Chris@16 93 //! <tt>const_node_ptr</tt>: A pointer to a const node
Chris@16 94 //!
Chris@16 95 //! <b>Static functions</b>:
Chris@16 96 //!
Chris@16 97 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
Chris@16 98 //!
Chris@16 99 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
Chris@16 100 //!
Chris@16 101 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
Chris@16 102 //!
Chris@16 103 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
Chris@16 104 //!
Chris@16 105 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
Chris@16 106 //!
Chris@16 107 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
Chris@16 108 template<class NodeTraits>
Chris@16 109 class treap_algorithms
Chris@16 110 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 111 : public bstree_algorithms<NodeTraits>
Chris@16 112 #endif
Chris@16 113 {
Chris@16 114 public:
Chris@16 115 typedef NodeTraits node_traits;
Chris@16 116 typedef typename NodeTraits::node node;
Chris@16 117 typedef typename NodeTraits::node_ptr node_ptr;
Chris@16 118 typedef typename NodeTraits::const_node_ptr const_node_ptr;
Chris@16 119
Chris@16 120 /// @cond
Chris@16 121 private:
Chris@16 122
Chris@16 123 typedef bstree_algorithms<NodeTraits> bstree_algo;
Chris@16 124
Chris@16 125 class rerotate_on_destroy
Chris@16 126 {
Chris@16 127 rerotate_on_destroy& operator=(const rerotate_on_destroy&);
Chris@16 128
Chris@16 129 public:
Chris@16 130 rerotate_on_destroy(const node_ptr & header, const node_ptr & p, std::size_t &n)
Chris@16 131 : header_(header), p_(p), n_(n), remove_it_(true)
Chris@16 132 {}
Chris@16 133
Chris@16 134 ~rerotate_on_destroy()
Chris@16 135 {
Chris@16 136 if(remove_it_){
Chris@16 137 rotate_up_n(header_, p_, n_);
Chris@16 138 }
Chris@16 139 }
Chris@16 140
Chris@16 141 void release()
Chris@16 142 { remove_it_ = false; }
Chris@16 143
Chris@16 144 const node_ptr header_;
Chris@16 145 const node_ptr p_;
Chris@16 146 std::size_t &n_;
Chris@16 147 bool remove_it_;
Chris@16 148 };
Chris@16 149
Chris@16 150 static void rotate_up_n(const node_ptr header, const node_ptr p, std::size_t n)
Chris@16 151 {
Chris@101 152 node_ptr p_parent(NodeTraits::get_parent(p));
Chris@101 153 node_ptr p_grandparent(NodeTraits::get_parent(p_parent));
Chris@101 154 while(n--){
Chris@101 155 if(p == NodeTraits::get_left(p_parent)){ //p is left child
Chris@101 156 bstree_algo::rotate_right(p_parent, p, p_grandparent, header);
Chris@16 157 }
Chris@101 158 else{ //p is right child
Chris@101 159 bstree_algo::rotate_left(p_parent, p, p_grandparent, header);
Chris@16 160 }
Chris@101 161 p_parent = p_grandparent;
Chris@101 162 p_grandparent = NodeTraits::get_parent(p_parent);
Chris@16 163 }
Chris@16 164 }
Chris@16 165
Chris@16 166 /// @endcond
Chris@16 167
Chris@16 168 public:
Chris@16 169 //! This type is the information that will be
Chris@16 170 //! filled by insert_unique_check
Chris@16 171 struct insert_commit_data
Chris@16 172 /// @cond
Chris@16 173 : public bstree_algo::insert_commit_data
Chris@16 174 /// @endcond
Chris@16 175 {
Chris@16 176 /// @cond
Chris@16 177 std::size_t rotations;
Chris@16 178 /// @endcond
Chris@16 179 };
Chris@16 180
Chris@16 181 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 182
Chris@16 183 //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
Chris@16 184 static node_ptr get_header(const const_node_ptr & n);
Chris@16 185
Chris@16 186 //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
Chris@16 187 static node_ptr begin_node(const const_node_ptr & header);
Chris@16 188
Chris@16 189 //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
Chris@16 190 static node_ptr end_node(const const_node_ptr & header);
Chris@16 191
Chris@16 192 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
Chris@16 193 static void swap_tree(const node_ptr & header1, const node_ptr & header2);
Chris@16 194
Chris@16 195 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
Chris@16 196 static void swap_nodes(const node_ptr & node1, const node_ptr & node2);
Chris@16 197
Chris@16 198 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 199 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2);
Chris@16 200
Chris@16 201 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
Chris@16 202 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node);
Chris@16 203
Chris@16 204 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
Chris@16 205 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node);
Chris@16 206 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 207
Chris@16 208 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&)
Chris@16 209 template<class NodePtrPriorityCompare>
Chris@16 210 static void unlink(const node_ptr & node, NodePtrPriorityCompare pcomp)
Chris@16 211 {
Chris@16 212 node_ptr x = NodeTraits::get_parent(node);
Chris@16 213 if(x){
Chris@16 214 while(!bstree_algo::is_header(x))
Chris@16 215 x = NodeTraits::get_parent(x);
Chris@16 216 erase(x, node, pcomp);
Chris@16 217 }
Chris@16 218 }
Chris@16 219
Chris@16 220 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 221 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
Chris@16 222 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
Chris@16 223
Chris@16 224 //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
Chris@16 225 static bool unique(const const_node_ptr & node);
Chris@16 226
Chris@16 227 //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
Chris@16 228 static std::size_t size(const const_node_ptr & header);
Chris@16 229
Chris@16 230 //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
Chris@16 231 static node_ptr next_node(const node_ptr & node);
Chris@16 232
Chris@16 233 //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
Chris@16 234 static node_ptr prev_node(const node_ptr & node);
Chris@16 235
Chris@16 236 //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
Chris@16 237 static void init(const node_ptr & node);
Chris@16 238
Chris@16 239 //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
Chris@16 240 static void init_header(const node_ptr & header);
Chris@16 241 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 242
Chris@16 243 //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
Chris@16 244 template<class NodePtrPriorityCompare>
Chris@16 245 static node_ptr erase(const node_ptr & header, const node_ptr & z, NodePtrPriorityCompare pcomp)
Chris@16 246 {
Chris@16 247 rebalance_for_erasure(header, z, pcomp);
Chris@16 248 bstree_algo::erase(header, z);
Chris@16 249 return z;
Chris@16 250 }
Chris@16 251
Chris@16 252 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 253 //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
Chris@16 254 template <class Cloner, class Disposer>
Chris@16 255 static void clone
Chris@16 256 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
Chris@16 257
Chris@16 258 //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
Chris@16 259 template<class Disposer>
Chris@16 260 static void clear_and_dispose(const node_ptr & header, Disposer disposer);
Chris@16 261
Chris@16 262 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 263 template<class KeyType, class KeyNodePtrCompare>
Chris@16 264 static node_ptr lower_bound
Chris@16 265 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 266
Chris@16 267 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 268 template<class KeyType, class KeyNodePtrCompare>
Chris@16 269 static node_ptr upper_bound
Chris@16 270 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 271
Chris@16 272 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
Chris@16 273 template<class KeyType, class KeyNodePtrCompare>
Chris@16 274 static node_ptr find
Chris@16 275 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 276
Chris@16 277 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 278 template<class KeyType, class KeyNodePtrCompare>
Chris@16 279 static std::pair<node_ptr, node_ptr> equal_range
Chris@16 280 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@16 281
Chris@16 282 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
Chris@16 283 template<class KeyType, class KeyNodePtrCompare>
Chris@16 284 static std::pair<node_ptr, node_ptr> bounded_range
Chris@16 285 (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
Chris@16 286 , bool left_closed, bool right_closed);
Chris@16 287
Chris@16 288 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
Chris@16 289 template<class KeyType, class KeyNodePtrCompare>
Chris@16 290 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
Chris@101 291
Chris@16 292 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 293
Chris@16 294 //! <b>Requires</b>: "h" must be the header node of a tree.
Chris@16 295 //! NodePtrCompare is a function object that induces a strict weak
Chris@16 296 //! ordering compatible with the strict weak ordering used to create the
Chris@16 297 //! the tree. NodePtrCompare compares two node_ptrs.
Chris@16 298 //! NodePtrPriorityCompare is a priority function object that induces a strict weak
Chris@16 299 //! ordering compatible with the one used to create the
Chris@16 300 //! the tree. NodePtrPriorityCompare compares two node_ptrs.
Chris@16 301 //!
Chris@16 302 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
Chris@16 303 //! according to "comp" and rotates the tree according to "pcomp".
Chris@16 304 //!
Chris@16 305 //! <b>Complexity</b>: Average complexity for insert element is at
Chris@16 306 //! most logarithmic.
Chris@16 307 //!
Chris@16 308 //! <b>Throws</b>: If "comp" throw or "pcomp" throw.
Chris@16 309 template<class NodePtrCompare, class NodePtrPriorityCompare>
Chris@16 310 static node_ptr insert_equal_upper_bound
Chris@16 311 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, NodePtrPriorityCompare pcomp)
Chris@16 312 {
Chris@16 313 insert_commit_data commit_data;
Chris@16 314 bstree_algo::insert_equal_upper_bound_check(h, new_node, comp, commit_data);
Chris@16 315 rebalance_check_and_commit(h, new_node, pcomp, commit_data);
Chris@16 316 return new_node;
Chris@16 317 }
Chris@16 318
Chris@16 319 //! <b>Requires</b>: "h" must be the header node of a tree.
Chris@16 320 //! NodePtrCompare is a function object that induces a strict weak
Chris@16 321 //! ordering compatible with the strict weak ordering used to create the
Chris@16 322 //! the tree. NodePtrCompare compares two node_ptrs.
Chris@16 323 //! NodePtrPriorityCompare is a priority function object that induces a strict weak
Chris@16 324 //! ordering compatible with the one used to create the
Chris@16 325 //! the tree. NodePtrPriorityCompare compares two node_ptrs.
Chris@16 326 //!
Chris@16 327 //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
Chris@16 328 //! according to "comp" and rotates the tree according to "pcomp".
Chris@16 329 //!
Chris@16 330 //! <b>Complexity</b>: Average complexity for insert element is at
Chris@16 331 //! most logarithmic.
Chris@16 332 //!
Chris@16 333 //! <b>Throws</b>: If "comp" throws.
Chris@16 334 template<class NodePtrCompare, class NodePtrPriorityCompare>
Chris@16 335 static node_ptr insert_equal_lower_bound
Chris@16 336 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, NodePtrPriorityCompare pcomp)
Chris@16 337 {
Chris@16 338 insert_commit_data commit_data;
Chris@16 339 bstree_algo::insert_equal_lower_bound_check(h, new_node, comp, commit_data);
Chris@16 340 rebalance_check_and_commit(h, new_node, pcomp, commit_data);
Chris@16 341 return new_node;
Chris@16 342 }
Chris@16 343
Chris@16 344 //! <b>Requires</b>: "header" must be the header node of a tree.
Chris@16 345 //! NodePtrCompare is a function object that induces a strict weak
Chris@16 346 //! ordering compatible with the strict weak ordering used to create the
Chris@16 347 //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
Chris@16 348 //! the "header"'s tree.
Chris@16 349 //! NodePtrPriorityCompare is a priority function object that induces a strict weak
Chris@16 350 //! ordering compatible with the one used to create the
Chris@16 351 //! the tree. NodePtrPriorityCompare compares two node_ptrs.
Chris@16 352 //!
Chris@16 353 //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
Chris@16 354 //! where it will be inserted. If "hint" is the upper_bound
Chris@16 355 //! the insertion takes constant time (two comparisons in the worst case).
Chris@16 356 //! Rotates the tree according to "pcomp".
Chris@16 357 //!
Chris@16 358 //! <b>Complexity</b>: Logarithmic in general, but it is amortized
Chris@16 359 //! constant time if new_node is inserted immediately before "hint".
Chris@16 360 //!
Chris@16 361 //! <b>Throws</b>: If "comp" throw or "pcomp" throw.
Chris@16 362 template<class NodePtrCompare, class NodePtrPriorityCompare>
Chris@16 363 static node_ptr insert_equal
Chris@16 364 (const node_ptr & h, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp, NodePtrPriorityCompare pcomp)
Chris@16 365 {
Chris@16 366 insert_commit_data commit_data;
Chris@16 367 bstree_algo::insert_equal_check(h, hint, new_node, comp, commit_data);
Chris@16 368 rebalance_check_and_commit(h, new_node, pcomp, commit_data);
Chris@16 369 return new_node;
Chris@16 370 }
Chris@16 371
Chris@16 372 //! <b>Requires</b>: "header" must be the header node of a tree.
Chris@16 373 //! "pos" must be a valid node of the tree (including header end) node.
Chris@16 374 //! "pos" must be a node pointing to the successor to "new_node"
Chris@16 375 //! once inserted according to the order of already inserted nodes. This function does not
Chris@16 376 //! check "pos" and this precondition must be guaranteed by the caller.
Chris@16 377 //! NodePtrPriorityCompare is a priority function object that induces a strict weak
Chris@16 378 //! ordering compatible with the one used to create the
Chris@16 379 //! the tree. NodePtrPriorityCompare compares two node_ptrs.
Chris@16 380 //!
Chris@16 381 //! <b>Effects</b>: Inserts new_node into the tree before "pos"
Chris@16 382 //! and rotates the tree according to "pcomp".
Chris@16 383 //!
Chris@16 384 //! <b>Complexity</b>: Constant-time.
Chris@16 385 //!
Chris@16 386 //! <b>Throws</b>: If "pcomp" throws, strong guarantee.
Chris@16 387 //!
Chris@16 388 //! <b>Note</b>: If "pos" is not the successor of the newly inserted "new_node"
Chris@16 389 //! tree invariants might be broken.
Chris@16 390 template<class NodePtrPriorityCompare>
Chris@16 391 static node_ptr insert_before
Chris@16 392 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node, NodePtrPriorityCompare pcomp)
Chris@16 393 {
Chris@16 394 insert_commit_data commit_data;
Chris@16 395 bstree_algo::insert_before_check(header, pos, commit_data);
Chris@16 396 rebalance_check_and_commit(header, new_node, pcomp, commit_data);
Chris@16 397 return new_node;
Chris@16 398 }
Chris@16 399
Chris@16 400 //! <b>Requires</b>: "header" must be the header node of a tree.
Chris@16 401 //! "new_node" must be, according to the used ordering no less than the
Chris@16 402 //! greatest inserted key.
Chris@16 403 //! NodePtrPriorityCompare is a priority function object that induces a strict weak
Chris@16 404 //! ordering compatible with the one used to create the
Chris@16 405 //! the tree. NodePtrPriorityCompare compares two node_ptrs.
Chris@16 406 //!
Chris@16 407 //! <b>Effects</b>: Inserts x into the tree in the last position
Chris@16 408 //! and rotates the tree according to "pcomp".
Chris@16 409 //!
Chris@16 410 //! <b>Complexity</b>: Constant-time.
Chris@16 411 //!
Chris@16 412 //! <b>Throws</b>: If "pcomp" throws, strong guarantee.
Chris@16 413 //!
Chris@16 414 //! <b>Note</b>: If "new_node" is less than the greatest inserted key
Chris@16 415 //! tree invariants are broken. This function is slightly faster than
Chris@16 416 //! using "insert_before".
Chris@16 417 template<class NodePtrPriorityCompare>
Chris@16 418 static void push_back(const node_ptr & header, const node_ptr & new_node, NodePtrPriorityCompare pcomp)
Chris@16 419 {
Chris@16 420 insert_commit_data commit_data;
Chris@16 421 bstree_algo::push_back_check(header, commit_data);
Chris@16 422 rebalance_check_and_commit(header, new_node, pcomp, commit_data);
Chris@16 423 }
Chris@16 424
Chris@16 425 //! <b>Requires</b>: "header" must be the header node of a tree.
Chris@16 426 //! "new_node" must be, according to the used ordering, no greater than the
Chris@16 427 //! lowest inserted key.
Chris@16 428 //! NodePtrPriorityCompare is a priority function object that induces a strict weak
Chris@16 429 //! ordering compatible with the one used to create the
Chris@16 430 //! the tree. NodePtrPriorityCompare compares two node_ptrs.
Chris@16 431 //!
Chris@16 432 //! <b>Effects</b>: Inserts x into the tree in the first position
Chris@16 433 //! and rotates the tree according to "pcomp".
Chris@16 434 //!
Chris@16 435 //! <b>Complexity</b>: Constant-time.
Chris@16 436 //!
Chris@16 437 //! <b>Throws</b>: If "pcomp" throws, strong guarantee.
Chris@16 438 //!
Chris@16 439 //! <b>Note</b>: If "new_node" is greater than the lowest inserted key
Chris@16 440 //! tree invariants are broken. This function is slightly faster than
Chris@16 441 //! using "insert_before".
Chris@16 442 template<class NodePtrPriorityCompare>
Chris@16 443 static void push_front(const node_ptr & header, const node_ptr & new_node, NodePtrPriorityCompare pcomp)
Chris@16 444 {
Chris@16 445 insert_commit_data commit_data;
Chris@16 446 bstree_algo::push_front_check(header, commit_data);
Chris@16 447 rebalance_check_and_commit(header, new_node, pcomp, commit_data);
Chris@16 448 }
Chris@16 449
Chris@16 450 //! <b>Requires</b>: "header" must be the header node of a tree.
Chris@16 451 //! KeyNodePtrCompare is a function object that induces a strict weak
Chris@16 452 //! ordering compatible with the strict weak ordering used to create the
Chris@16 453 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
Chris@16 454 //!
Chris@16 455 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
Chris@16 456 //! tree according to "comp" and obtains the needed information to realize
Chris@16 457 //! a constant-time node insertion if there is no equivalent node.
Chris@16 458 //!
Chris@16 459 //! <b>Returns</b>: If there is an equivalent value
Chris@16 460 //! returns a pair containing a node_ptr to the already present node
Chris@16 461 //! and false. If there is not equivalent key can be inserted returns true
Chris@16 462 //! in the returned pair's boolean and fills "commit_data" that is meant to
Chris@16 463 //! be used with the "insert_commit" function to achieve a constant-time
Chris@16 464 //! insertion function.
Chris@16 465 //!
Chris@16 466 //! <b>Complexity</b>: Average complexity is at most logarithmic.
Chris@16 467 //!
Chris@16 468 //! <b>Throws</b>: If "comp" throws.
Chris@16 469 //!
Chris@16 470 //! <b>Notes</b>: This function is used to improve performance when constructing
Chris@16 471 //! a node is expensive and the user does not want to have two equivalent nodes
Chris@16 472 //! in the tree: if there is an equivalent value
Chris@16 473 //! the constructed object must be discarded. Many times, the part of the
Chris@16 474 //! node that is used to impose the order is much cheaper to construct
Chris@16 475 //! than the node and this function offers the possibility to use that part
Chris@16 476 //! to check if the insertion will be successful.
Chris@16 477 //!
Chris@16 478 //! If the check is successful, the user can construct the node and use
Chris@16 479 //! "insert_commit" to insert the node in constant-time. This gives a total
Chris@16 480 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
Chris@16 481 //!
Chris@16 482 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
Chris@16 483 //! if no more objects are inserted or erased from the set.
Chris@16 484 template<class KeyType, class KeyNodePtrCompare, class KeyNodePtrPrioCompare>
Chris@16 485 static std::pair<node_ptr, bool> insert_unique_check
Chris@16 486 (const const_node_ptr & header, const KeyType &key
Chris@16 487 ,KeyNodePtrCompare comp, KeyNodePtrPrioCompare pcomp
Chris@16 488 ,insert_commit_data &commit_data)
Chris@16 489 {
Chris@16 490 std::pair<node_ptr, bool> ret =
Chris@16 491 bstree_algo::insert_unique_check(header, key, comp, commit_data);
Chris@16 492 if(ret.second)
Chris@16 493 rebalance_after_insertion_check(header, commit_data.node, key, pcomp, commit_data.rotations);
Chris@16 494 return ret;
Chris@16 495 }
Chris@16 496
Chris@16 497 //! <b>Requires</b>: "header" must be the header node of a tree.
Chris@16 498 //! KeyNodePtrCompare is a function object that induces a strict weak
Chris@16 499 //! ordering compatible with the strict weak ordering used to create the
Chris@16 500 //! the tree. NodePtrCompare compares KeyType with a node_ptr.
Chris@16 501 //! "hint" is node from the "header"'s tree.
Chris@16 502 //!
Chris@16 503 //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
Chris@16 504 //! tree according to "comp" using "hint" as a hint to where it should be
Chris@16 505 //! inserted and obtains the needed information to realize
Chris@16 506 //! a constant-time node insertion if there is no equivalent node.
Chris@16 507 //! If "hint" is the upper_bound the function has constant time
Chris@16 508 //! complexity (two comparisons in the worst case).
Chris@16 509 //!
Chris@16 510 //! <b>Returns</b>: If there is an equivalent value
Chris@16 511 //! returns a pair containing a node_ptr to the already present node
Chris@16 512 //! and false. If there is not equivalent key can be inserted returns true
Chris@16 513 //! in the returned pair's boolean and fills "commit_data" that is meant to
Chris@16 514 //! be used with the "insert_commit" function to achieve a constant-time
Chris@16 515 //! insertion function.
Chris@16 516 //!
Chris@16 517 //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
Chris@16 518 //! amortized constant time if new_node should be inserted immediately before "hint".
Chris@16 519 //!
Chris@16 520 //! <b>Throws</b>: If "comp" throws.
Chris@16 521 //!
Chris@16 522 //! <b>Notes</b>: This function is used to improve performance when constructing
Chris@16 523 //! a node is expensive and the user does not want to have two equivalent nodes
Chris@16 524 //! in the tree: if there is an equivalent value
Chris@16 525 //! the constructed object must be discarded. Many times, the part of the
Chris@16 526 //! node that is used to impose the order is much cheaper to construct
Chris@16 527 //! than the node and this function offers the possibility to use that part
Chris@16 528 //! to check if the insertion will be successful.
Chris@16 529 //!
Chris@16 530 //! If the check is successful, the user can construct the node and use
Chris@16 531 //! "insert_commit" to insert the node in constant-time. This gives a total
Chris@16 532 //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
Chris@16 533 //!
Chris@16 534 //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
Chris@16 535 //! if no more objects are inserted or erased from the set.
Chris@16 536 template<class KeyType, class KeyNodePtrCompare, class KeyNodePtrPrioCompare>
Chris@16 537 static std::pair<node_ptr, bool> insert_unique_check
Chris@16 538 (const const_node_ptr & header, const node_ptr & hint, const KeyType &key
Chris@16 539 ,KeyNodePtrCompare comp, KeyNodePtrPrioCompare pcomp, insert_commit_data &commit_data)
Chris@16 540 {
Chris@16 541 std::pair<node_ptr, bool> ret =
Chris@16 542 bstree_algo::insert_unique_check(header, hint, key, comp, commit_data);
Chris@16 543 if(ret.second)
Chris@16 544 rebalance_after_insertion_check(header, commit_data.node, key, pcomp, commit_data.rotations);
Chris@16 545 return ret;
Chris@16 546 }
Chris@16 547
Chris@16 548 //! <b>Requires</b>: "header" must be the header node of a tree.
Chris@16 549 //! "commit_data" must have been obtained from a previous call to
Chris@16 550 //! "insert_unique_check". No objects should have been inserted or erased
Chris@16 551 //! from the set between the "insert_unique_check" that filled "commit_data"
Chris@16 552 //! and the call to "insert_commit".
Chris@16 553 //!
Chris@16 554 //!
Chris@16 555 //! <b>Effects</b>: Inserts new_node in the set using the information obtained
Chris@16 556 //! from the "commit_data" that a previous "insert_check" filled.
Chris@16 557 //!
Chris@16 558 //! <b>Complexity</b>: Constant time.
Chris@16 559 //!
Chris@16 560 //! <b>Throws</b>: Nothing.
Chris@16 561 //!
Chris@16 562 //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
Chris@16 563 //! previously executed to fill "commit_data". No value should be inserted or
Chris@16 564 //! erased between the "insert_check" and "insert_commit" calls.
Chris@16 565 static void insert_unique_commit
Chris@16 566 (const node_ptr & header, const node_ptr & new_node, const insert_commit_data &commit_data)
Chris@16 567 {
Chris@16 568 bstree_algo::insert_unique_commit(header, new_node, commit_data);
Chris@101 569 rotate_up_n(header, new_node, commit_data.rotations);
Chris@16 570 }
Chris@16 571
Chris@16 572 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 573
Chris@16 574 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
Chris@16 575 static bool is_header(const const_node_ptr & p);
Chris@16 576 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
Chris@16 577
Chris@16 578 /// @cond
Chris@16 579 private:
Chris@16 580
Chris@16 581 template<class NodePtrPriorityCompare>
Chris@16 582 static void rebalance_for_erasure(const node_ptr & header, const node_ptr & z, NodePtrPriorityCompare pcomp)
Chris@16 583 {
Chris@16 584 std::size_t n = 0;
Chris@16 585 rerotate_on_destroy rb(header, z, n);
Chris@16 586
Chris@16 587 node_ptr z_left = NodeTraits::get_left(z);
Chris@16 588 node_ptr z_right = NodeTraits::get_right(z);
Chris@16 589 while(z_left || z_right){
Chris@101 590 const node_ptr z_parent(NodeTraits::get_parent(z));
Chris@16 591 if(!z_right || (z_left && pcomp(z_left, z_right))){
Chris@101 592 bstree_algo::rotate_right(z, z_left, z_parent, header);
Chris@16 593 }
Chris@16 594 else{
Chris@101 595 bstree_algo::rotate_left(z, z_right, z_parent, header);
Chris@16 596 }
Chris@16 597 ++n;
Chris@16 598 z_left = NodeTraits::get_left(z);
Chris@16 599 z_right = NodeTraits::get_right(z);
Chris@16 600 }
Chris@16 601 rb.release();
Chris@16 602 }
Chris@16 603
Chris@16 604 template<class NodePtrPriorityCompare>
Chris@16 605 static void rebalance_check_and_commit
Chris@16 606 (const node_ptr & h, const node_ptr & new_node, NodePtrPriorityCompare pcomp, insert_commit_data &commit_data)
Chris@16 607 {
Chris@16 608 rebalance_after_insertion_check(h, commit_data.node, new_node, pcomp, commit_data.rotations);
Chris@16 609 //No-throw
Chris@16 610 bstree_algo::insert_unique_commit(h, new_node, commit_data);
Chris@101 611 rotate_up_n(h, new_node, commit_data.rotations);
Chris@16 612 }
Chris@16 613
Chris@16 614 template<class Key, class KeyNodePriorityCompare>
Chris@16 615 static void rebalance_after_insertion_check
Chris@16 616 (const const_node_ptr &header, const const_node_ptr & up, const Key &k
Chris@16 617 , KeyNodePriorityCompare pcomp, std::size_t &num_rotations)
Chris@16 618 {
Chris@16 619 const_node_ptr upnode(up);
Chris@16 620 //First check rotations since pcomp can throw
Chris@16 621 num_rotations = 0;
Chris@16 622 std::size_t n = 0;
Chris@16 623 while(upnode != header && pcomp(k, upnode)){
Chris@16 624 ++n;
Chris@16 625 upnode = NodeTraits::get_parent(upnode);
Chris@16 626 }
Chris@16 627 num_rotations = n;
Chris@16 628 }
Chris@16 629
Chris@16 630 template<class NodePtrPriorityCompare>
Chris@16 631 static bool check_invariant(const const_node_ptr & header, NodePtrPriorityCompare pcomp)
Chris@16 632 {
Chris@16 633 node_ptr beg = begin_node(header);
Chris@16 634 node_ptr end = end_node(header);
Chris@16 635
Chris@16 636 while(beg != end){
Chris@16 637 node_ptr p = NodeTraits::get_parent(beg);
Chris@16 638 if(p != header){
Chris@16 639 if(pcomp(beg, p))
Chris@16 640 return false;
Chris@16 641 }
Chris@16 642 beg = next_node(beg);
Chris@16 643 }
Chris@16 644 return true;
Chris@16 645 }
Chris@16 646
Chris@16 647 /// @endcond
Chris@16 648 };
Chris@16 649
Chris@16 650 /// @cond
Chris@16 651
Chris@16 652 template<class NodeTraits>
Chris@16 653 struct get_algo<TreapAlgorithms, NodeTraits>
Chris@16 654 {
Chris@16 655 typedef treap_algorithms<NodeTraits> type;
Chris@16 656 };
Chris@16 657
Chris@101 658 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
Chris@101 659 struct get_node_checker<TreapAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
Chris@101 660 {
Chris@101 661 typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
Chris@101 662 };
Chris@101 663
Chris@16 664 /// @endcond
Chris@16 665
Chris@16 666 } //namespace intrusive
Chris@16 667 } //namespace boost
Chris@16 668
Chris@16 669 #include <boost/intrusive/detail/config_end.hpp>
Chris@16 670
Chris@16 671 #endif //BOOST_INTRUSIVE_TREAP_ALGORITHMS_HPP