OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
transform_input_iterator.cuh
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2011, Duane Merrill. All rights reserved.
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  ******************************************************************************/
28 
34 #pragma once
35 
36 #include <iterator>
37 #include <iostream>
38 
39 #include "../thread/thread_load.cuh"
40 #include "../thread/thread_store.cuh"
41 #include "../util_device.cuh"
42 #include "../util_namespace.cuh"
43 
44 #if (THRUST_VERSION >= 100700)
45  // This iterator is compatible with Thrust API 1.7 and newer
46  #include <thrust/iterator/iterator_facade.h>
47  #include <thrust/iterator/iterator_traits.h>
48 #endif // THRUST_VERSION
49 
50 
52 CUB_NS_PREFIX
53 
55 namespace cub {
56 
112 template <
113  typename ValueType,
114  typename ConversionOp,
115  typename InputIteratorT,
116  typename OffsetT = ptrdiff_t>
118 {
119 public:
120 
121  // Required iterator traits
124  typedef ValueType value_type;
125  typedef ValueType* pointer;
126  typedef ValueType reference;
127 
128 #if (THRUST_VERSION >= 100700)
129  // Use Thrust's iterator categories so we can use these iterators in Thrust 1.7 (or newer) methods
130  typedef typename thrust::detail::iterator_facade_category<
131  thrust::any_system_tag,
132  thrust::random_access_traversal_tag,
133  value_type,
134  reference
135  >::type iterator_category;
136 #else
137  typedef std::random_access_iterator_tag iterator_category;
138 #endif // THRUST_VERSION
139 
140 private:
141 
142  ConversionOp conversion_op;
143  InputIteratorT input_itr;
144 
145 public:
146 
148  __host__ __device__ __forceinline__ TransformInputIterator(
149  InputIteratorT input_itr,
150  ConversionOp conversion_op)
151  :
152  conversion_op(conversion_op),
153  input_itr(input_itr)
154  {}
155 
157  __host__ __device__ __forceinline__ self_type operator++(int)
158  {
159  self_type retval = *this;
160  input_itr++;
161  return retval;
162  }
163 
165  __host__ __device__ __forceinline__ self_type operator++()
166  {
167  input_itr++;
168  return *this;
169  }
170 
172  __host__ __device__ __forceinline__ reference operator*() const
173  {
174  return conversion_op(*input_itr);
175  }
176 
178  template <typename Distance>
179  __host__ __device__ __forceinline__ self_type operator+(Distance n) const
180  {
181  self_type retval(input_itr + n, conversion_op);
182  return retval;
183  }
184 
186  template <typename Distance>
187  __host__ __device__ __forceinline__ self_type& operator+=(Distance n)
188  {
189  input_itr += n;
190  return *this;
191  }
192 
194  template <typename Distance>
195  __host__ __device__ __forceinline__ self_type operator-(Distance n) const
196  {
197  self_type retval(input_itr - n, conversion_op);
198  return retval;
199  }
200 
202  template <typename Distance>
203  __host__ __device__ __forceinline__ self_type& operator-=(Distance n)
204  {
205  input_itr -= n;
206  return *this;
207  }
208 
210  __host__ __device__ __forceinline__ difference_type operator-(self_type other) const
211  {
212  return input_itr - other.input_itr;
213  }
214 
216  template <typename Distance>
217  __host__ __device__ __forceinline__ reference operator[](Distance n) const
218  {
219  return conversion_op(input_itr[n]);
220  }
221 
223  __host__ __device__ __forceinline__ pointer operator->()
224  {
225  return &conversion_op(*input_itr);
226  }
227 
229  __host__ __device__ __forceinline__ bool operator==(const self_type& rhs)
230  {
231  return (input_itr == rhs.input_itr);
232  }
233 
235  __host__ __device__ __forceinline__ bool operator!=(const self_type& rhs)
236  {
237  return (input_itr != rhs.input_itr);
238  }
239 
241  friend std::ostream& operator<<(std::ostream& os, const self_type& itr)
242  {
243  return os;
244  }
245 };
246 
247 
248  // end group UtilIterator
250 
251 } // CUB namespace
252 CUB_NS_POSTFIX // Optional outer namespace(s)
__host__ __device__ __forceinline__ bool operator!=(const self_type &rhs)
Not equal to.
__host__ __device__ __forceinline__ difference_type operator-(self_type other) const
Distance.
__host__ __device__ __forceinline__ reference operator *() const
Indirection.
Optional outer namespace(s)
ValueType * pointer
The type of a pointer to an element the iterator can point to.
__host__ __device__ __forceinline__ self_type & operator-=(Distance n)
Subtraction assignment.
A random-access input wrapper for transforming dereferenced values.
std::random_access_iterator_tag iterator_category
The iterator category.
__host__ __device__ __forceinline__ pointer operator->()
Structure dereference.
__host__ __device__ __forceinline__ self_type & operator+=(Distance n)
Addition assignment.
__host__ __device__ __forceinline__ self_type operator++()
Prefix increment.
OffsetT OffsetT
[in] Total number of input data items
__host__ __device__ __forceinline__ bool operator==(const self_type &rhs)
Equal to.
__host__ __device__ __forceinline__ TransformInputIterator(InputIteratorT input_itr, ConversionOp conversion_op)
Constructor.
ValueType reference
The type of a reference to an element the iterator can point to.
OffsetT difference_type
Type to express the result of subtracting one iterator from another.
__host__ __device__ __forceinline__ self_type operator+(Distance n) const
Addition.
__host__ __device__ __forceinline__ self_type operator++(int)
Postfix increment.
TransformInputIterator self_type
My own type.
__host__ __device__ __forceinline__ self_type operator-(Distance n) const
Subtraction.
ValueType value_type
The type of the element the iterator can point to.
__host__ __device__ __forceinline__ reference operator[](Distance n) const
Array subscript.
friend std::ostream & operator<<(std::ostream &os, const self_type &itr)
ostream operator