bstree.hpp 79 KB

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