OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
counting_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 
91 template <
92  typename ValueType,
93  typename OffsetT = ptrdiff_t>
95 {
96 public:
97 
98  // Required iterator traits
101  typedef ValueType value_type;
102  typedef ValueType* pointer;
103  typedef ValueType reference;
104 
105 #if (THRUST_VERSION >= 100700)
106  // Use Thrust's iterator categories so we can use these iterators in Thrust 1.7 (or newer) methods
107  typedef typename thrust::detail::iterator_facade_category<
108  thrust::any_system_tag,
109  thrust::random_access_traversal_tag,
110  value_type,
111  reference
112  >::type iterator_category;
113 #else
114  typedef std::random_access_iterator_tag iterator_category;
115 #endif // THRUST_VERSION
116 
117 private:
118 
119  ValueType val;
120 
121 public:
122 
124  __host__ __device__ __forceinline__ CountingInputIterator(
125  const ValueType &val)
126  :
127  val(val)
128  {}
129 
131  __host__ __device__ __forceinline__ self_type operator++(int)
132  {
133  self_type retval = *this;
134  val++;
135  return retval;
136  }
137 
139  __host__ __device__ __forceinline__ self_type operator++()
140  {
141  val++;
142  return *this;
143  }
144 
146  __host__ __device__ __forceinline__ reference operator*() const
147  {
148  return val;
149  }
150 
152  template <typename Distance>
153  __host__ __device__ __forceinline__ self_type operator+(Distance n) const
154  {
155  self_type retval(val + (ValueType) n);
156  return retval;
157  }
158 
160  template <typename Distance>
161  __host__ __device__ __forceinline__ self_type& operator+=(Distance n)
162  {
163  val += (ValueType) n;
164  return *this;
165  }
166 
168  template <typename Distance>
169  __host__ __device__ __forceinline__ self_type operator-(Distance n) const
170  {
171  self_type retval(val - (ValueType) n);
172  return retval;
173  }
174 
176  template <typename Distance>
177  __host__ __device__ __forceinline__ self_type& operator-=(Distance n)
178  {
179  val -= n;
180  return *this;
181  }
182 
184  __host__ __device__ __forceinline__ difference_type operator-(self_type other) const
185  {
186  return (difference_type) (val - other.val);
187  }
188 
190  template <typename Distance>
191  __host__ __device__ __forceinline__ reference operator[](Distance n) const
192  {
193  return val + (ValueType) n;
194  }
195 
197  __host__ __device__ __forceinline__ pointer operator->()
198  {
199  return &val;
200  }
201 
203  __host__ __device__ __forceinline__ bool operator==(const self_type& rhs)
204  {
205  return (val == rhs.val);
206  }
207 
209  __host__ __device__ __forceinline__ bool operator!=(const self_type& rhs)
210  {
211  return (val != rhs.val);
212  }
213 
215  friend std::ostream& operator<<(std::ostream& os, const self_type& itr)
216  {
217  os << "[" << itr.val << "]";
218  return os;
219  }
220 
221 };
222 
223 
224  // end group UtilIterator
226 
227 } // CUB namespace
228 CUB_NS_POSTFIX // Optional outer namespace(s)
A random-access input generator for dereferencing a sequence of incrementing integer values.
Optional outer namespace(s)
ValueType reference
The type of a reference to an element the iterator can point to.
friend std::ostream & operator<<(std::ostream &os, const self_type &itr)
ostream operator
__host__ __device__ __forceinline__ reference operator *() const
Indirection.
__host__ __device__ __forceinline__ self_type operator-(Distance n) const
Subtraction.
__host__ __device__ __forceinline__ pointer operator->()
Structure dereference.
std::random_access_iterator_tag iterator_category
The iterator category.
__host__ __device__ __forceinline__ reference operator[](Distance n) const
Array subscript.
__host__ __device__ __forceinline__ self_type & operator+=(Distance n)
Addition assignment.
OffsetT OffsetT
[in] Total number of input data items
__host__ __device__ __forceinline__ self_type operator++()
Prefix increment.
ValueType * pointer
The type of a pointer to an element the iterator can point to.
__host__ __device__ __forceinline__ self_type operator++(int)
Postfix increment.
__host__ __device__ __forceinline__ difference_type operator-(self_type other) const
Distance.
__host__ __device__ __forceinline__ bool operator!=(const self_type &rhs)
Not equal to.
CountingInputIterator self_type
My own type.
ValueType value_type
The type of the element the iterator can point to.
__host__ __device__ __forceinline__ CountingInputIterator(const ValueType &val)
Constructor.
__host__ __device__ __forceinline__ self_type operator+(Distance n) const
Addition.
__host__ __device__ __forceinline__ bool operator==(const self_type &rhs)
Equal to.
__host__ __device__ __forceinline__ self_type & operator-=(Distance n)
Subtraction assignment.
OffsetT difference_type
Type to express the result of subtracting one iterator from another.