llvm.org GIT mirror llvm / acec6ed
ADT: Add range helpers for pointer_ and pointee_iterator git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298841 91177308-0d34-0410-b5e6-96231b3b80d8 Justin Bogner 3 years ago
2 changed file(s) with 35 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
99 #ifndef LLVM_ADT_ITERATOR_H
1010 #define LLVM_ADT_ITERATOR_H
1111
12 #include "llvm/ADT/iterator_range.h"
1213 #include
1314 #include
1415 #include
289290 T &operator*() const { return **this->I; }
290291 };
291292
293 template
294 decltype(std::begin(std::declval()))>
295 iterator_range>
296 make_pointee_range(RangeT &&Range) {
297 using PointeeIteratorT = pointee_iterator;
298 return make_range(PointeeIteratorT(std::begin(std::forward(Range))),
299 PointeeIteratorT(std::end(std::forward(Range))));
300 }
301
292302 template
293303 typename T = decltype(&*std::declval())>
294304 class pointer_iterator
306316 const T &operator*() const { return Ptr = &*this->I; }
307317 };
308318
319 template
320 decltype(std::begin(std::declval()))>
321 iterator_range>
322 make_pointer_range(RangeT &&Range) {
323 using PointerIteratorT = pointer_iterator;
324 return make_range(PointerIteratorT(std::begin(std::forward(Range))),
325 PointerIteratorT(std::end(std::forward(Range))));
326 }
327
309328 } // end namespace llvm
310329
311330 #endif // LLVM_ADT_ITERATOR_H
115115 EXPECT_EQ(K, std::prev(I));
116116 }
117117 EXPECT_EQ(End, I);
118 }
119
120 TEST(PointeeIteratorTest, Range) {
121 int A[] = {1, 2, 3, 4};
122 SmallVector V{&A[0], &A[1], &A[2], &A[3]};
123
124 int I = 0;
125 for (int II : make_pointee_range(V))
126 EXPECT_EQ(A[I++], II);
118127 }
119128
120129 TEST(FilterIteratorTest, Lambda) {
210219 EXPECT_EQ(A + 4, std::next(*Begin, 4));
211220 }
212221
222 TEST(PointerIterator, Range) {
223 int A[] = {1, 2, 3, 4};
224 int I = 0;
225 for (int *P : make_pointer_range(A))
226 EXPECT_EQ(A + I++, P);
227 }
228
213229 TEST(ZipIteratorTest, Basic) {
214230 using namespace std;
215231 const SmallVector pi{3, 1, 4, 1, 5, 9};