Skip to content

Commit

Permalink
Merge pull request #151 from tcbrindle/pr/fix_150
Browse files Browse the repository at this point in the history
Reset inner_cur of (multipass) flatten_adaptor when reaching the end
  • Loading branch information
tcbrindle authored Jan 17, 2024
2 parents e942a67 + 1fc9de3 commit 0304fe6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ target_compile_options(flux-internal INTERFACE

$<$<CXX_COMPILER_ID:GNU>: -fconcepts-diagnostics-depth=2>

$<$<CXX_COMPILER_ID:Clang,AppleClang>: -fconstexpr-backtrace-limit=0>

$<$<CXX_COMPILER_ID:MSVC>:
# Various options for closer standard conformance
/utf-8 /Zc:__cplusplus /Zc:throwingNew /Zc:inline /Zc:externConstexpr
Expand Down
6 changes: 5 additions & 1 deletion include/flux/op/flatten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ struct flatten_adaptor<Base> : inline_sequence_base<flatten_adaptor<Base>> {

static constexpr auto satisfy(auto& self, cursor_type& cur) -> void
{
while (!flux::is_last(self.base_, cur.outer_cur)) {
while (true) {
if (flux::is_last(self.base_, cur.outer_cur)) {
cur.inner_cur = cursor_t<InnerSeq>{};
return;
}
auto&& inner = flux::read_at(self.base_, cur.outer_cur);
cur.inner_cur = flux::first(inner);
if (!flux::is_last(inner, cur.inner_cur)) {
Expand Down
32 changes: 32 additions & 0 deletions test/test_flatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,39 @@ constexpr bool test_flatten_multipass()
}
#endif // NO_CONSTEXPR_VECTOR

// Iterating to the end gives last()
{
std::array<std::string_view, 3> arr{"a", "b", "c"};

auto seq = flux::flatten(arr);

auto cur = seq.first();
while (!seq.is_last(cur)) {
seq.inc(cur);
}

STATIC_CHECK(cur == seq.last());
}

return true;
}
static_assert(test_flatten_multipass());

#ifndef NO_CONSTEXPR_VECTOR
constexpr
#endif
bool issue_150()
{
const std::vector<std::string> vec{"a", "b", "c"};

auto str = flux::ref(vec).flatten().to<std::string>();

return str == "abc";
}
#ifndef NO_CONSTEXPR_VECTOR
static_assert(issue_150());
#endif

}

TEST_CASE("flatten")
Expand All @@ -239,4 +268,7 @@ TEST_CASE("flatten")

bool mp = test_flatten_multipass();
REQUIRE(mp);

bool res = issue_150();
REQUIRE(res);
}

0 comments on commit 0304fe6

Please sign in to comment.