splaytree_algorithms.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-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. // 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/intrusive_fwd.hpp>
  33. #include <boost/intrusive/detail/assert.hpp>
  34. #include <boost/intrusive/detail/algo_type.hpp>
  35. #include <boost/intrusive/detail/uncast.hpp>
  36. #include <boost/intrusive/bstree_algorithms.hpp>
  37. #include <cstddef>
  38. #if defined(BOOST_HAS_PRAGMA_ONCE)
  39. # pragma once
  40. #endif
  41. namespace boost {
  42. namespace intrusive {
  43. /// @cond
  44. namespace detail {
  45. template<class NodeTraits>
  46. struct splaydown_assemble_and_fix_header
  47. {
  48. typedef typename NodeTraits::node_ptr node_ptr;
  49. splaydown_assemble_and_fix_header(node_ptr t, node_ptr header, node_ptr leftmost, node_ptr rightmost) BOOST_NOEXCEPT
  50. : t_(t)
  51. , null_node_(header)
  52. , l_(null_node_)
  53. , r_(null_node_)
  54. , leftmost_(leftmost)
  55. , rightmost_(rightmost)
  56. {}
  57. ~splaydown_assemble_and_fix_header()
  58. {
  59. this->assemble();
  60. //Now recover the original header except for the
  61. //splayed root node.
  62. //"t_" is the current root and "null_node_" is the header node
  63. NodeTraits::set_parent(null_node_, t_);
  64. NodeTraits::set_parent(t_, null_node_);
  65. //Recover leftmost/rightmost pointers
  66. NodeTraits::set_left (null_node_, leftmost_);
  67. NodeTraits::set_right(null_node_, rightmost_);
  68. }
  69. private:
  70. void assemble() BOOST_NOEXCEPT
  71. {
  72. //procedure assemble;
  73. // left(r), right(l) := right(t), left(t);
  74. // left(t), right(t) := right(null), left(null);
  75. //end assemble;
  76. { // left(r), right(l) := right(t), left(t);
  77. node_ptr const old_t_left = NodeTraits::get_left(t_);
  78. node_ptr const old_t_right = NodeTraits::get_right(t_);
  79. NodeTraits::set_right(l_, old_t_left);
  80. NodeTraits::set_left (r_, old_t_right);
  81. if(old_t_left){
  82. NodeTraits::set_parent(old_t_left, l_);
  83. }
  84. if(old_t_right){
  85. NodeTraits::set_parent(old_t_right, r_);
  86. }
  87. }
  88. { // left(t), right(t) := right(null), left(null);
  89. node_ptr const null_right = NodeTraits::get_right(null_node_);
  90. node_ptr const null_left = NodeTraits::get_left(null_node_);
  91. NodeTraits::set_left (t_, null_right);
  92. NodeTraits::set_right(t_, null_left);
  93. if(null_right){
  94. NodeTraits::set_parent(null_right, t_);
  95. }
  96. if(null_left){
  97. NodeTraits::set_parent(null_left, t_);
  98. }
  99. }
  100. }
  101. public:
  102. node_ptr t_, null_node_, l_, r_, leftmost_, rightmost_;
  103. };
  104. } //namespace detail {
  105. /// @endcond
  106. //! A splay tree is an implementation of a binary search tree. The tree is
  107. //! self balancing using the splay algorithm as described in
  108. //!
  109. //! "Self-Adjusting Binary Search Trees
  110. //! by Daniel Dominic Sleator and Robert Endre Tarjan
  111. //! AT&T Bell Laboratories, Murray Hill, NJ
  112. //! Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686
  113. //!
  114. //! splaytree_algorithms is configured with a NodeTraits class, which encapsulates the
  115. //! information about the node to be manipulated. NodeTraits must support the
  116. //! following interface:
  117. //!
  118. //! <b>Typedefs</b>:
  119. //!
  120. //! <tt>node</tt>: The type of the node that forms the binary search tree
  121. //!
  122. //! <tt>node_ptr</tt>: A pointer to a node
  123. //!
  124. //! <tt>const_node_ptr</tt>: A pointer to a const node
  125. //!
  126. //! <b>Static functions</b>:
  127. //!
  128. //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
  129. //!
  130. //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
  131. //!
  132. //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
  133. //!
  134. //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
  135. //!
  136. //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
  137. //!
  138. //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
  139. template<class NodeTraits>
  140. class splaytree_algorithms
  141. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  142. : public bstree_algorithms<NodeTraits>
  143. #endif
  144. {
  145. /// @cond
  146. private:
  147. typedef bstree_algorithms<NodeTraits> bstree_algo;
  148. /// @endcond
  149. public:
  150. typedef typename NodeTraits::node node;
  151. typedef NodeTraits node_traits;
  152. typedef typename NodeTraits::node_ptr node_ptr;
  153. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  154. //! This type is the information that will be
  155. //! filled by insert_unique_check
  156. typedef typename bstree_algo::insert_commit_data insert_commit_data;
  157. public:
  158. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  159. //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const_node_ptr)
  160. static node_ptr get_header(const_node_ptr n) BOOST_NOEXCEPT;
  161. //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
  162. static node_ptr begin_node(const_node_ptr header) BOOST_NOEXCEPT;
  163. //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
  164. static node_ptr end_node(const_node_ptr header) BOOST_NOEXCEPT;
  165. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
  166. static void swap_tree(node_ptr header1, node_ptr header2);
  167. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(node_ptr,node_ptr)
  168. static void swap_nodes(node_ptr node1, node_ptr node2) BOOST_NOEXCEPT;
  169. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(node_ptr,node_ptr,node_ptr,node_ptr)
  170. static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2) BOOST_NOEXCEPT;
  171. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(node_ptr,node_ptr)
  172. static void replace_node(node_ptr node_to_be_replaced, node_ptr new_node) BOOST_NOEXCEPT;
  173. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(node_ptr,node_ptr,node_ptr)
  174. static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node) BOOST_NOEXCEPT;
  175. //! @copydoc ::boost::intrusive::bstree_algorithms::unlink(node_ptr)
  176. static void unlink(node_ptr n) BOOST_NOEXCEPT;
  177. //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
  178. static node_ptr unlink_leftmost_without_rebalance(node_ptr header) BOOST_NOEXCEPT;
  179. //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const_node_ptr)
  180. static bool unique(const_node_ptr n) BOOST_NOEXCEPT;
  181. //! @copydoc ::boost::intrusive::bstree_algorithms::size(const_node_ptr)
  182. static std::size_t size(const_node_ptr header) BOOST_NOEXCEPT;
  183. //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(node_ptr)
  184. static node_ptr next_node(node_ptr n) BOOST_NOEXCEPT;
  185. //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(node_ptr)
  186. static node_ptr prev_node(node_ptr n) BOOST_NOEXCEPT;
  187. //! @copydoc ::boost::intrusive::bstree_algorithms::init(node_ptr)
  188. static void init(node_ptr n) BOOST_NOEXCEPT;
  189. //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(node_ptr)
  190. static void init_header(node_ptr header) BOOST_NOEXCEPT;
  191. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  192. //! @copydoc ::boost::intrusive::bstree_algorithms::erase(node_ptr,node_ptr)
  193. //! Additional notes: the previous node of z is splayed to speed up range deletions.
  194. static void erase(node_ptr header, node_ptr z) BOOST_NOEXCEPT
  195. {
  196. //posibility 1
  197. if(NodeTraits::get_left(z)){
  198. splay_up(bstree_algo::prev_node(z), header);
  199. }
  200. //possibility 2
  201. //if(NodeTraits::get_left(z)){
  202. // node_ptr l = NodeTraits::get_left(z);
  203. // splay_up(l, header);
  204. //}
  205. //if(NodeTraits::get_left(z)){
  206. // node_ptr l = bstree_algo::prev_node(z);
  207. // splay_up_impl(l, z);
  208. //}
  209. //possibility 4
  210. //splay_up(z, header);
  211. bstree_algo::erase(header, z);
  212. }
  213. //! @copydoc ::boost::intrusive::bstree_algorithms::transfer_unique
  214. template<class NodePtrCompare>
  215. static bool transfer_unique
  216. (node_ptr header1, NodePtrCompare comp, node_ptr header2, node_ptr z)
  217. {
  218. typename bstree_algo::insert_commit_data commit_data;
  219. bool const transferable = bstree_algo::insert_unique_check(header1, z, comp, commit_data).second;
  220. if(transferable){
  221. erase(header2, z);
  222. bstree_algo::insert_commit(header1, z, commit_data);
  223. splay_up(z, header1);
  224. }
  225. return transferable;
  226. }
  227. //! @copydoc ::boost::intrusive::bstree_algorithms::transfer_equal
  228. template<class NodePtrCompare>
  229. static void transfer_equal
  230. (node_ptr header1, NodePtrCompare comp, node_ptr header2, node_ptr z)
  231. {
  232. insert_commit_data commit_data;
  233. splay_down(header1, z, comp);
  234. bstree_algo::insert_equal_upper_bound_check(header1, z, comp, commit_data);
  235. erase(header2, z);
  236. bstree_algo::insert_commit(header1, z, commit_data);
  237. }
  238. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  239. //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const_node_ptr,node_ptr,Cloner,Disposer)
  240. template <class Cloner, class Disposer>
  241. static void clone
  242. (const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer);
  243. //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(node_ptr,Disposer)
  244. template<class Disposer>
  245. static void clear_and_dispose(node_ptr header, Disposer disposer) BOOST_NOEXCEPT;
  246. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  247. //! @copydoc ::boost::intrusive::bstree_algorithms::count(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  248. //! Additional notes: an element with key `key` is splayed.
  249. template<class KeyType, class KeyNodePtrCompare>
  250. static std::size_t count
  251. (node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  252. {
  253. std::pair<node_ptr, node_ptr> ret = equal_range(header, key, comp);
  254. std::size_t n = 0;
  255. while(ret.first != ret.second){
  256. ++n;
  257. ret.first = next_node(ret.first);
  258. }
  259. return n;
  260. }
  261. //! @copydoc ::boost::intrusive::bstree_algorithms::count(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  262. //! Additional note: no splaying is performed
  263. template<class KeyType, class KeyNodePtrCompare>
  264. static std::size_t count
  265. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  266. { return bstree_algo::count(header, key, comp); }
  267. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  268. //! Additional notes: the first node of the range is splayed.
  269. template<class KeyType, class KeyNodePtrCompare>
  270. static node_ptr lower_bound
  271. (node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  272. {
  273. splay_down(detail::uncast(header), key, comp);
  274. node_ptr y = bstree_algo::lower_bound(header, key, comp);
  275. //splay_up(y, detail::uncast(header));
  276. return y;
  277. }
  278. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  279. //! Additional note: no splaying is performed
  280. template<class KeyType, class KeyNodePtrCompare>
  281. static node_ptr lower_bound
  282. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  283. { return bstree_algo::lower_bound(header, key, comp); }
  284. //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  285. //! Additional notes: the first node of the range is splayed.
  286. template<class KeyType, class KeyNodePtrCompare>
  287. static node_ptr upper_bound
  288. (node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  289. {
  290. splay_down(detail::uncast(header), key, comp);
  291. node_ptr y = bstree_algo::upper_bound(header, key, comp);
  292. //splay_up(y, detail::uncast(header));
  293. return y;
  294. }
  295. //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  296. //! Additional note: no splaying is performed
  297. template<class KeyType, class KeyNodePtrCompare>
  298. static node_ptr upper_bound
  299. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  300. { return bstree_algo::upper_bound(header, key, comp); }
  301. //! @copydoc ::boost::intrusive::bstree_algorithms::find(const_node_ptr, const KeyType&,KeyNodePtrCompare)
  302. //! Additional notes: the found node of the lower bound is splayed.
  303. template<class KeyType, class KeyNodePtrCompare>
  304. static node_ptr find
  305. (node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  306. {
  307. splay_down(detail::uncast(header), key, comp);
  308. return bstree_algo::find(header, key, comp);
  309. }
  310. //! @copydoc ::boost::intrusive::bstree_algorithms::find(const_node_ptr, const KeyType&,KeyNodePtrCompare)
  311. //! Additional note: no splaying is performed
  312. template<class KeyType, class KeyNodePtrCompare>
  313. static node_ptr find
  314. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  315. { return bstree_algo::find(header, key, comp); }
  316. //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  317. //! Additional notes: the first node of the range is splayed.
  318. template<class KeyType, class KeyNodePtrCompare>
  319. static std::pair<node_ptr, node_ptr> equal_range
  320. (node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  321. {
  322. splay_down(detail::uncast(header), key, comp);
  323. std::pair<node_ptr, node_ptr> ret = bstree_algo::equal_range(header, key, comp);
  324. //splay_up(ret.first, detail::uncast(header));
  325. return ret;
  326. }
  327. //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  328. //! Additional note: no splaying is performed
  329. template<class KeyType, class KeyNodePtrCompare>
  330. static std::pair<node_ptr, node_ptr> equal_range
  331. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  332. { return bstree_algo::equal_range(header, key, comp); }
  333. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound_range(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  334. //! Additional notes: the first node of the range is splayed.
  335. template<class KeyType, class KeyNodePtrCompare>
  336. static std::pair<node_ptr, node_ptr> lower_bound_range
  337. (node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  338. {
  339. splay_down(detail::uncast(header), key, comp);
  340. std::pair<node_ptr, node_ptr> ret = bstree_algo::lower_bound_range(header, key, comp);
  341. //splay_up(ret.first, detail::uncast(header));
  342. return ret;
  343. }
  344. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound_range(const_node_ptr,const KeyType&,KeyNodePtrCompare)
  345. //! Additional note: no splaying is performed
  346. template<class KeyType, class KeyNodePtrCompare>
  347. static std::pair<node_ptr, node_ptr> lower_bound_range
  348. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
  349. { return bstree_algo::lower_bound_range(header, key, comp); }
  350. //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const_node_ptr,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
  351. //! Additional notes: the first node of the range is splayed.
  352. template<class KeyType, class KeyNodePtrCompare>
  353. static std::pair<node_ptr, node_ptr> bounded_range
  354. (node_ptr header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
  355. , bool left_closed, bool right_closed)
  356. {
  357. splay_down(detail::uncast(header), lower_key, comp);
  358. std::pair<node_ptr, node_ptr> ret =
  359. bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed);
  360. //splay_up(ret.first, detail::uncast(header));
  361. return ret;
  362. }
  363. //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const_node_ptr,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
  364. //! Additional note: no splaying is performed
  365. template<class KeyType, class KeyNodePtrCompare>
  366. static std::pair<node_ptr, node_ptr> bounded_range
  367. (const_node_ptr header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
  368. , bool left_closed, bool right_closed)
  369. { return bstree_algo::bounded_range(header, lower_key, upper_key, comp, left_closed, right_closed); }
  370. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(node_ptr,node_ptr,NodePtrCompare)
  371. //! Additional note: the inserted node is splayed
  372. template<class NodePtrCompare>
  373. static node_ptr insert_equal_upper_bound
  374. (node_ptr header, node_ptr new_node, NodePtrCompare comp)
  375. {
  376. splay_down(header, new_node, comp);
  377. return bstree_algo::insert_equal_upper_bound(header, new_node, comp);
  378. }
  379. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(node_ptr,node_ptr,NodePtrCompare)
  380. //! Additional note: the inserted node is splayed
  381. template<class NodePtrCompare>
  382. static node_ptr insert_equal_lower_bound
  383. (node_ptr header, node_ptr new_node, NodePtrCompare comp)
  384. {
  385. splay_down(header, new_node, comp);
  386. return bstree_algo::insert_equal_lower_bound(header, new_node, comp);
  387. }
  388. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(node_ptr,node_ptr,node_ptr,NodePtrCompare)
  389. //! Additional note: the inserted node is splayed
  390. template<class NodePtrCompare>
  391. static node_ptr insert_equal
  392. (node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp)
  393. {
  394. splay_down(header, new_node, comp);
  395. return bstree_algo::insert_equal(header, hint, new_node, comp);
  396. }
  397. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(node_ptr,node_ptr,node_ptr)
  398. //! Additional note: the inserted node is splayed
  399. static node_ptr insert_before
  400. (node_ptr header, node_ptr pos, node_ptr new_node) BOOST_NOEXCEPT
  401. {
  402. bstree_algo::insert_before(header, pos, new_node);
  403. splay_up(new_node, header);
  404. return new_node;
  405. }
  406. //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(node_ptr,node_ptr)
  407. //! Additional note: the inserted node is splayed
  408. static void push_back(node_ptr header, node_ptr new_node) BOOST_NOEXCEPT
  409. {
  410. bstree_algo::push_back(header, new_node);
  411. splay_up(new_node, header);
  412. }
  413. //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(node_ptr,node_ptr)
  414. //! Additional note: the inserted node is splayed
  415. static void push_front(node_ptr header, node_ptr new_node) BOOST_NOEXCEPT
  416. {
  417. bstree_algo::push_front(header, new_node);
  418. splay_up(new_node, header);
  419. }
  420. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const_node_ptr,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  421. //! Additional note: nodes with the given key are splayed
  422. template<class KeyType, class KeyNodePtrCompare>
  423. static std::pair<node_ptr, bool> insert_unique_check
  424. (node_ptr header, const KeyType &key
  425. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  426. {
  427. splay_down(header, key, comp);
  428. return bstree_algo::insert_unique_check(header, key, comp, commit_data);
  429. }
  430. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const_node_ptr,node_ptr,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  431. //! Additional note: nodes with the given key are splayed
  432. template<class KeyType, class KeyNodePtrCompare>
  433. static std::pair<node_ptr, bool> insert_unique_check
  434. (node_ptr header, node_ptr hint, const KeyType &key
  435. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  436. {
  437. splay_down(header, key, comp);
  438. return bstree_algo::insert_unique_check(header, hint, key, comp, commit_data);
  439. }
  440. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  441. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(node_ptr,node_ptr,const insert_commit_data&)
  442. static void insert_unique_commit
  443. (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data) BOOST_NOEXCEPT;
  444. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  445. static bool is_header(const_node_ptr p) BOOST_NOEXCEPT;
  446. //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance
  447. static void rebalance(node_ptr header) BOOST_NOEXCEPT;
  448. //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
  449. static node_ptr rebalance_subtree(node_ptr old_root) BOOST_NOEXCEPT;
  450. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  451. // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow
  452. static void splay_up(node_ptr n, node_ptr header) BOOST_NOEXCEPT
  453. { priv_splay_up<true>(n, header); }
  454. // top-down splay | complexity : logarithmic | exception : strong, note A
  455. template<class KeyType, class KeyNodePtrCompare>
  456. static node_ptr splay_down(node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool *pfound = 0)
  457. { return priv_splay_down<true>(header, key, comp, pfound); }
  458. private:
  459. /// @cond
  460. // bottom-up splay, use data_ as parent for n | complexity : logarithmic | exception : nothrow
  461. template<bool SimpleSplay>
  462. static void priv_splay_up(node_ptr n, node_ptr header) BOOST_NOEXCEPT
  463. {
  464. // If (node == header) do a splay for the right most node instead
  465. // this is to boost performance of equal_range/count on equivalent containers in the case
  466. // where there are many equal elements at the end
  467. if(n == header)
  468. n = NodeTraits::get_right(header);
  469. node_ptr t(header);
  470. if( n == t ) return;
  471. for( ;; ){
  472. node_ptr p(NodeTraits::get_parent(n));
  473. node_ptr g(NodeTraits::get_parent(p));
  474. if( p == t ) break;
  475. if( g == t ){
  476. // zig
  477. rotate(n);
  478. }
  479. else if ((NodeTraits::get_left(p) == n && NodeTraits::get_left(g) == p) ||
  480. (NodeTraits::get_right(p) == n && NodeTraits::get_right(g) == p) ){
  481. // zig-zig
  482. rotate(p);
  483. rotate(n);
  484. }
  485. else {
  486. // zig-zag
  487. rotate(n);
  488. if(!SimpleSplay){
  489. rotate(n);
  490. }
  491. }
  492. }
  493. }
  494. template<bool SimpleSplay, class KeyType, class KeyNodePtrCompare>
  495. static node_ptr priv_splay_down(node_ptr header, const KeyType &key, KeyNodePtrCompare comp, bool *pfound = 0)
  496. {
  497. //Most splay tree implementations use a dummy/null node to implement.
  498. //this function. This has some problems for a generic library like Intrusive:
  499. //
  500. // * The node might not have a default constructor.
  501. // * The default constructor could throw.
  502. //
  503. //We already have a header node. Leftmost and rightmost nodes of the tree
  504. //are not changed when splaying (because the invariants of the tree don't
  505. //change) We can back up them, use the header as the null node and
  506. //reassign old values after the function has been completed.
  507. node_ptr const old_root = NodeTraits::get_parent(header);
  508. node_ptr const leftmost = NodeTraits::get_left(header);
  509. node_ptr const rightmost = NodeTraits::get_right(header);
  510. if(leftmost == rightmost){ //Empty or unique node
  511. if(pfound){
  512. *pfound = old_root && !comp(key, old_root) && !comp(old_root, key);
  513. }
  514. return old_root ? old_root : header;
  515. }
  516. else{
  517. //Initialize "null node" (the header in our case)
  518. NodeTraits::set_left (header, node_ptr());
  519. NodeTraits::set_right(header, node_ptr());
  520. //Class that will backup leftmost/rightmost from header, commit the assemble(),
  521. //and will restore leftmost/rightmost to header even if "comp" throws
  522. detail::splaydown_assemble_and_fix_header<NodeTraits> commit(old_root, header, leftmost, rightmost);
  523. bool found = false;
  524. for( ;; ){
  525. if(comp(key, commit.t_)){
  526. node_ptr const t_left = NodeTraits::get_left(commit.t_);
  527. if(!t_left)
  528. break;
  529. if(comp(key, t_left)){
  530. bstree_algo::rotate_right_no_parent_fix(commit.t_, t_left);
  531. commit.t_ = t_left;
  532. if( !NodeTraits::get_left(commit.t_) )
  533. break;
  534. link_right(commit.t_, commit.r_);
  535. }
  536. else{
  537. link_right(commit.t_, commit.r_);
  538. if(!SimpleSplay && comp(t_left, key)){
  539. if( !NodeTraits::get_right(commit.t_) )
  540. break;
  541. link_left(commit.t_, commit.l_);
  542. }
  543. }
  544. }
  545. else if(comp(commit.t_, key)){
  546. node_ptr const t_right = NodeTraits::get_right(commit.t_);
  547. if(!t_right)
  548. break;
  549. if(comp(t_right, key)){
  550. bstree_algo::rotate_left_no_parent_fix(commit.t_, t_right);
  551. commit.t_ = t_right;
  552. if( !NodeTraits::get_right(commit.t_) )
  553. break;
  554. link_left(commit.t_, commit.l_);
  555. }
  556. else{
  557. link_left(commit.t_, commit.l_);
  558. if(!SimpleSplay && comp(key, t_right)){
  559. if( !NodeTraits::get_left(commit.t_) )
  560. break;
  561. link_right(commit.t_, commit.r_);
  562. }
  563. }
  564. }
  565. else{
  566. found = true;
  567. break;
  568. }
  569. }
  570. //commit.~splaydown_assemble_and_fix_header<NodeTraits>() will first
  571. //"assemble()" + link the new root & recover header's leftmost & rightmost
  572. if(pfound){
  573. *pfound = found;
  574. }
  575. return commit.t_;
  576. }
  577. }
  578. // break link to left child node and attach it to left tree pointed to by l | complexity : constant | exception : nothrow
  579. static void link_left(node_ptr & t, node_ptr & l) BOOST_NOEXCEPT
  580. {
  581. //procedure link_left;
  582. // t, l, right(l) := right(t), t, t
  583. //end link_left
  584. NodeTraits::set_right(l, t);
  585. NodeTraits::set_parent(t, l);
  586. l = t;
  587. t = NodeTraits::get_right(t);
  588. }
  589. // break link to right child node and attach it to right tree pointed to by r | complexity : constant | exception : nothrow
  590. static void link_right(node_ptr & t, node_ptr & r) BOOST_NOEXCEPT
  591. {
  592. //procedure link_right;
  593. // t, r, left(r) := left(t), t, t
  594. //end link_right;
  595. NodeTraits::set_left(r, t);
  596. NodeTraits::set_parent(t, r);
  597. r = t;
  598. t = NodeTraits::get_left(t);
  599. }
  600. // rotate n with its parent | complexity : constant | exception : nothrow
  601. static void rotate(node_ptr n) BOOST_NOEXCEPT
  602. {
  603. //procedure rotate_left;
  604. // t, right(t), left(right(t)) := right(t), left(right(t)), t
  605. //end rotate_left;
  606. node_ptr p = NodeTraits::get_parent(n);
  607. node_ptr g = NodeTraits::get_parent(p);
  608. //Test if g is header before breaking tree
  609. //invariants that would make is_header invalid
  610. bool g_is_header = bstree_algo::is_header(g);
  611. if(NodeTraits::get_left(p) == n){
  612. NodeTraits::set_left(p, NodeTraits::get_right(n));
  613. if(NodeTraits::get_left(p))
  614. NodeTraits::set_parent(NodeTraits::get_left(p), p);
  615. NodeTraits::set_right(n, p);
  616. }
  617. else{ // must be ( p->right == n )
  618. NodeTraits::set_right(p, NodeTraits::get_left(n));
  619. if(NodeTraits::get_right(p))
  620. NodeTraits::set_parent(NodeTraits::get_right(p), p);
  621. NodeTraits::set_left(n, p);
  622. }
  623. NodeTraits::set_parent(p, n);
  624. NodeTraits::set_parent(n, g);
  625. if(g_is_header){
  626. if(NodeTraits::get_parent(g) == p)
  627. NodeTraits::set_parent(g, n);
  628. else{//must be ( g->right == p )
  629. BOOST_INTRUSIVE_INVARIANT_ASSERT(false);
  630. NodeTraits::set_right(g, n);
  631. }
  632. }
  633. else{
  634. if(NodeTraits::get_left(g) == p)
  635. NodeTraits::set_left(g, n);
  636. else //must be ( g->right == p )
  637. NodeTraits::set_right(g, n);
  638. }
  639. }
  640. /// @endcond
  641. };
  642. /// @cond
  643. template<class NodeTraits>
  644. struct get_algo<SplayTreeAlgorithms, NodeTraits>
  645. {
  646. typedef splaytree_algorithms<NodeTraits> type;
  647. };
  648. template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
  649. struct get_node_checker<SplayTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
  650. {
  651. typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
  652. };
  653. /// @endcond
  654. } //namespace intrusive
  655. } //namespace boost
  656. #include <boost/intrusive/detail/config_end.hpp>
  657. #endif //BOOST_INTRUSIVE_SPLAYTREE_ALGORITHMS_HPP