[llvm] r279323 - [ADT] add pointer_iterator, the opposite of pointee_iterator
Tim Shen via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 19 14:04:45 PDT 2016
Author: timshen
Date: Fri Aug 19 16:04:45 2016
New Revision: 279323
URL: http://llvm.org/viewvc/llvm-project?rev=279323&view=rev
Log:
[ADT] add pointer_iterator, the opposite of pointee_iterator
Differential Revision: https://reviews.llvm.org/D23703
Modified:
llvm/trunk/include/llvm/ADT/iterator.h
llvm/trunk/unittests/Support/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=279323&r1=279322&r2=279323&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/iterator.h (original)
+++ llvm/trunk/include/llvm/ADT/iterator.h Fri Aug 19 16:04:45 2016
@@ -256,6 +256,23 @@ struct pointee_iterator
T &operator*() const { return **this->I; }
};
+template <typename WrappedIteratorT,
+ typename T = decltype(&*std::declval<WrappedIteratorT>())>
+class pointer_iterator
+ : public iterator_adaptor_base<pointer_iterator<WrappedIteratorT>,
+ WrappedIteratorT, T> {
+ mutable T Ptr;
+
+public:
+ pointer_iterator() {}
+
+ explicit pointer_iterator(WrappedIteratorT u)
+ : pointer_iterator::iterator_adaptor_base(std::move(u)) {}
+
+ T &operator*() { return Ptr = &*this->I; }
+ const T &operator*() const { return Ptr = &*this->I; }
+};
+
}
#endif
Modified: llvm/trunk/unittests/Support/IteratorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/IteratorTest.cpp?rev=279323&r1=279322&r2=279323&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/IteratorTest.cpp (original)
+++ llvm/trunk/unittests/Support/IteratorTest.cpp Fri Aug 19 16:04:45 2016
@@ -185,4 +185,28 @@ TEST(FilterIteratorTest, InputIterator)
EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
}
+TEST(PointerIterator, Basic) {
+ int A[] = {1, 2, 3, 4};
+ pointer_iterator<int *> Begin(std::begin(A)), End(std::end(A));
+ EXPECT_EQ(A, *Begin);
+ ++Begin;
+ EXPECT_EQ(A + 1, *Begin);
+ ++Begin;
+ EXPECT_EQ(A + 2, *Begin);
+ ++Begin;
+ EXPECT_EQ(A + 3, *Begin);
+ ++Begin;
+ EXPECT_EQ(Begin, End);
+}
+
+TEST(PointerIterator, Const) {
+ int A[] = {1, 2, 3, 4};
+ const pointer_iterator<int *> Begin(std::begin(A));
+ EXPECT_EQ(A, *Begin);
+ EXPECT_EQ(A + 1, std::next(*Begin, 1));
+ EXPECT_EQ(A + 2, std::next(*Begin, 2));
+ EXPECT_EQ(A + 3, std::next(*Begin, 3));
+ EXPECT_EQ(A + 4, std::next(*Begin, 4));
+}
+
} // anonymous namespace
More information about the llvm-commits
mailing list