[llvm] ba7a6b3 - Fix iterator_adaptor_base/enumerator_iter to allow composition of llvm::enumerate with llvm::make_filter_range

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 2 15:58:27 PDT 2021


Author: Mehdi Amini
Date: 2021-11-02T22:49:43Z
New Revision: ba7a6b314fd14bb2c9ff5d3f4fe2b6525514cada

URL: https://github.com/llvm/llvm-project/commit/ba7a6b314fd14bb2c9ff5d3f4fe2b6525514cada
DIFF: https://github.com/llvm/llvm-project/commit/ba7a6b314fd14bb2c9ff5d3f4fe2b6525514cada.diff

LOG: Fix iterator_adaptor_base/enumerator_iter to allow composition of llvm::enumerate with llvm::make_filter_range

* Properly specify reference type in enumerator_iter
* Fix constness of iterator_adaptor_base::operator*

Differential Revision: https://reviews.llvm.org/D112981

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 68bc656042073..a00561760bc0a 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1907,8 +1907,7 @@ class enumerator_iter
     : public iterator_facade_base<
           enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
           typename std::iterator_traits<IterOfRange<R>>::
diff erence_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:

diff  --git a/llvm/include/llvm/ADT/iterator.h b/llvm/include/llvm/ADT/iterator.h
index b9bac9d385c0b..b93aaf39c171b 100644
--- a/llvm/include/llvm/ADT/iterator.h
+++ b/llvm/include/llvm/ADT/iterator.h
@@ -296,7 +296,8 @@ class iterator_adaptor_base
     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

diff  --git a/llvm/unittests/ADT/IteratorTest.cpp b/llvm/unittests/ADT/IteratorTest.cpp
index de74811321a9a..f5fa88e7493f8 100644
--- a/llvm/unittests/ADT/IteratorTest.cpp
+++ b/llvm/unittests/ADT/IteratorTest.cpp
@@ -178,6 +178,16 @@ TEST(FilterIteratorTest, Lambda) {
   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 {


        


More information about the llvm-commits mailing list