tree.hpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. 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. #include "config_begin.hpp"
  13. #include <boost/container/detail/workaround.hpp>
  14. #include <boost/container/container_fwd.hpp>
  15. #include <boost/move/utility.hpp>
  16. #include <boost/intrusive/pointer_traits.hpp>
  17. #include <boost/type_traits/has_trivial_destructor.hpp>
  18. #include <boost/detail/no_exceptions_support.hpp>
  19. #include <boost/intrusive/rbtree.hpp>
  20. #include <boost/container/detail/utilities.hpp>
  21. #include <boost/container/detail/iterators.hpp>
  22. #include <boost/container/detail/algorithms.hpp>
  23. #include <boost/container/detail/node_alloc_holder.hpp>
  24. #include <boost/container/detail/destroyers.hpp>
  25. #include <boost/container/detail/pair.hpp>
  26. #include <boost/container/detail/type_traits.hpp>
  27. #include <boost/container/allocator_traits.hpp>
  28. #include <boost/detail/no_exceptions_support.hpp>
  29. #ifndef BOOST_CONTAINER_PERFECT_FORWARDING
  30. #include <boost/container/detail/preprocessor.hpp>
  31. #endif
  32. #include <utility> //std::pair
  33. #include <iterator>
  34. #include <algorithm>
  35. namespace boost {
  36. namespace container {
  37. namespace container_detail {
  38. template<class Key, class Value, class KeyCompare, class KeyOfValue>
  39. struct tree_value_compare
  40. : public KeyCompare
  41. {
  42. typedef Value value_type;
  43. typedef KeyCompare key_compare;
  44. typedef KeyOfValue key_of_value;
  45. typedef Key key_type;
  46. explicit tree_value_compare(const key_compare &kcomp)
  47. : KeyCompare(kcomp)
  48. {}
  49. tree_value_compare()
  50. : KeyCompare()
  51. {}
  52. const key_compare &key_comp() const
  53. { return static_cast<const key_compare &>(*this); }
  54. key_compare &key_comp()
  55. { return static_cast<key_compare &>(*this); }
  56. template<class T>
  57. struct is_key
  58. {
  59. static const bool value = is_same<const T, const key_type>::value;
  60. };
  61. template<class T>
  62. typename enable_if_c<is_key<T>::value, const key_type &>::type
  63. key_forward(const T &key) const
  64. { return key; }
  65. template<class T>
  66. typename enable_if_c<!is_key<T>::value, const key_type &>::type
  67. key_forward(const T &key) const
  68. { return KeyOfValue()(key); }
  69. template<class KeyType, class KeyType2>
  70. bool operator()(const KeyType &key1, const KeyType2 &key2) const
  71. { return key_compare::operator()(this->key_forward(key1), this->key_forward(key2)); }
  72. };
  73. template<class VoidPointer>
  74. struct rbtree_hook
  75. {
  76. typedef typename container_detail::bi::make_set_base_hook
  77. < container_detail::bi::void_pointer<VoidPointer>
  78. , container_detail::bi::link_mode<container_detail::bi::normal_link>
  79. , container_detail::bi::optimize_size<true>
  80. >::type type;
  81. };
  82. //This trait is used to type-pun std::pair because in C++03
  83. //compilers std::pair is useless for C++11 features
  84. template<class T>
  85. struct rbtree_internal_data_type
  86. {
  87. typedef T type;
  88. };
  89. template<class T1, class T2>
  90. struct rbtree_internal_data_type< std::pair<T1, T2> >
  91. {
  92. typedef pair<T1, T2> type;
  93. };
  94. //The node to be store in the tree
  95. template <class T, class VoidPointer>
  96. struct rbtree_node
  97. : public rbtree_hook<VoidPointer>::type
  98. {
  99. private:
  100. //BOOST_COPYABLE_AND_MOVABLE(rbtree_node)
  101. rbtree_node();
  102. public:
  103. typedef typename rbtree_hook<VoidPointer>::type hook_type;
  104. typedef T value_type;
  105. typedef typename rbtree_internal_data_type<T>::type internal_type;
  106. typedef rbtree_node<T, VoidPointer> node_type;
  107. T &get_data()
  108. {
  109. T* ptr = reinterpret_cast<T*>(&this->m_data);
  110. return *ptr;
  111. }
  112. const T &get_data() const
  113. {
  114. const T* ptr = reinterpret_cast<const T*>(&this->m_data);
  115. return *ptr;
  116. }
  117. internal_type m_data;
  118. template<class A, class B>
  119. void do_assign(const std::pair<const A, B> &p)
  120. {
  121. const_cast<A&>(m_data.first) = p.first;
  122. m_data.second = p.second;
  123. }
  124. template<class A, class B>
  125. void do_assign(const pair<const A, B> &p)
  126. {
  127. const_cast<A&>(m_data.first) = p.first;
  128. m_data.second = p.second;
  129. }
  130. template<class V>
  131. void do_assign(const V &v)
  132. { m_data = v; }
  133. template<class A, class B>
  134. void do_move_assign(std::pair<const A, B> &p)
  135. {
  136. const_cast<A&>(m_data.first) = ::boost::move(p.first);
  137. m_data.second = ::boost::move(p.second);
  138. }
  139. template<class A, class B>
  140. void do_move_assign(pair<const A, B> &p)
  141. {
  142. const_cast<A&>(m_data.first) = ::boost::move(p.first);
  143. m_data.second = ::boost::move(p.second);
  144. }
  145. template<class V>
  146. void do_move_assign(V &v)
  147. { m_data = ::boost::move(v); }
  148. };
  149. template<class Node, class Icont>
  150. class insert_equal_end_hint_functor
  151. {
  152. Icont &icont_;
  153. public:
  154. insert_equal_end_hint_functor(Icont &icont)
  155. : icont_(icont)
  156. {}
  157. void operator()(Node &n)
  158. { this->icont_.insert_equal(this->icont_.cend(), n); }
  159. };
  160. template<class Node, class Icont>
  161. class push_back_functor
  162. {
  163. Icont &icont_;
  164. public:
  165. push_back_functor(Icont &icont)
  166. : icont_(icont)
  167. {}
  168. void operator()(Node &n)
  169. { this->icont_.push_back(n); }
  170. };
  171. }//namespace container_detail {
  172. namespace container_detail {
  173. template<class A, class ValueCompare>
  174. struct intrusive_rbtree_type
  175. {
  176. typedef typename boost::container::
  177. allocator_traits<A>::value_type value_type;
  178. typedef typename boost::container::
  179. allocator_traits<A>::void_pointer void_pointer;
  180. typedef typename boost::container::
  181. allocator_traits<A>::size_type size_type;
  182. typedef typename container_detail::rbtree_node
  183. <value_type, void_pointer> node_type;
  184. typedef node_compare<ValueCompare, node_type> node_compare_type;
  185. typedef typename container_detail::bi::make_rbtree
  186. <node_type
  187. ,container_detail::bi::compare<node_compare_type>
  188. ,container_detail::bi::base_hook<typename rbtree_hook<void_pointer>::type>
  189. ,container_detail::bi::constant_time_size<true>
  190. ,container_detail::bi::size_type<size_type>
  191. >::type container_type;
  192. typedef container_type type ;
  193. };
  194. } //namespace container_detail {
  195. namespace container_detail {
  196. template <class Key, class Value, class KeyOfValue,
  197. class KeyCompare, class A>
  198. class rbtree
  199. : protected container_detail::node_alloc_holder
  200. < A
  201. , typename container_detail::intrusive_rbtree_type
  202. <A, tree_value_compare<Key, Value, KeyCompare, KeyOfValue>
  203. >::type
  204. , tree_value_compare<Key, Value, KeyCompare, KeyOfValue>
  205. >
  206. {
  207. typedef tree_value_compare
  208. <Key, Value, KeyCompare, KeyOfValue> ValComp;
  209. typedef typename container_detail::intrusive_rbtree_type
  210. < A, ValComp>::type Icont;
  211. typedef container_detail::node_alloc_holder
  212. <A, Icont, ValComp> AllocHolder;
  213. typedef typename AllocHolder::NodePtr NodePtr;
  214. typedef rbtree < Key, Value, KeyOfValue
  215. , KeyCompare, A> ThisType;
  216. typedef typename AllocHolder::NodeAlloc NodeAlloc;
  217. typedef typename AllocHolder::ValAlloc ValAlloc;
  218. typedef typename AllocHolder::Node Node;
  219. typedef typename Icont::iterator iiterator;
  220. typedef typename Icont::const_iterator iconst_iterator;
  221. typedef container_detail::allocator_destroyer<NodeAlloc> Destroyer;
  222. typedef typename AllocHolder::allocator_v1 allocator_v1;
  223. typedef typename AllocHolder::allocator_v2 allocator_v2;
  224. typedef typename AllocHolder::alloc_version alloc_version;
  225. class RecyclingCloner;
  226. friend class RecyclingCloner;
  227. class RecyclingCloner
  228. {
  229. public:
  230. RecyclingCloner(AllocHolder &holder, Icont &irbtree)
  231. : m_holder(holder), m_icont(irbtree)
  232. {}
  233. NodePtr operator()(const Node &other) const
  234. {
  235. if(NodePtr p = m_icont.unlink_leftmost_without_rebalance()){
  236. //First recycle a node (this can't throw)
  237. BOOST_TRY{
  238. //This can throw
  239. p->do_assign(other.m_data);
  240. return p;
  241. }
  242. BOOST_CATCH(...){
  243. //If there is an exception destroy the whole source
  244. m_holder.destroy_node(p);
  245. while((p = m_icont.unlink_leftmost_without_rebalance())){
  246. m_holder.destroy_node(p);
  247. }
  248. BOOST_RETHROW
  249. }
  250. BOOST_CATCH_END
  251. }
  252. else{
  253. return m_holder.create_node(other.m_data);
  254. }
  255. }
  256. AllocHolder &m_holder;
  257. Icont &m_icont;
  258. };
  259. class RecyclingMoveCloner;
  260. friend class RecyclingMoveCloner;
  261. class RecyclingMoveCloner
  262. {
  263. public:
  264. RecyclingMoveCloner(AllocHolder &holder, Icont &irbtree)
  265. : m_holder(holder), m_icont(irbtree)
  266. {}
  267. NodePtr operator()(const Node &other) const
  268. {
  269. if(NodePtr p = m_icont.unlink_leftmost_without_rebalance()){
  270. //First recycle a node (this can't throw)
  271. BOOST_TRY{
  272. //This can throw
  273. p->do_move_assign(const_cast<Node &>(other).m_data);
  274. return p;
  275. }
  276. BOOST_CATCH(...){
  277. //If there is an exception destroy the whole source
  278. m_holder.destroy_node(p);
  279. while((p = m_icont.unlink_leftmost_without_rebalance())){
  280. m_holder.destroy_node(p);
  281. }
  282. BOOST_RETHROW
  283. }
  284. BOOST_CATCH_END
  285. }
  286. else{
  287. return m_holder.create_node(other.m_data);
  288. }
  289. }
  290. AllocHolder &m_holder;
  291. Icont &m_icont;
  292. };
  293. BOOST_COPYABLE_AND_MOVABLE(rbtree)
  294. public:
  295. typedef Key key_type;
  296. typedef Value value_type;
  297. typedef A allocator_type;
  298. typedef KeyCompare key_compare;
  299. typedef ValComp value_compare;
  300. typedef typename boost::container::
  301. allocator_traits<A>::pointer pointer;
  302. typedef typename boost::container::
  303. allocator_traits<A>::const_pointer const_pointer;
  304. typedef typename boost::container::
  305. allocator_traits<A>::reference reference;
  306. typedef typename boost::container::
  307. allocator_traits<A>::const_reference const_reference;
  308. typedef typename boost::container::
  309. allocator_traits<A>::size_type size_type;
  310. typedef typename boost::container::
  311. allocator_traits<A>::difference_type difference_type;
  312. typedef difference_type rbtree_difference_type;
  313. typedef pointer rbtree_pointer;
  314. typedef const_pointer rbtree_const_pointer;
  315. typedef reference rbtree_reference;
  316. typedef const_reference rbtree_const_reference;
  317. typedef NodeAlloc stored_allocator_type;
  318. private:
  319. template<class KeyValueCompare>
  320. struct key_node_compare
  321. : private KeyValueCompare
  322. {
  323. key_node_compare(const KeyValueCompare &comp)
  324. : KeyValueCompare(comp)
  325. {}
  326. template<class T>
  327. struct is_node
  328. {
  329. static const bool value = is_same<T, Node>::value;
  330. };
  331. template<class T>
  332. typename enable_if_c<is_node<T>::value, const value_type &>::type
  333. key_forward(const T &node) const
  334. { return node.get_data(); }
  335. template<class T>
  336. typename enable_if_c<!is_node<T>::value, const T &>::type
  337. key_forward(const T &key) const
  338. { return key; }
  339. template<class KeyType, class KeyType2>
  340. bool operator()(const KeyType &key1, const KeyType2 &key2) const
  341. { return KeyValueCompare::operator()(this->key_forward(key1), this->key_forward(key2)); }
  342. };
  343. typedef key_node_compare<value_compare> KeyNodeCompare;
  344. public:
  345. typedef container_detail::iterator<iiterator, false> iterator;
  346. typedef container_detail::iterator<iiterator, true > const_iterator;
  347. typedef std::reverse_iterator<iterator> reverse_iterator;
  348. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  349. rbtree()
  350. : AllocHolder(ValComp(key_compare()))
  351. {}
  352. explicit rbtree(const key_compare& comp, const allocator_type& a = allocator_type())
  353. : AllocHolder(a, ValComp(comp))
  354. {}
  355. explicit rbtree(const allocator_type& a)
  356. : AllocHolder(a)
  357. {}
  358. template <class InputIterator>
  359. rbtree(bool unique_insertion, InputIterator first, InputIterator last, const key_compare& comp,
  360. const allocator_type& a
  361. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  362. , typename container_detail::enable_if_c
  363. < container_detail::is_input_iterator<InputIterator>::value
  364. || container_detail::is_same<alloc_version, allocator_v1>::value
  365. >::type * = 0
  366. #endif
  367. )
  368. : AllocHolder(a, value_compare(comp))
  369. {
  370. //Use cend() as hint to achieve linear time for
  371. //ordered ranges as required by the standard
  372. //for the constructor
  373. const const_iterator end_it(this->cend());
  374. if(unique_insertion){
  375. for ( ; first != last; ++first){
  376. this->insert_unique(end_it, *first);
  377. }
  378. }
  379. else{
  380. for ( ; first != last; ++first){
  381. this->insert_equal(end_it, *first);
  382. }
  383. }
  384. }
  385. template <class InputIterator>
  386. rbtree(bool unique_insertion, InputIterator first, InputIterator last, const key_compare& comp,
  387. const allocator_type& a
  388. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  389. , typename container_detail::enable_if_c
  390. < !(container_detail::is_input_iterator<InputIterator>::value
  391. || container_detail::is_same<alloc_version, allocator_v1>::value)
  392. >::type * = 0
  393. #endif
  394. )
  395. : AllocHolder(a, value_compare(comp))
  396. {
  397. if(unique_insertion){
  398. //Use cend() as hint to achieve linear time for
  399. //ordered ranges as required by the standard
  400. //for the constructor
  401. const const_iterator end_it(this->cend());
  402. for ( ; first != last; ++first){
  403. this->insert_unique(end_it, *first);
  404. }
  405. }
  406. else{
  407. //Optimized allocation and construction
  408. this->allocate_many_and_construct
  409. ( first, std::distance(first, last)
  410. , insert_equal_end_hint_functor<Node, Icont>(this->icont()));
  411. }
  412. }
  413. template <class InputIterator>
  414. rbtree( ordered_range_t, InputIterator first, InputIterator last
  415. , const key_compare& comp = key_compare(), const allocator_type& a = allocator_type()
  416. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  417. , typename container_detail::enable_if_c
  418. < container_detail::is_input_iterator<InputIterator>::value
  419. || container_detail::is_same<alloc_version, allocator_v1>::value
  420. >::type * = 0
  421. #endif
  422. )
  423. : AllocHolder(a, value_compare(comp))
  424. {
  425. for ( ; first != last; ++first){
  426. this->push_back_impl(*first);
  427. }
  428. }
  429. template <class InputIterator>
  430. rbtree( ordered_range_t, InputIterator first, InputIterator last
  431. , const key_compare& comp = key_compare(), const allocator_type& a = allocator_type()
  432. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  433. , typename container_detail::enable_if_c
  434. < !(container_detail::is_input_iterator<InputIterator>::value
  435. || container_detail::is_same<alloc_version, allocator_v1>::value)
  436. >::type * = 0
  437. #endif
  438. )
  439. : AllocHolder(a, value_compare(comp))
  440. {
  441. //Optimized allocation and construction
  442. this->allocate_many_and_construct
  443. ( first, std::distance(first, last)
  444. , container_detail::push_back_functor<Node, Icont>(this->icont()));
  445. }
  446. rbtree(const rbtree& x)
  447. : AllocHolder(x, x.value_comp())
  448. {
  449. this->icont().clone_from
  450. (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc()));
  451. }
  452. rbtree(BOOST_RV_REF(rbtree) x)
  453. : AllocHolder(::boost::move(static_cast<AllocHolder&>(x)), x.value_comp())
  454. {}
  455. rbtree(const rbtree& x, const allocator_type &a)
  456. : AllocHolder(a, x.value_comp())
  457. {
  458. this->icont().clone_from
  459. (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc()));
  460. }
  461. rbtree(BOOST_RV_REF(rbtree) x, const allocator_type &a)
  462. : AllocHolder(a, x.value_comp())
  463. {
  464. if(this->node_alloc() == x.node_alloc()){
  465. this->icont().swap(x.icont());
  466. }
  467. else{
  468. this->icont().clone_from
  469. (x.icont(), typename AllocHolder::cloner(*this), Destroyer(this->node_alloc()));
  470. }
  471. }
  472. ~rbtree()
  473. {} //AllocHolder clears the tree
  474. rbtree& operator=(BOOST_COPY_ASSIGN_REF(rbtree) x)
  475. {
  476. if (&x != this){
  477. NodeAlloc &this_alloc = this->get_stored_allocator();
  478. const NodeAlloc &x_alloc = x.get_stored_allocator();
  479. container_detail::bool_<allocator_traits<NodeAlloc>::
  480. propagate_on_container_copy_assignment::value> flag;
  481. if(flag && this_alloc != x_alloc){
  482. this->clear();
  483. }
  484. this->AllocHolder::copy_assign_alloc(x);
  485. //Transfer all the nodes to a temporary tree
  486. //If anything goes wrong, all the nodes will be destroyed
  487. //automatically
  488. Icont other_tree(::boost::move(this->icont()));
  489. //Now recreate the source tree reusing nodes stored by other_tree
  490. this->icont().clone_from
  491. (x.icont()
  492. , RecyclingCloner(*this, other_tree)
  493. , Destroyer(this->node_alloc()));
  494. //If there are remaining nodes, destroy them
  495. NodePtr p;
  496. while((p = other_tree.unlink_leftmost_without_rebalance())){
  497. AllocHolder::destroy_node(p);
  498. }
  499. }
  500. return *this;
  501. }
  502. rbtree& operator=(BOOST_RV_REF(rbtree) x)
  503. {
  504. if (&x != this){
  505. NodeAlloc &this_alloc = this->get_stored_allocator();
  506. const NodeAlloc &x_alloc = x.get_stored_allocator();
  507. //If allocators are equal we can just swap pointers
  508. if(this_alloc == x_alloc){
  509. //Destroy and swap pointers
  510. this->clear();
  511. this->icont() = ::boost::move(x.icont());
  512. //Move allocator if needed
  513. this->AllocHolder::move_assign_alloc(x);
  514. }
  515. //If unequal allocators, then do a one by one move
  516. else{
  517. //Transfer all the nodes to a temporary tree
  518. //If anything goes wrong, all the nodes will be destroyed
  519. //automatically
  520. Icont other_tree(::boost::move(this->icont()));
  521. //Now recreate the source tree reusing nodes stored by other_tree
  522. this->icont().clone_from
  523. (x.icont()
  524. , RecyclingMoveCloner(*this, other_tree)
  525. , Destroyer(this->node_alloc()));
  526. //If there are remaining nodes, destroy them
  527. NodePtr p;
  528. while((p = other_tree.unlink_leftmost_without_rebalance())){
  529. AllocHolder::destroy_node(p);
  530. }
  531. }
  532. }
  533. return *this;
  534. }
  535. public:
  536. // accessors:
  537. value_compare value_comp() const
  538. { return this->icont().value_comp().value_comp(); }
  539. key_compare key_comp() const
  540. { return this->icont().value_comp().value_comp().key_comp(); }
  541. allocator_type get_allocator() const
  542. { return allocator_type(this->node_alloc()); }
  543. const stored_allocator_type &get_stored_allocator() const
  544. { return this->node_alloc(); }
  545. stored_allocator_type &get_stored_allocator()
  546. { return this->node_alloc(); }
  547. iterator begin()
  548. { return iterator(this->icont().begin()); }
  549. const_iterator begin() const
  550. { return this->cbegin(); }
  551. iterator end()
  552. { return iterator(this->icont().end()); }
  553. const_iterator end() const
  554. { return this->cend(); }
  555. reverse_iterator rbegin()
  556. { return reverse_iterator(end()); }
  557. const_reverse_iterator rbegin() const
  558. { return this->crbegin(); }
  559. reverse_iterator rend()
  560. { return reverse_iterator(begin()); }
  561. const_reverse_iterator rend() const
  562. { return this->crend(); }
  563. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the container.
  564. //!
  565. //! <b>Throws</b>: Nothing.
  566. //!
  567. //! <b>Complexity</b>: Constant.
  568. const_iterator cbegin() const
  569. { return const_iterator(this->non_const_icont().begin()); }
  570. //! <b>Effects</b>: Returns a const_iterator to the end of the container.
  571. //!
  572. //! <b>Throws</b>: Nothing.
  573. //!
  574. //! <b>Complexity</b>: Constant.
  575. const_iterator cend() const
  576. { return const_iterator(this->non_const_icont().end()); }
  577. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  578. //! of the reversed container.
  579. //!
  580. //! <b>Throws</b>: Nothing.
  581. //!
  582. //! <b>Complexity</b>: Constant.
  583. const_reverse_iterator crbegin() const
  584. { return const_reverse_iterator(cend()); }
  585. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  586. //! of the reversed container.
  587. //!
  588. //! <b>Throws</b>: Nothing.
  589. //!
  590. //! <b>Complexity</b>: Constant.
  591. const_reverse_iterator crend() const
  592. { return const_reverse_iterator(cbegin()); }
  593. bool empty() const
  594. { return !this->size(); }
  595. size_type size() const
  596. { return this->icont().size(); }
  597. size_type max_size() const
  598. { return AllocHolder::max_size(); }
  599. void swap(ThisType& x)
  600. { AllocHolder::swap(x); }
  601. public:
  602. typedef typename Icont::insert_commit_data insert_commit_data;
  603. // insert/erase
  604. std::pair<iterator,bool> insert_unique_check
  605. (const key_type& key, insert_commit_data &data)
  606. {
  607. std::pair<iiterator, bool> ret =
  608. this->icont().insert_unique_check(key, KeyNodeCompare(value_comp()), data);
  609. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  610. }
  611. std::pair<iterator,bool> insert_unique_check
  612. (const_iterator hint, const key_type& key, insert_commit_data &data)
  613. {
  614. std::pair<iiterator, bool> ret =
  615. this->icont().insert_unique_check(hint.get(), key, KeyNodeCompare(value_comp()), data);
  616. return std::pair<iterator, bool>(iterator(ret.first), ret.second);
  617. }
  618. iterator insert_unique_commit(const value_type& v, insert_commit_data &data)
  619. {
  620. NodePtr tmp = AllocHolder::create_node(v);
  621. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  622. iterator ret(this->icont().insert_unique_commit(*tmp, data));
  623. destroy_deallocator.release();
  624. return ret;
  625. }
  626. template<class MovableConvertible>
  627. iterator insert_unique_commit
  628. (BOOST_FWD_REF(MovableConvertible) mv, insert_commit_data &data)
  629. {
  630. NodePtr tmp = AllocHolder::create_node(boost::forward<MovableConvertible>(mv));
  631. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  632. iterator ret(this->icont().insert_unique_commit(*tmp, data));
  633. destroy_deallocator.release();
  634. return ret;
  635. }
  636. std::pair<iterator,bool> insert_unique(const value_type& v)
  637. {
  638. insert_commit_data data;
  639. std::pair<iterator,bool> ret =
  640. this->insert_unique_check(KeyOfValue()(v), data);
  641. if(ret.second){
  642. ret.first = this->insert_unique_commit(v, data);
  643. }
  644. return ret;
  645. }
  646. template<class MovableConvertible>
  647. std::pair<iterator,bool> insert_unique(BOOST_FWD_REF(MovableConvertible) mv)
  648. {
  649. insert_commit_data data;
  650. std::pair<iterator,bool> ret =
  651. this->insert_unique_check(KeyOfValue()(mv), data);
  652. if(ret.second){
  653. ret.first = this->insert_unique_commit(boost::forward<MovableConvertible>(mv), data);
  654. }
  655. return ret;
  656. }
  657. private:
  658. template<class MovableConvertible>
  659. void push_back_impl(BOOST_FWD_REF(MovableConvertible) mv)
  660. {
  661. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(mv)));
  662. //push_back has no-throw guarantee so avoid any deallocator/destroyer
  663. this->icont().push_back(*tmp);
  664. }
  665. std::pair<iterator, bool> emplace_unique_impl(NodePtr p)
  666. {
  667. value_type &v = p->get_data();
  668. insert_commit_data data;
  669. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(p, this->node_alloc());
  670. std::pair<iterator,bool> ret =
  671. this->insert_unique_check(KeyOfValue()(v), data);
  672. if(!ret.second){
  673. return ret;
  674. }
  675. //No throw insertion part, release rollback
  676. destroy_deallocator.release();
  677. return std::pair<iterator,bool>
  678. ( iterator(iiterator(this->icont().insert_unique_commit(*p, data)))
  679. , true );
  680. }
  681. iterator emplace_unique_hint_impl(const_iterator hint, NodePtr p)
  682. {
  683. value_type &v = p->get_data();
  684. insert_commit_data data;
  685. std::pair<iterator,bool> ret =
  686. this->insert_unique_check(hint, KeyOfValue()(v), data);
  687. if(!ret.second){
  688. Destroyer(this->node_alloc())(p);
  689. return ret.first;
  690. }
  691. return iterator(iiterator(this->icont().insert_unique_commit(*p, data)));
  692. }
  693. public:
  694. #ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  695. template <class... Args>
  696. std::pair<iterator, bool> emplace_unique(Args&&... args)
  697. { return this->emplace_unique_impl(AllocHolder::create_node(boost::forward<Args>(args)...)); }
  698. template <class... Args>
  699. iterator emplace_hint_unique(const_iterator hint, Args&&... args)
  700. { return this->emplace_unique_hint_impl(hint, AllocHolder::create_node(boost::forward<Args>(args)...)); }
  701. template <class... Args>
  702. iterator emplace_equal(Args&&... args)
  703. {
  704. NodePtr tmp(AllocHolder::create_node(boost::forward<Args>(args)...));
  705. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  706. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  707. destroy_deallocator.release();
  708. return ret;
  709. }
  710. template <class... Args>
  711. iterator emplace_hint_equal(const_iterator hint, Args&&... args)
  712. {
  713. NodePtr tmp(AllocHolder::create_node(boost::forward<Args>(args)...));
  714. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  715. iterator ret(this->icont().insert_equal(hint.get(), *tmp));
  716. destroy_deallocator.release();
  717. return ret;
  718. }
  719. #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  720. #define BOOST_PP_LOCAL_MACRO(n) \
  721. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  722. std::pair<iterator, bool> emplace_unique(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  723. { \
  724. return this->emplace_unique_impl \
  725. (AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \
  726. } \
  727. \
  728. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  729. iterator emplace_hint_unique(const_iterator hint \
  730. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  731. { \
  732. return this->emplace_unique_hint_impl \
  733. (hint, AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \
  734. } \
  735. \
  736. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  737. iterator emplace_equal(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  738. { \
  739. NodePtr tmp(AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \
  740. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc()); \
  741. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp)); \
  742. destroy_deallocator.release(); \
  743. return ret; \
  744. } \
  745. \
  746. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  747. iterator emplace_hint_equal(const_iterator hint \
  748. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  749. { \
  750. NodePtr tmp(AllocHolder::create_node(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \
  751. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc()); \
  752. iterator ret(this->icont().insert_equal(hint.get(), *tmp)); \
  753. destroy_deallocator.release(); \
  754. return ret; \
  755. } \
  756. //!
  757. #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
  758. #include BOOST_PP_LOCAL_ITERATE()
  759. #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  760. iterator insert_unique(const_iterator hint, const value_type& v)
  761. {
  762. insert_commit_data data;
  763. std::pair<iterator,bool> ret =
  764. this->insert_unique_check(hint, KeyOfValue()(v), data);
  765. if(!ret.second)
  766. return ret.first;
  767. return this->insert_unique_commit(v, data);
  768. }
  769. template<class MovableConvertible>
  770. iterator insert_unique(const_iterator hint, BOOST_FWD_REF(MovableConvertible) mv)
  771. {
  772. insert_commit_data data;
  773. std::pair<iterator,bool> ret =
  774. this->insert_unique_check(hint, KeyOfValue()(mv), data);
  775. if(!ret.second)
  776. return ret.first;
  777. return this->insert_unique_commit(boost::forward<MovableConvertible>(mv), data);
  778. }
  779. template <class InputIterator>
  780. void insert_unique(InputIterator first, InputIterator last)
  781. {
  782. for( ; first != last; ++first)
  783. this->insert_unique(*first);
  784. }
  785. iterator insert_equal(const value_type& v)
  786. {
  787. NodePtr tmp(AllocHolder::create_node(v));
  788. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  789. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  790. destroy_deallocator.release();
  791. return ret;
  792. }
  793. template<class MovableConvertible>
  794. iterator insert_equal(BOOST_FWD_REF(MovableConvertible) mv)
  795. {
  796. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(mv)));
  797. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  798. iterator ret(this->icont().insert_equal(this->icont().end(), *tmp));
  799. destroy_deallocator.release();
  800. return ret;
  801. }
  802. iterator insert_equal(const_iterator hint, const value_type& v)
  803. {
  804. NodePtr tmp(AllocHolder::create_node(v));
  805. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  806. iterator ret(this->icont().insert_equal(hint.get(), *tmp));
  807. destroy_deallocator.release();
  808. return ret;
  809. }
  810. template<class MovableConvertible>
  811. iterator insert_equal(const_iterator hint, BOOST_FWD_REF(MovableConvertible) mv)
  812. {
  813. NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(mv)));
  814. scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
  815. iterator ret(this->icont().insert_equal(hint.get(), *tmp));
  816. destroy_deallocator.release();
  817. return ret;
  818. }
  819. template <class InputIterator>
  820. void insert_equal(InputIterator first, InputIterator last)
  821. {
  822. for( ; first != last; ++first)
  823. this->insert_equal(*first);
  824. }
  825. iterator erase(const_iterator position)
  826. { return iterator(this->icont().erase_and_dispose(position.get(), Destroyer(this->node_alloc()))); }
  827. size_type erase(const key_type& k)
  828. { return AllocHolder::erase_key(k, KeyNodeCompare(value_comp()), alloc_version()); }
  829. iterator erase(const_iterator first, const_iterator last)
  830. { return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version())); }
  831. void clear()
  832. { AllocHolder::clear(alloc_version()); }
  833. // set operations:
  834. iterator find(const key_type& k)
  835. { return iterator(this->icont().find(k, KeyNodeCompare(value_comp()))); }
  836. const_iterator find(const key_type& k) const
  837. { return const_iterator(this->non_const_icont().find(k, KeyNodeCompare(value_comp()))); }
  838. size_type count(const key_type& k) const
  839. { return size_type(this->icont().count(k, KeyNodeCompare(value_comp()))); }
  840. iterator lower_bound(const key_type& k)
  841. { return iterator(this->icont().lower_bound(k, KeyNodeCompare(value_comp()))); }
  842. const_iterator lower_bound(const key_type& k) const
  843. { return const_iterator(this->non_const_icont().lower_bound(k, KeyNodeCompare(value_comp()))); }
  844. iterator upper_bound(const key_type& k)
  845. { return iterator(this->icont().upper_bound(k, KeyNodeCompare(value_comp()))); }
  846. const_iterator upper_bound(const key_type& k) const
  847. { return const_iterator(this->non_const_icont().upper_bound(k, KeyNodeCompare(value_comp()))); }
  848. std::pair<iterator,iterator> equal_range(const key_type& k)
  849. {
  850. std::pair<iiterator, iiterator> ret =
  851. this->icont().equal_range(k, KeyNodeCompare(value_comp()));
  852. return std::pair<iterator,iterator>(iterator(ret.first), iterator(ret.second));
  853. }
  854. std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
  855. {
  856. std::pair<iiterator, iiterator> ret =
  857. this->non_const_icont().equal_range(k, KeyNodeCompare(value_comp()));
  858. return std::pair<const_iterator,const_iterator>
  859. (const_iterator(ret.first), const_iterator(ret.second));
  860. }
  861. };
  862. template <class Key, class Value, class KeyOfValue,
  863. class KeyCompare, class A>
  864. inline bool
  865. operator==(const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
  866. const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y)
  867. {
  868. return x.size() == y.size() &&
  869. std::equal(x.begin(), x.end(), y.begin());
  870. }
  871. template <class Key, class Value, class KeyOfValue,
  872. class KeyCompare, class A>
  873. inline bool
  874. operator<(const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
  875. const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y)
  876. {
  877. return std::lexicographical_compare(x.begin(), x.end(),
  878. y.begin(), y.end());
  879. }
  880. template <class Key, class Value, class KeyOfValue,
  881. class KeyCompare, class A>
  882. inline bool
  883. operator!=(const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
  884. const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y) {
  885. return !(x == y);
  886. }
  887. template <class Key, class Value, class KeyOfValue,
  888. class KeyCompare, class A>
  889. inline bool
  890. operator>(const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
  891. const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y) {
  892. return y < x;
  893. }
  894. template <class Key, class Value, class KeyOfValue,
  895. class KeyCompare, class A>
  896. inline bool
  897. operator<=(const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
  898. const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y) {
  899. return !(y < x);
  900. }
  901. template <class Key, class Value, class KeyOfValue,
  902. class KeyCompare, class A>
  903. inline bool
  904. operator>=(const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
  905. const rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y) {
  906. return !(x < y);
  907. }
  908. template <class Key, class Value, class KeyOfValue,
  909. class KeyCompare, class A>
  910. inline void
  911. swap(rbtree<Key,Value,KeyOfValue,KeyCompare,A>& x,
  912. rbtree<Key,Value,KeyOfValue,KeyCompare,A>& y)
  913. {
  914. x.swap(y);
  915. }
  916. } //namespace container_detail {
  917. } //namespace container {
  918. /*
  919. //!has_trivial_destructor_after_move<> == true_type
  920. //!specialization for optimizations
  921. template <class K, class V, class KOV,
  922. class C, class A>
  923. struct has_trivial_destructor_after_move
  924. <boost::container::container_detail::rbtree<K, V, KOV, C, A> >
  925. {
  926. static const bool value = has_trivial_destructor_after_move<A>::value && has_trivial_destructor_after_move<C>::value;
  927. };
  928. */
  929. } //namespace boost {
  930. #include <boost/container/detail/config_end.hpp>
  931. #endif //BOOST_CONTAINER_TREE_HPP