[PATCH] D23703: [ADT] Add pointer_iterator, the opposite of pointee_iterator
Tim Shen via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 19 13:39:22 PDT 2016
timshen updated this revision to Diff 68726.
timshen added a comment.
Locally cache the pointer, so that we can return a reference from operator*().
https://reviews.llvm.org/D23703
Files:
include/llvm/ADT/iterator.h
unittests/Support/IteratorTest.cpp
Index: unittests/Support/IteratorTest.cpp
===================================================================
--- unittests/Support/IteratorTest.cpp
+++ unittests/Support/IteratorTest.cpp
@@ -185,4 +185,28 @@
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
Index: include/llvm/ADT/iterator.h
===================================================================
--- include/llvm/ADT/iterator.h
+++ include/llvm/ADT/iterator.h
@@ -256,6 +256,23 @@
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23703.68726.patch
Type: text/x-patch
Size: 1726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160819/5ddc2107/attachment.bin>
More information about the llvm-commits
mailing list