rocPRIM
constant_iterator.hpp
1 // Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef ROCPRIM_ITERATOR_CONSTANT_ITERATOR_HPP_
22 #define ROCPRIM_ITERATOR_CONSTANT_ITERATOR_HPP_
23 
24 #include <iterator>
25 #include <iostream>
26 #include <cstddef>
27 #include <type_traits>
28 
29 #include "../config.hpp"
30 
33 
34 BEGIN_ROCPRIM_NAMESPACE
35 
47 template<
48  class ValueType,
49  class Difference = std::ptrdiff_t
50 >
52 {
53 public:
55  using value_type = typename std::remove_const<ValueType>::type;
59  using reference = value_type; // constant_iterator is not writable
62  using pointer = const value_type*; // constant_iterator is not writable
64  using difference_type = Difference;
66  using iterator_category = std::random_access_iterator_tag;
67 
68 #ifndef DOXYGEN_SHOULD_SKIP_THIS
70 #endif
71 
76  ROCPRIM_HOST_DEVICE inline
77  explicit constant_iterator(const value_type value, const size_t index = 0)
78  : value_(value), index_(index)
79  {
80  }
81 
82  ROCPRIM_HOST_DEVICE inline
83  ~constant_iterator() = default;
84 
85  #ifndef DOXYGEN_SHOULD_SKIP_THIS
86  ROCPRIM_HOST_DEVICE inline
87  value_type operator*() const
88  {
89  return value_;
90  }
91 
92  ROCPRIM_HOST_DEVICE inline
93  pointer operator->() const
94  {
95  return &value_;
96  }
97 
98  ROCPRIM_HOST_DEVICE inline
99  constant_iterator& operator++()
100  {
101  index_++;
102  return *this;
103  }
104 
105  ROCPRIM_HOST_DEVICE inline
106  constant_iterator operator++(int)
107  {
108  constant_iterator old_ci = *this;
109  index_++;
110  return old_ci;
111  }
112 
113  ROCPRIM_HOST_DEVICE inline
114  constant_iterator& operator--()
115  {
116  index_--;
117  return *this;
118  }
119 
120  ROCPRIM_HOST_DEVICE inline
121  constant_iterator operator--(int)
122  {
123  constant_iterator old_ci = *this;
124  index_--;
125  return old_ci;
126  }
127 
128  ROCPRIM_HOST_DEVICE inline
129  constant_iterator operator+(difference_type distance) const
130  {
131  return constant_iterator(value_, index_ + distance);
132  }
133 
134  ROCPRIM_HOST_DEVICE inline
135  constant_iterator& operator+=(difference_type distance)
136  {
137  index_ += distance;
138  return *this;
139  }
140 
141  ROCPRIM_HOST_DEVICE inline
142  constant_iterator operator-(difference_type distance) const
143  {
144  return constant_iterator(value_, index_ - distance);
145  }
146 
147  ROCPRIM_HOST_DEVICE inline
148  constant_iterator& operator-=(difference_type distance)
149  {
150  index_ -= distance;
151  return *this;
152  }
153 
154  ROCPRIM_HOST_DEVICE inline
155  difference_type operator-(constant_iterator other) const
156  {
157  return static_cast<difference_type>(index_ - other.index_);
158  }
159  #endif // DOXYGEN_SHOULD_SKIP_THIS
160 
164  ROCPRIM_HOST_DEVICE inline
166  {
167  return value_;
168  }
169 
170  #ifndef DOXYGEN_SHOULD_SKIP_THIS
171  ROCPRIM_HOST_DEVICE inline
172  bool operator==(constant_iterator other) const
173  {
174  return value_ == other.value_ && index_ == other.index_;
175  }
176 
177  ROCPRIM_HOST_DEVICE inline
178  bool operator!=(constant_iterator other) const
179  {
180  return !(*this == other);
181  }
182 
183  ROCPRIM_HOST_DEVICE inline
184  bool operator<(constant_iterator other) const
185  {
186  return distance_to(other) > 0;
187  }
188 
189  ROCPRIM_HOST_DEVICE inline
190  bool operator<=(constant_iterator other) const
191  {
192  return distance_to(other) >= 0;
193  }
194 
195  ROCPRIM_HOST_DEVICE inline
196  bool operator>(constant_iterator other) const
197  {
198  return distance_to(other) < 0;
199  }
200 
201  ROCPRIM_HOST_DEVICE inline
202  bool operator>=(constant_iterator other) const
203  {
204  return distance_to(other) <= 0;
205  }
206 
207  friend std::ostream& operator<<(std::ostream& os, const constant_iterator& iter)
208  {
209  os << "[" << iter.value_ << "]";
210  return os;
211  }
212  #endif // DOXYGEN_SHOULD_SKIP_THIS
213 
214 private:
215  inline
216  difference_type distance_to(const constant_iterator& other) const
217  {
218  return difference_type(other.index_) - difference_type(index_);
219  }
220 
221  value_type value_;
222  size_t index_;
223 };
224 
225 #ifndef DOXYGEN_SHOULD_SKIP_THIS
226 template<
227  class ValueType,
228  class Difference
229 >
230 ROCPRIM_HOST_DEVICE inline
234 {
235  return iter + distance;
236 }
237 #endif // DOXYGEN_SHOULD_SKIP_THIS
238 
247 template<
248  class ValueType,
249  class Difference = std::ptrdiff_t
250 >
251 ROCPRIM_HOST_DEVICE inline
253 make_constant_iterator(ValueType value, size_t index = 0)
254 {
255  return constant_iterator<ValueType, Difference>(value, index);
256 }
257 
258 END_ROCPRIM_NAMESPACE
259 
261 // end of group iteratormodule
262 
263 #endif // ROCPRIM_ITERATOR_CONSTANT_ITERATOR_HPP_
const value_type * pointer
A pointer type of the type iterated over (value_type).
Definition: constant_iterator.hpp:62
A random-access input (read-only) iterator which generates a sequence of homogeneous values...
Definition: constant_iterator.hpp:51
ROCPRIM_HOST_DEVICE constant_iterator(const value_type value, const size_t index=0)
Creates constant_iterator and sets its initial value to value.
Definition: constant_iterator.hpp:77
ROCPRIM_HOST_DEVICE value_type operator[](difference_type) const
Constant_iterator is not writable, so we don&#39;t return reference, just something convertible to refere...
Definition: constant_iterator.hpp:165
typename std::remove_const< ValueType >::type value_type
The type of the value that can be obtained by dereferencing the iterator.
Definition: constant_iterator.hpp:55
ROCPRIM_HOST_DEVICE constant_iterator< ValueType, Difference > make_constant_iterator(ValueType value, size_t index=0)
make_constant_iterator creates a constant_iterator with its initial value set to value.
Definition: constant_iterator.hpp:253
value_type reference
A reference type of the type iterated over (value_type).
Definition: constant_iterator.hpp:59
Difference difference_type
A type used for identify distance between iterators.
Definition: constant_iterator.hpp:64
std::random_access_iterator_tag iterator_category
The category of the iterator.
Definition: constant_iterator.hpp:66