2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * \brief Functions for swapping the value of elements
23 #include <thrust/detail/config.h>
24 #include <thrust/detail/execution_policy.h>
26 // empty Doxygen comment below so namespace thrust's documentation will be extracted
33 /*! \addtogroup utility
41 /*! \p swap assigns the contents of \c a to \c b and the
42 * contents of \c b to \c a. This is used as a primitive operation
43 * by many other algorithms.
45 * \param a The first value of interest. After completion,
46 * the value of b will be returned here.
47 * \param b The second value of interest. After completion,
48 * the value of a will be returned here.
50 * \tparam Assignable is a model of <a href="http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>.
52 * The following code snippet demonstrates how to use \p swap to
53 * swap the contents of two variables.
56 * #include <thrust/swap.h>
65 template<typename Assignable1, typename Assignable2>
67 inline void swap(Assignable1 &a, Assignable2 &b);
76 /*! \addtogroup copying
81 /*! \p swap_ranges swaps each of the elements in the range <tt>[first1, last1)</tt>
82 * with the corresponding element in the range <tt>[first2, first2 + (last1 - first1))</tt>.
83 * That is, for each integer \c n such that <tt>0 <= n < (last1 - first1)</tt>, it swaps
84 * <tt>*(first1 + n)</tt> and <tt>*(first2 + n)</tt>. The return value is
85 * <tt>first2 + (last1 - first1)</tt>.
87 * The algorithm's execution is parallelized as determined by \p exec.
89 * \param exec The execution policy to use for parallelization.
90 * \param first1 The beginning of the first sequence to swap.
91 * \param last1 One position past the last element of the first sequence to swap.
92 * \param first2 The beginning of the second sequence to swap.
93 * \return An iterator pointing to one position past the last element of the second
96 * \tparam DerivedPolicy The name of the derived execution policy.
97 * \tparam ForwardIterator1 is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
98 * and \p ForwardIterator1's \c value_type must be convertible to \p ForwardIterator2's \c value_type.
99 * \tparam ForwardIterator2 is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
100 * and \p ForwardIterator2's \c value_type must be convertible to \p ForwardIterator1's \c value_type.
102 * \pre \p first1 may equal \p first2, but the range <tt>[first1, last1)</tt> shall not overlap the range <tt>[first2, first2 + (last1 - first1))</tt> otherwise.
104 * The following code snippet demonstrates how to use \p swap_ranges to
105 * swap the contents of two \c thrust::device_vectors using the \p thrust::device execution
106 * policy for parallelization:
109 * #include <thrust/swap.h>
110 * #include <thrust/device_vector.h>
111 * #include <thrust/execution_policy.h>
113 * thrust::device_vector<int> v1(2), v2(2);
119 * thrust::swap_ranges(thrust::device, v1.begin(), v1.end(), v2.begin());
121 * // v1[0] == 3, v1[1] == 4, v2[0] == 1, v2[1] == 2
124 * \see http://www.sgi.com/tech/stl/swap_ranges.html
127 template<typename DerivedPolicy,
128 typename ForwardIterator1,
129 typename ForwardIterator2>
130 ForwardIterator2 swap_ranges(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
131 ForwardIterator1 first1,
132 ForwardIterator1 last1,
133 ForwardIterator2 first2);
136 /*! \p swap_ranges swaps each of the elements in the range <tt>[first1, last1)</tt>
137 * with the corresponding element in the range <tt>[first2, first2 + (last1 - first1))</tt>.
138 * That is, for each integer \c n such that <tt>0 <= n < (last1 - first1)</tt>, it swaps
139 * <tt>*(first1 + n)</tt> and <tt>*(first2 + n)</tt>. The return value is
140 * <tt>first2 + (last1 - first1)</tt>.
142 * \param first1 The beginning of the first sequence to swap.
143 * \param last1 One position past the last element of the first sequence to swap.
144 * \param first2 The beginning of the second sequence to swap.
145 * \return An iterator pointing to one position past the last element of the second
148 * \tparam ForwardIterator1 is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
149 * and \p ForwardIterator1's \c value_type must be convertible to \p ForwardIterator2's \c value_type.
150 * \tparam ForwardIterator2 is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
151 * and \p ForwardIterator2's \c value_type must be convertible to \p ForwardIterator1's \c value_type.
153 * \pre \p first1 may equal \p first2, but the range <tt>[first1, last1)</tt> shall not overlap the range <tt>[first2, first2 + (last1 - first1))</tt> otherwise.
155 * The following code snippet demonstrates how to use \p swap_ranges to
156 * swap the contents of two \c thrust::device_vectors.
159 * #include <thrust/swap.h>
160 * #include <thrust/device_vector.h>
162 * thrust::device_vector<int> v1(2), v2(2);
168 * thrust::swap_ranges(v1.begin(), v1.end(), v2.begin());
170 * // v1[0] == 3, v1[1] == 4, v2[0] == 1, v2[1] == 2
173 * \see http://www.sgi.com/tech/stl/swap_ranges.html
176 template<typename ForwardIterator1,
177 typename ForwardIterator2>
178 ForwardIterator2 swap_ranges(ForwardIterator1 first1,
179 ForwardIterator1 last1,
180 ForwardIterator2 first2);
189 #include <thrust/detail/swap.inl>