flat_tree.hpp 63 KB

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