Skip to content

Commit

Permalink
[oneDPL][ranges] + implementation fix for a random access range witho…
Browse files Browse the repository at this point in the history
…ut operator[] and size() method
  • Loading branch information
MikeDvorskiy committed Dec 23, 2024
1 parent 62863bd commit 0b537e6
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion include/oneapi/dpl/pstl/glue_algorithm_ranges_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,47 @@ namespace dpl
namespace ranges
{

namespace __internal
{

template<typename _T>
concept __is_subscriptable = requires(_T&& __a) { __a[0]; };

template<typename _T>
concept __is_not_subscriptable = !__is_subscriptable<_T>;

template<typename _T>
concept __is_sizeable = requires(_T&& __a) { __a.size(); };

template <typename _R>
struct _WrapperRAR: public _R
{
template <typename _Base>
_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<!__is_sizeable<_R>, 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
Expand All @@ -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)};
}
Expand Down

0 comments on commit 0b537e6

Please sign in to comment.