flat_tree.hpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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. struct select_container_type
  384. {
  385. typedef AllocatorOrContainer type;
  386. };
  387. template <class Value, class AllocatorOrContainer>
  388. struct select_container_type<Value, AllocatorOrContainer, false>
  389. {
  390. typedef boost::container::vector<Value, AllocatorOrContainer> type;
  391. };
  392. ///////////////////////////////////////
  393. //
  394. // flat_tree
  395. //
  396. ///////////////////////////////////////
  397. template <class Value, class KeyOfValue,
  398. class Compare, class AllocatorOrContainer>
  399. class flat_tree
  400. {
  401. public:
  402. typedef typename select_container_type<Value, AllocatorOrContainer>::type container_type;
  403. typedef container_type sequence_type; //For backwards compatibility
  404. private:
  405. typedef typename container_type::allocator_type allocator_t;
  406. typedef allocator_traits<allocator_t> allocator_traits_type;
  407. public:
  408. typedef flat_tree_value_compare<Compare, Value, KeyOfValue> value_compare;
  409. private:
  410. struct Data
  411. //Inherit from value_compare to do EBO
  412. : public value_compare
  413. {
  414. BOOST_COPYABLE_AND_MOVABLE(Data)
  415. public:
  416. Data()
  417. : value_compare(), m_seq()
  418. {}
  419. explicit Data(const allocator_t &alloc)
  420. : value_compare(), m_seq(alloc)
  421. {}
  422. explicit Data(const Compare &comp)
  423. : value_compare(comp), m_seq()
  424. {}
  425. Data(const Compare &comp, const allocator_t &alloc)
  426. : value_compare(comp), m_seq(alloc)
  427. {}
  428. explicit Data(const Data &d)
  429. : value_compare(static_cast<const value_compare&>(d)), m_seq(d.m_seq)
  430. {}
  431. Data(BOOST_RV_REF(Data) d)
  432. : value_compare(boost::move(static_cast<value_compare&>(d))), m_seq(boost::move(d.m_seq))
  433. {}
  434. Data(const Data &d, const allocator_t &a)
  435. : value_compare(static_cast<const value_compare&>(d)), m_seq(d.m_seq, a)
  436. {}
  437. Data(BOOST_RV_REF(Data) d, const allocator_t &a)
  438. : value_compare(boost::move(static_cast<value_compare&>(d))), m_seq(boost::move(d.m_seq), a)
  439. {}
  440. Data& operator=(BOOST_COPY_ASSIGN_REF(Data) d)
  441. {
  442. this->value_compare::operator=(d);
  443. m_seq = d.m_seq;
  444. return *this;
  445. }
  446. Data& operator=(BOOST_RV_REF(Data) d)
  447. {
  448. this->value_compare::operator=(boost::move(static_cast<value_compare &>(d)));
  449. m_seq = boost::move(d.m_seq);
  450. return *this;
  451. }
  452. void swap(Data &d)
  453. {
  454. value_compare& mycomp = *this, & othercomp = d;
  455. boost::adl_move_swap(mycomp, othercomp);
  456. this->m_seq.swap(d.m_seq);
  457. }
  458. container_type m_seq;
  459. };
  460. Data m_data;
  461. BOOST_COPYABLE_AND_MOVABLE(flat_tree)
  462. public:
  463. typedef typename container_type::value_type value_type;
  464. typedef typename container_type::pointer pointer;
  465. typedef typename container_type::const_pointer const_pointer;
  466. typedef typename container_type::reference reference;
  467. typedef typename container_type::const_reference const_reference;
  468. typedef typename KeyOfValue::type key_type;
  469. typedef Compare key_compare;
  470. typedef typename container_type::allocator_type allocator_type;
  471. typedef typename container_type::size_type size_type;
  472. typedef typename container_type::difference_type difference_type;
  473. typedef typename container_type::iterator iterator;
  474. typedef typename container_type::const_iterator const_iterator;
  475. typedef typename container_type::reverse_iterator reverse_iterator;
  476. typedef typename container_type::const_reverse_iterator const_reverse_iterator;
  477. //!Standard extension
  478. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  479. (boost::container::dtl::, container_type
  480. ,stored_allocator_type, allocator_type) stored_allocator_type;
  481. static const bool has_stored_allocator_type =
  482. BOOST_INTRUSIVE_HAS_TYPE(boost::container::dtl::, container_type, stored_allocator_type);
  483. private:
  484. typedef allocator_traits<stored_allocator_type> stored_allocator_traits;
  485. public:
  486. typedef typename dtl::if_c
  487. <has_stored_allocator_type, const stored_allocator_type &, allocator_type>::type get_stored_allocator_const_return_t;
  488. typedef typename dtl::if_c
  489. <has_stored_allocator_type, stored_allocator_type &, allocator_type>::type get_stored_allocator_noconst_return_t;
  490. BOOST_CONTAINER_FORCEINLINE flat_tree()
  491. : m_data()
  492. { }
  493. BOOST_CONTAINER_FORCEINLINE explicit flat_tree(const Compare& comp)
  494. : m_data(comp)
  495. { }
  496. BOOST_CONTAINER_FORCEINLINE explicit flat_tree(const allocator_type& a)
  497. : m_data(a)
  498. { }
  499. BOOST_CONTAINER_FORCEINLINE flat_tree(const Compare& comp, const allocator_type& a)
  500. : m_data(comp, a)
  501. { }
  502. BOOST_CONTAINER_FORCEINLINE flat_tree(const flat_tree& x)
  503. : m_data(x.m_data)
  504. { }
  505. BOOST_CONTAINER_FORCEINLINE flat_tree(BOOST_RV_REF(flat_tree) x)
  506. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_constructible<Compare>::value)
  507. : m_data(boost::move(x.m_data))
  508. { }
  509. BOOST_CONTAINER_FORCEINLINE flat_tree(const flat_tree& x, const allocator_type &a)
  510. : m_data(x.m_data, a)
  511. { }
  512. BOOST_CONTAINER_FORCEINLINE flat_tree(BOOST_RV_REF(flat_tree) x, const allocator_type &a)
  513. : m_data(boost::move(x.m_data), a)
  514. { }
  515. template <class InputIterator>
  516. BOOST_CONTAINER_FORCEINLINE
  517. flat_tree( ordered_range_t, InputIterator first, InputIterator last)
  518. : m_data()
  519. {
  520. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  521. BOOST_ASSERT((is_sorted)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  522. }
  523. template <class InputIterator>
  524. BOOST_CONTAINER_FORCEINLINE
  525. flat_tree( ordered_range_t, InputIterator first, InputIterator last, const Compare& comp)
  526. : m_data(comp)
  527. {
  528. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  529. BOOST_ASSERT((is_sorted)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  530. }
  531. template <class InputIterator>
  532. BOOST_CONTAINER_FORCEINLINE
  533. flat_tree( ordered_range_t, InputIterator first, InputIterator last, const Compare& comp, const allocator_type& a)
  534. : m_data(comp, a)
  535. {
  536. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  537. BOOST_ASSERT((is_sorted)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  538. }
  539. template <class InputIterator>
  540. BOOST_CONTAINER_FORCEINLINE
  541. flat_tree( ordered_unique_range_t, InputIterator first, InputIterator last)
  542. : m_data()
  543. {
  544. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  545. BOOST_ASSERT((is_sorted_and_unique)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  546. }
  547. template <class InputIterator>
  548. BOOST_CONTAINER_FORCEINLINE
  549. flat_tree( ordered_unique_range_t, InputIterator first, InputIterator last, const Compare& comp)
  550. : m_data(comp)
  551. {
  552. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  553. BOOST_ASSERT((is_sorted_and_unique)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  554. }
  555. template <class InputIterator>
  556. BOOST_CONTAINER_FORCEINLINE
  557. flat_tree( ordered_unique_range_t, InputIterator first, InputIterator last, const Compare& comp, const allocator_type& a)
  558. : m_data(comp, a)
  559. {
  560. this->m_data.m_seq.insert(this->m_data.m_seq.end(), first, last);
  561. BOOST_ASSERT((is_sorted_and_unique)(this->m_data.m_seq.cbegin(), this->m_data.m_seq.cend(), this->priv_value_comp()));
  562. }
  563. template <class InputIterator>
  564. BOOST_CONTAINER_FORCEINLINE
  565. flat_tree( bool unique_insertion, InputIterator first, InputIterator last)
  566. : m_data()
  567. {
  568. this->priv_range_insertion_construct(unique_insertion, first, last);
  569. }
  570. template <class InputIterator>
  571. BOOST_CONTAINER_FORCEINLINE
  572. flat_tree( bool unique_insertion, InputIterator first, InputIterator last
  573. , const Compare& comp)
  574. : m_data(comp)
  575. {
  576. this->priv_range_insertion_construct(unique_insertion, first, last);
  577. }
  578. template <class InputIterator>
  579. BOOST_CONTAINER_FORCEINLINE
  580. flat_tree( bool unique_insertion, InputIterator first, InputIterator last
  581. , const allocator_type& a)
  582. : m_data(a)
  583. {
  584. this->priv_range_insertion_construct(unique_insertion, first, last);
  585. }
  586. template <class InputIterator>
  587. BOOST_CONTAINER_FORCEINLINE
  588. flat_tree( bool unique_insertion, InputIterator first, InputIterator last
  589. , const Compare& comp, const allocator_type& a)
  590. : m_data(comp, a)
  591. {
  592. this->priv_range_insertion_construct(unique_insertion, first, last);
  593. }
  594. BOOST_CONTAINER_FORCEINLINE ~flat_tree()
  595. {}
  596. BOOST_CONTAINER_FORCEINLINE flat_tree& operator=(BOOST_COPY_ASSIGN_REF(flat_tree) x)
  597. { m_data = x.m_data; return *this; }
  598. BOOST_CONTAINER_FORCEINLINE flat_tree& operator=(BOOST_RV_REF(flat_tree) x)
  599. BOOST_NOEXCEPT_IF( (allocator_traits_type::propagate_on_container_move_assignment::value ||
  600. allocator_traits_type::is_always_equal::value) &&
  601. boost::container::dtl::is_nothrow_move_assignable<Compare>::value)
  602. { m_data = boost::move(x.m_data); return *this; }
  603. BOOST_CONTAINER_FORCEINLINE const value_compare &priv_value_comp() const
  604. { return static_cast<const value_compare &>(this->m_data); }
  605. BOOST_CONTAINER_FORCEINLINE value_compare &priv_value_comp()
  606. { return static_cast<value_compare &>(this->m_data); }
  607. BOOST_CONTAINER_FORCEINLINE const key_compare &priv_key_comp() const
  608. { return this->priv_value_comp().get_comp(); }
  609. BOOST_CONTAINER_FORCEINLINE key_compare &priv_key_comp()
  610. { return this->priv_value_comp().get_comp(); }
  611. struct insert_commit_data
  612. {
  613. const_iterator position;
  614. };
  615. public:
  616. // accessors:
  617. BOOST_CONTAINER_FORCEINLINE Compare key_comp() const
  618. { return this->m_data.get_comp(); }
  619. BOOST_CONTAINER_FORCEINLINE value_compare value_comp() const
  620. { return this->m_data; }
  621. BOOST_CONTAINER_FORCEINLINE allocator_type get_allocator() const
  622. { return this->m_data.m_seq.get_allocator(); }
  623. BOOST_CONTAINER_FORCEINLINE get_stored_allocator_const_return_t get_stored_allocator() const
  624. {
  625. return flat_tree_get_stored_allocator(this->m_data.m_seq, dtl::bool_<has_stored_allocator_type>());
  626. }
  627. BOOST_CONTAINER_FORCEINLINE get_stored_allocator_noconst_return_t get_stored_allocator()
  628. {
  629. return flat_tree_get_stored_allocator(this->m_data.m_seq, dtl::bool_<has_stored_allocator_type>());
  630. }
  631. BOOST_CONTAINER_FORCEINLINE iterator begin()
  632. { return this->m_data.m_seq.begin(); }
  633. BOOST_CONTAINER_FORCEINLINE const_iterator begin() const
  634. { return this->cbegin(); }
  635. BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const
  636. { return this->m_data.m_seq.begin(); }
  637. BOOST_CONTAINER_FORCEINLINE iterator end()
  638. { return this->m_data.m_seq.end(); }
  639. BOOST_CONTAINER_FORCEINLINE const_iterator end() const
  640. { return this->cend(); }
  641. BOOST_CONTAINER_FORCEINLINE const_iterator cend() const
  642. { return this->m_data.m_seq.end(); }
  643. BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin()
  644. { return reverse_iterator(this->end()); }
  645. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const
  646. { return this->crbegin(); }
  647. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const
  648. { return const_reverse_iterator(this->cend()); }
  649. BOOST_CONTAINER_FORCEINLINE reverse_iterator rend()
  650. { return reverse_iterator(this->begin()); }
  651. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const
  652. { return this->crend(); }
  653. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const
  654. { return const_reverse_iterator(this->cbegin()); }
  655. BOOST_CONTAINER_FORCEINLINE bool empty() const
  656. { return this->m_data.m_seq.empty(); }
  657. BOOST_CONTAINER_FORCEINLINE size_type size() const
  658. { return this->m_data.m_seq.size(); }
  659. BOOST_CONTAINER_FORCEINLINE size_type max_size() const
  660. { return this->m_data.m_seq.max_size(); }
  661. BOOST_CONTAINER_FORCEINLINE void swap(flat_tree& other)
  662. BOOST_NOEXCEPT_IF( allocator_traits_type::is_always_equal::value
  663. && boost::container::dtl::is_nothrow_swappable<Compare>::value )
  664. { this->m_data.swap(other.m_data); }
  665. public:
  666. // insert/erase
  667. std::pair<iterator,bool> insert_unique(const value_type& val)
  668. {
  669. std::pair<iterator,bool> ret;
  670. insert_commit_data data;
  671. ret.second = this->priv_insert_unique_prepare(KeyOfValue()(val), data);
  672. ret.first = ret.second ? this->priv_insert_commit(data, val)
  673. : this->begin() + (data.position - this->cbegin());
  674. //: iterator(vector_iterator_get_ptr(data.position));
  675. return ret;
  676. }
  677. std::pair<iterator,bool> insert_unique(BOOST_RV_REF(value_type) val)
  678. {
  679. std::pair<iterator,bool> ret;
  680. insert_commit_data data;
  681. ret.second = this->priv_insert_unique_prepare(KeyOfValue()(val), data);
  682. ret.first = ret.second ? this->priv_insert_commit(data, boost::move(val))
  683. : this->begin() + (data.position - this->cbegin());
  684. //: iterator(vector_iterator_get_ptr(data.position));
  685. return ret;
  686. }
  687. iterator insert_equal(const value_type& val)
  688. {
  689. iterator i = this->upper_bound(KeyOfValue()(val));
  690. i = this->m_data.m_seq.insert(i, val);
  691. return i;
  692. }
  693. iterator insert_equal(BOOST_RV_REF(value_type) mval)
  694. {
  695. iterator i = this->upper_bound(KeyOfValue()(mval));
  696. i = this->m_data.m_seq.insert(i, boost::move(mval));
  697. return i;
  698. }
  699. iterator insert_unique(const_iterator hint, const value_type& val)
  700. {
  701. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  702. insert_commit_data data;
  703. return this->priv_insert_unique_prepare(hint, KeyOfValue()(val), data)
  704. ? this->priv_insert_commit(data, val)
  705. : this->begin() + (data.position - this->cbegin());
  706. //: iterator(vector_iterator_get_ptr(data.position));
  707. }
  708. iterator insert_unique(const_iterator hint, BOOST_RV_REF(value_type) val)
  709. {
  710. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  711. insert_commit_data data;
  712. return this->priv_insert_unique_prepare(hint, KeyOfValue()(val), data)
  713. ? this->priv_insert_commit(data, boost::move(val))
  714. : this->begin() + (data.position - this->cbegin());
  715. //: iterator(vector_iterator_get_ptr(data.position));
  716. }
  717. iterator insert_equal(const_iterator hint, const value_type& val)
  718. {
  719. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  720. insert_commit_data data;
  721. this->priv_insert_equal_prepare(hint, val, data);
  722. return this->priv_insert_commit(data, val);
  723. }
  724. iterator insert_equal(const_iterator hint, BOOST_RV_REF(value_type) mval)
  725. {
  726. BOOST_ASSERT(this->priv_in_range_or_end(hint));
  727. insert_commit_data data;
  728. this->priv_insert_equal_prepare(hint, mval, data);
  729. return this->priv_insert_commit(data, boost::move(mval));
  730. }
  731. template <class InIt>
  732. void insert_unique(InIt first, InIt last)
  733. {
  734. dtl::bool_<is_contiguous_container<container_type>::value> contiguous_tag;
  735. container_type &seq = this->m_data.m_seq;
  736. value_compare &val_cmp = this->priv_value_comp();
  737. //Step 1: put new elements in the back
  738. typename container_type::iterator const it = seq.insert(seq.cend(), first, last);
  739. //Step 2: sort them
  740. boost::movelib::pdqsort(it, seq.end(), val_cmp);
  741. //Step 3: only left unique values from the back not already present in the original range
  742. typename container_type::iterator const e = boost::movelib::inplace_set_unique_difference
  743. (it, seq.end(), seq.begin(), it, val_cmp);
  744. seq.erase(e, seq.cend());
  745. //Step 4: merge both ranges
  746. (flat_tree_container_inplace_merge)(seq, it, this->priv_value_comp(), contiguous_tag);
  747. }
  748. template <class InIt>
  749. void insert_equal(InIt first, InIt last)
  750. {
  751. dtl::bool_<is_contiguous_container<container_type>::value> contiguous_tag;
  752. container_type &seq = this->m_data.m_seq;
  753. typename container_type::iterator const it = seq.insert(seq.cend(), first, last);
  754. (flat_tree_container_inplace_sort_ending)(seq, it, this->priv_value_comp(), contiguous_tag);
  755. (flat_tree_container_inplace_merge) (seq, it, this->priv_value_comp(), contiguous_tag);
  756. }
  757. //Ordered
  758. template <class InIt>
  759. void insert_equal(ordered_range_t, InIt first, InIt last)
  760. {
  761. const bool value = boost::container::dtl::
  762. has_member_function_callable_with_merge_unique<container_type, InIt, InIt, value_compare>::value;
  763. (flat_tree_merge_equal)(this->m_data.m_seq, first, last, this->priv_value_comp(), dtl::bool_<value>());
  764. }
  765. template <class InIt>
  766. void insert_unique(ordered_unique_range_t, InIt first, InIt last)
  767. {
  768. const bool value = boost::container::dtl::
  769. has_member_function_callable_with_merge_unique<container_type, InIt, InIt, value_compare>::value;
  770. (flat_tree_merge_unique)(this->m_data.m_seq, first, last, this->priv_value_comp(), dtl::bool_<value>());
  771. }
  772. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  773. template <class... Args>
  774. std::pair<iterator, bool> emplace_unique(BOOST_FWD_REF(Args)... args)
  775. {
  776. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  777. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));
  778. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  779. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  780. value_destructor<stored_allocator_type, value_type> d(a, val);
  781. return this->insert_unique(::boost::move(val));
  782. }
  783. template <class... Args>
  784. iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
  785. {
  786. //hint checked in insert_unique
  787. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  788. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));
  789. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  790. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  791. value_destructor<stored_allocator_type, value_type> d(a, val);
  792. return this->insert_unique(hint, ::boost::move(val));
  793. }
  794. template <class... Args>
  795. iterator emplace_equal(BOOST_FWD_REF(Args)... args)
  796. {
  797. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  798. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));
  799. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  800. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  801. value_destructor<stored_allocator_type, value_type> d(a, val);
  802. return this->insert_equal(::boost::move(val));
  803. }
  804. template <class... Args>
  805. iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
  806. {
  807. //hint checked in insert_equal
  808. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
  809. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));
  810. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
  811. stored_allocator_traits::construct(a, &val, ::boost::forward<Args>(args)... );
  812. value_destructor<stored_allocator_type, value_type> d(a, val);
  813. return this->insert_equal(hint, ::boost::move(val));
  814. }
  815. template <class KeyType, class... Args>
  816. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool> try_emplace
  817. (const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(Args)... args)
  818. {
  819. std::pair<iterator,bool> ret;
  820. insert_commit_data data;
  821. const key_type & k = key;
  822. ret.second = hint == const_iterator()
  823. ? this->priv_insert_unique_prepare(k, data)
  824. : this->priv_insert_unique_prepare(hint, k, data);
  825. if(!ret.second){
  826. ret.first = this->nth(data.position - this->cbegin());
  827. }
  828. else{
  829. typedef typename emplace_functor_type<try_emplace_t, KeyType, Args...>::type func_t;
  830. typedef emplace_iterator<value_type, func_t, difference_type> it_t;
  831. func_t func(try_emplace_t(), ::boost::forward<KeyType>(key), ::boost::forward<Args>(args)...);
  832. ret.first = this->m_data.m_seq.insert(data.position, it_t(func), it_t());
  833. }
  834. return ret;
  835. }
  836. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  837. #define BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE(N) \
  838. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  839. std::pair<iterator, bool> emplace_unique(BOOST_MOVE_UREF##N)\
  840. {\
  841. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  842. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));\
  843. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  844. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  845. value_destructor<stored_allocator_type, value_type> d(a, val);\
  846. return this->insert_unique(::boost::move(val));\
  847. }\
  848. \
  849. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  850. iterator emplace_hint_unique(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  851. {\
  852. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  853. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));\
  854. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  855. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  856. value_destructor<stored_allocator_type, value_type> d(a, val);\
  857. return this->insert_unique(hint, ::boost::move(val));\
  858. }\
  859. \
  860. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  861. iterator emplace_equal(BOOST_MOVE_UREF##N)\
  862. {\
  863. typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;\
  864. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));\
  865. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  866. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  867. value_destructor<stored_allocator_type, value_type> d(a, val);\
  868. return this->insert_equal(::boost::move(val));\
  869. }\
  870. \
  871. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  872. iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  873. {\
  874. typename aligned_storage <sizeof(value_type), alignment_of<value_type>::value>::type v;\
  875. value_type &val = *static_cast<value_type *>(static_cast<void *>(v.data));\
  876. get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
  877. stored_allocator_traits::construct(a, &val BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  878. value_destructor<stored_allocator_type, value_type> d(a, val);\
  879. return this->insert_equal(hint, ::boost::move(val));\
  880. }\
  881. template <class KeyType BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  882. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, bool>\
  883. try_emplace(const_iterator hint, BOOST_FWD_REF(KeyType) key BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  884. {\
  885. std::pair<iterator,bool> ret;\
  886. insert_commit_data data;\
  887. const key_type & k = key;\
  888. ret.second = hint == const_iterator()\
  889. ? this->priv_insert_unique_prepare(k, data)\
  890. : this->priv_insert_unique_prepare(hint, k, data);\
  891. \
  892. if(!ret.second){\
  893. ret.first = this->nth(data.position - this->cbegin());\
  894. }\
  895. else{\
  896. typedef typename emplace_functor_type<try_emplace_t, KeyType BOOST_MOVE_I##N BOOST_MOVE_TARG##N>::type func_t;\
  897. typedef emplace_iterator<value_type, func_t, difference_type> it_t;\
  898. func_t func(try_emplace_t(), ::boost::forward<KeyType>(key) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  899. ret.first = this->m_data.m_seq.insert(data.position, it_t(func), it_t());\
  900. }\
  901. return ret;\
  902. }\
  903. //
  904. BOOST_MOVE_ITERATE_0TO7(BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE)
  905. #undef BOOST_CONTAINER_FLAT_TREE_EMPLACE_CODE
  906. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  907. template<class KeyType, class M>
  908. std::pair<iterator, bool> insert_or_assign(const_iterator hint, BOOST_FWD_REF(KeyType) key, BOOST_FWD_REF(M) obj)
  909. {
  910. const key_type& k = key;
  911. std::pair<iterator,bool> ret;
  912. insert_commit_data data;
  913. ret.second = hint == const_iterator()
  914. ? this->priv_insert_unique_prepare(k, data)
  915. : this->priv_insert_unique_prepare(hint, k, data);
  916. if(!ret.second){
  917. ret.first = this->nth(data.position - this->cbegin());
  918. ret.first->second = boost::forward<M>(obj);
  919. }
  920. else{
  921. typedef typename emplace_functor_type<KeyType, M>::type func_t;
  922. typedef emplace_iterator<value_type, func_t, difference_type> it_t;
  923. func_t func(boost::forward<KeyType>(key), boost::forward<M>(obj));
  924. ret.first = this->m_data.m_seq.insert(data.position, it_t(func), it_t());
  925. }
  926. return ret;
  927. }
  928. BOOST_CONTAINER_FORCEINLINE iterator erase(const_iterator position)
  929. { return this->m_data.m_seq.erase(position); }
  930. size_type erase(const key_type& k)
  931. {
  932. std::pair<iterator,iterator > itp = this->equal_range(k);
  933. size_type ret = static_cast<size_type>(itp.second-itp.first);
  934. if (ret){
  935. this->m_data.m_seq.erase(itp.first, itp.second);
  936. }
  937. return ret;
  938. }
  939. BOOST_CONTAINER_FORCEINLINE iterator erase(const_iterator first, const_iterator last)
  940. { return this->m_data.m_seq.erase(first, last); }
  941. BOOST_CONTAINER_FORCEINLINE void clear()
  942. { this->m_data.m_seq.clear(); }
  943. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  944. // with previous allocations. The size of the vector is unchanged
  945. //!
  946. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  947. //!
  948. //! <b>Complexity</b>: Linear to size().
  949. BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
  950. { this->m_data.m_seq.shrink_to_fit(); }
  951. BOOST_CONTAINER_FORCEINLINE iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  952. {
  953. const bool value = boost::container::dtl::
  954. has_member_function_callable_with_nth<container_type, size_type>::value;
  955. return flat_tree_nth<iterator>(this->m_data.m_seq, n, dtl::bool_<value>());
  956. }
  957. BOOST_CONTAINER_FORCEINLINE const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  958. {
  959. const bool value = boost::container::dtl::
  960. has_member_function_callable_with_nth<container_type, size_type>::value;
  961. return flat_tree_nth<const_iterator>(this->m_data.m_seq, n, dtl::bool_<value>());
  962. }
  963. BOOST_CONTAINER_FORCEINLINE size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  964. {
  965. const bool value = boost::container::dtl::
  966. has_member_function_callable_with_index_of<container_type, iterator>::value;
  967. return flat_tree_index_of(this->m_data.m_seq, p, dtl::bool_<value>());
  968. }
  969. BOOST_CONTAINER_FORCEINLINE size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  970. {
  971. const bool value = boost::container::dtl::
  972. has_member_function_callable_with_index_of<container_type, const_iterator>::value;
  973. return flat_tree_index_of(this->m_data.m_seq, p, dtl::bool_<value>());
  974. }
  975. // set operations:
  976. iterator find(const key_type& k)
  977. {
  978. iterator i = this->lower_bound(k);
  979. iterator end_it = this->end();
  980. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  981. i = end_it;
  982. }
  983. return i;
  984. }
  985. const_iterator find(const key_type& k) const
  986. {
  987. const_iterator i = this->lower_bound(k);
  988. const_iterator end_it = this->cend();
  989. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  990. i = end_it;
  991. }
  992. return i;
  993. }
  994. template<class K>
  995. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  996. find(const K& k)
  997. {
  998. iterator i = this->lower_bound(k);
  999. iterator end_it = this->end();
  1000. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1001. i = end_it;
  1002. }
  1003. return i;
  1004. }
  1005. template<class K>
  1006. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1007. find(const K& k) const
  1008. {
  1009. const_iterator i = this->lower_bound(k);
  1010. const_iterator end_it = this->cend();
  1011. if (i != end_it && this->m_data.get_comp()(k, KeyOfValue()(*i))){
  1012. i = end_it;
  1013. }
  1014. return i;
  1015. }
  1016. size_type count(const key_type& k) const
  1017. {
  1018. std::pair<const_iterator, const_iterator> p = this->equal_range(k);
  1019. size_type n = p.second - p.first;
  1020. return n;
  1021. }
  1022. template<class K>
  1023. typename dtl::enable_if_transparent<key_compare, K, size_type>::type
  1024. count(const K& k) const
  1025. {
  1026. std::pair<const_iterator, const_iterator> p = this->equal_range(k);
  1027. size_type n = p.second - p.first;
  1028. return n;
  1029. }
  1030. template<class C2>
  1031. BOOST_CONTAINER_FORCEINLINE void merge_unique(flat_tree<Value, KeyOfValue, C2, AllocatorOrContainer>& source)
  1032. {
  1033. this->insert( boost::make_move_iterator(source.begin())
  1034. , boost::make_move_iterator(source.end()));
  1035. }
  1036. template<class C2>
  1037. BOOST_CONTAINER_FORCEINLINE void merge_equal(flat_tree<Value, KeyOfValue, C2, AllocatorOrContainer>& source)
  1038. {
  1039. this->insert( boost::make_move_iterator(source.begin())
  1040. , boost::make_move_iterator(source.end()));
  1041. }
  1042. BOOST_CONTAINER_FORCEINLINE void merge_unique(flat_tree& source)
  1043. {
  1044. const bool value = boost::container::dtl::
  1045. has_member_function_callable_with_merge_unique<container_type, iterator, iterator, value_compare>::value;
  1046. (flat_tree_merge_unique)
  1047. ( this->m_data.m_seq
  1048. , boost::make_move_iterator(source.m_data.m_seq.begin())
  1049. , boost::make_move_iterator(source.m_data.m_seq.end())
  1050. , this->priv_value_comp()
  1051. , dtl::bool_<value>());
  1052. }
  1053. BOOST_CONTAINER_FORCEINLINE void merge_equal(flat_tree& source)
  1054. {
  1055. const bool value = boost::container::dtl::
  1056. has_member_function_callable_with_merge<container_type, iterator, iterator, value_compare>::value;
  1057. (flat_tree_merge_equal)
  1058. ( this->m_data.m_seq
  1059. , boost::make_move_iterator(source.m_data.m_seq.begin())
  1060. , boost::make_move_iterator(source.m_data.m_seq.end())
  1061. , this->priv_value_comp()
  1062. , dtl::bool_<value>());
  1063. }
  1064. BOOST_CONTAINER_FORCEINLINE iterator lower_bound(const key_type& k)
  1065. { return this->priv_lower_bound(this->begin(), this->end(), k); }
  1066. BOOST_CONTAINER_FORCEINLINE const_iterator lower_bound(const key_type& k) const
  1067. { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
  1068. template<class K>
  1069. BOOST_CONTAINER_FORCEINLINE
  1070. typename dtl::enable_if_transparent<key_compare, K, iterator>::type
  1071. lower_bound(const K& k)
  1072. { return this->priv_lower_bound(this->begin(), this->end(), k); }
  1073. template<class K>
  1074. BOOST_CONTAINER_FORCEINLINE
  1075. typename dtl::enable_if_transparent<key_compare, K, const_iterator>::type
  1076. lower_bound(const K& k) const
  1077. { return this->priv_lower_bound(this->cbegin(), this->cend(), k); }
  1078. BOOST_CONTAINER_FORCEINLINE iterator upper_bound(const key_type& k)
  1079. { return this->priv_upper_bound(this->begin(), this->end(), k); }
  1080. BOOST_CONTAINER_FORCEINLINE const_iterator upper_bound(const key_type& k) const
  1081. { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
  1082. template<class K>
  1083. BOOST_CONTAINER_FORCEINLINE
  1084. typename dtl::enable_if_transparent<key_compare, K,iterator>::type
  1085. upper_bound(const K& k)
  1086. { return this->priv_upper_bound(this->begin(), this->end(), k); }
  1087. template<class K>
  1088. BOOST_CONTAINER_FORCEINLINE
  1089. typename dtl::enable_if_transparent<key_compare, K,const_iterator>::type
  1090. upper_bound(const K& k) const
  1091. { return this->priv_upper_bound(this->cbegin(), this->cend(), k); }
  1092. BOOST_CONTAINER_FORCEINLINE std::pair<iterator,iterator> equal_range(const key_type& k)
  1093. { return this->priv_equal_range(this->begin(), this->end(), k); }
  1094. BOOST_CONTAINER_FORCEINLINE std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
  1095. { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
  1096. template<class K>
  1097. BOOST_CONTAINER_FORCEINLINE
  1098. typename dtl::enable_if_transparent<key_compare, K,std::pair<iterator,iterator> >::type
  1099. equal_range(const K& k)
  1100. { return this->priv_equal_range(this->begin(), this->end(), k); }
  1101. template<class K>
  1102. BOOST_CONTAINER_FORCEINLINE
  1103. typename dtl::enable_if_transparent<key_compare, K,std::pair<const_iterator,const_iterator> >::type
  1104. equal_range(const K& k) const
  1105. { return this->priv_equal_range(this->cbegin(), this->cend(), k); }
  1106. BOOST_CONTAINER_FORCEINLINE std::pair<iterator, iterator> lower_bound_range(const key_type& k)
  1107. { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
  1108. BOOST_CONTAINER_FORCEINLINE std::pair<const_iterator, const_iterator> lower_bound_range(const key_type& k) const
  1109. { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
  1110. template<class K>
  1111. BOOST_CONTAINER_FORCEINLINE
  1112. typename dtl::enable_if_transparent<key_compare, K,std::pair<iterator,iterator> >::type
  1113. lower_bound_range(const K& k)
  1114. { return this->priv_lower_bound_range(this->begin(), this->end(), k); }
  1115. template<class K>
  1116. BOOST_CONTAINER_FORCEINLINE
  1117. typename dtl::enable_if_transparent<key_compare, K,std::pair<const_iterator,const_iterator> >::type
  1118. lower_bound_range(const K& k) const
  1119. { return this->priv_lower_bound_range(this->cbegin(), this->cend(), k); }
  1120. BOOST_CONTAINER_FORCEINLINE size_type capacity() const
  1121. {
  1122. const bool value = boost::container::dtl::
  1123. has_member_function_callable_with_capacity<container_type>::value;
  1124. return (flat_tree_capacity)(this->m_data.m_seq, dtl::bool_<value>());
  1125. }
  1126. BOOST_CONTAINER_FORCEINLINE void reserve(size_type cnt)
  1127. {
  1128. const bool value = boost::container::dtl::
  1129. has_member_function_callable_with_reserve<container_type, size_type>::value;
  1130. (flat_tree_reserve)(this->m_data.m_seq, cnt, dtl::bool_<value>());
  1131. }
  1132. BOOST_CONTAINER_FORCEINLINE container_type extract_sequence()
  1133. {
  1134. return boost::move(m_data.m_seq);
  1135. }
  1136. BOOST_CONTAINER_FORCEINLINE container_type &get_sequence_ref()
  1137. {
  1138. return m_data.m_seq;
  1139. }
  1140. BOOST_CONTAINER_FORCEINLINE void adopt_sequence_equal(BOOST_RV_REF(container_type) seq)
  1141. {
  1142. (flat_tree_adopt_sequence_equal)( m_data.m_seq, boost::move(seq), this->priv_value_comp()
  1143. , dtl::bool_<is_contiguous_container<container_type>::value>());
  1144. }
  1145. BOOST_CONTAINER_FORCEINLINE void adopt_sequence_unique(BOOST_RV_REF(container_type) seq)
  1146. {
  1147. (flat_tree_adopt_sequence_unique)(m_data.m_seq, boost::move(seq), this->priv_value_comp()
  1148. , dtl::bool_<is_contiguous_container<container_type>::value>());
  1149. }
  1150. void adopt_sequence_equal(ordered_range_t, BOOST_RV_REF(container_type) seq)
  1151. {
  1152. BOOST_ASSERT((is_sorted)(seq.cbegin(), seq.cend(), this->priv_value_comp()));
  1153. m_data.m_seq = boost::move(seq);
  1154. }
  1155. void adopt_sequence_unique(ordered_unique_range_t, BOOST_RV_REF(container_type) seq)
  1156. {
  1157. BOOST_ASSERT((is_sorted_and_unique)(seq.cbegin(), seq.cend(), this->priv_value_comp()));
  1158. m_data.m_seq = boost::move(seq);
  1159. }
  1160. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const flat_tree& x, const flat_tree& y)
  1161. {
  1162. return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin());
  1163. }
  1164. BOOST_CONTAINER_FORCEINLINE friend bool operator<(const flat_tree& x, const flat_tree& y)
  1165. {
  1166. return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  1167. }
  1168. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const flat_tree& x, const flat_tree& y)
  1169. { return !(x == y); }
  1170. BOOST_CONTAINER_FORCEINLINE friend bool operator>(const flat_tree& x, const flat_tree& y)
  1171. { return y < x; }
  1172. BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const flat_tree& x, const flat_tree& y)
  1173. { return !(y < x); }
  1174. BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const flat_tree& x, const flat_tree& y)
  1175. { return !(x < y); }
  1176. BOOST_CONTAINER_FORCEINLINE friend void swap(flat_tree& x, flat_tree& y)
  1177. { x.swap(y); }
  1178. private:
  1179. template <class InputIterator>
  1180. void priv_range_insertion_construct( bool unique_insertion, InputIterator first, InputIterator last)
  1181. {
  1182. //Use cend() as hint to achieve linear time for
  1183. //ordered ranges as required by the standard
  1184. //for the constructor
  1185. //Call end() every iteration as reallocation might have invalidated iterators
  1186. if(unique_insertion){
  1187. this->insert_unique(first, last);
  1188. }
  1189. else{
  1190. this->insert_equal (first, last);
  1191. }
  1192. }
  1193. BOOST_CONTAINER_FORCEINLINE bool priv_in_range_or_end(const_iterator pos) const
  1194. {
  1195. return (this->begin() <= pos) && (pos <= this->end());
  1196. }
  1197. // insert/erase
  1198. void priv_insert_equal_prepare
  1199. (const_iterator pos, const value_type& val, insert_commit_data &data)
  1200. {
  1201. // N1780
  1202. // To insert val at pos:
  1203. // if pos == end || val <= *pos
  1204. // if pos == begin || val >= *(pos-1)
  1205. // insert val before pos
  1206. // else
  1207. // insert val before upper_bound(val)
  1208. // else
  1209. // insert val before lower_bound(val)
  1210. const value_compare &val_cmp = this->m_data;
  1211. if(pos == this->cend() || !val_cmp(*pos, val)){
  1212. if (pos == this->cbegin() || !val_cmp(val, pos[-1])){
  1213. data.position = pos;
  1214. }
  1215. else{
  1216. data.position =
  1217. this->priv_upper_bound(this->cbegin(), pos, KeyOfValue()(val));
  1218. }
  1219. }
  1220. else{
  1221. data.position =
  1222. this->priv_lower_bound(pos, this->cend(), KeyOfValue()(val));
  1223. }
  1224. }
  1225. bool priv_insert_unique_prepare
  1226. (const_iterator b, const_iterator e, const key_type& k, insert_commit_data &commit_data)
  1227. {
  1228. const key_compare &key_cmp = this->priv_key_comp();
  1229. commit_data.position = this->priv_lower_bound(b, e, k);
  1230. return commit_data.position == e || key_cmp(k, KeyOfValue()(*commit_data.position));
  1231. }
  1232. BOOST_CONTAINER_FORCEINLINE bool priv_insert_unique_prepare
  1233. (const key_type& k, insert_commit_data &commit_data)
  1234. { return this->priv_insert_unique_prepare(this->cbegin(), this->cend(), k, commit_data); }
  1235. bool priv_insert_unique_prepare
  1236. (const_iterator pos, const key_type& k, insert_commit_data &commit_data)
  1237. {
  1238. //N1780. Props to Howard Hinnant!
  1239. //To insert k at pos:
  1240. //if pos == end || k <= *pos
  1241. // if pos == begin || k >= *(pos-1)
  1242. // insert k before pos
  1243. // else
  1244. // insert k before upper_bound(k)
  1245. //else if pos+1 == end || k <= *(pos+1)
  1246. // insert k after pos
  1247. //else
  1248. // insert k before lower_bound(k)
  1249. const key_compare &key_cmp = this->priv_key_comp();
  1250. const const_iterator cend_it = this->cend();
  1251. if(pos == cend_it || key_cmp(k, KeyOfValue()(*pos))){ //Check if k should go before end
  1252. const const_iterator cbeg = this->cbegin();
  1253. commit_data.position = pos;
  1254. if(pos == cbeg){ //If container is empty then insert it in the beginning
  1255. return true;
  1256. }
  1257. const_iterator prev(pos);
  1258. --prev;
  1259. if(key_cmp(KeyOfValue()(*prev), k)){ //If previous element was less, then it should go between prev and pos
  1260. return true;
  1261. }
  1262. else if(!key_cmp(k, KeyOfValue()(*prev))){ //If previous was equal then insertion should fail
  1263. commit_data.position = prev;
  1264. return false;
  1265. }
  1266. else{ //Previous was bigger so insertion hint was pointless, dispatch to hintless insertion
  1267. //but reduce the search between beg and prev as prev is bigger than k
  1268. return this->priv_insert_unique_prepare(cbeg, prev, k, commit_data);
  1269. }
  1270. }
  1271. else{
  1272. //The hint is before the insertion position, so insert it
  1273. //in the remaining range [pos, end)
  1274. return this->priv_insert_unique_prepare(pos, cend_it, k, commit_data);
  1275. }
  1276. }
  1277. template<class Convertible>
  1278. BOOST_CONTAINER_FORCEINLINE iterator priv_insert_commit
  1279. (insert_commit_data &commit_data, BOOST_FWD_REF(Convertible) convertible)
  1280. {
  1281. return this->m_data.m_seq.insert
  1282. ( commit_data.position
  1283. , boost::forward<Convertible>(convertible));
  1284. }
  1285. template <class RanIt, class K>
  1286. RanIt priv_lower_bound(RanIt first, const RanIt last,
  1287. const K & key) const
  1288. {
  1289. const Compare &key_cmp = this->m_data.get_comp();
  1290. KeyOfValue key_extract;
  1291. size_type len = static_cast<size_type>(last - first);
  1292. RanIt middle;
  1293. while (len) {
  1294. size_type step = len >> 1;
  1295. middle = first;
  1296. middle += step;
  1297. if (key_cmp(key_extract(*middle), key)) {
  1298. first = ++middle;
  1299. len -= step + 1;
  1300. }
  1301. else{
  1302. len = step;
  1303. }
  1304. }
  1305. return first;
  1306. }
  1307. template <class RanIt, class K>
  1308. RanIt priv_upper_bound
  1309. (RanIt first, const RanIt last,const K & key) const
  1310. {
  1311. const Compare &key_cmp = this->m_data.get_comp();
  1312. KeyOfValue key_extract;
  1313. size_type len = static_cast<size_type>(last - first);
  1314. RanIt middle;
  1315. while (len) {
  1316. size_type step = len >> 1;
  1317. middle = first;
  1318. middle += step;
  1319. if (key_cmp(key, key_extract(*middle))) {
  1320. len = step;
  1321. }
  1322. else{
  1323. first = ++middle;
  1324. len -= step + 1;
  1325. }
  1326. }
  1327. return first;
  1328. }
  1329. template <class RanIt, class K>
  1330. std::pair<RanIt, RanIt>
  1331. priv_equal_range(RanIt first, RanIt last, const K& key) const
  1332. {
  1333. const Compare &key_cmp = this->m_data.get_comp();
  1334. KeyOfValue key_extract;
  1335. size_type len = static_cast<size_type>(last - first);
  1336. RanIt middle;
  1337. while (len) {
  1338. size_type step = len >> 1;
  1339. middle = first;
  1340. middle += step;
  1341. if (key_cmp(key_extract(*middle), key)){
  1342. first = ++middle;
  1343. len -= step + 1;
  1344. }
  1345. else if (key_cmp(key, key_extract(*middle))){
  1346. len = step;
  1347. }
  1348. else {
  1349. //Middle is equal to key
  1350. last = first;
  1351. last += len;
  1352. RanIt const first_ret = this->priv_lower_bound(first, middle, key);
  1353. return std::pair<RanIt, RanIt>
  1354. ( first_ret, this->priv_upper_bound(++middle, last, key));
  1355. }
  1356. }
  1357. return std::pair<RanIt, RanIt>(first, first);
  1358. }
  1359. template<class RanIt, class K>
  1360. std::pair<RanIt, RanIt> priv_lower_bound_range(RanIt first, RanIt last, const K& k) const
  1361. {
  1362. const Compare &key_cmp = this->m_data.get_comp();
  1363. KeyOfValue key_extract;
  1364. RanIt lb(this->priv_lower_bound(first, last, k)), ub(lb);
  1365. if(lb != last && static_cast<difference_type>(!key_cmp(k, key_extract(*lb)))){
  1366. ++ub;
  1367. }
  1368. return std::pair<RanIt, RanIt>(lb, ub);
  1369. }
  1370. };
  1371. } //namespace dtl {
  1372. } //namespace container {
  1373. //!has_trivial_destructor_after_move<> == true_type
  1374. //!specialization for optimizations
  1375. template <class T, class KeyOfValue,
  1376. class Compare, class AllocatorOrContainer>
  1377. struct has_trivial_destructor_after_move<boost::container::dtl::flat_tree<T, KeyOfValue, Compare, AllocatorOrContainer> >
  1378. {
  1379. typedef typename boost::container::dtl::select_container_type<T, AllocatorOrContainer>::type container_type;
  1380. typedef typename container_type::allocator_type allocator_t;
  1381. typedef typename ::boost::container::allocator_traits<allocator_t>::pointer pointer;
  1382. static const bool value = ::boost::has_trivial_destructor_after_move<allocator_t>::value &&
  1383. ::boost::has_trivial_destructor_after_move<pointer>::value;
  1384. };
  1385. } //namespace boost {
  1386. #include <boost/container/detail/config_end.hpp>
  1387. #endif // BOOST_CONTAINER_FLAT_TREE_HPP