Skip to content

Commit

Permalink
Fixes cursor iteration for set_union
Browse files Browse the repository at this point in the history
last() must return a cursor with cursor_type::second otherwise comparing
against a cursor obtain via successive inc() calls fails.

This change also allows flux::to work with set_union.
  • Loading branch information
Jon authored and Jon committed Jul 8, 2023
1 parent b4b931b commit 434ec01
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions include/flux/op/set_adaptors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ struct set_union_adaptor
bounded_sequence<Base1> && bounded_sequence<Base2>
static constexpr auto last(Self& self) -> cursor_type
{
return cursor_type{flux::last(self.base1_), flux::last(self.base2_)};
return cursor_type{flux::last(self.base1_), flux::last(self.base2_), cursor_type::second};
}

template <typename Self>
Expand Down Expand Up @@ -558,4 +558,4 @@ inline constexpr auto set_intersection = detail::set_intersection_fn{};

} // namespace flux

#endif // namespace FLUX_OP_SET_ADAPTORS_HPP_INCLUDED
#endif // namespace FLUX_OP_SET_ADAPTORS_HPP_INCLUDED
16 changes: 15 additions & 1 deletion test/test_set_adaptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ constexpr bool test_set_union()
STATIC_CHECK(check_equal(union_seq, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}));
}

// test cursor iteration
{
int arr1[] = {0, 2, 4, 6};
int arr2[] = { 1, 3, 5};
auto union_seq = flux::set_union(flux::ref(arr1), flux::ref(arr2));

auto first = flux::first(union_seq);
auto last = flux::last(union_seq);
while (first != last) {
flux::inc(union_seq, first);
}
STATIC_CHECK(first == last);
}

return true;
}

Expand Down Expand Up @@ -618,4 +632,4 @@ TEST_CASE("set_intersection")
{
bool result = test_set_intersection();
REQUIRE(result);
}
}
10 changes: 9 additions & 1 deletion test/test_to.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ TEST_CASE("to")

CHECK(check_equal(vec, {"The", "quick", "brown", "fox"}));
}

SECTION("from set_union adaptor")
{
auto union_seq = flux::set_union(std::array{1,2,3}, std::array{4,5});
auto vec = flux::to<std::vector<int>>(union_seq);

CHECK(check_equal(vec, {1,2,3,4,5}));
}
}

SECTION("...using CTAD")
Expand Down Expand Up @@ -340,4 +348,4 @@ TEST_CASE("to")
}
}
}
}
}

0 comments on commit 434ec01

Please sign in to comment.