OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
arg_index_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 #include <thrust/version.h>
45 
46 #if (THRUST_VERSION >= 100700)
47  // This iterator is compatible with Thrust API 1.7 and newer
48  #include <thrust/iterator/iterator_facade.h>
49  #include <thrust/iterator/iterator_traits.h>
50 #endif // THRUST_VERSION
51 
53 CUB_NS_PREFIX
54 
56 namespace cub {
57 
109 template <
110  typename InputIteratorT,
111  typename OffsetT = ptrdiff_t,
112  typename OutputValueT = typename std::iterator_traits<InputIteratorT>::value_type>
114 {
115 public:
116 
117  // Required iterator traits
121  typedef value_type* pointer;
123 
124 #if (THRUST_VERSION >= 100700)
125  // Use Thrust's iterator categories so we can use these iterators in Thrust 1.7 (or newer) methods
126  typedef typename thrust::detail::iterator_facade_category<
127  thrust::any_system_tag,
128  thrust::random_access_traversal_tag,
129  value_type,
130  reference
131  >::type iterator_category;
132 #else
133  typedef std::random_access_iterator_tag iterator_category;
134 #endif // THRUST_VERSION
135 
136 private:
137 
138  InputIteratorT itr;
139  difference_type offset;
140 
141 public:
142 
144  __host__ __device__ __forceinline__ ArgIndexInputIterator(
145  InputIteratorT itr,
146  difference_type offset = 0)
147  :
148  itr(itr),
149  offset(offset)
150  {}
151 
153  __host__ __device__ __forceinline__ self_type operator++(int)
154  {
155  self_type retval = *this;
156  offset++;
157  return retval;
158  }
159 
161  __host__ __device__ __forceinline__ self_type operator++()
162  {
163  offset++;
164  return *this;
165  }
166 
168  __host__ __device__ __forceinline__ reference operator*() const
169  {
170  value_type retval;
171  retval.value = itr[offset];
172  retval.key = offset;
173  return retval;
174  }
175 
177  template <typename Distance>
178  __host__ __device__ __forceinline__ self_type operator+(Distance n) const
179  {
180  self_type retval(itr, offset + n);
181  return retval;
182  }
183 
185  template <typename Distance>
186  __host__ __device__ __forceinline__ self_type& operator+=(Distance n)
187  {
188  offset += n;
189  return *this;
190  }
191 
193  template <typename Distance>
194  __host__ __device__ __forceinline__ self_type operator-(Distance n) const
195  {
196  self_type retval(itr, offset - n);
197  return retval;
198  }
199 
201  template <typename Distance>
202  __host__ __device__ __forceinline__ self_type& operator-=(Distance n)
203  {
204  offset -= n;
205  return *this;
206  }
207 
209  __host__ __device__ __forceinline__ difference_type operator-(self_type other) const
210  {
211  return offset - other.offset;
212  }
213 
215  template <typename Distance>
216  __host__ __device__ __forceinline__ reference operator[](Distance n) const
217  {
218  self_type offset = (*this) + n;
219  return *offset;
220  }
221 
223  __host__ __device__ __forceinline__ pointer operator->()
224  {
225  return &(*(*this));
226  }
227 
229  __host__ __device__ __forceinline__ bool operator==(const self_type& rhs)
230  {
231  return ((itr == rhs.itr) && (offset == rhs.offset));
232  }
233 
235  __host__ __device__ __forceinline__ bool operator!=(const self_type& rhs)
236  {
237  return ((itr != rhs.itr) || (offset != rhs.offset));
238  }
239 
241  __host__ __device__ __forceinline__ void normalize()
242  {
243  itr += offset;
244  offset = 0;
245  }
246 
248  friend std::ostream& operator<<(std::ostream& os, const self_type& /*itr*/)
249  {
250  return os;
251  }
252 };
253 
254 
255  // end group UtilIterator
257 
258 } // CUB namespace
259 CUB_NS_POSTFIX // Optional outer namespace(s)
__host__ __device__ __forceinline__ self_type operator++(int)
Postfix increment.
Key key
Item key.
Definition: util_type.cuh:671
value_type * pointer
The type of a pointer to an element the iterator can point to.
Value value
Item value.
Definition: util_type.cuh:672
Optional outer namespace(s)
__host__ __device__ __forceinline__ self_type operator+(Distance n) const
Addition.
__host__ __device__ __forceinline__ reference operator *() const
Indirection.
__host__ __device__ __forceinline__ bool operator!=(const self_type &rhs)
Not equal to.
__host__ __device__ __forceinline__ self_type operator++()
Prefix increment.
__host__ __device__ __forceinline__ bool operator==(const self_type &rhs)
Equal to.
A random-access input wrapper for pairing dereferenced values with their corresponding indices (formi...
A key identifier paired with a corresponding value.
Definition: util_type.cuh:666
__host__ __device__ __forceinline__ void normalize()
Normalize.
__host__ __device__ __forceinline__ self_type & operator+=(Distance n)
Addition assignment.
OffsetT OffsetT
[in] Total number of input data items
friend std::ostream & operator<<(std::ostream &os, const self_type &)
ostream operator
value_type reference
The type of a reference to an element the iterator can point to.
KeyValuePair< difference_type, OutputValueT > value_type
The type of the element the iterator can point to.
__host__ __device__ __forceinline__ difference_type operator-(self_type other) const
Distance.
__host__ __device__ __forceinline__ reference operator[](Distance n) const
Array subscript.
__host__ __device__ __forceinline__ self_type operator-(Distance n) const
Subtraction.
__host__ __device__ __forceinline__ pointer operator->()
Structure dereference.
OffsetT difference_type
Type to express the result of subtracting one iterator from another.
ArgIndexInputIterator self_type
My own type.
__host__ __device__ __forceinline__ ArgIndexInputIterator(InputIteratorT itr, difference_type offset=0)
Constructor.
__host__ __device__ __forceinline__ self_type & operator-=(Distance n)
Subtraction assignment.
std::random_access_iterator_tag iterator_category
The iterator category.