treap_algorithms.hpp 28 KB

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