flat_tree.hpp 60 KB

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