copy_move_algo.hpp 38 KB

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