copy_move_algo.hpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. // move
  27. #include <boost/move/adl_move_swap.hpp>
  28. #include <boost/move/iterator.hpp>
  29. #include <boost/move/utility_core.hpp>
  30. // other
  31. #include <boost/core/no_exceptions_support.hpp>
  32. // std
  33. #include <cstring> //for memmove/memcpy
  34. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  35. #pragma GCC diagnostic push
  36. //pair memcpy optimizations rightfully detected by GCC
  37. # if defined(BOOST_GCC) && (BOOST_GCC >= 80000)
  38. # pragma GCC diagnostic ignored "-Wclass-memaccess"
  39. # endif
  40. //GCC 8 seems a bit confused about array access error with static_vector
  41. //when out of bound exceptions are being thrown.
  42. # if defined(BOOST_GCC) && (BOOST_GCC >= 80000) && (BOOST_GCC < 80200)
  43. # pragma GCC diagnostic ignored "-Wstringop-overflow"
  44. # endif
  45. # pragma GCC diagnostic ignored "-Warray-bounds"
  46. #endif
  47. namespace boost {
  48. namespace container {
  49. namespace dtl {
  50. template<class I>
  51. struct are_elements_contiguous
  52. {
  53. static const bool value = false;
  54. };
  55. /////////////////////////
  56. // raw pointers
  57. /////////////////////////
  58. template<class T>
  59. struct are_elements_contiguous<T*>
  60. {
  61. static const bool value = true;
  62. };
  63. /////////////////////////
  64. // move iterators
  65. /////////////////////////
  66. template<class It>
  67. struct are_elements_contiguous< ::boost::move_iterator<It> >
  68. : are_elements_contiguous<It>
  69. {};
  70. } //namespace dtl {
  71. /////////////////////////
  72. // predeclarations
  73. /////////////////////////
  74. template <class Pointer, bool IsConst>
  75. class vec_iterator;
  76. } //namespace container {
  77. namespace interprocess {
  78. template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment>
  79. class offset_ptr;
  80. } //namespace interprocess {
  81. namespace container {
  82. namespace dtl {
  83. /////////////////////////
  84. //vector_[const_]iterator
  85. /////////////////////////
  86. template <class Pointer, bool IsConst>
  87. struct are_elements_contiguous<boost::container::vec_iterator<Pointer, IsConst> >
  88. {
  89. static const bool value = true;
  90. };
  91. /////////////////////////
  92. // offset_ptr
  93. /////////////////////////
  94. template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment>
  95. struct are_elements_contiguous< ::boost::interprocess::offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment> >
  96. {
  97. static const bool value = true;
  98. };
  99. template <typename I, typename O>
  100. struct are_contiguous_and_same
  101. : boost::move_detail::and_
  102. < are_elements_contiguous<I>
  103. , are_elements_contiguous<O>
  104. , is_same< typename remove_const< typename ::boost::container::iterator_traits<I>::value_type >::type
  105. , typename ::boost::container::iterator_traits<O>::value_type
  106. >
  107. >
  108. {};
  109. template <typename I, typename O>
  110. struct is_memtransfer_copy_assignable
  111. : boost::move_detail::and_
  112. < are_contiguous_and_same<I, O>
  113. , dtl::is_trivially_copy_assignable< typename ::boost::container::iterator_traits<I>::value_type >
  114. >
  115. {};
  116. template <typename I, typename O>
  117. struct is_memtransfer_copy_constructible
  118. : boost::move_detail::and_
  119. < are_contiguous_and_same<I, O>
  120. , dtl::is_trivially_copy_constructible< typename ::boost::container::iterator_traits<I>::value_type >
  121. >
  122. {};
  123. template <typename I, typename O, typename R>
  124. struct enable_if_memtransfer_copy_constructible
  125. : enable_if<dtl::is_memtransfer_copy_constructible<I, O>, R>
  126. {};
  127. template <typename I, typename O, typename R>
  128. struct disable_if_memtransfer_copy_constructible
  129. : disable_if<dtl::is_memtransfer_copy_constructible<I, O>, R>
  130. {};
  131. template <typename I, typename O, typename R>
  132. struct enable_if_memtransfer_copy_assignable
  133. : enable_if<dtl::is_memtransfer_copy_assignable<I, O>, R>
  134. {};
  135. template <typename I, typename O, typename R>
  136. struct disable_if_memtransfer_copy_assignable
  137. : disable_if<dtl::is_memtransfer_copy_assignable<I, O>, R>
  138. {};
  139. template
  140. <typename I, // I models InputIterator
  141. typename F> // F models ForwardIterator
  142. inline F memmove(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  143. {
  144. typedef typename boost::container::iterator_traits<I>::value_type value_type;
  145. value_type *const dest_raw = boost::movelib::iterator_to_raw_pointer(r);
  146. const value_type *const beg_raw = boost::movelib::iterator_to_raw_pointer(f);
  147. const value_type *const end_raw = boost::movelib::iterator_to_raw_pointer(l);
  148. if(BOOST_LIKELY(beg_raw != end_raw && dest_raw && beg_raw)){
  149. const typename boost::container::iterator_traits<I>::difference_type n = end_raw - beg_raw;
  150. std::memmove(dest_raw, beg_raw, sizeof(value_type)*n);
  151. boost::container::iterator_advance(r, n);
  152. }
  153. return r;
  154. }
  155. template
  156. <typename I, // I models InputIterator
  157. typename U, // U models unsigned integral constant
  158. typename F> // F models ForwardIterator
  159. F memmove_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  160. {
  161. typedef typename boost::container::iterator_traits<I>::value_type value_type;
  162. if(BOOST_LIKELY(n)){
  163. std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
  164. boost::container::iterator_advance(r, n);
  165. }
  166. return r;
  167. }
  168. template
  169. <typename I, // I models InputIterator
  170. typename U, // U models unsigned integral constant
  171. typename F> // F models ForwardIterator
  172. I memmove_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  173. {
  174. if(BOOST_LIKELY(n)){
  175. typedef typename boost::container::iterator_traits<I>::value_type value_type;
  176. std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
  177. boost::container::iterator_advance(f, n);
  178. }
  179. return f;
  180. }
  181. template
  182. <typename I, // I models InputIterator
  183. typename U, // U models unsigned integral constant
  184. typename F> // F models ForwardIterator
  185. I memmove_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  186. {
  187. typedef typename boost::container::iterator_traits<I>::value_type value_type;
  188. if(BOOST_LIKELY(n)){
  189. std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
  190. boost::container::iterator_advance(f, n);
  191. boost::container::iterator_advance(r, n);
  192. }
  193. return f;
  194. }
  195. template <typename O>
  196. struct is_memzero_initializable
  197. {
  198. typedef typename ::boost::container::iterator_traits<O>::value_type value_type;
  199. static const bool value = are_elements_contiguous<O>::value &&
  200. ( dtl::is_integral<value_type>::value || dtl::is_enum<value_type>::value
  201. #if defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL)
  202. || dtl::is_pointer<value_type>::value
  203. #endif
  204. #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO)
  205. || dtl::is_floating_point<value_type>::value
  206. #endif
  207. #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO) && defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL)
  208. || dtl::is_pod<value_type>::value
  209. #endif
  210. );
  211. };
  212. template <typename O, typename R>
  213. struct enable_if_memzero_initializable
  214. : enable_if_c<dtl::is_memzero_initializable<O>::value, R>
  215. {};
  216. template <typename O, typename R>
  217. struct disable_if_memzero_initializable
  218. : enable_if_c<!dtl::is_memzero_initializable<O>::value, R>
  219. {};
  220. template <typename I, typename R>
  221. struct enable_if_trivially_destructible
  222. : enable_if_c < dtl::is_trivially_destructible
  223. <typename boost::container::iterator_traits<I>::value_type>::value
  224. , R>
  225. {};
  226. template <typename I, typename R>
  227. struct disable_if_trivially_destructible
  228. : enable_if_c <!dtl::is_trivially_destructible
  229. <typename boost::container::iterator_traits<I>::value_type>::value
  230. , R>
  231. {};
  232. } //namespace dtl {
  233. //////////////////////////////////////////////////////////////////////////////
  234. //
  235. // uninitialized_move_alloc
  236. //
  237. //////////////////////////////////////////////////////////////////////////////
  238. //! <b>Effects</b>:
  239. //! \code
  240. //! for (; f != l; ++r, ++f)
  241. //! allocator_traits::construct(a, &*r, boost::move(*f));
  242. //! \endcode
  243. //!
  244. //! <b>Returns</b>: r
  245. template
  246. <typename Allocator,
  247. typename I, // I models InputIterator
  248. typename F> // F models ForwardIterator
  249. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  250. uninitialized_move_alloc(Allocator &a, I f, I l, F r)
  251. {
  252. F back = r;
  253. BOOST_TRY{
  254. while (f != l) {
  255. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
  256. ++f; ++r;
  257. }
  258. }
  259. BOOST_CATCH(...){
  260. for (; back != r; ++back){
  261. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  262. }
  263. BOOST_RETHROW;
  264. }
  265. BOOST_CATCH_END
  266. return r;
  267. }
  268. template
  269. <typename Allocator,
  270. typename I, // I models InputIterator
  271. typename F> // F models ForwardIterator
  272. inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  273. uninitialized_move_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  274. { return dtl::memmove(f, l, r); }
  275. //////////////////////////////////////////////////////////////////////////////
  276. //
  277. // uninitialized_move_alloc_n
  278. //
  279. //////////////////////////////////////////////////////////////////////////////
  280. //! <b>Effects</b>:
  281. //! \code
  282. //! for (; n--; ++r, ++f)
  283. //! allocator_traits::construct(a, &*r, boost::move(*f));
  284. //! \endcode
  285. //!
  286. //! <b>Returns</b>: r
  287. template
  288. <typename Allocator,
  289. typename I, // I models InputIterator
  290. typename F> // F models ForwardIterator
  291. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  292. uninitialized_move_alloc_n(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  293. {
  294. F back = r;
  295. BOOST_TRY{
  296. while (n--) {
  297. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
  298. ++f; ++r;
  299. }
  300. }
  301. BOOST_CATCH(...){
  302. for (; back != r; ++back){
  303. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  304. }
  305. BOOST_RETHROW;
  306. }
  307. BOOST_CATCH_END
  308. return r;
  309. }
  310. template
  311. <typename Allocator,
  312. typename I, // I models InputIterator
  313. typename F> // F models ForwardIterator
  314. inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  315. uninitialized_move_alloc_n(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  316. { return dtl::memmove_n(f, n, r); }
  317. //////////////////////////////////////////////////////////////////////////////
  318. //
  319. // uninitialized_move_alloc_n_source
  320. //
  321. //////////////////////////////////////////////////////////////////////////////
  322. //! <b>Effects</b>:
  323. //! \code
  324. //! for (; n--; ++r, ++f)
  325. //! allocator_traits::construct(a, &*r, boost::move(*f));
  326. //! \endcode
  327. //!
  328. //! <b>Returns</b>: f (after incremented)
  329. template
  330. <typename Allocator,
  331. typename I, // I models InputIterator
  332. typename F> // F models ForwardIterator
  333. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, I>::type
  334. uninitialized_move_alloc_n_source(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  335. {
  336. F back = r;
  337. BOOST_TRY{
  338. while (n--) {
  339. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
  340. ++f; ++r;
  341. }
  342. }
  343. BOOST_CATCH(...){
  344. for (; back != r; ++back){
  345. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  346. }
  347. BOOST_RETHROW;
  348. }
  349. BOOST_CATCH_END
  350. return f;
  351. }
  352. template
  353. <typename Allocator,
  354. typename I, // I models InputIterator
  355. typename F> // F models ForwardIterator
  356. inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
  357. uninitialized_move_alloc_n_source(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  358. { return dtl::memmove_n_source(f, n, r); }
  359. //////////////////////////////////////////////////////////////////////////////
  360. //
  361. // uninitialized_copy_alloc
  362. //
  363. //////////////////////////////////////////////////////////////////////////////
  364. //! <b>Effects</b>:
  365. //! \code
  366. //! for (; f != l; ++r, ++f)
  367. //! allocator_traits::construct(a, &*r, *f);
  368. //! \endcode
  369. //!
  370. //! <b>Returns</b>: r
  371. template
  372. <typename Allocator,
  373. typename I, // I models InputIterator
  374. typename F> // F models ForwardIterator
  375. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  376. uninitialized_copy_alloc(Allocator &a, I f, I l, F r)
  377. {
  378. F back = r;
  379. BOOST_TRY{
  380. while (f != l) {
  381. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), *f);
  382. ++f; ++r;
  383. }
  384. }
  385. BOOST_CATCH(...){
  386. for (; back != r; ++back){
  387. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  388. }
  389. BOOST_RETHROW;
  390. }
  391. BOOST_CATCH_END
  392. return r;
  393. }
  394. template
  395. <typename Allocator,
  396. typename I, // I models InputIterator
  397. typename F> // F models ForwardIterator
  398. inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  399. uninitialized_copy_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  400. { return dtl::memmove(f, l, r); }
  401. //////////////////////////////////////////////////////////////////////////////
  402. //
  403. // uninitialized_copy_alloc_n
  404. //
  405. //////////////////////////////////////////////////////////////////////////////
  406. //! <b>Effects</b>:
  407. //! \code
  408. //! for (; n--; ++r, ++f)
  409. //! allocator_traits::construct(a, &*r, *f);
  410. //! \endcode
  411. //!
  412. //! <b>Returns</b>: r
  413. template
  414. <typename Allocator,
  415. typename I, // I models InputIterator
  416. typename F> // F models ForwardIterator
  417. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
  418. uninitialized_copy_alloc_n(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  419. {
  420. F back = r;
  421. BOOST_TRY{
  422. while (n--) {
  423. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), *f);
  424. ++f; ++r;
  425. }
  426. }
  427. BOOST_CATCH(...){
  428. for (; back != r; ++back){
  429. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  430. }
  431. BOOST_RETHROW;
  432. }
  433. BOOST_CATCH_END
  434. return r;
  435. }
  436. template
  437. <typename Allocator,
  438. typename I, // I models InputIterator
  439. typename F> // F models ForwardIterator
  440. inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
  441. uninitialized_copy_alloc_n(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  442. { return dtl::memmove_n(f, n, r); }
  443. //////////////////////////////////////////////////////////////////////////////
  444. //
  445. // uninitialized_copy_alloc_n_source
  446. //
  447. //////////////////////////////////////////////////////////////////////////////
  448. //! <b>Effects</b>:
  449. //! \code
  450. //! for (; n--; ++r, ++f)
  451. //! allocator_traits::construct(a, &*r, *f);
  452. //! \endcode
  453. //!
  454. //! <b>Returns</b>: f (after incremented)
  455. template
  456. <typename Allocator,
  457. typename I, // I models InputIterator
  458. typename F> // F models ForwardIterator
  459. inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, I>::type
  460. uninitialized_copy_alloc_n_source(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  461. {
  462. F back = r;
  463. BOOST_TRY{
  464. while (n) {
  465. boost::container::construct_in_place(a, boost::movelib::iterator_to_raw_pointer(r), f);
  466. ++f; ++r; --n;
  467. }
  468. }
  469. BOOST_CATCH(...){
  470. for (; back != r; ++back){
  471. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  472. }
  473. BOOST_RETHROW;
  474. }
  475. BOOST_CATCH_END
  476. return f;
  477. }
  478. template
  479. <typename Allocator,
  480. typename I, // I models InputIterator
  481. typename F> // F models ForwardIterator
  482. inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
  483. uninitialized_copy_alloc_n_source(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  484. { return dtl::memmove_n_source(f, n, r); }
  485. //////////////////////////////////////////////////////////////////////////////
  486. //
  487. // uninitialized_value_init_alloc_n
  488. //
  489. //////////////////////////////////////////////////////////////////////////////
  490. //! <b>Effects</b>:
  491. //! \code
  492. //! for (; n--; ++r, ++f)
  493. //! allocator_traits::construct(a, &*r);
  494. //! \endcode
  495. //!
  496. //! <b>Returns</b>: r
  497. template
  498. <typename Allocator,
  499. typename F> // F models ForwardIterator
  500. inline typename dtl::disable_if_memzero_initializable<F, F>::type
  501. uninitialized_value_init_alloc_n(Allocator &a, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  502. {
  503. F back = r;
  504. BOOST_TRY{
  505. while (n--) {
  506. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r));
  507. ++r;
  508. }
  509. }
  510. BOOST_CATCH(...){
  511. for (; back != r; ++back){
  512. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  513. }
  514. BOOST_RETHROW;
  515. }
  516. BOOST_CATCH_END
  517. return r;
  518. }
  519. template
  520. <typename Allocator,
  521. typename F> // F models ForwardIterator
  522. inline typename dtl::enable_if_memzero_initializable<F, F>::type
  523. uninitialized_value_init_alloc_n(Allocator &, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  524. {
  525. typedef typename boost::container::iterator_traits<F>::value_type value_type;
  526. std::memset((void*)boost::movelib::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n);
  527. boost::container::iterator_advance(r, n);
  528. return r;
  529. }
  530. //////////////////////////////////////////////////////////////////////////////
  531. //
  532. // uninitialized_default_init_alloc_n
  533. //
  534. //////////////////////////////////////////////////////////////////////////////
  535. //! <b>Effects</b>:
  536. //! \code
  537. //! for (; n--; ++r, ++f)
  538. //! allocator_traits::construct(a, &*r);
  539. //! \endcode
  540. //!
  541. //! <b>Returns</b>: r
  542. template
  543. <typename Allocator,
  544. typename F> // F models ForwardIterator
  545. inline F uninitialized_default_init_alloc_n(Allocator &a, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  546. {
  547. F back = r;
  548. BOOST_TRY{
  549. while (n--) {
  550. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), default_init);
  551. ++r;
  552. }
  553. }
  554. BOOST_CATCH(...){
  555. for (; back != r; ++back){
  556. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  557. }
  558. BOOST_RETHROW;
  559. }
  560. BOOST_CATCH_END
  561. return r;
  562. }
  563. //////////////////////////////////////////////////////////////////////////////
  564. //
  565. // uninitialized_fill_alloc
  566. //
  567. //////////////////////////////////////////////////////////////////////////////
  568. //! <b>Effects</b>:
  569. //! \code
  570. //! for (; f != l; ++r, ++f)
  571. //! allocator_traits::construct(a, &*r, *f);
  572. //! \endcode
  573. //!
  574. //! <b>Returns</b>: r
  575. template
  576. <typename Allocator,
  577. typename F, // F models ForwardIterator
  578. typename T>
  579. inline void uninitialized_fill_alloc(Allocator &a, F f, F l, const T &t)
  580. {
  581. F back = f;
  582. BOOST_TRY{
  583. while (f != l) {
  584. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(f), t);
  585. ++f;
  586. }
  587. }
  588. BOOST_CATCH(...){
  589. for (; back != l; ++back){
  590. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  591. }
  592. BOOST_RETHROW;
  593. }
  594. BOOST_CATCH_END
  595. }
  596. //////////////////////////////////////////////////////////////////////////////
  597. //
  598. // uninitialized_fill_alloc_n
  599. //
  600. //////////////////////////////////////////////////////////////////////////////
  601. //! <b>Effects</b>:
  602. //! \code
  603. //! for (; n--; ++r, ++f)
  604. //! allocator_traits::construct(a, &*r, v);
  605. //! \endcode
  606. //!
  607. //! <b>Returns</b>: r
  608. template
  609. <typename Allocator,
  610. typename T,
  611. typename F> // F models ForwardIterator
  612. inline F uninitialized_fill_alloc_n(Allocator &a, const T &v, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
  613. {
  614. F back = r;
  615. BOOST_TRY{
  616. while (n--) {
  617. allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), v);
  618. ++r;
  619. }
  620. }
  621. BOOST_CATCH(...){
  622. for (; back != r; ++back){
  623. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(back));
  624. }
  625. BOOST_RETHROW;
  626. }
  627. BOOST_CATCH_END
  628. return r;
  629. }
  630. //////////////////////////////////////////////////////////////////////////////
  631. //
  632. // copy
  633. //
  634. //////////////////////////////////////////////////////////////////////////////
  635. template
  636. <typename I, // I models InputIterator
  637. typename F> // F models ForwardIterator
  638. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  639. copy(I f, I l, F r)
  640. {
  641. while (f != l) {
  642. *r = *f;
  643. ++f; ++r;
  644. }
  645. return r;
  646. }
  647. template
  648. <typename I, // I models InputIterator
  649. typename F> // F models ForwardIterator
  650. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  651. copy(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  652. { return dtl::memmove(f, l, r); }
  653. //////////////////////////////////////////////////////////////////////////////
  654. //
  655. // copy_n
  656. //
  657. //////////////////////////////////////////////////////////////////////////////
  658. template
  659. <typename I, // I models InputIterator
  660. typename U, // U models unsigned integral constant
  661. typename F> // F models ForwardIterator
  662. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  663. copy_n(I f, U n, F r)
  664. {
  665. while (n) {
  666. --n;
  667. *r = *f;
  668. ++f; ++r;
  669. }
  670. return r;
  671. }
  672. template
  673. <typename I, // I models InputIterator
  674. typename U, // U models unsigned integral constant
  675. typename F> // F models ForwardIterator
  676. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  677. copy_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  678. { return dtl::memmove_n(f, n, r); }
  679. //////////////////////////////////////////////////////////////////////////////
  680. //
  681. // copy_n_source
  682. //
  683. //////////////////////////////////////////////////////////////////////////////
  684. template
  685. <typename I, // I models InputIterator
  686. typename U, // U models unsigned integral constant
  687. typename F> // F models ForwardIterator
  688. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  689. copy_n_source(I f, U n, F r)
  690. {
  691. while (n--) {
  692. boost::container::assign_in_place(r, f);
  693. ++f; ++r;
  694. }
  695. return f;
  696. }
  697. template
  698. <typename I, // I models InputIterator
  699. typename U, // U models unsigned integral constant
  700. typename F> // F models ForwardIterator
  701. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  702. copy_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  703. { return dtl::memmove_n_source(f, n, r); }
  704. //////////////////////////////////////////////////////////////////////////////
  705. //
  706. // copy_n_source_dest
  707. //
  708. //////////////////////////////////////////////////////////////////////////////
  709. template
  710. <typename I, // I models InputIterator
  711. typename U, // U models unsigned integral constant
  712. typename F> // F models ForwardIterator
  713. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  714. copy_n_source_dest(I f, U n, F &r)
  715. {
  716. while (n--) {
  717. *r = *f;
  718. ++f; ++r;
  719. }
  720. return f;
  721. }
  722. template
  723. <typename I, // I models InputIterator
  724. typename U, // U models unsigned integral constant
  725. typename F> // F models ForwardIterator
  726. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  727. copy_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  728. { return dtl::memmove_n_source_dest(f, n, r); }
  729. //////////////////////////////////////////////////////////////////////////////
  730. //
  731. // move
  732. //
  733. //////////////////////////////////////////////////////////////////////////////
  734. template
  735. <typename I, // I models InputIterator
  736. typename F> // F models ForwardIterator
  737. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  738. move(I f, I l, F r)
  739. {
  740. while (f != l) {
  741. *r = ::boost::move(*f);
  742. ++f; ++r;
  743. }
  744. return r;
  745. }
  746. template
  747. <typename I, // I models InputIterator
  748. typename F> // F models ForwardIterator
  749. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  750. move(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  751. { return dtl::memmove(f, l, r); }
  752. //////////////////////////////////////////////////////////////////////////////
  753. //
  754. // move_n
  755. //
  756. //////////////////////////////////////////////////////////////////////////////
  757. template
  758. <typename I, // I models InputIterator
  759. typename U, // U models unsigned integral constant
  760. typename F> // F models ForwardIterator
  761. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  762. move_n(I f, U n, F r)
  763. {
  764. while (n--) {
  765. *r = ::boost::move(*f);
  766. ++f; ++r;
  767. }
  768. return r;
  769. }
  770. template
  771. <typename I, // I models InputIterator
  772. typename U, // U models unsigned integral constant
  773. typename F> // F models ForwardIterator
  774. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  775. move_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  776. { return dtl::memmove_n(f, n, r); }
  777. //////////////////////////////////////////////////////////////////////////////
  778. //
  779. // move_backward
  780. //
  781. //////////////////////////////////////////////////////////////////////////////
  782. template
  783. <typename I, // I models BidirectionalIterator
  784. typename F> // F models ForwardIterator
  785. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
  786. move_backward(I f, I l, F r)
  787. {
  788. while (f != l) {
  789. --l; --r;
  790. *r = ::boost::move(*l);
  791. }
  792. return r;
  793. }
  794. template
  795. <typename I, // I models InputIterator
  796. typename F> // F models ForwardIterator
  797. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
  798. move_backward(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
  799. {
  800. typedef typename boost::container::iterator_traits<I>::value_type value_type;
  801. const typename boost::container::iterator_traits<I>::difference_type n = boost::container::iterator_distance(f, l);
  802. r -= n;
  803. std::memmove((boost::movelib::iterator_to_raw_pointer)(r), (boost::movelib::iterator_to_raw_pointer)(f), sizeof(value_type)*n);
  804. return r;
  805. }
  806. //////////////////////////////////////////////////////////////////////////////
  807. //
  808. // move_n_source_dest
  809. //
  810. //////////////////////////////////////////////////////////////////////////////
  811. template
  812. <typename I // I models InputIterator
  813. ,typename U // U models unsigned integral constant
  814. ,typename F> // F models ForwardIterator
  815. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  816. move_n_source_dest(I f, U n, F &r)
  817. {
  818. while (n--) {
  819. *r = ::boost::move(*f);
  820. ++f; ++r;
  821. }
  822. return f;
  823. }
  824. template
  825. <typename I // I models InputIterator
  826. ,typename U // U models unsigned integral constant
  827. ,typename F> // F models ForwardIterator
  828. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  829. move_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
  830. { return dtl::memmove_n_source_dest(f, n, r); }
  831. //////////////////////////////////////////////////////////////////////////////
  832. //
  833. // move_n_source
  834. //
  835. //////////////////////////////////////////////////////////////////////////////
  836. template
  837. <typename I // I models InputIterator
  838. ,typename U // U models unsigned integral constant
  839. ,typename F> // F models ForwardIterator
  840. inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
  841. move_n_source(I f, U n, F r)
  842. {
  843. while (n--) {
  844. *r = ::boost::move(*f);
  845. ++f; ++r;
  846. }
  847. return f;
  848. }
  849. template
  850. <typename I // I models InputIterator
  851. ,typename U // U models unsigned integral constant
  852. ,typename F> // F models ForwardIterator
  853. inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
  854. move_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
  855. { return dtl::memmove_n_source(f, n, r); }
  856. //////////////////////////////////////////////////////////////////////////////
  857. //
  858. // destroy_alloc_n
  859. //
  860. //////////////////////////////////////////////////////////////////////////////
  861. template
  862. <typename Allocator
  863. ,typename I // I models InputIterator
  864. ,typename U> // U models unsigned integral constant
  865. inline typename dtl::disable_if_trivially_destructible<I, void>::type
  866. destroy_alloc_n(Allocator &a, I f, U n)
  867. {
  868. while(n){
  869. --n;
  870. allocator_traits<Allocator>::destroy(a, boost::movelib::iterator_to_raw_pointer(f));
  871. ++f;
  872. }
  873. }
  874. template
  875. <typename Allocator
  876. ,typename I // I models InputIterator
  877. ,typename U> // U models unsigned integral constant
  878. inline typename dtl::enable_if_trivially_destructible<I, void>::type
  879. destroy_alloc_n(Allocator &, I, U)
  880. {}
  881. //////////////////////////////////////////////////////////////////////////////
  882. //
  883. // deep_swap_alloc_n
  884. //
  885. //////////////////////////////////////////////////////////////////////////////
  886. template
  887. <std::size_t MaxTmpBytes
  888. ,typename Allocator
  889. ,typename F // F models ForwardIterator
  890. ,typename G // G models ForwardIterator
  891. >
  892. inline typename dtl::disable_if_memtransfer_copy_assignable<F, G, void>::type
  893. deep_swap_alloc_n( Allocator &a, F short_range_f, typename allocator_traits<Allocator>::size_type n_i
  894. , G large_range_f, typename allocator_traits<Allocator>::size_type n_j)
  895. {
  896. typename allocator_traits<Allocator>::size_type n = 0;
  897. for (; n != n_i ; ++short_range_f, ++large_range_f, ++n){
  898. boost::adl_move_swap(*short_range_f, *large_range_f);
  899. }
  900. boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw
  901. boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i);
  902. }
  903. static const std::size_t DeepSwapAllocNMaxStorage = std::size_t(1) << std::size_t(11); //2K bytes
  904. template
  905. <std::size_t MaxTmpBytes
  906. ,typename Allocator
  907. ,typename F // F models ForwardIterator
  908. ,typename G // G models ForwardIterator
  909. >
  910. inline typename dtl::enable_if_c
  911. < dtl::is_memtransfer_copy_assignable<F, G>::value && (MaxTmpBytes <= DeepSwapAllocNMaxStorage) && false
  912. , void>::type
  913. deep_swap_alloc_n( Allocator &a, F short_range_f, typename allocator_traits<Allocator>::size_type n_i
  914. , G large_range_f, typename allocator_traits<Allocator>::size_type n_j)
  915. {
  916. typedef typename allocator_traits<Allocator>::value_type value_type;
  917. typedef typename dtl::aligned_storage
  918. <MaxTmpBytes, dtl::alignment_of<value_type>::value>::type storage_type;
  919. storage_type storage;
  920. const std::size_t n_i_bytes = sizeof(value_type)*n_i;
  921. void *const large_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f));
  922. void *const short_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f));
  923. void *const stora_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(storage.data));
  924. std::memcpy(stora_ptr, large_ptr, n_i_bytes);
  925. std::memcpy(large_ptr, short_ptr, n_i_bytes);
  926. std::memcpy(short_ptr, stora_ptr, n_i_bytes);
  927. boost::container::iterator_advance(large_range_f, n_i);
  928. boost::container::iterator_advance(short_range_f, n_i);
  929. boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw
  930. boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i);
  931. }
  932. template
  933. <std::size_t MaxTmpBytes
  934. ,typename Allocator
  935. ,typename F // F models ForwardIterator
  936. ,typename G // G models ForwardIterator
  937. >
  938. inline typename dtl::enable_if_c
  939. < dtl::is_memtransfer_copy_assignable<F, G>::value && true//(MaxTmpBytes > DeepSwapAllocNMaxStorage)
  940. , void>::type
  941. deep_swap_alloc_n( Allocator &a, F short_range_f, typename allocator_traits<Allocator>::size_type n_i
  942. , G large_range_f, typename allocator_traits<Allocator>::size_type n_j)
  943. {
  944. typedef typename allocator_traits<Allocator>::value_type value_type;
  945. typedef typename dtl::aligned_storage
  946. <DeepSwapAllocNMaxStorage, dtl::alignment_of<value_type>::value>::type storage_type;
  947. storage_type storage;
  948. const std::size_t sizeof_storage = sizeof(storage);
  949. std::size_t n_i_bytes = sizeof(value_type)*n_i;
  950. char *large_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f)));
  951. char *short_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f)));
  952. char *stora_ptr = static_cast<char*>(static_cast<void*>(storage.data));
  953. std::size_t szt_times = n_i_bytes/sizeof_storage;
  954. const std::size_t szt_rem = n_i_bytes%sizeof_storage;
  955. //Loop unrolling using Duff's device, as it seems it helps on some architectures
  956. const std::size_t Unroll = 4;
  957. std::size_t n = (szt_times + (Unroll-1))/Unroll;
  958. const std::size_t branch_number = (!szt_times)*Unroll + (szt_times % Unroll);
  959. switch(branch_number){
  960. case 4:
  961. break;
  962. case 0: do{
  963. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  964. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  965. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  966. large_ptr += sizeof_storage;
  967. short_ptr += sizeof_storage;
  968. BOOST_FALLTHROUGH;
  969. case 3:
  970. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  971. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  972. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  973. large_ptr += sizeof_storage;
  974. short_ptr += sizeof_storage;
  975. BOOST_FALLTHROUGH;
  976. case 2:
  977. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  978. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  979. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  980. large_ptr += sizeof_storage;
  981. short_ptr += sizeof_storage;
  982. BOOST_FALLTHROUGH;
  983. case 1:
  984. std::memcpy(stora_ptr, large_ptr, sizeof_storage);
  985. std::memcpy(large_ptr, short_ptr, sizeof_storage);
  986. std::memcpy(short_ptr, stora_ptr, sizeof_storage);
  987. large_ptr += sizeof_storage;
  988. short_ptr += sizeof_storage;
  989. } while(--n);
  990. }
  991. std::memcpy(stora_ptr, large_ptr, szt_rem);
  992. std::memcpy(large_ptr, short_ptr, szt_rem);
  993. std::memcpy(short_ptr, stora_ptr, szt_rem);
  994. boost::container::iterator_advance(large_range_f, n_i);
  995. boost::container::iterator_advance(short_range_f, n_i);
  996. boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw
  997. boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i);
  998. }
  999. //////////////////////////////////////////////////////////////////////////////
  1000. //
  1001. // copy_assign_range_alloc_n
  1002. //
  1003. //////////////////////////////////////////////////////////////////////////////
  1004. template
  1005. <typename Allocator
  1006. ,typename I // F models InputIterator
  1007. ,typename O // G models OutputIterator
  1008. >
  1009. void copy_assign_range_alloc_n( Allocator &a, I inp_start, typename allocator_traits<Allocator>::size_type n_i
  1010. , O out_start, typename allocator_traits<Allocator>::size_type n_o )
  1011. {
  1012. if (n_o < n_i){
  1013. inp_start = boost::container::copy_n_source_dest(inp_start, n_o, out_start); // may throw
  1014. boost::container::uninitialized_copy_alloc_n(a, inp_start, n_i - n_o, out_start);// may throw
  1015. }
  1016. else{
  1017. out_start = boost::container::copy_n(inp_start, n_i, out_start); // may throw
  1018. boost::container::destroy_alloc_n(a, out_start, n_o - n_i);
  1019. }
  1020. }
  1021. //////////////////////////////////////////////////////////////////////////////
  1022. //
  1023. // move_assign_range_alloc_n
  1024. //
  1025. //////////////////////////////////////////////////////////////////////////////
  1026. template
  1027. <typename Allocator
  1028. ,typename I // F models InputIterator
  1029. ,typename O // G models OutputIterator
  1030. >
  1031. void move_assign_range_alloc_n( Allocator &a, I inp_start, typename allocator_traits<Allocator>::size_type n_i
  1032. , O out_start, typename allocator_traits<Allocator>::size_type n_o )
  1033. {
  1034. if (n_o < n_i){
  1035. inp_start = boost::container::move_n_source_dest(inp_start, n_o, out_start); // may throw
  1036. boost::container::uninitialized_move_alloc_n(a, inp_start, n_i - n_o, out_start); // may throw
  1037. }
  1038. else{
  1039. out_start = boost::container::move_n(inp_start, n_i, out_start); // may throw
  1040. boost::container::destroy_alloc_n(a, out_start, n_o - n_i);
  1041. }
  1042. }
  1043. } //namespace container {
  1044. } //namespace boost {
  1045. //#pragma GCC diagnostic ignored "-Wclass-memaccess"
  1046. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  1047. #pragma GCC diagnostic pop
  1048. #endif
  1049. #endif //#ifndef BOOST_CONTAINER_DETAIL_COPY_MOVE_ALGO_HPP