flat_tree.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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_FLAT_TREE_HPP
  11. #define BOOST_CONTAINER_FLAT_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. #include <boost/container/container_fwd.hpp>
  21. #include <boost/move/utility_core.hpp>
  22. #include <boost/container/detail/pair.hpp>
  23. #include <boost/container/vector.hpp>
  24. #include <boost/container/detail/value_init.hpp>
  25. #include <boost/container/detail/destroyers.hpp>
  26. #include <boost/container/detail/algorithm.hpp> //algo_equal(), algo_lexicographical_compare
  27. #include <boost/container/detail/iterator.hpp>
  28. #include <boost/container/allocator_traits.hpp>
  29. #ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  30. #include <boost/intrusive/pointer_traits.hpp>
  31. #endif
  32. #include <boost/container/detail/type_traits.hpp>
  33. #include <boost/move/make_unique.hpp>
  34. #include <boost/move/adl_move_swap.hpp>
  35. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  36. #include <boost/move/detail/fwd_macros.hpp>
  37. #endif
  38. #include <boost/intrusive/detail/minimal_pair_header.hpp> //pair
  39. namespace boost {
  40. namespace container {
  41. namespace container_detail {
  42. template<class Compare, class Value, class KeyOfValue>
  43. class flat_tree_value_compare
  44. : private Compare
  45. {
  46. typedef Value first_argument_type;
  47. typedef Value second_argument_type;
  48. typedef bool return_type;
  49. public:
  50. flat_tree_value_compare()
  51. : Compare()
  52. {}
  53. flat_tree_value_compare(const Compare &pred)
  54. : Compare(pred)
  55. {}
  56. bool operator()(const Value& lhs, const Value& rhs) const
  57. {
  58. KeyOfValue key_extract;
  59. return Compare::operator()(key_extract(lhs), key_extract(rhs));
  60. }
  61. const Compare &get_comp() const
  62. { return *this; }
  63. Compare &get_comp()
  64. { return *this; }
  65. };
  66. template<class Pointer>
  67. struct get_flat_tree_iterators
  68. {
  69. #ifdef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  70. typedef Pointer iterator;
  71. typedef typename boost::intrusive::
  72. pointer_traits<Pointer>::element_type iterator_element_type;
  73. typedef typename boost::intrusive::
  74. pointer_traits<Pointer>:: template
  75. rebind_pointer<const iterator_element_type>::type const_iterator;
  76. #else //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  77. typedef typename boost::container::container_detail::
  78. vec_iterator<Pointer, false> iterator;
  79. typedef typename boost::container::container_detail::
  80. vec_iterator<Pointer, true > const_iterator;
  81. #endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  82. typedef boost::container::reverse_iterator<iterator> reverse_iterator;
  83. typedef boost::container::reverse_iterator<const_iterator> const_reverse_iterator;
  84. };
  85. template <class Key, class Value, class KeyOfValue,
  86. class Compare, class Allocator>
  87. class flat_tree
  88. {
  89. typedef boost::container::vector<Value, Allocator> vector_t;
  90. typedef Allocator allocator_t;
  91. typedef allocator_traits<Allocator> allocator_traits_type;
  92. public:
  93. typedef flat_tree_value_compare<Compare, Value, KeyOfValue> value_compare;
  94. private:
  95. struct Data
  96. //Inherit from value_compare to do EBO
  97. : public value_compare
  98. {
  99. BOOST_COPYABLE_AND_MOVABLE(Data)
  100. public:
  101. Data()
  102. : value_compare(), m_vect()
  103. {}
  104. explicit Data(const Data &d)
  105. : value_compare(static_cast<const value_compare&>(d)), m_vect(d.m_vect)
  106. {}
  107. Data(BOOST_RV_REF(Data) d)
  108. : value_compare(boost::move(static_cast<value_compare&>(d))), m_vect(boost::move(d.m_vect))
  109. {}
  110. Data(const Data &d, const Allocator &a)
  111. : value_compare(static_cast<const value_compare&>(d)), m_vect(d.m_vect, a)
  112. {}
  113. Data(BOOST_RV_REF(Data) d, const Allocator &a)
  114. : value_compare(boost::move(static_cast<value_compare&>(d))), m_vect(boost::move(d.m_vect), a)
  115. {}
  116. explicit Data(const Compare &comp)
  117. : value_compare(comp), m_vect()
  118. {}
  119. Data(const Compare &comp, const allocator_t &alloc)
  120. : value_compare(comp), m_vect(alloc)
  121. {}
  122. explicit Data(const allocator_t &alloc)
  123. : value_compare(), m_vect(alloc)
  124. {}
  125. Data& operator=(BOOST_COPY_ASSIGN_REF(Data) d)
  126. {
  127. this->value_compare::operator=(d);
  128. m_vect = d.m_vect;
  129. return *this;
  130. }
  131. Data& operator=(BOOST_RV_REF(Data) d)
  132. {
  133. this->value_compare::operator=(boost::move(static_cast<value_compare &>(d)));
  134. m_vect = boost::move(d.m_vect);
  135. return *this;
  136. }
  137. void swap(Data &d)
  138. {
  139. value_compare& mycomp = *this, & othercomp = d;
  140. boost::adl_move_swap(mycomp, othercomp);
  141. this->m_vect.swap(d.m_vect);
  142. }
  143. vector_t m_vect;
  144. };
  145. Data m_data;
  146. BOOST_COPYABLE_AND_MOVABLE(flat_tree)
  147. public:
  148. typedef typename vector_t::value_type value_type;
  149. typedef typename vector_t::pointer pointer;
  150. typedef typename vector_t::const_pointer const_pointer;
  151. typedef typename vector_t::reference reference;
  152. typedef typename vector_t::const_reference const_reference;
  153. typedef Key key_type;
  154. typedef Compare key_compare;
  155. typedef typename vector_t::allocator_type allocator_type;
  156. typedef typename vector_t::size_type size_type;
  157. typedef typename vector_t::difference_type difference_type;
  158. typedef typename vector_t::iterator iterator;
  159. typedef typename vector_t::const_iterator const_iterator;
  160. typedef typename vector_t::reverse_iterator reverse_iterator;
  161. typedef typename vector_t::const_reverse_iterator const_reverse_iterator;
  162. //!Standard extension
  163. typedef allocator_type stored_allocator_type;
  164. private:
  165. typedef allocator_traits<stored_allocator_type> stored_allocator_traits;
  166. public:
  167. flat_tree()
  168. : m_data()
  169. { }
  170. explicit flat_tree(const Compare& comp)
  171. : m_data(comp)
  172. { }
  173. flat_tree(const Compare& comp, const allocator_type& a)
  174. : m_data(comp, a)
  175. { }
  176. explicit flat_tree(const allocator_type& a)
  177. : m_data(a)
  178. { }
  179. flat_tree(const flat_tree& x)
  180. : m_data(x.m_data)
  181. { }
  182. flat_tree(BOOST_RV_REF(flat_tree) x)
  183. : m_data(boost::move(x.m_data))
  184. { }
  185. flat_tree(const flat_tree& x, const allocator_type &a)
  186. : m_data(x.m_data, a)
  187. { }
  188. flat_tree(BOOST_RV_REF(flat_tree) x, const allocator_type &a)
  189. : m_data(boost::move(x.m_data), a)
  190. { }
  191. template <class InputIterator>
  192. flat_tree( ordered_range_t, InputIterator first, InputIterator last
  193. , const Compare& comp = Compare()
  194. , const allocator_type& a = allocator_type())
  195. : m_data(comp, a)
  196. { this->m_data.m_vect.insert(this->m_data.m_vect.end(), first, last); }
  197. template <class InputIterator>
  198. flat_tree( bool unique_insertion
  199. , InputIterator first, InputIterator last
  200. , const Compare& comp = Compare()
  201. , const allocator_type& a = allocator_type())
  202. : m_data(comp, a)
  203. {
  204. //Use cend() as hint to achieve linear time for
  205. //ordered ranges as required by the standard
  206. //for the constructor
  207. //Call end() every iteration as reallocation might have invalidated iterators
  208. if(unique_insertion){
  209. for ( ; first != last; ++first){
  210. this->insert_unique(this->cend(), *first);
  211. }
  212. }
  213. else{
  214. for ( ; first != last; ++first){
  215. this->insert_equal(this->cend(), *first);
  216. }
  217. }
  218. }
  219. ~flat_tree()
  220. {}
  221. flat_tree& operator=(BOOST_COPY_ASSIGN_REF(flat_tree) x)
  222. { m_data = x.m_data; return *this; }
  223. flat_tree& operator=(BOOST_RV_REF(flat_tree) x)
  224. BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
  225. && boost::container::container_detail::is_nothrow_move_assignable<Compare>::value )
  226. { m_data = boost::move(x.m_data); return *this; }
  227. public:
  228. // accessors:
  229. Compare key_comp() const
  230. { return this->m_data.get_comp(); }
  231. value_compare value_comp() const
  232. { return this->m_data; }
  233. allocator_type get_allocator() const
  234. { return this->m_data.m_vect.get_allocator(); }
  235. const stored_allocator_type &get_stored_allocator() const
  236. { return this->m_data.m_vect.get_stored_allocator(); }
  237. stored_allocator_type &get_stored_allocator()
  238. { return this->m_data.m_vect.get_stored_allocator(); }
  239. iterator begin()
  240. { return this->m_data.m_vect.begin(); }
  241. const_iterator begin() const
  242. { return this->cbegin(); }
  243. const_iterator cbegin() const
  244. { return this->m_data.m_vect.begin(); }
  245. iterator end()
  246. { return this->m_data.m_vect.end(); }
  247. const_iterator end() const
  248. { return this->cend(); }
  249. const_iterator cend() const
  250. { return this->m_data.m_vect.end(); }
  251. reverse_iterator rbegin()
  252. { return reverse_iterator(this->end()); }
  253. const_reverse_iterator rbegin() const
  254. { return this->crbegin(); }
  255. const_reverse_iterator crbegin() const
  256. { return const_reverse_iterator(this->cend()); }
  257. reverse_iterator rend()
  258. { return reverse_iterator(this->begin()); }
  259. const_reverse_iterator rend() const
  260. { return this->crend(); }
  261. const_reverse_iterator crend() const
  262. { return const_reverse_iterator(this->cbegin()); }
  263. bool empty() const
  264. { return this->m_data.m_vect.empty(); }
  265. size_type size() const
  266. { return this->m_data.m_vect.size(); }
  267. size_type max_size() const
  268. { return this->m_data.m_vect.max_size(); }
  269. void swap(flat_tree& other)
  270. BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
  271. && boost::container::container_detail::is_nothrow_swappable<Compare>::value )
  272. { this->m_data.swap(other.m_data); }
  273. public:
  274. // insert/erase
  275. std::pair<iterator,bool> insert_unique(const value_type& val)
  276. {
  277. std::pair<iterator,bool> ret;
  278. insert_commit_data data;
  279. ret.second = this->priv_insert_unique_prepare(val, data);
  280. ret.first = ret.second ? this->priv_insert_commit(data, val)
  281. : iterator(vector_iterator_get_ptr(data.position));
  282. return ret;
  283. }
  284. std::pair<iterator,bool> insert_unique(BOOST_RV_REF(value_type) val)
  285. {
  286. std::pair<iterator,bool> ret;
  287. insert_commit_data data;
  288. ret.second = this->priv_insert_unique_prepare(val, data);
  289. ret.first = ret.second ? this->priv_insert_commit(data, boost::move(val))
  290. : iterator(vector_iterator_get_ptr(data.position));
  291. return ret;
  292. }
  293. iterator insert_equal(const value_type& val)
  294. {
  295. iterator i = this->upper_bound(KeyOfValue()(val));
  296. i = this->m_data.m_vect.insert(i, val);
  297. return i;
  298. }
  299. iterator insert_equal(BOOST_RV_REF(value_type) mval)
  300. {
  301. iterator i = this->upper_bound(KeyOfValue()(mval));
  302. i = this->m_data.m_vect.insert(i, boost::move(mval));
  303. return i;
  304. }
  305. iterator insert_unique(const_iterator hint, const value_type& val)
  306. {
  307. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  308. std::pair<iterator,bool> ret;
  309. insert_commit_data data;
  310. return this->priv_insert_unique_prepare(hint, val, data)
  311. ? this->priv_insert_commit(data, val)
  312. : iterator(vector_iterator_get_ptr(data.position));
  313. }
  314. iterator insert_unique(const_iterator hint, BOOST_RV_REF(value_type) val)
  315. {
  316. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  317. std::pair<iterator,bool> ret;
  318. insert_commit_data data;
  319. return this->priv_insert_unique_prepare(hint, val, data)
  320. ? this->priv_insert_commit(data, boost::move(val))
  321. : iterator(vector_iterator_get_ptr(data.position));
  322. }
  323. iterator insert_equal(const_iterator hint, const value_type& val)
  324. {
  325. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  326. insert_commit_data data;
  327. this->priv_insert_equal_prepare(hint, val, data);
  328. return this->priv_insert_commit(data, val);
  329. }
  330. iterator insert_equal(const_iterator hint, BOOST_RV_REF(value_type) mval)
  331. {
  332. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  333. insert_commit_data data;
  334. this->priv_insert_equal_prepare(hint, mval, data);
  335. return this->priv_insert_commit(data, boost::move(mval));
  336. }
  337. template <class InIt>
  338. void insert_unique(InIt first, InIt last)
  339. {
  340. for ( ; first != last; ++first){
  341. this->insert_unique(*first);
  342. }
  343. }
  344. template <class InIt>
  345. void insert_equal(InIt first, InIt last
  346. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  347. , typename container_detail::enable_if_c
  348. < container_detail::is_input_iterator<InIt>::value
  349. >::type * = 0
  350. #endif
  351. )
  352. { this->priv_insert_equal_loop(first, last); }
  353. template <class InIt>
  354. void insert_equal(InIt first, InIt last
  355. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  356. , typename container_detail::enable_if_c
  357. < !container_detail::is_input_iterator<InIt>::value
  358. >::type * = 0
  359. #endif
  360. )
  361. {
  362. const size_type len = static_cast<size_type>(boost::container::iterator_distance(first, last));
  363. this->reserve(this->size()+len);
  364. this->priv_insert_equal_loop(first, last);
  365. }
  366. //Ordered
  367. template <class InIt>
  368. void insert_equal(ordered_range_t, InIt first, InIt last
  369. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  370. , typename container_detail::enable_if_c
  371. < container_detail::is_input_iterator<InIt>::value
  372. >::type * = 0
  373. #endif
  374. )
  375. { this->priv_insert_equal_loop_ordered(first, last); }
  376. template <class FwdIt>
  377. void insert_equal(ordered_range_t, FwdIt first, FwdIt last
  378. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  379. , typename container_detail::enable_if_c
  380. < !container_detail::is_input_iterator<FwdIt>::value &&
  381. container_detail::is_forward_iterator<FwdIt>::value
  382. >::type * = 0
  383. #endif
  384. )
  385. {
  386. const size_type len = static_cast<size_type>(boost::container::iterator_distance(first, last));
  387. this->reserve(this->size()+len);
  388. this->priv_insert_equal_loop_ordered(first, last);
  389. }
  390. template <class BidirIt>
  391. void insert_equal(ordered_range_t, BidirIt first, BidirIt last
  392. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  393. , typename container_detail::disable_if_or
  394. < void
  395. , container_detail::is_input_iterator<BidirIt>
  396. , container_detail::is_forward_iterator<BidirIt>
  397. >::type * = 0
  398. #endif
  399. )
  400. { this->m_data.m_vect.merge(first, last); }
  401. template <class InIt>
  402. void insert_unique(ordered_unique_range_t, InIt first, InIt last
  403. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  404. , typename container_detail::enable_if_or
  405. < void
  406. , container_detail::is_input_iterator<InIt>
  407. , container_detail::is_forward_iterator<InIt>
  408. >::type * = 0
  409. #endif
  410. )
  411. {
  412. const_iterator pos(this->cend());
  413. for ( ; first != last; ++first){
  414. pos = this->insert_unique(pos, *first);
  415. ++pos;
  416. }
  417. }
  418. template <class BidirIt>
  419. void insert_unique(ordered_unique_range_t, BidirIt first, BidirIt last
  420. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  421. , typename container_detail::enable_if_c
  422. < !(container_detail::is_input_iterator<BidirIt>::value ||
  423. container_detail::is_forward_iterator<BidirIt>::value)
  424. >::type * = 0
  425. #endif
  426. )
  427. { this->m_data.m_vect.merge_unique(first, last, value_compare()); }
  428. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  429. template <class... Args>
  430. std::pair<iterator, bool> emplace_unique(BOOST_FWD_REF(Args)... args)
  431. {
  432. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  433. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
  434. stored_allocator_type &a = this->get_stored_allocator();
  435. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  436. value_destructor<stored_allocator_type> d(a, val);
  437. return this->insert_unique(::boost::move(val));
  438. }
  439. template <class... Args>
  440. iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
  441. {
  442. //hint checked in insert_unique
  443. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  444. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
  445. stored_allocator_type &a = this->get_stored_allocator();
  446. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  447. value_destructor<stored_allocator_type> d(a, val);
  448. return this->insert_unique(hint, ::boost::move(val));
  449. }
  450. template <class... Args>
  451. iterator emplace_equal(BOOST_FWD_REF(Args)... args)
  452. {
  453. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  454. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
  455. stored_allocator_type &a = this->get_stored_allocator();
  456. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  457. value_destructor<stored_allocator_type> d(a, val);
  458. return this->insert_equal(::boost::move(val));
  459. }
  460. template <class... Args>
  461. iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
  462. {
  463. //hint checked in insert_equal
  464. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  465. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
  466. stored_allocator_type &a = this->get_stored_allocator();
  467. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  468. value_destructor<stored_allocator_type> d(a, val);
  469. return this->insert_equal(hint, ::boost::move(val));
  470. }
  471. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  472. #define BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE(N) \
  473. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  474. std::pair<iterator, bool> emplace_unique(BOOST_MOVE_UREF##N)\
  475. {\
  476. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  477. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
  478. stored_allocator_type &a = this->get_stored_allocator();\
  479. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  480. value_destructor<stored_allocator_type> d(a, val);\
  481. return this->insert_unique(::boost::move(val));\
  482. }\
  483. \
  484. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  485. iterator emplace_hint_unique(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  486. {\
  487. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  488. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
  489. stored_allocator_type &a = this->get_stored_allocator();\
  490. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  491. value_destructor<stored_allocator_type> d(a, val);\
  492. return this->insert_unique(hint, ::boost::move(val));\
  493. }\
  494. \
  495. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  496. iterator emplace_equal(BOOST_MOVE_UREF##N)\
  497. {\
  498. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  499. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
  500. stored_allocator_type &a = this->get_stored_allocator();\
  501. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  502. value_destructor<stored_allocator_type> d(a, val);\
  503. return this->insert_equal(::boost::move(val));\
  504. }\
  505. \
  506. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  507. iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  508. {\
  509. typename aligned_storage <sizeof(value_type), alignment_of<value_type>::value>::type v;\
  510. value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));\
  511. stored_allocator_type &a = this->get_stored_allocator();\
  512. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  513. value_destructor<stored_allocator_type> d(a, val);\
  514. return this->insert_equal(hint, ::boost::move(val));\
  515. }\
  516. //
  517. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE)
  518. #undef BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE
  519. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  520. iterator erase(const_iterator position)
  521. { return this->m_data.m_vect.erase(position); }
  522. size_type erase(const key_type& k)
  523. {
  524. std::pair<iterator,iterator > itp = this->equal_range(k);
  525. size_type ret = static_cast<size_type>(itp.second-itp.first);
  526. if (ret){
  527. this->m_data.m_vect.erase(itp.first, itp.second);
  528. }
  529. return ret;
  530. }
  531. iterator erase(const_iterator first, const_iterator last)
  532. { return this->m_data.m_vect.erase(first, last); }
  533. void clear()
  534. { this->m_data.m_vect.clear(); }
  535. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  536. // with previous allocations. The size of the vector is unchanged
  537. //!
  538. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  539. //!
  540. //! <b>Complexity</b>: Linear to size().
  541. void shrink_to_fit()
  542. { this->m_data.m_vect.shrink_to_fit(); }
  543. iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  544. { return this->m_data.m_vect.nth(n); }
  545. const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  546. { return this->m_data.m_vect.nth(n); }
  547. size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  548. { return this->m_data.m_vect.index_of(p); }
  549. size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  550. { return this->m_data.m_vect.index_of(p); }
  551. // set operations:
  552. iterator find(const key_type& k)
  553. {
  554. iterator i = this->lower_bound(k);
  555. iterator end_it = this->end();
  556. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  557. i = end_it;
  558. }
  559. return i;
  560. }
  561. const_iterator find(const key_type& k) const
  562. {
  563. const_iterator i = this->lower_bound(k);
  564. const_iterator end_it = this->cend();
  565. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  566. i = end_it;
  567. }
  568. return i;
  569. }
  570. // set operations:
  571. size_type count(const key_type& k) const
  572. {
  573. std::pair<const_iterator, const_iterator> p = this->equal_range(k);
  574. size_type n = p.second - p.first;
  575. return n;
  576. }
  577. iterator lower_bound(const key_type& k)
  578. { return this->priv_lower_bound(this->begin(), this->end(), k); }
  579. const_iterator lower_bound(const key_type& k) const
  580. { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
  581. iterator upper_bound(const key_type& k)
  582. { return this->priv_upper_bound(this->begin(), this->end(), k); }
  583. const_iterator upper_bound(const key_type& k) const
  584. { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
  585. std::pair<iterator,iterator> equal_range(const key_type& k)
  586. { return this->priv_equal_range(this->begin(), this->end(), k); }
  587. std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
  588. { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
  589. std::pair<iterator, iterator> lower_bound_range(const key_type& k)
  590. { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
  591. std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
  592. { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
  593. size_type capacity() const
  594. { return this->m_data.m_vect.capacity(); }
  595. void reserve(size_type cnt)
  596. { this->m_data.m_vect.reserve(cnt); }
  597. friend bool operator==(const flat_tree& x, const flat_tree& y)
  598. {
  599. return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin());
  600. }
  601. friend bool operator<(const flat_tree& x, const flat_tree& y)
  602. {
  603. return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  604. }
  605. friend bool operator!=(const flat_tree& x, const flat_tree& y)
  606. { return !(x == y); }
  607. friend bool operator>(const flat_tree& x, const flat_tree& y)
  608. { return y < x; }
  609. friend bool operator<=(const flat_tree& x, const flat_tree& y)
  610. { return !(y < x); }
  611. friend bool operator>=(const flat_tree& x, const flat_tree& y)
  612. { return !(x < y); }
  613. friend void swap(flat_tree& x, flat_tree& y)
  614. { x.swap(y); }
  615. private:
  616. bool priv_in_range_or_end(const_iterator pos) const
  617. {
  618. return (this->begin() <= pos) && (pos <= this->end());
  619. }
  620. struct insert_commit_data
  621. {
  622. const_iterator position;
  623. };
  624. // insert/erase
  625. void priv_insert_equal_prepare
  626. (const_iterator pos, const value_type& val, insert_commit_data &data)
  627. {
  628. // N1780
  629. // To insert val at pos:
  630. // if pos == end || val <= *pos
  631. // if pos == begin || val >= *(pos-1)
  632. // insert val before pos
  633. // else
  634. // insert val before upper_bound(val)
  635. // else
  636. // insert val before lower_bound(val)
  637. const value_compare &val_cmp = this->m_data;
  638. if(pos == this->cend() || !val_cmp(*pos, val)){
  639. if (pos == this->cbegin() || !val_cmp(val, pos[-1])){
  640. data.position = pos;
  641. }
  642. else{
  643. data.position =
  644. this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val));
  645. }
  646. }
  647. else{
  648. data.position =
  649. this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val));
  650. }
  651. }
  652. bool priv_insert_unique_prepare
  653. (const_iterator b, const_iterator e, const value_type& val, insert_commit_data &commit_data)
  654. {
  655. const value_compare &val_cmp = this->m_data;
  656. commit_data.position = this->priv_lower_bound(b, e, KeyOfValue()(val));
  657. return commit_data.position == e || val_cmp(val, *commit_data.position);
  658. }
  659. bool priv_insert_unique_prepare
  660. (const value_type& val, insert_commit_data &commit_data)
  661. { return this->priv_insert_unique_prepare(this->cbegin(), this->cend(), val, commit_data); }
  662. bool priv_insert_unique_prepare
  663. (const_iterator pos, const value_type& val, insert_commit_data &commit_data)
  664. {
  665. //N1780. Props to Howard Hinnant!
  666. //To insert val at pos:
  667. //if pos == end || val <= *pos
  668. // if pos == begin || val >= *(pos-1)
  669. // insert val before pos
  670. // else
  671. // insert val before upper_bound(val)
  672. //else if pos+1 == end || val <= *(pos+1)
  673. // insert val after pos
  674. //else
  675. // insert val before lower_bound(val)
  676. const value_compare &val_cmp = this->m_data;
  677. const const_iterator cend_it = this->cend();
  678. if(pos == cend_it || val_cmp(val, *pos)){ //Check if val should go before end
  679. const const_iterator cbeg = this->cbegin();
  680. commit_data.position = pos;
  681. if(pos == cbeg){ //If container is empty then insert it in the beginning
  682. return true;
  683. }
  684. const_iterator prev(pos);
  685. --prev;
  686. if(val_cmp(*prev, val)){ //If previous element was less, then it should go between prev and pos
  687. return true;
  688. }
  689. else if(!val_cmp(val, *prev)){ //If previous was equal then insertion should fail
  690. commit_data.position = prev;
  691. return false;
  692. }
  693. else{ //Previous was bigger so insertion hint was pointless, dispatch to hintless insertion
  694. //but reduce the search between beg and prev as prev is bigger than val
  695. return this->priv_insert_unique_prepare(cbeg, prev, val, commit_data);
  696. }
  697. }
  698. else{
  699. //The hint is before the insertion position, so insert it
  700. //in the remaining range [pos, end)
  701. return this->priv_insert_unique_prepare(pos, cend_it, val, commit_data);
  702. }
  703. }
  704. template<class Convertible>
  705. iterator priv_insert_commit
  706. (insert_commit_data &commit_data, BOOST_FWD_REF(Convertible) convertible)
  707. {
  708. return this->m_data.m_vect.insert
  709. ( commit_data.position
  710. , boost::forward<Convertible>(convertible));
  711. }
  712. template <class RanIt>
  713. RanIt priv_lower_bound(RanIt first, const RanIt last,
  714. const key_type & key) const
  715. {
  716. const Compare &key_cmp = this->m_data.get_comp();
  717. KeyOfValue key_extract;
  718. size_type len = static_cast<size_type>(last - first);
  719. RanIt middle;
  720. while (len) {
  721. size_type step = len >> 1;
  722. middle = first;
  723. middle += step;
  724. if (key_cmp(key_extract(*middle), key)) {
  725. first = ++middle;
  726. len -= step + 1;
  727. }
  728. else{
  729. len = step;
  730. }
  731. }
  732. return first;
  733. }
  734. template <class RanIt>
  735. RanIt priv_upper_bound
  736. (RanIt first, const RanIt last,const key_type & key) const
  737. {
  738. const Compare &key_cmp = this->m_data.get_comp();
  739. KeyOfValue key_extract;
  740. size_type len = static_cast<size_type>(last - first);
  741. RanIt middle;
  742. while (len) {
  743. size_type step = len >> 1;
  744. middle = first;
  745. middle += step;
  746. if (key_cmp(key, key_extract(*middle))) {
  747. len = step;
  748. }
  749. else{
  750. first = ++middle;
  751. len -= step + 1;
  752. }
  753. }
  754. return first;
  755. }
  756. template <class RanIt>
  757. std::pair<RanIt, RanIt>
  758. priv_equal_range(RanIt first, RanIt last, const key_type& key) const
  759. {
  760. const Compare &key_cmp = this->m_data.get_comp();
  761. KeyOfValue key_extract;
  762. size_type len = static_cast<size_type>(last - first);
  763. RanIt middle;
  764. while (len) {
  765. size_type step = len >> 1;
  766. middle = first;
  767. middle += step;
  768. if (key_cmp(key_extract(*middle), key)){
  769. first = ++middle;
  770. len -= step + 1;
  771. }
  772. else if (key_cmp(key, key_extract(*middle))){
  773. len = step;
  774. }
  775. else {
  776. //Middle is equal to key
  777. last = first;
  778. last += len;
  779. RanIt const first_ret = this->priv_lower_bound(first, middle, key);
  780. return std::pair<RanIt, RanIt>
  781. ( first_ret, this->priv_upper_bound(++middle, last, key));
  782. }
  783. }
  784. return std::pair<RanIt, RanIt>(first, first);
  785. }
  786. template<class RanIt>
  787. std::pair<RanIt, RanIt> priv_lower_bound_range(RanIt first, RanIt last, const key_type& k) const
  788. {
  789. const Compare &key_cmp = this->m_data.get_comp();
  790. KeyOfValue key_extract;
  791. RanIt lb(this->priv_lower_bound(first, last, k)), ub(lb);
  792. if(lb != last && static_cast<difference_type>(!key_cmp(k, key_extract(*lb)))){
  793. ++ub;
  794. }
  795. return std::pair<RanIt, RanIt>(lb, ub);
  796. }
  797. template<class InIt>
  798. void priv_insert_equal_loop(InIt first, InIt last)
  799. {
  800. for ( ; first != last; ++first){
  801. this->insert_equal(*first);
  802. }
  803. }
  804. template<class InIt>
  805. void priv_insert_equal_loop_ordered(InIt first, InIt last)
  806. {
  807. const_iterator pos(this->cend());
  808. for ( ; first != last; ++first){
  809. //If ordered, then try hint version
  810. //to achieve constant-time complexity per insertion
  811. //in some cases
  812. pos = this->insert_equal(pos, *first);
  813. ++pos;
  814. }
  815. }
  816. };
  817. } //namespace container_detail {
  818. } //namespace container {
  819. //!has_trivial_destructor_after_move<> == true_type
  820. //!specialization for optimizations
  821. template <class Key, class T, class KeyOfValue,
  822. class Compare, class Allocator>
  823. struct has_trivial_destructor_after_move<boost::container::container_detail::flat_tree<Key, T, KeyOfValue, Compare, Allocator> >
  824. {
  825. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  826. static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
  827. ::boost::has_trivial_destructor_after_move<pointer>::value;
  828. };
  829. } //namespace boost {
  830. #include <boost/container/detail/config_end.hpp>
  831. #endif // BOOST_CONTAINER_FLAT_TREE_HPP