treap_algorithms.hpp 29 KB

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