copy_move_algo.hpp 72 KB

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