Skip to content

Commit 45918f5

Browse files
authored
[llvm][NFC] In SetVector, contains and count now automatically accept const T * arguments when the key is T * (#170377)
Also use `is_contained` to implement `contains`, since this tries the `contains` member function of the set type first.
1 parent 6c32535 commit 45918f5

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

llvm/include/llvm/ADT/SetVector.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ namespace llvm {
3939
///
4040
/// The key and value types are derived from the Set and Vector types
4141
/// respectively. This allows the vector-type operations and set-type operations
42-
/// to have different types. In particular, this is useful when storing pointers
43-
/// as "Foo *" values but looking them up as "const Foo *" keys.
42+
/// to have different types.
4443
///
4544
/// No constraint is placed on the key and value types, although it is assumed
4645
/// that value_type can be converted into key_type for insertion. Users must be
@@ -60,6 +59,9 @@ class SetVector {
6059
// excessively long linear scans from occuring.
6160
static_assert(N <= 32, "Small size should be less than or equal to 32!");
6261

62+
using const_arg_type =
63+
typename const_pointer_or_const_ref<typename Set::key_type>::type;
64+
6365
public:
6466
using value_type = typename Vector::value_type;
6567
using key_type = typename Set::key_type;
@@ -247,17 +249,17 @@ class SetVector {
247249
}
248250

249251
/// Check if the SetVector contains the given key.
250-
[[nodiscard]] bool contains(const key_type &key) const {
252+
[[nodiscard]] bool contains(const_arg_type key) const {
251253
if constexpr (canBeSmall())
252254
if (isSmall())
253255
return is_contained(vector_, key);
254256

255-
return set_.find(key) != set_.end();
257+
return is_contained(set_, key);
256258
}
257259

258260
/// Count the number of elements of a given key in the SetVector.
259261
/// \returns 0 if the element is not in the SetVector, 1 if it is.
260-
[[nodiscard]] size_type count(const key_type &key) const {
262+
[[nodiscard]] size_type count(const_arg_type key) const {
261263
return contains(key) ? 1 : 0;
262264
}
263265

llvm/unittests/ADT/SetVectorTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ TEST(SetVector, ContainsTest) {
5252
}
5353

5454
TEST(SetVector, ConstPtrKeyTest) {
55-
SetVector<int *, SmallVector<int *, 8>, SmallPtrSet<const int *, 8>> S, T;
55+
SetVector<int *> S, T;
5656
int i, j, k, m, n;
5757

5858
S.insert(&i);

0 commit comments

Comments
 (0)