You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 21, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: thrust/iterator/iterator_adaptor.h
+21-28Lines changed: 21 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -55,57 +55,46 @@ namespace thrust
55
55
* functionality, it is occasionally more straightforward to derive from \p iterator_adaptor directly.
56
56
*
57
57
* To see how to use \p iterator_adaptor to create a novel iterator type, let's examine how to use it to
58
-
* define our own version of \p transform_iterator, a fancy iterator which fuses the application of
59
-
* a unary function with an iterator dereference:
58
+
* define \p repeat_iterator, a fancy iterator which repeats elements from another range a given number of time:
60
59
*
61
60
* \code
62
61
* #include <thrust/iterator/iterator_adaptor.h>
63
62
*
64
-
* // derive my_transform_iterator from iterator_adaptor
65
-
* template<typename Function, typename Iterator>
66
-
* class my_transform_iterator
63
+
* // derive repeat_iterator from iterator_adaptor
64
+
* template<typename Iterator>
65
+
* class repeat_iterator
67
66
* : public thrust::iterator_adaptor<
68
-
* my_transform_iterator<Function, Iterator>, // the first template parameter is the name of the type we're creating
69
-
* Iterator, // the second template parameter is the name of the iterator we're adapting
70
-
* typename Function::result_type, // the third template parameter is the name of the iterator's value_type -- it's simply the function's result_type.
71
-
* thrust::use_default, // the fourth template parameter is the name of the iterator's system. use_default will use the same system as the base iterator.
72
-
* thrust::use_default, // the fifth template parameter is the name of the iterator's traversal. use_default will use the same traversal as the base iterator.
73
-
* typename Function::result_type // the sixth template parameter is the name of the iterator's reference. Like value_type, it's simply the function's result_type.
67
+
* repeat_iterator<Iterator>, // the first template parameter is the name of the iterator we're creating
68
+
* Iterator // the second template parameter is the name of the iterator we're adapting
69
+
* // we can use the default for the additional template parameters
74
70
* >
75
71
* {
76
72
* public:
77
73
* // shorthand for the name of the iterator_adaptor we're deriving from
78
74
* typedef thrust::iterator_adaptor<
79
-
* my_transform_iterator<Function, Iterator>,
80
-
* Iterator,
81
-
* typename Function::result_type,
82
-
* thrust::use_default,
83
-
* thrust::use_default,
84
-
* typename Function::result_type
75
+
* repeat_iterator<Iterator>,
76
+
* Iterator
85
77
* > super_t;
86
78
*
87
79
* __host__ __device__
88
-
* my_transform_iterator(const Iterator &x, Function f) : super_t(x), m_f(f) {}
* // befriend thrust::iterator_core_access to allow it access to the private interface below
94
83
* friend class thrust::iterator_core_access;
95
84
*
96
85
* private:
97
-
* // here we define what it means to dereference my_transform_iterator
86
+
* // repeat each element of the adapted range n times
87
+
* unsigned int n;
88
+
*
89
+
* // used to keep track of where we began
90
+
* const Iterator begin;
91
+
*
98
92
* // it is private because only thrust::iterator_core_access needs access to it
99
93
* __host__ __device__
100
94
* typename super_t::reference dereference() const
101
95
* {
102
-
* // when my_transform_iterator is dereferenced, it dereferences the base iterator
103
-
* // and applies the unary function
104
-
* return m_f(*this->base());
96
+
* return *(begin + (this->base() - begin) / n);
105
97
* }
106
-
*
107
-
* // the unary function
108
-
* Function m_f;
109
98
* };
110
99
* \endcode
111
100
*
@@ -116,6 +105,10 @@ namespace thrust
116
105
* The exception is Thrust's addition of the template parameter \p System, which is necessary to allow Thrust
117
106
* to dispatch an algorithm to one of several parallel backend systems.
118
107
*
108
+
* \p iterator_adaptor is a powerful tool for creating custom iterators directly. However, the large set of iterator semantics which must be satisfied
109
+
* for algorithm compatibility can make \p iterator_adaptor difficult to use correctly. Unless you require the full expressivity of \p iterator_adaptor,
110
+
* consider building a custom iterator through composition of existing higher-level fancy iterators instead.
111
+
*
119
112
* Interested users may refer to <tt>boost::iterator_adaptor</tt>'s documentation for further usage examples.
0 commit comments