bstree.hpp 86 KB

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