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