rocPRIM
discard_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_DISCARD_ITERATOR_HPP_
22 #define ROCPRIM_ITERATOR_DISCARD_ITERATOR_HPP_
23 
24 #include <iterator>
25 #include <cstddef>
26 #include <type_traits>
27 
28 #include "../config.hpp"
29 
32 
33 BEGIN_ROCPRIM_NAMESPACE
34 
44 {
45 public:
46  #ifndef DOXYGEN_SHOULD_SKIP_THIS // Skip internal implementation details.
48  {
49  ROCPRIM_HOST_DEVICE inline
50  discard_value() = default;
51 
52  template<class T>
53  ROCPRIM_HOST_DEVICE inline
54  discard_value(T) {};
55 
56  ROCPRIM_HOST_DEVICE inline
57  ~discard_value() = default;
58 
59  template<class T>
60  ROCPRIM_HOST_DEVICE inline
61  discard_value& operator=(const T&)
62  {
63  return *this;
64  }
65  };
66  #endif // DOXYGEN_SHOULD_SKIP_THIS
67 
75  using difference_type = std::ptrdiff_t;
77  using iterator_category = std::random_access_iterator_tag;
78 
82  ROCPRIM_HOST_DEVICE inline
83  discard_iterator(size_t index = 0)
84  : index_(index)
85  {
86  }
87 
88  ROCPRIM_HOST_DEVICE inline
89  ~discard_iterator() = default;
90 
91  #ifndef DOXYGEN_SHOULD_SKIP_THIS
92  ROCPRIM_HOST_DEVICE inline
93  discard_iterator& operator++()
94  {
95  index_++;
96  return *this;
97  }
98 
99  ROCPRIM_HOST_DEVICE inline
100  discard_iterator operator++(int)
101  {
102  discard_iterator old = *this;
103  index_++;
104  return old;
105  }
106 
107  ROCPRIM_HOST_DEVICE inline
108  discard_iterator& operator--()
109  {
110  index_--;
111  return *this;
112  }
113 
114  ROCPRIM_HOST_DEVICE inline
115  discard_iterator operator--(int)
116  {
117  discard_iterator old = *this;
118  index_--;
119  return old;
120  }
121 
122  ROCPRIM_HOST_DEVICE inline
123  discard_value operator*() const
124  {
125  return discard_value();
126  }
127 
128  ROCPRIM_HOST_DEVICE inline
129  discard_value operator[](difference_type distance) const
130  {
131  discard_iterator i = (*this) + distance;
132  return *i;
133  }
134 
135  ROCPRIM_HOST_DEVICE inline
136  discard_iterator operator+(difference_type distance) const
137  {
138  auto i = static_cast<size_t>(static_cast<difference_type>(index_) + distance);
139  return discard_iterator(i);
140  }
141 
142  ROCPRIM_HOST_DEVICE inline
143  discard_iterator& operator+=(difference_type distance)
144  {
145  index_ = static_cast<size_t>(static_cast<difference_type>(index_) + distance);
146  return *this;
147  }
148 
149  ROCPRIM_HOST_DEVICE inline
150  discard_iterator operator-(difference_type distance) const
151  {
152  auto i = static_cast<size_t>(static_cast<difference_type>(index_) - distance);
153  return discard_iterator(i);
154  }
155 
156  ROCPRIM_HOST_DEVICE inline
157  discard_iterator& operator-=(difference_type distance)
158  {
159  index_ = static_cast<size_t>(static_cast<difference_type>(index_) - distance);
160  return *this;
161  }
162 
163  ROCPRIM_HOST_DEVICE inline
164  difference_type operator-(discard_iterator other) const
165  {
166  return index_ - other.index_;
167  }
168 
169  ROCPRIM_HOST_DEVICE inline
170  bool operator==(discard_iterator other) const
171  {
172  return index_ == other.index_;
173  }
174 
175  ROCPRIM_HOST_DEVICE inline
176  bool operator!=(discard_iterator other) const
177  {
178  return index_ != other.index_;
179  }
180 
181  ROCPRIM_HOST_DEVICE inline
182  bool operator<(discard_iterator other) const
183  {
184  return index_ < other.index_;
185  }
186 
187  ROCPRIM_HOST_DEVICE inline
188  bool operator<=(discard_iterator other) const
189  {
190  return index_ <= other.index_;
191  }
192 
193  ROCPRIM_HOST_DEVICE inline
194  bool operator>(discard_iterator other) const
195  {
196  return index_ > other.index_;
197  }
198 
199  ROCPRIM_HOST_DEVICE inline
200  bool operator>=(discard_iterator other) const
201  {
202  return index_ >= other.index_;
203  }
204 
205  friend std::ostream& operator<<(std::ostream& os, const discard_iterator& /* iter */)
206  {
207  return os;
208  }
209  #endif // DOXYGEN_SHOULD_SKIP_THIS
210 
211 private:
212  mutable size_t index_;
213 };
214 
215 #ifndef DOXYGEN_SHOULD_SKIP_THIS
216 ROCPRIM_HOST_DEVICE inline
218 operator+(typename discard_iterator::difference_type distance,
219  const discard_iterator& iterator)
220 {
221  return iterator + distance;
222 }
223 #endif // DOXYGEN_SHOULD_SKIP_THIS
224 
230 ROCPRIM_HOST_DEVICE inline
232 make_discard_iterator(size_t index = 0)
233 {
234  return discard_iterator(index);
235 }
236 
237 END_ROCPRIM_NAMESPACE
238 
240 // end of group iteratormodule
241 
242 #endif // ROCPRIM_ITERATOR_DISCARD_ITERATOR_HPP_
Definition: discard_iterator.hpp:47
ROCPRIM_HOST_DEVICE bool operator>=(const tuple< TTypes... > &lhs, const tuple< UTypes... > &rhs)
Greater than or equal to operator for tuples.
Definition: tuple.hpp:915
std::random_access_iterator_tag iterator_category
The category of the iterator.
Definition: discard_iterator.hpp:77
ROCPRIM_HOST_DEVICE bool operator<(const tuple< TTypes... > &lhs, const tuple< UTypes... > &rhs)
Less than operator for tuples.
Definition: tuple.hpp:864
ROCPRIM_HOST_DEVICE discard_iterator(size_t index=0)
Creates a new discard_iterator.
Definition: discard_iterator.hpp:83
ROCPRIM_HOST_DEVICE bool operator!=(const tuple< TTypes... > &lhs, const tuple< UTypes... > &rhs)
Not equal to operator for tuples.
Definition: tuple.hpp:838
std::ptrdiff_t difference_type
A type used for identify distance between iterators.
Definition: discard_iterator.hpp:75
ROCPRIM_HOST_DEVICE bool operator<=(const tuple< TTypes... > &lhs, const tuple< UTypes... > &rhs)
Less than or equal to operator for tuples.
Definition: tuple.hpp:898
ROCPRIM_HOST_DEVICE bool operator>(const tuple< TTypes... > &lhs, const tuple< UTypes... > &rhs)
Greater than operator for tuples.
Definition: tuple.hpp:881
ROCPRIM_HOST_DEVICE bool operator==(const tuple< TTypes... > &lhs, const tuple< UTypes... > &rhs)
Equal to operator for tuples.
Definition: tuple.hpp:819
ROCPRIM_HOST_DEVICE discard_iterator make_discard_iterator(size_t index=0)
make_discard_iterator creates a discard_iterator using optional index parameter index.
Definition: discard_iterator.hpp:232
A random-access iterator which discards values assigned to it upon dereference.
Definition: discard_iterator.hpp:43