flat_tree.hpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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/vector.hpp>
  23. #include <boost/container/allocator_traits.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/detail/is_sorted.hpp>
  29. #include <boost/container/detail/type_traits.hpp>
  30. #include <boost/container/detail/iterators.hpp>
  31. #include <boost/container/detail/mpl.hpp>
  32. #include <boost/container/detail/is_contiguous_container.hpp>
  33. #include <boost/container/detail/is_container.hpp>
  34. #include <boost/intrusive/detail/minimal_pair_header.hpp> //pair
  35. #include <boost/move/iterator.hpp>
  36. #include <boost/move/adl_move_swap.hpp>
  37. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  38. #include <boost/move/detail/to_raw_pointer.hpp>
  39. #include <boost/move/detail/force_ptr.hpp>
  40. #include <boost/move/detail/launder.hpp>
  41. #include <boost/move/algo/adaptive_sort.hpp>
  42. #include <boost/move/algo/detail/pdqsort.hpp>
  43. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  44. #include <boost/move/detail/fwd_macros.hpp>
  45. #endif
  46. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  47. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  48. #pragma GCC diagnostic push
  49. #pragma GCC diagnostic ignored "-Wunused-result"
  50. #endif
  51. //merge_unique
  52. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME merge_unique
  53. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEG namespace boost { namespace container { namespace dtl {
  54. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}}
  55. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MIN 3
  56. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MAX 3
  57. #include <boost/intrusive/detail/has_member_function_callable_with.hpp>
  58. //merge_equal
  59. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME merge
  60. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEG namespace boost { namespace container { namespace dtl {
  61. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}}
  62. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MIN 3
  63. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MAX 3
  64. #include <boost/intrusive/detail/has_member_function_callable_with.hpp>
  65. //index_of
  66. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME index_of
  67. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEG namespace boost { namespace container { namespace dtl {
  68. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}}
  69. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MIN 1
  70. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MAX 1
  71. #include <boost/intrusive/detail/has_member_function_callable_with.hpp>
  72. //nth
  73. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME nth
  74. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEG namespace boost { namespace container { namespace dtl {
  75. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}}
  76. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MIN 1
  77. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MAX 1
  78. #include <boost/intrusive/detail/has_member_function_callable_with.hpp>
  79. //reserve
  80. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME reserve
  81. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEG namespace boost { namespace container { namespace dtl {
  82. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}}
  83. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MIN 1
  84. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MAX 1
  85. #include <boost/intrusive/detail/has_member_function_callable_with.hpp>
  86. //capacity
  87. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_FUNCNAME capacity
  88. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_BEG namespace boost { namespace container { namespace dtl {
  89. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_NS_END }}}
  90. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MIN 0
  91. #define BOOST_INTRUSIVE_HAS_MEMBER_FUNCTION_CALLABLE_WITH_MAX 0
  92. #include <boost/intrusive/detail/has_member_function_callable_with.hpp>
  93. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  94. #pragma GCC diagnostic pop
  95. #endif
  96. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  97. namespace boost {
  98. namespace container {
  99. namespace dtl {
  100. ///////////////////////////////////////
  101. //
  102. // Helper functions to merge elements
  103. //
  104. ///////////////////////////////////////
  105. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(stored_allocator_type)
  106. ///////////////////////////////////////
  107. //
  108. // flat_tree_container_inplace_merge
  109. //
  110. ///////////////////////////////////////
  111. template<class SequenceContainer, class Compare>
  112. inline void flat_tree_container_inplace_merge //is_contiguous_container == true
  113. (SequenceContainer& dest, typename SequenceContainer::iterator it, Compare comp , dtl::true_)
  114. {
  115. typedef typename SequenceContainer::value_type value_type;
  116. typedef typename SequenceContainer::size_type size_type;
  117. value_type *const braw = boost::movelib::to_raw_pointer(dest.data());
  118. value_type *const iraw = boost::movelib::iterator_to_raw_pointer(it);
  119. //Don't use iterator_to_raw_pointer for end as debug iterators can assert when
  120. //"operator ->" is used with the end iterator
  121. value_type *const eraw = braw + dest.size();
  122. size_type dest_unused_storage_size = 0;
  123. value_type *const dest_unused_storage_addr =
  124. unused_storage<SequenceContainer>::get(dest, dest_unused_storage_size);
  125. boost::movelib::adaptive_merge
  126. (braw, iraw, eraw, comp, dest_unused_storage_addr, dest_unused_storage_size);
  127. }
  128. template<class SequenceContainer, class Compare>
  129. inline void flat_tree_container_inplace_merge //is_contiguous_container == false
  130. (SequenceContainer& dest, typename SequenceContainer::iterator it, Compare comp, dtl::false_)
  131. {
  132. boost::movelib::adaptive_merge(dest.begin(), it, dest.end(), comp);
  133. }
  134. ///////////////////////////////////////
  135. //
  136. // flat_tree_container_inplace_sort_ending
  137. //
  138. ///////////////////////////////////////
  139. template<class SequenceContainer, class Compare>
  140. inline void flat_tree_container_inplace_sort_ending //is_contiguous_container == true
  141. (SequenceContainer& dest, typename SequenceContainer::iterator it, Compare comp, dtl::true_)
  142. {
  143. typedef typename SequenceContainer::value_type value_type;
  144. typedef typename SequenceContainer::size_type size_type;
  145. value_type *const iraw = boost::movelib::iterator_to_raw_pointer(it);
  146. //Don't use iterator_to_raw_pointer for end as debug iterators can assert when
  147. //"operator ->" is used with the end iterator
  148. value_type* const eraw = boost::movelib::to_raw_pointer(dest.data()) + dest.size();
  149. size_type dest_unused_storage_size;
  150. value_type* const dest_unused_storage_addr =
  151. unused_storage<SequenceContainer>::get(dest, dest_unused_storage_size);
  152. boost::movelib::adaptive_sort
  153. (iraw, eraw, comp, dest_unused_storage_addr, dest_unused_storage_size);
  154. }
  155. template<class SequenceContainer, class Compare>
  156. inline void flat_tree_container_inplace_sort_ending //is_contiguous_container == false
  157. (SequenceContainer& dest, typename SequenceContainer::iterator it, Compare comp , dtl::false_)
  158. {
  159. boost::movelib::adaptive_sort(it, dest.end(), comp);
  160. }
  161. ///////////////////////////////////////
  162. //
  163. // flat_tree_merge
  164. //
  165. ///////////////////////////////////////
  166. template<class SequenceContainer, class Iterator, class Compare>
  167. inline void flat_tree_merge_equal
  168. (SequenceContainer& dest, Iterator first, Iterator last, Compare comp, dtl::true_)
  169. {
  170. dest.merge(first, last, comp);
  171. }
  172. template<class SequenceContainer, class Iterator, class Compare>
  173. inline void flat_tree_merge_equal //has_merge_unique == false
  174. (SequenceContainer& dest, Iterator first, Iterator last, Compare comp, dtl::false_)
  175. {
  176. if(first != last) {
  177. typedef typename SequenceContainer::iterator iterator;
  178. iterator const it = dest.insert( dest.end(), first, last);
  179. BOOST_ASSERT((is_sorted)(it, dest.end(), comp));
  180. dtl::bool_<is_contiguous_container<SequenceContainer>::value> contiguous_tag;
  181. (flat_tree_container_inplace_merge)(dest, it, comp, contiguous_tag);
  182. }
  183. }
  184. ///////////////////////////////////////
  185. //
  186. // flat_tree_merge_unique
  187. //
  188. ///////////////////////////////////////
  189. template<class SequenceContainer, class Iterator, class Compare>
  190. inline void flat_tree_merge_unique //has_merge_unique == true
  191. (SequenceContainer& dest, Iterator first, Iterator last, Compare comp, dtl::true_)
  192. {
  193. dest.merge_unique(first, last, comp);
  194. }
  195. template<class SequenceContainer, class Iterator, class Compare>
  196. inline void flat_tree_merge_unique //has_merge_unique == false
  197. (SequenceContainer& dest, Iterator first, Iterator last, Compare comp, dtl::false_)
  198. {
  199. if (first != last) {
  200. typedef typename SequenceContainer::iterator iterator;
  201. typedef typename SequenceContainer::size_type size_type;
  202. typedef typename SequenceContainer::difference_type difference_type;
  203. size_type const old_sz = dest.size();
  204. iterator const first_new = dest.insert(dest.cend(), first, last);
  205. //We can't assert "is_sorted_and_unique" because the sequence can come from a multiset
  206. BOOST_ASSERT((is_sorted)(first_new, dest.end(), comp));
  207. iterator e = boost::movelib::inplace_set_unique_difference(first_new, dest.end(), dest.begin(), first_new, comp);
  208. dest.erase(e, dest.end());
  209. dtl::bool_<is_contiguous_container<SequenceContainer>::value> contiguous_tag;
  210. (flat_tree_container_inplace_merge)(dest, dest.begin() + difference_type(old_sz), comp, contiguous_tag);
  211. }
  212. }
  213. ///////////////////////////////////////
  214. //
  215. // flat_tree_index_of
  216. //
  217. ///////////////////////////////////////
  218. template<class SequenceContainer, class Iterator>
  219. inline typename SequenceContainer::size_type
  220. flat_tree_index_of // has_index_of == true
  221. (SequenceContainer& cont, Iterator p, dtl::true_)
  222. {
  223. return cont.index_of(p);
  224. }
  225. template<class SequenceContainer, class Iterator>
  226. inline typename SequenceContainer::size_type
  227. flat_tree_index_of // has_index_of == false
  228. (SequenceContainer& cont, Iterator p, dtl::false_)
  229. {
  230. typedef typename SequenceContainer::size_type size_type;
  231. return static_cast<size_type>(p - cont.begin());
  232. }
  233. ///////////////////////////////////////
  234. //
  235. // flat_tree_nth
  236. //
  237. ///////////////////////////////////////
  238. template<class Iterator, class SequenceContainer>
  239. inline Iterator
  240. flat_tree_nth // has_nth == true
  241. (SequenceContainer& cont, typename SequenceContainer::size_type n, dtl::true_)
  242. {
  243. return cont.nth(n);
  244. }
  245. template<class Iterator, class SequenceContainer>
  246. inline Iterator
  247. flat_tree_nth // has_nth == false
  248. (SequenceContainer& cont, typename SequenceContainer::size_type n, dtl::false_)
  249. {
  250. return cont.begin()+ typename SequenceContainer::difference_type(n);
  251. }
  252. ///////////////////////////////////////
  253. //
  254. // flat_tree_get_stored_allocator
  255. //
  256. ///////////////////////////////////////
  257. template<class SequenceContainer>
  258. inline typename SequenceContainer::stored_allocator_type &
  259. flat_tree_get_stored_allocator // has_get_stored_allocator == true
  260. (SequenceContainer& cont, dtl::true_)
  261. {
  262. return cont.get_stored_allocator();
  263. }
  264. template<class SequenceContainer>
  265. inline const typename SequenceContainer::stored_allocator_type &
  266. flat_tree_get_stored_allocator // has_get_stored_allocator == true
  267. (const SequenceContainer& cont, dtl::true_)
  268. {
  269. return cont.get_stored_allocator();
  270. }
  271. template<class SequenceContainer>
  272. inline typename SequenceContainer::allocator_type
  273. flat_tree_get_stored_allocator // has_get_stored_allocator == false
  274. (SequenceContainer& cont, dtl::false_)
  275. {
  276. return cont.get_allocator();
  277. }
  278. ///////////////////////////////////////
  279. //
  280. // flat_tree_adopt_sequence_equal
  281. //
  282. ///////////////////////////////////////
  283. template<class SequenceContainer, class Compare>
  284. void flat_tree_sort_contiguous_to_adopt // is_contiguous_container == true
  285. (SequenceContainer &tseq, BOOST_RV_REF(SequenceContainer) seq, Compare comp)
  286. {
  287. typedef typename SequenceContainer::value_type value_type;
  288. typedef typename SequenceContainer::size_type size_type;
  289. size_type tseq_unused_storage_size, seq_unused_storage_size;
  290. value_type* const tseq_unused_storage_addr =
  291. unused_storage<SequenceContainer>::get(tseq, tseq_unused_storage_size);
  292. value_type* const seq_unused_storage_addr =
  293. unused_storage<SequenceContainer>::get(seq, seq_unused_storage_size);
  294. tseq.clear();
  295. const bool use_tseq_storage = tseq_unused_storage_size > seq_unused_storage_size;
  296. value_type * const seq_beg = boost::movelib::iterator_to_raw_pointer(seq.data());
  297. boost::movelib::adaptive_sort
  298. ( seq_beg
  299. , seq_beg + seq.size()
  300. , comp
  301. , use_tseq_storage ? tseq_unused_storage_addr : seq_unused_storage_addr
  302. , use_tseq_storage ? tseq_unused_storage_size : seq_unused_storage_size);
  303. }
  304. template<class SequenceContainer, class Compare>
  305. inline void flat_tree_adopt_sequence_equal // is_contiguous_container == true
  306. (SequenceContainer &tseq, BOOST_RV_REF(SequenceContainer) seq, Compare comp, dtl::true_)
  307. {
  308. flat_tree_sort_contiguous_to_adopt(tseq, boost::move(seq), comp);
  309. tseq = boost::move(seq);
  310. }
  311. template<class SequenceContainer, class Compare>
  312. inline void flat_tree_adopt_sequence_equal // is_contiguous_container == false
  313. (SequenceContainer &tseq, BOOST_RV_REF(SequenceContainer) seq, Compare comp, dtl::false_)
  314. {
  315. boost::movelib::adaptive_sort(seq.begin(), seq.end(), comp);
  316. tseq = boost::move(seq);
  317. }
  318. ///////////////////////////////////////
  319. //
  320. // flat_tree_adopt_sequence_unique
  321. //
  322. ///////////////////////////////////////
  323. template<class SequenceContainer, class Compare>
  324. void flat_tree_adopt_sequence_unique// is_contiguous_container == true
  325. (SequenceContainer &tseq, BOOST_RV_REF(SequenceContainer) seq, Compare comp, dtl::true_)
  326. {
  327. typedef typename SequenceContainer::value_type value_type;
  328. value_type * const seq_beg = boost::movelib::iterator_to_raw_pointer(seq.data());
  329. boost::movelib::pdqsort
  330. ( seq_beg
  331. , seq_beg + seq.size()
  332. , comp);
  333. seq.erase(boost::movelib::unique
  334. (seq.begin(), seq.end(), boost::movelib::negate<Compare>(comp)), seq.cend());
  335. tseq = boost::move(seq);
  336. }
  337. template<class SequenceContainer, class Compare>
  338. void flat_tree_adopt_sequence_unique// is_contiguous_container == false
  339. (SequenceContainer &tseq, BOOST_RV_REF(SequenceContainer) seq, Compare comp, dtl::false_)
  340. {
  341. boost::movelib::pdqsort(seq.begin(), seq.end(), comp);
  342. seq.erase(boost::movelib::unique
  343. (seq.begin(), seq.end(), boost::movelib::negate<Compare>(comp)), seq.cend());
  344. tseq = boost::move(seq);
  345. }
  346. ///////////////////////////////////////
  347. //
  348. // flat_tree_reserve
  349. //
  350. ///////////////////////////////////////
  351. template<class SequenceContainer>
  352. inline void // has_reserve == true
  353. flat_tree_reserve(SequenceContainer &tseq, typename SequenceContainer::size_type cap, dtl::true_)
  354. {
  355. tseq.reserve(cap);
  356. }
  357. template<class SequenceContainer>
  358. inline void // has_reserve == false
  359. flat_tree_reserve(SequenceContainer &, typename SequenceContainer::size_type, dtl::false_)
  360. {
  361. }
  362. ///////////////////////////////////////
  363. //
  364. // flat_tree_capacity
  365. //
  366. ///////////////////////////////////////
  367. template<class SequenceContainer> // has_capacity == true
  368. inline typename SequenceContainer::size_type
  369. flat_tree_capacity(const SequenceContainer &tseq, dtl::true_)
  370. {
  371. return tseq.capacity();
  372. }
  373. template<class SequenceContainer> // has_capacity == false
  374. inline typename SequenceContainer::size_type
  375. flat_tree_capacity(const SequenceContainer &tseq, dtl::false_)
  376. {
  377. return tseq.size();
  378. }
  379. ///////////////////////////////////////
  380. //
  381. // flat_tree_value_compare
  382. //
  383. ///////////////////////////////////////
  384. template<class Compare, class Value, class KeyOfValue>
  385. class flat_tree_value_compare
  386. : private Compare
  387. {
  388. typedef Value first_argument_type;
  389. typedef Value second_argument_type;
  390. typedef bool return_type;
  391. public:
  392. inline flat_tree_value_compare()
  393. : Compare()
  394. {}
  395. inline flat_tree_value_compare(const Compare &pred)
  396. : Compare(pred)
  397. {}
  398. inline bool operator()(const Value& lhs, const Value& rhs) const
  399. {
  400. KeyOfValue key_extract;
  401. return Compare::operator()(key_extract(lhs), key_extract(rhs));
  402. }
  403. inline const Compare &get_comp() const
  404. { return *this; }
  405. inline Compare &get_comp()
  406. { return *this; }
  407. };
  408. ///////////////////////////////////////
  409. //
  410. // select_flat_tree_container_type
  411. //
  412. ///////////////////////////////////////
  413. template < class Value, class AllocatorOrContainer
  414. , bool = boost::container::dtl::is_container<AllocatorOrContainer>::value
  415. >
  416. struct select_flat_tree_container_type
  417. {
  418. typedef AllocatorOrContainer type;
  419. };
  420. template <class Value, class AllocatorOrVoid>
  421. struct select_flat_tree_container_type<Value, AllocatorOrVoid, false>
  422. {
  423. typedef boost::container::vector<Value, AllocatorOrVoid> type;
  424. };
  425. ///////////////////////////////////////
  426. //
  427. // flat_tree
  428. //
  429. ///////////////////////////////////////
  430. template <class Value, class KeyOfValue,
  431. class Compare, class AllocatorOrContainer>
  432. class flat_tree
  433. {
  434. public:
  435. typedef typename select_flat_tree_container_type<Value, AllocatorOrContainer>::type container_type;
  436. typedef container_type sequence_type; //For backwards compatibility
  437. private:
  438. typedef typename container_type::allocator_type allocator_t;
  439. typedef allocator_traits<allocator_t> allocator_traits_type;
  440. public:
  441. typedef flat_tree_value_compare<Compare, Value, KeyOfValue> value_compare;
  442. private:
  443. struct Data
  444. //Inherit from value_compare to do EBO
  445. : public value_compare
  446. {
  447. BOOST_COPYABLE_AND_MOVABLE(Data)
  448. public:
  449. inline Data()
  450. : value_compare(), m_seq()
  451. {}
  452. inline explicit Data(const allocator_t &alloc)
  453. : value_compare(), m_seq(alloc)
  454. {}
  455. inline explicit Data(const Compare &comp)
  456. : value_compare(comp), m_seq()
  457. {}
  458. inline Data(const Compare &comp, const allocator_t &alloc)
  459. : value_compare(comp), m_seq(alloc)
  460. {}
  461. inline explicit Data(const Data &d)
  462. : value_compare(static_cast<const value_compare&>(d)), m_seq(d.m_seq)
  463. {}
  464. inline Data(BOOST_RV_REF(Data) d)
  465. : value_compare(boost::move(static_cast<value_compare&>(d))), m_seq(boost::move(d.m_seq))
  466. {}
  467. inline Data(const Data &d, const allocator_t &a)
  468. : value_compare(static_cast<const value_compare&>(d)), m_seq(d.m_seq, a)
  469. {}
  470. inline Data(BOOST_RV_REF(Data) d, const allocator_t &a)
  471. : value_compare(boost::move(static_cast<value_compare&>(d))), m_seq(boost::move(d.m_seq), a)
  472. {}
  473. Data& operator=(BOOST_COPY_ASSIGN_REF(Data) d)
  474. {
  475. this->value_compare::operator=(d);
  476. m_seq = d.m_seq;
  477. return *this;
  478. }
  479. Data& operator=(BOOST_RV_REF(Data) d)
  480. {
  481. this->value_compare::operator=(boost::move(static_cast<value_compare &>(d)));
  482. m_seq = boost::move(d.m_seq);
  483. return *this;
  484. }
  485. void swap(Data &d)
  486. {
  487. value_compare& mycomp = *this, & othercomp = d;
  488. boost::adl_move_swap(mycomp, othercomp);
  489. this->m_seq.swap(d.m_seq);
  490. }
  491. container_type m_seq;
  492. };
  493. Data m_data;
  494. BOOST_COPYABLE_AND_MOVABLE(flat_tree)
  495. public:
  496. typedef typename container_type::value_type value_type;
  497. typedef typename container_type::pointer pointer;
  498. typedef typename container_type::const_pointer const_pointer;
  499. typedef typename container_type::reference reference;
  500. typedef typename container_type::const_reference const_reference;
  501. typedef typename KeyOfValue::type key_type;
  502. typedef Compare key_compare;
  503. typedef typename container_type::allocator_type allocator_type;
  504. typedef typename container_type::size_type size_type;
  505. typedef typename container_type::difference_type difference_type;
  506. typedef typename container_type::iterator iterator;
  507. typedef typename container_type::const_iterator const_iterator;
  508. typedef typename container_type::reverse_iterator reverse_iterator;
  509. typedef typename container_type::const_reverse_iterator const_reverse_iterator;
  510. //`allocator_type::value_type` must match container's `value type`. If this
  511. //assertion fails, please review your allocator definition.
  512. BOOST_CONTAINER_STATIC_ASSERT((is_same<value_type, typename allocator_traits_type::value_type>::value));
  513. //!Standard extension
  514. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  515. (boost::container::dtl::, container_type
  516. ,stored_allocator_type, allocator_type) stored_allocator_type;
  517. BOOST_STATIC_CONSTEXPR bool has_stored_allocator_type =
  518. BOOST_INTRUSIVE_HAS_TYPE(boost::container::dtl::, container_type, stored_allocator_type);
  519. private:
  520. typedef allocator_traits<stored_allocator_type> stored_allocator_traits;
  521. public:
  522. typedef typename dtl::if_c
  523. <has_stored_allocator_type, const stored_allocator_type &, allocator_type>::type get_stored_allocator_const_return_t;
  524. typedef typename dtl::if_c
  525. <has_stored_allocator_type, stored_allocator_type &, allocator_type>::type get_stored_allocator_noconst_return_t;
  526. inline flat_tree()
  527. : m_data()
  528. { }
  529. inline explicit flat_tree(const Compare& comp)
  530. : m_data(comp)
  531. { }
  532. inline explicit flat_tree(const allocator_type& a)
  533. : m_data(a)
  534. { }
  535. inline flat_tree(const Compare& comp, const allocator_type& a)
  536. : m_data(comp, a)
  537. { }
  538. inline flat_tree(const flat_tree& x)
  539. : m_data(x.m_data)
  540. { }
  541. inline flat_tree(BOOST_RV_REF(flat_tree) x)
  542. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_constructible<Compare>::value)
  543. : m_data(boost::move(x.m_data))
  544. { }
  545. inline flat_tree(const flat_tree& x, const allocator_type &a)
  546. : m_data(x.m_data, a)
  547. { }
  548. inline flat_tree(BOOST_RV_REF(flat_tree) x, const allocator_type &a)
  549. : m_data(boost::move(x.m_data), a)
  550. { }
  551. template <class InputIterator>
  552. inline
  553. flat_tree( ordered_range_t, InputIterator first, InputIterator last)
  554. : m_data()
  555. {
  556. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  557. BOOST_ASSERT((is_sorted)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  558. }
  559. template <class InputIterator>
  560. inline
  561. flat_tree( ordered_range_t, InputIterator first, InputIterator last, const Compare& comp)
  562. : m_data(comp)
  563. {
  564. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  565. BOOST_ASSERT((is_sorted)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  566. }
  567. template <class InputIterator>
  568. inline
  569. flat_tree( ordered_range_t, InputIterator first, InputIterator last, const Compare& comp, const allocator_type& a)
  570. : m_data(comp, a)
  571. {
  572. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  573. BOOST_ASSERT((is_sorted)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  574. }
  575. template <class InputIterator>
  576. inline
  577. flat_tree( ordered_unique_range_t, InputIterator first, InputIterator last)
  578. : m_data()
  579. {
  580. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  581. BOOST_ASSERT((is_sorted_and_unique)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  582. }
  583. template <class InputIterator>
  584. inline
  585. flat_tree( ordered_unique_range_t, InputIterator first, InputIterator last, const Compare& comp)
  586. : m_data(comp)
  587. {
  588. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  589. BOOST_ASSERT((is_sorted_and_unique)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  590. }
  591. template <class InputIterator>
  592. inline
  593. flat_tree( ordered_unique_range_t, InputIterator first, InputIterator last, const Compare& comp, const allocator_type& a)
  594. : m_data(comp, a)
  595. {
  596. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  597. BOOST_ASSERT((is_sorted_and_unique)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  598. }
  599. template <class InputIterator>
  600. inline
  601. flat_tree( bool unique_insertion, InputIterator first, InputIterator last)
  602. : m_data()
  603. {
  604. this->priv_range_insertion_construct(unique_insertion, first, last);
  605. }
  606. template <class InputIterator>
  607. inline
  608. flat_tree( bool unique_insertion, InputIterator first, InputIterator last
  609. , const Compare& comp)
  610. : m_data(comp)
  611. {
  612. this->priv_range_insertion_construct(unique_insertion, first, last);
  613. }
  614. template <class InputIterator>
  615. inline
  616. flat_tree( bool unique_insertion, InputIterator first, InputIterator last
  617. , const allocator_type& a)
  618. : m_data(a)
  619. {
  620. this->priv_range_insertion_construct(unique_insertion, first, last);
  621. }
  622. template <class InputIterator>
  623. inline
  624. flat_tree( bool unique_insertion, InputIterator first, InputIterator last
  625. , const Compare& comp, const allocator_type& a)
  626. : m_data(comp, a)
  627. {
  628. this->priv_range_insertion_construct(unique_insertion, first, last);
  629. }
  630. inline ~flat_tree()
  631. {
  632. }
  633. inline flat_tree& operator=(BOOST_COPY_ASSIGN_REF(flat_tree) x)
  634. { m_data = x.m_data; return *this; }
  635. inline flat_tree& operator=(BOOST_RV_REF(flat_tree) x)
  636. BOOST_NOEXCEPT_IF( (allocator_traits_type::propagate_on_container_move_assignment::value ||
  637. allocator_traits_type::is_always_equal::value) &&
  638. boost::container::dtl::is_nothrow_move_assignable<Compare>::value)
  639. { m_data = boost::move(x.m_data); return *this; }
  640. inline const value_compare &priv_value_comp() const
  641. { return static_cast<const value_compare &>(this->m_data); }
  642. inline value_compare &priv_value_comp()
  643. { return static_cast<value_compare &>(this->m_data); }
  644. inline const key_compare &priv_key_comp() const
  645. { return this->priv_value_comp().get_comp(); }
  646. inline key_compare &priv_key_comp()
  647. { return this->priv_value_comp().get_comp(); }
  648. struct insert_commit_data
  649. {
  650. const_iterator position;
  651. };
  652. public:
  653. // accessors:
  654. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  655. Compare key_comp() const
  656. { return this->m_data.get_comp(); }
  657. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  658. value_compare value_comp() const
  659. { return this->m_data; }
  660. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  661. allocator_type get_allocator() const
  662. { return this->m_data.m_seq.get_allocator(); }
  663. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  664. get_stored_allocator_const_return_t get_stored_allocator() const
  665. {
  666. return flat_tree_get_stored_allocator(this->m_data.m_seq, dtl::bool_<has_stored_allocator_type>());
  667. }
  668. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  669. get_stored_allocator_noconst_return_t get_stored_allocator()
  670. {
  671. return flat_tree_get_stored_allocator(this->m_data.m_seq, dtl::bool_<has_stored_allocator_type>());
  672. }
  673. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  674. iterator begin()
  675. { return this->m_data.m_seq.begin(); }
  676. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  677. const_iterator begin() const
  678. { return this->cbegin(); }
  679. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  680. const_iterator cbegin() const
  681. { return this->m_data.m_seq.begin(); }
  682. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  683. iterator end()
  684. { return this->m_data.m_seq.end(); }
  685. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  686. const_iterator end() const
  687. { return this->cend(); }
  688. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  689. const_iterator cend() const
  690. { return this->m_data.m_seq.end(); }
  691. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  692. reverse_iterator rbegin()
  693. { return reverse_iterator(this->end()); }
  694. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  695. const_reverse_iterator rbegin() const
  696. { return this->crbegin(); }
  697. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  698. const_reverse_iterator crbegin() const
  699. { return const_reverse_iterator(this->cend()); }
  700. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  701. reverse_iterator rend()
  702. { return reverse_iterator(this->begin()); }
  703. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  704. const_reverse_iterator rend() const
  705. { return this->crend(); }
  706. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  707. const_reverse_iterator crend() const
  708. { return const_reverse_iterator(this->cbegin()); }
  709. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  710. bool empty() const
  711. { return this->m_data.m_seq.empty(); }
  712. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  713. size_type size() const
  714. { return this->m_data.m_seq.size(); }
  715. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  716. size_type max_size() const
  717. { return this->m_data.m_seq.max_size(); }
  718. inline void swap(flat_tree& other)
  719. BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
  720. && boost::container::dtl::is_nothrow_swappable<Compare>::value )
  721. { this->m_data.swap(other.m_data); }
  722. public:
  723. // insert/erase
  724. std::pair<iterator,bool> insert_unique(const value_type& val)
  725. {
  726. std::pair<iterator,bool> ret;
  727. insert_commit_data data;
  728. ret.second = this->priv_insert_unique_prepare(KeyOfValue()(val), data);
  729. ret.first = ret.second ? this->priv_insert_commit(data, val)
  730. : this->begin() + (data.position - this->cbegin());
  731. //: iterator(vector_iterator_get_ptr(data.position));
  732. return ret;
  733. }
  734. std::pair<iterator,bool> insert_unique(BOOST_RV_REF(value_type) val)
  735. {
  736. std::pair<iterator,bool> ret;
  737. insert_commit_data data;
  738. ret.second = this->priv_insert_unique_prepare(KeyOfValue()(val), data);
  739. ret.first = ret.second ? this->priv_insert_commit(data, boost::move(val))
  740. : this->begin() + (data.position - this->cbegin());
  741. //: iterator(vector_iterator_get_ptr(data.position));
  742. return ret;
  743. }
  744. iterator insert_equal(const value_type& val)
  745. {
  746. iterator i = this->upper_bound(KeyOfValue()(val));
  747. i = this->m_data.m_seq.insert(i, val);
  748. return i;
  749. }
  750. iterator insert_equal(BOOST_RV_REF(value_type) mval)
  751. {
  752. iterator i = this->upper_bound(KeyOfValue()(mval));
  753. i = this->m_data.m_seq.insert(i, boost::move(mval));
  754. return i;
  755. }
  756. iterator insert_unique(const_iterator hint, const value_type& val)
  757. {
  758. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  759. insert_commit_data data;
  760. return this->priv_insert_unique_prepare(hint, KeyOfValue()(val), data)
  761. ? this->priv_insert_commit(data, val)
  762. : this->begin() + (data.position - this->cbegin());
  763. //: iterator(vector_iterator_get_ptr(data.position));
  764. }
  765. iterator insert_unique(const_iterator hint, BOOST_RV_REF(value_type) val)
  766. {
  767. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  768. insert_commit_data data;
  769. return this->priv_insert_unique_prepare(hint, KeyOfValue()(val), data)
  770. ? this->priv_insert_commit(data, boost::move(val))
  771. : this->begin() + (data.position - this->cbegin());
  772. //: iterator(vector_iterator_get_ptr(data.position));
  773. }
  774. iterator insert_equal(const_iterator hint, const value_type& val)
  775. {
  776. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  777. insert_commit_data data;
  778. this->priv_insert_equal_prepare(hint, val, data);
  779. return this->priv_insert_commit(data, val);
  780. }
  781. iterator insert_equal(const_iterator hint, BOOST_RV_REF(value_type) mval)
  782. {
  783. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  784. insert_commit_data data;
  785. this->priv_insert_equal_prepare(hint, mval, data);
  786. return this->priv_insert_commit(data, boost::move(mval));
  787. }
  788. template <class InIt>
  789. void insert_unique(InIt first, InIt last)
  790. {
  791. dtl::bool_<is_contiguous_container<container_type>::value> contiguous_tag;
  792. container_type &seq = this->m_data.m_seq;
  793. value_compare &val_cmp = this->priv_value_comp();
  794. //Step 1: put new elements in the back
  795. typename container_type::iterator const it = seq.insert(seq.cend(), first, last);
  796. //Step 2: sort them
  797. boost::movelib::pdqsort(it, seq.end(), val_cmp);
  798. //Step 3: only left unique values from the back not already present in the original range
  799. typename container_type::iterator const e = boost::movelib::inplace_set_unique_difference
  800. (it, seq.end(), seq.begin(), it, val_cmp);
  801. //it might be invalidated by erasing [e, seq.end) if e == it, so check it before
  802. const bool remaining = e != it;
  803. seq.erase(e, seq.cend());
  804. if (remaining)
  805. {
  806. //Step 4: merge both ranges
  807. (flat_tree_container_inplace_merge)(seq, it, this->priv_value_comp(), contiguous_tag);
  808. }
  809. }
  810. template <class InIt>
  811. void insert_equal(InIt first, InIt last)
  812. {
  813. if (first != last) {
  814. dtl::bool_<is_contiguous_container<container_type>::value> contiguous_tag;
  815. container_type &seq = this->m_data.m_seq;
  816. typename container_type::iterator const it = seq.insert(seq.cend(), first, last);
  817. (flat_tree_container_inplace_sort_ending)(seq, it, this->priv_value_comp(), contiguous_tag);
  818. (flat_tree_container_inplace_merge) (seq, it, this->priv_value_comp(), contiguous_tag);
  819. }
  820. }
  821. //Ordered
  822. template <class InIt>
  823. void insert_equal(ordered_range_t, InIt first, InIt last)
  824. {
  825. const bool value = boost::container::dtl::
  826. has_member_function_callable_with_merge_unique<container_type, InIt, InIt, value_compare>::value;
  827. (flat_tree_merge_equal)(this->m_data.m_seq, first, last, this->priv_value_comp(), dtl::bool_<value>());
  828. }
  829. template <class InIt>
  830. void insert_unique(ordered_unique_range_t, InIt first, InIt last)
  831. {
  832. const bool value = boost::container::dtl::
  833. has_member_function_callable_with_merge_unique<container_type, InIt, InIt, value_compare>::value;
  834. (flat_tree_merge_unique)(this->m_data.m_seq, first, last, this->priv_value_comp(), dtl::bool_<value>());
  835. }
  836. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  837. template <class... Args>
  838. std::pair<iterator, bool> emplace_unique(BOOST_FWD_REF(Args)... args)
  839. {
  840. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
  841. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  842. stored_allocator_traits::construct(a, (value_type *)(&v), ::boost::forward<Args>(args)... );
  843. value_type *pval = move_detail::launder_cast<value_type *>(&v);
  844. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  845. return this->insert_unique(::boost::move(*pval));
  846. }
  847. template <class... Args>
  848. iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
  849. {
  850. //hint checked in insert_unique
  851. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
  852. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  853. stored_allocator_traits::construct(a, (value_type*)(&v), ::boost::forward<Args>(args)... );
  854. value_type *pval = move_detail::launder_cast<value_type *>(&v);
  855. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  856. return this->insert_unique(hint, ::boost::move(*pval));
  857. }
  858. template <class... Args>
  859. iterator emplace_equal(BOOST_FWD_REF(Args)... args)
  860. {
  861. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
  862. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  863. stored_allocator_traits::construct(a, (value_type*)(&v), ::boost::forward<Args>(args)... );
  864. value_type *pval = move_detail::launder_cast<value_type *>(&v);
  865. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  866. return this->insert_equal(::boost::move(*pval));
  867. }
  868. template <class... Args>
  869. iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
  870. {
  871. //hint checked in insert_equal
  872. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
  873. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  874. stored_allocator_traits::construct(a, (value_type*)(&v), ::boost::forward<Args>(args)... );
  875. value_type *pval = move_detail::launder_cast<value_type *>(&v);
  876. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  877. return this->insert_equal(hint, ::boost::move(*pval));
  878. }
  879. template <class KeyType, class... Args>
  880. inline std::pair<iterator, bool> try_emplace
  881. (const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(Args)... args)
  882. {
  883. std::pair<iterator,bool> ret;
  884. insert_commit_data data;
  885. const key_type & k = key;
  886. ret.second = hint == const_iterator()
  887. ? this->priv_insert_unique_prepare(k, data)
  888. : this->priv_insert_unique_prepare(hint, k, data);
  889. if(!ret.second){
  890. ret.first = this->nth(size_type(data.position - this->cbegin()));
  891. }
  892. else{
  893. ret.first = this->m_data.m_seq.emplace(data.position, try_emplace_t(), ::boost::forward<KeyType>(key), ::boost::forward<Args>(args)...);
  894. }
  895. return ret;
  896. }
  897. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  898. #define BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE(N) \
  899. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  900. std::pair<iterator, bool> emplace_unique(BOOST_MOVE_UREF##N)\
  901. {\
  902. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
  903. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  904. stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  905. value_type *pval = move_detail::launder_cast<value_type *>(&v);\
  906. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  907. return this->insert_unique(::boost::move(*pval));\
  908. }\
  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. {\
  913. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
  914. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  915. stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  916. value_type *pval = move_detail::launder_cast<value_type *>(&v);\
  917. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  918. return this->insert_unique(hint, ::boost::move(*pval));\
  919. }\
  920. \
  921. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  922. iterator emplace_equal(BOOST_MOVE_UREF##N)\
  923. {\
  924. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
  925. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  926. stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  927. value_type *pval = move_detail::launder_cast<value_type *>(&v);\
  928. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  929. return this->insert_equal(::boost::move(*pval));\
  930. }\
  931. \
  932. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  933. iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  934. {\
  935. typename dtl::aligned_storage <sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
  936. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  937. stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  938. value_type *pval = move_detail::launder_cast<value_type *>(&v);\
  939. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  940. return this->insert_equal(hint, ::boost::move(*pval));\
  941. }\
  942. template <class KeyType BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  943. inline std::pair<iterator, bool>\
  944. try_emplace(const_iterator hint, BOOST_FWD_REF(KeyType) key BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  945. {\
  946. std::pair<iterator,bool> ret;\
  947. insert_commit_data data;\
  948. const key_type & k = key;\
  949. ret.second = hint == const_iterator()\
  950. ? this->priv_insert_unique_prepare(k, data)\
  951. : this->priv_insert_unique_prepare(hint, k, data);\
  952. \
  953. if(!ret.second){\
  954. ret.first = this->nth(size_type(data.position - this->cbegin()));\
  955. }\
  956. else{\
  957. ret.first = this->m_data.m_seq.emplace(data.position, try_emplace_t(), ::boost::forward<KeyType>(key) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  958. }\
  959. return ret;\
  960. }\
  961. //
  962. BOOST_MOVE_ITERATE_0TO7(BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE)
  963. #undef BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE
  964. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  965. template<class KeyType, class M>
  966. std::pair<iterator, bool> insert_or_assign(const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(M) obj)
  967. {
  968. const key_type& k = key;
  969. std::pair<iterator,bool> ret;
  970. insert_commit_data data;
  971. ret.second = hint == const_iterator()
  972. ? this->priv_insert_unique_prepare(k, data)
  973. : this->priv_insert_unique_prepare(hint, k, data);
  974. if(!ret.second){
  975. ret.first = this->nth(size_type(data.position - this->cbegin()));
  976. ret.first->second = boost::forward<M>(obj);
  977. }
  978. else{
  979. ret.first = this->m_data.m_seq.emplace(data.position, boost::forward<KeyType>(key), boost::forward<M>(obj));
  980. }
  981. return ret;
  982. }
  983. size_type erase(const key_type& k)
  984. {
  985. std::pair<iterator,iterator > itp = this->equal_range(k);
  986. size_type ret = static_cast<size_type>(itp.second-itp.first);
  987. if (ret){
  988. this->m_data.m_seq.erase(itp.first, itp.second);
  989. }
  990. return ret;
  991. }
  992. size_type erase_unique(const key_type& k)
  993. {
  994. const_iterator i = static_cast<const flat_tree &>(*this).find(k);
  995. size_type ret = static_cast<size_type>(i != this->cend());
  996. if (ret)
  997. this->erase(i);
  998. return ret;
  999. }
  1000. template <class K>
  1001. inline typename dtl::enable_if_c<
  1002. dtl::is_transparent<key_compare>::value && //transparent
  1003. !dtl::is_convertible<K, iterator>::value && //not convertible to iterator
  1004. !dtl::is_convertible<K, const_iterator>::value //not convertible to const_iterator
  1005. , size_type>::type
  1006. erase(BOOST_FWD_REF(K) key)
  1007. {
  1008. const typename remove_cvref<K>::type & k = key; //Support emulated rvalue references
  1009. std::pair<iterator, iterator > itp = this->equal_range(k);
  1010. size_type ret = static_cast<size_type>(itp.second - itp.first);
  1011. if (ret) {
  1012. this->m_data.m_seq.erase(itp.first, itp.second);
  1013. }
  1014. return ret;
  1015. }
  1016. template <class K>
  1017. inline typename dtl::enable_if_c<
  1018. dtl::is_transparent<key_compare>::value && //transparent
  1019. !dtl::is_convertible<K, iterator>::value && //not convertible to iterator
  1020. !dtl::is_convertible<K, const_iterator>::value //not convertible to const_iterator
  1021. , size_type>::type
  1022. erase_unique(const K& k)
  1023. {
  1024. const_iterator i = static_cast<const flat_tree&>(*this).find(k);
  1025. size_type ret = static_cast<size_type>(i != this->cend());
  1026. if (ret)
  1027. this->erase(i);
  1028. return ret;
  1029. }
  1030. inline iterator erase(const_iterator position)
  1031. { return this->m_data.m_seq.erase(position); }
  1032. inline iterator erase(const_iterator first, const_iterator last)
  1033. { return this->m_data.m_seq.erase(first, last); }
  1034. inline void clear()
  1035. { this->m_data.m_seq.clear(); }
  1036. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  1037. // with previous allocations. The size of the vector is unchanged
  1038. //!
  1039. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  1040. //!
  1041. //! <b>Complexity</b>: Linear to size().
  1042. inline void shrink_to_fit()
  1043. { this->m_data.m_seq.shrink_to_fit(); }
  1044. inline iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1045. {
  1046. const bool value = boost::container::dtl::
  1047. has_member_function_callable_with_nth<container_type, size_type>::value;
  1048. return flat_tree_nth<iterator>(this->m_data.m_seq, n, dtl::bool_<value>());
  1049. }
  1050. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1051. const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1052. {
  1053. const bool value = boost::container::dtl::
  1054. has_member_function_callable_with_nth<container_type, size_type>::value;
  1055. return flat_tree_nth<const_iterator>(this->m_data.m_seq, n, dtl::bool_<value>());
  1056. }
  1057. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1058. size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1059. {
  1060. const bool value = boost::container::dtl::
  1061. has_member_function_callable_with_index_of<container_type, iterator>::value;
  1062. return flat_tree_index_of(this->m_data.m_seq, p, dtl::bool_<value>());
  1063. }
  1064. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1065. size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  1066. {
  1067. const bool value = boost::container::dtl::
  1068. has_member_function_callable_with_index_of<container_type, const_iterator>::value;
  1069. return flat_tree_index_of(this->m_data.m_seq, p, dtl::bool_<value>());
  1070. }
  1071. // set operations:
  1072. BOOST_CONTAINER_ATTRIBUTE_NODISCARD
  1073. iterator find(const key_type& k)
  1074. {
  1075. iterator i = this->lower_bound(k);
  1076. iterator end_it = this->end();
  1077. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1078. i = end_it;
  1079. }
  1080. return i;
  1081. }
  1082. BOOST_CONTAINER_ATTRIBUTE_NODISCARD
  1083. const_iterator find(const key_type& k) const
  1084. {
  1085. const_iterator i = this->lower_bound(k);
  1086. const_iterator end_it = this->cend();
  1087. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1088. i = end_it;
  1089. }
  1090. return i;
  1091. }
  1092. template<class K>
  1093. BOOST_CONTAINER_ATTRIBUTE_NODISCARD
  1094. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1095. find(const K& k)
  1096. {
  1097. iterator i = this->lower_bound(k);
  1098. iterator end_it = this->end();
  1099. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1100. i = end_it;
  1101. }
  1102. return i;
  1103. }
  1104. template<class K>
  1105. BOOST_CONTAINER_ATTRIBUTE_NODISCARD
  1106. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1107. find(const K& k) const
  1108. {
  1109. const_iterator i = this->lower_bound(k);
  1110. const_iterator end_it = this->cend();
  1111. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1112. i = end_it;
  1113. }
  1114. return i;
  1115. }
  1116. BOOST_CONTAINER_ATTRIBUTE_NODISCARD
  1117. size_type count(const key_type& k) const
  1118. {
  1119. std::pair<const_iterator, const_iterator> p = this->equal_range(k);
  1120. size_type n = size_type(p.second - p.first);
  1121. return n;
  1122. }
  1123. template<class K>
  1124. BOOST_CONTAINER_ATTRIBUTE_NODISCARD
  1125. typename dtl::enable_if_transparent<key_compare, K, size_type>::type
  1126. count(const K& k) const
  1127. {
  1128. std::pair<const_iterator, const_iterator> p = this->equal_range(k);
  1129. size_type n = size_type(p.second - p.first);
  1130. return n;
  1131. }
  1132. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline bool contains(const key_type& x) const
  1133. { return this->find(x) != this->cend(); }
  1134. template<typename K>
  1135. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1136. typename dtl::enable_if_transparent<key_compare, K, bool>::type
  1137. contains(const K& x) const
  1138. { return this->find(x) != this->cend(); }
  1139. template<class C2>
  1140. inline void merge_unique(flat_tree<Value, KeyOfValue, C2, AllocatorOrContainer>& source)
  1141. {
  1142. this->insert_unique( boost::make_move_iterator(source.begin())
  1143. , boost::make_move_iterator(source.end()));
  1144. }
  1145. template<class C2>
  1146. inline void merge_equal(flat_tree<Value, KeyOfValue, C2, AllocatorOrContainer>& source)
  1147. {
  1148. this->insert_equal( boost::make_move_iterator(source.begin())
  1149. , boost::make_move_iterator(source.end()));
  1150. }
  1151. inline void merge_unique(flat_tree& source)
  1152. {
  1153. const bool value = boost::container::dtl::
  1154. has_member_function_callable_with_merge_unique<container_type, iterator, iterator, value_compare>::value;
  1155. (flat_tree_merge_unique)
  1156. ( this->m_data.m_seq
  1157. , boost::make_move_iterator(source.m_data.m_seq.begin())
  1158. , boost::make_move_iterator(source.m_data.m_seq.end())
  1159. , this->priv_value_comp()
  1160. , dtl::bool_<value>());
  1161. }
  1162. inline void merge_equal(flat_tree& source)
  1163. {
  1164. const bool value = boost::container::dtl::
  1165. has_member_function_callable_with_merge<container_type, iterator, iterator, value_compare>::value;
  1166. (flat_tree_merge_equal)
  1167. ( this->m_data.m_seq
  1168. , boost::make_move_iterator(source.m_data.m_seq.begin())
  1169. , boost::make_move_iterator(source.m_data.m_seq.end())
  1170. , this->priv_value_comp()
  1171. , dtl::bool_<value>());
  1172. }
  1173. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1174. iterator lower_bound(const key_type& k)
  1175. { return this->priv_lower_bound(this->begin(), this->end(), k); }
  1176. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1177. const_iterator lower_bound(const key_type& k) const
  1178. { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
  1179. template<class K>
  1180. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1181. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1182. lower_bound(const K& k)
  1183. { return this->priv_lower_bound(this->begin(), this->end(), k); }
  1184. template<class K>
  1185. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1186. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1187. lower_bound(const K& k) const
  1188. { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
  1189. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1190. iterator upper_bound(const key_type& k)
  1191. { return this->priv_upper_bound(this->begin(), this->end(), k); }
  1192. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1193. const_iterator upper_bound(const key_type& k) const
  1194. { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
  1195. template<class K>
  1196. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1197. typename dtl::enable_if_transparent<key_compare, K,iterator>::type
  1198. upper_bound(const K& k)
  1199. { return this->priv_upper_bound(this->begin(), this->end(), k); }
  1200. template<class K>
  1201. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1202. typename dtl::enable_if_transparent<key_compare, K,const_iterator>::type
  1203. upper_bound(const K& k) const
  1204. { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
  1205. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1206. std::pair<iterator,iterator> equal_range(const key_type& k)
  1207. { return this->priv_equal_range(this->begin(), this->end(), k); }
  1208. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1209. std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
  1210. { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
  1211. template<class K>
  1212. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1213. typename dtl::enable_if_transparent<key_compare, K, std::pair<iterator,iterator> >::type
  1214. equal_range(const K& k)
  1215. { return this->priv_equal_range(this->begin(), this->end(), k); }
  1216. template<class K>
  1217. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1218. typename dtl::enable_if_transparent<key_compare, K,std::pair<const_iterator,const_iterator> >::type
  1219. equal_range(const K& k) const
  1220. { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
  1221. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1222. std::pair<iterator, iterator> lower_bound_range(const key_type& k)
  1223. { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
  1224. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1225. std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
  1226. { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
  1227. template<class K>
  1228. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1229. typename dtl::enable_if_transparent<key_compare, K,std::pair<iterator,iterator> >::type
  1230. lower_bound_range(const K& k)
  1231. { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
  1232. template<class K>
  1233. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1234. typename dtl::enable_if_transparent<key_compare, K,std::pair<const_iterator,const_iterator> >::type
  1235. lower_bound_range(const K& k) const
  1236. { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
  1237. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1238. size_type capacity() const
  1239. {
  1240. const bool value = boost::container::dtl::
  1241. has_member_function_callable_with_capacity<container_type>::value;
  1242. return (flat_tree_capacity)(this->m_data.m_seq, dtl::bool_<value>());
  1243. }
  1244. inline
  1245. void reserve(size_type cnt)
  1246. {
  1247. const bool value = boost::container::dtl::
  1248. has_member_function_callable_with_reserve<container_type, size_type>::value;
  1249. (flat_tree_reserve)(this->m_data.m_seq, cnt, dtl::bool_<value>());
  1250. }
  1251. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1252. container_type extract_sequence()
  1253. { return boost::move(m_data.m_seq); }
  1254. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1255. container_type &get_sequence_ref()
  1256. { return m_data.m_seq; }
  1257. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1258. const container_type &get_sequence_cref() const
  1259. { return m_data.m_seq; }
  1260. inline void adopt_sequence_equal(BOOST_RV_REF(container_type) seq)
  1261. {
  1262. (flat_tree_adopt_sequence_equal)( m_data.m_seq, boost::move(seq), this->priv_value_comp()
  1263. , dtl::bool_<is_contiguous_container<container_type>::value>());
  1264. }
  1265. inline void adopt_sequence_unique(BOOST_RV_REF(container_type) seq)
  1266. {
  1267. (flat_tree_adopt_sequence_unique)(m_data.m_seq, boost::move(seq), this->priv_value_comp()
  1268. , dtl::bool_<is_contiguous_container<container_type>::value>());
  1269. }
  1270. void adopt_sequence_equal(ordered_range_t, BOOST_RV_REF(container_type) seq)
  1271. {
  1272. BOOST_ASSERT((is_sorted)(seq.cbegin(), seq.cend(), this->priv_value_comp()));
  1273. m_data.m_seq = boost::move(seq);
  1274. }
  1275. void adopt_sequence_unique(ordered_unique_range_t, BOOST_RV_REF(container_type) seq)
  1276. {
  1277. BOOST_ASSERT((is_sorted_and_unique)(seq.cbegin(), seq.cend(), this->priv_value_comp()));
  1278. m_data.m_seq = boost::move(seq);
  1279. }
  1280. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1281. friend bool operator==(const flat_tree& x, const flat_tree& y)
  1282. {
  1283. return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin());
  1284. }
  1285. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1286. friend bool operator<(const flat_tree& x, const flat_tree& y)
  1287. {
  1288. return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  1289. }
  1290. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1291. friend bool operator!=(const flat_tree& x, const flat_tree& y)
  1292. { return !(x == y); }
  1293. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1294. friend bool operator>(const flat_tree& x, const flat_tree& y)
  1295. { return y < x; }
  1296. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1297. friend bool operator<=(const flat_tree& x, const flat_tree& y)
  1298. { return !(y < x); }
  1299. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1300. friend bool operator>=(const flat_tree& x, const flat_tree& y)
  1301. { return !(x < y); }
  1302. inline friend void swap(flat_tree& x, flat_tree& y)
  1303. BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT(x.swap(y)))
  1304. { x.swap(y); }
  1305. private:
  1306. template <class InputIterator>
  1307. void priv_range_insertion_construct( bool unique_insertion, InputIterator first, InputIterator last)
  1308. {
  1309. //Use cend() as hint to achieve linear time for
  1310. //ordered ranges as required by the standard
  1311. //for the constructor
  1312. //Call end() every iteration as reallocation might have invalidated iterators
  1313. if(unique_insertion){
  1314. this->insert_unique(first, last);
  1315. }
  1316. else{
  1317. this->insert_equal (first, last);
  1318. }
  1319. }
  1320. inline bool priv_in_range_or_end(const_iterator pos) const
  1321. {
  1322. return (this->begin() <= pos) && (pos <= this->end());
  1323. }
  1324. // insert/erase
  1325. void priv_insert_equal_prepare
  1326. (const_iterator pos, const value_type& val, insert_commit_data &data)
  1327. {
  1328. // N1780
  1329. // To insert val at pos:
  1330. // if pos == end || val <= *pos
  1331. // if pos == begin || val >= *(pos-1)
  1332. // insert val before pos
  1333. // else
  1334. // insert val before upper_bound(val)
  1335. // else
  1336. // insert val before lower_bound(val)
  1337. const value_compare &val_cmp = this->m_data;
  1338. if(pos == this->cend() || !val_cmp(*pos, val)){
  1339. if (pos == this->cbegin() || !val_cmp(val, pos[-1])){
  1340. data.position = pos;
  1341. }
  1342. else{
  1343. data.position =
  1344. this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val));
  1345. }
  1346. }
  1347. else{
  1348. data.position =
  1349. this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val));
  1350. }
  1351. }
  1352. bool priv_insert_unique_prepare
  1353. (const_iterator b, const_iterator e, const key_type& k, insert_commit_data &commit_data)
  1354. {
  1355. const key_compare &key_cmp = this->priv_key_comp();
  1356. commit_data.position = this->priv_lower_bound(b, e, k);
  1357. return commit_data.position == e || key_cmp(k, KeyOfValue()(*commit_data.position));
  1358. }
  1359. inline bool priv_insert_unique_prepare
  1360. (const key_type& k, insert_commit_data &commit_data)
  1361. { return this->priv_insert_unique_prepare(this->cbegin(), this->cend(), k, commit_data); }
  1362. bool priv_insert_unique_prepare
  1363. (const_iterator pos, const key_type& k, insert_commit_data &commit_data)
  1364. {
  1365. //N1780. Props to Howard Hinnant!
  1366. //To insert k at pos:
  1367. //if pos == end || k <= *pos
  1368. // if pos == begin || k >= *(pos-1)
  1369. // insert k before pos
  1370. // else
  1371. // insert k before upper_bound(k)
  1372. //else if pos+1 == end || k <= *(pos+1)
  1373. // insert k after pos
  1374. //else
  1375. // insert k before lower_bound(k)
  1376. const key_compare &key_cmp = this->priv_key_comp();
  1377. const const_iterator cend_it = this->cend();
  1378. if(pos == cend_it || key_cmp(k, KeyOfValue()(*pos))){ //Check if k should go before end
  1379. const const_iterator cbeg = this->cbegin();
  1380. commit_data.position = pos;
  1381. if(pos == cbeg){ //If container is empty then insert it in the beginning
  1382. return true;
  1383. }
  1384. const_iterator prev(pos);
  1385. --prev;
  1386. if(key_cmp(KeyOfValue()(*prev), k)){ //If previous element was less, then it should go between prev and pos
  1387. return true;
  1388. }
  1389. else if(!key_cmp(k, KeyOfValue()(*prev))){ //If previous was equal then insertion should fail
  1390. commit_data.position = prev;
  1391. return false;
  1392. }
  1393. else{ //Previous was bigger so insertion hint was pointless, dispatch to hintless insertion
  1394. //but reduce the search between beg and prev as prev is bigger than k
  1395. return this->priv_insert_unique_prepare(cbeg, prev, k, commit_data);
  1396. }
  1397. }
  1398. else{
  1399. //The hint is before the insertion position, so insert it
  1400. //in the remaining range [pos, end)
  1401. return this->priv_insert_unique_prepare(pos, cend_it, k, commit_data);
  1402. }
  1403. }
  1404. template<class Convertible>
  1405. inline iterator priv_insert_commit
  1406. (insert_commit_data &commit_data, BOOST_FWD_REF(Convertible) convertible)
  1407. {
  1408. return this->m_data.m_seq.insert
  1409. ( commit_data.position
  1410. , boost::forward<Convertible>(convertible));
  1411. }
  1412. template <class RanIt, class K>
  1413. RanIt priv_lower_bound(RanIt first, const RanIt last,
  1414. const K & key) const
  1415. {
  1416. const Compare &key_cmp = this->m_data.get_comp();
  1417. KeyOfValue key_extract;
  1418. size_type len = static_cast<size_type>(last - first);
  1419. RanIt middle;
  1420. while (len) {
  1421. size_type step = len >> 1;
  1422. middle = first;
  1423. middle += difference_type(step);
  1424. if (key_cmp(key_extract(*middle), key)) {
  1425. first = ++middle;
  1426. len -= step + 1;
  1427. }
  1428. else{
  1429. len = step;
  1430. }
  1431. }
  1432. return first;
  1433. }
  1434. template <class RanIt, class K>
  1435. RanIt priv_upper_bound
  1436. (RanIt first, const RanIt last,const K & key) const
  1437. {
  1438. const Compare &key_cmp = this->m_data.get_comp();
  1439. KeyOfValue key_extract;
  1440. size_type len = static_cast<size_type>(last - first);
  1441. RanIt middle;
  1442. while (len) {
  1443. size_type step = len >> 1;
  1444. middle = first;
  1445. middle += difference_type(step);
  1446. if (key_cmp(key, key_extract(*middle))) {
  1447. len = step;
  1448. }
  1449. else{
  1450. first = ++middle;
  1451. len -= step + 1;
  1452. }
  1453. }
  1454. return first;
  1455. }
  1456. template <class RanIt, class K>
  1457. std::pair<RanIt, RanIt>
  1458. priv_equal_range(RanIt first, RanIt last, const K& key) const
  1459. {
  1460. const Compare &key_cmp = this->m_data.get_comp();
  1461. KeyOfValue key_extract;
  1462. size_type len = static_cast<size_type>(last - first);
  1463. RanIt middle;
  1464. while (len) {
  1465. size_type step = len >> 1;
  1466. middle = first;
  1467. middle += difference_type(step);
  1468. if (key_cmp(key_extract(*middle), key)){
  1469. first = ++middle;
  1470. len -= step + 1;
  1471. }
  1472. else if (key_cmp(key, key_extract(*middle))){
  1473. len = step;
  1474. }
  1475. else {
  1476. //Middle is equal to key
  1477. last = first;
  1478. last += difference_type(len);
  1479. RanIt const first_ret = this->priv_lower_bound(first, middle, key);
  1480. return std::pair<RanIt, RanIt>
  1481. ( first_ret, this->priv_upper_bound(++middle, last, key));
  1482. }
  1483. }
  1484. return std::pair<RanIt, RanIt>(first, first);
  1485. }
  1486. template<class RanIt, class K>
  1487. std::pair<RanIt, RanIt> priv_lower_bound_range(RanIt first, RanIt last, const K& k) const
  1488. {
  1489. const Compare &key_cmp = this->m_data.get_comp();
  1490. KeyOfValue key_extract;
  1491. RanIt lb(this->priv_lower_bound(first, last, k)), ub(lb);
  1492. if(lb != last && !key_cmp(k, key_extract(*lb))){
  1493. ++ub;
  1494. }
  1495. return std::pair<RanIt, RanIt>(lb, ub);
  1496. }
  1497. };
  1498. } //namespace dtl {
  1499. } //namespace container {
  1500. //!has_trivial_destructor_after_move<> == true_type
  1501. //!specialization for optimizations
  1502. template <class T, class KeyOfValue,
  1503. class Compare, class AllocatorOrContainer>
  1504. struct has_trivial_destructor_after_move<boost::container::dtl::flat_tree<T, KeyOfValue, Compare, AllocatorOrContainer> >
  1505. {
  1506. typedef boost::container::dtl::flat_tree<T, KeyOfValue, Compare, AllocatorOrContainer> flat_tree;
  1507. typedef typename flat_tree::container_type container_type;
  1508. typedef typename flat_tree::key_compare key_compare;
  1509. BOOST_STATIC_CONSTEXPR bool value = ::boost::has_trivial_destructor_after_move<container_type>::value &&
  1510. ::boost::has_trivial_destructor_after_move<key_compare>::value;
  1511. };
  1512. } //namespace boost {
  1513. #include <boost/container/detail/config_end.hpp>
  1514. #endif // BOOST_CONTAINER_FLAT_TREE_HPP