summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
authorDean Michael Berris <dberris@google.com>2017-01-24 04:11:18 +0000
committerDean Michael Berris <dberris@google.com>2017-01-24 04:11:18 +0000
commit227c6ebc663bffc48b8ac191d00b4d06ca27a53e (patch)
tree7d92a8913520c123e68cd8e812674766df635ed0 /llvm
parentb2a23cf3c08cee45614f27eb2c6d044e506aa6a6 (diff)
downloadbcm5719-llvm-227c6ebc663bffc48b8ac191d00b4d06ca27a53e.tar.gz
bcm5719-llvm-227c6ebc663bffc48b8ac191d00b4d06ca27a53e.zip
Allow DenseSet::iterators to be conveted to and compared with const_iterator
Summary: This seemed to be an oversight seeing as DenseMap has these conversions. This patch does the following: - Adds a default constructor to the iterators. - Allows DenseSet::ConstIterators to be copy constructed from DenseSet::Iterators - Allows mutual comparison between Iterators and ConstIterators. All of these are available in the DenseMap implementation, so the implementation here is trivial. Reviewers: dblaikie, dberris Reviewed By: dberris Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D28999 llvm-svn: 292879
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/ADT/DenseSet.h13
-rw-r--r--llvm/unittests/ADT/DenseSetTest.cpp9
2 files changed, 20 insertions, 2 deletions
diff --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h
index 0e26a9d42e8..b1345f7da73 100644
--- a/llvm/include/llvm/ADT/DenseSet.h
+++ b/llvm/include/llvm/ADT/DenseSet.h
@@ -90,9 +90,12 @@ public:
// Iterators.
+ class ConstIterator;
+
class Iterator {
typename MapTy::iterator I;
friend class DenseSetImpl;
+ friend class ConstIterator;
public:
typedef typename MapTy::iterator::difference_type difference_type;
@@ -101,6 +104,7 @@ public:
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,13 +114,14 @@ public:
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;
@@ -125,6 +130,10 @@ public:
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(); }
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index 4d5a82902f0..4b159744eba 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -73,6 +73,15 @@ TYPED_TEST(DenseSetTest, InitializerList) {
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());
OpenPOWER on IntegriCloud