okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
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 : 36T __my_reference_ ; 37_UnaryFunc __my_unary_func_ ; 38 39public : 40template < typename U > 41transform_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 47template < typename UnaryInputType > 48transform_output_ref_wrapper & operator = (const UnaryInputType & e ) { 49__my_reference_ = __my_unary_func_ (e ); 50return * 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 ; 61template < typename T > auto operator ()(T && val )const { 62return 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 ) { 75return oneapi ::dpl ::counting_iterator < T > (input ); 76} 77 78template < typename _Tp > class constant_iterator { 79public : 80typedef std ::false_type is_hetero ; 81typedef std ::true_type is_passed_directly ; 82typedef std ::ptrdiff_t difference_type ; 83typedef _Tp value_type ; 84typedef _Tp * pointer ; 85// There is no storage behind the iterator, so we return a value instead of 86// reference. 87typedef const _Tp reference ; 88typedef const _Tp const_reference ; 89typedef std ::random_access_iterator_tag iterator_category ; 90 91explicit 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 97constant_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. 103const_reference operator * ()const {return __my_value_ ; } 104const_reference operator [](difference_type )const {return __my_value_ ; } 105 106difference_type operator - (const constant_iterator & __it )const { 107return __my_counter_ - __it .__my_counter_ ; 108 } 109 110constant_iterator & operator += (difference_type __forward ) { 111__my_counter_ += __forward ; 112return * this ; 113 } 114constant_iterator & operator -= (difference_type __backward ) { 115return * this += - __backward ; 116 } 117constant_iterator & operator ++ () {return * this += 1 ; } 118constant_iterator & operator -- () {return * this -= 1 ; } 119 120constant_iterator operator ++ (int ) { 121constant_iterator __it (* this ); 122++ (* this ); 123return __it ; 124 } 125constant_iterator operator -- (int ) { 126constant_iterator __it (* this ); 127-- (* this ); 128return __it ; 129 } 130 131constant_iterator operator - (difference_type __backward )const { 132return constant_iterator (__my_value_ ,__my_counter_ - __backward ); 133 } 134constant_iterator operator + (difference_type __forward )const { 135return constant_iterator (__my_value_ ,__my_counter_ + __forward ); 136 } 137friend constant_iterator operator + (difference_type __forward , 138const constant_iterator __it ) { 139return __it + __forward ; 140 } 141 142bool operator == (const constant_iterator & __it )const { 143return __my_value_ == __it .__my_value_ && 144this -> __my_counter_ == __it .__my_counter_ ; 145 } 146bool operator != (const constant_iterator & __it )const { 147return !(* this == __it ); 148 } 149bool operator < (const constant_iterator & __it )const { 150return * this - __it < 0 ; 151 } 152bool operator > (const constant_iterator & __it )const {return __it < * this ; } 153bool operator <=(const constant_iterator & __it )const { 154return !(* this > __it ); 155 } 156bool operator >=(const constant_iterator & __it )const { 157return !(* this < __it ); 158 } 159 160private : 161_Tp __my_value_ ; 162uint64_t __my_counter_ ; 163}; 164 165template < typename _Tp > 166constant_iterator < _Tp > make_constant_iterator (_Tp __value ) { 167return 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 : 174key_value_pair ()= default ; 175 176key_value_pair (const _KeyTp & _key ,const _ValueTp & _value ) 177 :key (_key ),value (_value ) {} 178 179bool operator == (const key_value_pair < _KeyTp ,_ValueTp > & _kvp )const { 180return (key == _kvp .key )&& (value == _kvp .value ); 181 } 182 183bool operator != (const key_value_pair < _KeyTp ,_ValueTp > & _kvp )const { 184return (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 { 194template < typename ValRefTp > 195key_value_pair < KeyTp ,_ValueTp > 196operator ()(const oneapi ::dpl ::__internal ::tuple < KeyTp ,ValRefTp > & tup )const { 197return ::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 ...>> { 204using 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 , 219typename OutputValueT = 220typename ::std ::iterator_traits < InputIteratorT > ::value_type > 221class arg_index_input_iterator 222 :public oneapi ::dpl ::transform_iterator < 223oneapi ::dpl ::zip_iterator < oneapi ::dpl ::counting_iterator < OffsetT > , 224InputIteratorT > , 225detail ::make_key_value_pair < OffsetT ,OutputValueT >> { 226using arg_index_input_iterator_wrap = oneapi ::dpl ::transform_iterator < 227oneapi ::dpl ::zip_iterator < oneapi ::dpl ::counting_iterator < OffsetT > , 228InputIteratorT > , 229detail ::make_key_value_pair < OffsetT ,OutputValueT >>; 230 231public : 232typedef OffsetT difference_type ; 233 234// signal to __get_sycl_range that this iterator is as a direct pass iterator 235using is_zip = ::std ::true_type ; 236 237arg_index_input_iterator (const arg_index_input_iterator_wrap & __arg_wrap ) 238 :arg_index_input_iterator_wrap (__arg_wrap ) {} 239arg_index_input_iterator (InputIteratorT __iter ) 240 :arg_index_input_iterator_wrap ( 241oneapi ::dpl ::make_zip_iterator ( 242oneapi ::dpl ::counting_iterator (OffsetT (0 )),__iter ), 243detail ::make_key_value_pair < OffsetT ,OutputValueT > ()) {} 244 245arg_index_input_iterator & operator = (const arg_index_input_iterator & __input ) { 246arg_index_input_iterator_wrap ::operator = (__input ); 247return * this ; 248 } 249arg_index_input_iterator & operator ++ () { 250arg_index_input_iterator_wrap ::operator ++ (); 251return * this ; 252 } 253arg_index_input_iterator & operator -- () { 254arg_index_input_iterator_wrap ::operator -- (); 255return * this ; 256 } 257arg_index_input_iterator operator ++ (int ) { 258arg_index_input_iterator __it (* this ); 259++ (* this ); 260return __it ; 261 } 262arg_index_input_iterator operator -- (int ) { 263arg_index_input_iterator __it (* this ); 264-- (* this ); 265return __it ; 266 } 267arg_index_input_iterator operator + (difference_type __forward )const { 268return arg_index_input_iterator ( 269arg_index_input_iterator_wrap ::operator + (__forward )); 270 } 271arg_index_input_iterator operator - (difference_type __backward )const { 272return arg_index_input_iterator ( 273arg_index_input_iterator_wrap ::operator - (__backward )); 274 } 275arg_index_input_iterator & operator += (difference_type __forward ) { 276arg_index_input_iterator_wrap ::operator += (__forward ); 277return * this ; 278 } 279arg_index_input_iterator & operator -= (difference_type __backward ) { 280arg_index_input_iterator_wrap ::operator -= (__backward ); 281return * this ; 282 } 283 284friend arg_index_input_iterator 285operator + (difference_type __forward ,const arg_index_input_iterator & __it ) { 286return __it + __forward ; 287 } 288 289difference_type operator - (const arg_index_input_iterator & __it )const { 290return arg_index_input_iterator_wrap ::operator - (__it ); 291 } 292bool operator == (const arg_index_input_iterator & __it )const { 293return arg_index_input_iterator_wrap ::operator == (__it ); 294 } 295bool operator != (const arg_index_input_iterator & __it )const { 296return !(* this == __it ); 297 } 298bool operator < (const arg_index_input_iterator & __it )const { 299return * this - __it < 0 ; 300 } 301bool operator > (const arg_index_input_iterator & __it )const { 302return __it < * this ; 303 } 304bool operator <=(const arg_index_input_iterator & __it )const { 305return !(* this > __it ); 306 } 307bool operator >=(const arg_index_input_iterator & __it )const { 308return !(* this < __it ); 309 } 310 311// returns an arg_index_input_iterator with the same iter position, but a 312// count reset to 0 313arg_index_input_iterator create_normalized () { 314return 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 { 320inline io_iterator_pair () :selector (false ) {} 321 322inline io_iterator_pair (const IterT & first ,const IterT & second ) 323 :selector (false ) { 324iter [0 ]= first ; 325iter [1 ]= second ; 326 } 327 328inline IterT first ()const {return selector ?iter [1 ] :iter [0 ]; } 329 330inline IterT second ()const {return selector ?iter [0 ] :iter [1 ]; } 331 332inline void swap () {selector = !selector ; } 333 334bool selector ; 335 336IterT iter [2 ]; 337}; 338 339template < typename _Iter ,typename _UnaryFunc > 340auto make_transform_output_iterator (_Iter __it ,_UnaryFunc __unary_func ) { 341return oneapi ::dpl ::transform_iterator ( 342__it ,internal ::_Unary_Out < _UnaryFunc > (__unary_func )); 343} 344 345}// end namespace dpct 346 347#endif