OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / swap.h
1 /*
2  *  Copyright 2008-2013 NVIDIA Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*! \file swap.h
18  *  \brief Functions for swapping the value of elements
19  */
20
21 #pragma once
22
23 #include <thrust/detail/config.h>
24 #include <thrust/detail/execution_policy.h>
25
26 // empty Doxygen comment below so namespace thrust's documentation will be extracted
27
28 /*!
29  */
30 namespace thrust
31 {
32
33 /*! \addtogroup utility
34  *  \{
35  */
36
37 /*! \addtogroup swap
38  *  \{
39  */
40
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.
44  *  
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.
49  *
50  *  \tparam Assignable is a model of <a href="http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>.
51  *
52  *  The following code snippet demonstrates how to use \p swap to
53  *  swap the contents of two variables.
54  *
55  *  \code
56  *  #include <thrust/swap.h>
57  *  ...
58  *  int x = 1;
59  *  int y = 2;
60  *  thrust::swap(x,h);
61  *
62  *  // x == 2, y == 1
63  *  \endcode
64  */
65 template<typename Assignable1, typename Assignable2>
66 __host__ __device__ 
67 inline void swap(Assignable1 &a, Assignable2 &b);
68
69 /*! \} // swap
70  */
71
72 /*! \} // utility
73  */
74
75
76 /*! \addtogroup copying
77  *  \{
78  */
79
80
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>.
86  *
87  *  The algorithm's execution is parallelized as determined by \p exec.
88  *
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
94  *          sequence to swap.
95  *
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.
101  *
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.
103  *
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:
107  *
108  *  \code
109  *  #include <thrust/swap.h>
110  *  #include <thrust/device_vector.h>
111  *  #include <thrust/execution_policy.h>
112  *  ...
113  *  thrust::device_vector<int> v1(2), v2(2);
114  *  v1[0] = 1;
115  *  v1[1] = 2;
116  *  v2[0] = 3;
117  *  v2[1] = 4;
118  *
119  *  thrust::swap_ranges(thrust::device, v1.begin(), v1.end(), v2.begin());
120  *
121  *  // v1[0] == 3, v1[1] == 4, v2[0] == 1, v2[1] == 2
122  *  \endcode
123  *
124  *  \see http://www.sgi.com/tech/stl/swap_ranges.html
125  *  \see \c swap
126  */
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);
134
135
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>.
141  *
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
146  *          sequence to swap.
147  *
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.
152  *
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.
154  *
155  *  The following code snippet demonstrates how to use \p swap_ranges to
156  *  swap the contents of two \c thrust::device_vectors.
157  *
158  *  \code
159  *  #include <thrust/swap.h>
160  *  #include <thrust/device_vector.h>
161  *  ...
162  *  thrust::device_vector<int> v1(2), v2(2);
163  *  v1[0] = 1;
164  *  v1[1] = 2;
165  *  v2[0] = 3;
166  *  v2[1] = 4;
167  *
168  *  thrust::swap_ranges(v1.begin(), v1.end(), v2.begin());
169  *
170  *  // v1[0] == 3, v1[1] == 4, v2[0] == 1, v2[1] == 2
171  *  \endcode
172  *
173  *  \see http://www.sgi.com/tech/stl/swap_ranges.html
174  *  \see \c swap
175  */
176 template<typename ForwardIterator1,
177          typename ForwardIterator2>
178   ForwardIterator2 swap_ranges(ForwardIterator1 first1,
179                                ForwardIterator1 last1,
180                                ForwardIterator2 first2);
181
182
183 /*! \} // copying
184  */
185
186
187 } // end thrust
188
189 #include <thrust/detail/swap.inl>
190