treap.hpp 56 KB

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