Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
changkhothuychung committed Nov 21, 2024
1 parent bfbec38 commit 145318f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 24 deletions.
43 changes: 20 additions & 23 deletions libcxx/include/__ranges/concat_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,18 @@ template <input_range... Views>
}
}

template <size_t... Is, typename Func>
constexpr void apply_advance_fwd_by_index(size_t index, Func&& func, std::index_sequence<Is...>) {
// Unpack indices and call func with the correct compile-time constant
((index == Is ? (func(std::integral_constant<size_t, Is>{}), 0) : 0), ...);
}

template <size_t N, typename Func>
constexpr void apply_advance_fwd_by_index(size_t index, Func&& func) {
apply_advance_fwd_by_index(index, std::forward<Func>(func), std::make_index_sequence<N>{});
}


template <class... Args>
explicit constexpr iterator(__maybe_const<Const, concat_view>* parent, Args&&... args)
requires constructible_from<base_iter, Args&&...>
Expand All @@ -356,9 +368,12 @@ template <input_range... Views>

constexpr iterator& operator++()
{
constexpr auto i = it_.index();
++get<i>(it_);
satisfy<i>();
size_t active_index = it_.index();
apply_advance_fwd_by_index<std::variant_size_v<decltype(it_)>>(active_index, [&](auto index_constant){
constexpr size_t i = index_constant.value;
++get<i>(it_);
satisfy<i>();
});
return *this;
}

Expand All @@ -369,7 +384,6 @@ template <input_range... Views>
}
*/


constexpr iterator operator++(int)
requires (forward_range<__maybe_const<Const, Views>> && ...)
{
Expand All @@ -378,8 +392,6 @@ template <input_range... Views>
return tmp;
}



constexpr iterator& operator--()
requires concat_is_bidirectional<Const, Views...>
{
Expand Down Expand Up @@ -413,17 +425,6 @@ template <input_range... Views>
{
return it + n;
}

template <size_t... Is, typename Func>
constexpr void apply_advance_fwd_by_index(size_t index, Func&& func, std::index_sequence<Is...>) {
// Unpack indices and call func with the correct compile-time constant
((index == Is ? (func(std::integral_constant<size_t, Is>{}), 0) : 0), ...);
}

template <size_t N, typename Func>
constexpr void apply_advance_fwd_by_index(size_t index, Func&& func) {
apply_advance_fwd_by_index(index, std::forward<Func>(func), std::make_index_sequence<N>{});
}


constexpr iterator& operator+=(difference_type n)
Expand Down Expand Up @@ -594,18 +595,14 @@ template <input_range... Views>

}





/*
friend constexpr difference_type operator-(const iterator& x, default_sentinel_t)
requires see below;
friend constexpr difference_type operator-(default_sentinel_t, const iterator& x)
requires see below;
*/
};

Expand All @@ -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)...); }

};
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <ranges>

#include <cassert>
#include <concepts>
#include <initializer_list>
#include <type_traits>
#include <utility>
#include <vector>

#include "test_iterators.h"
#include "test_range.h"


struct Range : std::ranges::view_base {
using Iterator = forward_iterator<int*>;
using Sentinel = sentinel_wrapper<Iterator>;
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 <typename View>
constexpr void compareViews(View v, std::initializer_list<int> 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 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ constexpr bool test() {
{
auto result = std::views::concat(v1);
auto begin_iter = result.begin();

assert(begin_iter[0] == v1[0]);
}

Expand Down

0 comments on commit 145318f

Please sign in to comment.