treap.hpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008
  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_TRIE_HPP
  13. #define BOOST_INTRUSIVE_TRIE_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <algorithm>
  16. #include <cstddef>
  17. #include <functional>
  18. #include <iterator>
  19. #include <utility>
  20. #include <boost/intrusive/detail/assert.hpp>
  21. #include <boost/static_assert.hpp>
  22. #include <boost/intrusive/intrusive_fwd.hpp>
  23. #include <boost/intrusive/bs_set_hook.hpp>
  24. #include <boost/intrusive/detail/tree_node.hpp>
  25. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  26. #include <boost/intrusive/detail/pointer_to_other.hpp>
  27. #include <boost/intrusive/detail/clear_on_destructor_base.hpp>
  28. #include <boost/intrusive/options.hpp>
  29. #include <boost/intrusive/detail/mpl.hpp>
  30. #include <boost/intrusive/treap_algorithms.hpp>
  31. #include <boost/intrusive/link_mode.hpp>
  32. #include <boost/intrusive/priority_compare.hpp>
  33. namespace boost {
  34. namespace intrusive {
  35. /// @cond
  36. template <class ValueTraits, class Compare, class PrioCompare, class SizeType, bool ConstantTimeSize>
  37. struct treap_setopt
  38. {
  39. typedef ValueTraits value_traits;
  40. typedef Compare compare;
  41. typedef PrioCompare priority_compare;
  42. typedef SizeType size_type;
  43. static const bool constant_time_size = ConstantTimeSize;
  44. };
  45. template <class T>
  46. struct treap_set_defaults
  47. : pack_options
  48. < none
  49. , base_hook<detail::default_bs_set_hook>
  50. , constant_time_size<true>
  51. , size_type<std::size_t>
  52. , compare<std::less<T> >
  53. , priority<boost::intrusive::priority_compare<T> >
  54. >::type
  55. {};
  56. /// @endcond
  57. //! The class template treap is an intrusive treap container that
  58. //! is used to construct intrusive set and multiset containers. The no-throw
  59. //! guarantee holds only, if the value_compare object and priority_compare object
  60. //! don't throw.
  61. //!
  62. //! The template parameter \c T is the type to be managed by the container.
  63. //! The user can specify additional options and if no options are provided
  64. //! default options are used.
  65. //!
  66. //! The container supports the following options:
  67. //! \c base_hook<>/member_hook<>/value_traits<>,
  68. //! \c constant_time_size<>, \c size_type<>,
  69. //! \c compare<> and \c priority_compare<>
  70. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  71. template<class T, class ...Options>
  72. #else
  73. template<class Config>
  74. #endif
  75. class treap_impl
  76. : private detail::clear_on_destructor_base<treap_impl<Config> >
  77. {
  78. template<class C> friend class detail::clear_on_destructor_base;
  79. public:
  80. typedef typename Config::value_traits value_traits;
  81. /// @cond
  82. static const bool external_value_traits =
  83. detail::external_value_traits_is_true<value_traits>::value;
  84. typedef typename detail::eval_if_c
  85. < external_value_traits
  86. , detail::eval_value_traits<value_traits>
  87. , detail::identity<value_traits>
  88. >::type real_value_traits;
  89. /// @endcond
  90. typedef typename real_value_traits::pointer pointer;
  91. typedef typename real_value_traits::const_pointer const_pointer;
  92. typedef typename std::iterator_traits<pointer>::value_type value_type;
  93. typedef value_type key_type;
  94. typedef typename std::iterator_traits<pointer>::reference reference;
  95. typedef typename std::iterator_traits<const_pointer>::reference const_reference;
  96. typedef typename std::iterator_traits<pointer>::difference_type difference_type;
  97. typedef typename Config::size_type size_type;
  98. typedef typename Config::compare value_compare;
  99. typedef typename Config::priority_compare priority_compare;
  100. typedef value_compare key_compare;
  101. typedef tree_iterator<treap_impl, false> iterator;
  102. typedef tree_iterator<treap_impl, true> const_iterator;
  103. typedef std::reverse_iterator<iterator> reverse_iterator;
  104. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  105. typedef typename real_value_traits::node_traits node_traits;
  106. typedef typename node_traits::node node;
  107. typedef typename boost::pointer_to_other
  108. <pointer, node>::type node_ptr;
  109. typedef typename boost::pointer_to_other
  110. <node_ptr, const node>::type const_node_ptr;
  111. typedef treap_algorithms<node_traits> node_algorithms;
  112. static const bool constant_time_size = Config::constant_time_size;
  113. static const bool stateful_value_traits = detail::is_stateful_value_traits<real_value_traits>::value;
  114. /// @cond
  115. private:
  116. typedef detail::size_holder<constant_time_size, size_type> size_traits;
  117. //noncopyable
  118. treap_impl (const treap_impl&);
  119. treap_impl operator =(const treap_impl&);
  120. enum { safemode_or_autounlink =
  121. (int)real_value_traits::link_mode == (int)auto_unlink ||
  122. (int)real_value_traits::link_mode == (int)safe_link };
  123. //Constant-time size is incompatible with auto-unlink hooks!
  124. BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink)));
  125. struct header_plus_size : public size_traits
  126. { node header_; };
  127. struct node_plus_pred_t : public detail::ebo_functor_holder<value_compare>
  128. {
  129. node_plus_pred_t(const value_compare &comp, const priority_compare &p_comp)
  130. : detail::ebo_functor_holder<value_compare>(comp)
  131. , header_plus_priority_size_(p_comp)
  132. {}
  133. struct header_plus_priority_size
  134. : public detail::ebo_functor_holder<priority_compare>
  135. {
  136. header_plus_priority_size(const priority_compare &p_comp)
  137. : detail::ebo_functor_holder<priority_compare>(p_comp)
  138. {}
  139. header_plus_size header_plus_size_;
  140. } header_plus_priority_size_;
  141. };
  142. struct data_t : public treap_impl::value_traits
  143. {
  144. typedef typename treap_impl::value_traits value_traits;
  145. data_t(const value_compare & comp, const priority_compare &pcomp, const value_traits &val_traits)
  146. : value_traits(val_traits), node_plus_pred_(comp, pcomp)
  147. {}
  148. node_plus_pred_t node_plus_pred_;
  149. } data_;
  150. const value_compare &priv_comp() const
  151. { return data_.node_plus_pred_.get(); }
  152. value_compare &priv_comp()
  153. { return data_.node_plus_pred_.get(); }
  154. const priority_compare &priv_pcomp() const
  155. { return data_.node_plus_pred_.header_plus_priority_size_.get(); }
  156. priority_compare &priv_pcomp()
  157. { return data_.node_plus_pred_.header_plus_priority_size_.get(); }
  158. const node &priv_header() const
  159. { return data_.node_plus_pred_.header_plus_priority_size_.header_plus_size_.header_; }
  160. node &priv_header()
  161. { return data_.node_plus_pred_.header_plus_priority_size_.header_plus_size_.header_; }
  162. static node_ptr uncast(const_node_ptr ptr)
  163. {
  164. return node_ptr(const_cast<node*>(detail::get_pointer(ptr)));
  165. }
  166. size_traits &priv_size_traits()
  167. { return data_.node_plus_pred_.header_plus_priority_size_.header_plus_size_; }
  168. const size_traits &priv_size_traits() const
  169. { return data_.node_plus_pred_.header_plus_priority_size_.header_plus_size_; }
  170. const real_value_traits &get_real_value_traits(detail::bool_<false>) const
  171. { return data_; }
  172. const real_value_traits &get_real_value_traits(detail::bool_<true>) const
  173. { return data_.get_value_traits(*this); }
  174. real_value_traits &get_real_value_traits(detail::bool_<false>)
  175. { return data_; }
  176. real_value_traits &get_real_value_traits(detail::bool_<true>)
  177. { return data_.get_value_traits(*this); }
  178. /// @endcond
  179. public:
  180. const real_value_traits &get_real_value_traits() const
  181. { return this->get_real_value_traits(detail::bool_<external_value_traits>()); }
  182. real_value_traits &get_real_value_traits()
  183. { return this->get_real_value_traits(detail::bool_<external_value_traits>()); }
  184. typedef typename node_algorithms::insert_commit_data insert_commit_data;
  185. //! <b>Effects</b>: Constructs an empty treap.
  186. //!
  187. //! <b>Complexity</b>: Constant.
  188. //!
  189. //! <b>Throws</b>: If value_traits::node_traits::node
  190. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  191. //! or the copy constructor of the value_compare/priority_compare objects throw. Basic guarantee.
  192. treap_impl( const value_compare &cmp = value_compare()
  193. , const priority_compare &pcmp = priority_compare()
  194. , const value_traits &v_traits = value_traits())
  195. : data_(cmp, pcmp, v_traits)
  196. {
  197. node_algorithms::init_header(&priv_header());
  198. this->priv_size_traits().set_size(size_type(0));
  199. }
  200. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
  201. //! cmp must be a comparison function that induces a strict weak ordering.
  202. //!
  203. //! <b>Effects</b>: Constructs an empty treap and inserts elements from
  204. //! [b, e).
  205. //!
  206. //! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
  207. //! comp and otherwise N * log N, where N is the distance between first and last.
  208. //!
  209. //! <b>Throws</b>: If value_traits::node_traits::node
  210. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  211. //! or the copy constructor/operator() of the value_compare/priority_compare objects
  212. //! throw. Basic guarantee.
  213. template<class Iterator>
  214. treap_impl( bool unique, Iterator b, Iterator e
  215. , const value_compare &cmp = value_compare()
  216. , const priority_compare &pcmp = priority_compare()
  217. , const value_traits &v_traits = value_traits())
  218. : data_(cmp, pcmp, v_traits)
  219. {
  220. node_algorithms::init_header(&priv_header());
  221. this->priv_size_traits().set_size(size_type(0));
  222. if(unique)
  223. this->insert_unique(b, e);
  224. else
  225. this->insert_equal(b, e);
  226. }
  227. //! <b>Effects</b>: Detaches all elements from this. The objects in the set
  228. //! are not deleted (i.e. no destructors are called), but the nodes according to
  229. //! the value_traits template parameter are reinitialized and thus can be reused.
  230. //!
  231. //! <b>Complexity</b>: Linear to elements contained in *this
  232. //! if constant-time size option is disabled. Constant time otherwise.
  233. //!
  234. //! <b>Throws</b>: Nothing.
  235. ~treap_impl()
  236. {}
  237. //! <b>Effects</b>: Returns an iterator pointing to the beginning of the treap.
  238. //!
  239. //! <b>Complexity</b>: Constant.
  240. //!
  241. //! <b>Throws</b>: Nothing.
  242. iterator begin()
  243. { return iterator (node_traits::get_left(node_ptr(&priv_header())), this); }
  244. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the treap.
  245. //!
  246. //! <b>Complexity</b>: Constant.
  247. //!
  248. //! <b>Throws</b>: Nothing.
  249. const_iterator begin() const
  250. { return this->cbegin(); }
  251. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the treap.
  252. //!
  253. //! <b>Complexity</b>: Constant.
  254. //!
  255. //! <b>Throws</b>: Nothing.
  256. const_iterator cbegin() const
  257. { return const_iterator (node_traits::get_left(const_node_ptr(&priv_header())), this); }
  258. //! <b>Effects</b>: Returns an iterator pointing to the end of the treap.
  259. //!
  260. //! <b>Complexity</b>: Constant.
  261. //!
  262. //! <b>Throws</b>: Nothing.
  263. iterator end()
  264. { return iterator (node_ptr(&priv_header()), this); }
  265. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the treap.
  266. //!
  267. //! <b>Complexity</b>: Constant.
  268. //!
  269. //! <b>Throws</b>: Nothing.
  270. const_iterator end() const
  271. { return this->cend(); }
  272. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the treap.
  273. //!
  274. //! <b>Complexity</b>: Constant.
  275. //!
  276. //! <b>Throws</b>: Nothing.
  277. const_iterator cend() const
  278. { return const_iterator (uncast(const_node_ptr(&priv_header())), this); }
  279. //! <b>Effects</b>: Returns an iterator pointing to the highest priority object of the treap.
  280. //!
  281. //! <b>Complexity</b>: Constant.
  282. //!
  283. //! <b>Throws</b>: Nothing.
  284. iterator top()
  285. { return this->empty() ? this->end() : iterator (node_traits::get_parent(node_ptr(&priv_header())), this); }
  286. //! <b>Effects</b>: Returns a const_iterator pointing to the highest priority object of the treap..
  287. //!
  288. //! <b>Complexity</b>: Constant.
  289. //!
  290. //! <b>Throws</b>: Nothing.
  291. const_iterator top() const
  292. { return this->ctop(); }
  293. //! <b>Effects</b>: Returns a const_iterator pointing to the highest priority object of the treap..
  294. //!
  295. //! <b>Complexity</b>: Constant.
  296. //!
  297. //! <b>Throws</b>: Nothing.
  298. const_iterator ctop() const
  299. { return this->empty() ? this->cend() : const_iterator (node_traits::get_parent(const_node_ptr(&priv_header())), this); }
  300. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning of the
  301. //! reversed treap.
  302. //!
  303. //! <b>Complexity</b>: Constant.
  304. //!
  305. //! <b>Throws</b>: Nothing.
  306. reverse_iterator rbegin()
  307. { return reverse_iterator(this->end()); }
  308. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  309. //! of the reversed treap.
  310. //!
  311. //! <b>Complexity</b>: Constant.
  312. //!
  313. //! <b>Throws</b>: Nothing.
  314. const_reverse_iterator rbegin() const
  315. { return const_reverse_iterator(this->end()); }
  316. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  317. //! of the reversed treap.
  318. //!
  319. //! <b>Complexity</b>: Constant.
  320. //!
  321. //! <b>Throws</b>: Nothing.
  322. const_reverse_iterator crbegin() const
  323. { return const_reverse_iterator(this->end()); }
  324. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  325. //! of the reversed treap.
  326. //!
  327. //! <b>Complexity</b>: Constant.
  328. //!
  329. //! <b>Throws</b>: Nothing.
  330. reverse_iterator rend()
  331. { return reverse_iterator(this->begin()); }
  332. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  333. //! of the reversed treap.
  334. //!
  335. //! <b>Complexity</b>: Constant.
  336. //!
  337. //! <b>Throws</b>: Nothing.
  338. const_reverse_iterator rend() const
  339. { return const_reverse_iterator(this->begin()); }
  340. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  341. //! of the reversed treap.
  342. //!
  343. //! <b>Complexity</b>: Constant.
  344. //!
  345. //! <b>Throws</b>: Nothing.
  346. const_reverse_iterator crend() const
  347. { return const_reverse_iterator(this->begin()); }
  348. //! <b>Effects</b>: Returns a reverse_iterator pointing to the highest priority object of the
  349. //! reversed treap.
  350. //!
  351. //! <b>Complexity</b>: Constant.
  352. //!
  353. //! <b>Throws</b>: Nothing.
  354. reverse_iterator rtop()
  355. { return reverse_iterator(this->top()); }
  356. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the highest priority objec
  357. //! of the reversed treap.
  358. //!
  359. //! <b>Complexity</b>: Constant.
  360. //!
  361. //! <b>Throws</b>: Nothing.
  362. const_reverse_iterator rtop() const
  363. { return const_reverse_iterator(this->top()); }
  364. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the highest priority object
  365. //! of the reversed treap.
  366. //!
  367. //! <b>Complexity</b>: Constant.
  368. //!
  369. //! <b>Throws</b>: Nothing.
  370. const_reverse_iterator crtop() const
  371. { return const_reverse_iterator(this->top()); }
  372. //! <b>Precondition</b>: end_iterator must be a valid end iterator
  373. //! of treap.
  374. //!
  375. //! <b>Effects</b>: Returns a const reference to the treap associated to the end iterator
  376. //!
  377. //! <b>Throws</b>: Nothing.
  378. //!
  379. //! <b>Complexity</b>: Constant.
  380. static treap_impl &container_from_end_iterator(iterator end_iterator)
  381. { return priv_container_from_end_iterator(end_iterator); }
  382. //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
  383. //! of treap.
  384. //!
  385. //! <b>Effects</b>: Returns a const reference to the treap associated to the iterator
  386. //!
  387. //! <b>Throws</b>: Nothing.
  388. //!
  389. //! <b>Complexity</b>: Constant.
  390. static const treap_impl &container_from_end_iterator(const_iterator end_iterator)
  391. { return priv_container_from_end_iterator(end_iterator); }
  392. //! <b>Precondition</b>: it must be a valid iterator
  393. //! of treap.
  394. //!
  395. //! <b>Effects</b>: Returns a const reference to the treap associated to the iterator
  396. //!
  397. //! <b>Throws</b>: Nothing.
  398. //!
  399. //! <b>Complexity</b>: Logarithmic.
  400. static treap_impl &container_from_iterator(iterator it)
  401. { return priv_container_from_iterator(it); }
  402. //! <b>Precondition</b>: it must be a valid end const_iterator
  403. //! of treap.
  404. //!
  405. //! <b>Effects</b>: Returns a const reference to the treap associated to the end iterator
  406. //!
  407. //! <b>Throws</b>: Nothing.
  408. //!
  409. //! <b>Complexity</b>: Logarithmic.
  410. static const treap_impl &container_from_iterator(const_iterator it)
  411. { return priv_container_from_iterator(it); }
  412. //! <b>Effects</b>: Returns the value_compare object used by the treap.
  413. //!
  414. //! <b>Complexity</b>: Constant.
  415. //!
  416. //! <b>Throws</b>: If value_compare copy-constructor throws.
  417. value_compare value_comp() const
  418. { return this->priv_comp(); }
  419. //! <b>Effects</b>: Returns the priority_compare object used by the treap.
  420. //!
  421. //! <b>Complexity</b>: Constant.
  422. //!
  423. //! <b>Throws</b>: If priority_compare copy-constructor throws.
  424. priority_compare priority_comp() const
  425. { return this->priv_pcomp(); }
  426. //! <b>Effects</b>: Returns true if the container is empty.
  427. //!
  428. //! <b>Complexity</b>: Constant.
  429. //!
  430. //! <b>Throws</b>: Nothing.
  431. bool empty() const
  432. { return node_algorithms::unique(const_node_ptr(&priv_header())); }
  433. //! <b>Effects</b>: Returns the number of elements stored in the treap.
  434. //!
  435. //! <b>Complexity</b>: Linear to elements contained in *this
  436. //! if constant-time size option is disabled. Constant time otherwise.
  437. //!
  438. //! <b>Throws</b>: Nothing.
  439. size_type size() const
  440. {
  441. if(constant_time_size)
  442. return this->priv_size_traits().get_size();
  443. else{
  444. return (size_type)node_algorithms::size(const_node_ptr(&priv_header()));
  445. }
  446. }
  447. //! <b>Effects</b>: Swaps the contents of two treaps.
  448. //!
  449. //! <b>Complexity</b>: Constant.
  450. //!
  451. //! <b>Throws</b>: If the comparison functor's swap call throws.
  452. void swap(treap_impl& other)
  453. {
  454. //This can throw
  455. using std::swap;
  456. swap(priv_comp(), priv_comp());
  457. swap(priv_pcomp(), priv_pcomp());
  458. //These can't throw
  459. node_algorithms::swap_tree(node_ptr(&priv_header()), node_ptr(&other.priv_header()));
  460. if(constant_time_size){
  461. size_type backup = this->priv_size_traits().get_size();
  462. this->priv_size_traits().set_size(other.priv_size_traits().get_size());
  463. other.priv_size_traits().set_size(backup);
  464. }
  465. }
  466. //! <b>Requires</b>: value must be an lvalue
  467. //!
  468. //! <b>Effects</b>: Inserts value into the treap before the upper bound.
  469. //!
  470. //! <b>Complexity</b>: Average complexity for insert element is at
  471. //! most logarithmic.
  472. //!
  473. //! <b>Throws</b>: If the internal value_compare or priority_compare functions throw. Strong guarantee.
  474. //!
  475. //! <b>Note</b>: Does not affect the validity of iterators and references.
  476. //! No copy-constructors are called.
  477. iterator insert_equal(reference value)
  478. {
  479. detail::key_nodeptr_comp<value_compare, treap_impl>
  480. key_node_comp(priv_comp(), this);
  481. detail::key_nodeptr_comp<priority_compare, treap_impl>
  482. key_node_pcomp(priv_pcomp(), this);
  483. node_ptr to_insert(get_real_value_traits().to_node_ptr(value));
  484. if(safemode_or_autounlink)
  485. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  486. this->priv_size_traits().increment();
  487. return iterator(node_algorithms::insert_equal_upper_bound
  488. (node_ptr(&priv_header()), to_insert, key_node_comp, key_node_pcomp), this);
  489. }
  490. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  491. //! a valid iterator.
  492. //!
  493. //! <b>Effects</b>: Inserts x into the treap, using "hint" as a hint to
  494. //! where it will be inserted. If "hint" is the upper_bound
  495. //! the insertion takes constant time (two comparisons in the worst case)
  496. //!
  497. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  498. //! constant time if t is inserted immediately before hint.
  499. //!
  500. //! <b>Throws</b>: If the internal value_compare or priority_compare functions throw. Strong guarantee.
  501. //!
  502. //! <b>Note</b>: Does not affect the validity of iterators and references.
  503. //! No copy-constructors are called.
  504. iterator insert_equal(const_iterator hint, reference value)
  505. {
  506. detail::key_nodeptr_comp<value_compare, treap_impl>
  507. key_node_comp(priv_comp(), this);
  508. detail::key_nodeptr_comp<priority_compare, treap_impl>
  509. key_node_pcomp(priv_pcomp(), this);
  510. node_ptr to_insert(get_real_value_traits().to_node_ptr(value));
  511. if(safemode_or_autounlink)
  512. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  513. this->priv_size_traits().increment();
  514. return iterator(node_algorithms::insert_equal
  515. (node_ptr(&priv_header()), hint.pointed_node(), to_insert, key_node_comp, key_node_pcomp), this);
  516. }
  517. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  518. //! of type value_type.
  519. //!
  520. //! <b>Effects</b>: Inserts a each element of a range into the treap
  521. //! before the upper bound of the key of each element.
  522. //!
  523. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  524. //! size of the range. However, it is linear in N if the range is already sorted
  525. //! by value_comp().
  526. //!
  527. //! <b>Throws</b>: If the internal value_compare or priority_compare functions throw.
  528. //! Strong guarantee.
  529. //!
  530. //! <b>Note</b>: Does not affect the validity of iterators and references.
  531. //! No copy-constructors are called.
  532. template<class Iterator>
  533. void insert_equal(Iterator b, Iterator e)
  534. {
  535. iterator end(this->end());
  536. for (; b != e; ++b)
  537. this->insert_equal(end, *b);
  538. }
  539. //! <b>Requires</b>: value must be an lvalue
  540. //!
  541. //! <b>Effects</b>: Inserts value into the treap if the value
  542. //! is not already present.
  543. //!
  544. //! <b>Complexity</b>: Average complexity for insert element is at
  545. //! most logarithmic.
  546. //!
  547. //! <b>Throws</b>: If the internal value_compare or priority_compare functions throw.
  548. //! Strong guarantee.
  549. //!
  550. //! <b>Note</b>: Does not affect the validity of iterators and references.
  551. //! No copy-constructors are called.
  552. std::pair<iterator, bool> insert_unique(reference value)
  553. {
  554. insert_commit_data commit_data;
  555. std::pair<iterator, bool> ret = insert_unique_check(value, priv_comp(), priv_pcomp(), commit_data);
  556. if(!ret.second)
  557. return ret;
  558. return std::pair<iterator, bool> (insert_unique_commit(value, commit_data), true);
  559. }
  560. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  561. //! a valid iterator
  562. //!
  563. //! <b>Effects</b>: Tries to insert x into the treap, using "hint" as a hint
  564. //! to where it will be inserted.
  565. //!
  566. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  567. //! constant time (two comparisons in the worst case)
  568. //! if t is inserted immediately before hint.
  569. //!
  570. //! <b>Throws</b>: If the internal value_compare or priority_compare functions throw.
  571. //! Strong guarantee.
  572. //!
  573. //! <b>Note</b>: Does not affect the validity of iterators and references.
  574. //! No copy-constructors are called.
  575. iterator insert_unique(const_iterator hint, reference value)
  576. {
  577. insert_commit_data commit_data;
  578. std::pair<iterator, bool> ret = insert_unique_check(hint, value, priv_comp(), priv_pcomp(), commit_data);
  579. if(!ret.second)
  580. return ret.first;
  581. return insert_unique_commit(value, commit_data);
  582. }
  583. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  584. //! of type value_type.
  585. //!
  586. //! <b>Effects</b>: Tries to insert each element of a range into the treap.
  587. //!
  588. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  589. //! size of the range. However, it is linear in N if the range is already sorted
  590. //! by value_comp().
  591. //!
  592. //! <b>Throws</b>: If the internal value_compare or priority_compare functions throw.
  593. //! Strong guarantee.
  594. //!
  595. //! <b>Note</b>: Does not affect the validity of iterators and references.
  596. //! No copy-constructors are called.
  597. template<class Iterator>
  598. void insert_unique(Iterator b, Iterator e)
  599. {
  600. if(this->empty()){
  601. iterator end(this->end());
  602. for (; b != e; ++b)
  603. this->insert_unique(end, *b);
  604. }
  605. else{
  606. for (; b != e; ++b)
  607. this->insert_unique(*b);
  608. }
  609. }
  610. //! <b>Requires</b>: key_value_comp must be a comparison function that induces
  611. //! the same strict weak ordering as value_compare.
  612. //! key_value_pcomp must be a comparison function that induces
  613. //! the same strict weak ordering as priority_compare. The difference is that
  614. //! key_value_pcomp and key_value_comp compare an arbitrary key with the contained values.
  615. //!
  616. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  617. //! a user provided key instead of the value itself.
  618. //!
  619. //! <b>Returns</b>: If there is an equivalent value
  620. //! returns a pair containing an iterator to the already present value
  621. //! and false. If the value can be inserted returns true in the returned
  622. //! pair boolean and fills "commit_data" that is meant to be used with
  623. //! the "insert_commit" function.
  624. //!
  625. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  626. //!
  627. //! <b>Throws</b>: If the key_value_comp or key_value_pcomp
  628. //! ordering functions throw. Strong guarantee.
  629. //!
  630. //! <b>Notes</b>: This function is used to improve performance when constructing
  631. //! a value_type is expensive: if there is an equivalent value
  632. //! the constructed object must be discarded. Many times, the part of the
  633. //! node that is used to impose the order is much cheaper to construct
  634. //! than the value_type and this function offers the possibility to use that
  635. //! part to check if the insertion will be successful.
  636. //!
  637. //! If the check is successful, the user can construct the value_type and use
  638. //! "insert_commit" to insert the object in constant-time. This gives a total
  639. //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
  640. //!
  641. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  642. //! objects are inserted or erased from the container.
  643. template<class KeyType, class KeyValueCompare, class KeyValuePrioCompare>
  644. std::pair<iterator, bool> insert_unique_check
  645. ( const KeyType &key, KeyValueCompare key_value_comp
  646. , KeyValuePrioCompare key_value_pcomp, insert_commit_data &commit_data)
  647. {
  648. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  649. comp(key_value_comp, this);
  650. detail::key_nodeptr_comp<KeyValuePrioCompare, treap_impl>
  651. pcomp(key_value_pcomp, this);
  652. std::pair<node_ptr, bool> ret =
  653. (node_algorithms::insert_unique_check
  654. (node_ptr(&priv_header()), key, comp, pcomp, commit_data));
  655. return std::pair<iterator, bool>(iterator(ret.first, this), ret.second);
  656. }
  657. //! <b>Requires</b>: key_value_comp must be a comparison function that induces
  658. //! the same strict weak ordering as value_compare.
  659. //! key_value_pcomp must be a comparison function that induces
  660. //! the same strict weak ordering as priority_compare. The difference is that
  661. //! key_value_pcomp and key_value_comp compare an arbitrary key with the contained values.
  662. //!
  663. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  664. //! a user provided key instead of the value itself, using "hint"
  665. //! as a hint to where it will be inserted.
  666. //!
  667. //! <b>Returns</b>: If there is an equivalent value
  668. //! returns a pair containing an iterator to the already present value
  669. //! and false. If the value can be inserted returns true in the returned
  670. //! pair boolean and fills "commit_data" that is meant to be used with
  671. //! the "insert_commit" function.
  672. //!
  673. //! <b>Complexity</b>: Logarithmic in general, but it's amortized
  674. //! constant time if t is inserted immediately before hint.
  675. //!
  676. //! <b>Throws</b>: If the key_value_comp or key_value_pcomp
  677. //! ordering functions throw. Strong guarantee.
  678. //!
  679. //! <b>Notes</b>: This function is used to improve performance when constructing
  680. //! a value_type is expensive: if there is an equivalent value
  681. //! the constructed object must be discarded. Many times, the part of the
  682. //! constructing that is used to impose the order is much cheaper to construct
  683. //! than the value_type and this function offers the possibility to use that key
  684. //! to check if the insertion will be successful.
  685. //!
  686. //! If the check is successful, the user can construct the value_type and use
  687. //! "insert_commit" to insert the object in constant-time. This can give a total
  688. //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)).
  689. //!
  690. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  691. //! objects are inserted or erased from the container.
  692. template<class KeyType, class KeyValueCompare, class KeyValuePrioCompare>
  693. std::pair<iterator, bool> insert_unique_check
  694. ( const_iterator hint, const KeyType &key
  695. , KeyValueCompare key_value_comp
  696. , KeyValuePrioCompare key_value_pcomp
  697. , insert_commit_data &commit_data)
  698. {
  699. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  700. comp(key_value_comp, this);
  701. detail::key_nodeptr_comp<KeyValuePrioCompare, treap_impl>
  702. pcomp(key_value_pcomp, this);
  703. std::pair<node_ptr, bool> ret =
  704. (node_algorithms::insert_unique_check
  705. (node_ptr(&priv_header()), hint.pointed_node(), key, comp, pcomp, commit_data));
  706. return std::pair<iterator, bool>(iterator(ret.first, this), ret.second);
  707. }
  708. //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
  709. //! must have been obtained from a previous call to "insert_check".
  710. //! No objects should have been inserted or erased from the container between
  711. //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
  712. //!
  713. //! <b>Effects</b>: Inserts the value in the avl_set using the information obtained
  714. //! from the "commit_data" that a previous "insert_check" filled.
  715. //!
  716. //! <b>Returns</b>: An iterator to the newly inserted object.
  717. //!
  718. //! <b>Complexity</b>: Constant time.
  719. //!
  720. //! <b>Throws</b>: Nothing
  721. //!
  722. //! <b>Notes</b>: This function has only sense if a "insert_check" has been
  723. //! previously executed to fill "commit_data". No value should be inserted or
  724. //! erased between the "insert_check" and "insert_commit" calls.
  725. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
  726. {
  727. node_ptr to_insert(get_real_value_traits().to_node_ptr(value));
  728. if(safemode_or_autounlink)
  729. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  730. this->priv_size_traits().increment();
  731. node_algorithms::insert_unique_commit(node_ptr(&priv_header()), to_insert, commit_data);
  732. return iterator(to_insert, this);
  733. }
  734. //! <b>Requires</b>: value must be an lvalue, "pos" must be
  735. //! a valid iterator (or end) and must be the succesor of value
  736. //! once inserted according to the predicate
  737. //!
  738. //! <b>Effects</b>: Inserts x into the treap before "pos".
  739. //!
  740. //! <b>Complexity</b>: Constant time.
  741. //!
  742. //! <b>Throws</b>: If the internal priority_compare function throws. Strong guarantee.
  743. //!
  744. //! <b>Note</b>: This function does not check preconditions so if "pos" is not
  745. //! the successor of "value" treap ordering invariant will be broken.
  746. //! This is a low-level function to be used only for performance reasons
  747. //! by advanced users.
  748. iterator insert_before(const_iterator pos, reference value)
  749. {
  750. node_ptr to_insert(get_real_value_traits().to_node_ptr(value));
  751. if(safemode_or_autounlink)
  752. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  753. this->priv_size_traits().increment();
  754. detail::key_nodeptr_comp<priority_compare, treap_impl>
  755. pcomp(priv_pcomp(), this);
  756. return iterator(node_algorithms::insert_before
  757. (node_ptr(&priv_header()), pos.pointed_node(), to_insert, pcomp), this);
  758. }
  759. //! <b>Requires</b>: value must be an lvalue, and it must be no less
  760. //! than the greatest inserted key
  761. //!
  762. //! <b>Effects</b>: Inserts x into the treap in the last position.
  763. //!
  764. //! <b>Complexity</b>: Constant time.
  765. //!
  766. //! <b>Throws</b>: If the internal priority_compare function throws. Strong guarantee.
  767. //!
  768. //! <b>Note</b>: This function does not check preconditions so if value is
  769. //! less than the greatest inserted key treap ordering invariant will be broken.
  770. //! This function is slightly more efficient than using "insert_before".
  771. //! This is a low-level function to be used only for performance reasons
  772. //! by advanced users.
  773. void push_back(reference value)
  774. {
  775. node_ptr to_insert(get_real_value_traits().to_node_ptr(value));
  776. if(safemode_or_autounlink)
  777. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  778. this->priv_size_traits().increment();
  779. detail::key_nodeptr_comp<priority_compare, treap_impl>
  780. pcomp(priv_pcomp(), this);
  781. node_algorithms::push_back(node_ptr(&priv_header()), to_insert, pcomp);
  782. }
  783. //! <b>Requires</b>: value must be an lvalue, and it must be no greater
  784. //! than the minimum inserted key
  785. //!
  786. //! <b>Effects</b>: Inserts x into the treap in the first position.
  787. //!
  788. //! <b>Complexity</b>: Constant time.
  789. //!
  790. //! <b>Throws</b>: If the internal priority_compare function throws. Strong guarantee.
  791. //!
  792. //! <b>Note</b>: This function does not check preconditions so if value is
  793. //! greater than the minimum inserted key treap ordering invariant will be broken.
  794. //! This function is slightly more efficient than using "insert_before".
  795. //! This is a low-level function to be used only for performance reasons
  796. //! by advanced users.
  797. void push_front(reference value)
  798. {
  799. node_ptr to_insert(get_real_value_traits().to_node_ptr(value));
  800. if(safemode_or_autounlink)
  801. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert));
  802. this->priv_size_traits().increment();
  803. detail::key_nodeptr_comp<priority_compare, treap_impl>
  804. pcomp(priv_pcomp(), this);
  805. node_algorithms::push_front(node_ptr(&priv_header()), to_insert, pcomp);
  806. }
  807. //! <b>Effects</b>: Erases the element pointed to by pos.
  808. //!
  809. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  810. //!
  811. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  812. //!
  813. //! <b>Note</b>: Invalidates the iterators (but not the references)
  814. //! to the erased elements. No destructors are called.
  815. iterator erase(const_iterator i)
  816. {
  817. const_iterator ret(i);
  818. ++ret;
  819. node_ptr to_erase(i.pointed_node());
  820. if(safemode_or_autounlink)
  821. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!node_algorithms::unique(to_erase));
  822. detail::key_nodeptr_comp<priority_compare, treap_impl>
  823. key_node_pcomp(priv_pcomp(), this);
  824. node_algorithms::erase(&priv_header(), to_erase, key_node_pcomp);
  825. this->priv_size_traits().decrement();
  826. if(safemode_or_autounlink)
  827. node_algorithms::init(to_erase);
  828. return ret.unconst();
  829. }
  830. //! <b>Effects</b>: Erases the range pointed to by b end e.
  831. //!
  832. //! <b>Complexity</b>: Average complexity for erase range is at most
  833. //! O(log(size() + N)), where N is the number of elements in the range.
  834. //!
  835. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  836. //!
  837. //! <b>Note</b>: Invalidates the iterators (but not the references)
  838. //! to the erased elements. No destructors are called.
  839. iterator erase(const_iterator b, const_iterator e)
  840. { size_type n; return private_erase(b, e, n); }
  841. //! <b>Effects</b>: Erases all the elements with the given value.
  842. //!
  843. //! <b>Returns</b>: The number of erased elements.
  844. //!
  845. //! <b>Complexity</b>: O(log(size() + N).
  846. //!
  847. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  848. //!
  849. //! <b>Note</b>: Invalidates the iterators (but not the references)
  850. //! to the erased elements. No destructors are called.
  851. size_type erase(const_reference value)
  852. { return this->erase(value, priv_comp()); }
  853. //! <b>Effects</b>: Erases all the elements with the given key.
  854. //! according to the comparison functor "comp".
  855. //!
  856. //! <b>Returns</b>: The number of erased elements.
  857. //!
  858. //! <b>Complexity</b>: O(log(size() + N).
  859. //!
  860. //! <b>Throws</b>: if the internal priority_compare function throws.
  861. //! Equivalent guarantee to <i>while(beg != end) erase(beg++);</i>
  862. //!
  863. //! <b>Note</b>: Invalidates the iterators (but not the references)
  864. //! to the erased elements. No destructors are called.
  865. template<class KeyType, class KeyValueCompare>
  866. size_type erase(const KeyType& key, KeyValueCompare comp
  867. /// @cond
  868. , typename detail::enable_if_c<!detail::is_convertible<KeyValueCompare, const_iterator>::value >::type * = 0
  869. /// @endcond
  870. )
  871. {
  872. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  873. size_type n;
  874. private_erase(p.first, p.second, n);
  875. return n;
  876. }
  877. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  878. //!
  879. //! <b>Effects</b>: Erases the element pointed to by pos.
  880. //! Disposer::operator()(pointer) is called for the removed element.
  881. //!
  882. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  883. //!
  884. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  885. //!
  886. //! <b>Note</b>: Invalidates the iterators
  887. //! to the erased elements.
  888. template<class Disposer>
  889. iterator erase_and_dispose(const_iterator i, Disposer disposer)
  890. {
  891. node_ptr to_erase(i.pointed_node());
  892. iterator ret(this->erase(i));
  893. disposer(get_real_value_traits().to_value_ptr(to_erase));
  894. return ret;
  895. }
  896. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  897. template<class Disposer>
  898. iterator erase_and_dispose(iterator i, Disposer disposer)
  899. { return this->erase_and_dispose(const_iterator(i), disposer); }
  900. #endif
  901. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  902. //!
  903. //! <b>Effects</b>: Erases the range pointed to by b end e.
  904. //! Disposer::operator()(pointer) is called for the removed elements.
  905. //!
  906. //! <b>Complexity</b>: Average complexity for erase range is at most
  907. //! O(log(size() + N)), where N is the number of elements in the range.
  908. //!
  909. //! <b>Throws</b>: if the internal priority_compare function throws. Strong guarantee.
  910. //!
  911. //! <b>Note</b>: Invalidates the iterators
  912. //! to the erased elements.
  913. template<class Disposer>
  914. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
  915. { size_type n; return private_erase(b, e, n, disposer); }
  916. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  917. //!
  918. //! <b>Effects</b>: Erases all the elements with the given value.
  919. //! Disposer::operator()(pointer) is called for the removed elements.
  920. //!
  921. //! <b>Returns</b>: The number of erased elements.
  922. //!
  923. //! <b>Complexity</b>: O(log(size() + N).
  924. //!
  925. //! <b>Throws</b>: if the priority_compare function throws then weak guarantee and heap invariants are broken.
  926. //! The safest thing would be to clear or destroy the container.
  927. //!
  928. //! <b>Note</b>: Invalidates the iterators (but not the references)
  929. //! to the erased elements. No destructors are called.
  930. template<class Disposer>
  931. size_type erase_and_dispose(const_reference value, Disposer disposer)
  932. {
  933. std::pair<iterator,iterator> p = this->equal_range(value);
  934. size_type n;
  935. private_erase(p.first, p.second, n, disposer);
  936. return n;
  937. }
  938. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  939. //!
  940. //! <b>Effects</b>: Erases all the elements with the given key.
  941. //! according to the comparison functor "comp".
  942. //! Disposer::operator()(pointer) is called for the removed elements.
  943. //!
  944. //! <b>Returns</b>: The number of erased elements.
  945. //!
  946. //! <b>Complexity</b>: O(log(size() + N).
  947. //!
  948. //! <b>Throws</b>: if the priority_compare function throws then weak guarantee and heap invariants are broken.
  949. //! The safest thing would be to clear or destroy the container.
  950. //!
  951. //! <b>Note</b>: Invalidates the iterators
  952. //! to the erased elements.
  953. template<class KeyType, class KeyValueCompare, class Disposer>
  954. size_type erase_and_dispose(const KeyType& key, KeyValueCompare comp, Disposer disposer
  955. /// @cond
  956. , typename detail::enable_if_c<!detail::is_convertible<KeyValueCompare, const_iterator>::value >::type * = 0
  957. /// @endcond
  958. )
  959. {
  960. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  961. size_type n;
  962. private_erase(p.first, p.second, n, disposer);
  963. return n;
  964. }
  965. //! <b>Effects</b>: Erases all of the elements.
  966. //!
  967. //! <b>Complexity</b>: Linear to the number of elements on the container.
  968. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  969. //!
  970. //! <b>Throws</b>: Nothing.
  971. //!
  972. //! <b>Note</b>: Invalidates the iterators (but not the references)
  973. //! to the erased elements. No destructors are called.
  974. void clear()
  975. {
  976. if(safemode_or_autounlink){
  977. this->clear_and_dispose(detail::null_disposer());
  978. }
  979. else{
  980. node_algorithms::init_header(&priv_header());
  981. this->priv_size_traits().set_size(0);
  982. }
  983. }
  984. //! <b>Effects</b>: Erases all of the elements calling disposer(p) for
  985. //! each node to be erased.
  986. //! <b>Complexity</b>: Average complexity for is at most O(log(size() + N)),
  987. //! where N is the number of elements in the container.
  988. //!
  989. //! <b>Throws</b>: Nothing.
  990. //!
  991. //! <b>Note</b>: Invalidates the iterators (but not the references)
  992. //! to the erased elements. Calls N times to disposer functor.
  993. template<class Disposer>
  994. void clear_and_dispose(Disposer disposer)
  995. {
  996. node_algorithms::clear_and_dispose(node_ptr(&priv_header())
  997. , detail::node_disposer<Disposer, treap_impl>(disposer, this));
  998. node_algorithms::init_header(&priv_header());
  999. this->priv_size_traits().set_size(0);
  1000. }
  1001. //! <b>Effects</b>: Returns the number of contained elements with the given value
  1002. //!
  1003. //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
  1004. //! to number of objects with the given value.
  1005. //!
  1006. //! <b>Throws</b>: Nothing.
  1007. size_type count(const_reference value) const
  1008. { return this->count(value, priv_comp()); }
  1009. //! <b>Effects</b>: Returns the number of contained elements with the given key
  1010. //!
  1011. //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
  1012. //! to number of objects with the given key.
  1013. //!
  1014. //! <b>Throws</b>: Nothing.
  1015. template<class KeyType, class KeyValueCompare>
  1016. size_type count(const KeyType &key, KeyValueCompare comp) const
  1017. {
  1018. std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
  1019. return std::distance(ret.first, ret.second);
  1020. }
  1021. //! <b>Effects</b>: Returns an iterator to the first element whose
  1022. //! key is not less than k or end() if that element does not exist.
  1023. //!
  1024. //! <b>Complexity</b>: Logarithmic.
  1025. //!
  1026. //! <b>Throws</b>: Nothing.
  1027. iterator lower_bound(const_reference value)
  1028. { return this->lower_bound(value, priv_comp()); }
  1029. //! <b>Effects</b>: Returns an iterator to the first element whose
  1030. //! key is not less than k or end() if that element does not exist.
  1031. //!
  1032. //! <b>Complexity</b>: Logarithmic.
  1033. //!
  1034. //! <b>Throws</b>: Nothing.
  1035. const_iterator lower_bound(const_reference value) const
  1036. { return this->lower_bound(value, priv_comp()); }
  1037. //! <b>Effects</b>: Returns an iterator to the first element whose
  1038. //! key is not less than k or end() if that element does not exist.
  1039. //!
  1040. //! <b>Complexity</b>: Logarithmic.
  1041. //!
  1042. //! <b>Throws</b>: Nothing.
  1043. template<class KeyType, class KeyValueCompare>
  1044. iterator lower_bound(const KeyType &key, KeyValueCompare comp)
  1045. {
  1046. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1047. key_node_comp(comp, this);
  1048. return iterator(node_algorithms::lower_bound
  1049. (const_node_ptr(&priv_header()), key, key_node_comp), this);
  1050. }
  1051. //! <b>Effects</b>: Returns a const iterator to the first element whose
  1052. //! key is not less than k or end() if that element does not exist.
  1053. //!
  1054. //! <b>Complexity</b>: Logarithmic.
  1055. //!
  1056. //! <b>Throws</b>: Nothing.
  1057. template<class KeyType, class KeyValueCompare>
  1058. const_iterator lower_bound(const KeyType &key, KeyValueCompare comp) const
  1059. {
  1060. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1061. key_node_comp(comp, this);
  1062. return const_iterator(node_algorithms::lower_bound
  1063. (const_node_ptr(&priv_header()), key, key_node_comp), this);
  1064. }
  1065. //! <b>Effects</b>: Returns an iterator to the first element whose
  1066. //! key is greater than k or end() if that element does not exist.
  1067. //!
  1068. //! <b>Complexity</b>: Logarithmic.
  1069. //!
  1070. //! <b>Throws</b>: Nothing.
  1071. iterator upper_bound(const_reference value)
  1072. { return this->upper_bound(value, priv_comp()); }
  1073. //! <b>Effects</b>: Returns an iterator to the first element whose
  1074. //! key is greater than k according to comp or end() if that element
  1075. //! does not exist.
  1076. //!
  1077. //! <b>Complexity</b>: Logarithmic.
  1078. //!
  1079. //! <b>Throws</b>: Nothing.
  1080. template<class KeyType, class KeyValueCompare>
  1081. iterator upper_bound(const KeyType &key, KeyValueCompare comp)
  1082. {
  1083. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1084. key_node_comp(comp, this);
  1085. return iterator(node_algorithms::upper_bound
  1086. (const_node_ptr(&priv_header()), key, key_node_comp), this);
  1087. }
  1088. //! <b>Effects</b>: Returns an iterator to the first element whose
  1089. //! key is greater than k or end() if that element does not exist.
  1090. //!
  1091. //! <b>Complexity</b>: Logarithmic.
  1092. //!
  1093. //! <b>Throws</b>: Nothing.
  1094. const_iterator upper_bound(const_reference value) const
  1095. { return this->upper_bound(value, priv_comp()); }
  1096. //! <b>Effects</b>: Returns an iterator to the first element whose
  1097. //! key is greater than k according to comp or end() if that element
  1098. //! does not exist.
  1099. //!
  1100. //! <b>Complexity</b>: Logarithmic.
  1101. //!
  1102. //! <b>Throws</b>: Nothing.
  1103. template<class KeyType, class KeyValueCompare>
  1104. const_iterator upper_bound(const KeyType &key, KeyValueCompare comp) const
  1105. {
  1106. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1107. key_node_comp(comp, this);
  1108. return const_iterator(node_algorithms::upper_bound
  1109. (const_node_ptr(&priv_header()), key, key_node_comp), this);
  1110. }
  1111. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1112. //! k or end() if that element does not exist.
  1113. //!
  1114. //! <b>Complexity</b>: Logarithmic.
  1115. //!
  1116. //! <b>Throws</b>: Nothing.
  1117. iterator find(const_reference value)
  1118. { return this->find(value, priv_comp()); }
  1119. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1120. //! k or end() if that element does not exist.
  1121. //!
  1122. //! <b>Complexity</b>: Logarithmic.
  1123. //!
  1124. //! <b>Throws</b>: Nothing.
  1125. template<class KeyType, class KeyValueCompare>
  1126. iterator find(const KeyType &key, KeyValueCompare comp)
  1127. {
  1128. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1129. key_node_comp(comp, this);
  1130. return iterator
  1131. (node_algorithms::find(const_node_ptr(&priv_header()), key, key_node_comp), this);
  1132. }
  1133. //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
  1134. //! k or end() if that element does not exist.
  1135. //!
  1136. //! <b>Complexity</b>: Logarithmic.
  1137. //!
  1138. //! <b>Throws</b>: Nothing.
  1139. const_iterator find(const_reference value) const
  1140. { return this->find(value, priv_comp()); }
  1141. //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
  1142. //! k or end() if that element does not exist.
  1143. //!
  1144. //! <b>Complexity</b>: Logarithmic.
  1145. //!
  1146. //! <b>Throws</b>: Nothing.
  1147. template<class KeyType, class KeyValueCompare>
  1148. const_iterator find(const KeyType &key, KeyValueCompare comp) const
  1149. {
  1150. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1151. key_node_comp(comp, this);
  1152. return const_iterator
  1153. (node_algorithms::find(const_node_ptr(&priv_header()), key, key_node_comp), this);
  1154. }
  1155. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1156. //! an empty range that indicates the position where those elements would be
  1157. //! if they there is no elements with key k.
  1158. //!
  1159. //! <b>Complexity</b>: Logarithmic.
  1160. //!
  1161. //! <b>Throws</b>: Nothing.
  1162. std::pair<iterator,iterator> equal_range(const_reference value)
  1163. { return this->equal_range(value, priv_comp()); }
  1164. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1165. //! an empty range that indicates the position where those elements would be
  1166. //! if they there is no elements with key k.
  1167. //!
  1168. //! <b>Complexity</b>: Logarithmic.
  1169. //!
  1170. //! <b>Throws</b>: Nothing.
  1171. template<class KeyType, class KeyValueCompare>
  1172. std::pair<iterator,iterator> equal_range(const KeyType &key, KeyValueCompare comp)
  1173. {
  1174. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1175. key_node_comp(comp, this);
  1176. std::pair<node_ptr, node_ptr> ret
  1177. (node_algorithms::equal_range(const_node_ptr(&priv_header()), key, key_node_comp));
  1178. return std::pair<iterator, iterator>(iterator(ret.first, this), iterator(ret.second, this));
  1179. }
  1180. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1181. //! an empty range that indicates the position where those elements would be
  1182. //! if they there is no elements with key k.
  1183. //!
  1184. //! <b>Complexity</b>: Logarithmic.
  1185. //!
  1186. //! <b>Throws</b>: Nothing.
  1187. std::pair<const_iterator, const_iterator>
  1188. equal_range(const_reference value) const
  1189. { return this->equal_range(value, priv_comp()); }
  1190. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1191. //! an empty range that indicates the position where those elements would be
  1192. //! if they there is no elements with key k.
  1193. //!
  1194. //! <b>Complexity</b>: Logarithmic.
  1195. //!
  1196. //! <b>Throws</b>: Nothing.
  1197. template<class KeyType, class KeyValueCompare>
  1198. std::pair<const_iterator, const_iterator>
  1199. equal_range(const KeyType &key, KeyValueCompare comp) const
  1200. {
  1201. detail::key_nodeptr_comp<KeyValueCompare, treap_impl>
  1202. key_node_comp(comp, this);
  1203. std::pair<node_ptr, node_ptr> ret
  1204. (node_algorithms::equal_range(const_node_ptr(&priv_header()), key, key_node_comp));
  1205. return std::pair<const_iterator, const_iterator>(const_iterator(ret.first, this), const_iterator(ret.second, this));
  1206. }
  1207. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1208. //! Cloner should yield to nodes equivalent to the original nodes.
  1209. //!
  1210. //! <b>Effects</b>: Erases all the elements from *this
  1211. //! calling Disposer::operator()(pointer), clones all the
  1212. //! elements from src calling Cloner::operator()(const_reference )
  1213. //! and inserts them on *this. Copies the predicate from the source container.
  1214. //!
  1215. //! If cloner throws, all cloned elements are unlinked and disposed
  1216. //! calling Disposer::operator()(pointer).
  1217. //!
  1218. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  1219. //!
  1220. //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
  1221. template <class Cloner, class Disposer>
  1222. void clone_from(const treap_impl &src, Cloner cloner, Disposer disposer)
  1223. {
  1224. this->clear_and_dispose(disposer);
  1225. if(!src.empty()){
  1226. detail::exception_disposer<treap_impl, Disposer>
  1227. rollback(*this, disposer);
  1228. node_algorithms::clone
  1229. (const_node_ptr(&src.priv_header())
  1230. ,node_ptr(&this->priv_header())
  1231. ,detail::node_cloner<Cloner, treap_impl>(cloner, this)
  1232. ,detail::node_disposer<Disposer, treap_impl>(disposer, this));
  1233. this->priv_size_traits().set_size(src.priv_size_traits().get_size());
  1234. this->priv_comp() = src.priv_comp();
  1235. rollback.release();
  1236. }
  1237. }
  1238. //! <b>Effects</b>: Unlinks the leftmost node from the treap.
  1239. //!
  1240. //! <b>Complexity</b>: Average complexity is constant time.
  1241. //!
  1242. //! <b>Throws</b>: Nothing.
  1243. //!
  1244. //! <b>Notes</b>: This function breaks the treap and the treap can
  1245. //! only be used for more unlink_leftmost_without_rebalance calls.
  1246. //! This function is normally used to achieve a step by step
  1247. //! controlled destruction of the treap.
  1248. pointer unlink_leftmost_without_rebalance()
  1249. {
  1250. node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance
  1251. (node_ptr(&priv_header())));
  1252. if(!to_be_disposed)
  1253. return 0;
  1254. this->priv_size_traits().decrement();
  1255. if(safemode_or_autounlink)//If this is commented does not work with normal_link
  1256. node_algorithms::init(to_be_disposed);
  1257. return get_real_value_traits().to_value_ptr(to_be_disposed);
  1258. }
  1259. //! <b>Requires</b>: replace_this must be a valid iterator of *this
  1260. //! and with_this must not be inserted in any treap.
  1261. //!
  1262. //! <b>Effects</b>: Replaces replace_this in its position in the
  1263. //! treap with with_this. The treap does not need to be rebalanced.
  1264. //!
  1265. //! <b>Complexity</b>: Constant.
  1266. //!
  1267. //! <b>Throws</b>: Nothing.
  1268. //!
  1269. //! <b>Note</b>: This function will break container ordering invariants if
  1270. //! with_this is not equivalent to *replace_this according to the
  1271. //! ordering and priority rules. This function is faster than erasing and inserting
  1272. //! the node, since no rebalancing or comparison is needed.
  1273. void replace_node(iterator replace_this, reference with_this)
  1274. {
  1275. node_algorithms::replace_node( get_real_value_traits().to_node_ptr(*replace_this)
  1276. , node_ptr(&priv_header())
  1277. , get_real_value_traits().to_node_ptr(with_this));
  1278. }
  1279. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1280. //! appropriate type. Otherwise the behavior is undefined.
  1281. //!
  1282. //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
  1283. //! that points to the value
  1284. //!
  1285. //! <b>Complexity</b>: Constant.
  1286. //!
  1287. //! <b>Throws</b>: Nothing.
  1288. //!
  1289. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1290. //! is stateless.
  1291. static iterator s_iterator_to(reference value)
  1292. {
  1293. BOOST_STATIC_ASSERT((!stateful_value_traits));
  1294. return iterator (value_traits::to_node_ptr(value), 0);
  1295. }
  1296. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1297. //! appropriate type. Otherwise the behavior is undefined.
  1298. //!
  1299. //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
  1300. //! set that points to the value
  1301. //!
  1302. //! <b>Complexity</b>: Constant.
  1303. //!
  1304. //! <b>Throws</b>: Nothing.
  1305. //!
  1306. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1307. //! is stateless.
  1308. static const_iterator s_iterator_to(const_reference value)
  1309. {
  1310. BOOST_STATIC_ASSERT((!stateful_value_traits));
  1311. return const_iterator (value_traits::to_node_ptr(const_cast<reference> (value)), 0);
  1312. }
  1313. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1314. //! appropriate type. Otherwise the behavior is undefined.
  1315. //!
  1316. //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
  1317. //! that points to the value
  1318. //!
  1319. //! <b>Complexity</b>: Constant.
  1320. //!
  1321. //! <b>Throws</b>: Nothing.
  1322. iterator iterator_to(reference value)
  1323. { return iterator (value_traits::to_node_ptr(value), this); }
  1324. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1325. //! appropriate type. Otherwise the behavior is undefined.
  1326. //!
  1327. //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
  1328. //! set that points to the value
  1329. //!
  1330. //! <b>Complexity</b>: Constant.
  1331. //!
  1332. //! <b>Throws</b>: Nothing.
  1333. const_iterator iterator_to(const_reference value) const
  1334. { return const_iterator (value_traits::to_node_ptr(const_cast<reference> (value)), this); }
  1335. //! <b>Requires</b>: value shall not be in a treap.
  1336. //!
  1337. //! <b>Effects</b>: init_node puts the hook of a value in a well-known default
  1338. //! state.
  1339. //!
  1340. //! <b>Throws</b>: Nothing.
  1341. //!
  1342. //! <b>Complexity</b>: Constant time.
  1343. //!
  1344. //! <b>Note</b>: This function puts the hook in the well-known default state
  1345. //! used by auto_unlink and safe hooks.
  1346. static void init_node(reference value)
  1347. { node_algorithms::init(value_traits::to_node_ptr(value)); }
  1348. /// @cond
  1349. private:
  1350. template<class Disposer>
  1351. iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
  1352. {
  1353. for(n = 0; b != e; ++n)
  1354. this->erase_and_dispose(b++, disposer);
  1355. return b.unconst();
  1356. }
  1357. iterator private_erase(const_iterator b, const_iterator e, size_type &n)
  1358. {
  1359. for(n = 0; b != e; ++n)
  1360. this->erase(b++);
  1361. return b.unconst();
  1362. }
  1363. /// @endcond
  1364. private:
  1365. static treap_impl &priv_container_from_end_iterator(const const_iterator &end_iterator)
  1366. {
  1367. header_plus_size *r = detail::parent_from_member<header_plus_size, node>
  1368. ( detail::get_pointer(end_iterator.pointed_node()), &header_plus_size::header_);
  1369. typename node_plus_pred_t::header_plus_priority_size *n =
  1370. detail::parent_from_member
  1371. < typename node_plus_pred_t::header_plus_priority_size
  1372. , header_plus_size>
  1373. (r, &node_plus_pred_t::header_plus_priority_size::header_plus_size_);
  1374. node_plus_pred_t *pn = detail::parent_from_member
  1375. < node_plus_pred_t
  1376. , typename node_plus_pred_t::header_plus_priority_size>
  1377. (n, &node_plus_pred_t::header_plus_priority_size_);
  1378. data_t *d = detail::parent_from_member<data_t, node_plus_pred_t>(pn, &data_t::node_plus_pred_);
  1379. treap_impl *tr = detail::parent_from_member<treap_impl, data_t>(d, &treap_impl::data_);
  1380. return *tr;
  1381. }
  1382. static treap_impl &priv_container_from_iterator(const const_iterator &it)
  1383. { return priv_container_from_end_iterator(it.end_iterator_from_it()); }
  1384. };
  1385. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1386. template<class T, class ...Options>
  1387. #else
  1388. template<class Config>
  1389. #endif
  1390. inline bool operator<
  1391. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1392. (const treap_impl<T, Options...> &x, const treap_impl<T, Options...> &y)
  1393. #else
  1394. (const treap_impl<Config> &x, const treap_impl<Config> &y)
  1395. #endif
  1396. { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1397. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1398. template<class T, class ...Options>
  1399. #else
  1400. template<class Config>
  1401. #endif
  1402. bool operator==
  1403. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1404. (const treap_impl<T, Options...> &x, const treap_impl<T, Options...> &y)
  1405. #else
  1406. (const treap_impl<Config> &x, const treap_impl<Config> &y)
  1407. #endif
  1408. {
  1409. typedef treap_impl<Config> tree_type;
  1410. typedef typename tree_type::const_iterator const_iterator;
  1411. if(tree_type::constant_time_size && x.size() != y.size()){
  1412. return false;
  1413. }
  1414. const_iterator end1 = x.end();
  1415. const_iterator i1 = x.begin();
  1416. const_iterator i2 = y.begin();
  1417. if(tree_type::constant_time_size){
  1418. while (i1 != end1 && *i1 == *i2) {
  1419. ++i1;
  1420. ++i2;
  1421. }
  1422. return i1 == end1;
  1423. }
  1424. else{
  1425. const_iterator end2 = y.end();
  1426. while (i1 != end1 && i2 != end2 && *i1 == *i2) {
  1427. ++i1;
  1428. ++i2;
  1429. }
  1430. return i1 == end1 && i2 == end2;
  1431. }
  1432. }
  1433. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1434. template<class T, class ...Options>
  1435. #else
  1436. template<class Config>
  1437. #endif
  1438. inline bool operator!=
  1439. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1440. (const treap_impl<T, Options...> &x, const treap_impl<T, Options...> &y)
  1441. #else
  1442. (const treap_impl<Config> &x, const treap_impl<Config> &y)
  1443. #endif
  1444. { return !(x == y); }
  1445. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1446. template<class T, class ...Options>
  1447. #else
  1448. template<class Config>
  1449. #endif
  1450. inline bool operator>
  1451. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1452. (const treap_impl<T, Options...> &x, const treap_impl<T, Options...> &y)
  1453. #else
  1454. (const treap_impl<Config> &x, const treap_impl<Config> &y)
  1455. #endif
  1456. { return y < x; }
  1457. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1458. template<class T, class ...Options>
  1459. #else
  1460. template<class Config>
  1461. #endif
  1462. inline bool operator<=
  1463. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1464. (const treap_impl<T, Options...> &x, const treap_impl<T, Options...> &y)
  1465. #else
  1466. (const treap_impl<Config> &x, const treap_impl<Config> &y)
  1467. #endif
  1468. { return !(y < x); }
  1469. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1470. template<class T, class ...Options>
  1471. #else
  1472. template<class Config>
  1473. #endif
  1474. inline bool operator>=
  1475. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1476. (const treap_impl<T, Options...> &x, const treap_impl<T, Options...> &y)
  1477. #else
  1478. (const treap_impl<Config> &x, const treap_impl<Config> &y)
  1479. #endif
  1480. { return !(x < y); }
  1481. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1482. template<class T, class ...Options>
  1483. #else
  1484. template<class Config>
  1485. #endif
  1486. inline void swap
  1487. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1488. (treap_impl<T, Options...> &x, treap_impl<T, Options...> &y)
  1489. #else
  1490. (treap_impl<Config> &x, treap_impl<Config> &y)
  1491. #endif
  1492. { x.swap(y); }
  1493. /// @cond
  1494. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1495. template<class T, class O1 = none, class O2 = none
  1496. , class O3 = none, class O4 = none
  1497. >
  1498. #else
  1499. template<class T, class ...Options>
  1500. #endif
  1501. struct make_treap_opt
  1502. {
  1503. typedef typename pack_options
  1504. < treap_set_defaults<T>,
  1505. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1506. O1, O2, O3, O4
  1507. #else
  1508. Options...
  1509. #endif
  1510. >::type packed_options;
  1511. typedef typename detail::get_value_traits
  1512. <T, typename packed_options::value_traits>::type value_traits;
  1513. typedef treap_setopt
  1514. < value_traits
  1515. , typename packed_options::compare
  1516. , typename packed_options::priority
  1517. , typename packed_options::size_type
  1518. , packed_options::constant_time_size
  1519. > type;
  1520. };
  1521. /// @endcond
  1522. //! Helper metafunction to define a \c treap that yields to the same type when the
  1523. //! same options (either explicitly or implicitly) are used.
  1524. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1525. template<class T, class ...Options>
  1526. #else
  1527. template<class T, class O1 = none, class O2 = none
  1528. , class O3 = none, class O4 = none>
  1529. #endif
  1530. struct make_trie
  1531. {
  1532. /// @cond
  1533. typedef treap_impl
  1534. < typename make_treap_opt<T,
  1535. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1536. O1, O2, O3, O4
  1537. #else
  1538. Options...
  1539. #endif
  1540. >::type
  1541. > implementation_defined;
  1542. /// @endcond
  1543. typedef implementation_defined type;
  1544. };
  1545. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1546. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1547. template<class T, class O1, class O2, class O3, class O4>
  1548. #else
  1549. template<class T, class ...Options>
  1550. #endif
  1551. class treap
  1552. : public make_trie<T,
  1553. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1554. O1, O2, O3, O4
  1555. #else
  1556. Options...
  1557. #endif
  1558. >::type
  1559. {
  1560. typedef typename make_trie
  1561. <T,
  1562. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1563. O1, O2, O3, O4
  1564. #else
  1565. Options...
  1566. #endif
  1567. >::type Base;
  1568. public:
  1569. typedef typename Base::value_compare value_compare;
  1570. typedef typename Base::priority_compare priority_compare;
  1571. typedef typename Base::value_traits value_traits;
  1572. typedef typename Base::real_value_traits real_value_traits;
  1573. typedef typename Base::iterator iterator;
  1574. typedef typename Base::const_iterator const_iterator;
  1575. //Assert if passed value traits are compatible with the type
  1576. BOOST_STATIC_ASSERT((detail::is_same<typename real_value_traits::value_type, T>::value));
  1577. treap( const value_compare &cmp = value_compare()
  1578. , const priority_compare &pcmp = priority_compare()
  1579. , const value_traits &v_traits = value_traits())
  1580. : Base(cmp, pcmp, v_traits)
  1581. {}
  1582. template<class Iterator>
  1583. treap( bool unique, Iterator b, Iterator e
  1584. , const value_compare &cmp = value_compare()
  1585. , const priority_compare &pcmp = priority_compare()
  1586. , const value_traits &v_traits = value_traits())
  1587. : Base(unique, b, e, cmp, pcmp, v_traits)
  1588. {}
  1589. static treap &container_from_end_iterator(iterator end_iterator)
  1590. { return static_cast<treap &>(Base::container_from_end_iterator(end_iterator)); }
  1591. static const treap &container_from_end_iterator(const_iterator end_iterator)
  1592. { return static_cast<const treap &>(Base::container_from_end_iterator(end_iterator)); }
  1593. static treap &container_from_it(iterator it)
  1594. { return static_cast<treap &>(Base::container_from_iterator(it)); }
  1595. static const treap &container_from_it(const_iterator it)
  1596. { return static_cast<const treap &>(Base::container_from_iterator(it)); }
  1597. };
  1598. #endif
  1599. } //namespace intrusive
  1600. } //namespace boost
  1601. #include <boost/intrusive/detail/config_end.hpp>
  1602. #endif //BOOST_INTRUSIVE_TRIE_HPP