-
Couldn't load subscription status.
- Fork 15k
[STLExtras] Add take_begin and take_end range adaptors.
#164542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: users/vitalybuka/spr/main.stlextras-add-take_begin-and-take_end-range-adaptors
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| /// included. | ||
| template <typename T> auto take_begin(T &&RangeOrContainer, size_t N = 1) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we make this |
||
| return make_range(adl_begin(RangeOrContainer), | ||
| std::next(adl_begin(RangeOrContainer), N)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
|
||
| /// Return a range covering \p RangeOrContainer with the last N elements | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; }), | ||
|
|
||
There was a problem hiding this comment.
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?