Spaces:
Runtime error
Runtime error
File size: 7,915 Bytes
be11144 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
/*
* Copyright 2008-2013 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*! \file thrust/iterator/zip_iterator.h
* \brief An iterator which returns a tuple of the result of dereferencing
* a tuple of iterators when dereferenced
*/
/*
* Copyright David Abrahams and Thomas Becker 2000-2006.
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying NOTICE file for the complete license)
*
* For more information, see http://www.boost.org
*/
#pragma once
#include <thrust/detail/config.h>
#include <thrust/iterator/detail/zip_iterator_base.h>
#include <thrust/iterator/iterator_facade.h>
#include <thrust/detail/type_traits.h>
namespace thrust
{
/*! \addtogroup iterators
* \{
*/
/*! \addtogroup fancyiterator Fancy Iterators
* \ingroup iterators
* \{
*/
/*! \p zip_iterator is an iterator which represents a pointer into a range
* of \p tuples whose elements are themselves taken from a \p tuple of input
* iterators. This iterator is useful for creating a virtual array of structures
* while achieving the same performance and bandwidth as the structure of arrays
* idiom. \p zip_iterator also facilitates kernel fusion by providing a convenient
* means of amortizing the execution of the same operation over multiple ranges.
*
* The following code snippet demonstrates how to create a \p zip_iterator
* which represents the result of "zipping" multiple ranges together.
*
* \code
* #include <thrust/iterator/zip_iterator.h>
* #include <thrust/tuple.h>
* #include <thrust/device_vector.h>
* ...
* thrust::device_vector<int> int_v(3);
* int_v[0] = 0; int_v[1] = 1; int_v[2] = 2;
*
* thrust::device_vector<float> float_v(3);
* float_v[0] = 0.0f; float_v[1] = 1.0f; float_v[2] = 2.0f;
*
* thrust::device_vector<char> char_v(3);
* char_v[0] = 'a'; char_v[1] = 'b'; char_v[2] = 'c';
*
* // typedef these iterators for shorthand
* typedef thrust::device_vector<int>::iterator IntIterator;
* typedef thrust::device_vector<float>::iterator FloatIterator;
* typedef thrust::device_vector<char>::iterator CharIterator;
*
* // typedef a tuple of these iterators
* typedef thrust::tuple<IntIterator, FloatIterator, CharIterator> IteratorTuple;
*
* // typedef the zip_iterator of this tuple
* typedef thrust::zip_iterator<IteratorTuple> ZipIterator;
*
* // finally, create the zip_iterator
* ZipIterator iter(thrust::make_tuple(int_v.begin(), float_v.begin(), char_v.begin()));
*
* *iter; // returns (0, 0.0f, 'a')
* iter[0]; // returns (0, 0.0f, 'a')
* iter[1]; // returns (1, 1.0f, 'b')
* iter[2]; // returns (2, 2.0f, 'c')
*
* thrust::get<0>(iter[2]); // returns 2
* thrust::get<1>(iter[0]); // returns 0.0f
* thrust::get<2>(iter[1]); // returns 'b'
*
* // iter[3] is an out-of-bounds error
* \endcode
*
* Defining the type of a \p zip_iterator can be complex. The next code example demonstrates
* how to use the \p make_zip_iterator function with the \p make_tuple function to avoid
* explicitly specifying the type of the \p zip_iterator. This example shows how to use
* \p zip_iterator to copy multiple ranges with a single call to \p thrust::copy.
*
* \code
* #include <thrust/zip_iterator.h>
* #include <thrust/tuple.h>
* #include <thrust/device_vector.h>
*
* int main()
* {
* thrust::device_vector<int> int_in(3), int_out(3);
* int_in[0] = 0;
* int_in[1] = 1;
* int_in[2] = 2;
*
* thrust::device_vector<float> float_in(3), float_out(3);
* float_in[0] = 0.0f;
* float_in[1] = 10.0f;
* float_in[2] = 20.0f;
*
* thrust::copy(thrust::make_zip_iterator(thrust::make_tuple(int_in.begin(), float_in.begin())),
* thrust::make_zip_iterator(thrust::make_tuple(int_in.end(), float_in.end())),
* thrust::make_zip_iterator(thrust::make_tuple(int_out.begin(),float_out.begin())));
*
* // int_out is now [0, 1, 2]
* // float_out is now [0.0f, 10.0f, 20.0f]
*
* return 0;
* }
* \endcode
*
* \see make_zip_iterator
* \see make_tuple
* \see tuple
* \see get
*/
template <typename IteratorTuple>
class zip_iterator
: public detail::zip_iterator_base<IteratorTuple>::type
{
public:
/*! Null constructor does nothing.
*/
inline __host__ __device__
zip_iterator();
/*! This constructor creates a new \p zip_iterator from a
* \p tuple of iterators.
*
* \param iterator_tuple The \p tuple of iterators to copy from.
*/
inline __host__ __device__
zip_iterator(IteratorTuple iterator_tuple);
/*! This copy constructor creates a new \p zip_iterator from another
* \p zip_iterator.
*
* \param other The \p zip_iterator to copy.
*/
template<typename OtherIteratorTuple>
inline __host__ __device__
zip_iterator(const zip_iterator<OtherIteratorTuple> &other,
typename thrust::detail::enable_if_convertible<
OtherIteratorTuple,
IteratorTuple
>::type * = 0);
/*! This method returns a \c const reference to this \p zip_iterator's
* \p tuple of iterators.
*
* \return A \c const reference to this \p zip_iterator's \p tuple
* of iterators.
*/
inline __host__ __device__
const IteratorTuple &get_iterator_tuple() const;
/*! \cond
*/
private:
typedef typename
detail::zip_iterator_base<IteratorTuple>::type super_t;
friend class thrust::iterator_core_access;
// Dereferencing returns a tuple built from the dereferenced
// iterators in the iterator tuple.
__host__ __device__
typename super_t::reference dereference() const;
// Two zip_iterators are equal if the two first iterators of the
// tuple are equal. Note this differs from Boost's implementation, which
// considers the entire tuple.
template<typename OtherIteratorTuple>
inline __host__ __device__
bool equal(const zip_iterator<OtherIteratorTuple> &other) const;
// Advancing a zip_iterator means to advance all iterators in the tuple
inline __host__ __device__
void advance(typename super_t::difference_type n);
// Incrementing a zip iterator means to increment all iterators in the tuple
inline __host__ __device__
void increment();
// Decrementing a zip iterator means to decrement all iterators in the tuple
inline __host__ __device__
void decrement();
// Distance is calculated using the first iterator in the tuple.
template<typename OtherIteratorTuple>
inline __host__ __device__
typename super_t::difference_type
distance_to(const zip_iterator<OtherIteratorTuple> &other) const;
// The iterator tuple.
IteratorTuple m_iterator_tuple;
/*! \endcond
*/
}; // end zip_iterator
/*! \p make_zip_iterator creates a \p zip_iterator from a \p tuple
* of iterators.
*
* \param t The \p tuple of iterators to copy.
* \return A newly created \p zip_iterator which zips the iterators encapsulated in \p t.
*
* \see zip_iterator
*/
template<typename IteratorTuple>
inline __host__ __device__
zip_iterator<IteratorTuple> make_zip_iterator(IteratorTuple t);
/*! \} // end fancyiterators
*/
/*! \} // end iterators
*/
} // end thrust
#include <thrust/iterator/detail/zip_iterator.inl>
|