Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit ef58122

Browse files
committed
Merge pull request #352 from jaredhoberock/documentation
Documentation
2 parents 39c57cb + 6037722 commit ef58122

File tree

2 files changed

+115
-28
lines changed

2 files changed

+115
-28
lines changed

thrust/functional.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,22 +962,116 @@ template<typename BinaryPredicate>
962962
/*! \}
963963
*/
964964

965+
966+
/*! \addtogroup placeholder_objects Placeholder Objects
967+
* \ingroup function_objects
968+
* \{
969+
*/
970+
971+
972+
/*! \namespace placeholders
973+
* \brief Facilities for constructing simple functions inline.
974+
*
975+
* Objects in the \p thrust::placeholders namespace may be used to create simple arithmetic functions inline
976+
* in an algorithm invocation. Combining placeholders such as \p _1 and \p _2 with arithmetic operations such as \c +
977+
* creates an unnamed function object which applies the operation to their arguments.
978+
*
979+
* The type of placeholder objects is implementation-defined.
980+
*
981+
* The following code snippet demonstrates how to use the placeholders \p _1 and \p _2 with \p thrust::transform
982+
* to implement the SAXPY computation:
983+
*
984+
* \code
985+
* #include <thrust/device_vector.h>
986+
* #include <thrust/transform.h>
987+
* #include <thrust/functional.h>
988+
*
989+
* int main()
990+
* {
991+
* thrust::device_vector<float> x(4), y(4);
992+
* x[0] = 1;
993+
* x[1] = 2;
994+
* x[2] = 3;
995+
* x[3] = 4;
996+
*
997+
* y[0] = 1;
998+
* y[1] = 1;
999+
* y[2] = 1;
1000+
* y[3] = 1;
1001+
*
1002+
* float a = 2.0f;
1003+
*
1004+
* using namespace thrust::placeholders;
1005+
*
1006+
* thrust::transform(x.begin(), x.end(), y.begin(), y.begin(),
1007+
* a * _1 + 2
1008+
* );
1009+
*
1010+
* // y is now {3, 5, 7, 9}
1011+
* }
1012+
* \endcode
1013+
*/
9651014
namespace placeholders
9661015
{
9671016

1017+
1018+
/*! \p thrust::placeholders::_1 is the placeholder for the first function parameter.
1019+
*/
9681020
static const thrust::detail::functional::placeholder<0>::type _1;
1021+
1022+
1023+
/*! \p thrust::placeholders::_2 is the placeholder for the second function parameter.
1024+
*/
9691025
static const thrust::detail::functional::placeholder<1>::type _2;
1026+
1027+
1028+
/*! \p thrust::placeholders::_3 is the placeholder for the third function parameter.
1029+
*/
9701030
static const thrust::detail::functional::placeholder<2>::type _3;
1031+
1032+
1033+
/*! \p thrust::placeholders::_4 is the placeholder for the fourth function parameter.
1034+
*/
9711035
static const thrust::detail::functional::placeholder<3>::type _4;
1036+
1037+
1038+
/*! \p thrust::placeholders::_5 is the placeholder for the fifth function parameter.
1039+
*/
9721040
static const thrust::detail::functional::placeholder<4>::type _5;
1041+
1042+
1043+
/*! \p thrust::placeholders::_6 is the placeholder for the sixth function parameter.
1044+
*/
9731045
static const thrust::detail::functional::placeholder<5>::type _6;
1046+
1047+
1048+
/*! \p thrust::placeholders::_7 is the placeholder for the seventh function parameter.
1049+
*/
9741050
static const thrust::detail::functional::placeholder<6>::type _7;
1051+
1052+
1053+
/*! \p thrust::placeholders::_8 is the placeholder for the eighth function parameter.
1054+
*/
9751055
static const thrust::detail::functional::placeholder<7>::type _8;
1056+
1057+
1058+
/*! \p thrust::placeholders::_9 is the placeholder for the ninth function parameter.
1059+
*/
9761060
static const thrust::detail::functional::placeholder<8>::type _9;
1061+
1062+
1063+
/*! \p thrust::placeholders::_10 is the placeholder for the tenth function parameter.
1064+
*/
9771065
static const thrust::detail::functional::placeholder<9>::type _10;
9781066

1067+
9791068
} // end placeholders
9801069

1070+
1071+
/*! \} // placeholder_objects
1072+
*/
1073+
1074+
9811075
} // end thrust
9821076

9831077
#include <thrust/detail/functional.inl>

thrust/iterator/iterator_adaptor.h

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,57 +55,46 @@ namespace thrust
5555
* functionality, it is occasionally more straightforward to derive from \p iterator_adaptor directly.
5656
*
5757
* 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:
6059
*
6160
* \code
6261
* #include <thrust/iterator/iterator_adaptor.h>
6362
*
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
6766
* : 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
7470
* >
7571
* {
7672
* public:
7773
* // shorthand for the name of the iterator_adaptor we're deriving from
7874
* 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
8577
* > super_t;
8678
*
8779
* __host__ __device__
88-
* my_transform_iterator(const Iterator &x, Function f) : super_t(x), m_f(f) {}
89-
*
90-
* // other assorted constructors might go here
91-
* ...
80+
* repeat_iterator(const Iterator &x, int n) : super_t(x), begin(x), n(n) {}
9281
*
9382
* // befriend thrust::iterator_core_access to allow it access to the private interface below
9483
* friend class thrust::iterator_core_access;
9584
*
9685
* 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+
*
9892
* // it is private because only thrust::iterator_core_access needs access to it
9993
* __host__ __device__
10094
* typename super_t::reference dereference() const
10195
* {
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);
10597
* }
106-
*
107-
* // the unary function
108-
* Function m_f;
10998
* };
11099
* \endcode
111100
*
@@ -116,6 +105,10 @@ namespace thrust
116105
* The exception is Thrust's addition of the template parameter \p System, which is necessary to allow Thrust
117106
* to dispatch an algorithm to one of several parallel backend systems.
118107
*
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+
*
119112
* Interested users may refer to <tt>boost::iterator_adaptor</tt>'s documentation for further usage examples.
120113
*/
121114
template<typename Derived,

0 commit comments

Comments
 (0)