[PATCH] D23703: [ADT] Add pointer_iterator, the opposite of pointee_iterator
Tim Shen via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 18 22:25:07 PDT 2016
timshen created this revision.
timshen added a reviewer: dblaikie.
timshen added a subscriber: llvm-commits.
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,18 @@
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);
+}
+
} // anonymous namespace
Index: include/llvm/ADT/iterator.h
===================================================================
--- include/llvm/ADT/iterator.h
+++ include/llvm/ADT/iterator.h
@@ -256,6 +256,19 @@
T &operator*() const { return **this->I; }
};
+template <typename WrappedIteratorT,
+ typename T = decltype(&*std::declval<WrappedIteratorT>())>
+struct pointer_iterator
+ : iterator_adaptor_base<pointer_iterator<WrappedIteratorT>,
+ WrappedIteratorT, T> {
+ pointer_iterator() {}
+
+ explicit pointer_iterator(WrappedIteratorT u)
+ : pointer_iterator::iterator_adaptor_base(std::move(u)) {}
+
+ T operator*() const { return &*this->I; }
+};
+
}
#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23703.68647.patch
Type: text/x-patch
Size: 1309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160819/d0c3f5bc/attachment.bin>
More information about the llvm-commits
mailing list