bstree.hpp 93 KB

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