splaytree_algorithms.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-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. // The implementation of splay trees is based on the article and code published
  13. // in C++ Users Journal "Implementing Splay Trees in C++" (September 1, 2005).
  14. //
  15. // The splay code has been modified and (supposedly) improved by Ion Gaztanaga.
  16. //
  17. // Here is the copyright notice of the original file containing the splay code:
  18. //
  19. // splay_tree.h -- implementation of a STL compatible splay tree.
  20. //
  21. // Copyright (c) 2004 Ralf Mattethat
  22. //
  23. // Permission to copy, use, modify, sell and distribute this software
  24. // is granted provided this copyright notice appears in all copies.
  25. // This software is provided "as is" without express or implied
  26. // warranty, and with no claim as to its suitability for any purpose.
  27. //
  28. /////////////////////////////////////////////////////////////////////////////
  29. #ifndef BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
  30. #define BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP
  31. #include <boost/intrusive/detail/config_begin.hpp>
  32. #include <boost/intrusive/detail/assert.hpp>
  33. #include <boost/intrusive/intrusive_fwd.hpp>
  34. #include <boost/intrusive/pointer_traits.hpp>
  35. #include <cstddef>
  36. #include <boost/intrusive/detail/utilities.hpp>
  37. #include <boost/intrusive/bstree_algorithms.hpp>
  38. namespace boost {
  39. namespace intrusive {
  40. /// @cond
  41. namespace detail {
  42. template<class NodeTraits>
  43. struct splaydown_rollback
  44. {
  45. typedef typename NodeTraits::node_ptr node_ptr;
  46. splaydown_rollback( const node_ptr *pcur_subtree, const node_ptr & header
  47. , const node_ptr & leftmost , const node_ptr & rightmost)
  48. : pcur_subtree_(pcur_subtree) , header_(header)
  49. , leftmost_(leftmost) , rightmost_(rightmost)
  50. {}
  51. void release()
  52. { pcur_subtree_ = 0; }
  53. ~splaydown_rollback()
  54. {
  55. if(pcur_subtree_){
  56. //Exception can only be thrown by comp, but
  57. //tree invariants still hold. *pcur_subtree is the current root
  58. //so link it to the header.
  59. NodeTraits::set_parent(*pcur_subtree_, header_);
  60. NodeTraits::set_parent(header_, *pcur_subtree_);
  61. //Recover leftmost/rightmost pointers
  62. NodeTraits::set_left (header_, leftmost_);
  63. NodeTraits::set_right(header_, rightmost_);
  64. }
  65. }
  66. const node_ptr *pcur_subtree_;
  67. node_ptr header_, leftmost_, rightmost_;
  68. };
  69. } //namespace detail {
  70. /// @endcond
  71. //! A splay tree is an implementation of a binary search tree. The tree is
  72. //! self balancing using the splay algorithm as described in
  73. //!
  74. //! "Self-Adjusting Binary Search Trees
  75. //! by Daniel Dominic Sleator and Robert Endre Tarjan
  76. //! AT&T Bell Laboratories, Murray Hill, NJ
  77. //! Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686
  78. //!
  79. //! splaytree_algorithms is configured with a NodeTraits class, which encapsulates the
  80. //! information about the node to be manipulated. NodeTraits must support the
  81. //! following interface:
  82. //!
  83. //! <b>Typedefs</b>:
  84. //!
  85. //! <tt>node</tt>: The type of the node that forms the binary search tree
  86. //!
  87. //! <tt>node_ptr</tt>: A pointer to a node
  88. //!
  89. //! <tt>const_node_ptr</tt>: A pointer to a const node
  90. //!
  91. //! <b>Static functions</b>:
  92. //!
  93. //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
  94. //!
  95. //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
  96. //!
  97. //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
  98. //!
  99. //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
  100. //!
  101. //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
  102. //!
  103. //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
  104. template<class NodeTraits>
  105. class splaytree_algorithms
  106. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  107. : public bstree_algorithms<NodeTraits>
  108. #endif
  109. {
  110. /// @cond
  111. private:
  112. typedef bstree_algorithms<NodeTraits> bstree_algo;
  113. /// @endcond
  114. public:
  115. typedef typename NodeTraits::node node;
  116. typedef NodeTraits node_traits;
  117. typedef typename NodeTraits::node_ptr node_ptr;
  118. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  119. //! This type is the information that will be
  120. //! filled by insert_unique_check
  121. typedef typename bstree_algo::insert_commit_data insert_commit_data;
  122. public:
  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. //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(const node_ptr&)
  141. static void unlink(const node_ptr & node);
  142. //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
  143. static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
  144. //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
  145. static bool unique(const const_node_ptr & node);
  146. //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
  147. static std::size_t size(const const_node_ptr & header);
  148. //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
  149. static node_ptr next_node(const node_ptr & node);
  150. //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
  151. static node_ptr prev_node(const node_ptr & node);
  152. //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
  153. static void init(const node_ptr & node);
  154. //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
  155. static void init_header(const node_ptr & header);
  156. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  157. //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
  158. //! Additional notes: the previous node of z is splayed. The "splay" parameter which indicated if splaying
  159. //! should be performed, it's deprecated and will disappear in future versions.
  160. static void erase(const node_ptr & header, const node_ptr & z, bool splay = true)
  161. {
  162. //posibility 1
  163. if(splay && NodeTraits::get_left(z)){
  164. splay_up(bstree_algo::prev_node(z), header);
  165. }
  166. /*
  167. //possibility 2
  168. if(splay && NodeTraits::get_left(z)){
  169. node_ptr l = NodeTraits::get_left(z);
  170. splay_up(l, header);
  171. }*//*
  172. if(splay && NodeTraits::get_left(z)){
  173. node_ptr l = bstree_algo::prev_node(z);
  174. splay_up_impl(l, z);
  175. }*/
  176. /*
  177. //possibility 4
  178. if(splay){
  179. splay_up(z, header);
  180. }*/
  181. //if(splay)
  182. //splay_up(z, header);
  183. bstree_algo::erase(header, z);
  184. }
  185. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  186. //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
  187. template <class Cloner, class Disposer>
  188. static void clone
  189. (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
  190. //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
  191. template<class Disposer>
  192. static void clear_and_dispose(const node_ptr & header, Disposer disposer);
  193. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  194. //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  195. //! Additional notes: the first node of the range is splayed.
  196. template<class KeyType, class KeyNodePtrCompare>
  197. static std::size_t count
  198. (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  199. {
  200. std::pair<node_ptr, node_ptr> ret = equal_range(header, key, comp);
  201. std::size_t n = 0;
  202. while(ret.first != ret.second){
  203. ++n;
  204. ret.first = next_node(ret.first);
  205. }
  206. return n;
  207. }
  208. //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  209. //! Additional note: no splaying is performed
  210. template<class KeyType, class KeyNodePtrCompare>
  211. static std::size_t count
  212. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  213. { return bstree_algo::count(header, key, comp); }
  214. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  215. //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying
  216. //! should be performed, it's deprecated and will disappear in future versions.
  217. template<class KeyType, class KeyNodePtrCompare>
  218. static node_ptr lower_bound
  219. (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true)
  220. {
  221. //splay_down(detail::uncast(header), key, comp);
  222. node_ptr y = bstree_algo::lower_bound(header, key, comp);
  223. if(splay) splay_up(y, detail::uncast(header));
  224. return y;
  225. }
  226. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  227. //! Additional note: no splaying is performed
  228. template<class KeyType, class KeyNodePtrCompare>
  229. static node_ptr lower_bound
  230. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  231. { return bstree_algo::lower_bound(header, key, comp); }
  232. //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  233. //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying
  234. //! should be performed, it's deprecated and will disappear in future versions.
  235. template<class KeyType, class KeyNodePtrCompare>
  236. static node_ptr upper_bound
  237. (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true)
  238. {
  239. //splay_down(detail::uncast(header), key, comp);
  240. node_ptr y = bstree_algo::upper_bound(header, key, comp);
  241. if(splay) splay_up(y, detail::uncast(header));
  242. return y;
  243. }
  244. //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  245. //! Additional note: no splaying is performed
  246. template<class KeyType, class KeyNodePtrCompare>
  247. static node_ptr upper_bound
  248. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  249. { return bstree_algo::upper_bound(header, key, comp); }
  250. //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
  251. //! Additional notes: the found node of the lower bound is splayed. The "splay" parameter which indicated if splaying
  252. //! should be performed, it's deprecated and will disappear in future versions.
  253. template<class KeyType, class KeyNodePtrCompare>
  254. static node_ptr find
  255. (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true)
  256. {
  257. if(splay) splay_down(detail::uncast(header), key, comp);
  258. node_ptr end = detail::uncast(header);
  259. node_ptr y = bstree_algo::lower_bound(header, key, comp);
  260. node_ptr r = (y == end || comp(key, y)) ? end : y;
  261. return r;
  262. }
  263. //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
  264. //! Additional note: no splaying is performed
  265. template<class KeyType, class KeyNodePtrCompare>
  266. static node_ptr find
  267. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  268. { return bstree_algo::find(header, key, comp); }
  269. //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  270. //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying
  271. //! should be performed, it's deprecated and will disappear in future versions.
  272. template<class KeyType, class KeyNodePtrCompare>
  273. static std::pair<node_ptr, node_ptr> equal_range
  274. (const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp, bool splay = true)
  275. {
  276. //splay_down(detail::uncast(header), key, comp);
  277. std::pair<node_ptr, node_ptr> ret = bstree_algo::equal_range(header, key, comp);
  278. if(splay) splay_up(ret.first, detail::uncast(header));
  279. return ret;
  280. }
  281. //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  282. //! Additional note: no splaying is performed
  283. template<class KeyType, class KeyNodePtrCompare>
  284. static std::pair<node_ptr, node_ptr> equal_range
  285. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  286. { return bstree_algo::equal_range(header, key, comp); }
  287. //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
  288. //! Additional notes: the first node of the range is splayed. The "splay" parameter which indicated if splaying
  289. //! should be performed, it's deprecated and will disappear in future versions.
  290. template<class KeyType, class KeyNodePtrCompare>
  291. static std::pair<node_ptr, node_ptr> bounded_range
  292. (const node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
  293. , bool left_closed, bool right_closed, bool splay = true)
  294. {
  295. std::pair<node_ptr, node_ptr> ret =
  296. bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed);
  297. if(splay) splay_up(ret.first, detail::uncast(header));
  298. return ret;
  299. }
  300. //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
  301. //! Additional note: no splaying is performed
  302. template<class KeyType, class KeyNodePtrCompare>
  303. static std::pair<node_ptr, node_ptr> bounded_range
  304. (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
  305. , bool left_closed, bool right_closed)
  306. { return bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed); }
  307. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
  308. //! Additional note: the inserted node is splayed
  309. template<class NodePtrCompare>
  310. static node_ptr insert_equal_upper_bound
  311. (const node_ptr & header, const node_ptr & new_node, NodePtrCompare comp)
  312. {
  313. splay_down(header, new_node, comp);
  314. return bstree_algo::insert_equal_upper_bound(header, new_node, comp);
  315. }
  316. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
  317. //! Additional note: the inserted node is splayed
  318. template<class NodePtrCompare>
  319. static node_ptr insert_equal_lower_bound
  320. (const node_ptr & header, const node_ptr & new_node, NodePtrCompare comp)
  321. {
  322. splay_down(header, new_node, comp);
  323. return bstree_algo::insert_equal_lower_bound(header, new_node, comp);
  324. }
  325. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
  326. //! Additional note: the inserted node is splayed
  327. template<class NodePtrCompare>
  328. static node_ptr insert_equal
  329. (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp)
  330. {
  331. splay_down(header, new_node, comp);
  332. return bstree_algo::insert_equal(header, hint, new_node, comp);
  333. }
  334. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
  335. //! Additional note: the inserted node is splayed
  336. static node_ptr insert_before
  337. (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node)
  338. {
  339. bstree_algo::insert_before(header, pos, new_node);
  340. splay_up(new_node, header);
  341. return new_node;
  342. }
  343. //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
  344. //! Additional note: the inserted node is splayed
  345. static void push_back(const node_ptr & header, const node_ptr & new_node)
  346. {
  347. bstree_algo::push_back(header, new_node);
  348. splay_up(new_node, header);
  349. }
  350. //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
  351. //! Additional note: the inserted node is splayed
  352. static void push_front(const node_ptr & header, const node_ptr & new_node)
  353. {
  354. bstree_algo::push_front(header, new_node);
  355. splay_up(new_node, header);
  356. }
  357. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  358. //! Additional note: nodes with the given key are splayed
  359. template<class KeyType, class KeyNodePtrCompare>
  360. static std::pair<node_ptr, bool> insert_unique_check
  361. (const node_ptr & header, const KeyType &key
  362. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  363. {
  364. splay_down(header, key, comp);
  365. return bstree_algo::insert_unique_check(header, key, comp, commit_data);
  366. }
  367. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  368. //! Additional note: nodes with the given key are splayed
  369. template<class KeyType, class KeyNodePtrCompare>
  370. static std::pair<node_ptr, bool> insert_unique_check
  371. (const node_ptr & header, const node_ptr &hint, const KeyType &key
  372. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  373. {
  374. splay_down(header, key, comp);
  375. return bstree_algo::insert_unique_check(header, hint, key, comp, commit_data);
  376. }
  377. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  378. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&)
  379. static void insert_unique_commit
  380. (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data);
  381. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  382. static bool is_header(const const_node_ptr & p);
  383. //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance
  384. static void rebalance(const node_ptr & header);
  385. //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
  386. static node_ptr rebalance_subtree(const node_ptr & old_root);
  387. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  388. // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow
  389. static void splay_up(const node_ptr & node, const node_ptr & header)
  390. {
  391. // If (node == header) do a splay for the right most node instead
  392. // this is to boost performance of equal_range/count on equivalent containers in the case
  393. // where there are many equal elements at the end
  394. node_ptr n((node == header) ? NodeTraits::get_right(header) : node);
  395. node_ptr t(header);
  396. if( n == t ) return;
  397. for( ;; ){
  398. node_ptr p(NodeTraits::get_parent(n));
  399. node_ptr g(NodeTraits::get_parent(p));
  400. if( p == t ) break;
  401. if( g == t ){
  402. // zig
  403. rotate(n);
  404. }
  405. else if ((NodeTraits::get_left(p) == n && NodeTraits::get_left(g) == p) ||
  406. (NodeTraits::get_right(p) == n && NodeTraits::get_right(g) == p) ){
  407. // zig-zig
  408. rotate(p);
  409. rotate(n);
  410. }
  411. else{
  412. // zig-zag
  413. rotate(n);
  414. rotate(n);
  415. }
  416. }
  417. }
  418. // top-down splay | complexity : logarithmic | exception : strong, note A
  419. template<class KeyType, class KeyNodePtrCompare>
  420. static node_ptr splay_down(const node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  421. {
  422. if(!NodeTraits::get_parent(header))
  423. return header;
  424. //Most splay tree implementations use a dummy/null node to implement.
  425. //this function. This has some problems for a generic library like Intrusive:
  426. //
  427. // * The node might not have a default constructor.
  428. // * The default constructor could throw.
  429. //
  430. //We already have a header node. Leftmost and rightmost nodes of the tree
  431. //are not changed when splaying (because the invariants of the tree don't
  432. //change) We can back up them, use the header as the null node and
  433. //reassign old values after the function has been completed.
  434. node_ptr t = NodeTraits::get_parent(header);
  435. //Check if tree has a single node
  436. if(!NodeTraits::get_left(t) && !NodeTraits::get_right(t))
  437. return t;
  438. //Backup leftmost/rightmost
  439. node_ptr leftmost (NodeTraits::get_left(header));
  440. node_ptr rightmost(NodeTraits::get_right(header));
  441. {
  442. //Anti-exception rollback, recovers the original header node if an exception is thrown.
  443. detail::splaydown_rollback<NodeTraits> rollback(&t, header, leftmost, rightmost);
  444. node_ptr null_node = header;
  445. node_ptr l = null_node;
  446. node_ptr r = null_node;
  447. for( ;; ){
  448. if(comp(key, t)){
  449. if(NodeTraits::get_left(t) == node_ptr() )
  450. break;
  451. if(comp(key, NodeTraits::get_left(t))){
  452. t = bstree_algo::rotate_right(t);
  453. if(NodeTraits::get_left(t) == node_ptr())
  454. break;
  455. link_right(t, r);
  456. }
  457. else if(comp(NodeTraits::get_left(t), key)){
  458. link_right(t, r);
  459. if(NodeTraits::get_right(t) == node_ptr() )
  460. break;
  461. link_left(t, l);
  462. }
  463. else{
  464. link_right(t, r);
  465. }
  466. }
  467. else if(comp(t, key)){
  468. if(NodeTraits::get_right(t) == node_ptr() )
  469. break;
  470. if(comp(NodeTraits::get_right(t), key)){
  471. t = bstree_algo::rotate_left( t );
  472. if(NodeTraits::get_right(t) == node_ptr() )
  473. break;
  474. link_left(t, l);
  475. }
  476. else if(comp(key, NodeTraits::get_right(t))){
  477. link_left(t, l);
  478. if(NodeTraits::get_left(t) == node_ptr())
  479. break;
  480. link_right(t, r);
  481. }
  482. else{
  483. link_left(t, l);
  484. }
  485. }
  486. else{
  487. break;
  488. }
  489. }
  490. assemble(t, l, r, null_node);
  491. rollback.release();
  492. }
  493. //Now recover the original header except for the
  494. //splayed root node.
  495. //t is the current root
  496. NodeTraits::set_parent(header, t);
  497. NodeTraits::set_parent(t, header);
  498. //Recover leftmost/rightmost pointers
  499. NodeTraits::set_left (header, leftmost);
  500. NodeTraits::set_right(header, rightmost);
  501. return t;
  502. }
  503. private:
  504. /// @cond
  505. // assemble the three sub-trees into new tree pointed to by t | complexity : constant | exception : nothrow
  506. static void assemble(const node_ptr &t, const node_ptr & l, const node_ptr & r, const const_node_ptr & null_node )
  507. {
  508. NodeTraits::set_right(l, NodeTraits::get_left(t));
  509. NodeTraits::set_left(r, NodeTraits::get_right(t));
  510. if(NodeTraits::get_right(l) != node_ptr()){
  511. NodeTraits::set_parent(NodeTraits::get_right(l), l);
  512. }
  513. if(NodeTraits::get_left(r) != node_ptr()){
  514. NodeTraits::set_parent(NodeTraits::get_left(r), r);
  515. }
  516. NodeTraits::set_left (t, NodeTraits::get_right(null_node));
  517. NodeTraits::set_right(t, NodeTraits::get_left(null_node));
  518. if( NodeTraits::get_left(t) != node_ptr() ){
  519. NodeTraits::set_parent(NodeTraits::get_left(t), t);
  520. }
  521. if( NodeTraits::get_right(t) ){
  522. NodeTraits::set_parent(NodeTraits::get_right(t), t);
  523. }
  524. }
  525. // break link to left child node and attach it to left tree pointed to by l | complexity : constant | exception : nothrow
  526. static void link_left(node_ptr & t, node_ptr & l)
  527. {
  528. NodeTraits::set_right(l, t);
  529. NodeTraits::set_parent(t, l);
  530. l = t;
  531. t = NodeTraits::get_right(t);
  532. }
  533. // break link to right child node and attach it to right tree pointed to by r | complexity : constant | exception : nothrow
  534. static void link_right(node_ptr & t, node_ptr & r)
  535. {
  536. NodeTraits::set_left(r, t);
  537. NodeTraits::set_parent(t, r);
  538. r = t;
  539. t = NodeTraits::get_left(t);
  540. }
  541. // rotate n with its parent | complexity : constant | exception : nothrow
  542. static void rotate(const node_ptr & n)
  543. {
  544. node_ptr p = NodeTraits::get_parent(n);
  545. node_ptr g = NodeTraits::get_parent(p);
  546. //Test if g is header before breaking tree
  547. //invariants that would make is_header invalid
  548. bool g_is_header = bstree_algo::is_header(g);
  549. if(NodeTraits::get_left(p) == n){
  550. NodeTraits::set_left(p, NodeTraits::get_right(n));
  551. if(NodeTraits::get_left(p) != node_ptr())
  552. NodeTraits::set_parent(NodeTraits::get_left(p), p);
  553. NodeTraits::set_right(n, p);
  554. }
  555. else{ // must be ( p->right == n )
  556. NodeTraits::set_right(p, NodeTraits::get_left(n));
  557. if(NodeTraits::get_right(p) != node_ptr())
  558. NodeTraits::set_parent(NodeTraits::get_right(p), p);
  559. NodeTraits::set_left(n, p);
  560. }
  561. NodeTraits::set_parent(p, n);
  562. NodeTraits::set_parent(n, g);
  563. if(g_is_header){
  564. if(NodeTraits::get_parent(g) == p)
  565. NodeTraits::set_parent(g, n);
  566. else{//must be ( g->right == p )
  567. BOOST_INTRUSIVE_INVARIANT_ASSERT(false);
  568. NodeTraits::set_right(g, n);
  569. }
  570. }
  571. else{
  572. if(NodeTraits::get_left(g) == p)
  573. NodeTraits::set_left(g, n);
  574. else //must be ( g->right == p )
  575. NodeTraits::set_right(g, n);
  576. }
  577. }
  578. /// @endcond
  579. };
  580. /// @cond
  581. template<class NodeTraits>
  582. struct get_algo<SplayTreeAlgorithms, NodeTraits>
  583. {
  584. typedef splaytree_algorithms<NodeTraits> type;
  585. };
  586. /// @endcond
  587. } //namespace intrusive
  588. } //namespace boost
  589. #include <boost/intrusive/detail/config_end.hpp>
  590. #endif //BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP