[llvm] r283765 - [ADT] Add make_pointe{e,r}_iterator.
Justin Lebar via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 10 09:26:03 PDT 2016
Author: jlebar
Date: Mon Oct 10 11:26:03 2016
New Revision: 283765
URL: http://llvm.org/viewvc/llvm-project?rev=283765&view=rev
Log:
[ADT] Add make_pointe{e,r}_iterator.
Reviewers: timshen
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D25418
Modified:
llvm/trunk/include/llvm/ADT/iterator.h
llvm/trunk/unittests/ADT/IteratorTest.cpp
Modified: llvm/trunk/include/llvm/ADT/iterator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/iterator.h?rev=283765&r1=283764&r2=283765&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/iterator.h (original)
+++ llvm/trunk/include/llvm/ADT/iterator.h Mon Oct 10 11:26:03 2016
@@ -256,6 +256,11 @@ struct pointee_iterator
T &operator*() const { return **this->I; }
};
+template <typename WrappedIteratorT>
+pointee_iterator<WrappedIteratorT> make_pointee_iterator(WrappedIteratorT &&I) {
+ return pointee_iterator<WrappedIteratorT>(std::forward<WrappedIteratorT>(I));
+}
+
template <typename WrappedIteratorT,
typename T = decltype(&*std::declval<WrappedIteratorT>())>
class pointer_iterator
@@ -273,6 +278,11 @@ public:
const T &operator*() const { return Ptr = &*this->I; }
};
+template <typename WrappedIteratorT>
+pointer_iterator<WrappedIteratorT> make_pointer_iterator(WrappedIteratorT &&I) {
+ return pointer_iterator<WrappedIteratorT>(std::forward<WrappedIteratorT>(I));
}
+} // namespace llvm
+
#endif
Modified: llvm/trunk/unittests/ADT/IteratorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/IteratorTest.cpp?rev=283765&r1=283764&r2=283765&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/IteratorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/IteratorTest.cpp Mon Oct 10 11:26:03 2016
@@ -42,19 +42,20 @@ TEST(PointeeIteratorTest, Basic) {
V.push_back(&arr[2]);
V.push_back(&arr[3]);
- typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator> test_iterator;
+ auto Begin = make_pointee_iterator(V.begin());
+ auto End = make_pointee_iterator(V.end());
+ static_assert(
+ std::is_same<decltype(Begin),
+ pointee_iterator<SmallVectorImpl<int *>::iterator>>::value,
+ "Wrong type returned by make_pointee_iterator");
- test_iterator Begin, End;
- Begin = V.begin();
- End = test_iterator(V.end());
-
- test_iterator I = Begin;
+ auto I = Begin;
for (int i = 0; i < 4; ++i) {
EXPECT_EQ(*V[i], *I);
EXPECT_EQ(I, Begin + i);
EXPECT_EQ(I, std::next(Begin, i));
- test_iterator J = Begin;
+ auto J = Begin;
J += i;
EXPECT_EQ(I, J);
EXPECT_EQ(*V[i], Begin[i]);
@@ -69,7 +70,7 @@ TEST(PointeeIteratorTest, Basic) {
EXPECT_EQ(i, std::distance(Begin, I));
EXPECT_EQ(Begin, I - i);
- test_iterator K = I++;
+ auto K = I++;
EXPECT_EQ(K, std::prev(I));
}
EXPECT_EQ(End, I);
@@ -82,20 +83,21 @@ TEST(PointeeIteratorTest, SmartPointer)
V.push_back(make_unique<int>(3));
V.push_back(make_unique<int>(4));
- typedef pointee_iterator<
- SmallVectorImpl<std::unique_ptr<int>>::const_iterator> test_iterator;
-
- test_iterator Begin, End;
- Begin = V.begin();
- End = test_iterator(V.end());
+ auto Begin = make_pointee_iterator(V.begin());
+ auto End = make_pointee_iterator(V.end());
+ static_assert(
+ std::is_same<decltype(Begin),
+ pointee_iterator<
+ SmallVectorImpl<std::unique_ptr<int>>::iterator>>::value,
+ "Wrong type returned by make_pointee_iterator");
- test_iterator I = Begin;
+ auto I = Begin;
for (int i = 0; i < 4; ++i) {
EXPECT_EQ(*V[i], *I);
EXPECT_EQ(I, Begin + i);
EXPECT_EQ(I, std::next(Begin, i));
- test_iterator J = Begin;
+ auto J = Begin;
J += i;
EXPECT_EQ(I, J);
EXPECT_EQ(*V[i], Begin[i]);
@@ -110,7 +112,7 @@ TEST(PointeeIteratorTest, SmartPointer)
EXPECT_EQ(i, std::distance(Begin, I));
EXPECT_EQ(Begin, I - i);
- test_iterator K = I++;
+ auto K = I++;
EXPECT_EQ(K, std::prev(I));
}
EXPECT_EQ(End, I);
@@ -187,7 +189,10 @@ TEST(FilterIteratorTest, InputIterator)
TEST(PointerIterator, Basic) {
int A[] = {1, 2, 3, 4};
- pointer_iterator<int *> Begin(std::begin(A)), End(std::end(A));
+ auto Begin = make_pointer_iterator(std::begin(A));
+ auto End = make_pointer_iterator(std::end(A));
+ static_assert(std::is_same<decltype(Begin), pointer_iterator<int *>>::value,
+ "Wrong type returned by make_pointer_iterator");
EXPECT_EQ(A, *Begin);
++Begin;
EXPECT_EQ(A + 1, *Begin);
@@ -201,7 +206,7 @@ TEST(PointerIterator, Basic) {
TEST(PointerIterator, Const) {
int A[] = {1, 2, 3, 4};
- const pointer_iterator<int *> Begin(std::begin(A));
+ auto Begin = make_pointer_iterator(std::begin(A));
EXPECT_EQ(A, *Begin);
EXPECT_EQ(A + 1, std::next(*Begin, 1));
EXPECT_EQ(A + 2, std::next(*Begin, 2));
More information about the llvm-commits
mailing list