tree.hpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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 (&x != this){
  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. BOOST_ASSERT(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. return *this;
  739. }
  740. public:
  741. // accessors:
  742. BOOST_CONTAINER_FORCEINLINE value_compare value_comp() const
  743. { return this->icont().value_comp().predicate(); }
  744. BOOST_CONTAINER_FORCEINLINE key_compare key_comp() const
  745. { return this->icont().value_comp().predicate().key_comp(); }
  746. BOOST_CONTAINER_FORCEINLINE allocator_type get_allocator() const
  747. { return allocator_type(this->node_alloc()); }
  748. BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const
  749. { return this->node_alloc(); }
  750. BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator()
  751. { return this->node_alloc(); }
  752. BOOST_CONTAINER_FORCEINLINE iterator begin()
  753. { return iterator(this->icont().begin()); }
  754. BOOST_CONTAINER_FORCEINLINE const_iterator begin() const
  755. { return this->cbegin(); }
  756. BOOST_CONTAINER_FORCEINLINE iterator end()
  757. { return iterator(this->icont().end()); }
  758. BOOST_CONTAINER_FORCEINLINE const_iterator end() const
  759. { return this->cend(); }
  760. BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin()
  761. { return reverse_iterator(end()); }
  762. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const
  763. { return this->crbegin(); }
  764. BOOST_CONTAINER_FORCEINLINE reverse_iterator rend()
  765. { return reverse_iterator(begin()); }
  766. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const
  767. { return this->crend(); }
  768. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the container.
  769. //!
  770. //! <b>Throws</b>: Nothing.
  771. //!
  772. //! <b>Complexity</b>: Constant.
  773. BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const
  774. { return const_iterator(this->non_const_icont().begin()); }
  775. //! <b>Effects</b>: Returns a const_iterator to the end of the container.
  776. //!
  777. //! <b>Throws</b>: Nothing.
  778. //!
  779. //! <b>Complexity</b>: Constant.
  780. BOOST_CONTAINER_FORCEINLINE const_iterator cend() const
  781. { return const_iterator(this->non_const_icont().end()); }
  782. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  783. //! of the reversed container.
  784. //!
  785. //! <b>Throws</b>: Nothing.
  786. //!
  787. //! <b>Complexity</b>: Constant.
  788. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const
  789. { return const_reverse_iterator(cend()); }
  790. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  791. //! of the reversed container.
  792. //!
  793. //! <b>Throws</b>: Nothing.
  794. //!
  795. //! <b>Complexity</b>: Constant.
  796. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const
  797. { return const_reverse_iterator(cbegin()); }
  798. BOOST_CONTAINER_FORCEINLINE bool empty() const
  799. { return !this->size(); }
  800. BOOST_CONTAINER_FORCEINLINE size_type size() const
  801. { return this->icont().size(); }
  802. BOOST_CONTAINER_FORCEINLINE size_type max_size() const
  803. { return AllocHolder::max_size(); }
  804. BOOST_CONTAINER_FORCEINLINE void swap(ThisType& x)
  805. BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
  806. && boost::container::dtl::is_nothrow_swappable<Compare>::value )
  807. { AllocHolder::swap(x); }
  808. public:
  809. typedef typename Icont::insert_commit_data insert_commit_data;
  810. // insert/erase
  811. std::pair<iterator,bool> insert_unique_check
  812. (const key_type& key, insert_commit_data &data)
  813. {
  814. std::pair<iiterator, bool> ret =
  815. this->icont().insert_unique_check(key, KeyNodeCompare(key_comp()), data);
  816. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  817. }
  818. std::pair<iterator,bool> insert_unique_check
  819. (const_iterator hint, const key_type& key, insert_commit_data &data)
  820. {
  821. BOOST_ASSERT((priv_is_linked)(hint));
  822. std::pair<iiterator, bool> ret =
  823. this->icont().insert_unique_check(hint.get(), key, KeyNodeCompare(key_comp()), data);
  824. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  825. }
  826. template<class MovableConvertible>
  827. iterator insert_unique_commit
  828. (BOOST_FWD_REF(MovableConvertible) v, insert_commit_data &data)
  829. {
  830. NodePtr tmp = AllocHolder::create_node(boost::forward<MovableConvertible>(v));
  831. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  832. iterator ret(this->icont().insert_unique_commit(*tmp, data));
  833. destroy_deallocator.release();
  834. return ret;
  835. }
  836. template<class MovableConvertible>
  837. std::pair<iterator,bool> insert_unique(BOOST_FWD_REF(MovableConvertible) v)
  838. {
  839. insert_commit_data data;
  840. std::pair<iterator,bool> ret =
  841. this->insert_unique_check(key_of_value_t()(v), data);
  842. if(ret.second){
  843. ret.first = this->insert_unique_commit(boost::forward<MovableConvertible>(v), data);
  844. }
  845. return ret;
  846. }
  847. private:
  848. template<class KeyConvertible, class M>
  849. iiterator priv_insert_or_assign_commit
  850. (BOOST_FWD_REF(KeyConvertible) key, BOOST_FWD_REF(M) obj, insert_commit_data &data)
  851. {
  852. NodePtr tmp = AllocHolder::create_node(boost::forward<KeyConvertible>(key), boost::forward<M>(obj));
  853. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  854. iiterator ret(this->icont().insert_unique_commit(*tmp, data));
  855. destroy_deallocator.release();
  856. return ret;
  857. }
  858. bool priv_is_linked(const_iterator const position) const
  859. {
  860. iiterator const cur(position.get());
  861. return cur == this->icont().end() ||
  862. cur == this->icont().root() ||
  863. iiterator(cur).go_parent().go_left() == cur ||
  864. iiterator(cur).go_parent().go_right() == cur;
  865. }
  866. template<class MovableConvertible>
  867. void push_back_impl(BOOST_FWD_REF(MovableConvertible) v)
  868. {
  869. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(v)));
  870. //push_back has no-throw guarantee so avoid any deallocator/destroyer
  871. this->icont().push_back(*tmp);
  872. }
  873. std::pair<iterator, bool> emplace_unique_impl(NodePtr p)
  874. {
  875. value_type &v = p->get_data();
  876. insert_commit_data data;
  877. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(p, this->node_alloc());
  878. std::pair<iterator,bool> ret =
  879. this->insert_unique_check(key_of_value_t()(v), data);
  880. if(!ret.second){
  881. return ret;
  882. }
  883. //No throw insertion part, release rollback
  884. destroy_deallocator.release();
  885. return std::pair<iterator,bool>
  886. ( iterator(this->icont().insert_unique_commit(*p, data))
  887. , true );
  888. }
  889. iterator emplace_unique_hint_impl(const_iterator hint, NodePtr p)
  890. {
  891. BOOST_ASSERT((priv_is_linked)(hint));
  892. value_type &v = p->get_data();
  893. insert_commit_data data;
  894. std::pair<iterator,bool> ret =
  895. this->insert_unique_check(hint, key_of_value_t()(v), data);
  896. if(!ret.second){
  897. Destroyer(this->node_alloc())(p);
  898. return ret.first;
  899. }
  900. return iterator(this->icont().insert_unique_commit(*p, data));
  901. }
  902. public:
  903. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  904. template <class... Args>
  905. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool> emplace_unique(BOOST_FWD_REF(Args)... args)
  906. { return this->emplace_unique_impl(AllocHolder::create_node(boost::forward<Args>(args)...)); }
  907. template <class... Args>
  908. BOOST_CONTAINER_FORCEINLINE iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
  909. { return this->emplace_unique_hint_impl(hint, AllocHolder::create_node(boost::forward<Args>(args)...)); }
  910. template <class... Args>
  911. iterator emplace_equal(BOOST_FWD_REF(Args)... args)
  912. {
  913. NodePtr tmp(AllocHolder::create_node(boost::forward<Args>(args)...));
  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. template <class... Args>
  920. iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
  921. {
  922. BOOST_ASSERT((priv_is_linked)(hint));
  923. NodePtr tmp(AllocHolder::create_node(boost::forward<Args>(args)...));
  924. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  925. iterator ret(this->icont().insert_equal(hint.get(), *tmp));
  926. destroy_deallocator.release();
  927. return ret;
  928. }
  929. template <class KeyType, class... Args>
  930. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool> try_emplace
  931. (const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(Args)... args)
  932. {
  933. insert_commit_data data;
  934. const key_type & k = key; //Support emulated rvalue references
  935. std::pair<iiterator, bool> ret =
  936. hint == const_iterator() ? this->icont().insert_unique_check( k, KeyNodeCompare(key_comp()), data)
  937. : this->icont().insert_unique_check(hint.get(), k, KeyNodeCompare(key_comp()), data);
  938. if(ret.second){
  939. ret.first = this->icont().insert_unique_commit
  940. (*AllocHolder::create_node(try_emplace_t(), boost::forward<KeyType>(key), boost::forward<Args>(args)...), data);
  941. }
  942. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  943. }
  944. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  945. #define BOOST_CONTAINER_TREE_EMPLACE_CODE(N) \
  946. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  947. std::pair<iterator, bool> emplace_unique(BOOST_MOVE_UREF##N)\
  948. { return this->emplace_unique_impl(AllocHolder::create_node(BOOST_MOVE_FWD##N)); }\
  949. \
  950. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  951. iterator emplace_hint_unique(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  952. { return this->emplace_unique_hint_impl(hint, AllocHolder::create_node(BOOST_MOVE_FWD##N)); }\
  953. \
  954. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  955. iterator emplace_equal(BOOST_MOVE_UREF##N)\
  956. {\
  957. NodePtr tmp(AllocHolder::create_node(BOOST_MOVE_FWD##N));\
  958. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());\
  959. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));\
  960. destroy_deallocator.release();\
  961. return ret;\
  962. }\
  963. \
  964. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  965. iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  966. {\
  967. BOOST_ASSERT((priv_is_linked)(hint));\
  968. NodePtr tmp(AllocHolder::create_node(BOOST_MOVE_FWD##N));\
  969. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());\
  970. iterator ret(this->icont().insert_equal(hint.get(), *tmp));\
  971. destroy_deallocator.release();\
  972. return ret;\
  973. }\
  974. \
  975. template <class KeyType BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  976. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool>\
  977. try_emplace(const_iterator hint, BOOST_FWD_REF(KeyType) key BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  978. {\
  979. insert_commit_data data;\
  980. const key_type & k = key;\
  981. std::pair<iiterator, bool> ret =\
  982. hint == const_iterator() ? this->icont().insert_unique_check( k, KeyNodeCompare(key_comp()), data)\
  983. : this->icont().insert_unique_check(hint.get(), k, KeyNodeCompare(key_comp()), data);\
  984. if(ret.second){\
  985. ret.first = this->icont().insert_unique_commit\
  986. (*AllocHolder::create_node(try_emplace_t(), boost::forward<KeyType>(key) BOOST_MOVE_I##N BOOST_MOVE_FWD##N), data);\
  987. }\
  988. return std::pair<iterator, bool>(iterator(ret.first), ret.second);\
  989. }\
  990. //
  991. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_TREE_EMPLACE_CODE)
  992. #undef BOOST_CONTAINER_TREE_EMPLACE_CODE
  993. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  994. template<class MovableConvertible>
  995. iterator insert_unique_convertible(const_iterator hint, BOOST_FWD_REF(MovableConvertible) v)
  996. {
  997. BOOST_ASSERT((priv_is_linked)(hint));
  998. insert_commit_data data;
  999. std::pair<iterator,bool> ret =
  1000. this->insert_unique_check(hint, key_of_value_t()(v), data);
  1001. if(!ret.second)
  1002. return ret.first;
  1003. return this->insert_unique_commit(boost::forward<MovableConvertible>(v), data);
  1004. }
  1005. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert_unique, value_type, iterator, this->insert_unique_convertible, const_iterator, const_iterator)
  1006. template <class InputIterator>
  1007. void insert_unique(InputIterator first, InputIterator last)
  1008. {
  1009. for( ; first != last; ++first)
  1010. this->insert_unique(*first);
  1011. }
  1012. iterator insert_equal(const value_type& v)
  1013. {
  1014. NodePtr tmp(AllocHolder::create_node(v));
  1015. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  1016. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  1017. destroy_deallocator.release();
  1018. return ret;
  1019. }
  1020. template<class MovableConvertible>
  1021. iterator insert_equal(BOOST_FWD_REF(MovableConvertible) v)
  1022. {
  1023. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(v)));
  1024. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  1025. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  1026. destroy_deallocator.release();
  1027. return ret;
  1028. }
  1029. template<class MovableConvertible>
  1030. iterator insert_equal_convertible(const_iterator hint, BOOST_FWD_REF(MovableConvertible) v)
  1031. {
  1032. BOOST_ASSERT((priv_is_linked)(hint));
  1033. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(v)));
  1034. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  1035. iterator ret(this->icont().insert_equal(hint.get(), *tmp));
  1036. destroy_deallocator.release();
  1037. return ret;
  1038. }
  1039. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert_equal, value_type, iterator, this->insert_equal_convertible, const_iterator, const_iterator)
  1040. template <class InputIterator>
  1041. void insert_equal(InputIterator first, InputIterator last)
  1042. {
  1043. for( ; first != last; ++first)
  1044. this->insert_equal(*first);
  1045. }
  1046. template<class KeyType, class M>
  1047. std::pair<iterator, bool> insert_or_assign(const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(M) obj)
  1048. {
  1049. insert_commit_data data;
  1050. const key_type & k = key; //Support emulated rvalue references
  1051. std::pair<iiterator, bool> ret =
  1052. hint == const_iterator() ? this->icont().insert_unique_check(k, KeyNodeCompare(key_comp()), data)
  1053. : this->icont().insert_unique_check(hint.get(), k, KeyNodeCompare(key_comp()), data);
  1054. if(ret.second){
  1055. ret.first = this->priv_insert_or_assign_commit(boost::forward<KeyType>(key), boost::forward<M>(obj), data);
  1056. }
  1057. else{
  1058. ret.first->get_data().second = boost::forward<M>(obj);
  1059. }
  1060. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  1061. }
  1062. iterator erase(const_iterator position)
  1063. {
  1064. BOOST_ASSERT(position != this->cend() && (priv_is_linked)(position));
  1065. return iterator(this->icont().erase_and_dispose(position.get(), Destroyer(this->node_alloc())));
  1066. }
  1067. BOOST_CONTAINER_FORCEINLINE size_type erase(const key_type& k)
  1068. { return AllocHolder::erase_key(k, KeyNodeCompare(key_comp()), alloc_version()); }
  1069. iterator erase(const_iterator first, const_iterator last)
  1070. {
  1071. BOOST_ASSERT(first == last || (first != this->cend() && (priv_is_linked)(first)));
  1072. BOOST_ASSERT(first == last || (priv_is_linked)(last));
  1073. return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version()));
  1074. }
  1075. node_type extract(const key_type& k)
  1076. {
  1077. iterator const it = this->find(k);
  1078. if(this->end() != it){
  1079. return this->extract(it);
  1080. }
  1081. return node_type();
  1082. }
  1083. node_type extract(const_iterator position)
  1084. {
  1085. BOOST_ASSERT(position != this->cend() && (priv_is_linked)(position));
  1086. iiterator const iit(position.get());
  1087. this->icont().erase(iit);
  1088. return node_type(iit.operator->(), this->node_alloc());
  1089. }
  1090. insert_return_type insert_unique_node(BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1091. {
  1092. return this->insert_unique_node(this->end(), boost::move(nh));
  1093. }
  1094. insert_return_type insert_unique_node(const_iterator hint, BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1095. {
  1096. insert_return_type irt; //inserted == false, node.empty()
  1097. if(!nh.empty()){
  1098. insert_commit_data data;
  1099. std::pair<iterator,bool> ret =
  1100. this->insert_unique_check(hint, key_of_value_t()(nh.value()), data);
  1101. if(ret.second){
  1102. irt.inserted = true;
  1103. irt.position = iterator(this->icont().insert_unique_commit(*nh.get(), data));
  1104. nh.release();
  1105. }
  1106. else{
  1107. irt.position = ret.first;
  1108. irt.node = boost::move(nh);
  1109. }
  1110. }
  1111. else{
  1112. irt.position = this->end();
  1113. }
  1114. return BOOST_MOVE_RET(insert_return_type, irt);
  1115. }
  1116. iterator insert_equal_node(BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1117. {
  1118. if(nh.empty()){
  1119. return this->end();
  1120. }
  1121. else{
  1122. NodePtr const p(nh.release());
  1123. return iterator(this->icont().insert_equal(*p));
  1124. }
  1125. }
  1126. iterator insert_equal_node(const_iterator hint, BOOST_RV_REF_BEG_IF_CXX11 node_type BOOST_RV_REF_END_IF_CXX11 nh)
  1127. {
  1128. if(nh.empty()){
  1129. return this->end();
  1130. }
  1131. else{
  1132. NodePtr const p(nh.release());
  1133. return iterator(this->icont().insert_equal(hint.get(), *p));
  1134. }
  1135. }
  1136. template<class C2>
  1137. BOOST_CONTAINER_FORCEINLINE void merge_unique(tree<T, KeyOfValue, C2, Allocator, Options>& source)
  1138. { return this->icont().merge_unique(source.icont()); }
  1139. template<class C2>
  1140. BOOST_CONTAINER_FORCEINLINE void merge_equal(tree<T, KeyOfValue, C2, Allocator, Options>& source)
  1141. { return this->icont().merge_equal(source.icont()); }
  1142. BOOST_CONTAINER_FORCEINLINE void clear()
  1143. { AllocHolder::clear(alloc_version()); }
  1144. // search operations. Const and non-const overloads even if no iterator is returned
  1145. // so splay implementations can to their rebalancing when searching in non-const versions
  1146. BOOST_CONTAINER_FORCEINLINE iterator find(const key_type& k)
  1147. { return iterator(this->icont().find(k, KeyNodeCompare(key_comp()))); }
  1148. BOOST_CONTAINER_FORCEINLINE const_iterator find(const key_type& k) const
  1149. { return const_iterator(this->non_const_icont().find(k, KeyNodeCompare(key_comp()))); }
  1150. template <class K>
  1151. BOOST_CONTAINER_FORCEINLINE
  1152. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1153. find(const K& k)
  1154. { return iterator(this->icont().find(k, KeyNodeCompare(key_comp()))); }
  1155. template <class K>
  1156. BOOST_CONTAINER_FORCEINLINE
  1157. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1158. find(const K& k) const
  1159. { return const_iterator(this->non_const_icont().find(k, KeyNodeCompare(key_comp()))); }
  1160. BOOST_CONTAINER_FORCEINLINE size_type count(const key_type& k) const
  1161. { return size_type(this->icont().count(k, KeyNodeCompare(key_comp()))); }
  1162. template <class K>
  1163. BOOST_CONTAINER_FORCEINLINE
  1164. typename dtl::enable_if_transparent<key_compare, K, size_type>::type
  1165. count(const K& k) const
  1166. { return size_type(this->icont().count(k, KeyNodeCompare(key_comp()))); }
  1167. BOOST_CONTAINER_FORCEINLINE bool contains(const key_type& x) const
  1168. { return this->find(x) != this->cend(); }
  1169. template<typename K>
  1170. BOOST_CONTAINER_FORCEINLINE
  1171. typename dtl::enable_if_transparent<key_compare, K, bool>::type
  1172. contains(const K& x) const
  1173. { return this->find(x) != this->cend(); }
  1174. BOOST_CONTAINER_FORCEINLINE iterator lower_bound(const key_type& k)
  1175. { return iterator(this->icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1176. BOOST_CONTAINER_FORCEINLINE const_iterator lower_bound(const key_type& k) const
  1177. { return const_iterator(this->non_const_icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1178. template <class K>
  1179. BOOST_CONTAINER_FORCEINLINE
  1180. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1181. lower_bound(const K& k)
  1182. { return iterator(this->icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1183. template <class K>
  1184. BOOST_CONTAINER_FORCEINLINE
  1185. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1186. lower_bound(const K& k) const
  1187. { return const_iterator(this->non_const_icont().lower_bound(k, KeyNodeCompare(key_comp()))); }
  1188. BOOST_CONTAINER_FORCEINLINE iterator upper_bound(const key_type& k)
  1189. { return iterator(this->icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1190. BOOST_CONTAINER_FORCEINLINE const_iterator upper_bound(const key_type& k) const
  1191. { return const_iterator(this->non_const_icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1192. template <class K>
  1193. BOOST_CONTAINER_FORCEINLINE
  1194. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1195. upper_bound(const K& k)
  1196. { return iterator(this->icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1197. template <class K>
  1198. BOOST_CONTAINER_FORCEINLINE
  1199. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1200. upper_bound(const K& k) const
  1201. { return const_iterator(this->non_const_icont().upper_bound(k, KeyNodeCompare(key_comp()))); }
  1202. std::pair<iterator,iterator> equal_range(const key_type& k)
  1203. {
  1204. std::pair<iiterator, iiterator> ret =
  1205. this->icont().equal_range(k, KeyNodeCompare(key_comp()));
  1206. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1207. }
  1208. std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
  1209. {
  1210. std::pair<iiterator, iiterator> ret =
  1211. this->non_const_icont().equal_range(k, KeyNodeCompare(key_comp()));
  1212. return std::pair<const_iterator,const_iterator>
  1213. (const_iterator(ret.first), const_iterator(ret.second));
  1214. }
  1215. template <class K>
  1216. BOOST_CONTAINER_FORCEINLINE
  1217. typename dtl::enable_if_transparent<key_compare, K, std::pair<iterator,iterator> >::type
  1218. equal_range(const K& k)
  1219. {
  1220. std::pair<iiterator, iiterator> ret =
  1221. this->icont().equal_range(k, KeyNodeCompare(key_comp()));
  1222. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1223. }
  1224. template <class K>
  1225. BOOST_CONTAINER_FORCEINLINE
  1226. typename dtl::enable_if_transparent<key_compare, K, std::pair<const_iterator, const_iterator> >::type
  1227. equal_range(const K& k) const
  1228. {
  1229. std::pair<iiterator, iiterator> ret =
  1230. this->non_const_icont().equal_range(k, KeyNodeCompare(key_comp()));
  1231. return std::pair<const_iterator,const_iterator>
  1232. (const_iterator(ret.first), const_iterator(ret.second));
  1233. }
  1234. std::pair<iterator,iterator> lower_bound_range(const key_type& k)
  1235. {
  1236. std::pair<iiterator, iiterator> ret =
  1237. this->icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1238. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1239. }
  1240. std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
  1241. {
  1242. std::pair<iiterator, iiterator> ret =
  1243. this->non_const_icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1244. return std::pair<const_iterator,const_iterator>
  1245. (const_iterator(ret.first), const_iterator(ret.second));
  1246. }
  1247. template <class K>
  1248. BOOST_CONTAINER_FORCEINLINE
  1249. typename dtl::enable_if_transparent<key_compare, K, std::pair<iterator,iterator> >::type
  1250. lower_bound_range(const K& k)
  1251. {
  1252. std::pair<iiterator, iiterator> ret =
  1253. this->icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1254. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  1255. }
  1256. template <class K>
  1257. BOOST_CONTAINER_FORCEINLINE
  1258. typename dtl::enable_if_transparent<key_compare, K, std::pair<const_iterator, const_iterator> >::type
  1259. lower_bound_range(const K& k) const
  1260. {
  1261. std::pair<iiterator, iiterator> ret =
  1262. this->non_const_icont().lower_bound_range(k, KeyNodeCompare(key_comp()));
  1263. return std::pair<const_iterator,const_iterator>
  1264. (const_iterator(ret.first), const_iterator(ret.second));
  1265. }
  1266. BOOST_CONTAINER_FORCEINLINE void rebalance()
  1267. { intrusive_tree_proxy_t::rebalance(this->icont()); }
  1268. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const tree& x, const tree& y)
  1269. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1270. BOOST_CONTAINER_FORCEINLINE friend bool operator<(const tree& x, const tree& y)
  1271. { return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1272. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const tree& x, const tree& y)
  1273. { return !(x == y); }
  1274. BOOST_CONTAINER_FORCEINLINE friend bool operator>(const tree& x, const tree& y)
  1275. { return y < x; }
  1276. BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const tree& x, const tree& y)
  1277. { return !(y < x); }
  1278. BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const tree& x, const tree& y)
  1279. { return !(x < y); }
  1280. BOOST_CONTAINER_FORCEINLINE friend void swap(tree& x, tree& y)
  1281. BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
  1282. && boost::container::dtl::is_nothrow_swappable<Compare>::value )
  1283. { x.swap(y); }
  1284. };
  1285. } //namespace dtl {
  1286. } //namespace container {
  1287. template <class T>
  1288. struct has_trivial_destructor_after_move;
  1289. //!has_trivial_destructor_after_move<> == true_type
  1290. //!specialization for optimizations
  1291. template <class T, class KeyOfValue, class Compare, class Allocator, class Options>
  1292. struct has_trivial_destructor_after_move
  1293. <
  1294. ::boost::container::dtl::tree
  1295. <T, KeyOfValue, Compare, Allocator, Options>
  1296. >
  1297. {
  1298. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  1299. static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
  1300. ::boost::has_trivial_destructor_after_move<pointer>::value &&
  1301. ::boost::has_trivial_destructor_after_move<Compare>::value;
  1302. };
  1303. } //namespace boost {
  1304. #include <boost/container/detail/config_end.hpp>
  1305. #endif //BOOST_CONTAINER_TREE_HPP