algorithm.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //! \file
  12. #ifndef BOOST_MOVE_ALGORITHM_HPP
  13. #define BOOST_MOVE_ALGORITHM_HPP
  14. #include <boost/move/detail/config_begin.hpp>
  15. #include <boost/move/utility.hpp>
  16. #include <boost/move/iterator.hpp>
  17. #include <boost/detail/no_exceptions_support.hpp>
  18. #include <algorithm> //copy, copy_backward
  19. #include <memory> //uninitialized_copy
  20. namespace boost {
  21. //////////////////////////////////////////////////////////////////////////////
  22. //
  23. // move
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26. #if !defined(BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
  27. //! <b>Effects</b>: Moves elements in the range [first,last) into the range [result,result + (last -
  28. //! first)) starting from first and proceeding to last. For each non-negative integer n < (last-first),
  29. //! performs *(result + n) = ::boost::move (*(first + n)).
  30. //!
  31. //! <b>Effects</b>: result + (last - first).
  32. //!
  33. //! <b>Requires</b>: result shall not be in the range [first,last).
  34. //!
  35. //! <b>Complexity</b>: Exactly last - first move assignments.
  36. template <typename I, // I models InputIterator
  37. typename O> // O models OutputIterator
  38. O move(I f, I l, O result)
  39. {
  40. while (f != l) {
  41. *result = ::boost::move(*f);
  42. ++f; ++result;
  43. }
  44. return result;
  45. }
  46. //////////////////////////////////////////////////////////////////////////////
  47. //
  48. // move_backward
  49. //
  50. //////////////////////////////////////////////////////////////////////////////
  51. //! <b>Effects</b>: Moves elements in the range [first,last) into the range
  52. //! [result - (last-first),result) starting from last - 1 and proceeding to
  53. //! first. For each positive integer n <= (last - first),
  54. //! performs *(result - n) = ::boost::move(*(last - n)).
  55. //!
  56. //! <b>Requires</b>: result shall not be in the range [first,last).
  57. //!
  58. //! <b>Returns</b>: result - (last - first).
  59. //!
  60. //! <b>Complexity</b>: Exactly last - first assignments.
  61. template <typename I, // I models BidirectionalIterator
  62. typename O> // O models BidirectionalIterator
  63. O move_backward(I f, I l, O result)
  64. {
  65. while (f != l) {
  66. --l; --result;
  67. *result = ::boost::move(*l);
  68. }
  69. return result;
  70. }
  71. #else
  72. using ::std::move_backward;
  73. #endif //!defined(BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
  74. //////////////////////////////////////////////////////////////////////////////
  75. //
  76. // uninitialized_move
  77. //
  78. //////////////////////////////////////////////////////////////////////////////
  79. //! <b>Effects</b>:
  80. //! \code
  81. //! for (; first != last; ++result, ++first)
  82. //! new (static_cast<void*>(&*result))
  83. //! typename iterator_traits<ForwardIterator>::value_type(boost::move(*first));
  84. //! \endcode
  85. //!
  86. //! <b>Returns</b>: result
  87. template
  88. <typename I, // I models InputIterator
  89. typename F> // F models ForwardIterator
  90. F uninitialized_move(I f, I l, F r
  91. /// @cond
  92. // ,typename ::boost::move_detail::enable_if<has_move_emulation_enabled<typename std::iterator_traits<I>::value_type> >::type* = 0
  93. /// @endcond
  94. )
  95. {
  96. typedef typename std::iterator_traits<I>::value_type input_value_type;
  97. F back = r;
  98. BOOST_TRY{
  99. while (f != l) {
  100. void * const addr = static_cast<void*>(::boost::move_detail::addressof(*r));
  101. ::new(addr) input_value_type(::boost::move(*f));
  102. ++f; ++r;
  103. }
  104. }
  105. BOOST_CATCH(...){
  106. for (; back != r; ++back){
  107. back->~input_value_type();
  108. }
  109. BOOST_RETHROW;
  110. }
  111. BOOST_CATCH_END
  112. return r;
  113. }
  114. /// @cond
  115. /*
  116. template
  117. <typename I, // I models InputIterator
  118. typename F> // F models ForwardIterator
  119. F uninitialized_move(I f, I l, F r,
  120. typename ::boost::move_detail::disable_if<has_move_emulation_enabled<typename std::iterator_traits<I>::value_type> >::type* = 0)
  121. {
  122. return std::uninitialized_copy(f, l, r);
  123. }
  124. */
  125. //////////////////////////////////////////////////////////////////////////////
  126. //
  127. // uninitialized_copy_or_move
  128. //
  129. //////////////////////////////////////////////////////////////////////////////
  130. namespace move_detail {
  131. template
  132. <typename I, // I models InputIterator
  133. typename F> // F models ForwardIterator
  134. inline F uninitialized_move_move_iterator(I f, I l, F r
  135. // ,typename ::boost::move_detail::enable_if< has_move_emulation_enabled<typename I::value_type> >::type* = 0
  136. )
  137. {
  138. return ::boost::uninitialized_move(f, l, r);
  139. }
  140. /*
  141. template
  142. <typename I, // I models InputIterator
  143. typename F> // F models ForwardIterator
  144. F uninitialized_move_move_iterator(I f, I l, F r,
  145. typename ::boost::move_detail::disable_if< has_move_emulation_enabled<typename I::value_type> >::type* = 0)
  146. {
  147. return std::uninitialized_copy(f.base(), l.base(), r);
  148. }
  149. */
  150. } //namespace move_detail {
  151. template
  152. <typename I, // I models InputIterator
  153. typename F> // F models ForwardIterator
  154. inline F uninitialized_copy_or_move(I f, I l, F r,
  155. typename ::boost::move_detail::enable_if< move_detail::is_move_iterator<I> >::type* = 0)
  156. {
  157. return ::boost::move_detail::uninitialized_move_move_iterator(f, l, r);
  158. }
  159. //////////////////////////////////////////////////////////////////////////////
  160. //
  161. // copy_or_move
  162. //
  163. //////////////////////////////////////////////////////////////////////////////
  164. namespace move_detail {
  165. template
  166. <typename I, // I models InputIterator
  167. typename F> // F models ForwardIterator
  168. inline F move_move_iterator(I f, I l, F r
  169. // ,typename ::boost::move_detail::enable_if< has_move_emulation_enabled<typename I::value_type> >::type* = 0
  170. )
  171. {
  172. return ::boost::move(f, l, r);
  173. }
  174. /*
  175. template
  176. <typename I, // I models InputIterator
  177. typename F> // F models ForwardIterator
  178. F move_move_iterator(I f, I l, F r,
  179. typename ::boost::move_detail::disable_if< has_move_emulation_enabled<typename I::value_type> >::type* = 0)
  180. {
  181. return std::copy(f.base(), l.base(), r);
  182. }
  183. */
  184. } //namespace move_detail {
  185. template
  186. <typename I, // I models InputIterator
  187. typename F> // F models ForwardIterator
  188. inline F copy_or_move(I f, I l, F r,
  189. typename ::boost::move_detail::enable_if< move_detail::is_move_iterator<I> >::type* = 0)
  190. {
  191. return ::boost::move_detail::move_move_iterator(f, l, r);
  192. }
  193. /// @endcond
  194. //! <b>Effects</b>:
  195. //! \code
  196. //! for (; first != last; ++result, ++first)
  197. //! new (static_cast<void*>(&*result))
  198. //! typename iterator_traits<ForwardIterator>::value_type(*first);
  199. //! \endcode
  200. //!
  201. //! <b>Returns</b>: result
  202. //!
  203. //! <b>Note</b>: This function is provided because
  204. //! <i>std::uninitialized_copy</i> from some STL implementations
  205. //! is not compatible with <i>move_iterator</i>
  206. template
  207. <typename I, // I models InputIterator
  208. typename F> // F models ForwardIterator
  209. inline F uninitialized_copy_or_move(I f, I l, F r
  210. /// @cond
  211. ,typename ::boost::move_detail::disable_if< move_detail::is_move_iterator<I> >::type* = 0
  212. /// @endcond
  213. )
  214. {
  215. return std::uninitialized_copy(f, l, r);
  216. }
  217. //! <b>Effects</b>:
  218. //! \code
  219. //! for (; first != last; ++result, ++first)
  220. //! *result = *first;
  221. //! \endcode
  222. //!
  223. //! <b>Returns</b>: result
  224. //!
  225. //! <b>Note</b>: This function is provided because
  226. //! <i>std::uninitialized_copy</i> from some STL implementations
  227. //! is not compatible with <i>move_iterator</i>
  228. template
  229. <typename I, // I models InputIterator
  230. typename F> // F models ForwardIterator
  231. inline F copy_or_move(I f, I l, F r
  232. /// @cond
  233. ,typename ::boost::move_detail::disable_if< move_detail::is_move_iterator<I> >::type* = 0
  234. /// @endcond
  235. )
  236. {
  237. return std::copy(f, l, r);
  238. }
  239. } //namespace boost {
  240. #include <boost/move/detail/config_end.hpp>
  241. #endif //#ifndef BOOST_MOVE_MOVE_HPP