treap_algorithms.hpp 29 KB

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