tree.hpp 56 KB

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