bstree.hpp 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2013-2014
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_BSTREE_HPP
  13. #define BOOST_INTRUSIVE_BSTREE_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/intrusive_fwd.hpp>
  18. #include <boost/intrusive/bs_set_hook.hpp>
  19. #include <boost/intrusive/detail/tree_node.hpp>
  20. #include <boost/intrusive/detail/tree_iterator.hpp>
  21. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  22. #include <boost/intrusive/detail/mpl.hpp>
  23. #include <boost/intrusive/pointer_traits.hpp>
  24. #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
  25. #include <boost/intrusive/detail/empty_node_checker.hpp>
  26. #include <boost/intrusive/detail/default_header_holder.hpp>
  27. #include <boost/intrusive/detail/reverse_iterator.hpp>
  28. #include <boost/intrusive/detail/exception_disposer.hpp>
  29. #include <boost/intrusive/detail/node_cloner_disposer.hpp>
  30. #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
  31. #include <boost/intrusive/detail/simple_disposers.hpp>
  32. #include <boost/intrusive/detail/size_holder.hpp>
  33. #include <boost/intrusive/detail/algo_type.hpp>
  34. #include <boost/intrusive/detail/algorithm.hpp>
  35. #include <boost/intrusive/detail/tree_value_compare.hpp>
  36. #include <boost/intrusive/detail/get_value_traits.hpp>
  37. #include <boost/intrusive/bstree_algorithms.hpp>
  38. #include <boost/intrusive/link_mode.hpp>
  39. #include <boost/intrusive/parent_from_member.hpp>
  40. #include <boost/move/utility_core.hpp>
  41. #include <boost/move/adl_move_swap.hpp>
  42. #include <boost/intrusive/detail/minimal_pair_header.hpp>
  43. #include <cstddef> //size_t...
  44. #include <boost/intrusive/detail/minimal_less_equal_header.hpp>//less, equal_to
  45. #if defined(BOOST_HAS_PRAGMA_ONCE)
  46. # pragma once
  47. #endif
  48. namespace boost {
  49. namespace intrusive {
  50. /// @cond
  51. struct default_bstree_hook_applier
  52. { template <class T> struct apply{ typedef typename T::default_bstree_hook type; }; };
  53. template<>
  54. struct is_default_hook_tag<default_bstree_hook_applier>
  55. { static const bool value = true; };
  56. struct bstree_defaults
  57. {
  58. typedef default_bstree_hook_applier proto_value_traits;
  59. static const bool constant_time_size = true;
  60. typedef std::size_t size_type;
  61. typedef void compare;
  62. typedef void key_of_value;
  63. static const bool floating_point = true; //For sgtree
  64. typedef void priority; //For treap
  65. typedef void header_holder_type;
  66. };
  67. template<class ValueTraits, algo_types AlgoType, typename HeaderHolder>
  68. struct bstbase3
  69. {
  70. typedef ValueTraits value_traits;
  71. typedef typename value_traits::node_traits node_traits;
  72. typedef typename node_traits::node node_type;
  73. typedef typename get_algo<AlgoType, node_traits>::type node_algorithms;
  74. typedef typename node_traits::node_ptr node_ptr;
  75. typedef typename node_traits::const_node_ptr const_node_ptr;
  76. typedef tree_iterator<value_traits, false> iterator;
  77. typedef tree_iterator<value_traits, true> const_iterator;
  78. typedef boost::intrusive::reverse_iterator<iterator> reverse_iterator;
  79. typedef boost::intrusive::reverse_iterator<const_iterator> const_reverse_iterator;
  80. typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer;
  81. typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer;
  82. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::element_type) value_type;
  83. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference) reference;
  84. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference) const_reference;
  85. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type) difference_type;
  86. typedef typename detail::get_header_holder_type
  87. < value_traits,HeaderHolder >::type header_holder_type;
  88. static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
  89. static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
  90. static const bool has_container_from_iterator =
  91. detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value;
  92. struct holder_t : public ValueTraits
  93. {
  94. inline explicit holder_t(const ValueTraits &vtraits)
  95. : ValueTraits(vtraits)
  96. {}
  97. header_holder_type root;
  98. } holder;
  99. static bstbase3 &get_tree_base_from_end_iterator(const const_iterator &end_iterator)
  100. {
  101. BOOST_INTRUSIVE_STATIC_ASSERT(has_container_from_iterator);
  102. node_ptr p = end_iterator.pointed_node();
  103. header_holder_type* h = header_holder_type::get_holder(p);
  104. holder_t *holder = get_parent_from_member<holder_t, header_holder_type>(h, &holder_t::root);
  105. bstbase3 *base = get_parent_from_member<bstbase3, holder_t> (holder, &bstbase3::holder);
  106. return *base;
  107. }
  108. inline bstbase3(const ValueTraits &vtraits)
  109. : holder(vtraits)
  110. {
  111. node_algorithms::init_header(this->header_ptr());
  112. }
  113. inline node_ptr header_ptr()
  114. { return holder.root.get_node(); }
  115. inline const_node_ptr header_ptr() const
  116. { return holder.root.get_node(); }
  117. inline const value_traits &get_value_traits() const
  118. { return this->holder; }
  119. inline value_traits &get_value_traits()
  120. { return this->holder; }
  121. typedef typename boost::intrusive::value_traits_pointers
  122. <ValueTraits>::const_value_traits_ptr const_value_traits_ptr;
  123. inline const_value_traits_ptr priv_value_traits_ptr() const
  124. { return pointer_traits<const_value_traits_ptr>::pointer_to(this->get_value_traits()); }
  125. inline iterator begin() BOOST_NOEXCEPT
  126. { return iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); }
  127. inline const_iterator begin() const BOOST_NOEXCEPT
  128. { return cbegin(); }
  129. inline const_iterator cbegin() const BOOST_NOEXCEPT
  130. { return const_iterator(node_algorithms::begin_node(this->header_ptr()), this->priv_value_traits_ptr()); }
  131. inline iterator end() BOOST_NOEXCEPT
  132. { return iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); }
  133. inline const_iterator end() const BOOST_NOEXCEPT
  134. { return cend(); }
  135. inline const_iterator cend() const BOOST_NOEXCEPT
  136. { return const_iterator(node_algorithms::end_node(this->header_ptr()), this->priv_value_traits_ptr()); }
  137. inline iterator root()
  138. { return iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); }
  139. inline const_iterator root() const
  140. { return croot(); }
  141. inline const_iterator croot() const
  142. { return const_iterator(node_algorithms::root_node(this->header_ptr()), this->priv_value_traits_ptr()); }
  143. inline reverse_iterator rbegin()
  144. { return reverse_iterator(end()); }
  145. inline const_reverse_iterator rbegin() const
  146. { return const_reverse_iterator(end()); }
  147. inline const_reverse_iterator crbegin() const
  148. { return const_reverse_iterator(end()); }
  149. inline reverse_iterator rend()
  150. { return reverse_iterator(begin()); }
  151. inline const_reverse_iterator rend() const
  152. { return const_reverse_iterator(begin()); }
  153. inline const_reverse_iterator crend() const
  154. { return const_reverse_iterator(begin()); }
  155. void replace_node(iterator replace_this, reference with_this)
  156. {
  157. node_algorithms::replace_node( get_value_traits().to_node_ptr(*replace_this)
  158. , this->header_ptr()
  159. , get_value_traits().to_node_ptr(with_this));
  160. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  161. node_algorithms::init(replace_this.pointed_node());
  162. }
  163. inline void rebalance() BOOST_NOEXCEPT
  164. { node_algorithms::rebalance(this->header_ptr()); }
  165. iterator rebalance_subtree(iterator r) BOOST_NOEXCEPT
  166. { return iterator(node_algorithms::rebalance_subtree(r.pointed_node()), this->priv_value_traits_ptr()); }
  167. static iterator s_iterator_to(reference value) BOOST_NOEXCEPT
  168. {
  169. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  170. return iterator (value_traits::to_node_ptr(value), const_value_traits_ptr());
  171. }
  172. static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT
  173. {
  174. BOOST_INTRUSIVE_STATIC_ASSERT((!stateful_value_traits));
  175. return const_iterator (value_traits::to_node_ptr(*pointer_traits<pointer>::const_cast_from(pointer_traits<const_pointer>::pointer_to(value))), const_value_traits_ptr());
  176. }
  177. iterator iterator_to(reference value) BOOST_NOEXCEPT
  178. { return iterator (this->get_value_traits().to_node_ptr(value), this->priv_value_traits_ptr()); }
  179. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT
  180. { return const_iterator (this->get_value_traits().to_node_ptr(*pointer_traits<pointer>::const_cast_from(pointer_traits<const_pointer>::pointer_to(value))), this->priv_value_traits_ptr()); }
  181. inline static void init_node(reference value)
  182. { node_algorithms::init(value_traits::to_node_ptr(value)); }
  183. };
  184. template<class Less, class T>
  185. struct get_compare
  186. {
  187. typedef Less type;
  188. };
  189. template<class T>
  190. struct get_compare<void, T>
  191. {
  192. typedef ::std::less<T> type;
  193. };
  194. template<class KeyOfValue, class T>
  195. struct get_key_of_value
  196. {
  197. typedef KeyOfValue type;
  198. };
  199. template<class T>
  200. struct get_key_of_value<void, T>
  201. {
  202. typedef ::boost::intrusive::detail::identity<T> type;
  203. };
  204. template<class ValuePtr, class VoidOrKeyOfValue, class VoidOrKeyComp>
  205. struct bst_key_types
  206. {
  207. typedef typename
  208. boost::movelib::pointer_element<ValuePtr>::type value_type;
  209. typedef typename get_key_of_value
  210. < VoidOrKeyOfValue, value_type>::type key_of_value;
  211. typedef typename key_of_value::type key_type;
  212. typedef typename get_compare< VoidOrKeyComp
  213. , key_type
  214. >::type key_compare;
  215. typedef tree_value_compare
  216. <ValuePtr, key_compare, key_of_value> value_compare;
  217. };
  218. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, algo_types AlgoType, typename HeaderHolder>
  219. struct bstbase2
  220. //Put the (possibly empty) functor in the first position to get EBO in MSVC
  221. //Use public inheritance to avoid MSVC bugs with closures
  222. : public detail::ebo_functor_holder
  223. < typename bst_key_types
  224. < typename ValueTraits::pointer
  225. , VoidOrKeyOfValue
  226. , VoidOrKeyComp
  227. >::value_compare
  228. >
  229. , public bstbase3<ValueTraits, AlgoType, HeaderHolder>
  230. {
  231. typedef bstbase3<ValueTraits, AlgoType, HeaderHolder> treeheader_t;
  232. typedef bst_key_types< typename ValueTraits::pointer
  233. , VoidOrKeyOfValue
  234. , VoidOrKeyComp> key_types;
  235. typedef typename treeheader_t::value_traits value_traits;
  236. typedef typename treeheader_t::node_algorithms node_algorithms;
  237. typedef typename ValueTraits::value_type value_type;
  238. typedef typename key_types::key_type key_type;
  239. typedef typename key_types::key_of_value key_of_value;
  240. typedef typename key_types::key_compare key_compare;
  241. typedef typename key_types::value_compare value_compare;
  242. typedef typename treeheader_t::iterator iterator;
  243. typedef typename treeheader_t::const_iterator const_iterator;
  244. typedef typename treeheader_t::node_ptr node_ptr;
  245. typedef typename treeheader_t::const_node_ptr const_node_ptr;
  246. bstbase2(const key_compare &comp, const ValueTraits &vtraits)
  247. : detail::ebo_functor_holder<value_compare>(value_compare(comp)), treeheader_t(vtraits)
  248. {}
  249. const value_compare &get_comp() const
  250. { return this->get(); }
  251. value_compare &get_comp()
  252. { return this->get(); }
  253. typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer;
  254. typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer;
  255. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference) reference;
  256. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference) const_reference;
  257. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type) difference_type;
  258. typedef typename node_algorithms::insert_commit_data insert_commit_data;
  259. inline value_compare value_comp() const
  260. { return this->get_comp(); }
  261. inline key_compare key_comp() const
  262. { return this->get_comp().key_comp(); }
  263. //lower_bound
  264. inline iterator lower_bound(const key_type &key)
  265. { return this->lower_bound(key, this->key_comp()); }
  266. inline const_iterator lower_bound(const key_type &key) const
  267. { return this->lower_bound(key, this->key_comp()); }
  268. template<class KeyType, class KeyTypeKeyCompare>
  269. iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp)
  270. {
  271. return iterator(node_algorithms::lower_bound
  272. (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
  273. }
  274. template<class KeyType, class KeyTypeKeyCompare>
  275. const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const
  276. {
  277. return const_iterator(node_algorithms::lower_bound
  278. (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
  279. }
  280. //upper_bound
  281. inline iterator upper_bound(const key_type &key)
  282. { return this->upper_bound(key, this->key_comp()); }
  283. template<class KeyType, class KeyTypeKeyCompare>
  284. iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp)
  285. {
  286. return iterator(node_algorithms::upper_bound
  287. (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
  288. }
  289. inline const_iterator upper_bound(const key_type &key) const
  290. { return this->upper_bound(key, this->key_comp()); }
  291. template<class KeyType, class KeyTypeKeyCompare>
  292. const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const
  293. {
  294. return const_iterator(node_algorithms::upper_bound
  295. (this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
  296. }
  297. template<class KeyTypeKeyCompare>
  298. struct key_node_comp_ret
  299. { typedef detail::key_nodeptr_comp<KeyTypeKeyCompare, value_traits, key_of_value> type; };
  300. template<class KeyTypeKeyCompare>
  301. inline typename key_node_comp_ret<KeyTypeKeyCompare>::type key_node_comp(KeyTypeKeyCompare comp) const
  302. {
  303. return detail::key_nodeptr_comp<KeyTypeKeyCompare, value_traits, key_of_value>(comp, &this->get_value_traits());
  304. }
  305. //find
  306. inline iterator find(const key_type &key)
  307. { return this->find(key, this->key_comp()); }
  308. template<class KeyType, class KeyTypeKeyCompare>
  309. iterator find(const KeyType &key, KeyTypeKeyCompare comp)
  310. {
  311. return iterator
  312. (node_algorithms::find(this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
  313. }
  314. inline const_iterator find(const key_type &key) const
  315. { return this->find(key, this->key_comp()); }
  316. template<class KeyType, class KeyTypeKeyCompare>
  317. const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const
  318. {
  319. return const_iterator
  320. (node_algorithms::find(this->header_ptr(), key, this->key_node_comp(comp)), this->priv_value_traits_ptr());
  321. }
  322. //equal_range
  323. inline std::pair<iterator,iterator> equal_range(const key_type &key)
  324. { return this->equal_range(key, this->key_comp()); }
  325. template<class KeyType, class KeyTypeKeyCompare>
  326. std::pair<iterator,iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp)
  327. {
  328. std::pair<node_ptr, node_ptr> ret =
  329. node_algorithms::equal_range(this->header_ptr(), key, this->key_node_comp(comp));
  330. return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
  331. , iterator(ret.second, this->priv_value_traits_ptr()));
  332. }
  333. inline std::pair<const_iterator, const_iterator>
  334. equal_range(const key_type &key) const
  335. { return this->equal_range(key, this->key_comp()); }
  336. template<class KeyType, class KeyTypeKeyCompare>
  337. std::pair<const_iterator, const_iterator>
  338. equal_range(const KeyType &key, KeyTypeKeyCompare comp) const
  339. {
  340. std::pair<node_ptr, node_ptr> ret =
  341. node_algorithms::equal_range(this->header_ptr(), key, this->key_node_comp(comp));
  342. return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
  343. , const_iterator(ret.second, this->priv_value_traits_ptr()));
  344. }
  345. //lower_bound_range
  346. inline std::pair<iterator,iterator> lower_bound_range(const key_type &key)
  347. { return this->lower_bound_range(key, this->key_comp()); }
  348. template<class KeyType, class KeyTypeKeyCompare>
  349. std::pair<iterator,iterator> lower_bound_range(const KeyType &key, KeyTypeKeyCompare comp)
  350. {
  351. std::pair<node_ptr, node_ptr> ret =
  352. node_algorithms::lower_bound_range(this->header_ptr(), key, this->key_node_comp(comp));
  353. return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
  354. , iterator(ret.second, this->priv_value_traits_ptr()));
  355. }
  356. inline std::pair<const_iterator, const_iterator>
  357. lower_bound_range(const key_type &key) const
  358. { return this->lower_bound_range(key, this->key_comp()); }
  359. template<class KeyType, class KeyTypeKeyCompare>
  360. std::pair<const_iterator, const_iterator>
  361. lower_bound_range(const KeyType &key, KeyTypeKeyCompare comp) const
  362. {
  363. std::pair<node_ptr, node_ptr> ret =
  364. node_algorithms::lower_bound_range(this->header_ptr(), key, this->key_node_comp(comp));
  365. return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
  366. , const_iterator(ret.second, this->priv_value_traits_ptr()));
  367. }
  368. //bounded_range
  369. inline std::pair<iterator,iterator> bounded_range
  370. (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed)
  371. { return this->bounded_range(lower_key, upper_key, this->key_comp(), left_closed, right_closed); }
  372. template<class KeyType, class KeyTypeKeyCompare>
  373. std::pair<iterator,iterator> bounded_range
  374. (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed)
  375. {
  376. std::pair<node_ptr, node_ptr> ret =
  377. node_algorithms::bounded_range
  378. (this->header_ptr(), lower_key, upper_key, this->key_node_comp(comp), left_closed, right_closed);
  379. return std::pair<iterator, iterator>( iterator(ret.first, this->priv_value_traits_ptr())
  380. , iterator(ret.second, this->priv_value_traits_ptr()));
  381. }
  382. inline std::pair<const_iterator,const_iterator> bounded_range
  383. (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const
  384. { return this->bounded_range(lower_key, upper_key, this->key_comp(), left_closed, right_closed); }
  385. template<class KeyType, class KeyTypeKeyCompare>
  386. std::pair<const_iterator,const_iterator> bounded_range
  387. (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const
  388. {
  389. std::pair<node_ptr, node_ptr> ret =
  390. node_algorithms::bounded_range
  391. (this->header_ptr(), lower_key, upper_key, this->key_node_comp(comp), left_closed, right_closed);
  392. return std::pair<const_iterator, const_iterator>( const_iterator(ret.first, this->priv_value_traits_ptr())
  393. , const_iterator(ret.second, this->priv_value_traits_ptr()));
  394. }
  395. //insert_unique_check
  396. inline std::pair<iterator, bool> insert_unique_check
  397. (const key_type &key, insert_commit_data &commit_data)
  398. { return this->insert_unique_check(key, this->key_comp(), commit_data); }
  399. inline std::pair<iterator, bool> insert_unique_check
  400. (const_iterator hint, const key_type &key, insert_commit_data &commit_data)
  401. { return this->insert_unique_check(hint, key, this->key_comp(), commit_data); }
  402. template<class KeyType, class KeyTypeKeyCompare>
  403. BOOST_INTRUSIVE_DOC1ST(std::pair<iterator BOOST_INTRUSIVE_I bool>
  404. , typename detail::disable_if_convertible
  405. <KeyType BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I
  406. std::pair<iterator BOOST_INTRUSIVE_I bool> >::type)
  407. insert_unique_check
  408. (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data)
  409. {
  410. std::pair<node_ptr, bool> ret =
  411. (node_algorithms::insert_unique_check
  412. (this->header_ptr(), key, this->key_node_comp(comp), commit_data));
  413. return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
  414. }
  415. template<class KeyType, class KeyTypeKeyCompare>
  416. std::pair<iterator, bool> insert_unique_check
  417. (const_iterator hint, const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data)
  418. {
  419. std::pair<node_ptr, bool> ret =
  420. (node_algorithms::insert_unique_check
  421. (this->header_ptr(), hint.pointed_node(), key, this->key_node_comp(comp), commit_data));
  422. return std::pair<iterator, bool>(iterator(ret.first, this->priv_value_traits_ptr()), ret.second);
  423. }
  424. };
  425. //Due to MSVC's EBO implementation, to save space and maintain the ABI, we must put the non-empty size member
  426. //in the first position, but if size is not going to be stored then we'll use an specialization
  427. //that doesn't inherit from size_holder
  428. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, bool ConstantTimeSize, class SizeType, algo_types AlgoType, typename HeaderHolder>
  429. struct bstbase_hack
  430. : public detail::size_holder<ConstantTimeSize, SizeType>
  431. , public bstbase2 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder>
  432. {
  433. typedef bstbase2< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder> base_type;
  434. typedef typename base_type::key_compare key_compare;
  435. typedef typename base_type::value_compare value_compare;
  436. typedef SizeType size_type;
  437. typedef typename base_type::node_traits node_traits;
  438. typedef typename get_algo
  439. <AlgoType, node_traits>::type algo_type;
  440. inline bstbase_hack(const key_compare & comp, const ValueTraits &vtraits)
  441. : base_type(comp, vtraits)
  442. {
  443. this->sz_traits().set_size(size_type(0));
  444. }
  445. typedef detail::size_holder<ConstantTimeSize, SizeType> size_traits;
  446. inline size_traits &sz_traits()
  447. { return static_cast<size_traits &>(*this); }
  448. inline const size_traits &sz_traits() const
  449. { return static_cast<const size_traits &>(*this); }
  450. };
  451. //Specialization for ConstantTimeSize == false
  452. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, algo_types AlgoType, typename HeaderHolder>
  453. struct bstbase_hack<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, false, SizeType, AlgoType, HeaderHolder>
  454. : public bstbase2 < ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder>
  455. {
  456. typedef bstbase2< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, AlgoType, HeaderHolder> base_type;
  457. typedef typename base_type::value_compare value_compare;
  458. typedef typename base_type::key_compare key_compare;
  459. inline bstbase_hack(const key_compare & comp, const ValueTraits &vtraits)
  460. : base_type(comp, vtraits)
  461. {}
  462. typedef detail::size_holder<false, SizeType> size_traits;
  463. inline size_traits sz_traits() const
  464. { return size_traits(); }
  465. };
  466. //This class will
  467. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, bool ConstantTimeSize, class SizeType, algo_types AlgoType, typename HeaderHolder>
  468. struct bstbase
  469. : public bstbase_hack< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder>
  470. {
  471. typedef bstbase_hack< ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> base_type;
  472. typedef ValueTraits value_traits;
  473. typedef typename base_type::value_compare value_compare;
  474. typedef typename base_type::key_compare key_compare;
  475. typedef typename base_type::const_reference const_reference;
  476. typedef typename base_type::reference reference;
  477. typedef typename base_type::iterator iterator;
  478. typedef typename base_type::const_iterator const_iterator;
  479. typedef typename base_type::node_traits node_traits;
  480. typedef typename get_algo
  481. <AlgoType, node_traits>::type node_algorithms;
  482. typedef SizeType size_type;
  483. inline bstbase(const key_compare & comp, const ValueTraits &vtraits)
  484. : base_type(comp, vtraits)
  485. {}
  486. //Detach all inserted nodes. This will add exception safety to bstree_impl
  487. //constructors inserting elements.
  488. ~bstbase()
  489. {
  490. if(is_safe_autounlink<value_traits::link_mode>::value){
  491. node_algorithms::clear_and_dispose
  492. ( this->header_ptr()
  493. , detail::node_disposer<detail::null_disposer, value_traits, AlgoType>
  494. (detail::null_disposer(), &this->get_value_traits()));
  495. node_algorithms::init(this->header_ptr());
  496. }
  497. }
  498. };
  499. /// @endcond
  500. //! The class template bstree is an unbalanced intrusive binary search tree
  501. //! container. The no-throw guarantee holds only, if the key_compare object
  502. //! doesn't throw.
  503. //!
  504. //! The complexity guarantees only hold if the tree is balanced, logarithmic
  505. //! complexity would increase to linear if the tree is totally unbalanced.
  506. //!
  507. //! The template parameter \c T is the type to be managed by the container.
  508. //! The user can specify additional options and if no options are provided
  509. //! default options are used.
  510. //!
  511. //! The container supports the following options:
  512. //! \c base_hook<>/member_hook<>/value_traits<>,
  513. //! \c constant_time_size<>, \c size_type<> and
  514. //! \c compare<>.
  515. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  516. template<class T, class ...Options>
  517. #else
  518. template<class ValueTraits, class VoidOrKeyOfValue, class VoidOrKeyComp, class SizeType, bool ConstantTimeSize, algo_types AlgoType, typename HeaderHolder>
  519. #endif
  520. class bstree_impl
  521. : public bstbase<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder>
  522. {
  523. public:
  524. /// @cond
  525. typedef bstbase<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, ConstantTimeSize, SizeType, AlgoType, HeaderHolder> data_type;
  526. typedef tree_iterator<ValueTraits, false> iterator_type;
  527. typedef tree_iterator<ValueTraits, true> const_iterator_type;
  528. /// @endcond
  529. typedef BOOST_INTRUSIVE_IMPDEF(ValueTraits) value_traits;
  530. typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::pointer) pointer;
  531. typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::const_pointer) const_pointer;
  532. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::element_type) value_type;
  533. typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_type) key_type;
  534. typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_of_value) key_of_value;
  535. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<pointer>::reference) reference;
  536. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::reference) const_reference;
  537. typedef BOOST_INTRUSIVE_IMPDEF(typename pointer_traits<const_pointer>::difference_type) difference_type;
  538. typedef BOOST_INTRUSIVE_IMPDEF(SizeType) size_type;
  539. typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::value_compare) value_compare;
  540. typedef BOOST_INTRUSIVE_IMPDEF(typename data_type::key_compare) key_compare;
  541. typedef BOOST_INTRUSIVE_IMPDEF(iterator_type) iterator;
  542. typedef BOOST_INTRUSIVE_IMPDEF(const_iterator_type) const_iterator;
  543. typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator<iterator>) reverse_iterator;
  544. typedef BOOST_INTRUSIVE_IMPDEF(boost::intrusive::reverse_iterator<const_iterator>) const_reverse_iterator;
  545. typedef BOOST_INTRUSIVE_IMPDEF(typename value_traits::node_traits) node_traits;
  546. typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node) node;
  547. typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::node_ptr) node_ptr;
  548. typedef BOOST_INTRUSIVE_IMPDEF(typename node_traits::const_node_ptr) const_node_ptr;
  549. /// @cond
  550. typedef typename get_algo<AlgoType, node_traits>::type algo_type;
  551. /// @endcond
  552. typedef BOOST_INTRUSIVE_IMPDEF(algo_type) node_algorithms;
  553. static const bool constant_time_size = ConstantTimeSize;
  554. static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
  555. /// @cond
  556. private:
  557. //noncopyable
  558. BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree_impl)
  559. static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
  560. //Constant-time size is incompatible with auto-unlink hooks!
  561. BOOST_INTRUSIVE_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink)));
  562. protected:
  563. /// @endcond
  564. public:
  565. typedef typename node_algorithms::insert_commit_data insert_commit_data;
  566. //! <b>Effects</b>: Constructs an empty container.
  567. //!
  568. //! <b>Complexity</b>: Constant.
  569. //!
  570. //! <b>Throws</b>: If value_traits::node_traits::node
  571. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  572. //! or the copy constructor of the key_compare object throws. Basic guarantee.
  573. bstree_impl()
  574. : data_type(key_compare(), value_traits())
  575. {}
  576. //! <b>Effects</b>: Constructs an empty container with given comparison and traits.
  577. //!
  578. //! <b>Complexity</b>: Constant.
  579. //!
  580. //! <b>Throws</b>: If value_traits::node_traits::node
  581. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  582. //! or the copy constructor of the key_compare object throws. Basic guarantee.
  583. explicit bstree_impl( const key_compare &cmp, const value_traits &v_traits = value_traits())
  584. : data_type(cmp, v_traits)
  585. {}
  586. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
  587. //! cmp must be a comparison function that induces a strict weak ordering.
  588. //!
  589. //! <b>Effects</b>: Constructs an empty container and inserts elements from
  590. //! [b, e).
  591. //!
  592. //! <b>Complexity</b>: Linear in N if [b, e) is already sorted using
  593. //! comp and otherwise N * log N, where N is the distance between first and last.
  594. //!
  595. //! <b>Throws</b>: If value_traits::node_traits::node
  596. //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  597. //! or the copy constructor/operator() of the key_compare object throws. Basic guarantee.
  598. template<class Iterator>
  599. bstree_impl( bool unique, Iterator b, Iterator e
  600. , const key_compare &cmp = key_compare()
  601. , const value_traits &v_traits = value_traits())
  602. : data_type(cmp, v_traits)
  603. {
  604. //bstbase releases elements in case of exceptions
  605. if(unique)
  606. this->insert_unique(b, e);
  607. else
  608. this->insert_equal(b, e);
  609. }
  610. //! <b>Effects</b>: Constructs a container moving resources from another container.
  611. //! Internal comparison object and value traits are move constructed and
  612. //! nodes belonging to x (except the node representing the "end") are linked to *this.
  613. //!
  614. //! <b>Complexity</b>: Constant.
  615. //!
  616. //! <b>Throws</b>: If value_traits::node_traits::node's
  617. //! move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
  618. //! or the move constructor of the comparison objet throws.
  619. bstree_impl(BOOST_RV_REF(bstree_impl) x)
  620. : data_type(::boost::move(x.get_comp()), ::boost::move(x.get_value_traits()))
  621. {
  622. this->swap(x);
  623. }
  624. //! <b>Effects</b>: Equivalent to swap
  625. //!
  626. inline bstree_impl& operator=(BOOST_RV_REF(bstree_impl) x)
  627. { this->swap(x); return *this; }
  628. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  629. //! <b>Effects</b>: Detaches all elements from this. The objects in the set
  630. //! are not deleted (i.e. no destructors are called), but the nodes according to
  631. //! the value_traits template parameter are reinitialized and thus can be reused.
  632. //!
  633. //! <b>Complexity</b>: Linear to elements contained in *this.
  634. //!
  635. //! <b>Throws</b>: Nothing.
  636. ~bstree_impl()
  637. {}
  638. //! <b>Effects</b>: Returns an iterator pointing to the beginning of the container.
  639. //!
  640. //! <b>Complexity</b>: Constant.
  641. //!
  642. //! <b>Throws</b>: Nothing.
  643. iterator begin() BOOST_NOEXCEPT;
  644. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the container.
  645. //!
  646. //! <b>Complexity</b>: Constant.
  647. //!
  648. //! <b>Throws</b>: Nothing.
  649. const_iterator begin() const BOOST_NOEXCEPT;
  650. //! <b>Effects</b>: Returns a const_iterator pointing to the beginning of the container.
  651. //!
  652. //! <b>Complexity</b>: Constant.
  653. //!
  654. //! <b>Throws</b>: Nothing.
  655. const_iterator cbegin() const BOOST_NOEXCEPT;
  656. //! <b>Effects</b>: Returns an iterator pointing to the end of the container.
  657. //!
  658. //! <b>Complexity</b>: Constant.
  659. //!
  660. //! <b>Throws</b>: Nothing.
  661. iterator end() BOOST_NOEXCEPT;
  662. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the container.
  663. //!
  664. //! <b>Complexity</b>: Constant.
  665. //!
  666. //! <b>Throws</b>: Nothing.
  667. const_iterator end() const BOOST_NOEXCEPT;
  668. //! <b>Effects</b>: Returns a const_iterator pointing to the end of the container.
  669. //!
  670. //! <b>Complexity</b>: Constant.
  671. //!
  672. //! <b>Throws</b>: Nothing.
  673. const_iterator cend() const BOOST_NOEXCEPT;
  674. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning of the
  675. //! reversed container.
  676. //!
  677. //! <b>Complexity</b>: Constant.
  678. //!
  679. //! <b>Throws</b>: Nothing.
  680. reverse_iterator rbegin() BOOST_NOEXCEPT;
  681. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  682. //! of the reversed container.
  683. //!
  684. //! <b>Complexity</b>: Constant.
  685. //!
  686. //! <b>Throws</b>: Nothing.
  687. const_reverse_iterator rbegin() const BOOST_NOEXCEPT;
  688. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  689. //! of the reversed container.
  690. //!
  691. //! <b>Complexity</b>: Constant.
  692. //!
  693. //! <b>Throws</b>: Nothing.
  694. const_reverse_iterator crbegin() const BOOST_NOEXCEPT;
  695. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  696. //! of the reversed container.
  697. //!
  698. //! <b>Complexity</b>: Constant.
  699. //!
  700. //! <b>Throws</b>: Nothing.
  701. reverse_iterator rend() BOOST_NOEXCEPT;
  702. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  703. //! of the reversed container.
  704. //!
  705. //! <b>Complexity</b>: Constant.
  706. //!
  707. //! <b>Throws</b>: Nothing.
  708. const_reverse_iterator rend() const BOOST_NOEXCEPT;
  709. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  710. //! of the reversed container.
  711. //!
  712. //! <b>Complexity</b>: Constant.
  713. //!
  714. //! <b>Throws</b>: Nothing.
  715. const_reverse_iterator crend() const BOOST_NOEXCEPT;
  716. //! <b>Effects</b>: Returns a iterator pointing to the root node of the container or end() if not present.
  717. //!
  718. //! <b>Complexity</b>: Constant.
  719. //!
  720. //! <b>Throws</b>: Nothing.
  721. iterator root() BOOST_NOEXCEPT;
  722. //! <b>Effects</b>: Returns a const_iterator pointing to the root node of the container or cend() if not present.
  723. //!
  724. //! <b>Complexity</b>: Constant.
  725. //!
  726. //! <b>Throws</b>: Nothing.
  727. const_iterator root() const BOOST_NOEXCEPT;
  728. //! <b>Effects</b>: Returns a const_iterator pointing to the root node of the container or cend() if not present.
  729. //!
  730. //! <b>Complexity</b>: Constant.
  731. //!
  732. //! <b>Throws</b>: Nothing.
  733. const_iterator croot() const BOOST_NOEXCEPT;
  734. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  735. //! <b>Precondition</b>: end_iterator must be a valid end iterator
  736. //! of the container.
  737. //!
  738. //! <b>Effects</b>: Returns a const reference to the container associated to the end iterator
  739. //!
  740. //! <b>Throws</b>: Nothing.
  741. //!
  742. //! <b>Complexity</b>: Constant.
  743. static bstree_impl &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  744. {
  745. return static_cast<bstree_impl&>
  746. (data_type::get_tree_base_from_end_iterator(end_iterator));
  747. }
  748. //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
  749. //! of the container.
  750. //!
  751. //! <b>Effects</b>: Returns a const reference to the container associated to the iterator
  752. //!
  753. //! <b>Throws</b>: Nothing.
  754. //!
  755. //! <b>Complexity</b>: Constant.
  756. static const bstree_impl &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  757. {
  758. return static_cast<bstree_impl&>
  759. (data_type::get_tree_base_from_end_iterator(end_iterator));
  760. }
  761. //! <b>Precondition</b>: it must be a valid iterator
  762. //! of the container.
  763. //!
  764. //! <b>Effects</b>: Returns a const reference to the container associated to the iterator
  765. //!
  766. //! <b>Throws</b>: Nothing.
  767. //!
  768. //! <b>Complexity</b>: Logarithmic.
  769. static bstree_impl &container_from_iterator(iterator it) BOOST_NOEXCEPT
  770. { return container_from_end_iterator(it.end_iterator_from_it()); }
  771. //! <b>Precondition</b>: it must be a valid end const_iterator
  772. //! of container.
  773. //!
  774. //! <b>Effects</b>: Returns a const reference to the container associated to the end iterator
  775. //!
  776. //! <b>Throws</b>: Nothing.
  777. //!
  778. //! <b>Complexity</b>: Logarithmic.
  779. static const bstree_impl &container_from_iterator(const_iterator it) BOOST_NOEXCEPT
  780. { return container_from_end_iterator(it.end_iterator_from_it()); }
  781. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  782. //! <b>Effects</b>: Returns the key_compare object used by the container.
  783. //!
  784. //! <b>Complexity</b>: Constant.
  785. //!
  786. //! <b>Throws</b>: If key_compare copy-constructor throws.
  787. key_compare key_comp() const;
  788. //! <b>Effects</b>: Returns the value_compare object used by the container.
  789. //!
  790. //! <b>Complexity</b>: Constant.
  791. //!
  792. //! <b>Throws</b>: If value_compare copy-constructor throws.
  793. value_compare value_comp() const;
  794. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  795. //! <b>Effects</b>: Returns true if the container is empty.
  796. //!
  797. //! <b>Complexity</b>: Constant.
  798. //!
  799. //! <b>Throws</b>: Nothing.
  800. bool empty() const BOOST_NOEXCEPT
  801. {
  802. if(ConstantTimeSize){
  803. return !this->data_type::sz_traits().get_size();
  804. }
  805. else{
  806. return algo_type::unique(this->header_ptr());
  807. }
  808. }
  809. //! <b>Effects</b>: Returns the number of elements stored in the container.
  810. //!
  811. //! <b>Complexity</b>: Linear to elements contained in *this
  812. //! if constant-time size option is disabled. Constant time otherwise.
  813. //!
  814. //! <b>Throws</b>: Nothing.
  815. size_type size() const BOOST_NOEXCEPT
  816. {
  817. BOOST_IF_CONSTEXPR(constant_time_size)
  818. return this->sz_traits().get_size();
  819. else{
  820. return (size_type)node_algorithms::size(this->header_ptr());
  821. }
  822. }
  823. //! <b>Effects</b>: Swaps the contents of two containers.
  824. //!
  825. //! <b>Complexity</b>: Constant.
  826. //!
  827. //! <b>Throws</b>: If the comparison functor's swap call throws.
  828. void swap(bstree_impl& other)
  829. {
  830. //This can throw
  831. ::boost::adl_move_swap(this->get_comp(), other.get_comp());
  832. //These can't throw
  833. node_algorithms::swap_tree(this->header_ptr(), node_ptr(other.header_ptr()));
  834. this->sz_traits().swap(other.sz_traits());
  835. }
  836. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  837. //! Cloner should yield to nodes equivalent to the original nodes.
  838. //!
  839. //! <b>Effects</b>: Erases all the elements from *this
  840. //! calling Disposer::operator()(pointer), clones all the
  841. //! elements from src calling Cloner::operator()(const_reference )
  842. //! and inserts them on *this. Copies the predicate from the source container.
  843. //!
  844. //! If cloner throws, all cloned elements are unlinked and disposed
  845. //! calling Disposer::operator()(pointer).
  846. //!
  847. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  848. //!
  849. //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
  850. template <class Cloner, class Disposer>
  851. void clone_from(const bstree_impl &src, Cloner cloner, Disposer disposer)
  852. {
  853. this->clear_and_dispose(disposer);
  854. if(!src.empty()){
  855. detail::exception_disposer<bstree_impl, Disposer>
  856. rollback(*this, disposer);
  857. node_algorithms::clone
  858. (src.header_ptr()
  859. ,this->header_ptr()
  860. ,detail::node_cloner <Cloner, value_traits, AlgoType>(cloner, &this->get_value_traits())
  861. ,detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
  862. this->sz_traits().set_size(src.sz_traits().get_size());
  863. this->get_comp() = src.get_comp();
  864. rollback.release();
  865. }
  866. }
  867. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  868. //! Cloner should yield to nodes equivalent to the original nodes.
  869. //!
  870. //! <b>Effects</b>: Erases all the elements from *this
  871. //! calling Disposer::operator()(pointer), clones all the
  872. //! elements from src calling Cloner::operator()(reference)
  873. //! and inserts them on *this. Copies the predicate from the source container.
  874. //!
  875. //! If cloner throws, all cloned elements are unlinked and disposed
  876. //! calling Disposer::operator()(pointer).
  877. //!
  878. //! <b>Complexity</b>: Linear to erased plus inserted elements.
  879. //!
  880. //! <b>Throws</b>: If cloner throws or predicate copy assignment throws. Basic guarantee.
  881. //!
  882. //! <b>Note</b>: This version can modify the source container, useful to implement
  883. //! move semantics.
  884. template <class Cloner, class Disposer>
  885. void clone_from(BOOST_RV_REF(bstree_impl) src, Cloner cloner, Disposer disposer)
  886. {
  887. this->clear_and_dispose(disposer);
  888. if(!src.empty()){
  889. detail::exception_disposer<bstree_impl, Disposer>
  890. rollback(*this, disposer);
  891. node_algorithms::clone
  892. (src.header_ptr()
  893. ,this->header_ptr()
  894. ,detail::node_cloner <Cloner, value_traits, AlgoType, false>(cloner, &this->get_value_traits())
  895. ,detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
  896. this->sz_traits().set_size(src.sz_traits().get_size());
  897. this->get_comp() = src.get_comp();
  898. rollback.release();
  899. }
  900. }
  901. //! <b>Requires</b>: value must be an lvalue
  902. //!
  903. //! <b>Effects</b>: Inserts value into the container before the upper bound.
  904. //!
  905. //! <b>Complexity</b>: Average complexity for insert element is at
  906. //! most logarithmic.
  907. //!
  908. //! <b>Throws</b>: If the internal key_compare ordering function throws. Strong guarantee.
  909. //!
  910. //! <b>Note</b>: Does not affect the validity of iterators and references.
  911. //! No copy-constructors are called.
  912. iterator insert_equal(reference value)
  913. {
  914. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  915. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  916. iterator ret(node_algorithms::insert_equal_upper_bound
  917. (this->header_ptr(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
  918. this->sz_traits().increment();
  919. return ret;
  920. }
  921. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  922. //! a valid iterator.
  923. //!
  924. //! <b>Effects</b>: Inserts x into the container, using "hint" as a hint to
  925. //! where it will be inserted. If "hint" is the upper_bound
  926. //! the insertion takes constant time (two comparisons in the worst case)
  927. //!
  928. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  929. //! constant time if t is inserted immediately before hint.
  930. //!
  931. //! <b>Throws</b>: If the internal key_compare ordering function throws. Strong guarantee.
  932. //!
  933. //! <b>Note</b>: Does not affect the validity of iterators and references.
  934. //! No copy-constructors are called.
  935. iterator insert_equal(const_iterator hint, reference value)
  936. {
  937. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  938. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  939. iterator ret(node_algorithms::insert_equal
  940. (this->header_ptr(), hint.pointed_node(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
  941. this->sz_traits().increment();
  942. return ret;
  943. }
  944. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  945. //! of type value_type.
  946. //!
  947. //! <b>Effects</b>: Inserts a each element of a range into the container
  948. //! before the upper bound of the key of each element.
  949. //!
  950. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  951. //! size of the range. However, it is linear in N if the range is already sorted
  952. //! by value_comp().
  953. //!
  954. //! <b>Throws</b>: If the comparison functor call throws.
  955. //!
  956. //! <b>Note</b>: Does not affect the validity of iterators and references.
  957. //! No copy-constructors are called.
  958. template<class Iterator>
  959. void insert_equal(Iterator b, Iterator e)
  960. {
  961. iterator iend(this->end());
  962. for (; b != e; ++b)
  963. this->insert_equal(iend, *b);
  964. }
  965. //! <b>Requires</b>: value must be an lvalue
  966. //!
  967. //! <b>Effects</b>: Inserts value into the container if the value
  968. //! is not already present.
  969. //!
  970. //! <b>Complexity</b>: Average complexity for insert element is at
  971. //! most logarithmic.
  972. //!
  973. //! <b>Throws</b>: If the comparison functor call throws.
  974. //!
  975. //! <b>Note</b>: Does not affect the validity of iterators and references.
  976. //! No copy-constructors are called.
  977. std::pair<iterator, bool> insert_unique(reference value)
  978. {
  979. insert_commit_data commit_data;
  980. std::pair<node_ptr, bool> ret =
  981. (node_algorithms::insert_unique_check
  982. (this->header_ptr(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
  983. return std::pair<iterator, bool>
  984. ( ret.second ? this->insert_unique_commit(value, commit_data)
  985. : iterator(ret.first, this->priv_value_traits_ptr())
  986. , ret.second);
  987. }
  988. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  989. //! a valid iterator
  990. //!
  991. //! <b>Effects</b>: Tries to insert x into the container, using "hint" as a hint
  992. //! to where it will be inserted.
  993. //!
  994. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  995. //! constant time (two comparisons in the worst case)
  996. //! if t is inserted immediately before hint.
  997. //!
  998. //! <b>Throws</b>: If the comparison functor call throws.
  999. //!
  1000. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1001. //! No copy-constructors are called.
  1002. iterator insert_unique(const_iterator hint, reference value)
  1003. {
  1004. insert_commit_data commit_data;
  1005. std::pair<node_ptr, bool> ret =
  1006. (node_algorithms::insert_unique_check
  1007. (this->header_ptr(), hint.pointed_node(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
  1008. return ret.second ? this->insert_unique_commit(value, commit_data)
  1009. : iterator(ret.first, this->priv_value_traits_ptr());
  1010. }
  1011. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  1012. //! of type value_type.
  1013. //!
  1014. //! <b>Effects</b>: Tries to insert each element of a range into the container.
  1015. //!
  1016. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  1017. //! size of the range. However, it is linear in N if the range is already sorted
  1018. //! by value_comp().
  1019. //!
  1020. //! <b>Throws</b>: If the comparison functor call throws.
  1021. //!
  1022. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1023. //! No copy-constructors are called.
  1024. template<class Iterator>
  1025. void insert_unique(Iterator b, Iterator e)
  1026. {
  1027. if(this->empty()){
  1028. iterator iend(this->end());
  1029. for (; b != e; ++b)
  1030. this->insert_unique(iend, *b);
  1031. }
  1032. else{
  1033. for (; b != e; ++b)
  1034. this->insert_unique(*b);
  1035. }
  1036. }
  1037. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1038. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1039. //! a user provided key instead of the value itself.
  1040. //!
  1041. //! <b>Returns</b>: If there is an equivalent value
  1042. //! returns a pair containing an iterator to the already present value
  1043. //! and false. If the value can be inserted returns true in the returned
  1044. //! pair boolean and fills "commit_data" that is meant to be used with
  1045. //! the "insert_commit" function.
  1046. //!
  1047. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  1048. //!
  1049. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1050. std::pair<iterator, bool> insert_unique_check(const key_type &key, insert_commit_data &commit_data);
  1051. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1052. //! a user provided key instead of the value itself, using "hint"
  1053. //! as a hint to where it will be inserted.
  1054. //!
  1055. //! <b>Returns</b>: If there is an equivalent value
  1056. //! returns a pair containing an iterator to the already present value
  1057. //! and false. If the value can be inserted returns true in the returned
  1058. //! pair boolean and fills "commit_data" that is meant to be used with
  1059. //! the "insert_commit" function.
  1060. //!
  1061. //! <b>Complexity</b>: Logarithmic in general, but it's amortized
  1062. //! constant time if t is inserted immediately before hint.
  1063. //!
  1064. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1065. std::pair<iterator, bool> insert_unique_check(const_iterator hint, const key_type &key, insert_commit_data &commit_data);
  1066. //! <b>Requires</b>: comp must be a comparison function that induces
  1067. //! the same strict weak ordering as key_compare. The difference is that
  1068. //! comp compares an arbitrary key with the contained values.
  1069. //!
  1070. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1071. //! a user provided key instead of the value itself.
  1072. //!
  1073. //! <b>Returns</b>: If there is an equivalent value
  1074. //! returns a pair containing an iterator to the already present value
  1075. //! and false. If the value can be inserted returns true in the returned
  1076. //! pair boolean and fills "commit_data" that is meant to be used with
  1077. //! the "insert_commit" function.
  1078. //!
  1079. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  1080. //!
  1081. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1082. //!
  1083. //! <b>Notes</b>: This function is used to improve performance when constructing
  1084. //! a value_type is expensive: if there is an equivalent value
  1085. //! the constructed object must be discarded. Many times, the part of the
  1086. //! node that is used to impose the order is much cheaper to construct
  1087. //! than the value_type and this function offers the possibility to use that
  1088. //! part to check if the insertion will be successful.
  1089. //!
  1090. //! If the check is successful, the user can construct the value_type and use
  1091. //! "insert_commit" to insert the object in constant-time. This gives a total
  1092. //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
  1093. //!
  1094. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  1095. //! objects are inserted or erased from the container.
  1096. template<class KeyType, class KeyTypeKeyCompare>
  1097. std::pair<iterator, bool> insert_unique_check
  1098. (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data);
  1099. //! <b>Requires</b>: comp must be a comparison function that induces
  1100. //! the same strict weak ordering as key_compare. The difference is that
  1101. //! comp compares an arbitrary key with the contained values.
  1102. //!
  1103. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1104. //! a user provided key instead of the value itself, using "hint"
  1105. //! as a hint to where it will be inserted.
  1106. //!
  1107. //! <b>Returns</b>: If there is an equivalent value
  1108. //! returns a pair containing an iterator to the already present value
  1109. //! and false. If the value can be inserted returns true in the returned
  1110. //! pair boolean and fills "commit_data" that is meant to be used with
  1111. //! the "insert_commit" function.
  1112. //!
  1113. //! <b>Complexity</b>: Logarithmic in general, but it's amortized
  1114. //! constant time if t is inserted immediately before hint.
  1115. //!
  1116. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1117. //!
  1118. //! <b>Notes</b>: This function is used to improve performance when constructing
  1119. //! a value_type is expensive: if there is an equivalent value
  1120. //! the constructed object must be discarded. Many times, the part of the
  1121. //! constructing that is used to impose the order is much cheaper to construct
  1122. //! than the value_type and this function offers the possibility to use that key
  1123. //! to check if the insertion will be successful.
  1124. //!
  1125. //! If the check is successful, the user can construct the value_type and use
  1126. //! "insert_commit" to insert the object in constant-time. This can give a total
  1127. //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)).
  1128. //!
  1129. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  1130. //! objects are inserted or erased from the container.
  1131. template<class KeyType, class KeyTypeKeyCompare>
  1132. std::pair<iterator, bool> insert_unique_check
  1133. (const_iterator hint, const KeyType &key
  1134. ,KeyTypeKeyCompare comp, insert_commit_data &commit_data);
  1135. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1136. //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
  1137. //! must have been obtained from a previous call to "insert_check".
  1138. //! No objects should have been inserted or erased from the container between
  1139. //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
  1140. //!
  1141. //! <b>Effects</b>: Inserts the value in the container using the information obtained
  1142. //! from the "commit_data" that a previous "insert_check" filled.
  1143. //!
  1144. //! <b>Returns</b>: An iterator to the newly inserted object.
  1145. //!
  1146. //! <b>Complexity</b>: Constant time.
  1147. //!
  1148. //! <b>Throws</b>: Nothing.
  1149. //!
  1150. //! <b>Notes</b>: This function has only sense if a "insert_check" has been
  1151. //! previously executed to fill "commit_data". No value should be inserted or
  1152. //! erased between the "insert_check" and "insert_commit" calls.
  1153. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) BOOST_NOEXCEPT
  1154. {
  1155. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1156. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1157. #if !(defined(BOOST_DISABLE_ASSERTS) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && defined(NDEBUG) ))
  1158. //Test insertion position is correct
  1159. iterator p(commit_data.node, this->priv_value_traits_ptr());
  1160. if(!commit_data.link_left){
  1161. ++p;
  1162. }
  1163. //Check if the insertion point is correct to detect wrong
  1164. //uses insert_unique_check
  1165. BOOST_ASSERT(( p == this->end() || !this->get_comp()(*p, value) ));
  1166. BOOST_ASSERT(( p == this->begin() || !this->get_comp()(value, *--p) ));
  1167. #endif
  1168. node_algorithms::insert_unique_commit
  1169. (this->header_ptr(), to_insert, commit_data);
  1170. this->sz_traits().increment();
  1171. return iterator(to_insert, this->priv_value_traits_ptr());
  1172. }
  1173. //! <b>Requires</b>: value must be an lvalue, "pos" must be
  1174. //! a valid iterator (or end) and must be the succesor of value
  1175. //! once inserted according to the predicate
  1176. //!
  1177. //! <b>Effects</b>: Inserts x into the container before "pos".
  1178. //!
  1179. //! <b>Complexity</b>: Constant time.
  1180. //!
  1181. //! <b>Throws</b>: Nothing.
  1182. //!
  1183. //! <b>Note</b>: This function does not check preconditions so if "pos" is not
  1184. //! the successor of "value" container ordering invariant will be broken.
  1185. //! This is a low-level function to be used only for performance reasons
  1186. //! by advanced users.
  1187. iterator insert_before(const_iterator pos, reference value) BOOST_NOEXCEPT
  1188. {
  1189. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1190. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1191. this->sz_traits().increment();
  1192. return iterator(node_algorithms::insert_before
  1193. (this->header_ptr(), pos.pointed_node(), to_insert), this->priv_value_traits_ptr());
  1194. }
  1195. //! <b>Requires</b>: value must be an lvalue, and it must be no less
  1196. //! than the greatest inserted key
  1197. //!
  1198. //! <b>Effects</b>: Inserts x into the container in the last position.
  1199. //!
  1200. //! <b>Complexity</b>: Constant time.
  1201. //!
  1202. //! <b>Throws</b>: Nothing.
  1203. //!
  1204. //! <b>Note</b>: This function does not check preconditions so if value is
  1205. //! less than the greatest inserted key container ordering invariant will be broken.
  1206. //! This function is slightly more efficient than using "insert_before".
  1207. //! This is a low-level function to be used only for performance reasons
  1208. //! by advanced users.
  1209. void push_back(reference value) BOOST_NOEXCEPT
  1210. {
  1211. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1212. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1213. this->sz_traits().increment();
  1214. node_algorithms::push_back(this->header_ptr(), to_insert);
  1215. }
  1216. //! <b>Requires</b>: value must be an lvalue, and it must be no greater
  1217. //! than the minimum inserted key
  1218. //!
  1219. //! <b>Effects</b>: Inserts x into the container in the first position.
  1220. //!
  1221. //! <b>Complexity</b>: Constant time.
  1222. //!
  1223. //! <b>Throws</b>: Nothing.
  1224. //!
  1225. //! <b>Note</b>: This function does not check preconditions so if value is
  1226. //! greater than the minimum inserted key container ordering invariant will be broken.
  1227. //! This function is slightly more efficient than using "insert_before".
  1228. //! This is a low-level function to be used only for performance reasons
  1229. //! by advanced users.
  1230. void push_front(reference value) BOOST_NOEXCEPT
  1231. {
  1232. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1233. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1234. this->sz_traits().increment();
  1235. node_algorithms::push_front(this->header_ptr(), to_insert);
  1236. }
  1237. //! <b>Effects</b>: Erases the element pointed to by i.
  1238. //!
  1239. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  1240. //!
  1241. //! <b>Throws</b>: Nothing.
  1242. //!
  1243. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1244. //! to the erased elements. No destructors are called.
  1245. iterator erase(const_iterator i) BOOST_NOEXCEPT
  1246. {
  1247. const_iterator ret(i);
  1248. ++ret;
  1249. node_ptr to_erase(i.pointed_node());
  1250. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(to_erase));
  1251. node_algorithms::erase(this->header_ptr(), to_erase);
  1252. this->sz_traits().decrement();
  1253. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  1254. node_algorithms::init(to_erase);
  1255. return ret.unconst();
  1256. }
  1257. //! <b>Effects</b>: Erases the range pointed to by b end e.
  1258. //!
  1259. //! <b>Complexity</b>: Average complexity for erase range is at most
  1260. //! O(log(size() + N)), where N is the number of elements in the range.
  1261. //!
  1262. //! <b>Throws</b>: Nothing.
  1263. //!
  1264. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1265. //! to the erased elements. No destructors are called.
  1266. iterator erase(const_iterator b, const_iterator e) BOOST_NOEXCEPT
  1267. { size_type n; return this->private_erase(b, e, n); }
  1268. //! <b>Effects</b>: Erases all the elements with the given value.
  1269. //!
  1270. //! <b>Returns</b>: The number of erased elements.
  1271. //!
  1272. //! <b>Complexity</b>: O(log(size() + N).
  1273. //!
  1274. //! <b>Throws</b>: Nothing.
  1275. //!
  1276. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1277. //! to the erased elements. No destructors are called.
  1278. size_type erase(const key_type &key) BOOST_NOEXCEPT
  1279. { return this->erase(key, this->key_comp()); }
  1280. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1281. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1282. //! with nk the key_type of a value_type inserted into `*this`.
  1283. //!
  1284. //! <b>Effects</b>: Erases all the elements with the given key.
  1285. //! according to the comparison functor "comp".
  1286. //!
  1287. //! <b>Returns</b>: The number of erased elements.
  1288. //!
  1289. //! <b>Complexity</b>: O(log(size() + N).
  1290. //!
  1291. //! <b>Throws</b>: Nothing.
  1292. //!
  1293. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1294. //! to the erased elements. No destructors are called.
  1295. template<class KeyType, class KeyTypeKeyCompare>
  1296. BOOST_INTRUSIVE_DOC1ST(size_type
  1297. , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
  1298. erase(const KeyType& key, KeyTypeKeyCompare comp)
  1299. {
  1300. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  1301. size_type n;
  1302. this->private_erase(p.first, p.second, n);
  1303. return n;
  1304. }
  1305. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1306. //!
  1307. //! <b>Effects</b>: Erases the element pointed to by i.
  1308. //! Disposer::operator()(pointer) is called for the removed element.
  1309. //!
  1310. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  1311. //!
  1312. //! <b>Throws</b>: Nothing.
  1313. //!
  1314. //! <b>Note</b>: Invalidates the iterators
  1315. //! to the erased elements.
  1316. template<class Disposer>
  1317. iterator erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
  1318. {
  1319. node_ptr to_erase(i.pointed_node());
  1320. iterator ret(this->erase(i));
  1321. disposer(this->get_value_traits().to_value_ptr(to_erase));
  1322. return ret;
  1323. }
  1324. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1325. //!
  1326. //! <b>Effects</b>: Erases all the elements with the given value.
  1327. //! Disposer::operator()(pointer) is called for the removed elements.
  1328. //!
  1329. //! <b>Returns</b>: The number of erased elements.
  1330. //!
  1331. //! <b>Complexity</b>: O(log(size() + N).
  1332. //!
  1333. //! <b>Throws</b>: Nothing.
  1334. //!
  1335. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1336. //! to the erased elements. No destructors are called.
  1337. template<class Disposer>
  1338. size_type erase_and_dispose(const key_type &key, Disposer disposer)
  1339. {
  1340. std::pair<iterator,iterator> p = this->equal_range(key);
  1341. size_type n;
  1342. this->private_erase(p.first, p.second, n, disposer);
  1343. return n;
  1344. }
  1345. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1346. //!
  1347. //! <b>Effects</b>: Erases the range pointed to by b end e.
  1348. //! Disposer::operator()(pointer) is called for the removed elements.
  1349. //!
  1350. //! <b>Complexity</b>: Average complexity for erase range is at most
  1351. //! O(log(size() + N)), where N is the number of elements in the range.
  1352. //!
  1353. //! <b>Throws</b>: Nothing.
  1354. //!
  1355. //! <b>Note</b>: Invalidates the iterators
  1356. //! to the erased elements.
  1357. template<class Disposer>
  1358. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) BOOST_NOEXCEPT
  1359. { size_type n; return this->private_erase(b, e, n, disposer); }
  1360. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1361. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk)
  1362. //! and nk the key_type of a value_type inserted into `*this`.
  1363. //!
  1364. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1365. //!
  1366. //! <b>Effects</b>: Erases all the elements with the given key.
  1367. //! according to the comparison functor "comp".
  1368. //! Disposer::operator()(pointer) is called for the removed elements.
  1369. //!
  1370. //! <b>Returns</b>: The number of erased elements.
  1371. //!
  1372. //! <b>Complexity</b>: O(log(size() + N).
  1373. //!
  1374. //! <b>Throws</b>: Nothing.
  1375. //!
  1376. //! <b>Note</b>: Invalidates the iterators
  1377. //! to the erased elements.
  1378. template<class KeyType, class KeyTypeKeyCompare, class Disposer>
  1379. BOOST_INTRUSIVE_DOC1ST(size_type
  1380. , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
  1381. erase_and_dispose(const KeyType& key, KeyTypeKeyCompare comp, Disposer disposer)
  1382. {
  1383. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  1384. size_type n;
  1385. this->private_erase(p.first, p.second, n, disposer);
  1386. return n;
  1387. }
  1388. //! <b>Effects</b>: Erases all of the elements.
  1389. //!
  1390. //! <b>Complexity</b>: Linear to the number of elements on the container.
  1391. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  1392. //!
  1393. //! <b>Throws</b>: Nothing.
  1394. //!
  1395. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1396. //! to the erased elements. No destructors are called.
  1397. void clear() BOOST_NOEXCEPT
  1398. {
  1399. BOOST_IF_CONSTEXPR(safemode_or_autounlink){
  1400. this->clear_and_dispose(detail::null_disposer());
  1401. }
  1402. else{
  1403. node_algorithms::init_header(this->header_ptr());
  1404. this->sz_traits().set_size(0);
  1405. }
  1406. }
  1407. //! <b>Effects</b>: Erases all of the elements calling disposer(p) for
  1408. //! each node to be erased.
  1409. //! <b>Complexity</b>: Average complexity for is at most O(log(size() + N)),
  1410. //! where N is the number of elements in the container.
  1411. //!
  1412. //! <b>Throws</b>: Nothing.
  1413. //!
  1414. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1415. //! to the erased elements. Calls N times to disposer functor.
  1416. template<class Disposer>
  1417. void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
  1418. {
  1419. node_algorithms::clear_and_dispose(this->header_ptr()
  1420. , detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
  1421. node_algorithms::init_header(this->header_ptr());
  1422. this->sz_traits().set_size(0);
  1423. }
  1424. //! <b>Effects</b>: Returns the number of contained elements with the given value
  1425. //!
  1426. //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
  1427. //! to number of objects with the given value.
  1428. //!
  1429. //! <b>Throws</b>: If `key_compare` throws.
  1430. size_type count(const key_type &key) const
  1431. { return size_type(this->count(key, this->key_comp())); }
  1432. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1433. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1434. //! and nk the key_type of a value_type inserted into `*this`.
  1435. //!
  1436. //! <b>Effects</b>: Returns the number of contained elements with the given key
  1437. //!
  1438. //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
  1439. //! to number of objects with the given key.
  1440. //!
  1441. //! <b>Throws</b>: If `comp` throws.
  1442. template<class KeyType, class KeyTypeKeyCompare>
  1443. size_type count(const KeyType &key, KeyTypeKeyCompare comp) const
  1444. {
  1445. std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
  1446. size_type n = 0;
  1447. for(; ret.first != ret.second; ++ret.first){ ++n; }
  1448. return n;
  1449. }
  1450. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1451. //Add non-const overloads to theoretically const members
  1452. //as some algorithms have different behavior when non-const versions are used (like splay trees).
  1453. size_type count(const key_type &key)
  1454. { return size_type(this->count(key, this->key_comp())); }
  1455. template<class KeyType, class KeyTypeKeyCompare>
  1456. size_type count(const KeyType &key, KeyTypeKeyCompare comp)
  1457. {
  1458. std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
  1459. size_type n = 0;
  1460. for(; ret.first != ret.second; ++ret.first){ ++n; }
  1461. return n;
  1462. }
  1463. #else //defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1464. //! <b>Effects</b>: Returns an iterator to the first element whose
  1465. //! key is not less than k or end() if that element does not exist.
  1466. //!
  1467. //! <b>Complexity</b>: Logarithmic.
  1468. //!
  1469. //! <b>Throws</b>: If `key_compare` throws.
  1470. iterator lower_bound(const key_type &key);
  1471. //! <b>Effects</b>: Returns an iterator to the first element whose
  1472. //! key is not less than k or end() if that element does not exist.
  1473. //!
  1474. //! <b>Complexity</b>: Logarithmic.
  1475. //!
  1476. //! <b>Throws</b>: If `key_compare` throws.
  1477. const_iterator lower_bound(const key_type &key) const;
  1478. //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)
  1479. template<class KeyType, class KeyTypeKeyCompare>
  1480. iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp);
  1481. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)
  1482. template<class KeyType, class KeyTypeKeyCompare>
  1483. const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
  1484. //! <b>Effects</b>: Returns an iterator to the first element whose
  1485. //! key is greater than k or end() if that element does not exist.
  1486. //!
  1487. //! <b>Complexity</b>: Logarithmic.
  1488. //!
  1489. //! <b>Throws</b>: If `key_compare` throws.
  1490. iterator upper_bound(const key_type &key);
  1491. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1492. //! !comp(key, nk), with nk the key_type of a value_type inserted into `*this`.
  1493. //!
  1494. //! <b>Effects</b>: Returns an iterator to the first element whose
  1495. //! key is greater than k according to comp or end() if that element
  1496. //! does not exist.
  1497. //!
  1498. //! <b>Complexity</b>: Logarithmic.
  1499. //!
  1500. //! <b>Throws</b>: If `comp` throws.
  1501. template<class KeyType, class KeyTypeKeyCompare>
  1502. iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp);
  1503. //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)
  1504. const_iterator upper_bound(const key_type &key) const;
  1505. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)
  1506. template<class KeyType, class KeyTypeKeyCompare>
  1507. const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
  1508. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1509. //! k or end() if that element does not exist.
  1510. //!
  1511. //! <b>Complexity</b>: Logarithmic.
  1512. //!
  1513. //! <b>Throws</b>: If `key_compare` throws.
  1514. iterator find(const key_type &key);
  1515. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1516. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1517. //! and nk the key_type of a value_type inserted into `*this`.
  1518. //!
  1519. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1520. //! k or end() if that element does not exist.
  1521. //!
  1522. //! <b>Complexity</b>: Logarithmic.
  1523. //!
  1524. //! <b>Throws</b>: If `comp` throws.
  1525. template<class KeyType, class KeyTypeKeyCompare>
  1526. iterator find(const KeyType &key, KeyTypeKeyCompare comp);
  1527. //! @copydoc ::boost::intrusive::bstree::find(const key_type &)
  1528. const_iterator find(const key_type &key) const;
  1529. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)
  1530. template<class KeyType, class KeyTypeKeyCompare>
  1531. const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const;
  1532. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1533. //! an empty range that indicates the position where those elements would be
  1534. //! if they there is no elements with key k.
  1535. //!
  1536. //! <b>Complexity</b>: Logarithmic.
  1537. //!
  1538. //! <b>Throws</b>: If `key_compare` throws.
  1539. std::pair<iterator,iterator> equal_range(const key_type &key);
  1540. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1541. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1542. //! with nk the key_type of a value_type inserted into `*this`.
  1543. //!
  1544. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1545. //! an empty range that indicates the position where those elements would be
  1546. //! if they there is no elements with key k.
  1547. //!
  1548. //! <b>Complexity</b>: Logarithmic.
  1549. //!
  1550. //! <b>Throws</b>: If `comp` throws.
  1551. template<class KeyType, class KeyTypeKeyCompare>
  1552. std::pair<iterator,iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp);
  1553. //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)
  1554. std::pair<const_iterator, const_iterator> equal_range(const key_type &key) const;
  1555. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)
  1556. template<class KeyType, class KeyTypeKeyCompare>
  1557. std::pair<const_iterator, const_iterator>
  1558. equal_range(const KeyType &key, KeyTypeKeyCompare comp) const;
  1559. //! <b>Requires</b>:
  1560. //! `upper_key` shall not precede `lower_key` according to key_compare.
  1561. //! [key_comp()(upper_key, lower_key) shall be false]
  1562. //!
  1563. //! If `lower_key` is equivalent to `upper_key`
  1564. //! [!key_comp()(upper_key, lower_key) && !key_comp()(lower_key, upper_key)] then
  1565. //! ('left_closed' || 'right_closed') must be false.
  1566. //!
  1567. //! <b>Effects</b>: Returns an a pair with the following criteria:
  1568. //!
  1569. //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise
  1570. //!
  1571. //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise
  1572. //!
  1573. //! <b>Complexity</b>: Logarithmic.
  1574. //!
  1575. //! <b>Throws</b>: If `key_compare` throws.
  1576. //!
  1577. //! <b>Note</b>: This function can be more efficient than calling upper_bound
  1578. //! and lower_bound for lower_value and upper_value.
  1579. //!
  1580. //! <b>Note</b>: Experimental function, the interface might change in future releases.
  1581. std::pair<iterator,iterator> bounded_range
  1582. (const key_type &lower_key, const key_type &upper_value, bool left_closed, bool right_closed);
  1583. //! <b>Requires</b>:
  1584. //! `lower_key` is a value such that `*this` is partitioned with respect to
  1585. //! comp(nk, lower_key) if left_closed is true, with respect to !comp(lower_key, nk) otherwise.
  1586. //!
  1587. //! `upper_key` is a value such that `*this` is partitioned with respect to
  1588. //! !comp(upper_key, nk) if right_closed is true, with respect to comp(nk, upper_key) otherwise.
  1589. //!
  1590. //! `upper_key` shall not precede `lower_key` according to comp
  1591. //! [comp(upper_key, lower_key) shall be false]
  1592. //!
  1593. //! If `lower_key` is equivalent to `upper_key`
  1594. //! [!comp(upper_key, lower_key) && !comp(lower_key, upper_key)] then
  1595. //! ('left_closed' || 'right_closed') must be false.
  1596. //!
  1597. //! <b>Effects</b>: Returns an a pair with the following criteria:
  1598. //!
  1599. //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise
  1600. //!
  1601. //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise
  1602. //!
  1603. //! <b>Complexity</b>: Logarithmic.
  1604. //!
  1605. //! <b>Throws</b>: If `comp` throws.
  1606. //!
  1607. //! <b>Note</b>: This function can be more efficient than calling upper_bound
  1608. //! and lower_bound for lower_key and upper_key.
  1609. //!
  1610. //! <b>Note</b>: Experimental function, the interface might change in future releases.
  1611. template<class KeyType, class KeyTypeKeyCompare>
  1612. std::pair<iterator,iterator> bounded_range
  1613. (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed);
  1614. //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)
  1615. std::pair<const_iterator,const_iterator> bounded_range
  1616. (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const;
  1617. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)
  1618. template<class KeyType, class KeyTypeKeyCompare>
  1619. std::pair<const_iterator,const_iterator> bounded_range
  1620. (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const;
  1621. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1622. //! appropriate type. Otherwise the behavior is undefined.
  1623. //!
  1624. //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
  1625. //! that points to the value
  1626. //!
  1627. //! <b>Complexity</b>: Constant.
  1628. //!
  1629. //! <b>Throws</b>: Nothing.
  1630. //!
  1631. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1632. //! is stateless.
  1633. static iterator s_iterator_to(reference value) BOOST_NOEXCEPT;
  1634. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1635. //! appropriate type. Otherwise the behavior is undefined.
  1636. //!
  1637. //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
  1638. //! set that points to the value
  1639. //!
  1640. //! <b>Complexity</b>: Constant.
  1641. //!
  1642. //! <b>Throws</b>: Nothing.
  1643. //!
  1644. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1645. //! is stateless.
  1646. static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT;
  1647. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1648. //! appropriate type. Otherwise the behavior is undefined.
  1649. //!
  1650. //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
  1651. //! that points to the value
  1652. //!
  1653. //! <b>Complexity</b>: Constant.
  1654. //!
  1655. //! <b>Throws</b>: Nothing.
  1656. iterator iterator_to(reference value) BOOST_NOEXCEPT;
  1657. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1658. //! appropriate type. Otherwise the behavior is undefined.
  1659. //!
  1660. //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
  1661. //! set that points to the value
  1662. //!
  1663. //! <b>Complexity</b>: Constant.
  1664. //!
  1665. //! <b>Throws</b>: Nothing.
  1666. const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT;
  1667. //! <b>Requires</b>: value shall not be in a container.
  1668. //!
  1669. //! <b>Effects</b>: init_node puts the hook of a value in a well-known default
  1670. //! state.
  1671. //!
  1672. //! <b>Throws</b>: Nothing.
  1673. //!
  1674. //! <b>Complexity</b>: Constant time.
  1675. //!
  1676. //! <b>Note</b>: This function puts the hook in the well-known default state
  1677. //! used by auto_unlink and safe hooks.
  1678. static void init_node(reference value) BOOST_NOEXCEPT;
  1679. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1680. //! <b>Effects</b>: Unlinks the leftmost node from the container.
  1681. //!
  1682. //! <b>Complexity</b>: Average complexity is constant time.
  1683. //!
  1684. //! <b>Throws</b>: Nothing.
  1685. //!
  1686. //! <b>Notes</b>: This function breaks the container and the container can
  1687. //! only be used for more unlink_leftmost_without_rebalance calls.
  1688. //! This function is normally used to achieve a step by step
  1689. //! controlled destruction of the container.
  1690. pointer unlink_leftmost_without_rebalance() BOOST_NOEXCEPT
  1691. {
  1692. node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance
  1693. (this->header_ptr()));
  1694. if(!to_be_disposed)
  1695. return 0;
  1696. this->sz_traits().decrement();
  1697. BOOST_IF_CONSTEXPR(safemode_or_autounlink)//If this is commented does not work with normal_link
  1698. node_algorithms::init(to_be_disposed);
  1699. return this->get_value_traits().to_value_ptr(to_be_disposed);
  1700. }
  1701. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1702. //! <b>Requires</b>: replace_this must be a valid iterator of *this
  1703. //! and with_this must not be inserted in any container.
  1704. //!
  1705. //! <b>Effects</b>: Replaces replace_this in its position in the
  1706. //! container with with_this. The container does not need to be rebalanced.
  1707. //!
  1708. //! <b>Complexity</b>: Constant.
  1709. //!
  1710. //! <b>Throws</b>: Nothing.
  1711. //!
  1712. //! <b>Note</b>: This function will break container ordering invariants if
  1713. //! with_this is not equivalent to *replace_this according to the
  1714. //! ordering rules. This function is faster than erasing and inserting
  1715. //! the node, since no rebalancing or comparison is needed.
  1716. void replace_node(iterator replace_this, reference with_this) BOOST_NOEXCEPT;
  1717. //! <b>Effects</b>: Rebalances the tree.
  1718. //!
  1719. //! <b>Throws</b>: Nothing.
  1720. //!
  1721. //! <b>Complexity</b>: Linear.
  1722. void rebalance() BOOST_NOEXCEPT;
  1723. //! <b>Requires</b>: old_root is a node of a tree.
  1724. //!
  1725. //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
  1726. //!
  1727. //! <b>Returns</b>: The new root of the subtree.
  1728. //!
  1729. //! <b>Throws</b>: Nothing.
  1730. //!
  1731. //! <b>Complexity</b>: Linear to the elements in the subtree.
  1732. iterator rebalance_subtree(iterator r) BOOST_NOEXCEPT;
  1733. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1734. //! <b>Effects</b>: removes "value" from the container.
  1735. //!
  1736. //! <b>Throws</b>: Nothing.
  1737. //!
  1738. //! <b>Complexity</b>: Logarithmic time.
  1739. //!
  1740. //! <b>Note</b>: This static function is only usable with non-constant
  1741. //! time size containers that have stateless comparison functors.
  1742. //!
  1743. //! If the user calls
  1744. //! this function with a constant time size container or stateful comparison
  1745. //! functor a compilation error will be issued.
  1746. static void remove_node(reference value) BOOST_NOEXCEPT
  1747. {
  1748. BOOST_INTRUSIVE_STATIC_ASSERT((!constant_time_size));
  1749. node_ptr to_remove(value_traits::to_node_ptr(value));
  1750. node_algorithms::unlink(to_remove);
  1751. BOOST_IF_CONSTEXPR(safemode_or_autounlink)
  1752. node_algorithms::init(to_remove);
  1753. }
  1754. //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
  1755. //! function from *this.
  1756. //!
  1757. //! <b>Effects</b>: Attempts to extract each element in source and insert it into a using
  1758. //! the comparison object of *this. If there is an element in a with key equivalent to the
  1759. //! key of an element from source, then that element is not extracted from source.
  1760. //!
  1761. //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
  1762. //! to those same elements but as members of *this. Iterators referring to the transferred
  1763. //! elements will continue to refer to their elements, but they now behave as iterators into *this,
  1764. //! not into source.
  1765. //!
  1766. //! <b>Throws</b>: Nothing unless the comparison object throws.
  1767. //!
  1768. //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
  1769. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1770. template<class T, class ...Options2> void merge_unique(bstree<T, Options2...> &);
  1771. #else
  1772. template<class Compare2>
  1773. void merge_unique(bstree_impl
  1774. <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
  1775. #endif
  1776. {
  1777. node_ptr it (node_algorithms::begin_node(source.header_ptr()))
  1778. , itend(node_algorithms::end_node (source.header_ptr()));
  1779. while(it != itend){
  1780. node_ptr const p(it);
  1781. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
  1782. it = node_algorithms::next_node(it);
  1783. if( node_algorithms::transfer_unique(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p) ){
  1784. source.sz_traits().decrement();
  1785. this->sz_traits().increment();
  1786. }
  1787. }
  1788. }
  1789. //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
  1790. //! function from *this.
  1791. //!
  1792. //! <b>Effects</b>: Extracts each element in source and insert it into a using
  1793. //! the comparison object of *this.
  1794. //!
  1795. //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
  1796. //! to those same elements but as members of *this. Iterators referring to the transferred
  1797. //! elements will continue to refer to their elements, but they now behave as iterators into *this,
  1798. //! not into source.
  1799. //!
  1800. //! <b>Throws</b>: Nothing unless the comparison object throws.
  1801. //!
  1802. //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
  1803. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1804. template<class T, class ...Options2> void merge_equal(bstree<T, Options2...> &);
  1805. #else
  1806. template<class Compare2>
  1807. void merge_equal(bstree_impl
  1808. <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
  1809. #endif
  1810. {
  1811. node_ptr it (node_algorithms::begin_node(source.header_ptr()))
  1812. , itend(node_algorithms::end_node (source.header_ptr()));
  1813. while(it != itend){
  1814. node_ptr const p(it);
  1815. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
  1816. it = node_algorithms::next_node(it);
  1817. node_algorithms::transfer_equal(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p);
  1818. source.sz_traits().decrement();
  1819. this->sz_traits().increment();
  1820. }
  1821. }
  1822. //! <b>Effects</b>: Asserts the integrity of the container with additional checks provided by the user.
  1823. //!
  1824. //! <b>Complexity</b>: Linear time.
  1825. //!
  1826. //! <b>Note</b>: The method might not have effect when asserts are turned off (e.g., with NDEBUG).
  1827. //! Experimental function, interface might change in future versions.
  1828. template <class ExtraChecker>
  1829. void check(ExtraChecker extra_checker) const
  1830. {
  1831. typedef detail::key_nodeptr_comp<key_compare, value_traits, key_of_value> nodeptr_comp_t;
  1832. nodeptr_comp_t nodeptr_comp(this->key_comp(), &this->get_value_traits());
  1833. typedef typename get_node_checker<AlgoType, ValueTraits, nodeptr_comp_t, ExtraChecker>::type node_checker_t;
  1834. typename node_checker_t::return_type checker_return;
  1835. node_algorithms::check(this->header_ptr(), node_checker_t(nodeptr_comp, extra_checker), checker_return);
  1836. BOOST_INTRUSIVE_INVARIANT_ASSERT(!constant_time_size || this->sz_traits().get_size() == checker_return.node_count);
  1837. }
  1838. //! <b>Effects</b>: Asserts the integrity of the container.
  1839. //!
  1840. //! <b>Complexity</b>: Linear time.
  1841. //!
  1842. //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
  1843. //! Experimental function, interface might change in future versions.
  1844. void check() const
  1845. {
  1846. check(detail::empty_node_checker<ValueTraits>());
  1847. }
  1848. friend bool operator==(const bstree_impl &x, const bstree_impl &y)
  1849. {
  1850. if(constant_time_size && x.size() != y.size()){
  1851. return false;
  1852. }
  1853. return boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
  1854. }
  1855. friend bool operator!=(const bstree_impl &x, const bstree_impl &y)
  1856. { return !(x == y); }
  1857. friend bool operator<(const bstree_impl &x, const bstree_impl &y)
  1858. { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1859. friend bool operator>(const bstree_impl &x, const bstree_impl &y)
  1860. { return y < x; }
  1861. friend bool operator<=(const bstree_impl &x, const bstree_impl &y)
  1862. { return !(x > y); }
  1863. friend bool operator>=(const bstree_impl &x, const bstree_impl &y)
  1864. { return !(x < y); }
  1865. friend void swap(bstree_impl &x, bstree_impl &y)
  1866. { x.swap(y); }
  1867. /// @cond
  1868. private:
  1869. template<class Disposer>
  1870. iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
  1871. {
  1872. for(n = 0; b != e; ++n)
  1873. this->erase_and_dispose(b++, disposer);
  1874. return b.unconst();
  1875. }
  1876. iterator private_erase(const_iterator b, const_iterator e, size_type &n)
  1877. {
  1878. for(n = 0; b != e; ++n)
  1879. this->erase(b++);
  1880. return b.unconst();
  1881. }
  1882. /// @endcond
  1883. };
  1884. //! Helper metafunction to define a \c bstree that yields to the same type when the
  1885. //! same options (either explicitly or implicitly) are used.
  1886. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1887. template<class T, class ...Options>
  1888. #else
  1889. template<class T, class O1 = void, class O2 = void
  1890. , class O3 = void, class O4 = void
  1891. , class O5 = void, class O6 = void>
  1892. #endif
  1893. struct make_bstree
  1894. {
  1895. /// @cond
  1896. typedef typename pack_options
  1897. < bstree_defaults,
  1898. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1899. O1, O2, O3, O4, O5, O6
  1900. #else
  1901. Options...
  1902. #endif
  1903. >::type packed_options;
  1904. typedef typename detail::get_value_traits
  1905. <T, typename packed_options::proto_value_traits>::type value_traits;
  1906. typedef bstree_impl
  1907. < value_traits
  1908. , typename packed_options::key_of_value
  1909. , typename packed_options::compare
  1910. , typename packed_options::size_type
  1911. , packed_options::constant_time_size
  1912. , BsTreeAlgorithms
  1913. , typename packed_options::header_holder_type
  1914. > implementation_defined;
  1915. /// @endcond
  1916. typedef implementation_defined type;
  1917. };
  1918. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1919. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1920. template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
  1921. #else
  1922. template<class T, class ...Options>
  1923. #endif
  1924. class bstree
  1925. : public make_bstree<T,
  1926. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1927. O1, O2, O3, O4, O5, O6
  1928. #else
  1929. Options...
  1930. #endif
  1931. >::type
  1932. {
  1933. typedef typename make_bstree
  1934. <T,
  1935. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1936. O1, O2, O3, O4, O5, O6
  1937. #else
  1938. Options...
  1939. #endif
  1940. >::type Base;
  1941. BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree)
  1942. public:
  1943. typedef typename Base::key_compare key_compare;
  1944. typedef typename Base::value_traits value_traits;
  1945. typedef typename Base::iterator iterator;
  1946. typedef typename Base::const_iterator const_iterator;
  1947. //Assert if passed value traits are compatible with the type
  1948. BOOST_INTRUSIVE_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
  1949. inline bstree()
  1950. : Base()
  1951. {}
  1952. inline explicit bstree( const key_compare &cmp, const value_traits &v_traits = value_traits())
  1953. : Base(cmp, v_traits)
  1954. {}
  1955. template<class Iterator>
  1956. inline bstree( bool unique, Iterator b, Iterator e
  1957. , const key_compare &cmp = key_compare()
  1958. , const value_traits &v_traits = value_traits())
  1959. : Base(unique, b, e, cmp, v_traits)
  1960. {}
  1961. inline bstree(BOOST_RV_REF(bstree) x)
  1962. : Base(BOOST_MOVE_BASE(Base, x))
  1963. {}
  1964. inline bstree& operator=(BOOST_RV_REF(bstree) x)
  1965. { return static_cast<bstree &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  1966. template <class Cloner, class Disposer>
  1967. inline void clone_from(const bstree &src, Cloner cloner, Disposer disposer)
  1968. { Base::clone_from(src, cloner, disposer); }
  1969. template <class Cloner, class Disposer>
  1970. inline void clone_from(BOOST_RV_REF(bstree) src, Cloner cloner, Disposer disposer)
  1971. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  1972. inline static bstree &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
  1973. { return static_cast<bstree &>(Base::container_from_end_iterator(end_iterator)); }
  1974. inline static const bstree &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
  1975. { return static_cast<const bstree &>(Base::container_from_end_iterator(end_iterator)); }
  1976. inline static bstree &container_from_iterator(iterator it) BOOST_NOEXCEPT
  1977. { return static_cast<bstree &>(Base::container_from_iterator(it)); }
  1978. inline static const bstree &container_from_iterator(const_iterator it) BOOST_NOEXCEPT
  1979. { return static_cast<const bstree &>(Base::container_from_iterator(it)); }
  1980. };
  1981. #endif
  1982. } //namespace intrusive
  1983. } //namespace boost
  1984. #include <boost/intrusive/detail/config_end.hpp>
  1985. #endif //BOOST_INTRUSIVE_BSTREE_HPP