From fc04cafc39bc694473a2b8a34ea578dc6fcf4562 Mon Sep 17 00:00:00 2001 From: MikeDvorskiy Date: Thu, 26 Sep 2024 11:31:10 +0200 Subject: [PATCH] [oneDPL][ranges][zip_view] + begin_imp, end_impl to reduce code duplication --- include/oneapi/dpl/pstl/zip_view_impl.h | 54 ++++++++++++------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/include/oneapi/dpl/pstl/zip_view_impl.h b/include/oneapi/dpl/pstl/zip_view_impl.h index 6b3afad9e01..913724ee0c8 100644 --- a/include/oneapi/dpl/pstl/zip_view_impl.h +++ b/include/oneapi/dpl/pstl/zip_view_impl.h @@ -293,25 +293,22 @@ class zip_view : public std::ranges::view_interface> { tuple_type...>>; end_type end_; - }; // class sentinel + }; // class sentinel - constexpr auto begin() requires (std::ranges::range && ...) // !simple_view? - { - auto __tr = [](auto... __args) { return iterator(__args...);}; - return apply_to_tuple(__tr, std::ranges::begin, views_); - } - - constexpr auto begin() const requires ( std::ranges::range && ... ) +private: + template + constexpr auto begin_impl() { - auto __tr = [](auto... __args) { return iterator(__args...);}; + auto __tr = [](auto... __args) { return iterator(__args...);}; return apply_to_tuple(__tr, std::ranges::begin, views_); } - constexpr auto end() requires (std::ranges::range && ...) // requires !simple_view { + template + constexpr auto end_impl() { if constexpr (!zip_is_common) { - auto __tr = [](auto... __args) { return sentinel(__args...);}; + auto __tr = [](auto... __args) { return sentinel(__args...);}; return apply_to_tuple(__tr, std::ranges::end, views_); } else if constexpr ((std::ranges::random_access_range && ...)) @@ -322,29 +319,30 @@ class zip_view : public std::ranges::view_interface> { } else { - auto __tr = [](auto... __args) { return iterator(__args...);}; + auto __tr = [](auto... __args) { return iterator(__args...);}; return apply_to_tuple(__tr, std::ranges::end, views_); } } +public: + constexpr auto begin() requires (std::ranges::range && ...) // !simple_view? + { + return begin_impl(); + } + + constexpr auto begin() const requires ( std::ranges::range && ... ) + { + return const_cast(this)->begin_impl(); + } + + constexpr auto end() requires (std::ranges::range && ...) // requires !simple_view { + { + return end_impl(); + } + constexpr auto end() const requires (std::ranges::range && ...) { - if constexpr (!zip_is_common) - { - auto __tr = [](auto... __args) { return sentinel(__args...);}; - return apply_to_tuple(__tr, std::ranges::end, views_); - } - else if constexpr ((std::ranges::random_access_range && ...)) - { - auto it = begin(); - it += size(); - return it; - } - else - { - auto __tr = [](auto... __args) { return iterator(__args...);}; - return apply_to_tuple(__tr, std::ranges::end, views_); - } + return const_cast(this)->end_impl(); } constexpr auto size() requires (std::ranges::sized_range && ...)