treap.hpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_TREAP_HPP
  13. #define BOOST_INTRUSIVE_TREAP_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <boost/intrusive/intrusive_fwd.hpp>
  16. #include <boost/intrusive/detail/assert.hpp>
  17. #include <boost/intrusive/bs_set_hook.hpp>
  18. #include <boost/intrusive/bstree.hpp>
  19. #include <boost/intrusive/detail/tree_node.hpp>
  20. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  21. #include <boost/intrusive/pointer_traits.hpp>
  22. #include <boost/intrusive/detail/get_value_traits.hpp>
  23. #include <boost/intrusive/detail/mpl.hpp>
  24. #include <boost/intrusive/treap_algorithms.hpp>
  25. #include <boost/intrusive/link_mode.hpp>
  26. #include <boost/intrusive/priority_compare.hpp>
  27. #include <boost/intrusive/detail/node_cloner_disposer.hpp>
  28. #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
  29. #include <boost/move/utility_core.hpp>
  30. #include <boost/move/adl_move_swap.hpp>
  31. #include <cstddef>
  32. #include <boost/intrusive/detail/minimal_less_equal_header.hpp>
  33. #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
  34. #if defined(BOOST_HAS_PRAGMA_ONCE)
  35. # pragma once
  36. #endif
  37. namespace boost {
  38. namespace intrusive {
  39. /// @cond
  40. struct treap_defaults
  41. : bstree_defaults
  42. {
  43. typedef void priority;
  44. typedef void priority_of_value;
  45. };
  46. template<class ValuePtr, class VoidOrPrioOfValue, class VoidOrPrioComp>
  47. struct treap_prio_types
  48. {
  49. typedef typename
  50. boost::movelib::pointer_element<ValuePtr>::type value_type;
  51. typedef typename get_key_of_value
  52. < VoidOrPrioOfValue, value_type>::type priority_of_value;
  53. typedef typename priority_of_value::type priority_type;
  54. typedef typename get_prio_comp< VoidOrPrioComp
  55. , priority_type
  56. >::type priority_compare;
  57. };
  58. struct treap_tag;
  59. /// @endcond
  60. //! The class template treap is an intrusive treap container that
  61. //! is used to construct intrusive set and multiset containers. The no-throw
  62. //! guarantee holds only, if the key_compare object and priority_compare object
  63. //! don't throw.
  64. //!
  65. //! The template parameter \c T is the type to be managed by the container.
  66. //! The user can specify additional options and if no options are provided
  67. //! default options are used.
  68. //!
  69. //! The container supports the following options:
  70. //! \c base_hook<>/member_hook<>/value_traits<>,
  71. //! \c constant_time_size<>, \c size_type<>,
  72. //! \c compare<>, \c priority<> and \c priority_of_value<>
  73. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  74. template<class T, class ...Options>
  75. #else
  76. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class VoidOrPrioOfValue, class VoidOrPrioComp, class SizeType, bool ConstantTimeSize, typename HeaderHolder>
  77. #endif
  78. class treap_impl
  79. /// @cond
  80. : public bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, BsTreeAlgorithms, HeaderHolder>
  81. //Use public inheritance to avoid MSVC bugs with closures
  82. , public detail::ebo_functor_holder
  83. < typename treap_prio_types<typename ValueTraits::pointer, VoidOrPrioOfValue, VoidOrPrioComp>::priority_compare
  84. , treap_tag>
  85. /// @endcond
  86. {
  87. public:
  88. typedef ValueTraits value_traits;
  89. /// @cond
  90. typedef bstree_impl< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType
  91. , ConstantTimeSize, BsTreeAlgorithms
  92. , HeaderHolder> tree_type;
  93. typedef tree_type implementation_defined;
  94. typedef treap_prio_types
  95. < typename ValueTraits::pointer
  96. , VoidOrPrioOfValue, VoidOrPrioComp> treap_prio_types_t;
  97. typedef detail::ebo_functor_holder
  98. <typename treap_prio_types_t::priority_compare, treap_tag> prio_base;
  99. /// @endcond
  100. typedef typename implementation_defined::pointer pointer;
  101. typedef typename implementation_defined::const_pointer const_pointer;
  102. typedef typename implementation_defined::value_type value_type;
  103. typedef typename implementation_defined::key_type key_type;
  104. typedef typename implementation_defined::key_of_value key_of_value;
  105. typedef typename implementation_defined::reference reference;
  106. typedef typename implementation_defined::const_reference const_reference;
  107. typedef typename implementation_defined::difference_type difference_type;
  108. typedef typename implementation_defined::size_type size_type;
  109. typedef typename implementation_defined::value_compare value_compare;
  110. typedef typename implementation_defined::key_compare key_compare;
  111. typedef typename implementation_defined::iterator iterator;
  112. typedef typename implementation_defined::const_iterator const_iterator;
  113. typedef typename implementation_defined::reverse_iterator reverse_iterator;
  114. typedef typename implementation_defined::const_reverse_iterator const_reverse_iterator;
  115. typedef typename implementation_defined::node_traits node_traits;
  116. typedef typename implementation_defined::node node;
  117. typedef typename implementation_defined::node_ptr node_ptr;
  118. typedef typename implementation_defined::const_node_ptr const_node_ptr;
  119. typedef BOOST_INTRUSIVE_IMPDEF(treap_algorithms<node_traits>) node_algorithms;
  120. typedef BOOST_INTRUSIVE_IMPDEF
  121. (typename treap_prio_types_t::priority_type) priority_type;
  122. typedef BOOST_INTRUSIVE_IMPDEF
  123. (typename treap_prio_types_t::priority_of_value) priority_of_value;
  124. typedef BOOST_INTRUSIVE_IMPDEF
  125. (typename treap_prio_types_t::priority_compare) priority_compare;
  126. static const bool constant_time_size = implementation_defined::constant_time_size;
  127. static const bool stateful_value_traits = implementation_defined::stateful_value_traits;
  128. static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
  129. typedef detail::key_nodeptr_comp<priority_compare, value_traits, priority_of_value> prio_node_prio_comp_t;
  130. template<class PrioPrioComp>
  131. detail::key_nodeptr_comp<PrioPrioComp, value_traits, priority_of_value> prio_node_prio_comp(PrioPrioComp priopriocomp) const
  132. { return detail::key_nodeptr_comp<PrioPrioComp, value_traits, priority_of_value>(priopriocomp, &this->get_value_traits()); }
  133. /// @cond
  134. private:
  135. //noncopyable
  136. BOOST_MOVABLE_BUT_NOT_COPYABLE(treap_impl)
  137. const priority_compare &priv_pcomp() const
  138. { return static_cast<const prio_base&>(*this).get(); }
  139. priority_compare &priv_pcomp()
  140. { return static_cast<prio_base&>(*this).get(); }
  141. /// @endcond
  142. public:
  143. typedef typename node_algorithms::insert_commit_data insert_commit_data;
  144. //! <b>Effects</b>: Constructs an empty container.
  145. //!
  146. //! <b>Complexity</b>: Constant.
  147. //!
  148. //! <b>Throws</b>: If value_traits::node_traits::node
  149. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  150. //! or the copy constructor of the value_compare/priority_compare objects throw. Basic guarantee.
  151. treap_impl()
  152. : tree_type(), prio_base()
  153. {}
  154. //! <b>Effects</b>: Constructs an empty container.
  155. //!
  156. //! <b>Complexity</b>: Constant.
  157. //!
  158. //! <b>Throws</b>: If value_traits::node_traits::node
  159. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  160. //! or the copy constructor of the value_compare/priority_compare objects throw. Basic guarantee.
  161. explicit treap_impl( const key_compare &cmp
  162. , const priority_compare &pcmp = priority_compare()
  163. , const value_traits &v_traits = value_traits())
  164. : tree_type(cmp, v_traits), prio_base(pcmp)
  165. {}
  166. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
  167. //! cmp must be a comparison function that induces a strict weak ordering.
  168. //!
  169. //! <b>Effects</b>: Constructs an empty container and inserts elements from
  170. //! [b, e).
  171. //!
  172. //! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
  173. //! comp and otherwise N * log N, where N is the distance between first and last.
  174. //!
  175. //! <b>Throws</b>: If value_traits::node_traits::node
  176. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  177. //! or the copy constructor/operator() of the key_compare/priority_compare objects
  178. //! throw. Basic guarantee.
  179. template<class Iterator>
  180. treap_impl( bool unique, Iterator b, Iterator e
  181. , const key_compare &cmp = key_compare()
  182. , const priority_compare &pcmp = priority_compare()
  183. , const value_traits &v_traits = value_traits())
  184. : tree_type(cmp, v_traits), prio_base(pcmp)
  185. {
  186. if(unique)
  187. this->insert_unique(b, e);
  188. else
  189. this->insert_equal(b, e);
  190. }
  191. //! @copydoc ::boost::intrusive::bstree::bstree(bstree &&)
  192. treap_impl(BOOST_RV_REF(treap_impl) x)
  193. : tree_type(BOOST_MOVE_BASE(tree_type, x))
  194. , prio_base(::boost::move(x.priv_pcomp()))
  195. {}
  196. //! @copydoc ::boost::intrusive::bstree::operator=(bstree &&)
  197. treap_impl& operator=(BOOST_RV_REF(treap_impl) x)
  198. { this->swap(x); return *this; }
  199. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  200. //! @copydoc ::boost::intrusive::bstree::~bstree()
  201. ~treap_impl();
  202. //! @copydoc ::boost::intrusive::bstree::begin()
  203. iterator begin() BOOST_NOEXCEPT;
  204. //! @copydoc ::boost::intrusive::bstree::begin()const
  205. const_iterator begin() const BOOST_NOEXCEPT;
  206. //! @copydoc ::boost::intrusive::bstree::cbegin()const
  207. const_iterator cbegin() const BOOST_NOEXCEPT;
  208. //! @copydoc ::boost::intrusive::bstree::end()
  209. iterator end() BOOST_NOEXCEPT;
  210. //! @copydoc ::boost::intrusive::bstree::end()const
  211. const_iterator end() const BOOST_NOEXCEPT;
  212. //! @copydoc ::boost::intrusive::bstree::cend()const
  213. const_iterator cend() const BOOST_NOEXCEPT;
  214. #endif
  215. //! <b>Effects</b>: Returns an iterator pointing to the highest priority object of the treap.
  216. //!
  217. //! <b>Complexity</b>: Constant.
  218. //!
  219. //! <b>Throws</b>: Nothing.
  220. inline iterator top() BOOST_NOEXCEPT
  221. { return this->tree_type::root(); }
  222. //! <b>Effects</b>: Returns a const_iterator pointing to the highest priority object of the treap..
  223. //!
  224. //! <b>Complexity</b>: Constant.
  225. //!
  226. //! <b>Throws</b>: Nothing.
  227. inline const_iterator top() const BOOST_NOEXCEPT
  228. { return this->ctop(); }
  229. //! <b>Effects</b>: Returns a const_iterator pointing to the highest priority object of the treap..
  230. //!
  231. //! <b>Complexity</b>: Constant.
  232. //!
  233. //! <b>Throws</b>: Nothing.
  234. inline const_iterator ctop() const BOOST_NOEXCEPT
  235. { return this->tree_type::root(); }
  236. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  237. //! @copydoc ::boost::intrusive::bstree::rbegin()
  238. reverse_iterator rbegin() BOOST_NOEXCEPT;
  239. //! @copydoc ::boost::intrusive::bstree::rbegin()const
  240. const_reverse_iterator rbegin() const BOOST_NOEXCEPT;
  241. //! @copydoc ::boost::intrusive::bstree::crbegin()const
  242. const_reverse_iterator crbegin() const BOOST_NOEXCEPT;
  243. //! @copydoc ::boost::intrusive::bstree::rend()
  244. reverse_iterator rend() BOOST_NOEXCEPT;
  245. //! @copydoc ::boost::intrusive::bstree::rend()const
  246. const_reverse_iterator rend() const BOOST_NOEXCEPT;
  247. //! @copydoc ::boost::intrusive::bstree::crend()const
  248. const_reverse_iterator crend() const BOOST_NOEXCEPT;
  249. //! @copydoc ::boost::intrusive::bstree::root()
  250. iterator root() BOOST_NOEXCEPT;
  251. //! @copydoc ::boost::intrusive::bstree::root()const
  252. const_iterator root() const BOOST_NOEXCEPT;
  253. //! @copydoc ::boost::intrusive::bstree::croot()const
  254. const_iterator croot() const BOOST_NOEXCEPT;
  255. #endif
  256. //! <b>Effects</b>: Returns a reverse_iterator pointing to the highest priority object of the
  257. //! reversed treap.
  258. //!
  259. //! <b>Complexity</b>: Constant.
  260. //!
  261. //! <b>Throws</b>: Nothing.
  262. inline reverse_iterator rtop() BOOST_NOEXCEPT
  263. { return reverse_iterator(this->top()); }
  264. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the highest priority objec
  265. //! of the reversed treap.
  266. //!
  267. //! <b>Complexity</b>: Constant.
  268. //!
  269. //! <b>Throws</b>: Nothing.
  270. inline const_reverse_iterator rtop() const BOOST_NOEXCEPT
  271. { return const_reverse_iterator(this->top()); }
  272. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the highest priority object
  273. //! of the reversed treap.
  274. //!
  275. //! <b>Complexity</b>: Constant.
  276. //!
  277. //! <b>Throws</b>: Nothing.
  278. inline const_reverse_iterator crtop() const BOOST_NOEXCEPT
  279. { return const_reverse_iterator(this->top()); }
  280. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  281. //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(iterator)
  282. static treap_impl &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT;
  283. //! @copydoc ::boost::intrusive::bstree::container_from_end_iterator(const_iterator)
  284. static const treap_impl &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT;
  285. //! @copydoc ::boost::intrusive::bstree::container_from_iterator(iterator)
  286. static treap_impl &container_from_iterator(iterator it) BOOST_NOEXCEPT;
  287. //! @copydoc ::boost::intrusive::bstree::container_from_iterator(const_iterator)
  288. static const treap_impl &container_from_iterator(const_iterator it) BOOST_NOEXCEPT;
  289. //! @copydoc ::boost::intrusive::bstree::key_comp()const
  290. key_compare key_comp() const;
  291. //! @copydoc ::boost::intrusive::bstree::value_comp()const
  292. value_compare value_comp() const;
  293. //! @copydoc ::boost::intrusive::bstree::empty()const
  294. bool empty() const BOOST_NOEXCEPT;
  295. //! @copydoc ::boost::intrusive::bstree::size()const
  296. size_type size() const BOOST_NOEXCEPT;
  297. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  298. //! <b>Effects</b>: Returns the priority_compare object used by the container.
  299. //!
  300. //! <b>Complexity</b>: Constant.
  301. //!
  302. //! <b>Throws</b>: If priority_compare copy-constructor throws.
  303. priority_compare priority_comp() const
  304. { return this->priv_pcomp(); }
  305. //! <b>Effects</b>: Swaps the contents of two treaps.
  306. //!
  307. //! <b>Complexity</b>: Constant.
  308. //!
  309. //! <b>Throws</b>: If the comparison functor's swap call throws.
  310. void swap(treap_impl& other)
  311. {
  312. //This can throw
  313. ::boost::adl_move_swap(this->priv_pcomp(), other.priv_pcomp());
  314. tree_type::swap(other);
  315. }
  316. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  317. //! Cloner should yield to nodes equivalent to the original nodes.
  318. //!
  319. //! <b>Effects</b>: Erases all the elements from *this
  320. //! calling Disposer::operator()(pointer), clones all the
  321. //! elements from src calling Cloner::operator()(const_reference )
  322. //! and inserts them on *this. Copies the predicate from the source container.
  323. //!
  324. //! If cloner throws, all cloned elements are unlinked and disposed
  325. //! calling Disposer::operator()(pointer).
  326. //!
  327. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  328. //!
  329. //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
  330. template <class Cloner, class Disposer>
  331. void clone_from(const treap_impl &src, Cloner cloner, Disposer disposer)
  332. {
  333. tree_type::clone_from(src, cloner, disposer);
  334. this->priv_pcomp() = src.priv_pcomp();
  335. }
  336. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  337. //! Cloner should yield to nodes equivalent to the original nodes.
  338. //!
  339. //! <b>Effects</b>: Erases all the elements from *this
  340. //! calling Disposer::operator()(pointer), clones all the
  341. //! elements from src calling Cloner::operator()(reference)
  342. //! and inserts them on *this. Copies the predicate from the source container.
  343. //!
  344. //! If cloner throws, all cloned elements are unlinked and disposed
  345. //! calling Disposer::operator()(pointer).
  346. //!
  347. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  348. //!
  349. //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
  350. template <class Cloner, class Disposer>
  351. void clone_from(BOOST_RV_REF(treap_impl) src, Cloner cloner, Disposer disposer)
  352. {
  353. tree_type::clone_from(BOOST_MOVE_BASE(tree_type, src), cloner, disposer);
  354. this->priv_pcomp() = ::boost::move(src.priv_pcomp());
  355. }
  356. //! <b>Requires</b>: value must be an lvalue
  357. //!
  358. //! <b>Effects</b>: Inserts value into the container before the upper bound.
  359. //!
  360. //! <b>Complexity</b>: Average complexity for insert element is at
  361. //! most logarithmic.
  362. //!
  363. //! <b>Throws</b>: If the internal key_compare or priority_compare functions throw. Strong guarantee.
  364. //!
  365. //! <b>Note</b>: Does not affect the validity of iterators and references.
  366. //! No copy-constructors are called.
  367. iterator insert_equal(reference value)
  368. {
  369. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  370. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  371. iterator ret
  372. ( node_algorithms::insert_equal_upper_bound
  373. ( this->tree_type::header_ptr()
  374. , to_insert
  375. , this->key_node_comp(this->key_comp())
  376. , this->prio_node_prio_comp(this->priv_pcomp()))
  377. , this->priv_value_traits_ptr());
  378. this->tree_type::sz_traits().increment();
  379. return ret;
  380. }
  381. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  382. //! a valid iterator.
  383. //!
  384. //! <b>Effects</b>: Inserts x into the container, using "hint" as a hint to
  385. //! where it will be inserted. If "hint" is the upper_bound
  386. //! the insertion takes constant time (two comparisons in the worst case)
  387. //!
  388. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  389. //! constant time if t is inserted immediately before hint.
  390. //!
  391. //! <b>Throws</b>: If the internal key_compare or priority_compare functions throw. Strong guarantee.
  392. //!
  393. //! <b>Note</b>: Does not affect the validity of iterators and references.
  394. //! No copy-constructors are called.
  395. iterator insert_equal(const_iterator hint, reference value)
  396. {
  397. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  398. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  399. iterator ret
  400. (node_algorithms::insert_equal
  401. ( this->tree_type::header_ptr()
  402. , hint.pointed_node()
  403. , to_insert
  404. , this->key_node_comp(this->key_comp())
  405. , this->prio_node_prio_comp(this->priv_pcomp()))
  406. , this->priv_value_traits_ptr());
  407. this->tree_type::sz_traits().increment();
  408. return ret;
  409. }
  410. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  411. //! of type value_type.
  412. //!
  413. //! <b>Effects</b>: Inserts a each element of a range into the container
  414. //! before the upper bound of the key of each element.
  415. //!
  416. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  417. //! size of the range. However, it is linear in N if the range is already sorted
  418. //! by key_comp().
  419. //!
  420. //! <b>Throws</b>: If the internal key_compare or priority_compare functions throw.
  421. //! Strong guarantee.
  422. //!
  423. //! <b>Note</b>: Does not affect the validity of iterators and references.
  424. //! No copy-constructors are called.
  425. template<class Iterator>
  426. void insert_equal(Iterator b, Iterator e)
  427. {
  428. iterator iend(this->end());
  429. for (; b != e; ++b)
  430. this->insert_equal(iend, *b);
  431. }
  432. //! <b>Requires</b>: value must be an lvalue
  433. //!
  434. //! <b>Effects</b>: Inserts value into the container if the value
  435. //! is not already present.
  436. //!
  437. //! <b>Complexity</b>: Average complexity for insert element is at
  438. //! most logarithmic.
  439. //!
  440. //! <b>Throws</b>: If the internal key_compare or priority_compare functions throw.
  441. //! Strong guarantee.
  442. //!
  443. //! <b>Note</b>: Does not affect the validity of iterators and references.
  444. //! No copy-constructors are called.
  445. std::pair<iterator, bool> insert_unique(reference value)
  446. {
  447. insert_commit_data commit_data;
  448. std::pair<iterator, bool> ret = this->insert_unique_check(key_of_value()(value), priority_of_value()(value), commit_data);
  449. if(!ret.second)
  450. return ret;
  451. return std::pair<iterator, bool> (this->insert_unique_commit(value, commit_data), true);
  452. }
  453. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  454. //! a valid iterator
  455. //!
  456. //! <b>Effects</b>: Tries to insert x into the container, using "hint" as a hint
  457. //! to where it will be inserted.
  458. //!
  459. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  460. //! constant time (two comparisons in the worst case)
  461. //! if t is inserted immediately before hint.
  462. //!
  463. //! <b>Throws</b>: If the internal key_compare or priority_compare functions throw.
  464. //! Strong guarantee.
  465. //!
  466. //! <b>Note</b>: Does not affect the validity of iterators and references.
  467. //! No copy-constructors are called.
  468. iterator insert_unique(const_iterator hint, reference value)
  469. {
  470. insert_commit_data commit_data;
  471. std::pair<iterator, bool> ret = this->insert_unique_check(hint, key_of_value()(value), priority_of_value()(value), commit_data);
  472. if(!ret.second)
  473. return ret.first;
  474. return this->insert_unique_commit(value, commit_data);
  475. }
  476. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  477. //! of type value_type.
  478. //!
  479. //! <b>Effects</b>: Tries to insert each element of a range into the container.
  480. //!
  481. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  482. //! size of the range. However, it is linear in N if the range is already sorted
  483. //! by key_comp().
  484. //!
  485. //! <b>Throws</b>: If the internal key_compare or priority_compare functions throw.
  486. //! Strong guarantee.
  487. //!
  488. //! <b>Note</b>: Does not affect the validity of iterators and references.
  489. //! No copy-constructors are called.
  490. template<class Iterator>
  491. void insert_unique(Iterator b, Iterator e)
  492. {
  493. if(this->empty()){
  494. iterator iend(this->end());
  495. for (; b != e; ++b)
  496. this->insert_unique(iend, *b);
  497. }
  498. else{
  499. for (; b != e; ++b)
  500. this->insert_unique(*b);
  501. }
  502. }
  503. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  504. //! a user provided key instead of the value itself.
  505. //!
  506. //! <b>Returns</b>: If there is an equivalent value
  507. //! returns a pair containing an iterator to the already present value
  508. //! and false. If the value can be inserted returns true in the returned
  509. //! pair boolean and fills "commit_data" that is meant to be used with
  510. //! the "insert_commit" function.
  511. //!
  512. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  513. //!
  514. //! <b>Throws</b>: If the comparison or predicate functions throw. Strong guarantee.
  515. //!
  516. //! <b>Notes</b>: This function is used to improve performance when constructing
  517. //! a value_type is expensive: if there is an equivalent value
  518. //! the constructed object must be discarded. Many times, the part of the
  519. //! node that is used to impose the order is much cheaper to construct
  520. //! than the value_type and this function offers the possibility to use that
  521. //! part to check if the insertion will be successful.
  522. //!
  523. //! If the check is successful, the user can construct the value_type and use
  524. //! "insert_commit" to insert the object in constant-time. This gives a total
  525. //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
  526. //!
  527. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  528. //! objects are inserted or erased from the container.
  529. std::pair<iterator, bool> insert_unique_check
  530. ( const key_type &key, const priority_type &prio, insert_commit_data &commit_data)
  531. { return this->insert_unique_check(key, this->key_comp(), prio, this->priv_pcomp(), commit_data); }
  532. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  533. //! a user provided key instead of the value itself, using "hint"
  534. //! as a hint to where it will be inserted.
  535. //!
  536. //! <b>Returns</b>: If there is an equivalent value
  537. //! returns a pair containing an iterator to the already present value
  538. //! and false. If the value can be inserted returns true in the returned
  539. //! pair boolean and fills "commit_data" that is meant to be used with
  540. //! the "insert_commit" function.
  541. //!
  542. //! <b>Complexity</b>: Logarithmic in general, but it's amortized
  543. //! constant time if t is inserted immediately before hint.
  544. //!
  545. //! <b>Throws</b>: If the comparison or predicate functions throw. Strong guarantee.
  546. //!
  547. //! <b>Notes</b>: This function is used to improve performance when constructing
  548. //! a value_type is expensive: if there is an equivalent value
  549. //! the constructed object must be discarded. Many times, the part of the
  550. //! constructing that is used to impose the order is much cheaper to construct
  551. //! than the value_type and this function offers the possibility to use that key
  552. //! to check if the insertion will be successful.
  553. //!
  554. //! If the check is successful, the user can construct the value_type and use
  555. //! "insert_commit" to insert the object in constant-time. This can give a total
  556. //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)).
  557. //!
  558. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  559. //! objects are inserted or erased from the container.
  560. std::pair<iterator, bool> insert_unique_check
  561. ( const_iterator hint, const key_type &key, const priority_type &prio, insert_commit_data &commit_data)
  562. { return this->insert_unique_check(hint, key, this->key_comp(), prio, this->priv_pcomp(), commit_data); }
  563. //! <b>Requires</b>: comp must be a comparison function that induces
  564. //! the same strict weak ordering as key_compare.
  565. //! prio_value_pcomp must be a comparison function that induces
  566. //! the same strict weak ordering as priority_compare. The difference is that
  567. //! prio_value_pcomp and comp compare an arbitrary key/priority with the contained values.
  568. //!
  569. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  570. //! a user provided key instead of the value itself.
  571. //!
  572. //! <b>Returns</b>: If there is an equivalent value
  573. //! returns a pair containing an iterator to the already present value
  574. //! and false. If the value can be inserted returns true in the returned
  575. //! pair boolean and fills "commit_data" that is meant to be used with
  576. //! the "insert_commit" function.
  577. //!
  578. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  579. //!
  580. //! <b>Throws</b>: If the comp or prio_value_pcomp
  581. //! ordering functions throw. Strong guarantee.
  582. //!
  583. //! <b>Notes</b>: This function is used to improve performance when constructing
  584. //! a value_type is expensive: if there is an equivalent value
  585. //! the constructed object must be discarded. Many times, the part of the
  586. //! node that is used to impose the order is much cheaper to construct
  587. //! than the value_type and this function offers the possibility to use that
  588. //! part to check if the insertion will be successful.
  589. //!
  590. //! If the check is successful, the user can construct the value_type and use
  591. //! "insert_commit" to insert the object in constant-time. This gives a total
  592. //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
  593. //!
  594. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  595. //! objects are inserted or erased from the container.
  596. template<class KeyType, class KeyTypeKeyCompare, class PrioType, class PrioValuePrioCompare>
  597. BOOST_INTRUSIVE_DOC1ST(std::pair<iterator BOOST_INTRUSIVE_I bool>
  598. , typename detail::disable_if_convertible
  599. <KeyType BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I
  600. std::pair<iterator BOOST_INTRUSIVE_I bool> >::type)
  601. insert_unique_check
  602. ( const KeyType &key, KeyTypeKeyCompare comp
  603. , const PrioType &prio, PrioValuePrioCompare prio_value_pcomp, insert_commit_data &commit_data)
  604. {
  605. std::pair<node_ptr, bool> const ret =
  606. (node_algorithms::insert_unique_check
  607. ( this->tree_type::header_ptr()
  608. , key, this->key_node_comp(comp)
  609. , prio, this->prio_node_prio_comp(prio_value_pcomp)
  610. , commit_data));
  611. return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
  612. }
  613. //! <b>Requires</b>: comp must be a comparison function that induces
  614. //! the same strict weak ordering as key_compare.
  615. //! prio_value_pcomp must be a comparison function that induces
  616. //! the same strict weak ordering as priority_compare. The difference is that
  617. //! prio_value_pcomp and comp compare an arbitrary key/priority with the contained values.
  618. //!
  619. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  620. //! a user provided key instead of the value itself, using "hint"
  621. //! as a hint to where it will be inserted.
  622. //!
  623. //! <b>Returns</b>: If there is an equivalent value
  624. //! returns a pair containing an iterator to the already present value
  625. //! and false. If the value can be inserted returns true in the returned
  626. //! pair boolean and fills "commit_data" that is meant to be used with
  627. //! the "insert_commit" function.
  628. //!
  629. //! <b>Complexity</b>: Logarithmic in general, but it's amortized
  630. //! constant time if t is inserted immediately before hint.
  631. //!
  632. //! <b>Throws</b>: If the comp or prio_value_pcomp
  633. //! ordering functions throw. Strong guarantee.
  634. //!
  635. //! <b>Notes</b>: This function is used to improve performance when constructing
  636. //! a value_type is expensive: if there is an equivalent value
  637. //! the constructed object must be discarded. Many times, the part of the
  638. //! constructing that is used to impose the order is much cheaper to construct
  639. //! than the value_type and this function offers the possibility to use that key
  640. //! to check if the insertion will be successful.
  641. //!
  642. //! If the check is successful, the user can construct the value_type and use
  643. //! "insert_commit" to insert the object in constant-time. This can give a total
  644. //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)).
  645. //!
  646. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  647. //! objects are inserted or erased from the container.
  648. template<class KeyType, class KeyTypeKeyCompare, class PrioType, class PrioValuePrioCompare>
  649. std::pair<iterator, bool> insert_unique_check
  650. ( const_iterator hint
  651. , const KeyType &key
  652. , KeyTypeKeyCompare comp
  653. , const PrioType &prio
  654. , PrioValuePrioCompare prio_value_pcomp
  655. , insert_commit_data &commit_data)
  656. {
  657. std::pair<node_ptr, bool> const ret =
  658. (node_algorithms::insert_unique_check
  659. ( this->tree_type::header_ptr(), hint.pointed_node()
  660. , key, this->key_node_comp(comp)
  661. , prio, this->prio_node_prio_comp(prio_value_pcomp)
  662. , commit_data));
  663. return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
  664. }
  665. //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
  666. //! must have been obtained from a previous call to "insert_check".
  667. //! No objects should have been inserted or erased from the container between
  668. //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
  669. //!
  670. //! <b>Effects</b>: Inserts the value in the avl_set using the information obtained
  671. //! from the "commit_data" that a previous "insert_check" filled.
  672. //!
  673. //! <b>Returns</b>: An iterator to the newly inserted object.
  674. //!
  675. //! <b>Complexity</b>: Constant time.
  676. //!
  677. //! <b>Throws</b>: Nothing
  678. //!
  679. //! <b>Notes</b>: This function has only sense if a "insert_check" has been
  680. //! previously executed to fill "commit_data". No value should be inserted or
  681. //! erased between the "insert_check" and "insert_commit" calls.
  682. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) BOOST_NOEXCEPT
  683. {
  684. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  685. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  686. node_algorithms::insert_unique_commit(this->tree_type::header_ptr(), to_insert, commit_data);
  687. this->tree_type::sz_traits().increment();
  688. return iterator(to_insert, this->priv_value_traits_ptr());
  689. }
  690. //! <b>Requires</b>: value must be an lvalue, "pos" must be
  691. //! a valid iterator (or end) and must be the succesor of value
  692. //! once inserted according to the predicate
  693. //!
  694. //! <b>Effects</b>: Inserts x into the container before "pos".
  695. //!
  696. //! <b>Complexity</b>: Constant time.
  697. //!
  698. //! <b>Throws</b>: If the internal priority_compare function throws. Strong guarantee.
  699. //!
  700. //! <b>Note</b>: This function does not check preconditions so if "pos" is not
  701. //! the successor of "value" container ordering invariant will be broken.
  702. //! This is a low-level function to be used only for performance reasons
  703. //! by advanced users.
  704. iterator insert_before(const_iterator pos, reference value) BOOST_NOEXCEPT
  705. {
  706. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  707. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  708. iterator ret
  709. ( node_algorithms::insert_before
  710. ( this->tree_type::header_ptr()
  711. , pos.pointed_node()
  712. , to_insert
  713. , this->prio_node_prio_comp(this->priv_pcomp())
  714. )
  715. , this->priv_value_traits_ptr());
  716. this->tree_type::sz_traits().increment();
  717. return ret;
  718. }
  719. //! <b>Requires</b>: value must be an lvalue, and it must be no less
  720. //! than the greatest inserted key
  721. //!
  722. //! <b>Effects</b>: Inserts x into the container in the last position.
  723. //!
  724. //! <b>Complexity</b>: Constant time.
  725. //!
  726. //! <b>Throws</b>: If the internal priority_compare function throws. Strong guarantee.
  727. //!
  728. //! <b>Note</b>: This function does not check preconditions so if value is
  729. //! less than the greatest inserted key container ordering invariant will be broken.
  730. //! This function is slightly more efficient than using "insert_before".
  731. //! This is a low-level function to be used only for performance reasons
  732. //! by advanced users.
  733. void push_back(reference value) BOOST_NOEXCEPT
  734. {
  735. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  736. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  737. node_algorithms::push_back
  738. (this->tree_type::header_ptr(), to_insert, this->prio_node_prio_comp(this->priv_pcomp()));
  739. this->tree_type::sz_traits().increment();
  740. }
  741. //! <b>Requires</b>: value must be an lvalue, and it must be no greater
  742. //! than the minimum inserted key
  743. //!
  744. //! <b>Effects</b>: Inserts x into the container in the first position.
  745. //!
  746. //! <b>Complexity</b>: Constant time.
  747. //!
  748. //! <b>Throws</b>: If the internal priority_compare function throws. Strong guarantee.
  749. //!
  750. //! <b>Note</b>: This function does not check preconditions so if value is
  751. //! greater than the minimum inserted key container ordering invariant will be broken.
  752. //! This function is slightly more efficient than using "insert_before".
  753. //! This is a low-level function to be used only for performance reasons
  754. //! by advanced users.
  755. void push_front(reference value) BOOST_NOEXCEPT
  756. {
  757. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  758. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  759. node_algorithms::push_front
  760. (this->tree_type::header_ptr(), to_insert, this->prio_node_prio_comp(this->priv_pcomp()));
  761. this->tree_type::sz_traits().increment();
  762. }
  763. //! <b>Effects</b>: Erases the element pointed to by i.
  764. //!
  765. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  766. //!
  767. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  768. //!
  769. //! <b>Note</b>: Invalidates the iterators (but not the references)
  770. //! to the erased elements. No destructors are called.
  771. iterator erase(const_iterator i) BOOST_NOEXCEPT
  772. {
  773. const_iterator ret(i);
  774. ++ret;
  775. node_ptr to_erase(i.pointed_node());
  776. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(to_erase));
  777. node_algorithms::erase
  778. (this->tree_type::header_ptr(), to_erase, this->prio_node_prio_comp(this->priv_pcomp()));
  779. this->tree_type::sz_traits().decrement();
  780. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  781. node_algorithms::init(to_erase);
  782. return ret.unconst();
  783. }
  784. //! <b>Effects</b>: Erases the range pointed to by b end e.
  785. //!
  786. //! <b>Complexity</b>: Average complexity for erase range is at most
  787. //! O(log(size() + N)), where N is the number of elements in the range.
  788. //!
  789. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  790. //!
  791. //! <b>Note</b>: Invalidates the iterators (but not the references)
  792. //! to the erased elements. No destructors are called.
  793. iterator erase(const_iterator b, const_iterator e) BOOST_NOEXCEPT
  794. { size_type n; return private_erase(b, e, n); }
  795. //! <b>Effects</b>: Erases all the elements with the given value.
  796. //!
  797. //! <b>Returns</b>: The number of erased elements.
  798. //!
  799. //! <b>Complexity</b>: O(log(size() + N).
  800. //!
  801. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  802. //!
  803. //! <b>Note</b>: Invalidates the iterators (but not the references)
  804. //! to the erased elements. No destructors are called.
  805. size_type erase(const key_type &key)
  806. { return this->erase(key, this->key_comp()); }
  807. //! <b>Effects</b>: Erases all the elements with the given key.
  808. //! according to the comparison functor "comp".
  809. //!
  810. //! <b>Returns</b>: The number of erased elements.
  811. //!
  812. //! <b>Complexity</b>: O(log(size() + N).
  813. //!
  814. //! <b>Throws</b>: if the internal priority_compare function throws.
  815. //! Equivalent guarantee to <i>while(beg != end) erase(beg++);</i>
  816. //!
  817. //! <b>Note</b>: Invalidates the iterators (but not the references)
  818. //! to the erased elements. No destructors are called.
  819. template<class KeyType, class KeyTypeKeyCompare>
  820. BOOST_INTRUSIVE_DOC1ST(size_type
  821. , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
  822. erase(const KeyType& key, KeyTypeKeyCompare comp)
  823. {
  824. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  825. size_type n;
  826. private_erase(p.first, p.second, n);
  827. return n;
  828. }
  829. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  830. //!
  831. //! <b>Effects</b>: Erases the element pointed to by i.
  832. //! Disposer::operator()(pointer) is called for the removed element.
  833. //!
  834. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  835. //!
  836. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  837. //!
  838. //! <b>Note</b>: Invalidates the iterators
  839. //! to the erased elements.
  840. template<class Disposer>
  841. iterator erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
  842. {
  843. node_ptr to_erase(i.pointed_node());
  844. iterator ret(this->erase(i));
  845. disposer(this->get_value_traits().to_value_ptr(to_erase));
  846. return ret;
  847. }
  848. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  849. template<class Disposer>
  850. iterator erase_and_dispose(iterator i, Disposer disposer) BOOST_NOEXCEPT
  851. { return this->erase_and_dispose(const_iterator(i), disposer); }
  852. #endif
  853. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  854. //!
  855. //! <b>Effects</b>: Erases the range pointed to by b end e.
  856. //! Disposer::operator()(pointer) is called for the removed elements.
  857. //!
  858. //! <b>Complexity</b>: Average complexity for erase range is at most
  859. //! O(log(size() + N)), where N is the number of elements in the range.
  860. //!
  861. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  862. //!
  863. //! <b>Note</b>: Invalidates the iterators
  864. //! to the erased elements.
  865. template<class Disposer>
  866. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) BOOST_NOEXCEPT
  867. { size_type n; return private_erase(b, e, n, disposer); }
  868. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  869. //!
  870. //! <b>Effects</b>: Erases all the elements with the given value.
  871. //! Disposer::operator()(pointer) is called for the removed elements.
  872. //!
  873. //! <b>Returns</b>: The number of erased elements.
  874. //!
  875. //! <b>Complexity</b>: O(log(size() + N).
  876. //!
  877. //! <b>Throws</b>: if the priority_compare function throws then weak guarantee and heap invariants are broken.
  878. //! The safest thing would be to clear or destroy the container.
  879. //!
  880. //! <b>Note</b>: Invalidates the iterators (but not the references)
  881. //! to the erased elements. No destructors are called.
  882. template<class Disposer>
  883. size_type erase_and_dispose(const key_type &key, Disposer disposer)
  884. {
  885. std::pair<iterator,iterator> p = this->equal_range(key);
  886. size_type n;
  887. private_erase(p.first, p.second, n, disposer);
  888. return n;
  889. }
  890. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  891. //!
  892. //! <b>Effects</b>: Erases all the elements with the given key.
  893. //! according to the comparison functor "comp".
  894. //! Disposer::operator()(pointer) is called for the removed elements.
  895. //!
  896. //! <b>Returns</b>: The number of erased elements.
  897. //!
  898. //! <b>Complexity</b>: O(log(size() + N).
  899. //!
  900. //! <b>Throws</b>: if the priority_compare function throws then weak guarantee and heap invariants are broken.
  901. //! The safest thing would be to clear or destroy the container.
  902. //!
  903. //! <b>Note</b>: Invalidates the iterators
  904. //! to the erased elements.
  905. template<class KeyType, class KeyTypeKeyCompare, class Disposer>
  906. BOOST_INTRUSIVE_DOC1ST(size_type
  907. , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
  908. erase_and_dispose(const KeyType& key, KeyTypeKeyCompare comp, Disposer disposer)
  909. {
  910. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  911. size_type n;
  912. private_erase(p.first, p.second, n, disposer);
  913. return n;
  914. }
  915. //! <b>Effects</b>: Erases all of the elements.
  916. //!
  917. //! <b>Complexity</b>: Linear to the number of elements on the container.
  918. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  919. //!
  920. //! <b>Throws</b>: Nothing.
  921. //!
  922. //! <b>Note</b>: Invalidates the iterators (but not the references)
  923. //! to the erased elements. No destructors are called.
  924. void clear() BOOST_NOEXCEPT
  925. { tree_type::clear(); }
  926. //! <b>Effects</b>: Erases all of the elements calling disposer(p) for
  927. //! each node to be erased.
  928. //! <b>Complexity</b>: Average complexity for is at most O(log(size() + N)),
  929. //! where N is the number of elements in the container.
  930. //!
  931. //! <b>Throws</b>: Nothing.
  932. //!
  933. //! <b>Note</b>: Invalidates the iterators (but not the references)
  934. //! to the erased elements. Calls N times to disposer functor.
  935. template<class Disposer>
  936. void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
  937. {
  938. node_algorithms::clear_and_dispose(this->tree_type::header_ptr()
  939. , detail::node_disposer<Disposer, value_traits, TreapAlgorithms>(disposer, &this->get_value_traits()));
  940. node_algorithms::init_header(this->tree_type::header_ptr());
  941. this->tree_type::sz_traits().set_size(0);
  942. }
  943. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  944. //! @copydoc ::boost::intrusive::bstree::merge_unique
  945. template<class T, class ...Options2> void merge_unique(sgtree<T, Options2...> &);
  946. #else
  947. template<class Compare2>
  948. void merge_unique(treap_impl
  949. <ValueTraits, VoidOrKeyOfValue, Compare2, VoidOrPrioOfValue, VoidOrPrioComp, SizeType, ConstantTimeSize, HeaderHolder> &source)
  950. #endif
  951. {
  952. node_ptr it (node_algorithms::begin_node(source.header_ptr()))
  953. , itend(node_algorithms::end_node (source.header_ptr()));
  954. while(it != itend){
  955. node_ptr const p(it);
  956. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
  957. it = node_algorithms::next_node(it);
  958. if( node_algorithms::transfer_unique
  959. ( this->header_ptr(), this->key_node_comp(this->key_comp())
  960. , this->prio_node_prio_comp(this->priv_pcomp()), source.header_ptr(), p) ){
  961. this->sz_traits().increment();
  962. source.sz_traits().decrement();
  963. }
  964. }
  965. }
  966. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  967. //! @copydoc ::boost::intrusive::bstree::merge_equal(bstree<T, Options2...>&)
  968. template<class T, class ...Options2> void merge_equal(sgtree<T, Options2...> &);
  969. #else
  970. template<class Compare2>
  971. void merge_equal(treap_impl
  972. <ValueTraits, VoidOrKeyOfValue, Compare2, VoidOrPrioOfValue, VoidOrPrioComp, SizeType, ConstantTimeSize, HeaderHolder> &source)
  973. #endif
  974. {
  975. node_ptr it (node_algorithms::begin_node(source.header_ptr()))
  976. , itend(node_algorithms::end_node (source.header_ptr()));
  977. while(it != itend){
  978. node_ptr const p(it);
  979. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
  980. it = node_algorithms::next_node(it);
  981. node_algorithms::transfer_equal
  982. ( this->header_ptr(), this->key_node_comp(this->key_comp())
  983. , this->prio_node_prio_comp(this->priv_pcomp()), source.header_ptr(), p);
  984. this->sz_traits().increment();
  985. source.sz_traits().decrement();
  986. }
  987. }
  988. //! @copydoc ::boost::intrusive::bstree::check(ExtraChecker)const
  989. template <class ExtraChecker>
  990. void check(ExtraChecker extra_checker) const
  991. {
  992. typedef detail::key_nodeptr_comp<priority_compare, value_traits, priority_of_value> nodeptr_prio_comp_t;
  993. tree_type::check(detail::treap_node_extra_checker
  994. <ValueTraits, nodeptr_prio_comp_t, ExtraChecker>
  995. (this->prio_node_prio_comp(this->priv_pcomp()), extra_checker));
  996. }
  997. //! @copydoc ::boost::intrusive::bstree::check()const
  998. void check() const
  999. { check(detail::empty_node_checker<ValueTraits>()); }
  1000. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1001. //! @copydoc ::boost::intrusive::bstree::count(const key_type &)const
  1002. size_type count(const key_type &key) const;
  1003. //! @copydoc ::boost::intrusive::bstree::count(const KeyType&,KeyTypeKeyCompare)const
  1004. template<class KeyType, class KeyTypeKeyCompare>
  1005. size_type count(const KeyType& key, KeyTypeKeyCompare comp) const;
  1006. //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)
  1007. iterator lower_bound(const key_type &key);
  1008. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)
  1009. template<class KeyType, class KeyTypeKeyCompare>
  1010. iterator lower_bound(const KeyType& key, KeyTypeKeyCompare comp);
  1011. //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)const
  1012. const_iterator lower_bound(const key_type &key) const;
  1013. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)const
  1014. template<class KeyType, class KeyTypeKeyCompare>
  1015. const_iterator lower_bound(const KeyType& key, KeyTypeKeyCompare comp) const;
  1016. //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)
  1017. iterator upper_bound(const key_type &key);
  1018. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)
  1019. template<class KeyType, class KeyTypeKeyCompare>
  1020. iterator upper_bound(const KeyType& key, KeyTypeKeyCompare comp);
  1021. //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)const
  1022. const_iterator upper_bound(const key_type &key) const;
  1023. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)const
  1024. template<class KeyType, class KeyTypeKeyCompare>
  1025. const_iterator upper_bound(const KeyType& key, KeyTypeKeyCompare comp) const;
  1026. //! @copydoc ::boost::intrusive::bstree::find(const key_type &)
  1027. iterator find(const key_type &key);
  1028. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)
  1029. template<class KeyType, class KeyTypeKeyCompare>
  1030. iterator find(const KeyType& key, KeyTypeKeyCompare comp);
  1031. //! @copydoc ::boost::intrusive::bstree::find(const key_type &)const
  1032. const_iterator find(const key_type &key) const;
  1033. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)const
  1034. template<class KeyType, class KeyTypeKeyCompare>
  1035. const_iterator find(const KeyType& key, KeyTypeKeyCompare comp) const;
  1036. //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)
  1037. std::pair<iterator,iterator> equal_range(const key_type &key);
  1038. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)
  1039. template<class KeyType, class KeyTypeKeyCompare>
  1040. std::pair<iterator,iterator> equal_range(const KeyType& key, KeyTypeKeyCompare comp);
  1041. //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)const
  1042. std::pair<const_iterator, const_iterator>
  1043. equal_range(const key_type &key) const;
  1044. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)const
  1045. template<class KeyType, class KeyTypeKeyCompare>
  1046. std::pair<const_iterator, const_iterator>
  1047. equal_range(const KeyType& key, KeyTypeKeyCompare comp) const;
  1048. //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)
  1049. std::pair<iterator,iterator> bounded_range
  1050. (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed);
  1051. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)
  1052. template<class KeyType, class KeyTypeKeyCompare>
  1053. std::pair<iterator,iterator> bounded_range
  1054. (const KeyType& lower_key, const KeyType& upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed);
  1055. //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)const
  1056. std::pair<const_iterator, const_iterator>
  1057. bounded_range(const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const;
  1058. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)const
  1059. template<class KeyType, class KeyTypeKeyCompare>
  1060. std::pair<const_iterator, const_iterator> bounded_range
  1061. (const KeyType& lower_key, const KeyType& upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const;
  1062. //! @copydoc ::boost::intrusive::bstree::s_iterator_to(reference)
  1063. static iterator s_iterator_to(reference value) BOOST_NOEXCEPT;
  1064. //! @copydoc ::boost::intrusive::bstree::s_iterator_to(const_reference)
  1065. static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT;
  1066. //! @copydoc ::boost::intrusive::bstree::iterator_to(reference)
  1067. iterator iterator_to(reference value) BOOST_NOEXCEPT;
  1068. //! @copydoc ::boost::intrusive::bstree::iterator_to(const_reference)const
  1069. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT;
  1070. //! @copydoc ::boost::intrusive::bstree::init_node(reference)
  1071. static void init_node(reference value) BOOST_NOEXCEPT;
  1072. //! @copydoc ::boost::intrusive::bstree::unlink_leftmost_without_rebalance
  1073. pointer unlink_leftmost_without_rebalance() BOOST_NOEXCEPT;
  1074. //! @copydoc ::boost::intrusive::bstree::replace_node
  1075. void replace_node(iterator replace_this, reference with_this) BOOST_NOEXCEPT;
  1076. //! @copydoc ::boost::intrusive::bstree::remove_node
  1077. void remove_node(reference value) BOOST_NOEXCEPT;
  1078. friend bool operator< (const treap_impl &x, const treap_impl &y);
  1079. friend bool operator==(const treap_impl &x, const treap_impl &y);
  1080. friend bool operator!= (const treap_impl &x, const treap_impl &y);
  1081. friend bool operator>(const treap_impl &x, const treap_impl &y);
  1082. friend bool operator<=(const treap_impl &x, const treap_impl &y);
  1083. friend bool operator>=(const treap_impl &x, const treap_impl &y);
  1084. friend void swap(treap_impl &x, treap_impl &y);
  1085. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1086. /// @cond
  1087. private:
  1088. template<class Disposer>
  1089. iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
  1090. {
  1091. for(n = 0; b != e; ++n)
  1092. this->erase_and_dispose(b++, disposer);
  1093. return b.unconst();
  1094. }
  1095. iterator private_erase(const_iterator b, const_iterator e, size_type &n)
  1096. {
  1097. for(n = 0; b != e; ++n)
  1098. this->erase(b++);
  1099. return b.unconst();
  1100. }
  1101. /// @endcond
  1102. };
  1103. //! Helper metafunction to define a \c treap that yields to the same type when the
  1104. //! same options (either explicitly or implicitly) are used.
  1105. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1106. template<class T, class ...Options>
  1107. #else
  1108. template<class T, class O1 = void, class O2 = void
  1109. , class O3 = void, class O4 = void
  1110. , class O5 = void, class O6 = void
  1111. , class O7 = void>
  1112. #endif
  1113. struct make_treap
  1114. {
  1115. typedef typename pack_options
  1116. < treap_defaults,
  1117. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1118. O1, O2, O3, O4, O5, O6, O7
  1119. #else
  1120. Options...
  1121. #endif
  1122. >::type packed_options;
  1123. typedef typename detail::get_value_traits
  1124. <T, typename packed_options::proto_value_traits>::type value_traits;
  1125. typedef treap_impl
  1126. < value_traits
  1127. , typename packed_options::key_of_value
  1128. , typename packed_options::compare
  1129. , typename packed_options::priority_of_value
  1130. , typename packed_options::priority
  1131. , typename packed_options::size_type
  1132. , packed_options::constant_time_size
  1133. , typename packed_options::header_holder_type
  1134. > implementation_defined;
  1135. /// @endcond
  1136. typedef implementation_defined type;
  1137. };
  1138. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1139. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1140. template<class T, class O1, class O2, class O3, class O4, class O5, class O6, class O7>
  1141. #else
  1142. template<class T, class ...Options>
  1143. #endif
  1144. class treap
  1145. : public make_treap<T,
  1146. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1147. O1, O2, O3, O4, O5, O6, O7
  1148. #else
  1149. Options...
  1150. #endif
  1151. >::type
  1152. {
  1153. typedef typename make_treap
  1154. <T,
  1155. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1156. O1, O2, O3, O4, O5, O6, O7
  1157. #else
  1158. Options...
  1159. #endif
  1160. >::type Base;
  1161. BOOST_MOVABLE_BUT_NOT_COPYABLE(treap)
  1162. public:
  1163. typedef typename Base::key_compare key_compare;
  1164. typedef typename Base::priority_compare priority_compare;
  1165. typedef typename Base::value_traits value_traits;
  1166. typedef typename Base::iterator iterator;
  1167. typedef typename Base::const_iterator const_iterator;
  1168. typedef typename Base::reverse_iterator reverse_iterator;
  1169. typedef typename Base::const_reverse_iterator const_reverse_iterator;
  1170. //Assert if passed value traits are compatible with the type
  1171. BOOST_INTRUSIVE_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
  1172. inline treap()
  1173. : Base()
  1174. {}
  1175. inline explicit treap( const key_compare &cmp
  1176. , const priority_compare &pcmp = priority_compare()
  1177. , const value_traits &v_traits = value_traits())
  1178. : Base(cmp, pcmp, v_traits)
  1179. {}
  1180. template<class Iterator>
  1181. inline treap( bool unique, Iterator b, Iterator e
  1182. , const key_compare &cmp = key_compare()
  1183. , const priority_compare &pcmp = priority_compare()
  1184. , const value_traits &v_traits = value_traits())
  1185. : Base(unique, b, e, cmp, pcmp, v_traits)
  1186. {}
  1187. inline treap(BOOST_RV_REF(treap) x)
  1188. : Base(BOOST_MOVE_BASE(Base, x))
  1189. {}
  1190. inline treap& operator=(BOOST_RV_REF(treap) x)
  1191. { return static_cast<treap&>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  1192. template <class Cloner, class Disposer>
  1193. inline void clone_from(const treap &src, Cloner cloner, Disposer disposer)
  1194. { Base::clone_from(src, cloner, disposer); }
  1195. template <class Cloner, class Disposer>
  1196. inline void clone_from(BOOST_RV_REF(treap) src, Cloner cloner, Disposer disposer)
  1197. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  1198. inline static treap &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  1199. { return static_cast<treap &>(Base::container_from_end_iterator(end_iterator)); }
  1200. inline static const treap &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  1201. { return static_cast<const treap &>(Base::container_from_end_iterator(end_iterator)); }
  1202. inline static treap &container_from_iterator(iterator it) BOOST_NOEXCEPT
  1203. { return static_cast<treap &>(Base::container_from_iterator(it)); }
  1204. inline static const treap &container_from_iterator(const_iterator it) BOOST_NOEXCEPT
  1205. { return static_cast<const treap &>(Base::container_from_iterator(it)); }
  1206. };
  1207. #endif
  1208. } //namespace intrusive
  1209. } //namespace boost
  1210. #include <boost/intrusive/detail/config_end.hpp>
  1211. #endif //BOOST_INTRUSIVE_TREAP_HPP