tree.hpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_TREE_HPP
  11. #define BOOST_CONTAINER_TREE_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. // container
  21. #include <boost/container/allocator_traits.hpp>
  22. #include <boost/container/container_fwd.hpp>
  23. #include <boost/container/options.hpp>
  24. #include <boost/container/node_handle.hpp>
  25. // container/detail
  26. #include <boost/container/detail/algorithm.hpp> //algo_equal(), algo_lexicographical_compare
  27. #include <boost/container/detail/compare_functors.hpp>
  28. #include <boost/container/detail/destroyers.hpp>
  29. #include <boost/container/detail/iterator.hpp>
  30. #include <boost/container/detail/iterators.hpp>
  31. #include <boost/container/detail/node_alloc_holder.hpp>
  32. #include <boost/container/detail/pair.hpp>
  33. #include <boost/container/detail/type_traits.hpp>
  34. // intrusive
  35. #include <boost/intrusive/pointer_traits.hpp>
  36. #include <boost/intrusive/rbtree.hpp>
  37. #include <boost/intrusive/avltree.hpp>
  38. #include <boost/intrusive/splaytree.hpp>
  39. #include <boost/intrusive/sgtree.hpp>
  40. // intrusive/detail
  41. #include <boost/intrusive/detail/minimal_pair_header.hpp> //pair
  42. #include <boost/intrusive/detail/tree_value_compare.hpp> //tree_value_compare
  43. // move
  44. #include <boost/move/utility_core.hpp>
  45. // move/detail
  46. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  47. #include <boost/move/detail/fwd_macros.hpp>
  48. #endif
  49. #include <boost/move/detail/move_helpers.hpp>
  50. // other
  51. #include <boost/core/no_exceptions_support.hpp>
  52. #include <boost/container/detail/std_fwd.hpp>
  53. namespace boost {
  54. namespace container {
  55. namespace dtl {
  56. using boost::intrusive::tree_value_compare;
  57. template<class VoidPointer, boost::container::tree_type_enum tree_type_value, bool OptimizeSize>
  58. struct intrusive_tree_hook;
  59. template<class VoidPointer, bool OptimizeSize>
  60. struct intrusive_tree_hook<VoidPointer, boost::container::red_black_tree, OptimizeSize>
  61. {
  62. typedef typename dtl::bi::make_set_base_hook
  63. < dtl::bi::void_pointer<VoidPointer>
  64. , dtl::bi::link_mode<dtl::bi::normal_link>
  65. , dtl::bi::optimize_size<OptimizeSize>
  66. >::type type;
  67. };
  68. template<class VoidPointer, bool OptimizeSize>
  69. struct intrusive_tree_hook<VoidPointer, boost::container::avl_tree, OptimizeSize>
  70. {
  71. typedef typename dtl::bi::make_avl_set_base_hook
  72. < dtl::bi::void_pointer<VoidPointer>
  73. , dtl::bi::link_mode<dtl::bi::normal_link>
  74. , dtl::bi::optimize_size<OptimizeSize>
  75. >::type type;
  76. };
  77. template<class VoidPointer, bool OptimizeSize>
  78. struct intrusive_tree_hook<VoidPointer, boost::container::scapegoat_tree, OptimizeSize>
  79. {
  80. typedef typename dtl::bi::make_bs_set_base_hook
  81. < dtl::bi::void_pointer<VoidPointer>
  82. , dtl::bi::link_mode<dtl::bi::normal_link>
  83. >::type type;
  84. };
  85. template<class VoidPointer, bool OptimizeSize>
  86. struct intrusive_tree_hook<VoidPointer, boost::container::splay_tree, OptimizeSize>
  87. {
  88. typedef typename dtl::bi::make_bs_set_base_hook
  89. < dtl::bi::void_pointer<VoidPointer>
  90. , dtl::bi::link_mode<dtl::bi::normal_link>
  91. >::type type;
  92. };
  93. //This trait is used to type-pun std::pair because in C++03
  94. //compilers std::pair is useless for C++11 features
  95. template<class T>
  96. struct tree_internal_data_type
  97. {
  98. typedef T type;
  99. };
  100. template<class T1, class T2>
  101. struct tree_internal_data_type< std::pair<T1, T2> >
  102. {
  103. typedef pair<typename boost::move_detail::remove_const<T1>::type, T2> type;
  104. };
  105. //The node to be store in the tree
  106. template <class T, class VoidPointer, boost::container::tree_type_enum tree_type_value, bool OptimizeSize>
  107. struct tree_node
  108. : public intrusive_tree_hook<VoidPointer, tree_type_value, OptimizeSize>::type
  109. {
  110. private:
  111. //BOOST_COPYABLE_AND_MOVABLE(tree_node)
  112. tree_node();
  113. public:
  114. typedef typename intrusive_tree_hook
  115. <VoidPointer, tree_type_value, OptimizeSize>::type hook_type;
  116. typedef T value_type;
  117. typedef typename tree_internal_data_type<T>::type internal_type;
  118. typedef tree_node< T, VoidPointer
  119. , tree_type_value, OptimizeSize> node_t;
  120. BOOST_CONTAINER_FORCEINLINE T &get_data()
  121. {
  122. T* ptr = reinterpret_cast<T*>(&this->m_data);
  123. return *ptr;
  124. }
  125. BOOST_CONTAINER_FORCEINLINE const T &get_data() const
  126. {
  127. const T* ptr = reinterpret_cast<const T*>(&this->m_data);
  128. return *ptr;
  129. }
  130. internal_type m_data;
  131. template<class T1, class T2>
  132. BOOST_CONTAINER_FORCEINLINE void do_assign(const std::pair<const T1, T2> &p)
  133. {
  134. const_cast<T1&>(m_data.first) = p.first;
  135. m_data.second = p.second;
  136. }
  137. template<class T1, class T2>
  138. BOOST_CONTAINER_FORCEINLINE void do_assign(const pair<const T1, T2> &p)
  139. {
  140. const_cast<T1&>(m_data.first) = p.first;
  141. m_data.second = p.second;
  142. }
  143. template<class V>
  144. BOOST_CONTAINER_FORCEINLINE void do_assign(const V &v)
  145. { m_data = v; }
  146. template<class T1, class T2>
  147. BOOST_CONTAINER_FORCEINLINE void do_move_assign(std::pair<const T1, T2> &p)
  148. {
  149. const_cast<T1&>(m_data.first) = ::boost::move(p.first);
  150. m_data.second = ::boost::move(p.second);
  151. }
  152. template<class T1, class T2>
  153. BOOST_CONTAINER_FORCEINLINE void do_move_assign(pair<const T1, T2> &p)
  154. {
  155. const_cast<T1&>(m_data.first) = ::boost::move(p.first);
  156. m_data.second = ::boost::move(p.second);
  157. }
  158. template<class V>
  159. BOOST_CONTAINER_FORCEINLINE void do_move_assign(V &v)
  160. { m_data = ::boost::move(v); }
  161. };
  162. template <class T, class VoidPointer, boost::container::tree_type_enum tree_type_value, bool OptimizeSize>
  163. struct iiterator_node_value_type< tree_node<T, VoidPointer, tree_type_value, OptimizeSize> > {
  164. typedef T type;
  165. };
  166. template<class Node, class Icont>
  167. class insert_equal_end_hint_functor
  168. {
  169. Icont &icont_;
  170. public:
  171. BOOST_CONTAINER_FORCEINLINE insert_equal_end_hint_functor(Icont &icont)
  172. : icont_(icont)
  173. {}
  174. BOOST_CONTAINER_FORCEINLINE void operator()(Node &n)
  175. { this->icont_.insert_equal(this->icont_.cend(), n); }
  176. };
  177. template<class Node, class Icont>
  178. class push_back_functor
  179. {
  180. Icont &icont_;
  181. public:
  182. BOOST_CONTAINER_FORCEINLINE push_back_functor(Icont &icont)
  183. : icont_(icont)
  184. {}
  185. BOOST_CONTAINER_FORCEINLINE void operator()(Node &n)
  186. { this->icont_.push_back(n); }
  187. };
  188. }//namespace dtl {
  189. namespace dtl {
  190. template< class NodeType, class NodeCompareType
  191. , class SizeType, class HookType
  192. , boost::container::tree_type_enum tree_type_value>
  193. struct intrusive_tree_dispatch;
  194. template<class NodeType, class NodeCompareType, class SizeType, class HookType>
  195. struct intrusive_tree_dispatch
  196. <NodeType, NodeCompareType, SizeType, HookType, boost::container::red_black_tree>
  197. {
  198. typedef typename dtl::bi::make_rbtree
  199. <NodeType
  200. ,dtl::bi::compare<NodeCompareType>
  201. ,dtl::bi::base_hook<HookType>
  202. ,dtl::bi::constant_time_size<true>
  203. ,dtl::bi::size_type<SizeType>
  204. >::type type;
  205. };
  206. template<class NodeType, class NodeCompareType, class SizeType, class HookType>
  207. struct intrusive_tree_dispatch
  208. <NodeType, NodeCompareType, SizeType, HookType, boost::container::avl_tree>
  209. {
  210. typedef typename dtl::bi::make_avltree
  211. <NodeType
  212. ,dtl::bi::compare<NodeCompareType>
  213. ,dtl::bi::base_hook<HookType>
  214. ,dtl::bi::constant_time_size<true>
  215. ,dtl::bi::size_type<SizeType>
  216. >::type type;
  217. };
  218. template<class NodeType, class NodeCompareType, class SizeType, class HookType>
  219. struct intrusive_tree_dispatch
  220. <NodeType, NodeCompareType, SizeType, HookType, boost::container::scapegoat_tree>
  221. {
  222. typedef typename dtl::bi::make_sgtree
  223. <NodeType
  224. ,dtl::bi::compare<NodeCompareType>
  225. ,dtl::bi::base_hook<HookType>
  226. ,dtl::bi::floating_point<true>
  227. ,dtl::bi::size_type<SizeType>
  228. >::type type;
  229. };
  230. template<class NodeType, class NodeCompareType, class SizeType, class HookType>
  231. struct intrusive_tree_dispatch
  232. <NodeType, NodeCompareType, SizeType, HookType, boost::container::splay_tree>
  233. {
  234. typedef typename dtl::bi::make_splaytree
  235. <NodeType
  236. ,dtl::bi::compare<NodeCompareType>
  237. ,dtl::bi::base_hook<HookType>
  238. ,dtl::bi::constant_time_size<true>
  239. ,dtl::bi::size_type<SizeType>
  240. >::type type;
  241. };
  242. template<class Allocator, class ValueCompare, boost::container::tree_type_enum tree_type_value, bool OptimizeSize>
  243. struct intrusive_tree_type
  244. {
  245. private:
  246. typedef typename boost::container::
  247. allocator_traits<Allocator>::value_type value_type;
  248. typedef typename boost::container::
  249. allocator_traits<Allocator>::void_pointer void_pointer;
  250. typedef typename boost::container::
  251. allocator_traits<Allocator>::size_type size_type;
  252. typedef typename dtl::tree_node
  253. < value_type, void_pointer
  254. , tree_type_value, OptimizeSize> node_t;
  255. typedef value_to_node_compare
  256. <node_t, ValueCompare> node_compare_type;
  257. //Deducing the hook type from node_t (e.g. node_t::hook_type) would
  258. //provoke an early instantiation of node_t that could ruin recursive
  259. //tree definitions, so retype the complete type to avoid any problem.
  260. typedef typename intrusive_tree_hook
  261. <void_pointer, tree_type_value
  262. , OptimizeSize>::type hook_type;
  263. public:
  264. typedef typename intrusive_tree_dispatch
  265. < node_t, node_compare_type
  266. , size_type, hook_type
  267. , tree_type_value>::type type;
  268. };
  269. //Trait to detect manually rebalanceable tree types
  270. template<boost::container::tree_type_enum tree_type_value>
  271. struct is_manually_balanceable
  272. { static const bool value = true; };
  273. template<> struct is_manually_balanceable<red_black_tree>
  274. { static const bool value = false; };
  275. template<> struct is_manually_balanceable<avl_tree>
  276. { static const bool value = false; };
  277. //Proxy traits to implement different operations depending on the
  278. //is_manually_balanceable<>::value
  279. template< boost::container::tree_type_enum tree_type_value
  280. , bool IsManuallyRebalanceable = is_manually_balanceable<tree_type_value>::value>
  281. struct intrusive_tree_proxy
  282. {
  283. template<class Icont>
  284. BOOST_CONTAINER_FORCEINLINE static void rebalance(Icont &) {}
  285. };
  286. template<boost::container::tree_type_enum tree_type_value>
  287. struct intrusive_tree_proxy<tree_type_value, true>
  288. {
  289. template<class Icont>
  290. BOOST_CONTAINER_FORCEINLINE static void rebalance(Icont &c)
  291. { c.rebalance(); }
  292. };
  293. } //namespace dtl {
  294. namespace dtl {
  295. //This functor will be used with Intrusive clone functions to obtain
  296. //already allocated nodes from a intrusive container instead of
  297. //allocating new ones. When the intrusive container runs out of nodes
  298. //the node holder is used instead.
  299. template<class AllocHolder, bool DoMove>
  300. class RecyclingCloner
  301. {
  302. typedef typename AllocHolder::intrusive_container intrusive_container;
  303. typedef typename AllocHolder::Node node_t;
  304. typedef typename AllocHolder::NodePtr node_ptr_type;
  305. public:
  306. RecyclingCloner(AllocHolder &holder, intrusive_container &itree)
  307. : m_holder(holder), m_icont(itree)
  308. {}
  309. BOOST_CONTAINER_FORCEINLINE static void do_assign(node_ptr_type &p, const node_t &other, bool_<true>)
  310. { p->do_move_assign(const_cast<node_t &>(other).m_data); }
  311. BOOST_CONTAINER_FORCEINLINE static void do_assign(node_ptr_type &p, const node_t &other, bool_<false>)
  312. { p->do_assign(other.m_data); }
  313. node_ptr_type operator()(const node_t &other) const
  314. {
  315. if(node_ptr_type p = m_icont.unlink_leftmost_without_rebalance()){
  316. //First recycle a node (this can't throw)
  317. BOOST_TRY{
  318. //This can throw
  319. this->do_assign(p, other, bool_<DoMove>());
  320. return p;
  321. }
  322. BOOST_CATCH(...){
  323. //If there is an exception destroy the whole source
  324. m_holder.destroy_node(p);
  325. while((p = m_icont.unlink_leftmost_without_rebalance())){
  326. m_holder.destroy_node(p);
  327. }
  328. BOOST_RETHROW
  329. }
  330. BOOST_CATCH_END
  331. }
  332. else{
  333. return m_holder.create_node(other.m_data);
  334. }
  335. }
  336. AllocHolder &m_holder;
  337. intrusive_container &m_icont;
  338. };
  339. template<class KeyCompare, class KeyOfValue>
  340. struct key_node_compare
  341. : public boost::intrusive::detail::ebo_functor_holder<KeyCompare>
  342. {
  343. BOOST_CONTAINER_FORCEINLINE explicit key_node_compare(const KeyCompare &comp)
  344. : base_t(comp)
  345. {}
  346. typedef boost::intrusive::detail::ebo_functor_holder<KeyCompare> base_t;
  347. typedef KeyCompare key_compare;
  348. typedef KeyOfValue key_of_value;
  349. typedef typename KeyOfValue::type key_type;
  350. template <class T, class VoidPointer, boost::container::tree_type_enum tree_type_value, bool OptimizeSize>
  351. BOOST_CONTAINER_FORCEINLINE static const key_type &
  352. key_from(const tree_node<T, VoidPointer, tree_type_value, OptimizeSize> &n)
  353. {
  354. return key_of_value()(n.get_data());
  355. }
  356. template <class T>
  357. BOOST_CONTAINER_FORCEINLINE static const T &
  358. key_from(const T &t)
  359. {
  360. return t;
  361. }
  362. BOOST_CONTAINER_FORCEINLINE const key_compare &key_comp() const
  363. { return static_cast<const key_compare &>(*this); }
  364. BOOST_CONTAINER_FORCEINLINE key_compare &key_comp()
  365. { return static_cast<key_compare &>(*this); }
  366. BOOST_CONTAINER_FORCEINLINE bool operator()(const key_type &key1, const key_type &key2) const
  367. { return this->key_comp()(key1, key2); }
  368. template<class U>
  369. BOOST_CONTAINER_FORCEINLINE bool operator()(const key_type &key1, const U &nonkey2) const
  370. { return this->key_comp()(key1, this->key_from(nonkey2)); }
  371. template<class U>
  372. BOOST_CONTAINER_FORCEINLINE bool operator()(const U &nonkey1, const key_type &key2) const
  373. { return this->key_comp()(this->key_from(nonkey1), key2); }
  374. template<class U, class V>
  375. BOOST_CONTAINER_FORCEINLINE bool operator()(const U &nonkey1, const V &nonkey2) const
  376. { return this->key_comp()(this->key_from(nonkey1), this->key_from(nonkey2)); }
  377. };
  378. template<class Options>
  379. struct get_tree_opt
  380. {
  381. typedef Options type;
  382. };
  383. template<>
  384. struct get_tree_opt<void>
  385. {
  386. typedef tree_assoc_defaults type;
  387. };
  388. template <class T, class KeyOfValue, class Compare, class Allocator, class Options>
  389. class tree
  390. : public dtl::node_alloc_holder
  391. < Allocator
  392. , typename dtl::intrusive_tree_type
  393. < Allocator, tree_value_compare
  394. <typename allocator_traits<Allocator>::pointer, Compare, KeyOfValue>
  395. , get_tree_opt<Options>::type::tree_type
  396. , get_tree_opt<Options>::type::optimize_size
  397. >::type
  398. >
  399. {
  400. typedef tree_value_compare
  401. < typename allocator_traits<Allocator>::pointer
  402. , Compare, KeyOfValue> ValComp;
  403. typedef typename get_tree_opt<Options>::type options_type;
  404. typedef typename dtl::intrusive_tree_type
  405. < Allocator, ValComp
  406. , options_type::tree_type
  407. , options_type::optimize_size
  408. >::type Icont;
  409. typedef dtl::node_alloc_holder
  410. <Allocator, Icont> AllocHolder;
  411. typedef typename AllocHolder::NodePtr NodePtr;
  412. typedef tree < T, KeyOfValue
  413. , Compare, Allocator, Options> ThisType;
  414. typedef typename AllocHolder::NodeAlloc NodeAlloc;
  415. typedef boost::container::
  416. allocator_traits<NodeAlloc> allocator_traits_type;
  417. typedef typename AllocHolder::ValAlloc ValAlloc;
  418. typedef typename AllocHolder::Node Node;
  419. typedef typename Icont::iterator iiterator;
  420. typedef typename Icont::const_iterator iconst_iterator;
  421. typedef dtl::allocator_destroyer<NodeAlloc> Destroyer;
  422. typedef typename AllocHolder::alloc_version alloc_version;
  423. typedef intrusive_tree_proxy<options_type::tree_type> intrusive_tree_proxy_t;
  424. BOOST_COPYABLE_AND_MOVABLE(tree)
  425. public:
  426. typedef typename KeyOfValue::type key_type;
  427. typedef T value_type;
  428. typedef Allocator allocator_type;
  429. typedef Compare key_compare;
  430. typedef ValComp value_compare;
  431. typedef typename boost::container::
  432. allocator_traits<Allocator>::pointer pointer;
  433. typedef typename boost::container::
  434. allocator_traits<Allocator>::const_pointer const_pointer;
  435. typedef typename boost::container::
  436. allocator_traits<Allocator>::reference reference;
  437. typedef typename boost::container::
  438. allocator_traits<Allocator>::const_reference const_reference;
  439. typedef typename boost::container::
  440. allocator_traits<Allocator>::size_type size_type;
  441. typedef typename boost::container::
  442. allocator_traits<Allocator>::difference_type difference_type;
  443. typedef dtl::iterator_from_iiterator
  444. <iiterator, false> iterator;
  445. typedef dtl::iterator_from_iiterator
  446. <iiterator, true > const_iterator;
  447. typedef boost::container::reverse_iterator
  448. <iterator> reverse_iterator;
  449. typedef boost::container::reverse_iterator
  450. <const_iterator> const_reverse_iterator;
  451. typedef node_handle
  452. < NodeAlloc, void> node_type;
  453. typedef insert_return_type_base
  454. <iterator, node_type> insert_return_type;
  455. typedef NodeAlloc stored_allocator_type;
  456. private:
  457. typedef key_node_compare<key_compare, KeyOfValue> KeyNodeCompare;
  458. public:
  459. BOOST_CONTAINER_FORCEINLINE tree()
  460. : AllocHolder()
  461. {}
  462. BOOST_CONTAINER_FORCEINLINE explicit tree(const key_compare& comp)
  463. : AllocHolder(ValComp(comp))
  464. {}
  465. BOOST_CONTAINER_FORCEINLINE explicit tree(const key_compare& comp, const allocator_type& a)
  466. : AllocHolder(ValComp(comp), a)
  467. {}
  468. BOOST_CONTAINER_FORCEINLINE explicit tree(const allocator_type& a)
  469. : AllocHolder(a)
  470. {}
  471. template <class InputIterator>
  472. tree(bool unique_insertion, InputIterator first, InputIterator last)
  473. : AllocHolder(value_compare(key_compare()))
  474. {
  475. this->tree_construct(unique_insertion, first, last);
  476. //AllocHolder clears in case of exception
  477. }
  478. template <class InputIterator>
  479. tree(bool unique_insertion, InputIterator first, InputIterator last, const key_compare& comp)
  480. : AllocHolder(value_compare(comp))
  481. {
  482. this->tree_construct(unique_insertion, first, last);
  483. //AllocHolder clears in case of exception
  484. }
  485. template <class InputIterator>
  486. tree(bool unique_insertion, InputIterator first, InputIterator last, const key_compare& comp, const allocator_type& a)
  487. : AllocHolder(value_compare(comp), a)
  488. {
  489. this->tree_construct(unique_insertion, first, last);
  490. //AllocHolder clears in case of exception
  491. }
  492. //construct with ordered range
  493. template <class InputIterator>
  494. tree( ordered_range_t, InputIterator first, InputIterator last)
  495. : AllocHolder(value_compare(key_compare()))
  496. {
  497. this->tree_construct(ordered_range_t(), first, last);
  498. }
  499. template <class InputIterator>
  500. tree( ordered_range_t, InputIterator first, InputIterator last, const key_compare& comp)
  501. : AllocHolder(value_compare(comp))
  502. {
  503. this->tree_construct(ordered_range_t(), first, last);
  504. }
  505. template <class InputIterator>
  506. tree( ordered_range_t, InputIterator first, InputIterator last
  507. , const key_compare& comp, const allocator_type& a)
  508. : AllocHolder(value_compare(comp), a)
  509. {
  510. this->tree_construct(ordered_range_t(), first, last);
  511. }
  512. private:
  513. template <class InputIterator>
  514. void tree_construct(bool unique_insertion, InputIterator first, InputIterator last)
  515. {
  516. //Use cend() as hint to achieve linear time for
  517. //ordered ranges as required by the standard
  518. //for the constructor
  519. if(unique_insertion){
  520. const const_iterator end_it(this->cend());
  521. for ( ; first != last; ++first){
  522. this->insert_unique_convertible(end_it, *first);
  523. }
  524. }
  525. else{
  526. this->tree_construct_non_unique(first, last);
  527. }
  528. }
  529. template <class InputIterator>
  530. void tree_construct_non_unique(InputIterator first, InputIterator last
  531. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  532. , typename dtl::enable_if_or
  533. < void
  534. , dtl::is_same<alloc_version, version_1>
  535. , dtl::is_input_iterator<InputIterator>
  536. >::type * = 0
  537. #endif
  538. )
  539. {
  540. //Use cend() as hint to achieve linear time for
  541. //ordered ranges as required by the standard
  542. //for the constructor
  543. const const_iterator end_it(this->cend());
  544. for ( ; first != last; ++first){
  545. this->insert_equal_convertible(end_it, *first);
  546. }
  547. }
  548. template <class InputIterator>
  549. void tree_construct_non_unique(InputIterator first, InputIterator last
  550. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  551. , typename dtl::disable_if_or
  552. < void
  553. , dtl::is_same<alloc_version, version_1>
  554. , dtl::is_input_iterator<InputIterator>
  555. >::type * = 0
  556. #endif
  557. )
  558. {
  559. //Optimized allocation and construction
  560. this->allocate_many_and_construct
  561. ( first, boost::container::iterator_distance(first, last)
  562. , insert_equal_end_hint_functor<Node, Icont>(this->icont()));
  563. }
  564. template <class InputIterator>
  565. void tree_construct( ordered_range_t, InputIterator first, InputIterator last
  566. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  567. , typename dtl::disable_if_or
  568. < void
  569. , dtl::is_same<alloc_version, version_1>
  570. , dtl::is_input_iterator<InputIterator>
  571. >::type * = 0
  572. #endif
  573. )
  574. {
  575. //Optimized allocation and construction
  576. this->allocate_many_and_construct
  577. ( first, boost::container::iterator_distance(first, last)
  578. , dtl::push_back_functor<Node, Icont>(this->icont()));
  579. //AllocHolder clears in case of exception
  580. }
  581. template <class InputIterator>
  582. void tree_construct( ordered_range_t, InputIterator first, InputIterator last
  583. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  584. , typename dtl::enable_if_or
  585. < void
  586. , dtl::is_same<alloc_version, version_1>
  587. , dtl::is_input_iterator<InputIterator>
  588. >::type * = 0
  589. #endif
  590. )
  591. {
  592. for ( ; first != last; ++first){
  593. this->push_back_impl(*first);
  594. }
  595. }
  596. public:
  597. BOOST_CONTAINER_FORCEINLINE tree(const tree& x)
  598. : AllocHolder(x, x.value_comp())
  599. {
  600. this->icont().clone_from
  601. (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc()));
  602. }
  603. BOOST_CONTAINER_FORCEINLINE tree(BOOST_RV_REF(tree) x)
  604. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_constructible<Compare>::value)
  605. : AllocHolder(BOOST_MOVE_BASE(AllocHolder, x), x.value_comp())
  606. {}
  607. BOOST_CONTAINER_FORCEINLINE tree(const tree& x, const allocator_type &a)
  608. : AllocHolder(x.value_comp(), a)
  609. {
  610. this->icont().clone_from
  611. (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc()));
  612. //AllocHolder clears in case of exception
  613. }
  614. tree(BOOST_RV_REF(tree) x, const allocator_type &a)
  615. : AllocHolder(x.value_comp(), a)
  616. {
  617. if(this->node_alloc() == x.node_alloc()){
  618. this->icont().swap(x.icont());
  619. }
  620. else{
  621. this->icont().clone_from
  622. (boost::move(x.icont()), typename AllocHolder::move_cloner(*this), Destroyer(this->node_alloc()));
  623. }
  624. //AllocHolder clears in case of exception
  625. }
  626. BOOST_CONTAINER_FORCEINLINE ~tree()
  627. {} //AllocHolder clears the tree
  628. tree& operator=(BOOST_COPY_ASSIGN_REF(tree) x)
  629. {
  630. if (&x != this){
  631. NodeAlloc &this_alloc = this->get_stored_allocator();
  632. const NodeAlloc &x_alloc = x.get_stored_allocator();
  633. dtl::bool_<allocator_traits<NodeAlloc>::
  634. propagate_on_container_copy_assignment::value> flag;
  635. if(flag && this_alloc != x_alloc){
  636. this->clear();
  637. }
  638. this->AllocHolder::copy_assign_alloc(x);
  639. //Transfer all the nodes to a temporary tree
  640. //If anything goes wrong, all the nodes will be destroyed
  641. //automatically
  642. Icont other_tree(::boost::move(this->icont()));
  643. //Now recreate the source tree reusing nodes stored by other_tree
  644. this->icont().clone_from
  645. (x.icont()
  646. , RecyclingCloner<AllocHolder, false>(*this, other_tree)
  647. , Destroyer(this->node_alloc()));
  648. //If there are remaining nodes, destroy them
  649. NodePtr p;
  650. while((p = other_tree.unlink_leftmost_without_rebalance())){
  651. AllocHolder::destroy_node(p);
  652. }
  653. }
  654. return *this;
  655. }
  656. tree& operator=(BOOST_RV_REF(tree) x)
  657. BOOST_NOEXCEPT_IF( (allocator_traits_type::propagate_on_container_move_assignment::value ||
  658. allocator_traits_type::is_always_equal::value) &&
  659. boost::container::dtl::is_nothrow_move_assignable<Compare>::value)
  660. {
  661. BOOST_ASSERT(this != &x);
  662. NodeAlloc &this_alloc = this->node_alloc();
  663. NodeAlloc &x_alloc = x.node_alloc();
  664. const bool propagate_alloc = allocator_traits<NodeAlloc>::
  665. propagate_on_container_move_assignment::value;
  666. const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
  667. //Resources can be transferred if both allocators are
  668. //going to be equal after this function (either propagated or already equal)
  669. if(propagate_alloc || allocators_equal){
  670. //Destroy
  671. this->clear();
  672. //Move allocator if needed
  673. this->AllocHolder::move_assign_alloc(x);
  674. //Obtain resources
  675. this->icont() = boost::move(x.icont());
  676. }
  677. //Else do a one by one move
  678. else{
  679. //Transfer all the nodes to a temporary tree
  680. //If anything goes wrong, all the nodes will be destroyed
  681. //automatically
  682. Icont other_tree(::boost::move(this->icont()));
  683. //Now recreate the source tree reusing nodes stored by other_tree
  684. this->icont().clone_from
  685. (::boost::move(x.icont())
  686. , RecyclingCloner<AllocHolder, true>(*this, other_tree)
  687. , Destroyer(this->node_alloc()));
  688. //If there are remaining nodes, destroy them
  689. NodePtr p;
  690. while((p = other_tree.unlink_leftmost_without_rebalance())){
  691. AllocHolder::destroy_node(p);
  692. }
  693. }
  694. return *this;
  695. }
  696. public:
  697. // accessors:
  698. BOOST_CONTAINER_FORCEINLINE value_compare value_comp() const
  699. { return this->icont().value_comp().predicate(); }
  700. BOOST_CONTAINER_FORCEINLINE key_compare key_comp() const
  701. { return this->icont().value_comp().predicate().key_comp(); }
  702. BOOST_CONTAINER_FORCEINLINE allocator_type get_allocator() const
  703. { return allocator_type(this->node_alloc()); }
  704. BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const
  705. { return this->node_alloc(); }
  706. BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator()
  707. { return this->node_alloc(); }
  708. BOOST_CONTAINER_FORCEINLINE iterator begin()
  709. { return iterator(this->icont().begin()); }
  710. BOOST_CONTAINER_FORCEINLINE const_iterator begin() const
  711. { return this->cbegin(); }
  712. BOOST_CONTAINER_FORCEINLINE iterator end()
  713. { return iterator(this->icont().end()); }
  714. BOOST_CONTAINER_FORCEINLINE const_iterator end() const
  715. { return this->cend(); }
  716. BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin()
  717. { return reverse_iterator(end()); }
  718. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const
  719. { return this->crbegin(); }
  720. BOOST_CONTAINER_FORCEINLINE reverse_iterator rend()
  721. { return reverse_iterator(begin()); }
  722. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const
  723. { return this->crend(); }
  724. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the container.
  725. //!
  726. //! <b>Throws</b>: Nothing.
  727. //!
  728. //! <b>Complexity</b>: Constant.
  729. BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const
  730. { return const_iterator(this->non_const_icont().begin()); }
  731. //! <b>Effects</b>: Returns a const_iterator to the end of the container.
  732. //!
  733. //! <b>Throws</b>: Nothing.
  734. //!
  735. //! <b>Complexity</b>: Constant.
  736. BOOST_CONTAINER_FORCEINLINE const_iterator cend() const
  737. { return const_iterator(this->non_const_icont().end()); }
  738. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  739. //! of the reversed container.
  740. //!
  741. //! <b>Throws</b>: Nothing.
  742. //!
  743. //! <b>Complexity</b>: Constant.
  744. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const
  745. { return const_reverse_iterator(cend()); }
  746. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  747. //! of the reversed container.
  748. //!
  749. //! <b>Throws</b>: Nothing.
  750. //!
  751. //! <b>Complexity</b>: Constant.
  752. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const
  753. { return const_reverse_iterator(cbegin()); }
  754. BOOST_CONTAINER_FORCEINLINE bool empty() const
  755. { return !this->size(); }
  756. BOOST_CONTAINER_FORCEINLINE size_type size() const
  757. { return this->icont().size(); }
  758. BOOST_CONTAINER_FORCEINLINE size_type max_size() const
  759. { return AllocHolder::max_size(); }
  760. BOOST_CONTAINER_FORCEINLINE void swap(ThisType& x)
  761. BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
  762. && boost::container::dtl::is_nothrow_swappable<Compare>::value )
  763. { AllocHolder::swap(x); }
  764. public:
  765. typedef typename Icont::insert_commit_data insert_commit_data;
  766. // insert/erase
  767. std::pair<iterator,bool> insert_unique_check
  768. (const key_type& key, insert_commit_data &data)
  769. {
  770. std::pair<iiterator, bool> ret =
  771. this->icont().insert_unique_check(key, KeyNodeCompare(key_comp()), data);
  772. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  773. }
  774. std::pair<iterator,bool> insert_unique_check
  775. (const_iterator hint, const key_type& key, insert_commit_data &data)
  776. {
  777. BOOST_ASSERT((priv_is_linked)(hint));
  778. std::pair<iiterator, bool> ret =
  779. this->icont().insert_unique_check(hint.get(), key, KeyNodeCompare(key_comp()), data);
  780. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  781. }
  782. template<class MovableConvertible>
  783. iterator insert_unique_commit
  784. (BOOST_FWD_REF(MovableConvertible) v, insert_commit_data &data)
  785. {
  786. NodePtr tmp = AllocHolder::create_node(boost::forward<MovableConvertible>(v));
  787. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  788. iterator ret(this->icont().insert_unique_commit(*tmp, data));
  789. destroy_deallocator.release();
  790. return ret;
  791. }
  792. template<class MovableConvertible>
  793. std::pair<iterator,bool> insert_unique(BOOST_FWD_REF(MovableConvertible) v)
  794. {
  795. insert_commit_data data;
  796. std::pair<iterator,bool> ret =
  797. this->insert_unique_check(KeyOfValue()(v), data);
  798. if(ret.second){
  799. ret.first = this->insert_unique_commit(boost::forward<MovableConvertible>(v), data);
  800. }
  801. return ret;
  802. }
  803. private:
  804. template<class KeyConvertible, class M>
  805. iiterator priv_insert_or_assign_commit
  806. (BOOST_FWD_REF(KeyConvertible) key, BOOST_FWD_REF(M) obj, insert_commit_data &data)
  807. {
  808. NodePtr tmp = AllocHolder::create_node(boost::forward<KeyConvertible>(key), boost::forward<M>(obj));
  809. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  810. iiterator ret(this->icont().insert_unique_commit(*tmp, data));
  811. destroy_deallocator.release();
  812. return ret;
  813. }
  814. bool priv_is_linked(const_iterator const position) const
  815. {
  816. iiterator const cur(position.get());
  817. return cur == this->icont().end() ||
  818. cur == this->icont().root() ||
  819. iiterator(cur).go_parent().go_left() == cur ||
  820. iiterator(cur).go_parent().go_right() == cur;
  821. }
  822. template<class MovableConvertible>
  823. void push_back_impl(BOOST_FWD_REF(MovableConvertible) v)
  824. {
  825. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(v)));
  826. //push_back has no-throw guarantee so avoid any deallocator/destroyer
  827. this->icont().push_back(*tmp);
  828. }
  829. std::pair<iterator, bool> emplace_unique_impl(NodePtr p)
  830. {
  831. value_type &v = p->get_data();
  832. insert_commit_data data;
  833. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(p, this->node_alloc());
  834. std::pair<iterator,bool> ret =
  835. this->insert_unique_check(KeyOfValue()(v), data);
  836. if(!ret.second){
  837. return ret;
  838. }
  839. //No throw insertion part, release rollback
  840. destroy_deallocator.release();
  841. return std::pair<iterator,bool>
  842. ( iterator(this->icont().insert_unique_commit(*p, data))
  843. , true );
  844. }
  845. iterator emplace_unique_hint_impl(const_iterator hint, NodePtr p)
  846. {
  847. BOOST_ASSERT((priv_is_linked)(hint));
  848. value_type &v = p->get_data();
  849. insert_commit_data data;
  850. std::pair<iterator,bool> ret =
  851. this->insert_unique_check(hint, KeyOfValue()(v), data);
  852. if(!ret.second){
  853. Destroyer(this->node_alloc())(p);
  854. return ret.first;
  855. }
  856. return iterator(this->icont().insert_unique_commit(*p, data));
  857. }
  858. public:
  859. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  860. template <class... Args>
  861. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool> emplace_unique(BOOST_FWD_REF(Args)... args)
  862. { return this->emplace_unique_impl(AllocHolder::create_node(boost::forward<Args>(args)...)); }
  863. template <class... Args>
  864. BOOST_CONTAINER_FORCEINLINE iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
  865. { return this->emplace_unique_hint_impl(hint, AllocHolder::create_node(boost::forward<Args>(args)...)); }
  866. template <class... Args>
  867. iterator emplace_equal(BOOST_FWD_REF(Args)... args)
  868. {
  869. NodePtr tmp(AllocHolder::create_node(boost::forward<Args>(args)...));
  870. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  871. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  872. destroy_deallocator.release();
  873. return ret;
  874. }
  875. template <class... Args>
  876. iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
  877. {
  878. BOOST_ASSERT((priv_is_linked)(hint));
  879. NodePtr tmp(AllocHolder::create_node(boost::forward<Args>(args)...));
  880. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  881. iterator ret(this->icont().insert_equal(hint.get(), *tmp));
  882. destroy_deallocator.release();
  883. return ret;
  884. }
  885. template <class KeyType, class... Args>
  886. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool> try_emplace
  887. (const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(Args)... args)
  888. {
  889. insert_commit_data data;
  890. const key_type & k = key; //Support emulated rvalue references
  891. std::pair<iiterator, bool> ret =
  892. hint == const_iterator() ? this->icont().insert_unique_check( k, KeyNodeCompare(key_comp()), data)
  893. : this->icont().insert_unique_check(hint.get(), k, KeyNodeCompare(key_comp()), data);
  894. if(ret.second){
  895. ret.first = this->icont().insert_unique_commit
  896. (*AllocHolder::create_node(try_emplace_t(), boost::forward<KeyType>(key), boost::forward<Args>(args)...), data);
  897. }
  898. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  899. }
  900. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  901. #define BOOST_CONTAINER_TREE_EMPLACE_CODE(N) \
  902. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  903. std::pair<iterator, bool> emplace_unique(BOOST_MOVE_UREF##N)\
  904. { return this->emplace_unique_impl(AllocHolder::create_node(BOOST_MOVE_FWD##N)); }\
  905. \
  906. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  907. iterator emplace_hint_unique(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  908. { return this->emplace_unique_hint_impl(hint, AllocHolder::create_node(BOOST_MOVE_FWD##N)); }\
  909. \
  910. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  911. iterator emplace_equal(BOOST_MOVE_UREF##N)\
  912. {\
  913. NodePtr tmp(AllocHolder::create_node(BOOST_MOVE_FWD##N));\
  914. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());\
  915. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));\
  916. destroy_deallocator.release();\
  917. return ret;\
  918. }\
  919. \
  920. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  921. iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  922. {\
  923. BOOST_ASSERT((priv_is_linked)(hint));\
  924. NodePtr tmp(AllocHolder::create_node(BOOST_MOVE_FWD##N));\
  925. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());\
  926. iterator ret(this->icont().insert_equal(hint.get(), *tmp));\
  927. destroy_deallocator.release();\
  928. return ret;\
  929. }\
  930. \
  931. template <class KeyType BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  932. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool>\
  933. try_emplace(const_iterator hint, BOOST_FWD_REF(KeyType) key BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  934. {\
  935. insert_commit_data data;\
  936. const key_type & k = key;\
  937. std::pair<iiterator, bool> ret =\
  938. hint == const_iterator() ? this->icont().insert_unique_check( k, KeyNodeCompare(key_comp()), data)\
  939. : this->icont().insert_unique_check(hint.get(), k, KeyNodeCompare(key_comp()), data);\
  940. if(ret.second){\
  941. ret.first = this->icont().insert_unique_commit\
  942. (*AllocHolder::create_node(try_emplace_t(), boost::forward<KeyType>(key) BOOST_MOVE_I##N BOOST_MOVE_FWD##N), data);\
  943. }\
  944. return std::pair<iterator, bool>(iterator(ret.first), ret.second);\
  945. }\
  946. //
  947. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_TREE_EMPLACE_CODE)
  948. #undef BOOST_CONTAINER_TREE_EMPLACE_CODE
  949. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  950. template<class MovableConvertible>
  951. iterator insert_unique_convertible(const_iterator hint, BOOST_FWD_REF(MovableConvertible) v)
  952. {
  953. BOOST_ASSERT((priv_is_linked)(hint));
  954. insert_commit_data data;
  955. std::pair<iterator,bool> ret =
  956. this->insert_unique_check(hint, KeyOfValue()(v), data);
  957. if(!ret.second)
  958. return ret.first;
  959. return this->insert_unique_commit(boost::forward<MovableConvertible>(v), data);
  960. }
  961. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert_unique, value_type, iterator, this->insert_unique_convertible, const_iterator, const_iterator)
  962. template <class InputIterator>
  963. void insert_unique(InputIterator first, InputIterator last)
  964. {
  965. for( ; first != last; ++first)
  966. this->insert_unique(*first);
  967. }
  968. iterator insert_equal(const value_type& v)
  969. {
  970. NodePtr tmp(AllocHolder::create_node(v));
  971. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  972. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  973. destroy_deallocator.release();
  974. return ret;
  975. }
  976. template<class MovableConvertible>
  977. iterator insert_equal(BOOST_FWD_REF(MovableConvertible) v)
  978. {
  979. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(v)));
  980. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  981. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  982. destroy_deallocator.release();
  983. return ret;
  984. }
  985. template<class MovableConvertible>
  986. iterator insert_equal_convertible(const_iterator hint, BOOST_FWD_REF(MovableConvertible) v)
  987. {
  988. BOOST_ASSERT((priv_is_linked)(hint));
  989. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(v)));
  990. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  991. iterator ret(this->icont().insert_equal(hint.get(), *tmp));
  992. destroy_deallocator.release();
  993. return ret;
  994. }
  995. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert_equal, value_type, iterator, this->insert_equal_convertible, const_iterator, const_iterator)
  996. template <class InputIterator>
  997. void insert_equal(InputIterator first, InputIterator last)
  998. {
  999. for( ; first != last; ++first)
  1000. this->insert_equal(*first);
  1001. }
  1002. template<class KeyType, class M>
  1003. std::pair<iterator, bool> insert_or_assign(const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(M) obj)
  1004. {
  1005. insert_commit_data data;
  1006. const key_type & k = key; //Support emulated rvalue references
  1007. std::pair<iiterator, bool> ret =
  1008. hint == const_iterator() ? this->icont().insert_unique_check(k, KeyNodeCompare(key_comp()), data)
  1009. : this->icont().insert_unique_check(hint.get(), k, KeyNodeCompare(key_comp()), data);
  1010. if(ret.second){
  1011. ret.first = this->priv_insert_or_assign_commit(boost::forward<KeyType>(key), boost::forward<M>(obj), data);
  1012. }
  1013. else{
  1014. ret.first->get_data().second = boost::forward<M>(obj);
  1015. }
  1016. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  1017. }
  1018. iterator erase(const_iterator position)
  1019. {
  1020. BOOST_ASSERT(position != this->cend() && (priv_is_linked)(position));
  1021. return iterator(this->icont().erase_and_dispose(position.get(), Destroyer(this->node_alloc())));
  1022. }
  1023. BOOST_CONTAINER_FORCEINLINE size_type erase(const key_type& k)
  1024. { return AllocHolder::erase_key(k, KeyNodeCompare(key_comp()), alloc_version()); }
  1025. iterator erase(const_iterator first, const_iterator last)
  1026. {
  1027. BOOST_ASSERT(first == last || (first != this->cend() && (priv_is_linked)(first)));
  1028. BOOST_ASSERT(first == last || (priv_is_linked)(last));
  1029. return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version()));
  1030. }
  1031. node_type extract(const key_type& k)
  1032. {
  1033. iterator const it = this->find(k);
  1034. if(this->end() != it){
  1035. return this->extract(it);
  1036. }
  1037. return node_type();
  1038. }
  1039. node_type extract(const_iterator position)
  1040. {
  1041. BOOST_ASSERT(position != this->cend() && (priv_is_linked)(position));
  1042. iiterator const iit(position.get());
  1043. this->icont().erase(iit);
  1044. return node_type(iit.operator->(), this->node_alloc());
  1045. }
  1046. insert_return_type insert_unique_node(BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1047. {
  1048. return this->insert_unique_node(this->end(), boost::move(nh));
  1049. }
  1050. insert_return_type insert_unique_node(const_iterator hint, BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1051. {
  1052. insert_return_type irt; //inserted == false, node.empty()
  1053. if(!nh.empty()){
  1054. insert_commit_data data;
  1055. std::pair<iterator,bool> ret =
  1056. this->insert_unique_check(hint, KeyOfValue()(nh.value()), data);
  1057. if(ret.second){
  1058. irt.inserted = true;
  1059. irt.position = iterator(this->icont().insert_unique_commit(*nh.get(), data));
  1060. nh.release();
  1061. }
  1062. else{
  1063. irt.position = ret.first;
  1064. irt.node = boost::move(nh);
  1065. }
  1066. }
  1067. else{
  1068. irt.position = this->end();
  1069. }
  1070. return BOOST_MOVE_RET(insert_return_type, irt);
  1071. }
  1072. iterator insert_equal_node(BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1073. {
  1074. if(nh.empty()){
  1075. return this->end();
  1076. }
  1077. else{
  1078. NodePtr const p(nh.release());
  1079. return iterator(this->icont().insert_equal(*p));
  1080. }
  1081. }
  1082. iterator insert_equal_node(const_iterator hint, BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1083. {
  1084. if(nh.empty()){
  1085. return this->end();
  1086. }
  1087. else{
  1088. NodePtr const p(nh.release());
  1089. return iterator(this->icont().insert_equal(hint.get(), *p));
  1090. }
  1091. }
  1092. template<class C2>
  1093. BOOST_CONTAINER_FORCEINLINE void merge_unique(tree<T, KeyOfValue, C2, Allocator, Options>& source)
  1094. { return this->icont().merge_unique(source.icont()); }
  1095. template<class C2>
  1096. BOOST_CONTAINER_FORCEINLINE void merge_equal(tree<T, KeyOfValue, C2, Allocator, Options>& source)
  1097. { return this->icont().merge_equal(source.icont()); }
  1098. BOOST_CONTAINER_FORCEINLINE void clear()
  1099. { AllocHolder::clear(alloc_version()); }
  1100. // search operations. Const and non-const overloads even if no iterator is returned
  1101. // so splay implementations can to their rebalancing when searching in non-const versions
  1102. BOOST_CONTAINER_FORCEINLINE iterator find(const key_type& k)
  1103. { return iterator(this->icont().find(k, KeyNodeCompare(key_comp()))); }
  1104. BOOST_CONTAINER_FORCEINLINE const_iterator find(const key_type& k) const
  1105. { return const_iterator(this->non_const_icont().find(k, KeyNodeCompare(key_comp()))); }
  1106. template <class K>
  1107. BOOST_CONTAINER_FORCEINLINE
  1108. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1109. find(const K& k)
  1110. { return iterator(this->icont().find(k, KeyNodeCompare(key_comp()))); }
  1111. template <class K>
  1112. BOOST_CONTAINER_FORCEINLINE
  1113. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1114. find(const K& k) const
  1115. { return const_iterator(this->non_const_icont().find(k, KeyNodeCompare(key_comp()))); }
  1116. BOOST_CONTAINER_FORCEINLINE size_type count(const key_type& k) const
  1117. { return size_type(this->icont().count(k, KeyNodeCompare(key_comp()))); }
  1118. template <class K>
  1119. BOOST_CONTAINER_FORCEINLINE
  1120. typename dtl::enable_if_transparent<key_compare, K, size_type>::type
  1121. count(const K& k) const
  1122. { return size_type(this->icont().count(k, KeyNodeCompare(key_comp()))); }
  1123. BOOST_CONTAINER_FORCEINLINE iterator lower_bound(const key_type& k)
  1124. { return iterator(this->icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1125. BOOST_CONTAINER_FORCEINLINE const_iterator lower_bound(const key_type& k) const
  1126. { return const_iterator(this->non_const_icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1127. template <class K>
  1128. BOOST_CONTAINER_FORCEINLINE
  1129. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1130. lower_bound(const K& k)
  1131. { return iterator(this->icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1132. template <class K>
  1133. BOOST_CONTAINER_FORCEINLINE
  1134. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1135. lower_bound(const K& k) const
  1136. { return const_iterator(this->non_const_icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1137. BOOST_CONTAINER_FORCEINLINE iterator upper_bound(const key_type& k)
  1138. { return iterator(this->icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1139. BOOST_CONTAINER_FORCEINLINE const_iterator upper_bound(const key_type& k) const
  1140. { return const_iterator(this->non_const_icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1141. template <class K>
  1142. BOOST_CONTAINER_FORCEINLINE
  1143. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1144. upper_bound(const K& k)
  1145. { return iterator(this->icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1146. template <class K>
  1147. BOOST_CONTAINER_FORCEINLINE
  1148. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1149. upper_bound(const K& k) const
  1150. { return const_iterator(this->non_const_icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1151. std::pair<iterator,iterator> equal_range(const key_type& k)
  1152. {
  1153. std::pair<iiterator, iiterator> ret =
  1154. this->icont().equal_range(k, KeyNodeCompare(key_comp()));
  1155. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1156. }
  1157. std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
  1158. {
  1159. std::pair<iiterator, iiterator> ret =
  1160. this->non_const_icont().equal_range(k, KeyNodeCompare(key_comp()));
  1161. return std::pair<const_iterator,const_iterator>
  1162. (const_iterator(ret.first), const_iterator(ret.second));
  1163. }
  1164. template <class K>
  1165. BOOST_CONTAINER_FORCEINLINE
  1166. typename dtl::enable_if_transparent<key_compare, K, std::pair<iterator,iterator> >::type
  1167. equal_range(const K& k)
  1168. {
  1169. std::pair<iiterator, iiterator> ret =
  1170. this->icont().equal_range(k, KeyNodeCompare(key_comp()));
  1171. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1172. }
  1173. template <class K>
  1174. BOOST_CONTAINER_FORCEINLINE
  1175. typename dtl::enable_if_transparent<key_compare, K, std::pair<const_iterator, const_iterator> >::type
  1176. equal_range(const K& k) const
  1177. {
  1178. std::pair<iiterator, iiterator> ret =
  1179. this->non_const_icont().equal_range(k, KeyNodeCompare(key_comp()));
  1180. return std::pair<const_iterator,const_iterator>
  1181. (const_iterator(ret.first), const_iterator(ret.second));
  1182. }
  1183. std::pair<iterator,iterator> lower_bound_range(const key_type& k)
  1184. {
  1185. std::pair<iiterator, iiterator> ret =
  1186. this->icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1187. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1188. }
  1189. std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
  1190. {
  1191. std::pair<iiterator, iiterator> ret =
  1192. this->non_const_icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1193. return std::pair<const_iterator,const_iterator>
  1194. (const_iterator(ret.first), const_iterator(ret.second));
  1195. }
  1196. template <class K>
  1197. BOOST_CONTAINER_FORCEINLINE
  1198. typename dtl::enable_if_transparent<key_compare, K, std::pair<iterator,iterator> >::type
  1199. lower_bound_range(const K& k)
  1200. {
  1201. std::pair<iiterator, iiterator> ret =
  1202. this->icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1203. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1204. }
  1205. template <class K>
  1206. BOOST_CONTAINER_FORCEINLINE
  1207. typename dtl::enable_if_transparent<key_compare, K, std::pair<const_iterator, const_iterator> >::type
  1208. lower_bound_range(const K& k) const
  1209. {
  1210. std::pair<iiterator, iiterator> ret =
  1211. this->non_const_icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1212. return std::pair<const_iterator,const_iterator>
  1213. (const_iterator(ret.first), const_iterator(ret.second));
  1214. }
  1215. BOOST_CONTAINER_FORCEINLINE void rebalance()
  1216. { intrusive_tree_proxy_t::rebalance(this->icont()); }
  1217. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const tree& x, const tree& y)
  1218. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1219. BOOST_CONTAINER_FORCEINLINE friend bool operator<(const tree& x, const tree& y)
  1220. { return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1221. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const tree& x, const tree& y)
  1222. { return !(x == y); }
  1223. BOOST_CONTAINER_FORCEINLINE friend bool operator>(const tree& x, const tree& y)
  1224. { return y < x; }
  1225. BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const tree& x, const tree& y)
  1226. { return !(y < x); }
  1227. BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const tree& x, const tree& y)
  1228. { return !(x < y); }
  1229. BOOST_CONTAINER_FORCEINLINE friend void swap(tree& x, tree& y)
  1230. { x.swap(y); }
  1231. };
  1232. } //namespace dtl {
  1233. } //namespace container {
  1234. template <class T>
  1235. struct has_trivial_destructor_after_move;
  1236. //!has_trivial_destructor_after_move<> == true_type
  1237. //!specialization for optimizations
  1238. template <class T, class KeyOfValue, class Compare, class Allocator, class Options>
  1239. struct has_trivial_destructor_after_move
  1240. <
  1241. ::boost::container::dtl::tree
  1242. <T, KeyOfValue, Compare, Allocator, Options>
  1243. >
  1244. {
  1245. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  1246. static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
  1247. ::boost::has_trivial_destructor_after_move<pointer>::value &&
  1248. ::boost::has_trivial_destructor_after_move<Compare>::value;
  1249. };
  1250. } //namespace boost {
  1251. #include <boost/container/detail/config_end.hpp>
  1252. #endif //BOOST_CONTAINER_TREE_HPP