[llvm] 058bc4c - [ADT] Move drop_begin from iterator_range.h into STLExtras.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 14 08:12:04 PST 2019
Author: Lang Hames
Date: 2019-11-14T08:10:59-08:00
New Revision: 058bc4c8d48a1f3644845f4795832391bc5eb9de
URL: https://github.com/llvm/llvm-project/commit/058bc4c8d48a1f3644845f4795832391bc5eb9de
DIFF: https://github.com/llvm/llvm-project/commit/058bc4c8d48a1f3644845f4795832391bc5eb9de.diff
LOG: [ADT] Move drop_begin from iterator_range.h into STLExtras.
Summary:
drop_begin depends on adl_begin/adl_end, which are defined in STLExtras.h,
but we can't just #include STLExtras.h in iterator_range.h as that would
introduce a circular reference (STLExtras.h already depends on
iterator_range.h). The simplest solution is to move drop_begin into
STLExtras.h, which is a reasonable home for it anyway.
Reviewers: dblaikie
Subscribers: dexonsmith, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70189
Added:
Modified:
llvm/include/llvm/ADT/STLExtras.h
llvm/include/llvm/ADT/iterator_range.h
llvm/unittests/ADT/IteratorTest.cpp
llvm/unittests/ADT/STLExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 274933bc5204..7c121238b698 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -198,6 +198,15 @@ constexpr bool empty(const T &RangeOrContainer) {
return adl_begin(RangeOrContainer) == adl_end(RangeOrContainer);
}
+/// Return a range covering \p RangeOrContainer with the first N elements
+/// excluded.
+template <typename T>
+auto drop_begin(T &&RangeOrContainer, size_t N) ->
+ iterator_range<decltype(adl_begin(RangeOrContainer))> {
+ return make_range(std::next(adl_begin(RangeOrContainer), N),
+ adl_end(RangeOrContainer));
+}
+
// mapped_iterator - This is a simple iterator adapter that causes a function to
// be applied whenever operator* is invoked on the iterator.
diff --git a/llvm/include/llvm/ADT/iterator_range.h b/llvm/include/llvm/ADT/iterator_range.h
index aa8830943cab..f038f6bf2128 100644
--- a/llvm/include/llvm/ADT/iterator_range.h
+++ b/llvm/include/llvm/ADT/iterator_range.h
@@ -59,11 +59,6 @@ template <typename T> iterator_range<T> make_range(std::pair<T, T> p) {
return iterator_range<T>(std::move(p.first), std::move(p.second));
}
-template <typename T>
-iterator_range<decltype(adl_begin(std::declval<T>()))> drop_begin(T &&t,
- int n) {
- return make_range(std::next(adl_begin(t), n), adl_end(t));
-}
}
#endif
diff --git a/llvm/unittests/ADT/IteratorTest.cpp b/llvm/unittests/ADT/IteratorTest.cpp
index 73fd15e90e9c..de74811321a9 100644
--- a/llvm/unittests/ADT/IteratorTest.cpp
+++ b/llvm/unittests/ADT/IteratorTest.cpp
@@ -449,17 +449,4 @@ TEST(RangeTest, Distance) {
EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
}
-TEST(IteratorRangeTest, DropBegin) {
- SmallVector<int, 5> vec{0, 1, 2, 3, 4};
-
- for (int n = 0; n < 5; ++n) {
- int i = n;
- for (auto &v : drop_begin(vec, n)) {
- EXPECT_EQ(v, i);
- i += 1;
- }
- EXPECT_EQ(i, 5);
- }
-}
-
} // anonymous namespace
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 4cbef904ca81..d0220e309fef 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -380,6 +380,19 @@ TEST(STLExtrasTest, EmptyTest) {
EXPECT_FALSE(llvm::empty(R1));
}
+TEST(STLExtrasTest, DropBeginTest) {
+ SmallVector<int, 5> vec{0, 1, 2, 3, 4};
+
+ for (int n = 0; n < 5; ++n) {
+ int i = n;
+ for (auto &v : drop_begin(vec, n)) {
+ EXPECT_EQ(v, i);
+ i += 1;
+ }
+ EXPECT_EQ(i, 5);
+ }
+}
+
TEST(STLExtrasTest, EarlyIncrementTest) {
std::list<int> L = {1, 2, 3, 4};
More information about the llvm-commits
mailing list