[PATCH] D112981: Fix iterator_adaptor_base/enumerator_iter to allow composition of llvm::enumerate with llvm::make_filter_range

Mehdi AMINI via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 1 21:54:00 PDT 2021


mehdi_amini created this revision.
mehdi_amini added a reviewer: rriddle.
Herald added a subscriber: dexonsmith.
mehdi_amini requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112981

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


Index: llvm/unittests/ADT/IteratorTest.cpp
===================================================================
--- llvm/unittests/ADT/IteratorTest.cpp
+++ llvm/unittests/ADT/IteratorTest.cpp
@@ -178,6 +178,16 @@
   EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
 }
 
+TEST(FilterIteratorTest, Enumerate) {
+  auto IsOdd = [](auto N) { return N.value() % 2 == 1; };
+  int A[] = {0, 1, 2, 3, 4, 5, 6};
+  auto Enumerate = llvm::enumerate(A);
+  SmallVector<int> Actual;
+  for (auto IndexedValue : make_filter_range(Enumerate, IsOdd))
+    Actual.push_back(IndexedValue.value());
+  EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
+}
+
 TEST(FilterIteratorTest, CallableObject) {
   int Counter = 0;
   struct Callable {
Index: llvm/include/llvm/ADT/iterator.h
===================================================================
--- llvm/include/llvm/ADT/iterator.h
+++ llvm/include/llvm/ADT/iterator.h
@@ -296,7 +296,8 @@
     return LHS.I < RHS.I;
   }
 
-  ReferenceT operator*() const { return *I; }
+  const ReferenceT operator*() const { return *I; }
+  ReferenceT operator*() { return *I; }
 };
 
 /// An iterator type that allows iterating over the pointees via some
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1907,8 +1907,7 @@
     : public iterator_facade_base<
           enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
           typename std::iterator_traits<IterOfRange<R>>::difference_type,
-          typename std::iterator_traits<IterOfRange<R>>::pointer,
-          typename std::iterator_traits<IterOfRange<R>>::reference> {
+          typename std::iterator_traits<IterOfRange<R>>::pointer> {
   using result_type = result_pair<R>;
 
 public:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112981.383963.patch
Type: text/x-patch
Size: 1830 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211102/1d3faa5e/attachment.bin>


More information about the llvm-commits mailing list