diff --git a/include/oneapi/dpl/pstl/glue_algorithm_ranges_impl.h b/include/oneapi/dpl/pstl/glue_algorithm_ranges_impl.h index 77a4875ccde..d0bc2809ad4 100644 --- a/include/oneapi/dpl/pstl/glue_algorithm_ranges_impl.h +++ b/include/oneapi/dpl/pstl/glue_algorithm_ranges_impl.h @@ -48,6 +48,47 @@ namespace dpl namespace ranges { +namespace __internal +{ + +template +concept __is_subscriptable = requires(_T&& __a) { __a[0]; }; + +template +concept __is_not_subscriptable = !__is_subscriptable<_T>; + +template +concept __is_sizeable = requires(_T&& __a) { __a.size(); }; + +template +struct _WrapperRAR: public _R +{ + template + _WrapperRAR(_Base&& __r): _R(std::forward<_Base>(__r)) {} + decltype(auto) operator[](auto __i) { return _R::begin()[__i]; } + decltype(auto) operator[](auto __i) const { return _R::begin()[__i]; } + + std::enable_if_t, std::ranges::range_size_t<_R>> + size() const { return this->_R::end() - this->_R::begin(); } +}; + +template <__is_not_subscriptable _R> +constexpr decltype (auto) +__get_r(_R&& __r) +{ + using _T = std::remove_reference_t<_R>; + return _WrapperRAR<_T>(std::forward<_R>(__r)); +} + +template <__is_subscriptable _R> +constexpr decltype (auto) +__get_r(_R&& __r) +{ + return std::forward<_R>(__r); +} + +} //__internal + // [alg.foreach] namespace __internal @@ -64,7 +105,7 @@ struct __for_each_fn { const auto __dispatch_tag = oneapi::dpl::__ranges::__select_backend(__exec); oneapi::dpl::__internal::__ranges::__pattern_for_each( - __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __r, __f, __proj); + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __get_r(__r), __f, __proj); return {std::ranges::begin(__r) + std::ranges::size(__r)}; }