bstree.hpp 93 KB

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