[llvm] [ADT] Define SetVector::count in terms of SetVector::contains (NFC) (PR #155789)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 28 01:53:18 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

We can avoid repeating the same code in count by delegating to
contains.

While I am at it, this patch adds [[nodiscard] to contains and count.


---
Full diff: https://github.com/llvm/llvm-project/pull/155789.diff


1 Files Affected:

- (modified) llvm/include/llvm/ADT/SetVector.h (+3-7) 


``````````diff
diff --git a/llvm/include/llvm/ADT/SetVector.h b/llvm/include/llvm/ADT/SetVector.h
index 85d4f2d5ee28a..ecd39f2809fb8 100644
--- a/llvm/include/llvm/ADT/SetVector.h
+++ b/llvm/include/llvm/ADT/SetVector.h
@@ -261,7 +261,7 @@ class SetVector {
   }
 
   /// Check if the SetVector contains the given key.
-  bool contains(const key_type &key) const {
+  [[nodiscard]] bool contains(const key_type &key) const {
     if constexpr (canBeSmall())
       if (isSmall())
         return is_contained(vector_, key);
@@ -271,12 +271,8 @@ class SetVector {
 
   /// Count the number of elements of a given key in the SetVector.
   /// \returns 0 if the element is not in the SetVector, 1 if it is.
-  size_type count(const key_type &key) const {
-    if constexpr (canBeSmall())
-      if (isSmall())
-        return is_contained(vector_, key);
-
-    return set_.count(key);
+  [[nodiscard]] size_type count(const key_type &key) const {
+    return contains(key) ? 1 : 0;
   }
 
   /// Completely clear the SetVector

``````````

</details>


https://github.com/llvm/llvm-project/pull/155789


More information about the llvm-commits mailing list