flat_tree.hpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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_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. //Step 4: merge both ranges
  747. (flat_tree_container_inplace_merge)(seq, it, this->priv_value_comp(), contiguous_tag);
  748. }
  749. template <class InIt>
  750. void insert_equal(InIt first, InIt last)
  751. {
  752. dtl::bool_<is_contiguous_container<container_type>::value> contiguous_tag;
  753. container_type &seq = this->m_data.m_seq;
  754. typename container_type::iterator const it = seq.insert(seq.cend(), first, last);
  755. (flat_tree_container_inplace_sort_ending)(seq, it, this->priv_value_comp(), contiguous_tag);
  756. (flat_tree_container_inplace_merge) (seq, it, this->priv_value_comp(), contiguous_tag);
  757. }
  758. //Ordered
  759. template <class InIt>
  760. void insert_equal(ordered_range_t, InIt first, InIt last)
  761. {
  762. const bool value = boost::container::dtl::
  763. has_member_function_callable_with_merge_unique<container_type, InIt, InIt, value_compare>::value;
  764. (flat_tree_merge_equal)(this->m_data.m_seq, first, last, this->priv_value_comp(), dtl::bool_<value>());
  765. }
  766. template <class InIt>
  767. void insert_unique(ordered_unique_range_t, InIt first, InIt last)
  768. {
  769. const bool value = boost::container::dtl::
  770. has_member_function_callable_with_merge_unique<container_type, InIt, InIt, value_compare>::value;
  771. (flat_tree_merge_unique)(this->m_data.m_seq, first, last, this->priv_value_comp(), dtl::bool_<value>());
  772. }
  773. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  774. template <class... Args>
  775. std::pair<iterator, bool> emplace_unique(BOOST_FWD_REF(Args)... args)
  776. {
  777. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  778. value_type *pval = reinterpret_cast<value_type *>(v.data);
  779. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  780. stored_allocator_traits::construct(a, pval, ::boost::forward<Args>(args)... );
  781. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  782. return this->insert_unique(::boost::move(*pval));
  783. }
  784. template <class... Args>
  785. iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
  786. {
  787. //hint checked in insert_unique
  788. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  789. value_type *pval = reinterpret_cast<value_type *>(v.data);
  790. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  791. stored_allocator_traits::construct(a, pval, ::boost::forward<Args>(args)... );
  792. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  793. return this->insert_unique(hint, ::boost::move(*pval));
  794. }
  795. template <class... Args>
  796. iterator emplace_equal(BOOST_FWD_REF(Args)... args)
  797. {
  798. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  799. value_type *pval = reinterpret_cast<value_type *>(v.data);
  800. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  801. stored_allocator_traits::construct(a, pval, ::boost::forward<Args>(args)... );
  802. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  803. return this->insert_equal(::boost::move(*pval));
  804. }
  805. template <class... Args>
  806. iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
  807. {
  808. //hint checked in insert_equal
  809. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  810. value_type *pval = reinterpret_cast<value_type *>(v.data);
  811. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  812. stored_allocator_traits::construct(a, pval, ::boost::forward<Args>(args)... );
  813. value_destructor<stored_allocator_type, value_type> d(a, *pval);
  814. return this->insert_equal(hint, ::boost::move(*pval));
  815. }
  816. template <class KeyType, class... Args>
  817. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool> try_emplace
  818. (const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(Args)... args)
  819. {
  820. std::pair<iterator,bool> ret;
  821. insert_commit_data data;
  822. const key_type & k = key;
  823. ret.second = hint == const_iterator()
  824. ? this->priv_insert_unique_prepare(k, data)
  825. : this->priv_insert_unique_prepare(hint, k, data);
  826. if(!ret.second){
  827. ret.first = this->nth(data.position - this->cbegin());
  828. }
  829. else{
  830. typedef typename emplace_functor_type<try_emplace_t, KeyType, Args...>::type func_t;
  831. typedef emplace_iterator<value_type, func_t, difference_type> it_t;
  832. func_t func(try_emplace_t(), ::boost::forward<KeyType>(key), ::boost::forward<Args>(args)...);
  833. ret.first = this->m_data.m_seq.insert(data.position, it_t(func), it_t());
  834. }
  835. return ret;
  836. }
  837. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  838. #define BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE(N) \
  839. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  840. std::pair<iterator, bool> emplace_unique(BOOST_MOVE_UREF##N)\
  841. {\
  842. typename aligned_storage<sizeof(value_type), 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_MOVE_I##N BOOST_MOVE_FWD##N);\
  846. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  847. return this->insert_unique(::boost::move(*pval));\
  848. }\
  849. \
  850. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  851. iterator emplace_hint_unique(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  852. {\
  853. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  854. value_type *pval = reinterpret_cast<value_type *>(v.data);\
  855. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  856. stored_allocator_traits::construct(a, pval BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  857. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  858. return this->insert_unique(hint, ::boost::move(*pval));\
  859. }\
  860. \
  861. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  862. iterator emplace_equal(BOOST_MOVE_UREF##N)\
  863. {\
  864. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  865. value_type *pval = reinterpret_cast<value_type *>(v.data);\
  866. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  867. stored_allocator_traits::construct(a, pval BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  868. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  869. return this->insert_equal(::boost::move(*pval));\
  870. }\
  871. \
  872. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  873. iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  874. {\
  875. typename aligned_storage <sizeof(value_type), alignment_of<value_type>::value>::type v;\
  876. value_type *pval = reinterpret_cast<value_type *>(v.data);\
  877. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  878. stored_allocator_traits::construct(a, pval BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  879. value_destructor<stored_allocator_type, value_type> d(a, *pval);\
  880. return this->insert_equal(hint, ::boost::move(*pval));\
  881. }\
  882. template <class KeyType BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  883. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool>\
  884. try_emplace(const_iterator hint, BOOST_FWD_REF(KeyType) key BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  885. {\
  886. std::pair<iterator,bool> ret;\
  887. insert_commit_data data;\
  888. const key_type & k = key;\
  889. ret.second = hint == const_iterator()\
  890. ? this->priv_insert_unique_prepare(k, data)\
  891. : this->priv_insert_unique_prepare(hint, k, data);\
  892. \
  893. if(!ret.second){\
  894. ret.first = this->nth(data.position - this->cbegin());\
  895. }\
  896. else{\
  897. typedef typename emplace_functor_type<try_emplace_t, KeyType BOOST_MOVE_I##N BOOST_MOVE_TARG##N>::type func_t;\
  898. typedef emplace_iterator<value_type, func_t, difference_type> it_t;\
  899. func_t func(try_emplace_t(), ::boost::forward<KeyType>(key) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  900. ret.first = this->m_data.m_seq.insert(data.position, it_t(func), it_t());\
  901. }\
  902. return ret;\
  903. }\
  904. //
  905. BOOST_MOVE_ITERATE_0TO7(BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE)
  906. #undef BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE
  907. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  908. template<class KeyType, class M>
  909. std::pair<iterator, bool> insert_or_assign(const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(M) obj)
  910. {
  911. const key_type& k = key;
  912. std::pair<iterator,bool> ret;
  913. insert_commit_data data;
  914. ret.second = hint == const_iterator()
  915. ? this->priv_insert_unique_prepare(k, data)
  916. : this->priv_insert_unique_prepare(hint, k, data);
  917. if(!ret.second){
  918. ret.first = this->nth(data.position - this->cbegin());
  919. ret.first->second = boost::forward<M>(obj);
  920. }
  921. else{
  922. typedef typename emplace_functor_type<KeyType, M>::type func_t;
  923. typedef emplace_iterator<value_type, func_t, difference_type> it_t;
  924. func_t func(boost::forward<KeyType>(key), boost::forward<M>(obj));
  925. ret.first = this->m_data.m_seq.insert(data.position, it_t(func), it_t());
  926. }
  927. return ret;
  928. }
  929. BOOST_CONTAINER_FORCEINLINE iterator erase(const_iterator position)
  930. { return this->m_data.m_seq.erase(position); }
  931. size_type erase(const key_type& k)
  932. {
  933. std::pair<iterator,iterator > itp = this->equal_range(k);
  934. size_type ret = static_cast<size_type>(itp.second-itp.first);
  935. if (ret){
  936. this->m_data.m_seq.erase(itp.first, itp.second);
  937. }
  938. return ret;
  939. }
  940. BOOST_CONTAINER_FORCEINLINE iterator erase(const_iterator first, const_iterator last)
  941. { return this->m_data.m_seq.erase(first, last); }
  942. BOOST_CONTAINER_FORCEINLINE void clear()
  943. { this->m_data.m_seq.clear(); }
  944. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  945. // with previous allocations. The size of the vector is unchanged
  946. //!
  947. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  948. //!
  949. //! <b>Complexity</b>: Linear to size().
  950. BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
  951. { this->m_data.m_seq.shrink_to_fit(); }
  952. BOOST_CONTAINER_FORCEINLINE iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  953. {
  954. const bool value = boost::container::dtl::
  955. has_member_function_callable_with_nth<container_type, size_type>::value;
  956. return flat_tree_nth<iterator>(this->m_data.m_seq, n, dtl::bool_<value>());
  957. }
  958. BOOST_CONTAINER_FORCEINLINE const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  959. {
  960. const bool value = boost::container::dtl::
  961. has_member_function_callable_with_nth<container_type, size_type>::value;
  962. return flat_tree_nth<const_iterator>(this->m_data.m_seq, n, dtl::bool_<value>());
  963. }
  964. BOOST_CONTAINER_FORCEINLINE size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  965. {
  966. const bool value = boost::container::dtl::
  967. has_member_function_callable_with_index_of<container_type, iterator>::value;
  968. return flat_tree_index_of(this->m_data.m_seq, p, dtl::bool_<value>());
  969. }
  970. BOOST_CONTAINER_FORCEINLINE size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  971. {
  972. const bool value = boost::container::dtl::
  973. has_member_function_callable_with_index_of<container_type, const_iterator>::value;
  974. return flat_tree_index_of(this->m_data.m_seq, p, dtl::bool_<value>());
  975. }
  976. // set operations:
  977. iterator find(const key_type& k)
  978. {
  979. iterator i = this->lower_bound(k);
  980. iterator end_it = this->end();
  981. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  982. i = end_it;
  983. }
  984. return i;
  985. }
  986. const_iterator find(const key_type& k) const
  987. {
  988. const_iterator i = this->lower_bound(k);
  989. const_iterator end_it = this->cend();
  990. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  991. i = end_it;
  992. }
  993. return i;
  994. }
  995. template<class K>
  996. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  997. find(const K& k)
  998. {
  999. iterator i = this->lower_bound(k);
  1000. iterator end_it = this->end();
  1001. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1002. i = end_it;
  1003. }
  1004. return i;
  1005. }
  1006. template<class K>
  1007. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1008. find(const K& k) const
  1009. {
  1010. const_iterator i = this->lower_bound(k);
  1011. const_iterator end_it = this->cend();
  1012. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1013. i = end_it;
  1014. }
  1015. return i;
  1016. }
  1017. size_type count(const key_type& k) const
  1018. {
  1019. std::pair<const_iterator, const_iterator> p = this->equal_range(k);
  1020. size_type n = p.second - p.first;
  1021. return n;
  1022. }
  1023. template<class K>
  1024. typename dtl::enable_if_transparent<key_compare, K, size_type>::type
  1025. count(const K& k) const
  1026. {
  1027. std::pair<const_iterator, const_iterator> p = this->equal_range(k);
  1028. size_type n = p.second - p.first;
  1029. return n;
  1030. }
  1031. BOOST_CONTAINER_FORCEINLINE bool contains(const key_type& x) const
  1032. { return this->find(x) != this->cend(); }
  1033. template<typename K>
  1034. BOOST_CONTAINER_FORCEINLINE
  1035. typename dtl::enable_if_transparent<key_compare, K, bool>::type
  1036. contains(const K& x) const
  1037. { return this->find(x) != this->cend(); }
  1038. template<class C2>
  1039. BOOST_CONTAINER_FORCEINLINE void merge_unique(flat_tree<Value, KeyOfValue, C2, AllocatorOrContainer>& source)
  1040. {
  1041. this->insert_unique( boost::make_move_iterator(source.begin())
  1042. , boost::make_move_iterator(source.end()));
  1043. }
  1044. template<class C2>
  1045. BOOST_CONTAINER_FORCEINLINE void merge_equal(flat_tree<Value, KeyOfValue, C2, AllocatorOrContainer>& source)
  1046. {
  1047. this->insert_equal( boost::make_move_iterator(source.begin())
  1048. , boost::make_move_iterator(source.end()));
  1049. }
  1050. BOOST_CONTAINER_FORCEINLINE void merge_unique(flat_tree& source)
  1051. {
  1052. const bool value = boost::container::dtl::
  1053. has_member_function_callable_with_merge_unique<container_type, iterator, iterator, value_compare>::value;
  1054. (flat_tree_merge_unique)
  1055. ( this->m_data.m_seq
  1056. , boost::make_move_iterator(source.m_data.m_seq.begin())
  1057. , boost::make_move_iterator(source.m_data.m_seq.end())
  1058. , this->priv_value_comp()
  1059. , dtl::bool_<value>());
  1060. }
  1061. BOOST_CONTAINER_FORCEINLINE void merge_equal(flat_tree& source)
  1062. {
  1063. const bool value = boost::container::dtl::
  1064. has_member_function_callable_with_merge<container_type, iterator, iterator, value_compare>::value;
  1065. (flat_tree_merge_equal)
  1066. ( this->m_data.m_seq
  1067. , boost::make_move_iterator(source.m_data.m_seq.begin())
  1068. , boost::make_move_iterator(source.m_data.m_seq.end())
  1069. , this->priv_value_comp()
  1070. , dtl::bool_<value>());
  1071. }
  1072. BOOST_CONTAINER_FORCEINLINE iterator lower_bound(const key_type& k)
  1073. { return this->priv_lower_bound(this->begin(), this->end(), k); }
  1074. BOOST_CONTAINER_FORCEINLINE const_iterator lower_bound(const key_type& k) const
  1075. { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
  1076. template<class K>
  1077. BOOST_CONTAINER_FORCEINLINE
  1078. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1079. lower_bound(const K& k)
  1080. { return this->priv_lower_bound(this->begin(), this->end(), k); }
  1081. template<class K>
  1082. BOOST_CONTAINER_FORCEINLINE
  1083. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1084. lower_bound(const K& k) const
  1085. { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
  1086. BOOST_CONTAINER_FORCEINLINE iterator upper_bound(const key_type& k)
  1087. { return this->priv_upper_bound(this->begin(), this->end(), k); }
  1088. BOOST_CONTAINER_FORCEINLINE const_iterator upper_bound(const key_type& k) const
  1089. { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
  1090. template<class K>
  1091. BOOST_CONTAINER_FORCEINLINE
  1092. typename dtl::enable_if_transparent<key_compare, K,iterator>::type
  1093. upper_bound(const K& k)
  1094. { return this->priv_upper_bound(this->begin(), this->end(), k); }
  1095. template<class K>
  1096. BOOST_CONTAINER_FORCEINLINE
  1097. typename dtl::enable_if_transparent<key_compare, K,const_iterator>::type
  1098. upper_bound(const K& k) const
  1099. { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
  1100. BOOST_CONTAINER_FORCEINLINE std::pair<iterator,iterator> equal_range(const key_type& k)
  1101. { return this->priv_equal_range(this->begin(), this->end(), k); }
  1102. BOOST_CONTAINER_FORCEINLINE std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
  1103. { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
  1104. template<class K>
  1105. BOOST_CONTAINER_FORCEINLINE
  1106. typename dtl::enable_if_transparent<key_compare, K,std::pair<iterator,iterator> >::type
  1107. equal_range(const K& k)
  1108. { return this->priv_equal_range(this->begin(), this->end(), k); }
  1109. template<class K>
  1110. BOOST_CONTAINER_FORCEINLINE
  1111. typename dtl::enable_if_transparent<key_compare, K,std::pair<const_iterator,const_iterator> >::type
  1112. equal_range(const K& k) const
  1113. { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
  1114. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, iterator> lower_bound_range(const key_type& k)
  1115. { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
  1116. BOOST_CONTAINER_FORCEINLINE std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
  1117. { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
  1118. template<class K>
  1119. BOOST_CONTAINER_FORCEINLINE
  1120. typename dtl::enable_if_transparent<key_compare, K,std::pair<iterator,iterator> >::type
  1121. lower_bound_range(const K& k)
  1122. { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
  1123. template<class K>
  1124. BOOST_CONTAINER_FORCEINLINE
  1125. typename dtl::enable_if_transparent<key_compare, K,std::pair<const_iterator,const_iterator> >::type
  1126. lower_bound_range(const K& k) const
  1127. { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
  1128. BOOST_CONTAINER_FORCEINLINE size_type capacity() const
  1129. {
  1130. const bool value = boost::container::dtl::
  1131. has_member_function_callable_with_capacity<container_type>::value;
  1132. return (flat_tree_capacity)(this->m_data.m_seq, dtl::bool_<value>());
  1133. }
  1134. BOOST_CONTAINER_FORCEINLINE void reserve(size_type cnt)
  1135. {
  1136. const bool value = boost::container::dtl::
  1137. has_member_function_callable_with_reserve<container_type, size_type>::value;
  1138. (flat_tree_reserve)(this->m_data.m_seq, cnt, dtl::bool_<value>());
  1139. }
  1140. BOOST_CONTAINER_FORCEINLINE container_type extract_sequence()
  1141. {
  1142. return boost::move(m_data.m_seq);
  1143. }
  1144. BOOST_CONTAINER_FORCEINLINE container_type &get_sequence_ref()
  1145. {
  1146. return m_data.m_seq;
  1147. }
  1148. BOOST_CONTAINER_FORCEINLINE void adopt_sequence_equal(BOOST_RV_REF(container_type) seq)
  1149. {
  1150. (flat_tree_adopt_sequence_equal)( m_data.m_seq, boost::move(seq), this->priv_value_comp()
  1151. , dtl::bool_<is_contiguous_container<container_type>::value>());
  1152. }
  1153. BOOST_CONTAINER_FORCEINLINE void adopt_sequence_unique(BOOST_RV_REF(container_type) seq)
  1154. {
  1155. (flat_tree_adopt_sequence_unique)(m_data.m_seq, boost::move(seq), this->priv_value_comp()
  1156. , dtl::bool_<is_contiguous_container<container_type>::value>());
  1157. }
  1158. void adopt_sequence_equal(ordered_range_t, BOOST_RV_REF(container_type) seq)
  1159. {
  1160. BOOST_ASSERT((is_sorted)(seq.cbegin(), seq.cend(), this->priv_value_comp()));
  1161. m_data.m_seq = boost::move(seq);
  1162. }
  1163. void adopt_sequence_unique(ordered_unique_range_t, BOOST_RV_REF(container_type) seq)
  1164. {
  1165. BOOST_ASSERT((is_sorted_and_unique)(seq.cbegin(), seq.cend(), this->priv_value_comp()));
  1166. m_data.m_seq = boost::move(seq);
  1167. }
  1168. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const flat_tree& x, const flat_tree& y)
  1169. {
  1170. return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin());
  1171. }
  1172. BOOST_CONTAINER_FORCEINLINE friend bool operator<(const flat_tree& x, const flat_tree& y)
  1173. {
  1174. return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  1175. }
  1176. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const flat_tree& x, const flat_tree& y)
  1177. { return !(x == y); }
  1178. BOOST_CONTAINER_FORCEINLINE friend bool operator>(const flat_tree& x, const flat_tree& y)
  1179. { return y < x; }
  1180. BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const flat_tree& x, const flat_tree& y)
  1181. { return !(y < x); }
  1182. BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const flat_tree& x, const flat_tree& y)
  1183. { return !(x < y); }
  1184. BOOST_CONTAINER_FORCEINLINE friend void swap(flat_tree& x, flat_tree& y)
  1185. { x.swap(y); }
  1186. private:
  1187. template <class InputIterator>
  1188. void priv_range_insertion_construct( bool unique_insertion, InputIterator first, InputIterator last)
  1189. {
  1190. //Use cend() as hint to achieve linear time for
  1191. //ordered ranges as required by the standard
  1192. //for the constructor
  1193. //Call end() every iteration as reallocation might have invalidated iterators
  1194. if(unique_insertion){
  1195. this->insert_unique(first, last);
  1196. }
  1197. else{
  1198. this->insert_equal (first, last);
  1199. }
  1200. }
  1201. BOOST_CONTAINER_FORCEINLINE bool priv_in_range_or_end(const_iterator pos) const
  1202. {
  1203. return (this->begin() <= pos) && (pos <= this->end());
  1204. }
  1205. // insert/erase
  1206. void priv_insert_equal_prepare
  1207. (const_iterator pos, const value_type& val, insert_commit_data &data)
  1208. {
  1209. // N1780
  1210. // To insert val at pos:
  1211. // if pos == end || val <= *pos
  1212. // if pos == begin || val >= *(pos-1)
  1213. // insert val before pos
  1214. // else
  1215. // insert val before upper_bound(val)
  1216. // else
  1217. // insert val before lower_bound(val)
  1218. const value_compare &val_cmp = this->m_data;
  1219. if(pos == this->cend() || !val_cmp(*pos, val)){
  1220. if (pos == this->cbegin() || !val_cmp(val, pos[-1])){
  1221. data.position = pos;
  1222. }
  1223. else{
  1224. data.position =
  1225. this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val));
  1226. }
  1227. }
  1228. else{
  1229. data.position =
  1230. this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val));
  1231. }
  1232. }
  1233. bool priv_insert_unique_prepare
  1234. (const_iterator b, const_iterator e, const key_type& k, insert_commit_data &commit_data)
  1235. {
  1236. const key_compare &key_cmp = this->priv_key_comp();
  1237. commit_data.position = this->priv_lower_bound(b, e, k);
  1238. return commit_data.position == e || key_cmp(k, KeyOfValue()(*commit_data.position));
  1239. }
  1240. BOOST_CONTAINER_FORCEINLINE bool priv_insert_unique_prepare
  1241. (const key_type& k, insert_commit_data &commit_data)
  1242. { return this->priv_insert_unique_prepare(this->cbegin(), this->cend(), k, commit_data); }
  1243. bool priv_insert_unique_prepare
  1244. (const_iterator pos, const key_type& k, insert_commit_data &commit_data)
  1245. {
  1246. //N1780. Props to Howard Hinnant!
  1247. //To insert k at pos:
  1248. //if pos == end || k <= *pos
  1249. // if pos == begin || k >= *(pos-1)
  1250. // insert k before pos
  1251. // else
  1252. // insert k before upper_bound(k)
  1253. //else if pos+1 == end || k <= *(pos+1)
  1254. // insert k after pos
  1255. //else
  1256. // insert k before lower_bound(k)
  1257. const key_compare &key_cmp = this->priv_key_comp();
  1258. const const_iterator cend_it = this->cend();
  1259. if(pos == cend_it || key_cmp(k, KeyOfValue()(*pos))){ //Check if k should go before end
  1260. const const_iterator cbeg = this->cbegin();
  1261. commit_data.position = pos;
  1262. if(pos == cbeg){ //If container is empty then insert it in the beginning
  1263. return true;
  1264. }
  1265. const_iterator prev(pos);
  1266. --prev;
  1267. if(key_cmp(KeyOfValue()(*prev), k)){ //If previous element was less, then it should go between prev and pos
  1268. return true;
  1269. }
  1270. else if(!key_cmp(k, KeyOfValue()(*prev))){ //If previous was equal then insertion should fail
  1271. commit_data.position = prev;
  1272. return false;
  1273. }
  1274. else{ //Previous was bigger so insertion hint was pointless, dispatch to hintless insertion
  1275. //but reduce the search between beg and prev as prev is bigger than k
  1276. return this->priv_insert_unique_prepare(cbeg, prev, k, commit_data);
  1277. }
  1278. }
  1279. else{
  1280. //The hint is before the insertion position, so insert it
  1281. //in the remaining range [pos, end)
  1282. return this->priv_insert_unique_prepare(pos, cend_it, k, commit_data);
  1283. }
  1284. }
  1285. template<class Convertible>
  1286. BOOST_CONTAINER_FORCEINLINE iterator priv_insert_commit
  1287. (insert_commit_data &commit_data, BOOST_FWD_REF(Convertible) convertible)
  1288. {
  1289. return this->m_data.m_seq.insert
  1290. ( commit_data.position
  1291. , boost::forward<Convertible>(convertible));
  1292. }
  1293. template <class RanIt, class K>
  1294. RanIt priv_lower_bound(RanIt first, const RanIt last,
  1295. const K & key) const
  1296. {
  1297. const Compare &key_cmp = this->m_data.get_comp();
  1298. KeyOfValue key_extract;
  1299. size_type len = static_cast<size_type>(last - first);
  1300. RanIt middle;
  1301. while (len) {
  1302. size_type step = len >> 1;
  1303. middle = first;
  1304. middle += step;
  1305. if (key_cmp(key_extract(*middle), key)) {
  1306. first = ++middle;
  1307. len -= step + 1;
  1308. }
  1309. else{
  1310. len = step;
  1311. }
  1312. }
  1313. return first;
  1314. }
  1315. template <class RanIt, class K>
  1316. RanIt priv_upper_bound
  1317. (RanIt first, const RanIt last,const K & key) const
  1318. {
  1319. const Compare &key_cmp = this->m_data.get_comp();
  1320. KeyOfValue key_extract;
  1321. size_type len = static_cast<size_type>(last - first);
  1322. RanIt middle;
  1323. while (len) {
  1324. size_type step = len >> 1;
  1325. middle = first;
  1326. middle += step;
  1327. if (key_cmp(key, key_extract(*middle))) {
  1328. len = step;
  1329. }
  1330. else{
  1331. first = ++middle;
  1332. len -= step + 1;
  1333. }
  1334. }
  1335. return first;
  1336. }
  1337. template <class RanIt, class K>
  1338. std::pair<RanIt, RanIt>
  1339. priv_equal_range(RanIt first, RanIt last, const K& key) const
  1340. {
  1341. const Compare &key_cmp = this->m_data.get_comp();
  1342. KeyOfValue key_extract;
  1343. size_type len = static_cast<size_type>(last - first);
  1344. RanIt middle;
  1345. while (len) {
  1346. size_type step = len >> 1;
  1347. middle = first;
  1348. middle += step;
  1349. if (key_cmp(key_extract(*middle), key)){
  1350. first = ++middle;
  1351. len -= step + 1;
  1352. }
  1353. else if (key_cmp(key, key_extract(*middle))){
  1354. len = step;
  1355. }
  1356. else {
  1357. //Middle is equal to key
  1358. last = first;
  1359. last += len;
  1360. RanIt const first_ret = this->priv_lower_bound(first, middle, key);
  1361. return std::pair<RanIt, RanIt>
  1362. ( first_ret, this->priv_upper_bound(++middle, last, key));
  1363. }
  1364. }
  1365. return std::pair<RanIt, RanIt>(first, first);
  1366. }
  1367. template<class RanIt, class K>
  1368. std::pair<RanIt, RanIt> priv_lower_bound_range(RanIt first, RanIt last, const K& k) const
  1369. {
  1370. const Compare &key_cmp = this->m_data.get_comp();
  1371. KeyOfValue key_extract;
  1372. RanIt lb(this->priv_lower_bound(first, last, k)), ub(lb);
  1373. if(lb != last && static_cast<difference_type>(!key_cmp(k, key_extract(*lb)))){
  1374. ++ub;
  1375. }
  1376. return std::pair<RanIt, RanIt>(lb, ub);
  1377. }
  1378. };
  1379. } //namespace dtl {
  1380. } //namespace container {
  1381. //!has_trivial_destructor_after_move<> == true_type
  1382. //!specialization for optimizations
  1383. template <class T, class KeyOfValue,
  1384. class Compare, class AllocatorOrContainer>
  1385. struct has_trivial_destructor_after_move<boost::container::dtl::flat_tree<T, KeyOfValue, Compare, AllocatorOrContainer> >
  1386. {
  1387. typedef typename boost::container::dtl::select_container_type<T, AllocatorOrContainer>::type container_type;
  1388. typedef typename container_type::allocator_type allocator_t;
  1389. typedef typename ::boost::container::allocator_traits<allocator_t>::pointer pointer;
  1390. static const bool value = ::boost::has_trivial_destructor_after_move<allocator_t>::value &&
  1391. ::boost::has_trivial_destructor_after_move<pointer>::value;
  1392. };
  1393. } //namespace boost {
  1394. #include <boost/container/detail/config_end.hpp>
  1395. #endif // BOOST_CONTAINER_FLAT_TREE_HPP