splaytree.hpp 28 KB

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