diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -870,19 +870,21 @@ } // end namespace detail -/// zip iterator for two or more iteratable types. +/// zip iterator for two or more iteratable types. Iteration continues until the +/// end of the *shortest* iteratee is reached. template detail::zippy zip(T &&t, U &&u, - Args &&... args) { + Args &&...args) { return detail::zippy( std::forward(t), std::forward(u), std::forward(args)...); } /// zip iterator that, for the sake of efficiency, assumes the first iteratee to -/// be the shortest. +/// be the shortest. Iteration continues until the end of the first iteratee is +/// reached. template detail::zippy zip_first(T &&t, U &&u, - Args &&... args) { + Args &&...args) { return detail::zippy( std::forward(t), std::forward(u), std::forward(args)...); } diff --git a/llvm/unittests/ADT/IteratorTest.cpp b/llvm/unittests/ADT/IteratorTest.cpp --- a/llvm/unittests/ADT/IteratorTest.cpp +++ b/llvm/unittests/ADT/IteratorTest.cpp @@ -395,16 +395,25 @@ const SmallVector pi{3, 1, 4, 1, 5, 9}; SmallVector odd{1, 1, 0, 1, 1, 1}; const char message[] = "yynyyy\0"; + std::array shortArr = {42, 43}; for (auto tup : zip(pi, odd, message)) { EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup)); EXPECT_EQ(get<0>(tup) & 0x01 ? 'y' : 'n', get<2>(tup)); } - // note the rvalue + // Note the rvalue. for (auto tup : zip(pi, SmallVector{1, 1, 0, 1, 1})) { EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup)); } + + // Iterate until we run out elements in the *shortest* range. + for (auto [idx, elem] : enumerate(zip(odd, shortArr))) { + EXPECT_LT(idx, static_cast(2)); + } + for (auto [idx, elem] : enumerate(zip(shortArr, odd))) { + EXPECT_LT(idx, static_cast(2)); + } } TEST(ZipIteratorTest, ZipFirstBasic) {