copy_move_algo.hpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  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 >= 110000)
  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. BOOST_STATIC_CONSTEXPR bool value = false;
  61. };
  62. /////////////////////////
  63. // raw pointers
  64. /////////////////////////
  65. template<class T>
  66. struct are_elements_contiguous<T*>
  67. {
  68. BOOST_STATIC_CONSTEXPR 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. BOOST_STATIC_CONSTEXPR 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. BOOST_STATIC_CONSTEXPR 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. BOOST_STATIC_CONSTEXPR 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. BOOST_STATIC_CONSTEXPR bool value = InsertionProxy::single_value;
  162. };
  163. template<class InsertionProxy>
  164. struct is_single_value_proxy_impl<InsertionProxy, false>
  165. {
  166. BOOST_STATIC_CONSTEXPR 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(reinterpret_cast<void *>(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. BOOST_STATIC_CONSTEXPR 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 F> // F models ForwardIterator
  719. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  720. copy_n(I f, std::size_t n, F r)
  721. {
  722. while (n) {
  723. --n;
  724. *r = *f;
  725. ++f; ++r;
  726. }
  727. return r;
  728. }
  729. template
  730. <typename I, // I models InputIterator
  731. typename F> // F models ForwardIterator
  732. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  733. copy_n(I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  734. { return dtl::memmove_n(f, n, r); }
  735. //////////////////////////////////////////////////////////////////////////////
  736. //
  737. // copy_n_source
  738. //
  739. //////////////////////////////////////////////////////////////////////////////
  740. template
  741. <typename I, // I models InputIterator
  742. typename F> // F models ForwardIterator
  743. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  744. copy_n_source(I f, std::size_t n, F r)
  745. {
  746. while (n) {
  747. --n;
  748. boost::container::assign_in_place(r, f);
  749. ++f; ++r;
  750. }
  751. return f;
  752. }
  753. template
  754. <typename I, // I models InputIterator
  755. typename F> // F models ForwardIterator
  756. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  757. copy_n_source(I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  758. { return dtl::memmove_n_source(f, n, r); }
  759. //////////////////////////////////////////////////////////////////////////////
  760. //
  761. // copy_n_source_dest
  762. //
  763. //////////////////////////////////////////////////////////////////////////////
  764. template
  765. <typename I, // I models InputIterator
  766. typename F> // F models ForwardIterator
  767. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  768. copy_n_source_dest(I f, std::size_t n, F &r)
  769. {
  770. while (n) {
  771. --n;
  772. *r = *f;
  773. ++f; ++r;
  774. }
  775. return f;
  776. }
  777. template
  778. <typename I, // I models InputIterator
  779. typename F> // F models ForwardIterator
  780. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  781. copy_n_source_dest(I f, std::size_t n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  782. { return dtl::memmove_n_source_dest(f, n, r); }
  783. //////////////////////////////////////////////////////////////////////////////
  784. //
  785. // move
  786. //
  787. //////////////////////////////////////////////////////////////////////////////
  788. template
  789. <typename I, // I models InputIterator
  790. typename F> // F models ForwardIterator
  791. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  792. move(I f, I l, F r)
  793. {
  794. while (f != l) {
  795. *r = ::boost::move(*f);
  796. ++f; ++r;
  797. }
  798. return r;
  799. }
  800. template
  801. <typename I, // I models InputIterator
  802. typename F> // F models ForwardIterator
  803. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  804. move(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  805. { return dtl::memmove(f, l, r); }
  806. //////////////////////////////////////////////////////////////////////////////
  807. //
  808. // move_n
  809. //
  810. //////////////////////////////////////////////////////////////////////////////
  811. template
  812. <typename I, // I models InputIterator
  813. typename F> // F models ForwardIterator
  814. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  815. move_n(I f, std::size_t n, F r)
  816. {
  817. while (n) {
  818. --n;
  819. *r = ::boost::move(*f);
  820. ++f; ++r;
  821. }
  822. return r;
  823. }
  824. template
  825. <typename I, // I models InputIterator
  826. typename F> // F models ForwardIterator
  827. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  828. move_n(I f, std::size_t 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. inline 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(reinterpret_cast<void *>((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_backward_n
  864. //
  865. //////////////////////////////////////////////////////////////////////////////
  866. template
  867. <typename I, // I models BidirectionalIterator
  868. typename F> // F models ForwardIterator
  869. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  870. move_backward_n(I l, std::size_t n, F r)
  871. {
  872. while (n) {
  873. --n;
  874. --l; --r;
  875. *r = ::boost::move(*l);
  876. }
  877. return r;
  878. }
  879. template
  880. <typename I, // I models InputIterator
  881. typename F> // F models ForwardIterator
  882. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  883. move_backward_n(I l, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  884. {
  885. typedef typename boost::container::iter_value<I>::type value_type;
  886. if (BOOST_LIKELY(n != 0)){
  887. r -= n;
  888. std::memmove (reinterpret_cast<void *>((boost::movelib::iterator_to_raw_pointer)(r))
  889. , reinterpret_cast<const void *>((boost::movelib::iterator_to_raw_pointer)(l) - n)
  890. , sizeof(value_type)*n);
  891. }
  892. return r;
  893. }
  894. //////////////////////////////////////////////////////////////////////////////
  895. //
  896. // move_backward_n_source
  897. //
  898. //////////////////////////////////////////////////////////////////////////////
  899. template
  900. <typename I, // I models BidirectionalIterator
  901. typename F> // F models ForwardIterator
  902. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  903. move_backward_n_source(I l, std::size_t n, F r)
  904. {
  905. while (n) {
  906. --n;
  907. --l; --r;
  908. *r = ::boost::move(*l);
  909. }
  910. return l;
  911. }
  912. template
  913. <typename I, // I models InputIterator
  914. typename F> // F models ForwardIterator
  915. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  916. move_backward_n_source(I l, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  917. {
  918. typedef typename boost::container::iter_value<I>::type value_type;
  919. if (BOOST_LIKELY(n != 0)){
  920. r -= n;
  921. l -= n;
  922. std::memmove( reinterpret_cast<void *>((boost::movelib::iterator_to_raw_pointer)(r))
  923. , l
  924. , sizeof(value_type)*n);
  925. }
  926. return l;
  927. }
  928. //////////////////////////////////////////////////////////////////////////////
  929. //
  930. // move_n_source_dest
  931. //
  932. //////////////////////////////////////////////////////////////////////////////
  933. template
  934. <typename I // I models InputIterator
  935. ,typename F> // F models ForwardIterator
  936. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  937. move_n_source_dest(I f, std::size_t n, F &r)
  938. {
  939. while (n) {
  940. --n;
  941. *r = ::boost::move(*f);
  942. ++f; ++r;
  943. }
  944. return f;
  945. }
  946. template
  947. <typename I // I models InputIterator
  948. ,typename F> // F models ForwardIterator
  949. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  950. move_n_source_dest(I f, std::size_t n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  951. { return dtl::memmove_n_source_dest(f, n, r); }
  952. //////////////////////////////////////////////////////////////////////////////
  953. //
  954. // move_n_source
  955. //
  956. //////////////////////////////////////////////////////////////////////////////
  957. template
  958. <typename I // I models InputIterator
  959. ,typename F> // F models ForwardIterator
  960. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  961. move_n_source(I f, std::size_t n, F r)
  962. {
  963. while (n) {
  964. --n;
  965. *r = ::boost::move(*f);
  966. ++f; ++r;
  967. }
  968. return f;
  969. }
  970. template
  971. <typename I // I models InputIterator
  972. ,typename F> // F models ForwardIterator
  973. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  974. move_n_source(I f, std::size_t n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  975. { return dtl::memmove_n_source(f, n, r); }
  976. template<typename F> // F models ForwardIterator
  977. inline F move_forward_overlapping(F f, F l, F r)
  978. {
  979. return (f != r) ? (move)(f, l, r) : l;
  980. }
  981. template<typename B> // B models BidirIterator
  982. inline B move_backward_overlapping(B f, B l, B rl)
  983. {
  984. return (l != rl) ? (move_backward)(f, l, rl) : f;
  985. }
  986. //////////////////////////////////////////////////////////////////////////////
  987. //
  988. // destroy_alloc_n
  989. //
  990. //////////////////////////////////////////////////////////////////////////////
  991. template
  992. <typename Allocator
  993. ,typename I> // I models InputIterator
  994. inline typename dtl::disable_if_trivially_destructible<I, void>::type
  995. destroy_alloc_n(Allocator &a, I f, std::size_t n)
  996. {
  997. while(n){
  998. --n;
  999. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(f));
  1000. ++f;
  1001. }
  1002. }
  1003. template
  1004. <typename Allocator
  1005. ,typename I> // I models InputIterator
  1006. inline typename dtl::enable_if_trivially_destructible<I, void>::type
  1007. destroy_alloc_n(Allocator &, I, std::size_t)
  1008. {}
  1009. //////////////////////////////////////////////////////////////////////////////
  1010. //
  1011. // destroy_alloc
  1012. //
  1013. //////////////////////////////////////////////////////////////////////////////
  1014. template
  1015. <typename Allocator
  1016. ,typename I> // I models InputIterator
  1017. inline typename dtl::disable_if_trivially_destructible<I, void>::type
  1018. destroy_alloc(Allocator &a, I f, I l)
  1019. {
  1020. while(f != l){
  1021. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(f));
  1022. ++f;
  1023. }
  1024. }
  1025. template
  1026. <typename Allocator
  1027. ,typename I > // I models InputIterator
  1028. inline typename dtl::enable_if_trivially_destructible<I, void>::type
  1029. destroy_alloc(Allocator &, I, I)
  1030. {}
  1031. //////////////////////////////////////////////////////////////////////////////
  1032. //
  1033. // deep_swap_alloc_n
  1034. //
  1035. //////////////////////////////////////////////////////////////////////////////
  1036. template
  1037. <std::size_t MaxTmpBytes
  1038. ,typename Allocator
  1039. ,typename F // F models ForwardIterator
  1040. ,typename G // G models ForwardIterator
  1041. >
  1042. inline typename dtl::disable_if_memtransfer_copy_assignable<F, G, void>::type
  1043. deep_swap_alloc_n( Allocator &a, F short_range_f, std::size_t n_i, G large_range_f, std::size_t n_j)
  1044. {
  1045. std::size_t n = 0;
  1046. for (; n != n_i ; ++short_range_f, ++large_range_f, ++n){
  1047. boost::adl_move_swap(*short_range_f, *large_range_f);
  1048. }
  1049. boost::container::uninitialized_move_alloc_n(a, large_range_f, std::size_t(n_j - n_i), short_range_f); // may throw
  1050. boost::container::destroy_alloc_n(a, large_range_f, std::size_t(n_j - n_i));
  1051. }
  1052. BOOST_CONTAINER_CONSTANT_VAR std::size_t DeepSwapAllocNMaxStorage = std::size_t(1) << std::size_t(11); //2K bytes
  1053. template
  1054. <std::size_t MaxTmpBytes
  1055. ,typename Allocator
  1056. ,typename F // F models ForwardIterator
  1057. ,typename G // G models ForwardIterator
  1058. >
  1059. inline typename dtl::enable_if_c
  1060. < dtl::is_memtransfer_copy_assignable<F, G>::value && (MaxTmpBytes <= DeepSwapAllocNMaxStorage) && false
  1061. , void>::type
  1062. deep_swap_alloc_n( Allocator &a, F short_range_f, std::size_t n_i, G large_range_f, std::size_t n_j)
  1063. {
  1064. typedef typename allocator_traits<Allocator>::value_type value_type;
  1065. typedef typename dtl::aligned_storage
  1066. <MaxTmpBytes, dtl::alignment_of<value_type>::value>::type storage_type;
  1067. storage_type storage;
  1068. const std::size_t n_i_bytes = sizeof(value_type)*n_i;
  1069. void *const large_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f));
  1070. void *const short_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f));
  1071. void *const stora_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(storage.data));
  1072. std::memcpy(stora_ptr, large_ptr, n_i_bytes);
  1073. std::memcpy(large_ptr, short_ptr, n_i_bytes);
  1074. std::memcpy(short_ptr, stora_ptr, n_i_bytes);
  1075. boost::container::iterator_uadvance(large_range_f, n_i);
  1076. boost::container::iterator_uadvance(short_range_f, n_i);
  1077. boost::container::uninitialized_move_alloc_n(a, large_range_f, std::size_t(n_j - n_i), short_range_f); // may throw
  1078. boost::container::destroy_alloc_n(a, large_range_f, std::size_t(n_j - n_i));
  1079. }
  1080. template
  1081. <std::size_t MaxTmpBytes
  1082. ,typename Allocator
  1083. ,typename F // F models ForwardIterator
  1084. ,typename G // G models ForwardIterator
  1085. >
  1086. inline typename dtl::enable_if_c
  1087. < dtl::is_memtransfer_copy_assignable<F, G>::value && true//(MaxTmpBytes > DeepSwapAllocNMaxStorage)
  1088. , void>::type
  1089. deep_swap_alloc_n( Allocator &a, F short_range_f, std::size_t n_i, G large_range_f, std::size_t n_j)
  1090. {
  1091. typedef typename allocator_traits<Allocator>::value_type value_type;
  1092. typedef typename dtl::aligned_storage
  1093. <DeepSwapAllocNMaxStorage, dtl::alignment_of<value_type>::value>::type storage_type;
  1094. storage_type storage;
  1095. const std::size_t sizeof_storage = sizeof(storage);
  1096. std::size_t n_i_bytes = sizeof(value_type)*n_i;
  1097. char *large_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f)));
  1098. char *short_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f)));
  1099. char *stora_ptr = static_cast<char*>(static_cast<void*>(storage.data));
  1100. std::size_t szt_times = n_i_bytes/sizeof_storage;
  1101. const std::size_t szt_rem = n_i_bytes%sizeof_storage;
  1102. //Loop unrolling using Duff's device, as it seems it helps on some architectures
  1103. const std::size_t Unroll = 4;
  1104. std::size_t n = (szt_times + (Unroll-1))/Unroll;
  1105. const std::size_t branch_number = (szt_times == 0)*Unroll + (szt_times % Unroll);
  1106. switch(branch_number){
  1107. case 4:
  1108. break;
  1109. case 0: do{
  1110. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1111. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1112. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1113. large_ptr += sizeof_storage;
  1114. short_ptr += sizeof_storage;
  1115. BOOST_FALLTHROUGH;
  1116. case 3:
  1117. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1118. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1119. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1120. large_ptr += sizeof_storage;
  1121. short_ptr += sizeof_storage;
  1122. BOOST_FALLTHROUGH;
  1123. case 2:
  1124. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1125. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1126. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1127. large_ptr += sizeof_storage;
  1128. short_ptr += sizeof_storage;
  1129. BOOST_FALLTHROUGH;
  1130. case 1:
  1131. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  1132. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  1133. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  1134. large_ptr += sizeof_storage;
  1135. short_ptr += sizeof_storage;
  1136. } while(--n);
  1137. }
  1138. std::memcpy(stora_ptr, large_ptr, szt_rem);
  1139. std::memcpy(large_ptr, short_ptr, szt_rem);
  1140. std::memcpy(short_ptr, stora_ptr, szt_rem);
  1141. boost::container::iterator_uadvance(large_range_f, n_i);
  1142. boost::container::iterator_uadvance(short_range_f, n_i);
  1143. boost::container::uninitialized_move_alloc_n(a, large_range_f, std::size_t(n_j - n_i), short_range_f); // may throw
  1144. boost::container::destroy_alloc_n(a, large_range_f, std::size_t(n_j - n_i));
  1145. }
  1146. //////////////////////////////////////////////////////////////////////////////
  1147. //
  1148. // copy_assign_range_alloc_n
  1149. //
  1150. //////////////////////////////////////////////////////////////////////////////
  1151. template
  1152. <typename Allocator
  1153. ,typename I // F models InputIterator
  1154. ,typename O // G models OutputIterator
  1155. >
  1156. void copy_assign_range_alloc_n( Allocator &a, I inp_start, std::size_t n_i, O out_start, std::size_t n_o )
  1157. {
  1158. if (n_o < n_i){
  1159. inp_start = boost::container::copy_n_source_dest(inp_start, n_o, out_start); // may throw
  1160. boost::container::uninitialized_copy_alloc_n(a, inp_start, std::size_t(n_i - n_o), out_start);// may throw
  1161. }
  1162. else{
  1163. out_start = boost::container::copy_n(inp_start, n_i, out_start); // may throw
  1164. boost::container::destroy_alloc_n(a, out_start, std::size_t(n_o - n_i));
  1165. }
  1166. }
  1167. //////////////////////////////////////////////////////////////////////////////
  1168. //
  1169. // move_assign_range_alloc_n
  1170. //
  1171. //////////////////////////////////////////////////////////////////////////////
  1172. template
  1173. <typename Allocator
  1174. ,typename I // F models InputIterator
  1175. ,typename O // G models OutputIterator
  1176. >
  1177. void move_assign_range_alloc_n( Allocator &a, I inp_start, std::size_t n_i, O out_start, std::size_t n_o )
  1178. {
  1179. if (n_o < n_i){
  1180. inp_start = boost::container::move_n_source_dest(inp_start, n_o, out_start); // may throw
  1181. boost::container::uninitialized_move_alloc_n(a, inp_start, std::size_t(n_i - n_o), out_start); // may throw
  1182. }
  1183. else{
  1184. out_start = boost::container::move_n(inp_start, n_i, out_start); // may throw
  1185. boost::container::destroy_alloc_n(a, out_start, std::size_t(n_o - n_i));
  1186. }
  1187. }
  1188. template<class Allocator>
  1189. struct array_destructor
  1190. {
  1191. typedef typename ::boost::container::allocator_traits<Allocator>::value_type value_type;
  1192. typedef typename dtl::if_c
  1193. <dtl::is_trivially_destructible<value_type>::value
  1194. ,dtl::null_scoped_destructor_range<Allocator>
  1195. ,dtl::scoped_destructor_range<Allocator>
  1196. >::type type;
  1197. };
  1198. template<class Allocator>
  1199. struct value_destructor
  1200. {
  1201. typedef typename ::boost::container::allocator_traits<Allocator>::value_type value_type;
  1202. typedef typename dtl::if_c
  1203. <dtl::is_trivially_destructible<value_type>::value
  1204. , dtl::null_scoped_destructor<Allocator>
  1205. , dtl::scoped_destructor<Allocator>
  1206. >::type type;
  1207. };
  1208. template
  1209. <typename Allocator
  1210. ,typename F // F models ForwardIterator
  1211. ,typename O // G models OutputIterator
  1212. ,typename InsertionProxy
  1213. >
  1214. void uninitialized_move_and_insert_alloc
  1215. ( Allocator &a
  1216. , F first
  1217. , F pos
  1218. , F last
  1219. , O d_first
  1220. , std::size_t n
  1221. , InsertionProxy insertion_proxy)
  1222. {
  1223. typedef typename array_destructor<Allocator>::type array_destructor_t;
  1224. //Anti-exception rollbacks
  1225. array_destructor_t new_values_destroyer(d_first, d_first, a);
  1226. //Initialize with [begin(), pos) old buffer
  1227. //the start of the new buffer
  1228. O d_last = ::boost::container::uninitialized_move_alloc(a, first, pos, d_first);
  1229. new_values_destroyer.set_end(d_last);
  1230. //Initialize new objects, starting from previous point
  1231. insertion_proxy.uninitialized_copy_n_and_update(a, d_last, n);
  1232. d_last += n;
  1233. new_values_destroyer.set_end(d_last);
  1234. //Initialize from the rest of the old buffer,
  1235. //starting from previous point
  1236. (void) ::boost::container::uninitialized_move_alloc(a, pos, last, d_last);
  1237. //All construction successful, disable rollbacks
  1238. new_values_destroyer.release();
  1239. }
  1240. template
  1241. <typename Allocator
  1242. ,typename F // F models ForwardIterator
  1243. ,typename InsertionProxy
  1244. >
  1245. typename dtl::enable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1246. expand_backward_and_insert_nonempty_middle_alloc
  1247. ( Allocator &a
  1248. , F const first
  1249. , F const pos
  1250. , std::size_t const
  1251. , InsertionProxy insertion_proxy)
  1252. {
  1253. BOOST_ASSERT(first != pos);
  1254. typedef typename value_destructor<Allocator>::type value_destructor_t;
  1255. F aux = first; --aux;
  1256. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(aux), boost::move(*first));
  1257. value_destructor_t on_exception(a, boost::movelib::iterator_to_raw_pointer(aux));
  1258. //Copy previous to last objects to the initialized end
  1259. aux = first; ++aux;
  1260. aux = boost::container::move(aux, pos, first);
  1261. //Insert new objects in the pos
  1262. insertion_proxy.copy_n_and_update(a, aux, 1u);
  1263. on_exception.release();
  1264. }
  1265. template
  1266. <typename Allocator
  1267. ,typename F // F models ForwardIterator
  1268. ,typename InsertionProxy
  1269. >
  1270. typename dtl::disable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1271. expand_backward_and_insert_nonempty_middle_alloc
  1272. ( Allocator &a
  1273. , F first
  1274. , F pos
  1275. , std::size_t const n
  1276. , InsertionProxy insertion_proxy)
  1277. {
  1278. BOOST_ASSERT(first != pos);
  1279. BOOST_ASSERT(n != 0);
  1280. typedef typename array_destructor<Allocator>::type array_destructor_t;
  1281. const std::size_t elems_before = iterator_udistance(first, pos);
  1282. if(elems_before >= n){
  1283. //New elements can be just copied.
  1284. //Move to uninitialized memory last objects
  1285. F const first_less_n = first - n;
  1286. F nxt = ::boost::container::uninitialized_move_alloc_n_source(a, first, n, first_less_n);
  1287. array_destructor_t on_exception(first_less_n, first, a);
  1288. //Copy previous to last objects to the initialized end
  1289. nxt = boost::container::move(nxt, pos, first);
  1290. //Insert new objects in the pos
  1291. insertion_proxy.copy_n_and_update(a, nxt, n);
  1292. on_exception.release();
  1293. }
  1294. else {
  1295. //The new elements don't fit in the [pos, end()) range.
  1296. //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
  1297. F aux = ::boost::container::uninitialized_move_alloc(a, first, pos, first - n);
  1298. array_destructor_t on_exception(first -n, aux, a);
  1299. //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
  1300. insertion_proxy.uninitialized_copy_n_and_update(a, aux, std::size_t(n - elems_before));
  1301. insertion_proxy.copy_n_and_update(a, first, elems_before);
  1302. on_exception.release();
  1303. }
  1304. }
  1305. template
  1306. <typename Allocator
  1307. ,typename F // F models ForwardIterator
  1308. ,typename InsertionProxy
  1309. >
  1310. typename dtl::enable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1311. expand_forward_and_insert_nonempty_middle_alloc
  1312. ( Allocator &a
  1313. , F pos
  1314. , F last
  1315. , std::size_t const
  1316. , InsertionProxy insertion_proxy)
  1317. {
  1318. BOOST_ASSERT(last != pos);
  1319. typedef typename value_destructor<Allocator>::type value_destructor_t;
  1320. F last_m_n = last; --last_m_n;
  1321. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(last), boost::move(*last_m_n));
  1322. value_destructor_t on_exception(a, boost::movelib::iterator_to_raw_pointer(last));
  1323. //Copy previous to last objects to the initialized end
  1324. boost::container::move_backward(pos, last_m_n, last);
  1325. //Insert new objects in the pos
  1326. insertion_proxy.copy_n_and_update(a, pos, 1);
  1327. on_exception.release();
  1328. }
  1329. template
  1330. <typename Allocator
  1331. ,typename F // F models ForwardIterator
  1332. ,typename InsertionProxy
  1333. >
  1334. typename dtl::disable_if_c<dtl::is_single_value_proxy<InsertionProxy>::value, void>::type
  1335. expand_forward_and_insert_nonempty_middle_alloc
  1336. ( Allocator &a
  1337. , F pos
  1338. , F last
  1339. , std::size_t const n
  1340. , InsertionProxy insertion_proxy)
  1341. {
  1342. BOOST_ASSERT(last != pos);
  1343. BOOST_ASSERT(n != 0);
  1344. typedef typename array_destructor<Allocator>::type array_destructor_t;
  1345. const std::size_t elems_after = iterator_udistance(pos, last);
  1346. if(elems_after >= n){
  1347. //New elements can be just copied.
  1348. //Move to uninitialized memory last objects
  1349. F const last_m_n = last - n;
  1350. F const nxt = ::boost::container::uninitialized_move_alloc_n(a, last_m_n, n, last);
  1351. array_destructor_t on_exception(last, nxt, a);
  1352. //Copy previous to last objects to the initialized end
  1353. boost::container::move_backward(pos, last_m_n, last);
  1354. //Insert new objects in the pos
  1355. insertion_proxy.copy_n_and_update(a, pos, n);
  1356. on_exception.release();
  1357. }
  1358. else {
  1359. //The new elements don't fit in the [pos, end()) range.
  1360. //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
  1361. F new_last = ::boost::container::uninitialized_move_alloc(a, pos, last, pos + n);
  1362. array_destructor_t on_exception(pos + n, new_last, a);
  1363. //Copy first new elements in pos (gap is still there)
  1364. insertion_proxy.copy_n_and_update(a, pos, elems_after);
  1365. //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
  1366. insertion_proxy.uninitialized_copy_n_and_update(a, last, std::size_t(n - elems_after));
  1367. on_exception.release();
  1368. }
  1369. }
  1370. template
  1371. <typename Allocator
  1372. , typename F // F models ForwardIterator
  1373. , typename InsertionProxy
  1374. >
  1375. inline void expand_forward_and_insert_alloc
  1376. ( Allocator& a
  1377. , F pos
  1378. , F last
  1379. , std::size_t const n
  1380. , InsertionProxy insertion_proxy)
  1381. {
  1382. if (last == pos) {
  1383. insertion_proxy.uninitialized_copy_n_and_update(a, last, n);
  1384. }
  1385. else{
  1386. const bool single_value = dtl::is_single_value_proxy<InsertionProxy>::value;
  1387. BOOST_IF_CONSTEXPR(!single_value){
  1388. if (BOOST_UNLIKELY(!n)) {
  1389. return;
  1390. }
  1391. }
  1392. expand_forward_and_insert_nonempty_middle_alloc(a, pos, last, n, insertion_proxy);
  1393. }
  1394. }
  1395. template <class B, class InsertionProxy, class Allocator>
  1396. void expand_backward_forward_and_insert_alloc_move_backward
  1397. ( B const old_start
  1398. , std::size_t const old_size
  1399. , B const new_start
  1400. , B const pos
  1401. , std::size_t const n
  1402. , InsertionProxy insertion_proxy
  1403. , Allocator& a)
  1404. {
  1405. typedef std::size_t size_type;
  1406. typedef typename allocator_traits<Allocator>::value_type value_type;
  1407. BOOST_STATIC_CONSTEXPR bool trivial_dctr_after_move = has_trivial_destructor_after_move<value_type>::value;
  1408. BOOST_STATIC_CONSTEXPR bool trivial_dctr = dtl::is_trivially_destructible<value_type>::value;
  1409. typedef typename dtl::if_c
  1410. <trivial_dctr
  1411. , dtl::null_scoped_destructor_n<Allocator, B>
  1412. , dtl::scoped_destructor_n<Allocator, B>
  1413. >::type array_destructor_t;
  1414. //n can be zero to just expand capacity
  1415. B old_finish = make_iterator_uadvance(old_start, old_size);
  1416. //We can have 8 possibilities:
  1417. const size_type elemsbefore = static_cast<size_type>(iterator_udistance(old_start, pos));
  1418. const size_type raw_before = static_cast<size_type>(iterator_udistance(new_start, old_start));
  1419. const size_type before_plus_new = size_type(elemsbefore + n);
  1420. //Check if raw_before is big enough to hold the beginning of old data + new data
  1421. if (raw_before >= before_plus_new) {
  1422. //If anything goes wrong, this object will destroy
  1423. //all the old objects to fulfill previous vector state
  1424. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1425. // _________________________________________________________
  1426. //| raw_mem | old_begin | old_end | //Old situation
  1427. //| __________________________________|___________|_________|
  1428. // _________________________________________________________
  1429. //| old_begin | new | raw_mem | old_begin | old_end | //First step
  1430. //|___________|__________|____________|___________|_________|
  1431. //Copy first old values before pos, after that the new objects
  1432. B const new_elem_pos = ::boost::container::uninitialized_move_alloc(a, old_start, pos, new_start);
  1433. array_destructor_t new_values_destroyer(new_start, a, elemsbefore);
  1434. insertion_proxy.uninitialized_copy_n_and_update(a, new_elem_pos, n);
  1435. new_values_destroyer.set_size(before_plus_new);
  1436. const size_type new_size = size_type(old_size + n);
  1437. //Check if raw_before is so big that even copying the old data + new data
  1438. //there is a gap between the new data and the old data
  1439. if (raw_before >= new_size) {
  1440. // _______________________________________________________
  1441. //| raw_mem | old_begin | old_end | //Old situation
  1442. //|_________________________________|___________|_________|
  1443. // _______________________________________________________
  1444. //| old_begin | new | raw_mem | old_begin | old_end | //First step
  1445. //|___________|________|____________|___________|_________|
  1446. // _______________________________________________________
  1447. //| old_begin | new | old_end | raw_mem | //New situation
  1448. //|___________|________|_________|________________________|
  1449. //
  1450. //Now initialize the rest of memory with the last old values
  1451. if (before_plus_new != new_size) { //Special case to avoid operations in back insertion
  1452. B new_start_end(make_iterator_uadvance(new_start, before_plus_new));
  1453. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, new_start_end);
  1454. }
  1455. //All new elements correctly constructed, avoid new element destruction
  1456. new_values_destroyer.release();
  1457. //Old values destroyed automatically with "old_values_destroyer"
  1458. //when "old_values_destroyer" goes out of scope unless the have trivial
  1459. //destructor after move.
  1460. if(trivial_dctr_after_move)
  1461. old_values_destroyer.release();
  1462. }
  1463. //raw_before is so big that divides old_end
  1464. else {
  1465. // _________________________________________________
  1466. //| raw | old_beg | old_end | //Old situation
  1467. //|_____________________________|_________|_________|
  1468. // _________________________________________________
  1469. //| old_begin | new | raw | old_beg | old_end | //First step
  1470. //|___________|__________|______|_________|_________|
  1471. // _________________________________________________
  1472. //| old_begin | new | old_end | raw_mem | //New situation
  1473. //|___________|__________|_________|________________|
  1474. //Now initialize the rest of memory with the last old values
  1475. //All new elements correctly constructed, avoid new element destruction
  1476. BOOST_IF_CONSTEXPR(!trivial_dctr) {
  1477. //Now initialize the rest of raw_before memory with the
  1478. //first of elements after new values
  1479. const size_type raw_gap = raw_before - before_plus_new;
  1480. B new_start_plus(make_iterator_uadvance(new_start, before_plus_new));
  1481. ::boost::container::uninitialized_move_alloc_n(a, pos, raw_gap, new_start_plus);
  1482. new_values_destroyer.release();
  1483. old_values_destroyer.increment_size_backwards(raw_before);
  1484. //Now move remaining last objects in the old buffer begin
  1485. B remaining_pos(make_iterator_uadvance(pos, raw_gap));
  1486. remaining_pos = ::boost::container::move_forward_overlapping(remaining_pos, old_finish, old_start);
  1487. (void)remaining_pos;
  1488. //Once moved, avoid calling the destructors if trivial after move
  1489. if(!trivial_dctr_after_move) {
  1490. boost::container::destroy_alloc(a, remaining_pos, old_finish);
  1491. }
  1492. }
  1493. else { //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
  1494. ::boost::container::uninitialized_move_alloc_n
  1495. (a, pos, static_cast<size_type>(old_finish - pos), make_iterator_uadvance(new_start, before_plus_new));
  1496. }
  1497. old_values_destroyer.release();
  1498. }
  1499. }
  1500. else {
  1501. //If anything goes wrong, this object will destroy
  1502. //all the old objects to fulfill previous vector state
  1503. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1504. //Check if we have to do the insertion in two phases
  1505. //since maybe raw_before is not big enough and
  1506. //the buffer was expanded both sides
  1507. // _________________________________________________
  1508. //| raw_mem | old_begin + old_end | raw_mem | //Old situation
  1509. //|_________|_____________________|_________________|
  1510. // _________________________________________________
  1511. //| old_begin + new + old_end | raw_mem | //New situation with do_after
  1512. //|___________________________________|_____________|
  1513. // _________________________________________________
  1514. //| old_begin + new + old_end | raw_mem | //New without do_after
  1515. //|____________________________|____________________|
  1516. //
  1517. const bool do_after = n > raw_before;
  1518. //Now we can have two situations: the raw_mem of the
  1519. //beginning divides the old_begin, or the new elements:
  1520. if (raw_before <= elemsbefore) {
  1521. //The raw memory divides the old_begin group:
  1522. //
  1523. //If we need two phase construction (do_after)
  1524. //new group is divided in new = new_beg + new_end groups
  1525. //In this phase only new_beg will be inserted
  1526. //
  1527. // _________________________________________________
  1528. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1529. //|_________|___________|_________|_________________|
  1530. // _________________________________________________
  1531. //| old_begin | new_beg | old_end | raw_mem | //New situation with do_after(1),
  1532. //|___________|_________|_________|_________________| //not definitive, pending operations
  1533. // _________________________________________________
  1534. //| old_begin | new | old_end | raw_mem | //New situation without do_after,
  1535. //|___________|_____|_________|_____________________| //definitive.
  1536. //
  1537. //Copy the first part of old_begin to raw_mem
  1538. ::boost::container::uninitialized_move_alloc_n(a, old_start, raw_before, new_start);
  1539. //The buffer is all constructed until old_end,
  1540. //so program trailing destruction and assign final size
  1541. //if !do_after, raw_before+n otherwise.
  1542. size_type new_1st_range;
  1543. old_values_destroyer.increment_size_backwards(raw_before);
  1544. new_1st_range = do_after ? raw_before : n;
  1545. //Now copy the second part of old_begin overwriting itself
  1546. B const old_next(make_iterator_uadvance(old_start, raw_before));
  1547. B const next = ::boost::container::move(old_next, pos, old_start);
  1548. //Now copy the new_beg elements
  1549. insertion_proxy.copy_n_and_update(a, next, new_1st_range);
  1550. //If there is no after work and the last old part needs to be moved to front, do it
  1551. if (!do_after) {
  1552. //Now displace old_end elements and destroy trailing
  1553. B const new_first(make_iterator_uadvance(next, new_1st_range));
  1554. B const p = ::boost::container::move_forward_overlapping(pos, old_finish, new_first);
  1555. (void)p;
  1556. if(!trivial_dctr_after_move)
  1557. boost::container::destroy_alloc(a, p, old_finish);
  1558. }
  1559. }
  1560. else {
  1561. //If we have to expand both sides,
  1562. //we will play if the first new values so
  1563. //calculate the upper bound of new values
  1564. //The raw memory divides the new elements
  1565. //
  1566. //If we need two phase construction (do_after)
  1567. //new group is divided in new = new_beg + new_end groups
  1568. //In this phase only new_beg will be inserted
  1569. //
  1570. // ____________________________________________________
  1571. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1572. //|_______________|___________|_________|______________|
  1573. // ____________________________________________________
  1574. //| old_begin | new_beg | old_end | raw_mem | //New situation with do_after(),
  1575. //|___________|_______________|_________|______________| //not definitive, pending operations
  1576. // ____________________________________________________
  1577. //| old_begin | new | old_end | raw_mem | //New situation without do_after,
  1578. //|___________|_____|_________|________________________| //definitive
  1579. //
  1580. //First copy whole old_begin and part of new to raw_mem
  1581. B const new_pos = ::boost::container::uninitialized_move_alloc(a, old_start, pos, new_start);
  1582. array_destructor_t new_values_destroyer(new_start, a, elemsbefore);
  1583. const size_type mid_n = size_type(raw_before - elemsbefore);
  1584. insertion_proxy.uninitialized_copy_n_and_update(a, new_pos, mid_n);
  1585. new_values_destroyer.release();
  1586. //The buffer is all constructed until old_end
  1587. old_values_destroyer.increment_size_backwards(raw_before);
  1588. if (do_after) {
  1589. //Copy new_beg part
  1590. insertion_proxy.copy_n_and_update(a, old_start, elemsbefore);
  1591. }
  1592. else {
  1593. //Copy all new elements
  1594. const size_type rest_new = size_type(n - mid_n);
  1595. insertion_proxy.copy_n_and_update(a, old_start, rest_new);
  1596. B move_start(make_iterator_uadvance(old_start, rest_new));
  1597. //Displace old_end, but make sure data has to be moved
  1598. B const move_end = ::boost::container::move_forward_overlapping(pos, old_finish, move_start);
  1599. (void)move_end; //To avoid warnings of unused initialization for move_end in case
  1600. //trivial_dctr_after_move is true
  1601. //Destroy remaining moved elements from old_end except if they
  1602. //have trivial destructor after being moved
  1603. if(!trivial_dctr_after_move) {
  1604. boost::container::destroy_alloc(a, move_end, old_finish);
  1605. }
  1606. }
  1607. }
  1608. //This is only executed if two phase construction is needed
  1609. if (do_after) {
  1610. //The raw memory divides the new elements
  1611. // ______________________________________________________
  1612. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1613. //|______________|___________|____________|______________|
  1614. // _______________________________________________________
  1615. //| old_begin + new_beg | new_end |old_end | rawmem | //New situation with do_after(1)
  1616. //|__________________________|_________|________|________|
  1617. // ______________________________________________________
  1618. //| old_begin + new | old_end |raw | //New situation with do_after(2)
  1619. //|_______________________________________|_________|____|
  1620. const size_type n_after = size_type(n - raw_before);
  1621. const size_type elemsafter = size_type(old_size - elemsbefore);
  1622. //We can have two situations:
  1623. if (elemsafter >= n_after) {
  1624. //The raw_mem from end will divide displaced old_end
  1625. //
  1626. //Old situation:
  1627. // ______________________________________________________
  1628. //| raw_mem | old_begin | old_end | raw_mem |
  1629. //|______________|___________|____________|______________|
  1630. //
  1631. //New situation with do_after(1):
  1632. // _______________________________________________________
  1633. //| old_begin + new_beg | new_end |old_end | raw_mem |
  1634. //|__________________________|_________|________|_________|
  1635. //
  1636. //First copy the part of old_end raw_mem
  1637. B finish_n = make_iterator_advance(old_finish, -std::ptrdiff_t(n_after));
  1638. ::boost::container::uninitialized_move_alloc(a, finish_n, old_finish, old_finish);
  1639. old_values_destroyer.increment_size(n_after);
  1640. //Displace the rest of old_end to the new position
  1641. boost::container::move_backward_overlapping(pos, finish_n, old_finish);
  1642. //Now overwrite with new_end
  1643. //The new_end part is [first + (n - n_after), last)
  1644. insertion_proxy.copy_n_and_update(a, pos, n_after);
  1645. }
  1646. else {
  1647. //The raw_mem from end will divide new_end part
  1648. // _____________________________________________________________
  1649. //| raw_mem | old_begin | old_end | raw_mem | //Old situation
  1650. //|______________|___________|____________|_____________________|
  1651. // _____________________________________________________________
  1652. //| old_begin + new_beg | new_end |old_end | raw_mem | //New situation with do_after(2)
  1653. //|__________________________|_______________|________|_________|
  1654. //First initialize data in raw memory
  1655. const size_type mid_last_dist = size_type(n_after - elemsafter);
  1656. //Copy to the old_end part to the uninitialized zone leaving a gap.
  1657. B const mid_last(make_iterator_uadvance(old_finish, mid_last_dist));
  1658. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, mid_last);
  1659. array_destructor_t old_end_destroyer(mid_last, a, iterator_udistance(pos, old_finish));
  1660. //Copy the first part to the already constructed old_end zone
  1661. insertion_proxy.copy_n_and_update(a, pos, elemsafter);
  1662. //Copy the rest to the uninitialized zone filling the gap
  1663. insertion_proxy.uninitialized_copy_n_and_update(a, old_finish, mid_last_dist);
  1664. old_end_destroyer.release();
  1665. }
  1666. }
  1667. old_values_destroyer.release();
  1668. }
  1669. }
  1670. template
  1671. <typename Allocator
  1672. , typename B // B models BidirIterator
  1673. , typename InsertionProxy
  1674. >
  1675. inline void expand_backward_forward_and_insert_alloc_move_forward
  1676. ( B const old_start
  1677. , std::size_t const old_size
  1678. , B const new_start
  1679. , B const pos
  1680. , std::size_t const n
  1681. , InsertionProxy insertion_proxy
  1682. , Allocator& a)
  1683. {
  1684. typedef std::size_t size_type;
  1685. typedef typename allocator_traits<Allocator>::value_type value_type;
  1686. BOOST_STATIC_CONSTEXPR bool trivial_dctr_after_move = has_trivial_destructor_after_move<value_type>::value;
  1687. BOOST_STATIC_CONSTEXPR bool trivial_dctr = dtl::is_trivially_destructible<value_type>::value;
  1688. typedef typename dtl::if_c
  1689. <trivial_dctr
  1690. , dtl::null_scoped_destructor_n<Allocator, B>
  1691. , dtl::scoped_destructor_n<Allocator, B>
  1692. >::type array_destructor_t;
  1693. //n can be zero to just expand capacity
  1694. B const old_finish = make_iterator_uadvance(old_start, old_size);
  1695. const size_type new_size = size_type(old_size + n);
  1696. B const new_finish = make_iterator_uadvance(new_start, new_size);
  1697. //We can have 8 possibilities:
  1698. const size_type elemsafter = static_cast<size_type>(iterator_udistance(pos, old_finish));
  1699. const size_type raw_after = static_cast<size_type>(iterator_udistance(old_finish, new_finish));
  1700. const size_type after_plus_new = size_type(elemsafter + n);
  1701. //Check if raw_before is big enough to hold the new data + the end of old data
  1702. if (raw_after >= after_plus_new) {
  1703. //If anything goes wrong, this object will destroy
  1704. //all the old objects to fulfill previous vector state
  1705. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1706. //______________________ __________________________________
  1707. //| old_begin | old_end | raw_mem //Old situation
  1708. //|___________|_________|__________________________________
  1709. // _____________________ _________________________________
  1710. //| old_begin | old_end | raw_mem | new | old_end | //First step
  1711. //|___________|_________|__________|__________|___________|
  1712. //Copy first new objects, after that old values after pos
  1713. B new_elem_pos = new_finish - after_plus_new;
  1714. insertion_proxy.uninitialized_copy_n_and_update(a, new_elem_pos, n);
  1715. array_destructor_t new_values_destroyer(new_elem_pos, a, n);
  1716. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, new_elem_pos+n);
  1717. new_values_destroyer.set_size(after_plus_new);
  1718. //Check if raw_before is so big that even copying the old data + new data
  1719. //there is a gap between the new data and the old data
  1720. if (raw_after >= new_size) {
  1721. //______________________ __________________________________
  1722. //| old_begin | old_end | raw_mem //Old situation
  1723. //|___________|_________|__________________________________
  1724. // _____________________ _________________________________
  1725. //| old_begin | old_end | raw_mem | new | old_end | //First step
  1726. //|___________|_________|______________|________|_________|
  1727. // _____________________V_________________________________
  1728. //| raw_mem | old_begin | new | old_end | //New situation
  1729. //|________________________|___________|________|_________|
  1730. //
  1731. //Now initialize the rest of memory with the last old values
  1732. ::boost::container::uninitialized_move_alloc(a, old_start, pos, new_start);
  1733. //All new elements correctly constructed, avoid new element destruction
  1734. new_values_destroyer.release();
  1735. //Old values destroyed automatically with "old_values_destroyer"
  1736. //when "old_values_destroyer" goes out of scope unless the have trivial
  1737. //destructor after move.
  1738. if(trivial_dctr_after_move)
  1739. old_values_destroyer.release();
  1740. }
  1741. //raw_before is so big that divides old_end
  1742. else {
  1743. //______________________ ____________________________
  1744. //| old_begin | old_end | raw_mem //Old situation
  1745. //|___________|_________|____________________________
  1746. // _____________________ ____________________________
  1747. //| old_begin | old_end | raw_mem | new | old_end | //First step
  1748. //|___________|_________|_________|________|_________|
  1749. // _________________________________________________
  1750. //| raw_mem | old_begin | new | old_end | //New situation
  1751. //|___________________|___________|________|_________|
  1752. //Now initialize the rest of raw_before memory with the
  1753. //last elements before new values
  1754. const size_type raw_gap = raw_after - after_plus_new;
  1755. B const pre_pos_raw = pos - raw_gap;
  1756. ::boost::container::uninitialized_move_alloc_n(a, pre_pos_raw, raw_gap, old_finish);
  1757. new_values_destroyer.release();
  1758. old_values_destroyer.increment_size(raw_after);
  1759. //Now move remaining last objects in the old buffer begin
  1760. BOOST_ASSERT(old_start != old_finish);
  1761. boost::container::move_backward_overlapping(old_start, pre_pos_raw, old_finish);
  1762. old_values_destroyer.release();
  1763. if (!trivial_dctr_after_move) {
  1764. boost::container::destroy_alloc(a, old_start, new_start);
  1765. }
  1766. }
  1767. }
  1768. else{
  1769. //If anything goes wrong, this object will destroy
  1770. //all the old objects to fulfill previous vector state
  1771. array_destructor_t old_values_destroyer(old_start, a, old_size);
  1772. //Now we can have two situations: the raw_mem of the
  1773. //end divides the new elements or the old_end
  1774. if (raw_after > elemsafter) {
  1775. //The raw memory divides the new elements
  1776. //__________________________________
  1777. //| old_begin | old_end | raw | //Old situation
  1778. //|___________|_________|___________|
  1779. // _____ ___________________________
  1780. //| raw | old_begin | new | old_end | //New situation
  1781. //|_____|___________|_____|_________|
  1782. //First copy whole old_end and part of new to raw_mem
  1783. B p = new_finish - elemsafter;
  1784. ::boost::container::uninitialized_move_alloc(a, pos, old_finish, p);
  1785. array_destructor_t new_values_destroyer(p, a, elemsafter);
  1786. //Copy all new elements
  1787. const size_type mid_n = size_type(raw_after - elemsafter);
  1788. const size_type rest_new = size_type(n - mid_n);
  1789. B new_rng_start = old_finish - rest_new;
  1790. insertion_proxy.copy_n_and_update(a, new_rng_start, rest_new);
  1791. insertion_proxy.uninitialized_copy_n_and_update(a, old_finish, mid_n);
  1792. new_values_destroyer.release();
  1793. old_values_destroyer.increment_size_backwards(raw_after);
  1794. //Displace old_end, but make sure data has to be moved
  1795. p = ::boost::container::move_backward_overlapping(old_start, pos, new_rng_start);
  1796. //Destroy remaining moved elements from old_begin except if they
  1797. //have trivial destructor after being moved
  1798. old_values_destroyer.release();
  1799. if (!trivial_dctr_after_move) {
  1800. boost::container::destroy_alloc(a, old_start, p);
  1801. }
  1802. }
  1803. else {
  1804. //The raw memory divides the old_end group:
  1805. //________________________________________
  1806. //| old_begin | old_end | raw | //Old situation
  1807. //|___________|_______________|___________|
  1808. // _____ __________________________________
  1809. //| raw | old_begin | new | old_end | //New situation
  1810. //|_____|___________|_____|_______________|
  1811. //
  1812. //Copy the last part of old_end to raw_mem
  1813. const B old_end_pivot = old_finish - raw_after;
  1814. ::boost::container::uninitialized_move_alloc_n(a, old_end_pivot, raw_after, old_finish);
  1815. //The buffer is all constructed
  1816. old_values_destroyer.increment_size_backwards(raw_after);
  1817. //Now copy the first part of old_end overwriting itself
  1818. B const new_end_pos = ::boost::container::move_backward_overlapping(pos, old_end_pivot, old_finish);
  1819. B const new_beg_pos = new_end_pos - n;
  1820. //Now copy the new_beg elements
  1821. insertion_proxy.copy_n_and_update(a, new_beg_pos, n);
  1822. B const p = ::boost::container::move_backward_overlapping(old_start, pos, new_beg_pos);
  1823. old_values_destroyer.release();
  1824. if (!trivial_dctr_after_move) {
  1825. (void)p;
  1826. boost::container::destroy_alloc(a, old_start, p);
  1827. }
  1828. }
  1829. }
  1830. }
  1831. template <class R, class InsertionProxy, class Allocator>
  1832. void expand_backward_forward_and_insert_alloc
  1833. ( R const old_start
  1834. , std::size_t const old_size
  1835. , R const new_start
  1836. , R const pos
  1837. , std::size_t const n
  1838. , InsertionProxy insertion_proxy
  1839. , Allocator& a)
  1840. {
  1841. if(new_start < old_start){
  1842. expand_backward_forward_and_insert_alloc_move_backward(old_start, old_size, new_start, pos, n, insertion_proxy, a);
  1843. }
  1844. else{
  1845. expand_backward_forward_and_insert_alloc_move_forward(old_start, old_size, new_start, pos, n, insertion_proxy, a);
  1846. }
  1847. }
  1848. } //namespace container {
  1849. } //namespace boost {
  1850. //#pragma GCC diagnostic ignored "-Wclass-memaccess"
  1851. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  1852. #pragma GCC diagnostic pop
  1853. #endif
  1854. #endif //#ifndef BOOST_CONTAINER_DETAIL_COPY_MOVE_ALGO_HPP