copy_move_algo.hpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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