treap_algorithms.hpp 27 KB

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