[llvm] r298841 - ADT: Add range helpers for pointer_ and pointee_iterator
Justin Bogner via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 27 05:56:12 PDT 2017
Author: bogner
Date: Mon Mar 27 07:56:12 2017
New Revision: 298841
URL: http://llvm.org/viewvc/llvm-project?rev=298841&view=rev
Log:
ADT: Add range helpers for pointer_ and pointee_iterator
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=298841&r1=298840&r2=298841&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/iterator.h (original)
+++ llvm/trunk/include/llvm/ADT/iterator.h Mon Mar 27 07:56:12 2017
@@ -10,6 +10,7 @@
#ifndef LLVM_ADT_ITERATOR_H
#define LLVM_ADT_ITERATOR_H
+#include "llvm/ADT/iterator_range.h"
#include <cstddef>
#include <iterator>
#include <type_traits>
@@ -290,6 +291,15 @@ struct pointee_iterator
T &operator*() const { return **this->I; }
};
+template <typename RangeT, typename WrappedIteratorT =
+ decltype(std::begin(std::declval<RangeT>()))>
+iterator_range<pointee_iterator<WrappedIteratorT>>
+make_pointee_range(RangeT &&Range) {
+ using PointeeIteratorT = pointee_iterator<WrappedIteratorT>;
+ return make_range(PointeeIteratorT(std::begin(std::forward<RangeT>(Range))),
+ PointeeIteratorT(std::end(std::forward<RangeT>(Range))));
+}
+
template <typename WrappedIteratorT,
typename T = decltype(&*std::declval<WrappedIteratorT>())>
class pointer_iterator
@@ -307,6 +317,15 @@ public:
const T &operator*() const { return Ptr = &*this->I; }
};
+template <typename RangeT, typename WrappedIteratorT =
+ decltype(std::begin(std::declval<RangeT>()))>
+iterator_range<pointer_iterator<WrappedIteratorT>>
+make_pointer_range(RangeT &&Range) {
+ using PointerIteratorT = pointer_iterator<WrappedIteratorT>;
+ return make_range(PointerIteratorT(std::begin(std::forward<RangeT>(Range))),
+ PointerIteratorT(std::end(std::forward<RangeT>(Range))));
+}
+
} // end namespace llvm
#endif // LLVM_ADT_ITERATOR_H
Modified: llvm/trunk/unittests/ADT/IteratorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/IteratorTest.cpp?rev=298841&r1=298840&r2=298841&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/IteratorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/IteratorTest.cpp Mon Mar 27 07:56:12 2017
@@ -118,6 +118,15 @@ TEST(PointeeIteratorTest, SmartPointer)
EXPECT_EQ(End, I);
}
+TEST(PointeeIteratorTest, Range) {
+ int A[] = {1, 2, 3, 4};
+ SmallVector<int *, 4> V{&A[0], &A[1], &A[2], &A[3]};
+
+ int I = 0;
+ for (int II : make_pointee_range(V))
+ EXPECT_EQ(A[I++], II);
+}
+
TEST(FilterIteratorTest, Lambda) {
auto IsOdd = [](int N) { return N % 2 == 1; };
int A[] = {0, 1, 2, 3, 4, 5, 6};
@@ -211,6 +220,13 @@ TEST(PointerIterator, Const) {
EXPECT_EQ(A + 4, std::next(*Begin, 4));
}
+TEST(PointerIterator, Range) {
+ int A[] = {1, 2, 3, 4};
+ int I = 0;
+ for (int *P : make_pointer_range(A))
+ EXPECT_EQ(A + I++, P);
+}
+
TEST(ZipIteratorTest, Basic) {
using namespace std;
const SmallVector<unsigned, 6> pi{3, 1, 4, 1, 5, 9};
More information about the llvm-commits
mailing list