splaytree.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #ifndef BOOST_INTRUSIVE_SPLAYTREE_HPP
  13. #define BOOST_INTRUSIVE_SPLAYTREE_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <boost/intrusive/intrusive_fwd.hpp>
  16. #include <cstddef>
  17. #include <boost/intrusive/detail/minimal_less_equal_header.hpp>
  18. #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
  19. #include <boost/static_assert.hpp>
  20. #include <boost/intrusive/bstree.hpp>
  21. #include <boost/intrusive/detail/tree_node.hpp>
  22. #include <boost/intrusive/detail/mpl.hpp>
  23. #include <boost/intrusive/pointer_traits.hpp>
  24. #include <boost/intrusive/detail/function_detector.hpp>
  25. #include <boost/intrusive/detail/get_value_traits.hpp>
  26. #include <boost/intrusive/splaytree_algorithms.hpp>
  27. #include <boost/intrusive/link_mode.hpp>
  28. #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
  29. #include <boost/move/utility_core.hpp>
  30. #if defined(BOOST_HAS_PRAGMA_ONCE)
  31. # pragma once
  32. #endif
  33. namespace boost {
  34. namespace intrusive {
  35. /// @cond
  36. struct splaytree_defaults
  37. : bstree_defaults
  38. {};
  39. /// @endcond
  40. //! The class template splaytree is an intrusive splay tree container that
  41. //! is used to construct intrusive splay_set and splay_multiset containers. The no-throw
  42. //! guarantee holds only, if the key_compare object
  43. //! doesn't throw.
  44. //!
  45. //! The template parameter \c T is the type to be managed by the container.
  46. //! The user can specify additional options and if no options are provided
  47. //! default options are used.
  48. //!
  49. //! The container supports the following options:
  50. //! \c base_hook<>/member_hook<>/value_traits<>,
  51. //! \c constant_time_size<>, \c size_type<> and
  52. //! \c compare<>.
  53. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  54. template<class T, class ...Options>
  55. #else
  56. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, bool ConstantTimeSize, typename HeaderHolder>
  57. #endif
  58. class splaytree_impl
  59. /// @cond
  60. : public bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, SplayTreeAlgorithms, HeaderHolder>
  61. /// @endcond
  62. {
  63. public:
  64. typedef ValueTraits value_traits;
  65. /// @cond
  66. typedef bstree_impl< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType
  67. , ConstantTimeSize, SplayTreeAlgorithms
  68. , HeaderHolder> tree_type;
  69. typedef tree_type implementation_defined;
  70. /// @endcond
  71. typedef typename implementation_defined::pointer pointer;
  72. typedef typename implementation_defined::const_pointer const_pointer;
  73. typedef typename implementation_defined::value_type value_type;
  74. typedef typename implementation_defined::key_type key_type;
  75. typedef typename implementation_defined::key_of_value key_of_value;
  76. typedef typename implementation_defined::reference reference;
  77. typedef typename implementation_defined::const_reference const_reference;
  78. typedef typename implementation_defined::difference_type difference_type;
  79. typedef typename implementation_defined::size_type size_type;
  80. typedef typename implementation_defined::value_compare value_compare;
  81. typedef typename implementation_defined::key_compare key_compare;
  82. typedef typename implementation_defined::iterator iterator;
  83. typedef typename implementation_defined::const_iterator const_iterator;
  84. typedef typename implementation_defined::reverse_iterator reverse_iterator;
  85. typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator;
  86. typedef typename implementation_defined::node_traits node_traits;
  87. typedef typename implementation_defined::node node;
  88. typedef typename implementation_defined::node_ptr node_ptr;
  89. typedef typename implementation_defined::const_node_ptr const_node_ptr;
  90. typedef typename implementation_defined::node_algorithms node_algorithms;
  91. static const bool constant_time_size = implementation_defined::constant_time_size;
  92. /// @cond
  93. private:
  94. //noncopyable
  95. BOOST_MOVABLE_BUT_NOT_COPYABLE(splaytree_impl)
  96. /// @endcond
  97. public:
  98. typedef typename implementation_defined::insert_commit_data insert_commit_data;
  99. //! @copydoc ::boost::intrusive::bstree::bstree()
  100. splaytree_impl()
  101. : tree_type()
  102. {}
  103. //! @copydoc ::boost::intrusive::bstree::bstree(const key_compare &,const value_traits &)
  104. explicit splaytree_impl( const key_compare &cmp, const value_traits &v_traits = value_traits())
  105. : tree_type(cmp, v_traits)
  106. {}
  107. //! @copydoc ::boost::intrusive::bstree::bstree(bool,Iterator,Iterator,const key_compare &,const value_traits &)
  108. template<class Iterator>
  109. splaytree_impl( bool unique, Iterator b, Iterator e
  110. , const key_compare &cmp = key_compare()
  111. , const value_traits &v_traits = value_traits())
  112. : tree_type(cmp, v_traits)
  113. {
  114. if(unique)
  115. this->insert_unique(b, e);
  116. else
  117. this->insert_equal(b, e);
  118. }
  119. //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&)
  120. splaytree_impl(BOOST_RV_REF(splaytree_impl) x)
  121. : tree_type(BOOST_MOVE_BASE(tree_type, x))
  122. {}
  123. //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&)
  124. splaytree_impl& operator=(BOOST_RV_REF(splaytree_impl) x)
  125. { return static_cast<splaytree_impl&>(tree_type::operator=(BOOST_MOVE_BASE(tree_type, x))); }
  126. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  127. //! @copydoc ::boost::intrusive::bstree::~bstree()
  128. ~splaytree_impl();
  129. //! @copydoc ::boost::intrusive::bstree::begin()
  130. iterator begin();
  131. //! @copydoc ::boost::intrusive::bstree::begin()const
  132. const_iterator begin() const;
  133. //! @copydoc ::boost::intrusive::bstree::cbegin()const
  134. const_iterator cbegin() const;
  135. //! @copydoc ::boost::intrusive::bstree::end()
  136. iterator end();
  137. //! @copydoc ::boost::intrusive::bstree::end()const
  138. const_iterator end() const;
  139. //! @copydoc ::boost::intrusive::bstree::cend()const
  140. const_iterator cend() const;
  141. //! @copydoc ::boost::intrusive::bstree::rbegin()
  142. reverse_iterator rbegin();
  143. //! @copydoc ::boost::intrusive::bstree::rbegin()const
  144. const_reverse_iterator rbegin() const;
  145. //! @copydoc ::boost::intrusive::bstree::crbegin()const
  146. const_reverse_iterator crbegin() const;
  147. //! @copydoc ::boost::intrusive::bstree::rend()
  148. reverse_iterator rend();
  149. //! @copydoc ::boost::intrusive::bstree::rend()const
  150. const_reverse_iterator rend() const;
  151. //! @copydoc ::boost::intrusive::bstree::crend()const
  152. const_reverse_iterator crend() const;
  153. //! @copydoc ::boost::intrusive::bstree::root()
  154. iterator root();
  155. //! @copydoc ::boost::intrusive::bstree::root()const
  156. const_iterator root() const;
  157. //! @copydoc ::boost::intrusive::bstree::croot()const
  158. const_iterator croot() const;
  159. //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator)
  160. static splaytree_impl &container_from_end_iterator(iterator end_iterator);
  161. //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator)
  162. static const splaytree_impl &container_from_end_iterator(const_iterator end_iterator);
  163. //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator)
  164. static splaytree_impl &container_from_iterator(iterator it);
  165. //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator)
  166. static const splaytree_impl &container_from_iterator(const_iterator it);
  167. //! @copydoc ::boost::intrusive::bstree::key_comp()const
  168. key_compare key_comp() const;
  169. //! @copydoc ::boost::intrusive::bstree::value_comp()const
  170. value_compare value_comp() const;
  171. //! @copydoc ::boost::intrusive::bstree::empty()const
  172. bool empty() const;
  173. //! @copydoc ::boost::intrusive::bstree::size()const
  174. size_type size() const;
  175. //! @copydoc ::boost::intrusive::bstree::swap
  176. void swap(splaytree_impl& other);
  177. //! @copydoc ::boost::intrusive::bstree::clone_from(const bstree&,Cloner,Disposer)
  178. //! Additional notes: it also copies the alpha factor from the source container.
  179. template <class Cloner, class Disposer>
  180. void clone_from(const splaytree_impl &src, Cloner cloner, Disposer disposer);
  181. #else //BOOST_INTRUSIVE_DOXYGEN_INVOKED
  182. using tree_type::clone_from;
  183. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  184. //! @copydoc ::boost::intrusive::bstree::clone_from(bstree&&,Cloner,Disposer)
  185. template <class Cloner, class Disposer>
  186. void clone_from(BOOST_RV_REF(splaytree_impl) src, Cloner cloner, Disposer disposer)
  187. { tree_type::clone_from(BOOST_MOVE_BASE(tree_type, src), cloner, disposer); }
  188. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  189. //! @copydoc ::boost::intrusive::bstree::insert_equal(reference)
  190. iterator insert_equal(reference value);
  191. //! @copydoc ::boost::intrusive::bstree::insert_equal(const_iterator,reference)
  192. iterator insert_equal(const_iterator hint, reference value);
  193. //! @copydoc ::boost::intrusive::bstree::insert_equal(Iterator,Iterator)
  194. template<class Iterator>
  195. void insert_equal(Iterator b, Iterator e);
  196. //! @copydoc ::boost::intrusive::bstree::insert_unique(reference)
  197. std::pair<iterator, bool> insert_unique(reference value);
  198. //! @copydoc ::boost::intrusive::bstree::insert_unique(const_iterator,reference)
  199. iterator insert_unique(const_iterator hint, reference value);
  200. //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const key_type&,insert_commit_data&)
  201. std::pair<iterator, bool> insert_unique_check
  202. (const key_type &key, insert_commit_data &commit_data);
  203. //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const key_type&,insert_commit_data&)
  204. std::pair<iterator, bool> insert_unique_check
  205. (const_iterator hint, const key_type &key, insert_commit_data &commit_data);
  206. //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const KeyType&,KeyTypeKeyCompare,insert_commit_data&)
  207. template<class KeyType, class KeyTypeKeyCompare>
  208. std::pair<iterator, bool> insert_unique_check
  209. (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data);
  210. //! @copydoc ::boost::intrusive::bstree::insert_unique_check(const_iterator,const KeyType&,KeyTypeKeyCompare,insert_commit_data&)
  211. template<class KeyType, class KeyTypeKeyCompare>
  212. std::pair<iterator, bool> insert_unique_check
  213. (const_iterator hint, const KeyType &key
  214. ,KeyTypeKeyCompare comp, insert_commit_data &commit_data);
  215. //! @copydoc ::boost::intrusive::bstree::insert_unique_commit
  216. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data);
  217. //! @copydoc ::boost::intrusive::bstree::insert_unique(Iterator,Iterator)
  218. template<class Iterator>
  219. void insert_unique(Iterator b, Iterator e);
  220. //! @copydoc ::boost::intrusive::bstree::insert_before
  221. iterator insert_before(const_iterator pos, reference value);
  222. //! @copydoc ::boost::intrusive::bstree::push_back
  223. void push_back(reference value);
  224. //! @copydoc ::boost::intrusive::bstree::push_front
  225. void push_front(reference value);
  226. //! @copydoc ::boost::intrusive::bstree::erase(const_iterator)
  227. iterator erase(const_iterator i);
  228. //! @copydoc ::boost::intrusive::bstree::erase(const_iterator,const_iterator)
  229. iterator erase(const_iterator b, const_iterator e);
  230. //! @copydoc ::boost::intrusive::bstree::erase(const key_type &)
  231. size_type erase(const key_type &key);
  232. //! @copydoc ::boost::intrusive::bstree::erase(const KeyType&,KeyTypeKeyCompare)
  233. template<class KeyType, class KeyTypeKeyCompare>
  234. size_type erase(const KeyType& key, KeyTypeKeyCompare comp);
  235. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,Disposer)
  236. template<class Disposer>
  237. iterator erase_and_dispose(const_iterator i, Disposer disposer);
  238. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const_iterator,const_iterator,Disposer)
  239. template<class Disposer>
  240. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer);
  241. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const key_type &, Disposer)
  242. template<class Disposer>
  243. size_type erase_and_dispose(const key_type &key, Disposer disposer);
  244. //! @copydoc ::boost::intrusive::bstree::erase_and_dispose(const KeyType&,KeyTypeKeyCompare,Disposer)
  245. template<class KeyType, class KeyTypeKeyCompare, class Disposer>
  246. size_type erase_and_dispose(const KeyType& key, KeyTypeKeyCompare comp, Disposer disposer);
  247. //! @copydoc ::boost::intrusive::bstree::clear
  248. void clear();
  249. //! @copydoc ::boost::intrusive::bstree::clear_and_dispose
  250. template<class Disposer>
  251. void clear_and_dispose(Disposer disposer);
  252. //! @copydoc ::boost::intrusive::bstree::count(const key_type &)const
  253. //! Additional note: non-const function, splaying is performed.
  254. size_type count(const key_type &key);
  255. //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyTypeKeyCompare)const
  256. //! Additional note: non-const function, splaying is performed.
  257. template<class KeyType, class KeyTypeKeyCompare>
  258. size_type count(const KeyType &key, KeyTypeKeyCompare comp);
  259. //! @copydoc ::boost::intrusive::bstree::count(const key_type &)const
  260. //! Additional note: const function, no splaying is performed
  261. size_type count(const key_type &key) const;
  262. //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyTypeKeyCompare)const
  263. //! Additional note: const function, no splaying is performed
  264. template<class KeyType, class KeyTypeKeyCompare>
  265. size_type count(const KeyType &key, KeyTypeKeyCompare comp) const;
  266. //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)
  267. //! Additional note: non-const function, splaying is performed.
  268. iterator lower_bound(const key_type &key);
  269. //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)const
  270. //! Additional note: const function, no splaying is performed
  271. const_iterator lower_bound(const key_type &key) const;
  272. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)
  273. //! Additional note: non-const function, splaying is performed for the first
  274. //! element of the equal range of "key"
  275. template<class KeyType, class KeyTypeKeyCompare>
  276. iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp);
  277. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)const
  278. //! Additional note: const function, no splaying is performed
  279. template<class KeyType, class KeyTypeKeyCompare>
  280. const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
  281. //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)
  282. //! Additional note: non-const function, splaying is performed for the first
  283. //! element of the equal range of "value"
  284. iterator upper_bound(const key_type &key);
  285. //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)const
  286. //! Additional note: const function, no splaying is performed
  287. const_iterator upper_bound(const key_type &key) const;
  288. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)
  289. //! Additional note: non-const function, splaying is performed for the first
  290. //! element of the equal range of "key"
  291. template<class KeyType, class KeyTypeKeyCompare>
  292. iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp);
  293. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)const
  294. //! Additional note: const function, no splaying is performed
  295. template<class KeyType, class KeyTypeKeyCompare>
  296. const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
  297. //! @copydoc ::boost::intrusive::bstree::find(const key_type &)
  298. //! Additional note: non-const function, splaying is performed for the first
  299. //! element of the equal range of "value"
  300. iterator find(const key_type &key);
  301. //! @copydoc ::boost::intrusive::bstree::find(const key_type &)const
  302. //! Additional note: const function, no splaying is performed
  303. const_iterator find(const key_type &key) const;
  304. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)
  305. //! Additional note: non-const function, splaying is performed for the first
  306. //! element of the equal range of "key"
  307. template<class KeyType, class KeyTypeKeyCompare>
  308. iterator find(const KeyType &key, KeyTypeKeyCompare comp);
  309. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)const
  310. //! Additional note: const function, no splaying is performed
  311. template<class KeyType, class KeyTypeKeyCompare>
  312. const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const;
  313. //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)
  314. //! Additional note: non-const function, splaying is performed for the first
  315. //! element of the equal range of "value"
  316. std::pair<iterator, iterator> equal_range(const key_type &key);
  317. //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)const
  318. //! Additional note: const function, no splaying is performed
  319. std::pair<const_iterator, const_iterator> equal_range(const key_type &key) const;
  320. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)
  321. //! Additional note: non-const function, splaying is performed for the first
  322. //! element of the equal range of "key"
  323. template<class KeyType, class KeyTypeKeyCompare>
  324. std::pair<iterator, iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp);
  325. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)const
  326. //! Additional note: const function, no splaying is performed
  327. template<class KeyType, class KeyTypeKeyCompare>
  328. std::pair<const_iterator, const_iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp) const;
  329. //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)
  330. std::pair<iterator,iterator> bounded_range
  331. (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed);
  332. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)
  333. template<class KeyType, class KeyTypeKeyCompare>
  334. std::pair<iterator,iterator> bounded_range
  335. (const KeyType& lower_key, const KeyType& upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed);
  336. //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)const
  337. std::pair<const_iterator, const_iterator> bounded_range
  338. (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const;
  339. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)const
  340. template<class KeyType, class KeyTypeKeyCompare>
  341. std::pair<const_iterator, const_iterator> bounded_range
  342. (const KeyType& lower_key, const KeyType& upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const;
  343. //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference)
  344. static iterator s_iterator_to(reference value);
  345. //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference)
  346. static const_iterator s_iterator_to(const_reference value);
  347. //! @copydoc ::boost::intrusive::bstree::iterator_to(reference)
  348. iterator iterator_to(reference value);
  349. //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const
  350. const_iterator iterator_to(const_reference value) const;
  351. //! @copydoc ::boost::intrusive::bstree::init_node(reference)
  352. static void init_node(reference value);
  353. //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance
  354. pointer unlink_leftmost_without_rebalance();
  355. //! @copydoc ::boost::intrusive::bstree::replace_node
  356. void replace_node(iterator replace_this, reference with_this);
  357. //! @copydoc ::boost::intrusive::bstree::remove_node
  358. void remove_node(reference value);
  359. //! @copydoc ::boost::intrusive::bstree::merge_unique(bstree<T, Options2...>&)
  360. template<class T, class ...Options2>
  361. void merge_unique(splaytree<T, Options2...> &);
  362. //! @copydoc ::boost::intrusive::bstree::merge_equal(bstree<T, Options2...>&)
  363. template<class T, class ...Options2>
  364. void merge_equal(splaytree<T, Options2...> &);
  365. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  366. //! <b>Requires</b>: i must be a valid iterator of *this.
  367. //!
  368. //! <b>Effects</b>: Rearranges the container so that the element pointed by i
  369. //! is placed as the root of the tree, improving future searches of this value.
  370. //!
  371. //! <b>Complexity</b>: Amortized logarithmic.
  372. //!
  373. //! <b>Throws</b>: Nothing.
  374. void splay_up(iterator i)
  375. { return node_algorithms::splay_up(i.pointed_node(), tree_type::header_ptr()); }
  376. //! <b>Effects</b>: Rearranges the container so that if *this stores an element
  377. //! with a key equivalent to value the element is placed as the root of the
  378. //! tree. If the element is not present returns the last node compared with the key.
  379. //! If the tree is empty, end() is returned.
  380. //!
  381. //! <b>Complexity</b>: Amortized logarithmic.
  382. //!
  383. //! <b>Returns</b>: An iterator to the new root of the tree, end() if the tree is empty.
  384. //!
  385. //! <b>Throws</b>: If the comparison functor throws.
  386. template<class KeyType, class KeyTypeKeyCompare>
  387. iterator splay_down(const KeyType &key, KeyTypeKeyCompare comp)
  388. {
  389. detail::key_nodeptr_comp<value_compare, value_traits>
  390. key_node_comp(comp, &this->get_value_traits());
  391. node_ptr r = node_algorithms::splay_down(tree_type::header_ptr(), key, key_node_comp);
  392. return iterator(r, this->priv_value_traits_ptr());
  393. }
  394. //! <b>Effects</b>: Rearranges the container so that if *this stores an element
  395. //! with a key equivalent to value the element is placed as the root of the
  396. //! tree.
  397. //!
  398. //! <b>Complexity</b>: Amortized logarithmic.
  399. //!
  400. //! <b>Returns</b>: An iterator to the new root of the tree, end() if the tree is empty.
  401. //!
  402. //! <b>Throws</b>: If the predicate throws.
  403. iterator splay_down(const key_type &key)
  404. { return this->splay_down(key, this->key_comp()); }
  405. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  406. //! @copydoc ::boost::intrusive::bstree::rebalance
  407. void rebalance();
  408. //! @copydoc ::boost::intrusive::bstree::rebalance_subtree
  409. iterator rebalance_subtree(iterator root);
  410. friend bool operator< (const splaytree_impl &x, const splaytree_impl &y);
  411. friend bool operator==(const splaytree_impl &x, const splaytree_impl &y);
  412. friend bool operator!= (const splaytree_impl &x, const splaytree_impl &y);
  413. friend bool operator>(const splaytree_impl &x, const splaytree_impl &y);
  414. friend bool operator<=(const splaytree_impl &x, const splaytree_impl &y);
  415. friend bool operator>=(const splaytree_impl &x, const splaytree_impl &y);
  416. friend void swap(splaytree_impl &x, splaytree_impl &y);
  417. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  418. };
  419. //! Helper metafunction to define a \c splaytree that yields to the same type when the
  420. //! same options (either explicitly or implicitly) are used.
  421. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  422. template<class T, class ...Options>
  423. #else
  424. template<class T, class O1 = void, class O2 = void
  425. , class O3 = void, class O4 = void
  426. , class O5 = void, class O6 = void>
  427. #endif
  428. struct make_splaytree
  429. {
  430. /// @cond
  431. typedef typename pack_options
  432. < splaytree_defaults,
  433. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  434. O1, O2, O3, O4, O5, O6
  435. #else
  436. Options...
  437. #endif
  438. >::type packed_options;
  439. typedef typename detail::get_value_traits
  440. <T, typename packed_options::proto_value_traits>::type value_traits;
  441. typedef splaytree_impl
  442. < value_traits
  443. , typename packed_options::key_of_value
  444. , typename packed_options::compare
  445. , typename packed_options::size_type
  446. , packed_options::constant_time_size
  447. , typename packed_options::header_holder_type
  448. > implementation_defined;
  449. /// @endcond
  450. typedef implementation_defined type;
  451. };
  452. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  453. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  454. template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
  455. #else
  456. template<class T, class ...Options>
  457. #endif
  458. class splaytree
  459. : public make_splaytree<T,
  460. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  461. O1, O2, O3, O4, O5, O6
  462. #else
  463. Options...
  464. #endif
  465. >::type
  466. {
  467. typedef typename make_splaytree
  468. <T,
  469. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  470. O1, O2, O3, O4, O5, O6
  471. #else
  472. Options...
  473. #endif
  474. >::type Base;
  475. BOOST_MOVABLE_BUT_NOT_COPYABLE(splaytree)
  476. public:
  477. typedef typename Base::key_compare key_compare;
  478. typedef typename Base::value_traits value_traits;
  479. typedef typename Base::iterator iterator;
  480. typedef typename Base::const_iterator const_iterator;
  481. typedef typename Base::reverse_iterator reverse_iterator;
  482. typedef typename Base::const_reverse_iterator const_reverse_iterator;
  483. //Assert if passed value traits are compatible with the type
  484. BOOST_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
  485. BOOST_INTRUSIVE_FORCEINLINE splaytree()
  486. : Base()
  487. {}
  488. BOOST_INTRUSIVE_FORCEINLINE explicit splaytree( const key_compare &cmp, const value_traits &v_traits = value_traits())
  489. : Base(cmp, v_traits)
  490. {}
  491. template<class Iterator>
  492. BOOST_INTRUSIVE_FORCEINLINE splaytree( bool unique, Iterator b, Iterator e
  493. , const key_compare &cmp = key_compare()
  494. , const value_traits &v_traits = value_traits())
  495. : Base(unique, b, e, cmp, v_traits)
  496. {}
  497. BOOST_INTRUSIVE_FORCEINLINE splaytree(BOOST_RV_REF(splaytree) x)
  498. : Base(BOOST_MOVE_BASE(Base, x))
  499. {}
  500. BOOST_INTRUSIVE_FORCEINLINE splaytree& operator=(BOOST_RV_REF(splaytree) x)
  501. { return static_cast<splaytree &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  502. template <class Cloner, class Disposer>
  503. BOOST_INTRUSIVE_FORCEINLINE void clone_from(const splaytree &src, Cloner cloner, Disposer disposer)
  504. { Base::clone_from(src, cloner, disposer); }
  505. template <class Cloner, class Disposer>
  506. BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(splaytree) src, Cloner cloner, Disposer disposer)
  507. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  508. BOOST_INTRUSIVE_FORCEINLINE static splaytree &container_from_end_iterator(iterator end_iterator)
  509. { return static_cast<splaytree &>(Base::container_from_end_iterator(end_iterator)); }
  510. BOOST_INTRUSIVE_FORCEINLINE static const splaytree &container_from_end_iterator(const_iterator end_iterator)
  511. { return static_cast<const splaytree &>(Base::container_from_end_iterator(end_iterator)); }
  512. BOOST_INTRUSIVE_FORCEINLINE static splaytree &container_from_iterator(iterator it)
  513. { return static_cast<splaytree &>(Base::container_from_iterator(it)); }
  514. BOOST_INTRUSIVE_FORCEINLINE static const splaytree &container_from_iterator(const_iterator it)
  515. { return static_cast<const splaytree &>(Base::container_from_iterator(it)); }
  516. };
  517. #endif
  518. } //namespace intrusive
  519. } //namespace boost
  520. #include <boost/intrusive/detail/config_end.hpp>
  521. #endif //BOOST_INTRUSIVE_SPLAYTREE_HPP