Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ template <typename T> auto drop_end(T &&RangeOrContainer, size_t N = 1) {
std::prev(adl_end(RangeOrContainer), N));
}

/// Return a range covering \p RangeOrContainer with the first N elements
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what happens when the range has fewer than N elements?

/// included.
template <typename T> auto take_begin(T &&RangeOrContainer, size_t N = 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this constexpr?

return make_range(adl_begin(RangeOrContainer),
std::next(adl_begin(RangeOrContainer), N));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for random access iterators, but for forward or input iterators we should not be traversing the container twice. Instead, it would be better to have an iterator type that maintains a counter.
(Note that drop_begin doesn't suffer from the same issue because we don't come back to the front of the range.)

}

/// Return a range covering \p RangeOrContainer with the last N elements
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, specify what happens when the range has fewer elements than N

/// included.
template <typename T> auto take_end(T &&RangeOrContainer, size_t N = 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this constexpr?

return make_range(std::prev(adl_end(RangeOrContainer), N),
adl_end(RangeOrContainer));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: query end only once

Otherwise this implementation seems fine -- I don't think we can do much better

}

// mapped_iterator - This is a simple iterator adapter that causes a function to
// be applied whenever operator* is invoked on the iterator.

Expand Down
29 changes: 29 additions & 0 deletions llvm/unittests/ADT/STLExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,35 @@ TEST(STLExtrasTest, DropEndDefaultTest) {
EXPECT_THAT(drop_end(vec), ElementsAre(0, 1, 2, 3));
}

TEST(STLExtrasTest, TakeBeginTest) {
SmallVector<int, 5> vec{0, 1, 2, 3, 4};

for (int n = 0; n < 5; ++n) {
EXPECT_THAT(take_begin(vec, n), ElementsAreArray(ArrayRef(vec.data(), n)));
}
}

TEST(STLExtrasTest, TakeBeginDefaultTest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would combine this with the previous function

SmallVector<int, 5> vec{0, 1, 2, 3, 4};

EXPECT_THAT(take_begin(vec), ElementsAre(0));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case with an input iterator?


TEST(STLExtrasTest, TakeEndTest) {
SmallVector<int, 5> vec{0, 1, 2, 3, 4};

for (int n = 0; n < 5; ++n) {
EXPECT_THAT(take_end(vec, n),
ElementsAreArray(ArrayRef(&vec[vec.size() - n], n)));
}
}

TEST(STLExtrasTest, TakeEndDefaultTest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here

SmallVector<int, 5> vec{0, 1, 2, 3, 4};

EXPECT_THAT(take_end(vec), ElementsAre(4));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a testcase with a bidirectional iterator?


TEST(STLExtrasTest, MapRangeTest) {
SmallVector<int, 5> Vec{0, 1, 2};
EXPECT_THAT(map_range(Vec, [](int V) { return V + 1; }),
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.