[llvm] 39000aa - [llvm] Add contains(KeyType) -> bool methods to SparseSet

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 11:26:59 PDT 2020


Author: David Blaikie
Date: 2020-07-17T11:26:27-07:00
New Revision: 39000aad81ff30b68c443087842e960d25346e88

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

LOG: [llvm] Add contains(KeyType) -> bool methods to SparseSet

Matches C++20 API addition.

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

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SparseSet.h
    llvm/unittests/ADT/SparseSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SparseSet.h b/llvm/include/llvm/ADT/SparseSet.h
index 74457d5fd679..d8acf1ee2f3a 100644
--- a/llvm/include/llvm/ADT/SparseSet.h
+++ b/llvm/include/llvm/ADT/SparseSet.h
@@ -229,12 +229,15 @@ class SparseSet {
     return const_cast<SparseSet*>(this)->findIndex(KeyIndexOf(Key));
   }
 
+  /// Check if the set contains the given \c Key.
+  ///
+  /// @param Key A valid key to find.
+  bool contains(const KeyT &Key) const { return find(Key) == end() ? 0 : 1; }
+
   /// count - Returns 1 if this set contains an element identified by Key,
   /// 0 otherwise.
   ///
-  size_type count(const KeyT &Key) const {
-    return find(Key) == end() ? 0 : 1;
-  }
+  size_type count(const KeyT &Key) const { return contains(Key) ? 1 : 0; }
 
   /// insert - Attempts to insert a new element.
   ///

diff  --git a/llvm/unittests/ADT/SparseSetTest.cpp b/llvm/unittests/ADT/SparseSetTest.cpp
index 2b065ea901f3..3eea4bde8c07 100644
--- a/llvm/unittests/ADT/SparseSetTest.cpp
+++ b/llvm/unittests/ADT/SparseSetTest.cpp
@@ -25,15 +25,15 @@ TEST(SparseSetTest, EmptySet) {
   Set.setUniverse(10);
 
   // Lookups on empty set.
-  EXPECT_TRUE(Set.find(0) == Set.end());
-  EXPECT_TRUE(Set.find(9) == Set.end());
+  EXPECT_FALSE(Set.contains(0));
+  EXPECT_FALSE(Set.contains(9));
 
   // Same thing on a const reference.
   const USet &CSet = Set;
   EXPECT_TRUE(CSet.empty());
   EXPECT_TRUE(CSet.begin() == CSet.end());
   EXPECT_EQ(0u, CSet.size());
-  EXPECT_TRUE(CSet.find(0) == CSet.end());
+  EXPECT_FALSE(CSet.contains(0));
   USet::const_iterator I = CSet.find(5);
   EXPECT_TRUE(I == CSet.end());
 }
@@ -51,8 +51,9 @@ TEST(SparseSetTest, SingleEntrySet) {
   EXPECT_TRUE(Set.begin() + 1 == Set.end());
   EXPECT_EQ(1u, Set.size());
 
-  EXPECT_TRUE(Set.find(0) == Set.end());
-  EXPECT_TRUE(Set.find(9) == Set.end());
+  EXPECT_FALSE(Set.contains(0));
+  EXPECT_FALSE(Set.contains(9));
+  EXPECT_TRUE(Set.contains(5));
 
   EXPECT_FALSE(Set.count(0));
   EXPECT_TRUE(Set.count(5));
@@ -71,6 +72,7 @@ TEST(SparseSetTest, SingleEntrySet) {
   USet::iterator I = Set.find(5);
   EXPECT_TRUE(I == Set.begin());
   I = Set.erase(I);
+  EXPECT_FALSE(Set.contains(5));
   EXPECT_TRUE(I == Set.end());
   EXPECT_TRUE(Set.empty());
 }


        


More information about the llvm-commits mailing list