[PATCH] D28999: Allow DenseSet::iterators to be conveted to and compared with const_iterator

Alexis Shaw via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 22 21:50:37 PST 2017


varno updated this revision to Diff 85320.
varno added a comment.

- address review comments
- add tests


https://reviews.llvm.org/D28999

Files:
  include/llvm/ADT/DenseSet.h
  unittests/ADT/DenseSetTest.cpp


Index: unittests/ADT/DenseSetTest.cpp
===================================================================
--- unittests/ADT/DenseSetTest.cpp
+++ unittests/ADT/DenseSetTest.cpp
@@ -73,6 +73,15 @@
   EXPECT_EQ(0u, set.count(3));
 }
 
+TYPED_TEST(DenseSetTest, ConstIteratorComparison){
+  TypeParam set({1});
+  const TypeParam &cset = set;
+  EXPECT_EQ(set.begin(), cset.begin());
+  EXPECT_EQ(set.end(), cset.end());
+  EXPECT_NE(set.end(), cset.begin());
+  EXPECT_NE(set.begin(), cset.end());
+}
+
 TYPED_TEST(DenseSetTest, EmptyInitializerList) {
   TypeParam set({});
   EXPECT_EQ(0u, set.size());
Index: include/llvm/ADT/DenseSet.h
===================================================================
--- include/llvm/ADT/DenseSet.h
+++ include/llvm/ADT/DenseSet.h
@@ -90,17 +90,21 @@
 
   // Iterators.
 
+  class ConstIterator;
+
   class Iterator {
     typename MapTy::iterator I;
     friend class DenseSetImpl;
+    friend class ConstIterator;
 
   public:
     typedef typename MapTy::iterator::difference_type difference_type;
     typedef ValueT value_type;
     typedef value_type *pointer;
     typedef value_type &reference;
     typedef std::forward_iterator_tag iterator_category;
 
+    Iterator() =default;
     Iterator(const typename MapTy::iterator &i) : I(i) {}
 
     ValueT &operator*() { return I->getFirst(); }
@@ -110,21 +114,26 @@
 
     Iterator& operator++() { ++I; return *this; }
     Iterator operator++(int) { auto T = *this; ++I; return T; }
-    bool operator==(const Iterator& X) const { return I == X.I; }
-    bool operator!=(const Iterator& X) const { return I != X.I; }
+    bool operator==(const ConstIterator& X) const { return I == X.I; }
+    bool operator!=(const ConstIterator& X) const { return I != X.I; }
   };
 
   class ConstIterator {
     typename MapTy::const_iterator I;
     friend class DenseSet;
+    friend class Iterator;
 
   public:
     typedef typename MapTy::const_iterator::difference_type difference_type;
     typedef ValueT value_type;
     typedef value_type *pointer;
     typedef value_type &reference;
     typedef std::forward_iterator_tag iterator_category;
 
+    ConstIterator(const Iterator &B) : I(B.I) {}
+
+    ConstIterator() = default;
+
     ConstIterator(const typename MapTy::const_iterator &i) : I(i) {}
 
     const ValueT &operator*() const { return I->getFirst(); }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28999.85320.patch
Type: text/x-patch
Size: 2359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170123/7882b443/attachment.bin>


More information about the llvm-commits mailing list