tree.hpp 54 KB

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