Skip to content

Commit

Permalink
Fix zip/zip_with distance_to (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
PkXwmpgN authored May 23, 2023
1 parent c3f0d17 commit 5ac35e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion include/range/v3/view/zip_with.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ namespace ranges
{
// Return the smallest distance (in magnitude) of any of the iterator
// pairs. This is to accommodate zippers of sequences of different length.
if(0 < std::get<0>(that.its_) - std::get<0>(its_))
auto first_size = std::get<0>(that.its_) - std::get<0>(its_);
if(first_size == 0)
return static_cast<difference_type>(0);

if(0 < first_size)
return tuple_foldl(
tuple_transform(its_, that.its_, detail::distance_to),
(std::numeric_limits<difference_type>::max)(),
Expand Down
23 changes: 23 additions & 0 deletions test/view/zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,29 @@ int main()

has_cardinality<cardinality::unknown>(rng);
}

{
std::vector<int> v0{1, 2, 3};
std::vector<int> v1{};

auto rng0 = views::zip(v0, v1);
auto rng1 = views::zip(v1, v0);

CHECK(ranges::distance(ranges::begin(rng0), ranges::end(rng0)) == 0);
CHECK(ranges::distance(ranges::begin(rng1), ranges::end(rng1)) == 0);
CHECK(ranges::distance(ranges::end(rng0), ranges::begin(rng0)) == 0);
CHECK(ranges::distance(ranges::end(rng1), ranges::begin(rng1)) == 0);
}

{
std::vector<int> v0{1, 2, 3};
std::vector<int> v1{1, 2, 3, 4, 5};

auto rng = views::zip(v0, v1);

CHECK(ranges::distance(ranges::begin(rng), ranges::end(rng)) == 3);
CHECK(ranges::distance(ranges::end(rng), ranges::begin(rng)) == -3);
}

return test_result();
}

0 comments on commit 5ac35e4

Please sign in to comment.