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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 28 01:52:37 PDT 2025


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

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.


>From 59329848a7abd79b337a7bd9e18ed934d089e210 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 24 Aug 2025 21:43:13 -0700
Subject: [PATCH] [ADT] Define SetVector::count in terms of SetVector::contains
 (NFC)

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.
---
 llvm/include/llvm/ADT/SetVector.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

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



More information about the llvm-commits mailing list