Skip to content

Commit aa7a3d4

Browse files
committed
[ADT] Clarify zip behavior with iteratees of different lengths
Update the documentation comment and add a new test case. Add an assertion in `zip_first` checking the iteratee length precondition. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D138858
1 parent 26e44d4 commit aa7a3d4

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,19 +870,27 @@ template <template <typename...> class ItType, typename... Args> class zippy {
870870

871871
} // end namespace detail
872872

873-
/// zip iterator for two or more iteratable types.
873+
/// zip iterator for two or more iteratable types. Iteration continues until the
874+
/// end of the *shortest* iteratee is reached.
874875
template <typename T, typename U, typename... Args>
875876
detail::zippy<detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u,
876-
Args &&... args) {
877+
Args &&...args) {
877878
return detail::zippy<detail::zip_shortest, T, U, Args...>(
878879
std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
879880
}
880881

881882
/// zip iterator that, for the sake of efficiency, assumes the first iteratee to
882-
/// be the shortest.
883+
/// be the shortest. Iteration continues until the end of the first iteratee is
884+
/// reached. In builds with assertions on, we check that the assumption about
885+
/// the first iteratee being the shortest holds.
883886
template <typename T, typename U, typename... Args>
884887
detail::zippy<detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u,
885-
Args &&... args) {
888+
Args &&...args) {
889+
assert(std::distance(adl_begin(t), adl_end(t)) <=
890+
std::min({std::distance(adl_begin(u), adl_end(u)),
891+
std::distance(adl_begin(args), adl_end(args))...}) &&
892+
"First iteratee is not the shortest");
893+
886894
return detail::zippy<detail::zip_first, T, U, Args...>(
887895
std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
888896
}

llvm/unittests/ADT/IteratorTest.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,25 @@ TEST(ZipIteratorTest, Basic) {
395395
const SmallVector<unsigned, 6> pi{3, 1, 4, 1, 5, 9};
396396
SmallVector<bool, 6> odd{1, 1, 0, 1, 1, 1};
397397
const char message[] = "yynyyy\0";
398+
std::array<int, 2> shortArr = {42, 43};
398399

399400
for (auto tup : zip(pi, odd, message)) {
400401
EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup));
401402
EXPECT_EQ(get<0>(tup) & 0x01 ? 'y' : 'n', get<2>(tup));
402403
}
403404

404-
// note the rvalue
405+
// Note the rvalue.
405406
for (auto tup : zip(pi, SmallVector<bool, 0>{1, 1, 0, 1, 1})) {
406407
EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup));
407408
}
409+
410+
// Iterate until we run out elements in the *shortest* range.
411+
for (auto [idx, elem] : enumerate(zip(odd, shortArr))) {
412+
EXPECT_LT(idx, static_cast<size_t>(2));
413+
}
414+
for (auto [idx, elem] : enumerate(zip(shortArr, odd))) {
415+
EXPECT_LT(idx, static_cast<size_t>(2));
416+
}
408417
}
409418

410419
TEST(ZipIteratorTest, ZipFirstBasic) {
@@ -420,6 +429,21 @@ TEST(ZipIteratorTest, ZipFirstBasic) {
420429
EXPECT_EQ(iters, 4u);
421430
}
422431

432+
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
433+
// Make sure that we can detect when the first range is not the shortest.
434+
TEST(ZipIteratorTest, ZipFirstNotShortest) {
435+
const std::array<unsigned, 6> longer = {};
436+
const std::array<unsigned, 4> shorter = {};
437+
438+
EXPECT_DEATH(zip_first(longer, shorter),
439+
"First iteratee is not the shortest");
440+
EXPECT_DEATH(zip_first(longer, shorter, longer),
441+
"First iteratee is not the shortest");
442+
EXPECT_DEATH(zip_first(longer, longer, shorter),
443+
"First iteratee is not the shortest");
444+
}
445+
#endif
446+
423447
TEST(ZipIteratorTest, ZipLongestBasic) {
424448
using namespace std;
425449
const vector<unsigned> pi{3, 1, 4, 1, 5, 9};

0 commit comments

Comments
 (0)