copy_move_algo.hpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013. 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_DETAIL_COPY_MOVE_ALGO_HPP
  11. #define BOOST_CONTAINER_DETAIL_COPY_MOVE_ALGO_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. // container
  19. #include <boost/container/allocator_traits.hpp>
  20. // container/detail
  21. #include <boost/container/detail/iterator.hpp>
  22. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  23. #include <boost/container/detail/mpl.hpp>
  24. #include <boost/container/detail/type_traits.hpp>
  25. #include <boost/container/detail/construct_in_place.hpp>
  26. #include <boost/container/detail/destroyers.hpp>
  27. // move
  28. #include <boost/move/adl_move_swap.hpp>
  29. #include <boost/move/iterator.hpp>
  30. #include <boost/move/utility_core.hpp>
  31. #include <boost/move/traits.hpp>
  32. // other
  33. #include <boost/assert.hpp>
  34. // std
  35. #include <cstring> //for memmove/memcpy
  36. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  37. #pragma GCC diagnostic push
  38. //pair memcpy optimizations rightfully detected by GCC
  39. # if defined(BOOST_GCC) && (BOOST_GCC >= 80000)
  40. # pragma GCC diagnostic ignored "-Wclass-memaccess"
  41. # endif
  42. //GCC 8 seems a bit confused about array access error with static_vector
  43. //when out of bound exceptions are being thrown.
  44. # if defined(BOOST_GCC) && (BOOST_GCC >= 80000) && (BOOST_GCC < 80200)
  45. # pragma GCC diagnostic ignored "-Wstringop-overflow"
  46. # endif
  47. # pragma GCC diagnostic ignored "-Warray-bounds"
  48. #endif
  49. namespace boost {
  50. namespace container {
  51. namespace dtl {
  52. template<class I>
  53. struct are_elements_contiguous
  54. {
  55. static const bool value = false;
  56. };
  57. /////////////////////////
  58. // raw pointers
  59. /////////////////////////
  60. template<class T>
  61. struct are_elements_contiguous<T*>
  62. {
  63. static const bool value = true;
  64. };
  65. /////////////////////////
  66. // move iterators
  67. /////////////////////////
  68. template<class It>
  69. struct are_elements_contiguous< ::boost::move_iterator<It> >
  70. : are_elements_contiguous<It>
  71. {};
  72. } //namespace dtl {
  73. /////////////////////////
  74. // predeclarations
  75. /////////////////////////
  76. template <class Pointer, bool IsConst>
  77. class vec_iterator;
  78. } //namespace container {
  79. namespace interprocess {
  80. template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment>
  81. class offset_ptr;
  82. } //namespace interprocess {
  83. namespace container {
  84. namespace dtl {
  85. /////////////////////////
  86. //vector_[const_]iterator
  87. /////////////////////////
  88. template <class Pointer, bool IsConst>
  89. struct are_elements_contiguous<boost::container::vec_iterator<Pointer, IsConst> >
  90. {
  91. static const bool value = true;
  92. };
  93. /////////////////////////
  94. // offset_ptr
  95. /////////////////////////
  96. template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment>
  97. struct are_elements_contiguous< ::boost::interprocess::offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment> >
  98. {
  99. static const bool value = true;
  100. };
  101. template <typename I, typename O>
  102. struct are_contiguous_and_same
  103. : boost::move_detail::and_
  104. < are_elements_contiguous<I>
  105. , are_elements_contiguous<O>
  106. , is_same< typename remove_const< typename ::boost::container::iter_value<I>::type >::type
  107. , typename ::boost::container::iterator_traits<O>::value_type
  108. >
  109. >
  110. {};
  111. template <typename I, typename O>
  112. struct is_memtransfer_copy_assignable
  113. : boost::move_detail::and_
  114. < are_contiguous_and_same<I, O>
  115. , dtl::is_trivially_copy_assignable< typename ::boost::container::iter_value<I>::type >
  116. >
  117. {};
  118. template <typename I, typename O>
  119. struct is_memtransfer_copy_constructible
  120. : boost::move_detail::and_
  121. < are_contiguous_and_same<I, O>
  122. , dtl::is_trivially_copy_constructible< typename ::boost::container::iter_value<I>::type >
  123. >
  124. {};
  125. template <typename I, typename O, typename R>
  126. struct enable_if_memtransfer_copy_constructible
  127. : enable_if<dtl::is_memtransfer_copy_constructible<I, O>, R>
  128. {};
  129. template <typename I, typename O, typename R>
  130. struct disable_if_memtransfer_copy_constructible
  131. : disable_if<dtl::is_memtransfer_copy_constructible<I, O>, R>
  132. {};
  133. template <typename I, typename O, typename R>
  134. struct enable_if_memtransfer_copy_assignable
  135. : enable_if<dtl::is_memtransfer_copy_assignable<I, O>, R>
  136. {};
  137. template <typename I, typename O, typename R>
  138. struct disable_if_memtransfer_copy_assignable
  139. : disable_if<dtl::is_memtransfer_copy_assignable<I, O>, R>
  140. {};
  141. template <class T>
  142. struct has_single_value
  143. {
  144. private:
  145. struct two { char array_[2]; };
  146. template<bool Arg> struct wrapper;
  147. template <class U> static two test(int, ...);
  148. template <class U> static char test(int, const wrapper<U::single_value>*);
  149. public:
  150. static const bool value = sizeof(test<T>(0, 0)) == 1;
  151. void dummy() {}
  152. };
  153. template<class InsertionProxy, bool = has_single_value<InsertionProxy>::value>
  154. struct is_single_value_proxy_impl
  155. {
  156. static const bool value = InsertionProxy::single_value;
  157. };
  158. template<class InsertionProxy>
  159. struct is_single_value_proxy_impl<InsertionProxy, false>
  160. {
  161. static const bool value = false;
  162. };
  163. template<class InsertionProxy>
  164. struct is_single_value_proxy
  165. : is_single_value_proxy_impl<InsertionProxy>
  166. {};
  167. template <typename P, typename R = void>
  168. struct enable_if_single_value_proxy
  169. : enable_if<is_single_value_proxy<P>, R>
  170. {};
  171. template <typename P, typename R = void>
  172. struct disable_if_single_value_proxy
  173. : disable_if<is_single_value_proxy<P>, R>
  174. {};
  175. template
  176. <typename I, // I models InputIterator
  177. typename F> // F models ForwardIterator
  178. BOOST_CONTAINER_FORCEINLINE F memmove(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  179. {
  180. typedef typename boost::container::iter_value<I>::type value_type;
  181. typedef typename boost::container::iterator_traits<F>::difference_type r_difference_type;
  182. value_type *const dest_raw = boost::movelib::iterator_to_raw_pointer(r);
  183. const value_type *const beg_raw = boost::movelib::iterator_to_raw_pointer(f);
  184. const value_type *const end_raw = boost::movelib::iterator_to_raw_pointer(l);
  185. if(BOOST_LIKELY(beg_raw != end_raw && dest_raw && beg_raw)){
  186. const std::size_t n = std::size_t(end_raw - beg_raw) ;
  187. std::memmove(dest_raw, beg_raw, sizeof(value_type)*n);
  188. r += static_cast<r_difference_type>(n);
  189. }
  190. return r;
  191. }
  192. template
  193. <typename I, // I models InputIterator
  194. typename F> // F models ForwardIterator
  195. BOOST_CONTAINER_FORCEINLINE F memmove_n(I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  196. {
  197. typedef typename boost::container::iter_value<I>::type value_type;
  198. typedef typename boost::container::iterator_traits<F>::difference_type r_difference_type;
  199. if(BOOST_LIKELY(n != 0)){
  200. void *dst = boost::movelib::iterator_to_raw_pointer(r);
  201. const void *src = boost::movelib::iterator_to_raw_pointer(f);
  202. if (dst && src)
  203. std::memmove(dst, src, sizeof(value_type)*n);
  204. r += static_cast<r_difference_type>(n);
  205. }
  206. return r;
  207. }
  208. template
  209. <typename I, // I models InputIterator
  210. typename F> // F models ForwardIterator
  211. BOOST_CONTAINER_FORCEINLINE I memmove_n_source(I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  212. {
  213. if(BOOST_LIKELY(n != 0)){
  214. typedef typename boost::container::iter_value<I>::type value_type;
  215. typedef typename boost::container::iterator_traits<I>::difference_type i_difference_type;
  216. void *dst = boost::movelib::iterator_to_raw_pointer(r);
  217. const void *src = boost::movelib::iterator_to_raw_pointer(f);
  218. if (dst && src)
  219. std::memmove(dst, src, sizeof(value_type)*n);
  220. f += static_cast<i_difference_type>(n);
  221. }
  222. return f;
  223. }
  224. template
  225. <typename I, // I models InputIterator
  226. typename F> // F models ForwardIterator
  227. BOOST_CONTAINER_FORCEINLINE I memmove_n_source_dest(I f, std::size_t n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  228. {
  229. typedef typename boost::container::iter_value<I>::type value_type;
  230. typedef typename boost::container::iterator_traits<F>::difference_type i_difference_type;
  231. typedef typename boost::container::iterator_traits<F>::difference_type f_difference_type;
  232. if(BOOST_LIKELY(n != 0)){
  233. void *dst = boost::movelib::iterator_to_raw_pointer(r);
  234. const void *src = boost::movelib::iterator_to_raw_pointer(f);
  235. if (dst && src)
  236. std::memmove(dst, src, sizeof(value_type)*n);
  237. f += i_difference_type(n);
  238. r += f_difference_type(n);
  239. }
  240. return f;
  241. }
  242. template <typename O>
  243. struct is_memzero_initializable
  244. {
  245. typedef typename ::boost::container::iterator_traits<O>::value_type value_type;
  246. static const bool value = are_elements_contiguous<O>::value &&
  247. ( dtl::is_integral<value_type>::value || dtl::is_enum<value_type>::value
  248. #if defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL)
  249. || dtl::is_pointer<value_type>::value
  250. #endif
  251. #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO)
  252. || dtl::is_floating_point<value_type>::value
  253. #endif
  254. );
  255. };
  256. template <typename O, typename R>
  257. struct enable_if_memzero_initializable
  258. : enable_if_c<dtl::is_memzero_initializable<O>::value, R>
  259. {};
  260. template <typename O, typename R>
  261. struct disable_if_memzero_initializable
  262. : enable_if_c<!dtl::is_memzero_initializable<O>::value, R>
  263. {};
  264. template <typename I, typename R>
  265. struct enable_if_trivially_destructible
  266. : enable_if_c < dtl::is_trivially_destructible
  267. <typename boost::container::iter_value<I>::type>::value
  268. , R>
  269. {};
  270. template <typename I, typename R>
  271. struct disable_if_trivially_destructible
  272. : enable_if_c <!dtl::is_trivially_destructible
  273. <typename boost::container::iter_value<I>::type>::value
  274. , R>
  275. {};
  276. } //namespace dtl {
  277. //////////////////////////////////////////////////////////////////////////////
  278. //
  279. // uninitialized_move_alloc
  280. //
  281. //////////////////////////////////////////////////////////////////////////////
  282. //! <b>Effects</b>:
  283. //! \code
  284. //! for (; f != l; ++r, ++f)
  285. //! allocator_traits::construct(a, &*r, boost::move(*f));
  286. //! \endcode
  287. //!
  288. //! <b>Returns</b>: r
  289. template
  290. <typename Allocator,
  291. typename I, // I models InputIterator
  292. typename F> // F models ForwardIterator
  293. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  294. uninitialized_move_alloc(Allocator &a, I f, I l, F r)
  295. {
  296. F back = r;
  297. BOOST_CONTAINER_TRY{
  298. while (f != l) {
  299. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
  300. ++f; ++r;
  301. }
  302. }
  303. BOOST_CONTAINER_CATCH(...){
  304. for (; back != r; ++back){
  305. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  306. }
  307. BOOST_CONTAINER_RETHROW;
  308. }
  309. BOOST_CONTAINER_CATCH_END
  310. return r;
  311. }
  312. template
  313. <typename Allocator,
  314. typename I, // I models InputIterator
  315. typename F> // F models ForwardIterator
  316. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  317. uninitialized_move_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  318. { return dtl::memmove(f, l, r); }
  319. //////////////////////////////////////////////////////////////////////////////
  320. //
  321. // uninitialized_move_alloc_n
  322. //
  323. //////////////////////////////////////////////////////////////////////////////
  324. //! <b>Effects</b>:
  325. //! \code
  326. //! for (; n--; ++r, ++f)
  327. //! allocator_traits::construct(a, &*r, boost::move(*f));
  328. //! \endcode
  329. //!
  330. //! <b>Returns</b>: r
  331. template
  332. <typename Allocator,
  333. typename I, // I models InputIterator
  334. typename F> // F models ForwardIterator
  335. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  336. uninitialized_move_alloc_n(Allocator &a, I f, std::size_t n, F r)
  337. {
  338. F back = r;
  339. BOOST_CONTAINER_TRY{
  340. while (n) {
  341. --n;
  342. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
  343. ++f; ++r;
  344. }
  345. }
  346. BOOST_CONTAINER_CATCH(...){
  347. for (; back != r; ++back){
  348. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  349. }
  350. BOOST_CONTAINER_RETHROW;
  351. }
  352. BOOST_CONTAINER_CATCH_END
  353. return r;
  354. }
  355. template
  356. <typename Allocator,
  357. typename I, // I models InputIterator
  358. typename F> // F models ForwardIterator
  359. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  360. uninitialized_move_alloc_n(Allocator &, I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  361. { return dtl::memmove_n(f, n, r); }
  362. //////////////////////////////////////////////////////////////////////////////
  363. //
  364. // uninitialized_move_alloc_n_source
  365. //
  366. //////////////////////////////////////////////////////////////////////////////
  367. //! <b>Effects</b>:
  368. //! \code
  369. //! for (; n--; ++r, ++f)
  370. //! allocator_traits::construct(a, &*r, boost::move(*f));
  371. //! \endcode
  372. //!
  373. //! <b>Returns</b>: f (after incremented)
  374. template
  375. <typename Allocator,
  376. typename I, // I models InputIterator
  377. typename F> // F models ForwardIterator
  378. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, I>::type
  379. uninitialized_move_alloc_n_source(Allocator &a, I f, std::size_t n, F r)
  380. {
  381. F back = r;
  382. BOOST_CONTAINER_TRY{
  383. while (n) {
  384. --n;
  385. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
  386. ++f; ++r;
  387. }
  388. }
  389. BOOST_CONTAINER_CATCH(...){
  390. for (; back != r; ++back){
  391. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  392. }
  393. BOOST_CONTAINER_RETHROW;
  394. }
  395. BOOST_CONTAINER_CATCH_END
  396. return f;
  397. }
  398. template
  399. <typename Allocator,
  400. typename I, // I models InputIterator
  401. typename F> // F models ForwardIterator
  402. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
  403. uninitialized_move_alloc_n_source(Allocator &, I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  404. { return dtl::memmove_n_source(f, n, r); }
  405. //////////////////////////////////////////////////////////////////////////////
  406. //
  407. // uninitialized_copy_alloc
  408. //
  409. //////////////////////////////////////////////////////////////////////////////
  410. //! <b>Effects</b>:
  411. //! \code
  412. //! for (; f != l; ++r, ++f)
  413. //! allocator_traits::construct(a, &*r, *f);
  414. //! \endcode
  415. //!
  416. //! <b>Returns</b>: r
  417. template
  418. <typename Allocator,
  419. typename I, // I models InputIterator
  420. typename F> // F models ForwardIterator
  421. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  422. uninitialized_copy_alloc(Allocator &a, I f, I l, F r)
  423. {
  424. F back = r;
  425. BOOST_CONTAINER_TRY{
  426. while (f != l) {
  427. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), *f);
  428. ++f; ++r;
  429. }
  430. }
  431. BOOST_CONTAINER_CATCH(...){
  432. for (; back != r; ++back){
  433. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  434. }
  435. BOOST_CONTAINER_RETHROW;
  436. }
  437. BOOST_CONTAINER_CATCH_END
  438. return r;
  439. }
  440. template
  441. <typename Allocator,
  442. typename I, // I models InputIterator
  443. typename F> // F models ForwardIterator
  444. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  445. uninitialized_copy_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  446. { return dtl::memmove(f, l, r); }
  447. //////////////////////////////////////////////////////////////////////////////
  448. //
  449. // uninitialized_copy_alloc_n
  450. //
  451. //////////////////////////////////////////////////////////////////////////////
  452. //! <b>Effects</b>:
  453. //! \code
  454. //! for (; n--; ++r, ++f)
  455. //! allocator_traits::construct(a, &*r, *f);
  456. //! \endcode
  457. //!
  458. //! <b>Returns</b>: r
  459. template
  460. <typename Allocator,
  461. typename I, // I models InputIterator
  462. typename F> // F models ForwardIterator
  463. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  464. uninitialized_copy_alloc_n(Allocator &a, I f, std::size_t n, F r)
  465. {
  466. F back = r;
  467. BOOST_CONTAINER_TRY{
  468. while (n) {
  469. --n;
  470. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), *f);
  471. ++f; ++r;
  472. }
  473. }
  474. BOOST_CONTAINER_CATCH(...){
  475. for (; back != r; ++back){
  476. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  477. }
  478. BOOST_CONTAINER_RETHROW;
  479. }
  480. BOOST_CONTAINER_CATCH_END
  481. return r;
  482. }
  483. template
  484. <typename Allocator,
  485. typename I, // I models InputIterator
  486. typename F> // F models ForwardIterator
  487. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  488. uninitialized_copy_alloc_n(Allocator &, I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  489. { return dtl::memmove_n(f, n, r); }
  490. //////////////////////////////////////////////////////////////////////////////
  491. //
  492. // uninitialized_copy_alloc_n_source
  493. //
  494. //////////////////////////////////////////////////////////////////////////////
  495. //! <b>Effects</b>:
  496. //! \code
  497. //! for (; n--; ++r, ++f)
  498. //! allocator_traits::construct(a, &*r, *f);
  499. //! \endcode
  500. //!
  501. //! <b>Returns</b>: f (after incremented)
  502. template
  503. <typename Allocator,
  504. typename I, // I models InputIterator
  505. typename F> // F models ForwardIterator
  506. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, I>::type
  507. uninitialized_copy_alloc_n_source(Allocator &a, I f, std::size_t n, F r)
  508. {
  509. F back = r;
  510. BOOST_CONTAINER_TRY{
  511. while (n) {
  512. boost::container::construct_in_place(a, boost::movelib::iterator_to_raw_pointer(r), f);
  513. ++f; ++r; --n;
  514. }
  515. }
  516. BOOST_CONTAINER_CATCH(...){
  517. for (; back != r; ++back){
  518. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  519. }
  520. BOOST_CONTAINER_RETHROW;
  521. }
  522. BOOST_CONTAINER_CATCH_END
  523. return f;
  524. }
  525. template
  526. <typename Allocator,
  527. typename I, // I models InputIterator
  528. typename F> // F models ForwardIterator
  529. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
  530. uninitialized_copy_alloc_n_source(Allocator &, I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  531. { return dtl::memmove_n_source(f, n, r); }
  532. //////////////////////////////////////////////////////////////////////////////
  533. //
  534. // uninitialized_value_init_alloc_n
  535. //
  536. //////////////////////////////////////////////////////////////////////////////
  537. //! <b>Effects</b>:
  538. //! \code
  539. //! for (; n--; ++r, ++f)
  540. //! allocator_traits::construct(a, &*r);
  541. //! \endcode
  542. //!
  543. //! <b>Returns</b>: r
  544. template
  545. <typename Allocator,
  546. typename F> // F models ForwardIterator
  547. inline typename dtl::disable_if_memzero_initializable<F, F>::type
  548. uninitialized_value_init_alloc_n(Allocator &a, std::size_t n, F r)
  549. {
  550. F back = r;
  551. BOOST_CONTAINER_TRY{
  552. while (n) {
  553. --n;
  554. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r));
  555. ++r;
  556. }
  557. }
  558. BOOST_CONTAINER_CATCH(...){
  559. for (; back != r; ++back){
  560. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  561. }
  562. BOOST_CONTAINER_RETHROW;
  563. }
  564. BOOST_CONTAINER_CATCH_END
  565. return r;
  566. }
  567. template
  568. <typename Allocator,
  569. typename F> // F models ForwardIterator
  570. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memzero_initializable<F, F>::type
  571. uninitialized_value_init_alloc_n(Allocator &, std::size_t n, F r)
  572. {
  573. typedef typename boost::container::iterator_traits<F>::value_type value_type;
  574. typedef typename boost::container::iterator_traits<F>::difference_type r_difference_type;
  575. if (BOOST_LIKELY(n != 0)){
  576. std::memset((void*)boost::movelib::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n);
  577. r += static_cast<r_difference_type>(n);
  578. }
  579. return r;
  580. }
  581. //////////////////////////////////////////////////////////////////////////////
  582. //
  583. // uninitialized_default_init_alloc_n
  584. //
  585. //////////////////////////////////////////////////////////////////////////////
  586. //! <b>Effects</b>:
  587. //! \code
  588. //! for (; n--; ++r, ++f)
  589. //! allocator_traits::construct(a, &*r);
  590. //! \endcode
  591. //!
  592. //! <b>Returns</b>: r
  593. template
  594. <typename Allocator,
  595. typename F> // F models ForwardIterator
  596. inline F uninitialized_default_init_alloc_n(Allocator &a, std::size_t n, F r)
  597. {
  598. F back = r;
  599. BOOST_CONTAINER_TRY{
  600. while (n) {
  601. --n;
  602. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), default_init);
  603. ++r;
  604. }
  605. }
  606. BOOST_CONTAINER_CATCH(...){
  607. for (; back != r; ++back){
  608. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  609. }
  610. BOOST_CONTAINER_RETHROW;
  611. }
  612. BOOST_CONTAINER_CATCH_END
  613. return r;
  614. }
  615. //////////////////////////////////////////////////////////////////////////////
  616. //
  617. // uninitialized_fill_alloc
  618. //
  619. //////////////////////////////////////////////////////////////////////////////
  620. //! <b>Effects</b>:
  621. //! \code
  622. //! for (; f != l; ++r, ++f)
  623. //! allocator_traits::construct(a, &*r, *f);
  624. //! \endcode
  625. //!
  626. //! <b>Returns</b>: r
  627. template
  628. <typename Allocator,
  629. typename F, // F models ForwardIterator
  630. typename T>
  631. inline void uninitialized_fill_alloc(Allocator &a, F f, F l, const T &t)
  632. {
  633. F back = f;
  634. BOOST_CONTAINER_TRY{
  635. while (f != l) {
  636. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(f), t);
  637. ++f;
  638. }
  639. }
  640. BOOST_CONTAINER_CATCH(...){
  641. for (; back != l; ++back){
  642. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  643. }
  644. BOOST_CONTAINER_RETHROW;
  645. }
  646. BOOST_CONTAINER_CATCH_END
  647. }
  648. //////////////////////////////////////////////////////////////////////////////
  649. //
  650. // uninitialized_fill_alloc_n
  651. //
  652. //////////////////////////////////////////////////////////////////////////////
  653. //! <b>Effects</b>:
  654. //! \code
  655. //! for (; n--; ++r, ++f)
  656. //! allocator_traits::construct(a, &*r, v);
  657. //! \endcode
  658. //!
  659. //! <b>Returns</b>: r
  660. template
  661. <typename Allocator,
  662. typename T,
  663. typename F> // F models ForwardIterator
  664. inline F uninitialized_fill_alloc_n(Allocator &a, const T &v, std::size_t n, F r)
  665. {
  666. F back = r;
  667. BOOST_CONTAINER_TRY{
  668. while (n) {
  669. --n;
  670. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), v);
  671. ++r;
  672. }
  673. }
  674. BOOST_CONTAINER_CATCH(...){
  675. for (; back != r; ++back){
  676. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  677. }
  678. BOOST_CONTAINER_RETHROW;
  679. }
  680. BOOST_CONTAINER_CATCH_END
  681. return r;
  682. }
  683. //////////////////////////////////////////////////////////////////////////////
  684. //
  685. // copy
  686. //
  687. //////////////////////////////////////////////////////////////////////////////
  688. template
  689. <typename I, // I models InputIterator
  690. typename F> // F models ForwardIterator
  691. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  692. copy(I f, I l, F r)
  693. {
  694. while (f != l) {
  695. *r = *f;
  696. ++f; ++r;
  697. }
  698. return r;
  699. }
  700. template
  701. <typename I, // I models InputIterator
  702. typename F> // F models ForwardIterator
  703. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  704. copy(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  705. { return dtl::memmove(f, l, r); }
  706. //////////////////////////////////////////////////////////////////////////////
  707. //
  708. // copy_n
  709. //
  710. //////////////////////////////////////////////////////////////////////////////
  711. template
  712. <typename I, // I models InputIterator
  713. typename U, // U models unsigned integral constant
  714. typename F> // F models ForwardIterator
  715. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  716. copy_n(I f, U n, F r)
  717. {
  718. while (n) {
  719. --n;
  720. *r = *f;
  721. ++f; ++r;
  722. }
  723. return r;
  724. }
  725. template
  726. <typename I, // I models InputIterator
  727. typename U, // U models unsigned integral constant
  728. typename F> // F models ForwardIterator
  729. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  730. copy_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  731. { return dtl::memmove_n(f, n, r); }
  732. //////////////////////////////////////////////////////////////////////////////
  733. //
  734. // copy_n_source
  735. //
  736. //////////////////////////////////////////////////////////////////////////////
  737. template
  738. <typename I, // I models InputIterator
  739. typename U, // U models unsigned integral constant
  740. typename F> // F models ForwardIterator
  741. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  742. copy_n_source(I f, U n, F r)
  743. {
  744. while (n) {
  745. --n;
  746. boost::container::assign_in_place(r, f);
  747. ++f; ++r;
  748. }
  749. return f;
  750. }
  751. template
  752. <typename I, // I models InputIterator
  753. typename F> // F models ForwardIterator
  754. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  755. copy_n_source(I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  756. { return dtl::memmove_n_source(f, n, r); }
  757. //////////////////////////////////////////////////////////////////////////////
  758. //
  759. // copy_n_source_dest
  760. //
  761. //////////////////////////////////////////////////////////////////////////////
  762. template
  763. <typename I, // I models InputIterator
  764. typename F> // F models ForwardIterator
  765. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  766. copy_n_source_dest(I f, std::size_t n, F &r)
  767. {
  768. while (n) {
  769. --n;
  770. *r = *f;
  771. ++f; ++r;
  772. }
  773. return f;
  774. }
  775. template
  776. <typename I, // I models InputIterator
  777. typename F> // F models ForwardIterator
  778. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  779. copy_n_source_dest(I f, std::size_t n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  780. { return dtl::memmove_n_source_dest(f, n, r); }
  781. //////////////////////////////////////////////////////////////////////////////
  782. //
  783. // move
  784. //
  785. //////////////////////////////////////////////////////////////////////////////
  786. template
  787. <typename I, // I models InputIterator
  788. typename F> // F models ForwardIterator
  789. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  790. move(I f, I l, F r)
  791. {
  792. while (f != l) {
  793. *r = ::boost::move(*f);
  794. ++f; ++r;
  795. }
  796. return r;
  797. }
  798. template
  799. <typename I, // I models InputIterator
  800. typename F> // F models ForwardIterator
  801. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  802. move(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  803. { return dtl::memmove(f, l, r); }
  804. //////////////////////////////////////////////////////////////////////////////
  805. //
  806. // move_n
  807. //
  808. //////////////////////////////////////////////////////////////////////////////
  809. template
  810. <typename I, // I models InputIterator
  811. typename U, // U models unsigned integral constant
  812. typename F> // F models ForwardIterator
  813. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  814. move_n(I f, U n, F r)
  815. {
  816. while (n) {
  817. --n;
  818. *r = ::boost::move(*f);
  819. ++f; ++r;
  820. }
  821. return r;
  822. }
  823. template
  824. <typename I, // I models InputIterator
  825. typename U, // U models unsigned integral constant
  826. typename F> // F models ForwardIterator
  827. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  828. move_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  829. { return dtl::memmove_n(f, n, r); }
  830. //////////////////////////////////////////////////////////////////////////////
  831. //
  832. // move_backward
  833. //
  834. //////////////////////////////////////////////////////////////////////////////
  835. template
  836. <typename I, // I models BidirectionalIterator
  837. typename F> // F models ForwardIterator
  838. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  839. move_backward(I f, I l, F r)
  840. {
  841. while (f != l) {
  842. --l; --r;
  843. *r = ::boost::move(*l);
  844. }
  845. return r;
  846. }
  847. template
  848. <typename I, // I models InputIterator
  849. typename F> // F models ForwardIterator
  850. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  851. move_backward(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  852. {
  853. typedef typename boost::container::iter_value<I>::type value_type;
  854. const std::size_t n = boost::container::iterator_udistance(f, l);
  855. if (BOOST_LIKELY(n != 0)){
  856. r -= n;
  857. std::memmove((boost::movelib::iterator_to_raw_pointer)(r), (boost::movelib::iterator_to_raw_pointer)(f), sizeof(value_type)*n);
  858. }
  859. return r;
  860. }
  861. //////////////////////////////////////////////////////////////////////////////
  862. //
  863. // move_n_source_dest
  864. //
  865. //////////////////////////////////////////////////////////////////////////////
  866. template
  867. <typename I // I models InputIterator
  868. ,typename U // U models unsigned integral constant
  869. ,typename F> // F models ForwardIterator
  870. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  871. move_n_source_dest(I f, U n, F &r)
  872. {
  873. while (n) {
  874. --n;
  875. *r = ::boost::move(*f);
  876. ++f; ++r;
  877. }
  878. return f;
  879. }
  880. template
  881. <typename I // I models InputIterator
  882. ,typename F> // F models ForwardIterator
  883. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  884. move_n_source_dest(I f, std::size_t n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  885. { return dtl::memmove_n_source_dest(f, n, r); }
  886. //////////////////////////////////////////////////////////////////////////////
  887. //
  888. // move_n_source
  889. //
  890. //////////////////////////////////////////////////////////////////////////////
  891. template
  892. <typename I // I models InputIterator
  893. ,typename U // U models unsigned integral constant
  894. ,typename F> // F models ForwardIterator
  895. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  896. move_n_source(I f, U n, F r)
  897. {
  898. while (n) {
  899. --n;
  900. *r = ::boost::move(*f);
  901. ++f; ++r;
  902. }
  903. return f;
  904. }
  905. template
  906. <typename I // I models InputIterator
  907. ,typename F> // F models ForwardIterator
  908. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  909. move_n_source(I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  910. { return dtl::memmove_n_source(f, n, r); }
  911. template<typename F> // F models ForwardIterator
  912. BOOST_CONTAINER_FORCEINLINE F move_forward_overlapping(F f, F l, F r)
  913. {
  914. return (f != r) ? (move)(f, l, r) : l;
  915. }
  916. template<typename B> // B models BidirIterator
  917. BOOST_CONTAINER_FORCEINLINE B move_backward_overlapping(B f, B l, B rl)
  918. {
  919. return (l != rl) ? (move_backward)(f, l, rl) : f;
  920. }
  921. //////////////////////////////////////////////////////////////////////////////
  922. //
  923. // destroy_alloc_n
  924. //
  925. //////////////////////////////////////////////////////////////////////////////
  926. template
  927. <typename Allocator
  928. ,typename I // I models InputIterator
  929. ,typename U> // U models unsigned integral constant
  930. inline typename dtl::disable_if_trivially_destructible<I, void>::type
  931. destroy_alloc_n(Allocator &a, I f, U n)
  932. {
  933. while(n){
  934. --n;
  935. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(f));
  936. ++f;
  937. }
  938. }
  939. template
  940. <typename Allocator
  941. ,typename I // I models InputIterator
  942. ,typename U> // U models unsigned integral constant
  943. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_trivially_destructible<I, void>::type
  944. destroy_alloc_n(Allocator &, I, U)
  945. {}
  946. //////////////////////////////////////////////////////////////////////////////
  947. //
  948. // destroy_alloc
  949. //
  950. //////////////////////////////////////////////////////////////////////////////
  951. template
  952. <typename Allocator
  953. ,typename I> // I models InputIterator
  954. inline typename dtl::disable_if_trivially_destructible<I, void>::type
  955. destroy_alloc(Allocator &a, I f, I l)
  956. {
  957. while(f != l){
  958. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(f));
  959. ++f;
  960. }
  961. }
  962. template
  963. <typename Allocator
  964. ,typename I > // I models InputIterator
  965. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_trivially_destructible<I, void>::type
  966. destroy_alloc(Allocator &, I, I)
  967. {}
  968. //////////////////////////////////////////////////////////////////////////////
  969. //
  970. // deep_swap_alloc_n
  971. //
  972. //////////////////////////////////////////////////////////////////////////////
  973. template
  974. <std::size_t MaxTmpBytes
  975. ,typename Allocator
  976. ,typename F // F models ForwardIterator
  977. ,typename G // G models ForwardIterator
  978. >
  979. inline typename dtl::disable_if_memtransfer_copy_assignable<F, G, void>::type
  980. deep_swap_alloc_n( Allocator &a, F short_range_f, std::size_t n_i, G large_range_f, std::size_t n_j)
  981. {
  982. std::size_t n = 0;
  983. for (; n != n_i ; ++short_range_f, ++large_range_f, ++n){
  984. boost::adl_move_swap(*short_range_f, *large_range_f);
  985. }
  986. boost::container::uninitialized_move_alloc_n(a, large_range_f, std::size_t(n_j - n_i), short_range_f); // may throw
  987. boost::container::destroy_alloc_n(a, large_range_f, std::size_t(n_j - n_i));
  988. }
  989. static const std::size_t DeepSwapAllocNMaxStorage = std::size_t(1) << std::size_t(11); //2K bytes
  990. template
  991. <std::size_t MaxTmpBytes
  992. ,typename Allocator
  993. ,typename F // F models ForwardIterator
  994. ,typename G // G models ForwardIterator
  995. >
  996. inline typename dtl::enable_if_c
  997. < dtl::is_memtransfer_copy_assignable<F, G>::value && (MaxTmpBytes <= DeepSwapAllocNMaxStorage) && false
  998. , void>::type
  999. deep_swap_alloc_n( Allocator &a, F short_range_f, std::size_t n_i, G large_range_f, std::size_t n_j)
  1000. {
  1001. typedef typename allocator_traits<Allocator>::value_type value_type;
  1002. typedef typename dtl::aligned_storage
  1003. <MaxTmpBytes, dtl::alignment_of<value_type>::value>::type storage_type;
  1004. storage_type storage;
  1005. const std::size_t n_i_bytes = sizeof(value_type)*n_i;
  1006. void *const large_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f));
  1007. void *const short_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f));
  1008. void *const stora_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(storage.data));
  1009. std::memcpy(stora_ptr, large_ptr, n_i_bytes);
  1010. std::memcpy(large_ptr, short_ptr, n_i_bytes);
  1011. std::memcpy(short_ptr, stora_ptr, n_i_bytes);
  1012. boost::container::iterator_uadvance(large_range_f, n_i);
  1013. boost::container::iterator_uadvance(short_range_f, n_i);
  1014. boost::container::uninitialized_move_alloc_n(a, large_range_f, std::size_t(n_j - n_i), short_range_f); // may throw
  1015. boost::container::destroy_alloc_n(a, large_range_f, std::size_t(n_j - n_i));
  1016. }
  1017. template
  1018. <std::size_t MaxTmpBytes
  1019. ,typename Allocator
  1020. ,typename F // F models ForwardIterator
  1021. ,typename G // G models ForwardIterator
  1022. >
  1023. inline typename dtl::enable_if_c
  1024. < dtl::is_memtransfer_copy_assignable<F, G>::value && true//(MaxTmpBytes > DeepSwapAllocNMaxStorage)
  1025. , void>::type
  1026. deep_swap_alloc_n( Allocator &a, F short_range_f, std::size_t n_i, G large_range_f, std::size_t n_j)
  1027. {
  1028. typedef typename allocator_traits<Allocator>::value_type value_type;
  1029. typedef typename dtl::aligned_storage
  1030. <DeepSwapAllocNMaxStorage, dtl::alignment_of<value_type>::value>::type storage_type;
  1031. storage_type storage;
  1032. const std::size_t sizeof_storage = sizeof(storage);
  1033. std::size_t n_i_bytes = sizeof(value_type)*n_i;
  1034. char *large_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f)));
  1035. char *short_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f)));
  1036. char *stora_ptr = static_cast<char*>(static_cast<void*>(storage.data));
  1037. std::size_t szt_times = n_i_bytes/sizeof_storage;
  1038. const std::size_t szt_rem = n_i_bytes%sizeof_storage;
  1039. //Loop unrolling using Duff's device, as it seems it helps on some architectures
  1040. const std::size_t Unroll = 4;
  1041. std::size_t n = (szt_times + (Unroll-1))/Unroll;
  1042. const std::size_t branch_number = (szt_times == 0)*Unroll + (szt_times % Unroll);
  1043. switch(branch_number){
  1044. case 4:
  1045. break;
  1046. case 0: do{
  1047. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1048. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1049. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1050. large_ptr += sizeof_storage;
  1051. short_ptr += sizeof_storage;
  1052. BOOST_FALLTHROUGH;
  1053. case 3:
  1054. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1055. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1056. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1057. large_ptr += sizeof_storage;
  1058. short_ptr += sizeof_storage;
  1059. BOOST_FALLTHROUGH;
  1060. case 2:
  1061. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1062. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1063. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1064. large_ptr += sizeof_storage;
  1065. short_ptr += sizeof_storage;
  1066. BOOST_FALLTHROUGH;
  1067. case 1:
  1068. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1069. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1070. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1071. large_ptr += sizeof_storage;
  1072. short_ptr += sizeof_storage;
  1073. } while(--n);
  1074. }
  1075. std::memcpy(stora_ptr, large_ptr, szt_rem);
  1076. std::memcpy(large_ptr, short_ptr, szt_rem);
  1077. std::memcpy(short_ptr, stora_ptr, szt_rem);
  1078. boost::container::iterator_uadvance(large_range_f, n_i);
  1079. boost::container::iterator_uadvance(short_range_f, n_i);
  1080. boost::container::uninitialized_move_alloc_n(a, large_range_f, std::size_t(n_j - n_i), short_range_f); // may throw
  1081. boost::container::destroy_alloc_n(a, large_range_f, std::size_t(n_j - n_i));
  1082. }
  1083. //////////////////////////////////////////////////////////////////////////////
  1084. //
  1085. // copy_assign_range_alloc_n
  1086. //
  1087. //////////////////////////////////////////////////////////////////////////////
  1088. template
  1089. <typename Allocator
  1090. ,typename I // F models InputIterator
  1091. ,typename O // G models OutputIterator
  1092. >
  1093. void copy_assign_range_alloc_n( Allocator &a, I inp_start, std::size_t n_i, O out_start, std::size_t n_o )
  1094. {
  1095. if (n_o < n_i){
  1096. inp_start = boost::container::copy_n_source_dest(inp_start, n_o, out_start); // may throw
  1097. boost::container::uninitialized_copy_alloc_n(a, inp_start, std::size_t(n_i - n_o), out_start);// may throw
  1098. }
  1099. else{
  1100. out_start = boost::container::copy_n(inp_start, n_i, out_start); // may throw
  1101. boost::container::destroy_alloc_n(a, out_start, std::size_t(n_o - n_i));
  1102. }
  1103. }
  1104. //////////////////////////////////////////////////////////////////////////////
  1105. //
  1106. // move_assign_range_alloc_n
  1107. //
  1108. //////////////////////////////////////////////////////////////////////////////
  1109. template
  1110. <typename Allocator
  1111. ,typename I // F models InputIterator
  1112. ,typename O // G models OutputIterator
  1113. >
  1114. void move_assign_range_alloc_n( Allocator &a, I inp_start, std::size_t n_i, O out_start, std::size_t n_o )
  1115. {
  1116. if (n_o < n_i){
  1117. inp_start = boost::container::move_n_source_dest(inp_start, n_o, out_start); // may throw
  1118. boost::container::uninitialized_move_alloc_n(a, inp_start, std::size_t(n_i - n_o), out_start); // may throw
  1119. }
  1120. else{
  1121. out_start = boost::container::move_n(inp_start, n_i, out_start); // may throw
  1122. boost::container::destroy_alloc_n(a, out_start, std::size_t(n_o - n_i));
  1123. }
  1124. }
  1125. template<class Allocator>
  1126. struct array_destructor
  1127. {
  1128. typedef typename ::boost::container::allocator_traits<Allocator>::value_type value_type;
  1129. typedef typename dtl::if_c
  1130. <dtl::is_trivially_destructible<value_type>::value
  1131. ,dtl::null_scoped_destructor_range<Allocator>
  1132. ,dtl::scoped_destructor_range<Allocator>
  1133. >::type type;
  1134. };
  1135. template<class Allocator>
  1136. struct value_destructor
  1137. {
  1138. typedef typename ::boost::container::allocator_traits<Allocator>::value_type value_type;
  1139. typedef typename dtl::if_c
  1140. <dtl::is_trivially_destructible<value_type>::value
  1141. , dtl::null_scoped_destructor<Allocator>
  1142. , dtl::scoped_destructor<Allocator>
  1143. >::type type;
  1144. };
  1145. template
  1146. <typename Allocator
  1147. ,typename F // F models ForwardIterator
  1148. ,typename O // G models OutputIterator
  1149. ,typename InsertionProxy
  1150. >
  1151. void uninitialized_move_and_insert_alloc
  1152. ( Allocator &a
  1153. , F first
  1154. , F pos
  1155. , F last
  1156. , O d_first
  1157. , std::size_t n
  1158. , InsertionProxy insertion_proxy)
  1159. {
  1160. typedef typename array_destructor<Allocator>::type array_destructor_t;
  1161. //Anti-exception rollbacks
  1162. array_destructor_t new_values_destroyer(d_first, d_first, a);
  1163. //Initialize with [begin(), pos) old buffer
  1164. //the start of the new buffer
  1165. O d_last = ::boost::container::uninitialized_move_alloc(a, first, pos, d_first);
  1166. new_values_destroyer.set_end(d_last);
  1167. //Initialize new objects, starting from previous point
  1168. insertion_proxy.uninitialized_copy_n_and_update(a, d_last, n);
  1169. d_last += n;
  1170. new_values_destroyer.set_end(d_last);
  1171. //Initialize from the rest of the old buffer,
  1172. //starting from previous point
  1173. (void) ::boost::container::uninitialized_move_alloc(a, pos, last, d_last);
  1174. //All construction successful, disable rollbacks
  1175. new_values_destroyer.release();
  1176. }
  1177. template
  1178. <typename Allocator
  1179. ,typename F // F models ForwardIterator
  1180. ,typename InsertionProxy
  1181. >
  1182. typename dtl::enable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1183. expand_backward_and_insert_nonempty_middle_alloc
  1184. ( Allocator &a
  1185. , F const first
  1186. , F const pos
  1187. , std::size_t const
  1188. , InsertionProxy insertion_proxy)
  1189. {
  1190. BOOST_ASSERT(first != pos);
  1191. typedef typename value_destructor<Allocator>::type value_destructor_t;
  1192. F aux = first; --aux;
  1193. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(aux), boost::move(*first));
  1194. value_destructor_t on_exception(a, boost::movelib::iterator_to_raw_pointer(aux));
  1195. //Copy previous to last objects to the initialized end
  1196. aux = first; ++aux;
  1197. aux = boost::container::move(aux, pos, first);
  1198. //Insert new objects in the pos
  1199. insertion_proxy.copy_n_and_update(a, aux, 1u);
  1200. on_exception.release();
  1201. }
  1202. template
  1203. <typename Allocator
  1204. ,typename F // F models ForwardIterator
  1205. ,typename InsertionProxy
  1206. >
  1207. typename dtl::disable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1208. expand_backward_and_insert_nonempty_middle_alloc
  1209. ( Allocator &a
  1210. , F first
  1211. , F pos
  1212. , std::size_t const n
  1213. , InsertionProxy insertion_proxy)
  1214. {
  1215. BOOST_ASSERT(first != pos);
  1216. BOOST_ASSERT(n != 0);
  1217. typedef typename array_destructor<Allocator>::type array_destructor_t;
  1218. const std::size_t elems_before = iterator_udistance(first, pos);
  1219. if(elems_before >= n){
  1220. //New elements can be just copied.
  1221. //Move to uninitialized memory last objects
  1222. F const first_less_n = first - n;
  1223. F nxt = ::boost::container::uninitialized_move_alloc_n_source(a, first, n, first_less_n);
  1224. array_destructor_t on_exception(first_less_n, first, a);
  1225. //Copy previous to last objects to the initialized end
  1226. nxt = boost::container::move(nxt, pos, first);
  1227. //Insert new objects in the pos
  1228. insertion_proxy.copy_n_and_update(a, nxt, n);
  1229. on_exception.release();
  1230. }
  1231. else {
  1232. //The new elements don't fit in the [pos, end()) range.
  1233. //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
  1234. F aux = ::boost::container::uninitialized_move_alloc(a, first, pos, first - n);
  1235. array_destructor_t on_exception(first -n, aux, a);
  1236. //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
  1237. insertion_proxy.uninitialized_copy_n_and_update(a, aux, std::size_t(n - elems_before));
  1238. insertion_proxy.copy_n_and_update(a, first, elems_before);
  1239. on_exception.release();
  1240. }
  1241. }
  1242. template
  1243. <typename Allocator
  1244. ,typename F // F models ForwardIterator
  1245. ,typename InsertionProxy
  1246. >
  1247. typename dtl::enable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1248. expand_forward_and_insert_nonempty_middle_alloc
  1249. ( Allocator &a
  1250. , F pos
  1251. , F last
  1252. , std::size_t const
  1253. , InsertionProxy insertion_proxy)
  1254. {
  1255. BOOST_ASSERT(last != pos);
  1256. typedef typename value_destructor<Allocator>::type value_destructor_t;
  1257. F last_m_n = last; --last_m_n;
  1258. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(last), boost::move(*last_m_n));
  1259. value_destructor_t on_exception(a, boost::movelib::iterator_to_raw_pointer(last));
  1260. //Copy previous to last objects to the initialized end
  1261. boost::container::move_backward(pos, last_m_n, last);
  1262. //Insert new objects in the pos
  1263. insertion_proxy.copy_n_and_update(a, pos, 1);
  1264. on_exception.release();
  1265. }
  1266. template
  1267. <typename Allocator
  1268. ,typename F // F models ForwardIterator
  1269. ,typename InsertionProxy
  1270. >
  1271. typename dtl::disable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1272. expand_forward_and_insert_nonempty_middle_alloc
  1273. ( Allocator &a
  1274. , F pos
  1275. , F last
  1276. , std::size_t const n
  1277. , InsertionProxy insertion_proxy)
  1278. {
  1279. BOOST_ASSERT(last != pos);
  1280. BOOST_ASSERT(n != 0);
  1281. typedef typename array_destructor<Allocator>::type array_destructor_t;
  1282. const std::size_t elems_after = iterator_udistance(pos, last);
  1283. if(elems_after >= n){
  1284. //New elements can be just copied.
  1285. //Move to uninitialized memory last objects
  1286. F const last_m_n = last - n;
  1287. F const nxt = ::boost::container::uninitialized_move_alloc_n(a, last_m_n, n, last);
  1288. array_destructor_t on_exception(last, nxt, a);
  1289. //Copy previous to last objects to the initialized end
  1290. boost::container::move_backward(pos, last_m_n, last);
  1291. //Insert new objects in the pos
  1292. insertion_proxy.copy_n_and_update(a, pos, n);
  1293. on_exception.release();
  1294. }
  1295. else {
  1296. //The new elements don't fit in the [pos, end()) range.
  1297. //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
  1298. F new_last = ::boost::container::uninitialized_move_alloc(a, pos, last, pos + n);
  1299. array_destructor_t on_exception(pos + n, new_last, a);
  1300. //Copy first new elements in pos (gap is still there)
  1301. insertion_proxy.copy_n_and_update(a, pos, elems_after);
  1302. //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
  1303. insertion_proxy.uninitialized_copy_n_and_update(a, last, std::size_t(n - elems_after));
  1304. on_exception.release();
  1305. }
  1306. }
  1307. template
  1308. <typename Allocator
  1309. , typename F // F models ForwardIterator
  1310. , typename InsertionProxy
  1311. >
  1312. BOOST_CONTAINER_FORCEINLINE void expand_forward_and_insert_alloc
  1313. ( Allocator& a
  1314. , F pos
  1315. , F last
  1316. , std::size_t const n
  1317. , InsertionProxy insertion_proxy)
  1318. {
  1319. if (last == pos) {
  1320. insertion_proxy.uninitialized_copy_n_and_update(a, last, n);
  1321. }
  1322. else{
  1323. const bool single_value = dtl::is_single_value_proxy<InsertionProxy>::value;
  1324. BOOST_IF_CONSTEXPR(!single_value){
  1325. if (BOOST_UNLIKELY(!n)) {
  1326. return;
  1327. }
  1328. }
  1329. expand_forward_and_insert_nonempty_middle_alloc(a, pos, last, n, insertion_proxy);
  1330. }
  1331. }
  1332. template <class B, class InsertionProxy, class Allocator>
  1333. void expand_backward_forward_and_insert_alloc_move_backward
  1334. ( B const old_start
  1335. , std::size_t const old_size
  1336. , B const new_start
  1337. , B const pos
  1338. , std::size_t const n
  1339. , InsertionProxy insertion_proxy
  1340. , Allocator& a)
  1341. {
  1342. typedef std::size_t size_type;
  1343. typedef typename allocator_traits<Allocator>::value_type value_type;
  1344. static const bool trivial_dctr_after_move = has_trivial_destructor_after_move<value_type>::value;
  1345. static const bool trivial_dctr = dtl::is_trivially_destructible<value_type>::value;
  1346. typedef typename dtl::if_c
  1347. <trivial_dctr
  1348. , dtl::null_scoped_destructor_n<Allocator, B>
  1349. , dtl::scoped_destructor_n<Allocator, B>
  1350. >::type array_destructor_t;
  1351. //n can be zero to just expand capacity
  1352. B old_finish = make_iterator_uadvance(old_start, old_size);
  1353. //We can have 8 possibilities:
  1354. const size_type elemsbefore = static_cast<size_type>(iterator_udistance(old_start, pos));
  1355. const size_type raw_before = static_cast<size_type>(iterator_udistance(new_start, old_start));
  1356. const size_type before_plus_new = size_type(elemsbefore + n);
  1357. //Check if raw_before is big enough to hold the beginning of old data + new data
  1358. if (raw_before >= before_plus_new) {
  1359. //If anything goes wrong, this object will destroy
  1360. //all the old objects to fulfill previous vector state
  1361. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1362. // _________________________________________________________
  1363. //| raw_mem | old_begin | old_end | //Old situation
  1364. //| __________________________________|___________|_________|
  1365. // _________________________________________________________
  1366. //| old_begin | new | raw_mem | old_begin | old_end | //First step
  1367. //|___________|__________|____________|___________|_________|
  1368. //Copy first old values before pos, after that the new objects
  1369. B const new_elem_pos = ::boost::container::uninitialized_move_alloc(a, old_start, pos, new_start);
  1370. array_destructor_t new_values_destroyer(new_start, a, elemsbefore);
  1371. insertion_proxy.uninitialized_copy_n_and_update(a, new_elem_pos, n);
  1372. new_values_destroyer.set_size(before_plus_new);
  1373. const size_type new_size = size_type(old_size + n);
  1374. //Check if raw_before is so big that even copying the old data + new data
  1375. //there is a gap between the new data and the old data
  1376. if (raw_before >= new_size) {
  1377. // _______________________________________________________
  1378. //| raw_mem | old_begin | old_end | //Old situation
  1379. //|_________________________________|___________|_________|
  1380. // _______________________________________________________
  1381. //| old_begin | new | raw_mem | old_begin | old_end | //First step
  1382. //|___________|________|____________|___________|_________|
  1383. // _______________________________________________________
  1384. //| old_begin | new | old_end | raw_mem | //New situation
  1385. //|___________|________|_________|________________________|
  1386. //
  1387. //Now initialize the rest of memory with the last old values
  1388. if (before_plus_new != new_size) { //Special case to avoid operations in back insertion
  1389. B new_start_end(make_iterator_uadvance(new_start, before_plus_new));
  1390. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, new_start_end);
  1391. }
  1392. //All new elements correctly constructed, avoid new element destruction
  1393. new_values_destroyer.release();
  1394. //Old values destroyed automatically with "old_values_destroyer"
  1395. //when "old_values_destroyer" goes out of scope unless the have trivial
  1396. //destructor after move.
  1397. if(trivial_dctr_after_move)
  1398. old_values_destroyer.release();
  1399. }
  1400. //raw_before is so big that divides old_end
  1401. else {
  1402. // _________________________________________________
  1403. //| raw | old_beg | old_end | //Old situation
  1404. //|_____________________________|_________|_________|
  1405. // _________________________________________________
  1406. //| old_begin | new | raw | old_beg | old_end | //First step
  1407. //|___________|__________|______|_________|_________|
  1408. // _________________________________________________
  1409. //| old_begin | new | old_end | raw_mem | //New situation
  1410. //|___________|__________|_________|________________|
  1411. //Now initialize the rest of memory with the last old values
  1412. //All new elements correctly constructed, avoid new element destruction
  1413. BOOST_IF_CONSTEXPR(!trivial_dctr) {
  1414. //Now initialize the rest of raw_before memory with the
  1415. //first of elements after new values
  1416. const size_type raw_gap = raw_before - before_plus_new;
  1417. B new_start_plus(make_iterator_uadvance(new_start, before_plus_new));
  1418. ::boost::container::uninitialized_move_alloc_n(a, pos, raw_gap, new_start_plus);
  1419. new_values_destroyer.release();
  1420. old_values_destroyer.increment_size_backwards(raw_before);
  1421. //Now move remaining last objects in the old buffer begin
  1422. B remaining_pos(make_iterator_uadvance(pos, raw_gap));
  1423. remaining_pos = ::boost::container::move_forward_overlapping(remaining_pos, old_finish, old_start);
  1424. (void)remaining_pos;
  1425. //Once moved, avoid calling the destructors if trivial after move
  1426. if(!trivial_dctr_after_move) {
  1427. boost::container::destroy_alloc(a, remaining_pos, old_finish);
  1428. }
  1429. }
  1430. else { //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
  1431. ::boost::container::uninitialized_move_alloc_n
  1432. (a, pos, static_cast<size_type>(old_finish - pos), make_iterator_uadvance(new_start, before_plus_new));
  1433. }
  1434. old_values_destroyer.release();
  1435. }
  1436. }
  1437. else {
  1438. //If anything goes wrong, this object will destroy
  1439. //all the old objects to fulfill previous vector state
  1440. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1441. //Check if we have to do the insertion in two phases
  1442. //since maybe raw_before is not big enough and
  1443. //the buffer was expanded both sides
  1444. // _________________________________________________
  1445. //| raw_mem | old_begin + old_end | raw_mem | //Old situation
  1446. //|_________|_____________________|_________________|
  1447. // _________________________________________________
  1448. //| old_begin + new + old_end | raw_mem | //New situation with do_after
  1449. //|___________________________________|_____________|
  1450. // _________________________________________________
  1451. //| old_begin + new + old_end | raw_mem | //New without do_after
  1452. //|____________________________|____________________|
  1453. //
  1454. const bool do_after = n > raw_before;
  1455. //Now we can have two situations: the raw_mem of the
  1456. //beginning divides the old_begin, or the new elements:
  1457. if (raw_before <= elemsbefore) {
  1458. //The raw memory divides the old_begin group:
  1459. //
  1460. //If we need two phase construction (do_after)
  1461. //new group is divided in new = new_beg + new_end groups
  1462. //In this phase only new_beg will be inserted
  1463. //
  1464. // _________________________________________________
  1465. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1466. //|_________|___________|_________|_________________|
  1467. // _________________________________________________
  1468. //| old_begin | new_beg | old_end | raw_mem | //New situation with do_after(1),
  1469. //|___________|_________|_________|_________________| //not definitive, pending operations
  1470. // _________________________________________________
  1471. //| old_begin | new | old_end | raw_mem | //New situation without do_after,
  1472. //|___________|_____|_________|_____________________| //definitive.
  1473. //
  1474. //Copy the first part of old_begin to raw_mem
  1475. ::boost::container::uninitialized_move_alloc_n(a, old_start, raw_before, new_start);
  1476. //The buffer is all constructed until old_end,
  1477. //so program trailing destruction and assign final size
  1478. //if !do_after, raw_before+n otherwise.
  1479. size_type new_1st_range;
  1480. old_values_destroyer.increment_size_backwards(raw_before);
  1481. new_1st_range = do_after ? raw_before : n;
  1482. //Now copy the second part of old_begin overwriting itself
  1483. B const old_next(make_iterator_uadvance(old_start, raw_before));
  1484. B const next = ::boost::container::move(old_next, pos, old_start);
  1485. //Now copy the new_beg elements
  1486. insertion_proxy.copy_n_and_update(a, next, new_1st_range);
  1487. //If there is no after work and the last old part needs to be moved to front, do it
  1488. if (!do_after) {
  1489. //Now displace old_end elements and destroy trailing
  1490. B const new_first(make_iterator_uadvance(next, new_1st_range));
  1491. B const p = ::boost::container::move_forward_overlapping(pos, old_finish, new_first);
  1492. (void)p;
  1493. if(!trivial_dctr_after_move)
  1494. boost::container::destroy_alloc(a, p, old_finish);
  1495. }
  1496. }
  1497. else {
  1498. //If we have to expand both sides,
  1499. //we will play if the first new values so
  1500. //calculate the upper bound of new values
  1501. //The raw memory divides the new elements
  1502. //
  1503. //If we need two phase construction (do_after)
  1504. //new group is divided in new = new_beg + new_end groups
  1505. //In this phase only new_beg will be inserted
  1506. //
  1507. // ____________________________________________________
  1508. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1509. //|_______________|___________|_________|______________|
  1510. // ____________________________________________________
  1511. //| old_begin | new_beg | old_end | raw_mem | //New situation with do_after(),
  1512. //|___________|_______________|_________|______________| //not definitive, pending operations
  1513. // ____________________________________________________
  1514. //| old_begin | new | old_end | raw_mem | //New situation without do_after,
  1515. //|___________|_____|_________|________________________| //definitive
  1516. //
  1517. //First copy whole old_begin and part of new to raw_mem
  1518. B const new_pos = ::boost::container::uninitialized_move_alloc(a, old_start, pos, new_start);
  1519. array_destructor_t new_values_destroyer(new_start, a, elemsbefore);
  1520. const size_type mid_n = size_type(raw_before - elemsbefore);
  1521. insertion_proxy.uninitialized_copy_n_and_update(a, new_pos, mid_n);
  1522. new_values_destroyer.release();
  1523. //The buffer is all constructed until old_end
  1524. old_values_destroyer.increment_size_backwards(raw_before);
  1525. if (do_after) {
  1526. //Copy new_beg part
  1527. insertion_proxy.copy_n_and_update(a, old_start, elemsbefore);
  1528. }
  1529. else {
  1530. //Copy all new elements
  1531. const size_type rest_new = size_type(n - mid_n);
  1532. insertion_proxy.copy_n_and_update(a, old_start, rest_new);
  1533. B move_start(make_iterator_uadvance(old_start, rest_new));
  1534. //Displace old_end, but make sure data has to be moved
  1535. B const move_end = ::boost::container::move_forward_overlapping(pos, old_finish, move_start);
  1536. (void)move_end; //To avoid warnings of unused initialization for move_end in case
  1537. //trivial_dctr_after_move is true
  1538. //Destroy remaining moved elements from old_end except if they
  1539. //have trivial destructor after being moved
  1540. if(!trivial_dctr_after_move) {
  1541. boost::container::destroy_alloc(a, move_end, old_finish);
  1542. }
  1543. }
  1544. }
  1545. //This is only executed if two phase construction is needed
  1546. if (do_after) {
  1547. //The raw memory divides the new elements
  1548. // ______________________________________________________
  1549. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1550. //|______________|___________|____________|______________|
  1551. // _______________________________________________________
  1552. //| old_begin + new_beg | new_end |old_end | rawmem | //New situation with do_after(1)
  1553. //|__________________________|_________|________|________|
  1554. // ______________________________________________________
  1555. //| old_begin + new | old_end |raw | //New situation with do_after(2)
  1556. //|_______________________________________|_________|____|
  1557. const size_type n_after = size_type(n - raw_before);
  1558. const size_type elemsafter = size_type(old_size - elemsbefore);
  1559. //We can have two situations:
  1560. if (elemsafter >= n_after) {
  1561. //The raw_mem from end will divide displaced old_end
  1562. //
  1563. //Old situation:
  1564. // ______________________________________________________
  1565. //| raw_mem | old_begin | old_end | raw_mem |
  1566. //|______________|___________|____________|______________|
  1567. //
  1568. //New situation with do_after(1):
  1569. // _______________________________________________________
  1570. //| old_begin + new_beg | new_end |old_end | raw_mem |
  1571. //|__________________________|_________|________|_________|
  1572. //
  1573. //First copy the part of old_end raw_mem
  1574. B finish_n = make_iterator_advance(old_finish, -std::ptrdiff_t(n_after));
  1575. ::boost::container::uninitialized_move_alloc(a, finish_n, old_finish, old_finish);
  1576. old_values_destroyer.increment_size(n_after);
  1577. //Displace the rest of old_end to the new position
  1578. boost::container::move_backward_overlapping(pos, finish_n, old_finish);
  1579. //Now overwrite with new_end
  1580. //The new_end part is [first + (n - n_after), last)
  1581. insertion_proxy.copy_n_and_update(a, pos, n_after);
  1582. }
  1583. else {
  1584. //The raw_mem from end will divide new_end part
  1585. // _____________________________________________________________
  1586. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1587. //|______________|___________|____________|_____________________|
  1588. // _____________________________________________________________
  1589. //| old_begin + new_beg | new_end |old_end | raw_mem | //New situation with do_after(2)
  1590. //|__________________________|_______________|________|_________|
  1591. //First initialize data in raw memory
  1592. const size_type mid_last_dist = size_type(n_after - elemsafter);
  1593. //Copy to the old_end part to the uninitialized zone leaving a gap.
  1594. B const mid_last(make_iterator_uadvance(old_finish, mid_last_dist));
  1595. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, mid_last);
  1596. array_destructor_t old_end_destroyer(mid_last, a, iterator_udistance(pos, old_finish));
  1597. //Copy the first part to the already constructed old_end zone
  1598. insertion_proxy.copy_n_and_update(a, pos, elemsafter);
  1599. //Copy the rest to the uninitialized zone filling the gap
  1600. insertion_proxy.uninitialized_copy_n_and_update(a, old_finish, mid_last_dist);
  1601. old_end_destroyer.release();
  1602. }
  1603. }
  1604. old_values_destroyer.release();
  1605. }
  1606. }
  1607. template
  1608. <typename Allocator
  1609. , typename B // B models BidirIterator
  1610. , typename InsertionProxy
  1611. >
  1612. BOOST_CONTAINER_FORCEINLINE void expand_backward_forward_and_insert_alloc_move_forward
  1613. ( B const old_start
  1614. , std::size_t const old_size
  1615. , B const new_start
  1616. , B const pos
  1617. , std::size_t const n
  1618. , InsertionProxy insertion_proxy
  1619. , Allocator& a)
  1620. {
  1621. typedef std::size_t size_type;
  1622. typedef typename allocator_traits<Allocator>::value_type value_type;
  1623. static const bool trivial_dctr_after_move = has_trivial_destructor_after_move<value_type>::value;
  1624. static const bool trivial_dctr = dtl::is_trivially_destructible<value_type>::value;
  1625. typedef typename dtl::if_c
  1626. <trivial_dctr
  1627. , dtl::null_scoped_destructor_n<Allocator, B>
  1628. , dtl::scoped_destructor_n<Allocator, B>
  1629. >::type array_destructor_t;
  1630. //n can be zero to just expand capacity
  1631. B const old_finish = make_iterator_uadvance(old_start, old_size);
  1632. const size_type new_size = size_type(old_size + n);
  1633. B const new_finish = make_iterator_uadvance(new_start, new_size);
  1634. //We can have 8 possibilities:
  1635. const size_type elemsafter = static_cast<size_type>(iterator_udistance(pos, old_finish));
  1636. const size_type raw_after = static_cast<size_type>(iterator_udistance(old_finish, new_finish));
  1637. const size_type after_plus_new = size_type(elemsafter + n);
  1638. //Check if raw_before is big enough to hold the new data + the end of old data
  1639. if (raw_after >= after_plus_new) {
  1640. //If anything goes wrong, this object will destroy
  1641. //all the old objects to fulfill previous vector state
  1642. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1643. //______________________ __________________________________
  1644. //| old_begin | old_end | raw_mem //Old situation
  1645. //|___________|_________|__________________________________
  1646. // _____________________ _________________________________
  1647. //| old_begin | old_end | raw_mem | new | old_end | //First step
  1648. //|___________|_________|__________|__________|___________|
  1649. //Copy first new objects, after that old values after pos
  1650. B new_elem_pos = new_finish - after_plus_new;
  1651. insertion_proxy.uninitialized_copy_n_and_update(a, new_elem_pos, n);
  1652. array_destructor_t new_values_destroyer(new_elem_pos, a, n);
  1653. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, new_elem_pos+n);
  1654. new_values_destroyer.set_size(after_plus_new);
  1655. //Check if raw_before is so big that even copying the old data + new data
  1656. //there is a gap between the new data and the old data
  1657. if (raw_after >= new_size) {
  1658. //______________________ __________________________________
  1659. //| old_begin | old_end | raw_mem //Old situation
  1660. //|___________|_________|__________________________________
  1661. // _____________________ _________________________________
  1662. //| old_begin | old_end | raw_mem | new | old_end | //First step
  1663. //|___________|_________|______________|________|_________|
  1664. // _____________________V_________________________________
  1665. //| raw_mem | old_begin | new | old_end | //New situation
  1666. //|________________________|___________|________|_________|
  1667. //
  1668. //Now initialize the rest of memory with the last old values
  1669. ::boost::container::uninitialized_move_alloc(a, old_start, pos, new_start);
  1670. //All new elements correctly constructed, avoid new element destruction
  1671. new_values_destroyer.release();
  1672. //Old values destroyed automatically with "old_values_destroyer"
  1673. //when "old_values_destroyer" goes out of scope unless the have trivial
  1674. //destructor after move.
  1675. if(trivial_dctr_after_move)
  1676. old_values_destroyer.release();
  1677. }
  1678. //raw_before is so big that divides old_end
  1679. else {
  1680. //______________________ ____________________________
  1681. //| old_begin | old_end | raw_mem //Old situation
  1682. //|___________|_________|____________________________
  1683. // _____________________ ____________________________
  1684. //| old_begin | old_end | raw_mem | new | old_end | //First step
  1685. //|___________|_________|_________|________|_________|
  1686. // _________________________________________________
  1687. //| raw_mem | old_begin | new | old_end | //New situation
  1688. //|___________________|___________|________|_________|
  1689. //Now initialize the rest of raw_before memory with the
  1690. //last elements before new values
  1691. const size_type raw_gap = raw_after - after_plus_new;
  1692. B const pre_pos_raw = pos - raw_gap;
  1693. ::boost::container::uninitialized_move_alloc_n(a, pre_pos_raw, raw_gap, old_finish);
  1694. new_values_destroyer.release();
  1695. old_values_destroyer.increment_size(raw_after);
  1696. //Now move remaining last objects in the old buffer begin
  1697. BOOST_ASSERT(old_start != old_finish);
  1698. boost::container::move_backward_overlapping(old_start, pre_pos_raw, old_finish);
  1699. old_values_destroyer.release();
  1700. if (!trivial_dctr_after_move) {
  1701. boost::container::destroy_alloc(a, old_start, new_start);
  1702. }
  1703. }
  1704. }
  1705. else{
  1706. //If anything goes wrong, this object will destroy
  1707. //all the old objects to fulfill previous vector state
  1708. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1709. //Now we can have two situations: the raw_mem of the
  1710. //end divides the new elements or the old_end
  1711. if (raw_after > elemsafter) {
  1712. //The raw memory divides the new elements
  1713. //__________________________________
  1714. //| old_begin | old_end | raw | //Old situation
  1715. //|___________|_________|___________|
  1716. // _____ ___________________________
  1717. //| raw | old_begin | new | old_end | //New situation
  1718. //|_____|___________|_____|_________|
  1719. //First copy whole old_end and part of new to raw_mem
  1720. B p = new_finish - elemsafter;
  1721. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, p);
  1722. array_destructor_t new_values_destroyer(p, a, elemsafter);
  1723. //Copy all new elements
  1724. const size_type mid_n = size_type(raw_after - elemsafter);
  1725. const size_type rest_new = size_type(n - mid_n);
  1726. B new_rng_start = old_finish - rest_new;
  1727. insertion_proxy.copy_n_and_update(a, new_rng_start, rest_new);
  1728. insertion_proxy.uninitialized_copy_n_and_update(a, old_finish, mid_n);
  1729. new_values_destroyer.release();
  1730. old_values_destroyer.increment_size_backwards(raw_after);
  1731. //Displace old_end, but make sure data has to be moved
  1732. p = ::boost::container::move_backward_overlapping(old_start, pos, new_rng_start);
  1733. //Destroy remaining moved elements from old_begin except if they
  1734. //have trivial destructor after being moved
  1735. old_values_destroyer.release();
  1736. if (!trivial_dctr_after_move) {
  1737. boost::container::destroy_alloc(a, old_start, p);
  1738. }
  1739. }
  1740. else {
  1741. //The raw memory divides the old_end group:
  1742. //________________________________________
  1743. //| old_begin | old_end | raw | //Old situation
  1744. //|___________|_______________|___________|
  1745. // _____ __________________________________
  1746. //| raw | old_begin | new | old_end | //New situation
  1747. //|_____|___________|_____|_______________|
  1748. //
  1749. //Copy the last part of old_end to raw_mem
  1750. const B old_end_pivot = old_finish - raw_after;
  1751. ::boost::container::uninitialized_move_alloc_n(a, old_end_pivot, raw_after, old_finish);
  1752. //The buffer is all constructed
  1753. old_values_destroyer.increment_size_backwards(raw_after);
  1754. //Now copy the first part of old_end overwriting itself
  1755. B const new_end_pos = ::boost::container::move_backward_overlapping(pos, old_end_pivot, old_finish);
  1756. B const new_beg_pos = new_end_pos - n;
  1757. //Now copy the new_beg elements
  1758. insertion_proxy.copy_n_and_update(a, new_beg_pos, n);
  1759. B const p = ::boost::container::move_backward_overlapping(old_start, pos, new_beg_pos);
  1760. old_values_destroyer.release();
  1761. if (!trivial_dctr_after_move) {
  1762. (void)p;
  1763. boost::container::destroy_alloc(a, old_start, p);
  1764. }
  1765. }
  1766. }
  1767. }
  1768. template <class R, class InsertionProxy, class Allocator>
  1769. void expand_backward_forward_and_insert_alloc
  1770. ( R const old_start
  1771. , std::size_t const old_size
  1772. , R const new_start
  1773. , R const pos
  1774. , std::size_t const n
  1775. , InsertionProxy insertion_proxy
  1776. , Allocator& a)
  1777. {
  1778. if(new_start < old_start){
  1779. expand_backward_forward_and_insert_alloc_move_backward(old_start, old_size, new_start, pos, n, insertion_proxy, a);
  1780. }
  1781. else{
  1782. expand_backward_forward_and_insert_alloc_move_forward(old_start, old_size, new_start, pos, n, insertion_proxy, a);
  1783. }
  1784. }
  1785. } //namespace container {
  1786. } //namespace boost {
  1787. //#pragma GCC diagnostic ignored "-Wclass-memaccess"
  1788. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  1789. #pragma GCC diagnostic pop
  1790. #endif
  1791. #endif //#ifndef BOOST_CONTAINER_DETAIL_COPY_MOVE_ALGO_HPP