okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
11.3 KiB347 linesraw
1//==---- iterators.h ------------------------------*- C++ -*----------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_ITERATORS_H__
10#define __DPCT_ITERATORS_H__
11
12#include <oneapi/dpl/iterator>
13
14#include "functional.h"
15
16namespace dpct {
17
18namespace internal {
19
20// Wrapper class returned from a dereferenced transform_iterator which was
21// created using
22//  make_transform_output_iterator(). Used to apply the supplied transform
23//  function when writing into an object of this class.
24//
25// Example:
26// int a[] = {0, 1, 2, 3, 4};
27// int* p = a;
28// auto f = [](auto v) {return v*v;};
29// auto tr_out = dpct::make_transform_output_iterator(p+1, f);
30// auto wrap = *tr_out;         // wrap is a transform_output_ref_wrapper
31// std::cout<<*(p+1)<<std::endl;  // '1'
32// wrap = 2;                    // apply function, store 2*2=4
33// std::cout<<*(p+1)<<std::endl;  // '4'
34template <typename T, typename _UnaryFunc> class transform_output_ref_wrapper {
35private:
36  T __my_reference_;
37  _UnaryFunc __my_unary_func_;
38
39public:
40  template <typename U>
41  transform_output_ref_wrapper(U &&__reference, _UnaryFunc __unary_func)
42      : __my_reference_(std::forward<U>(__reference)),
43        __my_unary_func_(__unary_func) {}
44
45  // When writing to an object of this type, apply the supplied unary function,
46  // then write to the wrapped reference
47  template <typename UnaryInputType>
48  transform_output_ref_wrapper &operator=(const UnaryInputType &e) {
49    __my_reference_ = __my_unary_func_(e);
50    return *this;
51  }
52};
53
54// Unary functor to create a transform_output_reference_wrapper when a
55// transform_iterator is dereferenced, so that a
56// the supplied unary function may be applied on write, resulting in a
57// transform_output_iterator
58template <typename _UnaryFunc> struct _Unary_Out {
59  _Unary_Out(_UnaryFunc __f_) : __f(__f_) {}
60  _UnaryFunc __f;
61  template <typename T> auto operator()(T &&val) const {
62    return transform_output_ref_wrapper<T, _UnaryFunc>(std::forward<T>(val),
63                                                       __f);
64  }
65};
66
67} // end namespace internal
68
69using std::advance;
70
71using std::distance;
72
73template <typename T>
74oneapi::dpl::counting_iterator<T> make_counting_iterator(const T &input) {
75  return oneapi::dpl::counting_iterator<T>(input);
76}
77
78template <typename _Tp> class constant_iterator {
79public:
80  typedef std::false_type is_hetero;
81  typedef std::true_type is_passed_directly;
82  typedef std::ptrdiff_t difference_type;
83  typedef _Tp value_type;
84  typedef _Tp *pointer;
85  // There is no storage behind the iterator, so we return a value instead of
86  // reference.
87  typedef const _Tp reference;
88  typedef const _Tp const_reference;
89  typedef std::random_access_iterator_tag iterator_category;
90
91  explicit constant_iterator(_Tp __init)
92      : __my_value_(__init), __my_counter_(0) {}
93
94private:
95  // used to construct iterator instances with different counter values required
96  // by arithmetic operators
97  constant_iterator(const _Tp &__value, const difference_type &__offset)
98      : __my_value_(__value), __my_counter_(__offset) {}
99
100public:
101  // non-const variants of access operators are not provided so unintended
102  // writes are caught at compile time.
103  const_reference operator*() const { return __my_value_; }
104  const_reference operator[](difference_type) const { return __my_value_; }
105
106  difference_type operator-(const constant_iterator &__it) const {
107    return __my_counter_ - __it.__my_counter_;
108  }
109
110  constant_iterator &operator+=(difference_type __forward) {
111    __my_counter_ += __forward;
112    return *this;
113  }
114  constant_iterator &operator-=(difference_type __backward) {
115    return *this += -__backward;
116  }
117  constant_iterator &operator++() { return *this += 1; }
118  constant_iterator &operator--() { return *this -= 1; }
119
120  constant_iterator operator++(int) {
121    constant_iterator __it(*this);
122    ++(*this);
123    return __it;
124  }
125  constant_iterator operator--(int) {
126    constant_iterator __it(*this);
127    --(*this);
128    return __it;
129  }
130
131  constant_iterator operator-(difference_type __backward) const {
132    return constant_iterator(__my_value_, __my_counter_ - __backward);
133  }
134  constant_iterator operator+(difference_type __forward) const {
135    return constant_iterator(__my_value_, __my_counter_ + __forward);
136  }
137  friend constant_iterator operator+(difference_type __forward,
138                                     const constant_iterator __it) {
139    return __it + __forward;
140  }
141
142  bool operator==(const constant_iterator &__it) const {
143    return __my_value_ == __it.__my_value_ &&
144           this->__my_counter_ == __it.__my_counter_;
145  }
146  bool operator!=(const constant_iterator &__it) const {
147    return !(*this == __it);
148  }
149  bool operator<(const constant_iterator &__it) const {
150    return *this - __it < 0;
151  }
152  bool operator>(const constant_iterator &__it) const { return __it < *this; }
153  bool operator<=(const constant_iterator &__it) const {
154    return !(*this > __it);
155  }
156  bool operator>=(const constant_iterator &__it) const {
157    return !(*this < __it);
158  }
159
160private:
161  _Tp __my_value_;
162  uint64_t __my_counter_;
163};
164
165template <typename _Tp>
166constant_iterator<_Tp> make_constant_iterator(_Tp __value) {
167  return constant_iterator<_Tp>(__value);
168}
169
170// key_value_pair class to represent a key and value, specifically a
171// dereferenced arg_index_input_iterator
172template <typename _KeyTp, typename _ValueTp> class key_value_pair {
173public:
174  key_value_pair() = default;
175
176  key_value_pair(const _KeyTp &_key, const _ValueTp &_value)
177      : key(_key), value(_value) {}
178
179  bool operator==(const key_value_pair<_KeyTp, _ValueTp> &_kvp) const {
180    return (key == _kvp.key) && (value == _kvp.value);
181  }
182
183  bool operator!=(const key_value_pair<_KeyTp, _ValueTp> &_kvp) const {
184    return (key != _kvp.key) || (value != _kvp.value);
185  }
186
187  _KeyTp key;
188  _ValueTp value;
189};
190
191namespace detail {
192
193template <typename KeyTp, typename _ValueTp> struct make_key_value_pair {
194  template <typename ValRefTp>
195  key_value_pair<KeyTp, _ValueTp>
196  operator()(const oneapi::dpl::__internal::tuple<KeyTp, ValRefTp> &tup) const {
197    return ::dpct::key_value_pair<KeyTp, _ValueTp>(::std::get<0>(tup),
198                                                   ::std::get<1>(tup));
199  }
200};
201
202template <class T> struct __zip_iterator_impl;
203template <class... Ts> struct __zip_iterator_impl<std::tuple<Ts...>> {
204  using type = oneapi::dpl::zip_iterator<Ts...>;
205};
206
207} // end namespace detail
208
209// dpct::zip_iterator can only accept std::tuple type as template argument for
210// compatibility purpose. Please use oneapi::dpl::zip_iterator if you want to
211// pass iterator's types directly.
212template <typename... Ts>
213using zip_iterator = typename detail::__zip_iterator_impl<Ts...>::type;
214
215// arg_index_input_iterator is an iterator over a input iterator, with a index.
216// When dereferenced, it returns a key_value_pair, which can be interrogated for
217// the index key or the value from the input iterator
218template <typename InputIteratorT, typename OffsetT = ptrdiff_t,
219          typename OutputValueT =
220              typename ::std::iterator_traits<InputIteratorT>::value_type>
221class arg_index_input_iterator
222    : public oneapi::dpl::transform_iterator<
223          oneapi::dpl::zip_iterator<oneapi::dpl::counting_iterator<OffsetT>,
224                                    InputIteratorT>,
225          detail::make_key_value_pair<OffsetT, OutputValueT>> {
226  using arg_index_input_iterator_wrap = oneapi::dpl::transform_iterator<
227      oneapi::dpl::zip_iterator<oneapi::dpl::counting_iterator<OffsetT>,
228                                InputIteratorT>,
229      detail::make_key_value_pair<OffsetT, OutputValueT>>;
230
231public:
232  typedef OffsetT difference_type;
233
234  // signal to __get_sycl_range that this iterator is as a direct pass iterator
235  using is_zip = ::std::true_type;
236
237  arg_index_input_iterator(const arg_index_input_iterator_wrap &__arg_wrap)
238      : arg_index_input_iterator_wrap(__arg_wrap) {}
239  arg_index_input_iterator(InputIteratorT __iter)
240      : arg_index_input_iterator_wrap(
241            oneapi::dpl::make_zip_iterator(
242                oneapi::dpl::counting_iterator(OffsetT(0)), __iter),
243            detail::make_key_value_pair<OffsetT, OutputValueT>()) {}
244
245  arg_index_input_iterator &operator=(const arg_index_input_iterator &__input) {
246    arg_index_input_iterator_wrap::operator=(__input);
247    return *this;
248  }
249  arg_index_input_iterator &operator++() {
250    arg_index_input_iterator_wrap::operator++();
251    return *this;
252  }
253  arg_index_input_iterator &operator--() {
254    arg_index_input_iterator_wrap::operator--();
255    return *this;
256  }
257  arg_index_input_iterator operator++(int) {
258    arg_index_input_iterator __it(*this);
259    ++(*this);
260    return __it;
261  }
262  arg_index_input_iterator operator--(int) {
263    arg_index_input_iterator __it(*this);
264    --(*this);
265    return __it;
266  }
267  arg_index_input_iterator operator+(difference_type __forward) const {
268    return arg_index_input_iterator(
269        arg_index_input_iterator_wrap::operator+(__forward));
270  }
271  arg_index_input_iterator operator-(difference_type __backward) const {
272    return arg_index_input_iterator(
273        arg_index_input_iterator_wrap::operator-(__backward));
274  }
275  arg_index_input_iterator &operator+=(difference_type __forward) {
276    arg_index_input_iterator_wrap::operator+=(__forward);
277    return *this;
278  }
279  arg_index_input_iterator &operator-=(difference_type __backward) {
280    arg_index_input_iterator_wrap::operator-=(__backward);
281    return *this;
282  }
283
284  friend arg_index_input_iterator
285  operator+(difference_type __forward, const arg_index_input_iterator &__it) {
286    return __it + __forward;
287  }
288
289  difference_type operator-(const arg_index_input_iterator &__it) const {
290    return arg_index_input_iterator_wrap::operator-(__it);
291  }
292  bool operator==(const arg_index_input_iterator &__it) const {
293    return arg_index_input_iterator_wrap::operator==(__it);
294  }
295  bool operator!=(const arg_index_input_iterator &__it) const {
296    return !(*this == __it);
297  }
298  bool operator<(const arg_index_input_iterator &__it) const {
299    return *this - __it < 0;
300  }
301  bool operator>(const arg_index_input_iterator &__it) const {
302    return __it < *this;
303  }
304  bool operator<=(const arg_index_input_iterator &__it) const {
305    return !(*this > __it);
306  }
307  bool operator>=(const arg_index_input_iterator &__it) const {
308    return !(*this < __it);
309  }
310
311  // returns an arg_index_input_iterator with the same iter position, but a
312  // count reset to 0
313  arg_index_input_iterator create_normalized() {
314    return arg_index_input_iterator(
315        ::std::get<1>(arg_index_input_iterator_wrap::base().base()));
316  }
317};
318
319template <typename IterT> struct io_iterator_pair {
320  inline io_iterator_pair() : selector(false) {}
321
322  inline io_iterator_pair(const IterT &first, const IterT &second)
323      : selector(false) {
324    iter[0] = first;
325    iter[1] = second;
326  }
327
328  inline IterT first() const { return selector ? iter[1] : iter[0]; }
329
330  inline IterT second() const { return selector ? iter[0] : iter[1]; }
331
332  inline void swap() { selector = !selector; }
333
334  bool selector;
335
336  IterT iter[2];
337};
338
339template <typename _Iter, typename _UnaryFunc>
340auto make_transform_output_iterator(_Iter __it, _UnaryFunc __unary_func) {
341  return oneapi::dpl::transform_iterator(
342      __it, internal::_Unary_Out<_UnaryFunc>(__unary_func));
343}
344
345} // end namespace dpct
346
347#endif