flat_tree.hpp 66 KB

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