From 145318fe3861025f51b3c1dc759804407c7f21cf Mon Sep 17 00:00:00 2001 From: changkhothuychung Date: Wed, 20 Nov 2024 23:59:44 -0500 Subject: [PATCH] fix --- libcxx/include/__ranges/concat_view.h | 43 +++++------ .../range.concat/adaptor.pass.cpp | 74 +++++++++++++++++++ .../range.adaptors/range.concat/base.pass.cpp | 1 - 3 files changed, 94 insertions(+), 24 deletions(-) create mode 100644 libcxx/test/std/ranges/range.adaptors/range.concat/adaptor.pass.cpp diff --git a/libcxx/include/__ranges/concat_view.h b/libcxx/include/__ranges/concat_view.h index 1e6bd894822e25d..250632afd13fb1d 100644 --- a/libcxx/include/__ranges/concat_view.h +++ b/libcxx/include/__ranges/concat_view.h @@ -335,6 +335,18 @@ template } } + template + constexpr void apply_advance_fwd_by_index(size_t index, Func&& func, std::index_sequence) { + // Unpack indices and call func with the correct compile-time constant + ((index == Is ? (func(std::integral_constant{}), 0) : 0), ...); + } + + template + constexpr void apply_advance_fwd_by_index(size_t index, Func&& func) { + apply_advance_fwd_by_index(index, std::forward(func), std::make_index_sequence{}); + } + + template explicit constexpr iterator(__maybe_const* parent, Args&&... args) requires constructible_from @@ -356,9 +368,12 @@ template constexpr iterator& operator++() { - constexpr auto i = it_.index(); - ++get(it_); - satisfy(); + size_t active_index = it_.index(); + apply_advance_fwd_by_index>(active_index, [&](auto index_constant){ + constexpr size_t i = index_constant.value; + ++get(it_); + satisfy(); + }); return *this; } @@ -369,7 +384,6 @@ template } */ - constexpr iterator operator++(int) requires (forward_range<__maybe_const> && ...) { @@ -378,8 +392,6 @@ template return tmp; } - - constexpr iterator& operator--() requires concat_is_bidirectional { @@ -413,17 +425,6 @@ template { return it + n; } - - template - constexpr void apply_advance_fwd_by_index(size_t index, Func&& func, std::index_sequence) { - // Unpack indices and call func with the correct compile-time constant - ((index == Is ? (func(std::integral_constant{}), 0) : 0), ...); - } - - template - constexpr void apply_advance_fwd_by_index(size_t index, Func&& func) { - apply_advance_fwd_by_index(index, std::forward(func), std::make_index_sequence{}); - } constexpr iterator& operator+=(difference_type n) @@ -594,9 +595,7 @@ template } - - - + /* friend constexpr difference_type operator-(const iterator& x, default_sentinel_t) @@ -604,8 +603,6 @@ template friend constexpr difference_type operator-(default_sentinel_t, const iterator& x) requires see below; - - */ }; @@ -619,7 +616,7 @@ namespace __concat { [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Views... views) const noexcept(noexcept(concat_view(std::forward<_Views>(views)...))) - -> decltype( concat_view(std::forward<_Views>(views)...)) + -> decltype(concat_view(std::forward<_Views>(views)...)) { return concat_view(std::forward<_Views>(views)...); } }; diff --git a/libcxx/test/std/ranges/range.adaptors/range.concat/adaptor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.concat/adaptor.pass.cpp new file mode 100644 index 000000000000000..bab2cec1cb2ede8 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.concat/adaptor.pass.cpp @@ -0,0 +1,74 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// std::views::filter + +#include + +#include +#include +#include +#include +#include +#include + +#include "test_iterators.h" +#include "test_range.h" + + +struct Range : std::ranges::view_base { + using Iterator = forward_iterator; + using Sentinel = sentinel_wrapper; + constexpr explicit Range(int* b, int* e) : begin_(b), end_(e) { } + constexpr Iterator begin() const { return Iterator(begin_); } + constexpr Sentinel end() const { return Sentinel(Iterator(end_)); } + +private: + int* begin_; + int* end_; +}; + + +template +constexpr void compareViews(View v, std::initializer_list list) { + auto b1 = v.begin(); + auto e1 = v.end(); + auto b2 = list.begin(); + auto e2 = list.end(); + for (; b1 != e1 && b2 != e2; ++b1, ++b2) { + assert(*b1 == *b2); + } + assert(b1 == e1); + assert(b2 == e2); +} + +constexpr bool test() { + int buff[] = {0, 1, 2, 3, 4, 5, 6, 7}; + + // Test `views::filter(pred)(v)` + { + using Result = std::ranges::concat_view; + Range range(buff, buff + 8); + + { + decltype(auto) result = std::views::concat(range); + compareViews(result, {0, 1, 2, 3, 4, 5, 6, 7}); + } + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.concat/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.concat/base.pass.cpp index 69e3eb1251485ce..5d9952b6966f5f0 100644 --- a/libcxx/test/std/ranges/range.adaptors/range.concat/base.pass.cpp +++ b/libcxx/test/std/ranges/range.adaptors/range.concat/base.pass.cpp @@ -57,7 +57,6 @@ constexpr bool test() { { auto result = std::views::concat(v1); auto begin_iter = result.begin(); - assert(begin_iter[0] == v1[0]); }