bstree.hpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  917. iterator ret(node_algorithms::insert_equal_upper_bound
  918. (this->header_ptr(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
  919. this->sz_traits().increment();
  920. return ret;
  921. }
  922. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  923. //! a valid iterator.
  924. //!
  925. //! <b>Effects</b>: Inserts x into the container, using "hint" as a hint to
  926. //! where it will be inserted. If "hint" is the upper_bound
  927. //! the insertion takes constant time (two comparisons in the worst case)
  928. //!
  929. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  930. //! constant time if t is inserted immediately before hint.
  931. //!
  932. //! <b>Throws</b>: If the internal key_compare ordering function throws. Strong guarantee.
  933. //!
  934. //! <b>Note</b>: Does not affect the validity of iterators and references.
  935. //! No copy-constructors are called.
  936. iterator insert_equal(const_iterator hint, reference value)
  937. {
  938. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  939. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  940. iterator ret(node_algorithms::insert_equal
  941. (this->header_ptr(), hint.pointed_node(), to_insert, this->key_node_comp(this->key_comp())), this->priv_value_traits_ptr());
  942. this->sz_traits().increment();
  943. return ret;
  944. }
  945. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  946. //! of type value_type.
  947. //!
  948. //! <b>Effects</b>: Inserts a each element of a range into the container
  949. //! before the upper bound of the key of each element.
  950. //!
  951. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  952. //! size of the range. However, it is linear in N if the range is already sorted
  953. //! by value_comp().
  954. //!
  955. //! <b>Throws</b>: Nothing.
  956. //!
  957. //! <b>Note</b>: Does not affect the validity of iterators and references.
  958. //! No copy-constructors are called.
  959. template<class Iterator>
  960. void insert_equal(Iterator b, Iterator e)
  961. {
  962. iterator iend(this->end());
  963. for (; b != e; ++b)
  964. this->insert_equal(iend, *b);
  965. }
  966. //! <b>Requires</b>: value must be an lvalue
  967. //!
  968. //! <b>Effects</b>: Inserts value into the container if the value
  969. //! is not already present.
  970. //!
  971. //! <b>Complexity</b>: Average complexity for insert element is at
  972. //! most logarithmic.
  973. //!
  974. //! <b>Throws</b>: Nothing.
  975. //!
  976. //! <b>Note</b>: Does not affect the validity of iterators and references.
  977. //! No copy-constructors are called.
  978. std::pair<iterator, bool> insert_unique(reference value)
  979. {
  980. insert_commit_data commit_data;
  981. std::pair<node_ptr, bool> ret =
  982. (node_algorithms::insert_unique_check
  983. (this->header_ptr(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
  984. return std::pair<iterator, bool>
  985. ( ret.second ? this->insert_unique_commit(value, commit_data)
  986. : iterator(ret.first, this->priv_value_traits_ptr())
  987. , ret.second);
  988. }
  989. //! <b>Requires</b>: value must be an lvalue, and "hint" must be
  990. //! a valid iterator
  991. //!
  992. //! <b>Effects</b>: Tries to insert x into the container, using "hint" as a hint
  993. //! to where it will be inserted.
  994. //!
  995. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  996. //! constant time (two comparisons in the worst case)
  997. //! if t is inserted immediately before hint.
  998. //!
  999. //! <b>Throws</b>: Nothing.
  1000. //!
  1001. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1002. //! No copy-constructors are called.
  1003. iterator insert_unique(const_iterator hint, reference value)
  1004. {
  1005. insert_commit_data commit_data;
  1006. std::pair<node_ptr, bool> ret =
  1007. (node_algorithms::insert_unique_check
  1008. (this->header_ptr(), hint.pointed_node(), key_of_value()(value), this->key_node_comp(this->key_comp()), commit_data));
  1009. return ret.second ? this->insert_unique_commit(value, commit_data)
  1010. : iterator(ret.first, this->priv_value_traits_ptr());
  1011. }
  1012. //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
  1013. //! of type value_type.
  1014. //!
  1015. //! <b>Effects</b>: Tries to insert each element of a range into the container.
  1016. //!
  1017. //! <b>Complexity</b>: Insert range is in general O(N * log(N)), where N is the
  1018. //! size of the range. However, it is linear in N if the range is already sorted
  1019. //! by value_comp().
  1020. //!
  1021. //! <b>Throws</b>: Nothing.
  1022. //!
  1023. //! <b>Note</b>: Does not affect the validity of iterators and references.
  1024. //! No copy-constructors are called.
  1025. template<class Iterator>
  1026. void insert_unique(Iterator b, Iterator e)
  1027. {
  1028. if(this->empty()){
  1029. iterator iend(this->end());
  1030. for (; b != e; ++b)
  1031. this->insert_unique(iend, *b);
  1032. }
  1033. else{
  1034. for (; b != e; ++b)
  1035. this->insert_unique(*b);
  1036. }
  1037. }
  1038. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1039. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1040. //! a user provided key instead of the value itself.
  1041. //!
  1042. //! <b>Returns</b>: If there is an equivalent value
  1043. //! returns a pair containing an iterator to the already present value
  1044. //! and false. If the value can be inserted returns true in the returned
  1045. //! pair boolean and fills "commit_data" that is meant to be used with
  1046. //! the "insert_commit" function.
  1047. //!
  1048. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  1049. //!
  1050. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1051. std::pair<iterator, bool> insert_unique_check(const key_type &key, insert_commit_data &commit_data);
  1052. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1053. //! a user provided key instead of the value itself, using "hint"
  1054. //! as a hint to where it will be inserted.
  1055. //!
  1056. //! <b>Returns</b>: If there is an equivalent value
  1057. //! returns a pair containing an iterator to the already present value
  1058. //! and false. If the value can be inserted returns true in the returned
  1059. //! pair boolean and fills "commit_data" that is meant to be used with
  1060. //! the "insert_commit" function.
  1061. //!
  1062. //! <b>Complexity</b>: Logarithmic in general, but it's amortized
  1063. //! constant time if t is inserted immediately before hint.
  1064. //!
  1065. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1066. std::pair<iterator, bool> insert_unique_check(const_iterator hint, const key_type &key, insert_commit_data &commit_data);
  1067. //! <b>Requires</b>: comp must be a comparison function that induces
  1068. //! the same strict weak ordering as key_compare. The difference is that
  1069. //! comp compares an arbitrary key with the contained values.
  1070. //!
  1071. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1072. //! a user provided key instead of the value itself.
  1073. //!
  1074. //! <b>Returns</b>: If there is an equivalent value
  1075. //! returns a pair containing an iterator to the already present value
  1076. //! and false. If the value can be inserted returns true in the returned
  1077. //! pair boolean and fills "commit_data" that is meant to be used with
  1078. //! the "insert_commit" function.
  1079. //!
  1080. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  1081. //!
  1082. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1083. //!
  1084. //! <b>Notes</b>: This function is used to improve performance when constructing
  1085. //! a value_type is expensive: if there is an equivalent value
  1086. //! the constructed object must be discarded. Many times, the part of the
  1087. //! node that is used to impose the order is much cheaper to construct
  1088. //! than the value_type and this function offers the possibility to use that
  1089. //! part to check if the insertion will be successful.
  1090. //!
  1091. //! If the check is successful, the user can construct the value_type and use
  1092. //! "insert_commit" to insert the object in constant-time. This gives a total
  1093. //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
  1094. //!
  1095. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  1096. //! objects are inserted or erased from the container.
  1097. template<class KeyType, class KeyTypeKeyCompare>
  1098. std::pair<iterator, bool> insert_unique_check
  1099. (const KeyType &key, KeyTypeKeyCompare comp, insert_commit_data &commit_data);
  1100. //! <b>Requires</b>: comp must be a comparison function that induces
  1101. //! the same strict weak ordering as key_compare. The difference is that
  1102. //! comp compares an arbitrary key with the contained values.
  1103. //!
  1104. //! <b>Effects</b>: Checks if a value can be inserted in the container, using
  1105. //! a user provided key instead of the value itself, using "hint"
  1106. //! as a hint to where it will be inserted.
  1107. //!
  1108. //! <b>Returns</b>: If there is an equivalent value
  1109. //! returns a pair containing an iterator to the already present value
  1110. //! and false. If the value can be inserted returns true in the returned
  1111. //! pair boolean and fills "commit_data" that is meant to be used with
  1112. //! the "insert_commit" function.
  1113. //!
  1114. //! <b>Complexity</b>: Logarithmic in general, but it's amortized
  1115. //! constant time if t is inserted immediately before hint.
  1116. //!
  1117. //! <b>Throws</b>: If the comp ordering function throws. Strong guarantee.
  1118. //!
  1119. //! <b>Notes</b>: This function is used to improve performance when constructing
  1120. //! a value_type is expensive: if there is an equivalent value
  1121. //! the constructed object must be discarded. Many times, the part of the
  1122. //! constructing that is used to impose the order is much cheaper to construct
  1123. //! than the value_type and this function offers the possibility to use that key
  1124. //! to check if the insertion will be successful.
  1125. //!
  1126. //! If the check is successful, the user can construct the value_type and use
  1127. //! "insert_commit" to insert the object in constant-time. This can give a total
  1128. //! constant-time complexity to the insertion: check(O(1)) + commit(O(1)).
  1129. //!
  1130. //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
  1131. //! objects are inserted or erased from the container.
  1132. template<class KeyType, class KeyTypeKeyCompare>
  1133. std::pair<iterator, bool> insert_unique_check
  1134. (const_iterator hint, const KeyType &key
  1135. ,KeyTypeKeyCompare comp, insert_commit_data &commit_data);
  1136. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1137. //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
  1138. //! must have been obtained from a previous call to "insert_check".
  1139. //! No objects should have been inserted or erased from the container between
  1140. //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
  1141. //!
  1142. //! <b>Effects</b>: Inserts the value in the container using the information obtained
  1143. //! from the "commit_data" that a previous "insert_check" filled.
  1144. //!
  1145. //! <b>Returns</b>: An iterator to the newly inserted object.
  1146. //!
  1147. //! <b>Complexity</b>: Constant time.
  1148. //!
  1149. //! <b>Throws</b>: Nothing.
  1150. //!
  1151. //! <b>Notes</b>: This function has only sense if a "insert_check" has been
  1152. //! previously executed to fill "commit_data". No value should be inserted or
  1153. //! erased between the "insert_check" and "insert_commit" calls.
  1154. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
  1155. {
  1156. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1157. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1158. #if !(defined(BOOST_DISABLE_ASSERTS) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && defined(NDEBUG) ))
  1159. //Test insertion position is correct
  1160. iterator p(commit_data.node, this->priv_value_traits_ptr());
  1161. if(!commit_data.link_left){
  1162. ++p;
  1163. }
  1164. //Check if the insertion point is correct to detect wrong
  1165. //uses insert_unique_check
  1166. BOOST_ASSERT(( p == this->end() || !this->comp()(*p, value) ));
  1167. BOOST_ASSERT(( p == this->begin() || !this->comp()(value, *--p) ));
  1168. #endif
  1169. node_algorithms::insert_unique_commit
  1170. (this->header_ptr(), to_insert, commit_data);
  1171. this->sz_traits().increment();
  1172. return iterator(to_insert, this->priv_value_traits_ptr());
  1173. }
  1174. //! <b>Requires</b>: value must be an lvalue, "pos" must be
  1175. //! a valid iterator (or end) and must be the succesor of value
  1176. //! once inserted according to the predicate
  1177. //!
  1178. //! <b>Effects</b>: Inserts x into the container before "pos".
  1179. //!
  1180. //! <b>Complexity</b>: Constant time.
  1181. //!
  1182. //! <b>Throws</b>: Nothing.
  1183. //!
  1184. //! <b>Note</b>: This function does not check preconditions so if "pos" is not
  1185. //! the successor of "value" container ordering invariant will be broken.
  1186. //! This is a low-level function to be used only for performance reasons
  1187. //! by advanced users.
  1188. iterator insert_before(const_iterator pos, reference value)
  1189. {
  1190. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1191. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1192. this->sz_traits().increment();
  1193. return iterator(node_algorithms::insert_before
  1194. (this->header_ptr(), pos.pointed_node(), to_insert), this->priv_value_traits_ptr());
  1195. }
  1196. //! <b>Requires</b>: value must be an lvalue, and it must be no less
  1197. //! than the greatest inserted key
  1198. //!
  1199. //! <b>Effects</b>: Inserts x into the container in the last position.
  1200. //!
  1201. //! <b>Complexity</b>: Constant time.
  1202. //!
  1203. //! <b>Throws</b>: Nothing.
  1204. //!
  1205. //! <b>Note</b>: This function does not check preconditions so if value is
  1206. //! less than the greatest inserted key container ordering invariant will be broken.
  1207. //! This function is slightly more efficient than using "insert_before".
  1208. //! This is a low-level function to be used only for performance reasons
  1209. //! by advanced users.
  1210. void push_back(reference value)
  1211. {
  1212. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1213. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1214. this->sz_traits().increment();
  1215. node_algorithms::push_back(this->header_ptr(), to_insert);
  1216. }
  1217. //! <b>Requires</b>: value must be an lvalue, and it must be no greater
  1218. //! than the minimum inserted key
  1219. //!
  1220. //! <b>Effects</b>: Inserts x into the container in the first position.
  1221. //!
  1222. //! <b>Complexity</b>: Constant time.
  1223. //!
  1224. //! <b>Throws</b>: Nothing.
  1225. //!
  1226. //! <b>Note</b>: This function does not check preconditions so if value is
  1227. //! greater than the minimum inserted key container ordering invariant will be broken.
  1228. //! This function is slightly more efficient than using "insert_before".
  1229. //! This is a low-level function to be used only for performance reasons
  1230. //! by advanced users.
  1231. void push_front(reference value)
  1232. {
  1233. node_ptr to_insert(this->get_value_traits().to_node_ptr(value));
  1234. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(to_insert));
  1235. this->sz_traits().increment();
  1236. node_algorithms::push_front(this->header_ptr(), to_insert);
  1237. }
  1238. //! <b>Effects</b>: Erases the element pointed to by i.
  1239. //!
  1240. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  1241. //!
  1242. //! <b>Throws</b>: Nothing.
  1243. //!
  1244. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1245. //! to the erased elements. No destructors are called.
  1246. iterator erase(const_iterator i)
  1247. {
  1248. const_iterator ret(i);
  1249. ++ret;
  1250. node_ptr to_erase(i.pointed_node());
  1251. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(to_erase));
  1252. node_algorithms::erase(this->header_ptr(), to_erase);
  1253. this->sz_traits().decrement();
  1254. if(safemode_or_autounlink)
  1255. node_algorithms::init(to_erase);
  1256. return ret.unconst();
  1257. }
  1258. //! <b>Effects</b>: Erases the range pointed to by b end e.
  1259. //!
  1260. //! <b>Complexity</b>: Average complexity for erase range is at most
  1261. //! O(log(size() + N)), where N is the number of elements in the range.
  1262. //!
  1263. //! <b>Throws</b>: Nothing.
  1264. //!
  1265. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1266. //! to the erased elements. No destructors are called.
  1267. iterator erase(const_iterator b, const_iterator e)
  1268. { size_type n; return this->private_erase(b, e, n); }
  1269. //! <b>Effects</b>: Erases all the elements with the given value.
  1270. //!
  1271. //! <b>Returns</b>: The number of erased elements.
  1272. //!
  1273. //! <b>Complexity</b>: O(log(size() + N).
  1274. //!
  1275. //! <b>Throws</b>: Nothing.
  1276. //!
  1277. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1278. //! to the erased elements. No destructors are called.
  1279. size_type erase(const key_type &key)
  1280. { return this->erase(key, this->key_comp()); }
  1281. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1282. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1283. //! with nk the key_type of a value_type inserted into `*this`.
  1284. //!
  1285. //! <b>Effects</b>: Erases all the elements with the given key.
  1286. //! according to the comparison functor "comp".
  1287. //!
  1288. //! <b>Returns</b>: The number of erased elements.
  1289. //!
  1290. //! <b>Complexity</b>: O(log(size() + N).
  1291. //!
  1292. //! <b>Throws</b>: Nothing.
  1293. //!
  1294. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1295. //! to the erased elements. No destructors are called.
  1296. template<class KeyType, class KeyTypeKeyCompare>
  1297. BOOST_INTRUSIVE_DOC1ST(size_type
  1298. , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
  1299. erase(const KeyType& key, KeyTypeKeyCompare comp)
  1300. {
  1301. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  1302. size_type n;
  1303. this->private_erase(p.first, p.second, n);
  1304. return n;
  1305. }
  1306. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1307. //!
  1308. //! <b>Effects</b>: Erases the element pointed to by i.
  1309. //! Disposer::operator()(pointer) is called for the removed element.
  1310. //!
  1311. //! <b>Complexity</b>: Average complexity for erase element is constant time.
  1312. //!
  1313. //! <b>Throws</b>: Nothing.
  1314. //!
  1315. //! <b>Note</b>: Invalidates the iterators
  1316. //! to the erased elements.
  1317. template<class Disposer>
  1318. iterator erase_and_dispose(const_iterator i, Disposer disposer)
  1319. {
  1320. node_ptr to_erase(i.pointed_node());
  1321. iterator ret(this->erase(i));
  1322. disposer(this->get_value_traits().to_value_ptr(to_erase));
  1323. return ret;
  1324. }
  1325. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1326. //!
  1327. //! <b>Effects</b>: Erases all the elements with the given value.
  1328. //! Disposer::operator()(pointer) is called for the removed elements.
  1329. //!
  1330. //! <b>Returns</b>: The number of erased elements.
  1331. //!
  1332. //! <b>Complexity</b>: O(log(size() + N).
  1333. //!
  1334. //! <b>Throws</b>: Nothing.
  1335. //!
  1336. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1337. //! to the erased elements. No destructors are called.
  1338. template<class Disposer>
  1339. size_type erase_and_dispose(const key_type &key, Disposer disposer)
  1340. {
  1341. std::pair<iterator,iterator> p = this->equal_range(key);
  1342. size_type n;
  1343. this->private_erase(p.first, p.second, n, disposer);
  1344. return n;
  1345. }
  1346. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1347. //!
  1348. //! <b>Effects</b>: Erases the range pointed to by b end e.
  1349. //! Disposer::operator()(pointer) is called for the removed elements.
  1350. //!
  1351. //! <b>Complexity</b>: Average complexity for erase range is at most
  1352. //! O(log(size() + N)), where N is the number of elements in the range.
  1353. //!
  1354. //! <b>Throws</b>: Nothing.
  1355. //!
  1356. //! <b>Note</b>: Invalidates the iterators
  1357. //! to the erased elements.
  1358. template<class Disposer>
  1359. iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
  1360. { size_type n; return this->private_erase(b, e, n, disposer); }
  1361. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1362. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk)
  1363. //! and nk the key_type of a value_type inserted into `*this`.
  1364. //!
  1365. //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
  1366. //!
  1367. //! <b>Effects</b>: Erases all the elements with the given key.
  1368. //! according to the comparison functor "comp".
  1369. //! Disposer::operator()(pointer) is called for the removed elements.
  1370. //!
  1371. //! <b>Returns</b>: The number of erased elements.
  1372. //!
  1373. //! <b>Complexity</b>: O(log(size() + N).
  1374. //!
  1375. //! <b>Throws</b>: Nothing.
  1376. //!
  1377. //! <b>Note</b>: Invalidates the iterators
  1378. //! to the erased elements.
  1379. template<class KeyType, class KeyTypeKeyCompare, class Disposer>
  1380. BOOST_INTRUSIVE_DOC1ST(size_type
  1381. , typename detail::disable_if_convertible<KeyTypeKeyCompare BOOST_INTRUSIVE_I const_iterator BOOST_INTRUSIVE_I size_type>::type)
  1382. erase_and_dispose(const KeyType& key, KeyTypeKeyCompare comp, Disposer disposer)
  1383. {
  1384. std::pair<iterator,iterator> p = this->equal_range(key, comp);
  1385. size_type n;
  1386. this->private_erase(p.first, p.second, n, disposer);
  1387. return n;
  1388. }
  1389. //! <b>Effects</b>: Erases all of the elements.
  1390. //!
  1391. //! <b>Complexity</b>: Linear to the number of elements on the container.
  1392. //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
  1393. //!
  1394. //! <b>Throws</b>: Nothing.
  1395. //!
  1396. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1397. //! to the erased elements. No destructors are called.
  1398. void clear()
  1399. {
  1400. if(safemode_or_autounlink){
  1401. this->clear_and_dispose(detail::null_disposer());
  1402. }
  1403. else{
  1404. node_algorithms::init_header(this->header_ptr());
  1405. this->sz_traits().set_size(0);
  1406. }
  1407. }
  1408. //! <b>Effects</b>: Erases all of the elements calling disposer(p) for
  1409. //! each node to be erased.
  1410. //! <b>Complexity</b>: Average complexity for is at most O(log(size() + N)),
  1411. //! where N is the number of elements in the container.
  1412. //!
  1413. //! <b>Throws</b>: Nothing.
  1414. //!
  1415. //! <b>Note</b>: Invalidates the iterators (but not the references)
  1416. //! to the erased elements. Calls N times to disposer functor.
  1417. template<class Disposer>
  1418. void clear_and_dispose(Disposer disposer)
  1419. {
  1420. node_algorithms::clear_and_dispose(this->header_ptr()
  1421. , detail::node_disposer<Disposer, value_traits, AlgoType>(disposer, &this->get_value_traits()));
  1422. node_algorithms::init_header(this->header_ptr());
  1423. this->sz_traits().set_size(0);
  1424. }
  1425. //! <b>Effects</b>: Returns the number of contained elements with the given value
  1426. //!
  1427. //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
  1428. //! to number of objects with the given value.
  1429. //!
  1430. //! <b>Throws</b>: If `key_compare` throws.
  1431. size_type count(const key_type &key) const
  1432. { return size_type(this->count(key, this->key_comp())); }
  1433. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1434. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1435. //! and nk the key_type of a value_type inserted into `*this`.
  1436. //!
  1437. //! <b>Effects</b>: Returns the number of contained elements with the given key
  1438. //!
  1439. //! <b>Complexity</b>: Logarithmic to the number of elements contained plus lineal
  1440. //! to number of objects with the given key.
  1441. //!
  1442. //! <b>Throws</b>: If `comp` throws.
  1443. template<class KeyType, class KeyTypeKeyCompare>
  1444. size_type count(const KeyType &key, KeyTypeKeyCompare comp) const
  1445. {
  1446. std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
  1447. size_type n = 0;
  1448. for(; ret.first != ret.second; ++ret.first){ ++n; }
  1449. return n;
  1450. }
  1451. #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1452. //Add non-const overloads to theoretically const members
  1453. //as some algorithms have different behavior when non-const versions are used (like splay trees).
  1454. size_type count(const key_type &key)
  1455. { return size_type(this->count(key, this->key_comp())); }
  1456. template<class KeyType, class KeyTypeKeyCompare>
  1457. size_type count(const KeyType &key, KeyTypeKeyCompare comp)
  1458. {
  1459. std::pair<const_iterator, const_iterator> ret = this->equal_range(key, comp);
  1460. size_type n = 0;
  1461. for(; ret.first != ret.second; ++ret.first){ ++n; }
  1462. return n;
  1463. }
  1464. #else //defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1465. //! <b>Effects</b>: Returns an iterator to the first element whose
  1466. //! key is not less than k or end() if that element does not exist.
  1467. //!
  1468. //! <b>Complexity</b>: Logarithmic.
  1469. //!
  1470. //! <b>Throws</b>: If `key_compare` throws.
  1471. iterator lower_bound(const key_type &key);
  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. const_iterator lower_bound(const key_type &key) const;
  1479. //! @copydoc ::boost::intrusive::bstree::lower_bound(const key_type &)
  1480. template<class KeyType, class KeyTypeKeyCompare>
  1481. iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp);
  1482. //! @copydoc ::boost::intrusive::bstree::lower_bound(const KeyType&,KeyTypeKeyCompare)
  1483. template<class KeyType, class KeyTypeKeyCompare>
  1484. const_iterator lower_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
  1485. //! <b>Effects</b>: Returns an iterator to the first element whose
  1486. //! key is greater than k or end() if that element does not exist.
  1487. //!
  1488. //! <b>Complexity</b>: Logarithmic.
  1489. //!
  1490. //! <b>Throws</b>: If `key_compare` throws.
  1491. iterator upper_bound(const key_type &key);
  1492. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1493. //! !comp(key, nk), with nk the key_type of a value_type inserted into `*this`.
  1494. //!
  1495. //! <b>Effects</b>: Returns an iterator to the first element whose
  1496. //! key is greater than k according to comp or end() if that element
  1497. //! does not exist.
  1498. //!
  1499. //! <b>Complexity</b>: Logarithmic.
  1500. //!
  1501. //! <b>Throws</b>: If `comp` throws.
  1502. template<class KeyType, class KeyTypeKeyCompare>
  1503. iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp);
  1504. //! @copydoc ::boost::intrusive::bstree::upper_bound(const key_type &)
  1505. const_iterator upper_bound(const key_type &key) const;
  1506. //! @copydoc ::boost::intrusive::bstree::upper_bound(const KeyType&,KeyTypeKeyCompare)
  1507. template<class KeyType, class KeyTypeKeyCompare>
  1508. const_iterator upper_bound(const KeyType &key, KeyTypeKeyCompare comp) const;
  1509. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1510. //! k or end() if that element does not exist.
  1511. //!
  1512. //! <b>Complexity</b>: Logarithmic.
  1513. //!
  1514. //! <b>Throws</b>: If `key_compare` throws.
  1515. iterator find(const key_type &key);
  1516. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1517. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1518. //! and nk the key_type of a value_type inserted into `*this`.
  1519. //!
  1520. //! <b>Effects</b>: Finds an iterator to the first element whose key is
  1521. //! k or end() if that element does not exist.
  1522. //!
  1523. //! <b>Complexity</b>: Logarithmic.
  1524. //!
  1525. //! <b>Throws</b>: If `comp` throws.
  1526. template<class KeyType, class KeyTypeKeyCompare>
  1527. iterator find(const KeyType &key, KeyTypeKeyCompare comp);
  1528. //! @copydoc ::boost::intrusive::bstree::find(const key_type &)
  1529. const_iterator find(const key_type &key) const;
  1530. //! @copydoc ::boost::intrusive::bstree::find(const KeyType&,KeyTypeKeyCompare)
  1531. template<class KeyType, class KeyTypeKeyCompare>
  1532. const_iterator find(const KeyType &key, KeyTypeKeyCompare comp) const;
  1533. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1534. //! an empty range that indicates the position where those elements would be
  1535. //! if they there is no elements with key k.
  1536. //!
  1537. //! <b>Complexity</b>: Logarithmic.
  1538. //!
  1539. //! <b>Throws</b>: If `key_compare` throws.
  1540. std::pair<iterator,iterator> equal_range(const key_type &key);
  1541. //! <b>Requires</b>: key is a value such that `*this` is partitioned with respect to
  1542. //! comp(nk, key) and !comp(key, nk), with comp(nk, key) implying !comp(key, nk),
  1543. //! with nk the key_type of a value_type inserted into `*this`.
  1544. //!
  1545. //! <b>Effects</b>: Finds a range containing all elements whose key is k or
  1546. //! an empty range that indicates the position where those elements would be
  1547. //! if they there is no elements with key k.
  1548. //!
  1549. //! <b>Complexity</b>: Logarithmic.
  1550. //!
  1551. //! <b>Throws</b>: If `comp` throws.
  1552. template<class KeyType, class KeyTypeKeyCompare>
  1553. std::pair<iterator,iterator> equal_range(const KeyType &key, KeyTypeKeyCompare comp);
  1554. //! @copydoc ::boost::intrusive::bstree::equal_range(const key_type &)
  1555. std::pair<const_iterator, const_iterator> equal_range(const key_type &key) const;
  1556. //! @copydoc ::boost::intrusive::bstree::equal_range(const KeyType&,KeyTypeKeyCompare)
  1557. template<class KeyType, class KeyTypeKeyCompare>
  1558. std::pair<const_iterator, const_iterator>
  1559. equal_range(const KeyType &key, KeyTypeKeyCompare comp) const;
  1560. //! <b>Requires</b>:
  1561. //! `upper_key` shall not precede `lower_key` according to key_compare.
  1562. //! [key_comp()(upper_key, lower_key) shall be false]
  1563. //!
  1564. //! If `lower_key` is equivalent to `upper_key`
  1565. //! [!key_comp()(upper_key, lower_key) && !key_comp()(lower_key, upper_key)] then
  1566. //! ('left_closed' || 'right_closed') must be false.
  1567. //!
  1568. //! <b>Effects</b>: Returns an a pair with the following criteria:
  1569. //!
  1570. //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise
  1571. //!
  1572. //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise
  1573. //!
  1574. //! <b>Complexity</b>: Logarithmic.
  1575. //!
  1576. //! <b>Throws</b>: If `key_compare` throws.
  1577. //!
  1578. //! <b>Note</b>: This function can be more efficient than calling upper_bound
  1579. //! and lower_bound for lower_value and upper_value.
  1580. //!
  1581. //! <b>Note</b>: Experimental function, the interface might change in future releases.
  1582. std::pair<iterator,iterator> bounded_range
  1583. (const key_type &lower_key, const key_type &upper_value, bool left_closed, bool right_closed);
  1584. //! <b>Requires</b>:
  1585. //! `lower_key` is a value such that `*this` is partitioned with respect to
  1586. //! comp(nk, lower_key) if left_closed is true, with respect to !comp(lower_key, nk) otherwise.
  1587. //!
  1588. //! `upper_key` is a value such that `*this` is partitioned with respect to
  1589. //! !comp(upper_key, nk) if right_closed is true, with respect to comp(nk, upper_key) otherwise.
  1590. //!
  1591. //! `upper_key` shall not precede `lower_key` according to comp
  1592. //! [comp(upper_key, lower_key) shall be false]
  1593. //!
  1594. //! If `lower_key` is equivalent to `upper_key`
  1595. //! [!comp(upper_key, lower_key) && !comp(lower_key, upper_key)] then
  1596. //! ('left_closed' || 'right_closed') must be false.
  1597. //!
  1598. //! <b>Effects</b>: Returns an a pair with the following criteria:
  1599. //!
  1600. //! first = lower_bound(lower_key, comp) if left_closed, upper_bound(lower_key, comp) otherwise
  1601. //!
  1602. //! second = upper_bound(upper_key, comp) if right_closed, lower_bound(upper_key, comp) otherwise
  1603. //!
  1604. //! <b>Complexity</b>: Logarithmic.
  1605. //!
  1606. //! <b>Throws</b>: If `comp` throws.
  1607. //!
  1608. //! <b>Note</b>: This function can be more efficient than calling upper_bound
  1609. //! and lower_bound for lower_key and upper_key.
  1610. //!
  1611. //! <b>Note</b>: Experimental function, the interface might change in future releases.
  1612. template<class KeyType, class KeyTypeKeyCompare>
  1613. std::pair<iterator,iterator> bounded_range
  1614. (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed);
  1615. //! @copydoc ::boost::intrusive::bstree::bounded_range(const key_type &,const key_type &,bool,bool)
  1616. std::pair<const_iterator,const_iterator> bounded_range
  1617. (const key_type &lower_key, const key_type &upper_key, bool left_closed, bool right_closed) const;
  1618. //! @copydoc ::boost::intrusive::bstree::bounded_range(const KeyType&,const KeyType&,KeyTypeKeyCompare,bool,bool)
  1619. template<class KeyType, class KeyTypeKeyCompare>
  1620. std::pair<const_iterator,const_iterator> bounded_range
  1621. (const KeyType &lower_key, const KeyType &upper_key, KeyTypeKeyCompare comp, bool left_closed, bool right_closed) const;
  1622. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1623. //! appropriate type. Otherwise the behavior is undefined.
  1624. //!
  1625. //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
  1626. //! that points to the value
  1627. //!
  1628. //! <b>Complexity</b>: Constant.
  1629. //!
  1630. //! <b>Throws</b>: Nothing.
  1631. //!
  1632. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1633. //! is stateless.
  1634. static iterator s_iterator_to(reference value);
  1635. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1636. //! appropriate type. Otherwise the behavior is undefined.
  1637. //!
  1638. //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
  1639. //! set that points to the value
  1640. //!
  1641. //! <b>Complexity</b>: Constant.
  1642. //!
  1643. //! <b>Throws</b>: Nothing.
  1644. //!
  1645. //! <b>Note</b>: This static function is available only if the <i>value traits</i>
  1646. //! is stateless.
  1647. static const_iterator s_iterator_to(const_reference value);
  1648. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1649. //! appropriate type. Otherwise the behavior is undefined.
  1650. //!
  1651. //! <b>Effects</b>: Returns: a valid iterator i belonging to the set
  1652. //! that points to the value
  1653. //!
  1654. //! <b>Complexity</b>: Constant.
  1655. //!
  1656. //! <b>Throws</b>: Nothing.
  1657. iterator iterator_to(reference value);
  1658. //! <b>Requires</b>: value must be an lvalue and shall be in a set of
  1659. //! appropriate type. Otherwise the behavior is undefined.
  1660. //!
  1661. //! <b>Effects</b>: Returns: a valid const_iterator i belonging to the
  1662. //! set that points to the value
  1663. //!
  1664. //! <b>Complexity</b>: Constant.
  1665. //!
  1666. //! <b>Throws</b>: Nothing.
  1667. const_iterator iterator_to(const_reference value) const;
  1668. //! <b>Requires</b>: value shall not be in a container.
  1669. //!
  1670. //! <b>Effects</b>: init_node puts the hook of a value in a well-known default
  1671. //! state.
  1672. //!
  1673. //! <b>Throws</b>: Nothing.
  1674. //!
  1675. //! <b>Complexity</b>: Constant time.
  1676. //!
  1677. //! <b>Note</b>: This function puts the hook in the well-known default state
  1678. //! used by auto_unlink and safe hooks.
  1679. static void init_node(reference value);
  1680. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1681. //! <b>Effects</b>: Unlinks the leftmost node from the container.
  1682. //!
  1683. //! <b>Complexity</b>: Average complexity is constant time.
  1684. //!
  1685. //! <b>Throws</b>: Nothing.
  1686. //!
  1687. //! <b>Notes</b>: This function breaks the container and the container can
  1688. //! only be used for more unlink_leftmost_without_rebalance calls.
  1689. //! This function is normally used to achieve a step by step
  1690. //! controlled destruction of the container.
  1691. pointer unlink_leftmost_without_rebalance()
  1692. {
  1693. node_ptr to_be_disposed(node_algorithms::unlink_leftmost_without_rebalance
  1694. (this->header_ptr()));
  1695. if(!to_be_disposed)
  1696. return 0;
  1697. this->sz_traits().decrement();
  1698. if(safemode_or_autounlink)//If this is commented does not work with normal_link
  1699. node_algorithms::init(to_be_disposed);
  1700. return this->get_value_traits().to_value_ptr(to_be_disposed);
  1701. }
  1702. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1703. //! <b>Requires</b>: replace_this must be a valid iterator of *this
  1704. //! and with_this must not be inserted in any container.
  1705. //!
  1706. //! <b>Effects</b>: Replaces replace_this in its position in the
  1707. //! container with with_this. The container does not need to be rebalanced.
  1708. //!
  1709. //! <b>Complexity</b>: Constant.
  1710. //!
  1711. //! <b>Throws</b>: Nothing.
  1712. //!
  1713. //! <b>Note</b>: This function will break container ordering invariants if
  1714. //! with_this is not equivalent to *replace_this according to the
  1715. //! ordering rules. This function is faster than erasing and inserting
  1716. //! the node, since no rebalancing or comparison is needed.
  1717. void replace_node(iterator replace_this, reference with_this);
  1718. //! <b>Effects</b>: Rebalances the tree.
  1719. //!
  1720. //! <b>Throws</b>: Nothing.
  1721. //!
  1722. //! <b>Complexity</b>: Linear.
  1723. void rebalance();
  1724. //! <b>Requires</b>: old_root is a node of a tree.
  1725. //!
  1726. //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
  1727. //!
  1728. //! <b>Returns</b>: The new root of the subtree.
  1729. //!
  1730. //! <b>Throws</b>: Nothing.
  1731. //!
  1732. //! <b>Complexity</b>: Linear to the elements in the subtree.
  1733. iterator rebalance_subtree(iterator root);
  1734. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1735. //! <b>Effects</b>: removes "value" from the container.
  1736. //!
  1737. //! <b>Throws</b>: Nothing.
  1738. //!
  1739. //! <b>Complexity</b>: Logarithmic time.
  1740. //!
  1741. //! <b>Note</b>: This static function is only usable with non-constant
  1742. //! time size containers that have stateless comparison functors.
  1743. //!
  1744. //! If the user calls
  1745. //! this function with a constant time size container or stateful comparison
  1746. //! functor a compilation error will be issued.
  1747. static void remove_node(reference value)
  1748. {
  1749. BOOST_STATIC_ASSERT((!constant_time_size));
  1750. node_ptr to_remove(value_traits::to_node_ptr(value));
  1751. node_algorithms::unlink(to_remove);
  1752. if(safemode_or_autounlink)
  1753. node_algorithms::init(to_remove);
  1754. }
  1755. //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
  1756. //! function from *this.
  1757. //!
  1758. //! <b>Effects</b>: Attempts to extract each element in source and insert it into a using
  1759. //! the comparison object of *this. If there is an element in a with key equivalent to the
  1760. //! key of an element from source, then that element is not extracted from source.
  1761. //!
  1762. //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
  1763. //! to those same elements but as members of *this. Iterators referring to the transferred
  1764. //! elements will continue to refer to their elements, but they now behave as iterators into *this,
  1765. //! not into source.
  1766. //!
  1767. //! <b>Throws</b>: Nothing unless the comparison object throws.
  1768. //!
  1769. //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
  1770. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1771. template<class T, class ...Options2> void merge_unique(bstree<T, Options2...> &);
  1772. #else
  1773. template<class Compare2>
  1774. void merge_unique(bstree_impl
  1775. <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
  1776. #endif
  1777. {
  1778. node_ptr it (node_algorithms::begin_node(source.header_ptr()))
  1779. , itend(node_algorithms::end_node (source.header_ptr()));
  1780. while(it != itend){
  1781. node_ptr const p(it);
  1782. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
  1783. it = node_algorithms::next_node(it);
  1784. if( node_algorithms::transfer_unique(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p) ){
  1785. source.sz_traits().decrement();
  1786. this->sz_traits().increment();
  1787. }
  1788. }
  1789. }
  1790. //! <b>Requires</b>: "source" container's Options can only can differ in the comparison
  1791. //! function from *this.
  1792. //!
  1793. //! <b>Effects</b>: Extracts each element in source and insert it into a using
  1794. //! the comparison object of *this.
  1795. //!
  1796. //! <b>Postcondition</b>: Pointers and references to the transferred elements of source refer
  1797. //! to those same elements but as members of *this. Iterators referring to the transferred
  1798. //! elements will continue to refer to their elements, but they now behave as iterators into *this,
  1799. //! not into source.
  1800. //!
  1801. //! <b>Throws</b>: Nothing unless the comparison object throws.
  1802. //!
  1803. //! <b>Complexity</b>: N log(a.size() + N) (N has the value source.size())
  1804. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  1805. template<class T, class ...Options2> void merge_equal(bstree<T, Options2...> &);
  1806. #else
  1807. template<class Compare2>
  1808. void merge_equal(bstree_impl
  1809. <ValueTraits, VoidOrKeyOfValue, Compare2, SizeType, ConstantTimeSize, AlgoType, HeaderHolder> &source)
  1810. #endif
  1811. {
  1812. node_ptr it (node_algorithms::begin_node(source.header_ptr()))
  1813. , itend(node_algorithms::end_node (source.header_ptr()));
  1814. while(it != itend){
  1815. node_ptr const p(it);
  1816. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || !node_algorithms::unique(p));
  1817. it = node_algorithms::next_node(it);
  1818. node_algorithms::transfer_equal(this->header_ptr(), this->key_node_comp(this->key_comp()), source.header_ptr(), p);
  1819. source.sz_traits().decrement();
  1820. this->sz_traits().increment();
  1821. }
  1822. }
  1823. //! <b>Effects</b>: Asserts the integrity of the container with additional checks provided by the user.
  1824. //!
  1825. //! <b>Complexity</b>: Linear time.
  1826. //!
  1827. //! <b>Note</b>: The method might not have effect when asserts are turned off (e.g., with NDEBUG).
  1828. //! Experimental function, interface might change in future versions.
  1829. template <class ExtraChecker>
  1830. void check(ExtraChecker extra_checker) const
  1831. {
  1832. typedef detail::key_nodeptr_comp<key_compare, value_traits, key_of_value> nodeptr_comp_t;
  1833. nodeptr_comp_t nodeptr_comp(this->key_comp(), &this->get_value_traits());
  1834. typedef typename get_node_checker<AlgoType, ValueTraits, nodeptr_comp_t, ExtraChecker>::type node_checker_t;
  1835. typename node_checker_t::return_type checker_return;
  1836. node_algorithms::check(this->header_ptr(), node_checker_t(nodeptr_comp, extra_checker), checker_return);
  1837. BOOST_INTRUSIVE_INVARIANT_ASSERT(!constant_time_size || this->sz_traits().get_size() == checker_return.node_count);
  1838. }
  1839. //! <b>Effects</b>: Asserts the integrity of the container.
  1840. //!
  1841. //! <b>Complexity</b>: Linear time.
  1842. //!
  1843. //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
  1844. //! Experimental function, interface might change in future versions.
  1845. void check() const
  1846. {
  1847. check(detail::empty_node_checker<ValueTraits>());
  1848. }
  1849. friend bool operator==(const bstree_impl &x, const bstree_impl &y)
  1850. {
  1851. if(constant_time_size && x.size() != y.size()){
  1852. return false;
  1853. }
  1854. return boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
  1855. }
  1856. friend bool operator!=(const bstree_impl &x, const bstree_impl &y)
  1857. { return !(x == y); }
  1858. friend bool operator<(const bstree_impl &x, const bstree_impl &y)
  1859. { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1860. friend bool operator>(const bstree_impl &x, const bstree_impl &y)
  1861. { return y < x; }
  1862. friend bool operator<=(const bstree_impl &x, const bstree_impl &y)
  1863. { return !(x > y); }
  1864. friend bool operator>=(const bstree_impl &x, const bstree_impl &y)
  1865. { return !(x < y); }
  1866. friend void swap(bstree_impl &x, bstree_impl &y)
  1867. { x.swap(y); }
  1868. /// @cond
  1869. private:
  1870. template<class Disposer>
  1871. iterator private_erase(const_iterator b, const_iterator e, size_type &n, Disposer disposer)
  1872. {
  1873. for(n = 0; b != e; ++n)
  1874. this->erase_and_dispose(b++, disposer);
  1875. return b.unconst();
  1876. }
  1877. iterator private_erase(const_iterator b, const_iterator e, size_type &n)
  1878. {
  1879. for(n = 0; b != e; ++n)
  1880. this->erase(b++);
  1881. return b.unconst();
  1882. }
  1883. /// @endcond
  1884. };
  1885. //! Helper metafunction to define a \c bstree that yields to the same type when the
  1886. //! same options (either explicitly or implicitly) are used.
  1887. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1888. template<class T, class ...Options>
  1889. #else
  1890. template<class T, class O1 = void, class O2 = void
  1891. , class O3 = void, class O4 = void
  1892. , class O5 = void, class O6 = void>
  1893. #endif
  1894. struct make_bstree
  1895. {
  1896. /// @cond
  1897. typedef typename pack_options
  1898. < bstree_defaults,
  1899. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1900. O1, O2, O3, O4, O5, O6
  1901. #else
  1902. Options...
  1903. #endif
  1904. >::type packed_options;
  1905. typedef typename detail::get_value_traits
  1906. <T, typename packed_options::proto_value_traits>::type value_traits;
  1907. typedef bstree_impl
  1908. < value_traits
  1909. , typename packed_options::key_of_value
  1910. , typename packed_options::compare
  1911. , typename packed_options::size_type
  1912. , packed_options::constant_time_size
  1913. , BsTreeAlgorithms
  1914. , typename packed_options::header_holder_type
  1915. > implementation_defined;
  1916. /// @endcond
  1917. typedef implementation_defined type;
  1918. };
  1919. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1920. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1921. template<class T, class O1, class O2, class O3, class O4, class O5, class O6>
  1922. #else
  1923. template<class T, class ...Options>
  1924. #endif
  1925. class bstree
  1926. : public make_bstree<T,
  1927. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1928. O1, O2, O3, O4, O5, O6
  1929. #else
  1930. Options...
  1931. #endif
  1932. >::type
  1933. {
  1934. typedef typename make_bstree
  1935. <T,
  1936. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  1937. O1, O2, O3, O4, O5, O6
  1938. #else
  1939. Options...
  1940. #endif
  1941. >::type Base;
  1942. BOOST_MOVABLE_BUT_NOT_COPYABLE(bstree)
  1943. public:
  1944. typedef typename Base::key_compare key_compare;
  1945. typedef typename Base::value_traits value_traits;
  1946. typedef typename Base::iterator iterator;
  1947. typedef typename Base::const_iterator const_iterator;
  1948. //Assert if passed value traits are compatible with the type
  1949. BOOST_STATIC_ASSERT((detail::is_same<typename value_traits::value_type, T>::value));
  1950. BOOST_INTRUSIVE_FORCEINLINE bstree()
  1951. : Base()
  1952. {}
  1953. BOOST_INTRUSIVE_FORCEINLINE explicit bstree( const key_compare &cmp, const value_traits &v_traits = value_traits())
  1954. : Base(cmp, v_traits)
  1955. {}
  1956. template<class Iterator>
  1957. BOOST_INTRUSIVE_FORCEINLINE bstree( bool unique, Iterator b, Iterator e
  1958. , const key_compare &cmp = key_compare()
  1959. , const value_traits &v_traits = value_traits())
  1960. : Base(unique, b, e, cmp, v_traits)
  1961. {}
  1962. BOOST_INTRUSIVE_FORCEINLINE bstree(BOOST_RV_REF(bstree) x)
  1963. : Base(BOOST_MOVE_BASE(Base, x))
  1964. {}
  1965. BOOST_INTRUSIVE_FORCEINLINE bstree& operator=(BOOST_RV_REF(bstree) x)
  1966. { return static_cast<bstree &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
  1967. template <class Cloner, class Disposer>
  1968. BOOST_INTRUSIVE_FORCEINLINE void clone_from(const bstree &src, Cloner cloner, Disposer disposer)
  1969. { Base::clone_from(src, cloner, disposer); }
  1970. template <class Cloner, class Disposer>
  1971. BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(bstree) src, Cloner cloner, Disposer disposer)
  1972. { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
  1973. BOOST_INTRUSIVE_FORCEINLINE static bstree &container_from_end_iterator(iterator end_iterator)
  1974. { return static_cast<bstree &>(Base::container_from_end_iterator(end_iterator)); }
  1975. BOOST_INTRUSIVE_FORCEINLINE static const bstree &container_from_end_iterator(const_iterator end_iterator)
  1976. { return static_cast<const bstree &>(Base::container_from_end_iterator(end_iterator)); }
  1977. BOOST_INTRUSIVE_FORCEINLINE static bstree &container_from_iterator(iterator it)
  1978. { return static_cast<bstree &>(Base::container_from_iterator(it)); }
  1979. BOOST_INTRUSIVE_FORCEINLINE static const bstree &container_from_iterator(const_iterator it)
  1980. { return static_cast<const bstree &>(Base::container_from_iterator(it)); }
  1981. };
  1982. #endif
  1983. } //namespace intrusive
  1984. } //namespace boost
  1985. #include <boost/intrusive/detail/config_end.hpp>
  1986. #endif //BOOST_INTRUSIVE_BSTREE_HPP