treap_algorithms.hpp 29 KB

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