[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 14:12:56 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL279323: [ADT] add pointer_iterator, the opposite of pointee_iterator (authored by timshen).

Changed prior to commit:
  https://reviews.llvm.org/D23703?vs=68726&id=68729#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D23703

Files:
  llvm/trunk/include/llvm/ADT/iterator.h
  llvm/trunk/unittests/Support/IteratorTest.cpp

Index: llvm/trunk/include/llvm/ADT/iterator.h
===================================================================
--- llvm/trunk/include/llvm/ADT/iterator.h
+++ llvm/trunk/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
Index: llvm/trunk/unittests/Support/IteratorTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/IteratorTest.cpp
+++ llvm/trunk/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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23703.68729.patch
Type: text/x-patch
Size: 1792 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160819/8d8916e8/attachment.bin>


More information about the llvm-commits mailing list