Skip to content

Commit 410b0dc

Browse files
dwblaikiezmodem
authored andcommitted
[llvm] Add contains(KeyType) -> bool methods to SmallPtrSet
Matches C++20 API addition. Differential Revision: https://reviews.llvm.org/D83449 (cherry picked from commit a0385bd)
1 parent b78e5de commit 410b0dc

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
378378
iterator find(ConstPtrType Ptr) const {
379379
return makeIterator(find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)));
380380
}
381+
bool contains(ConstPtrType Ptr) const {
382+
return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer();
383+
}
381384

382385
template <typename IterT>
383386
void insert(IterT I, IterT E) {

llvm/unittests/ADT/SmallPtrSetTest.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ TEST(SmallPtrSetTest, ConstTest) {
313313
IntSet.insert(B);
314314
EXPECT_EQ(IntSet.count(B), 1u);
315315
EXPECT_EQ(IntSet.count(C), 1u);
316-
EXPECT_NE(IntSet.find(B), IntSet.end());
317-
EXPECT_NE(IntSet.find(C), IntSet.end());
316+
EXPECT_TRUE(IntSet.contains(B));
317+
EXPECT_TRUE(IntSet.contains(C));
318318
}
319319

320320
// Verify that we automatically get the const version of PointerLikeTypeTraits
@@ -327,7 +327,7 @@ TEST(SmallPtrSetTest, ConstNonPtrTest) {
327327
TestPair Pair(&A[0], 1);
328328
IntSet.insert(Pair);
329329
EXPECT_EQ(IntSet.count(Pair), 1u);
330-
EXPECT_NE(IntSet.find(Pair), IntSet.end());
330+
EXPECT_TRUE(IntSet.contains(Pair));
331331
}
332332

333333
// Test equality comparison.
@@ -367,3 +367,31 @@ TEST(SmallPtrSetTest, EqualityComparison) {
367367
EXPECT_NE(c, e);
368368
EXPECT_NE(e, d);
369369
}
370+
371+
TEST(SmallPtrSetTest, Contains) {
372+
SmallPtrSet<int *, 2> Set;
373+
int buf[4] = {0, 11, 22, 11};
374+
EXPECT_FALSE(Set.contains(&buf[0]));
375+
EXPECT_FALSE(Set.contains(&buf[1]));
376+
377+
Set.insert(&buf[0]);
378+
Set.insert(&buf[1]);
379+
EXPECT_TRUE(Set.contains(&buf[0]));
380+
EXPECT_TRUE(Set.contains(&buf[1]));
381+
EXPECT_FALSE(Set.contains(&buf[3]));
382+
383+
Set.insert(&buf[1]);
384+
EXPECT_TRUE(Set.contains(&buf[0]));
385+
EXPECT_TRUE(Set.contains(&buf[1]));
386+
EXPECT_FALSE(Set.contains(&buf[3]));
387+
388+
Set.erase(&buf[1]);
389+
EXPECT_TRUE(Set.contains(&buf[0]));
390+
EXPECT_FALSE(Set.contains(&buf[1]));
391+
392+
Set.insert(&buf[1]);
393+
Set.insert(&buf[2]);
394+
EXPECT_TRUE(Set.contains(&buf[0]));
395+
EXPECT_TRUE(Set.contains(&buf[1]));
396+
EXPECT_TRUE(Set.contains(&buf[2]));
397+
}

0 commit comments

Comments
 (0)