Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

This patch consolidates the two implementations of SparseSetValFunctor
with "if constexpr". std::is_same_v<KeyT, ValueT> is more readable
than "KeyT, KeyT" in the template parameter list.

This patch consolidates the two implementations of SparseSetValFunctor
with "if constexpr".  std::is_same_v<KeyT, ValueT> is more readable
than "KeyT, KeyT" in the template parameter list.
@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

This patch consolidates the two implementations of SparseSetValFunctor
with "if constexpr". std::is_same_v<KeyT, ValueT> is more readable
than "KeyT, KeyT" in the template parameter list.


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

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/SparseSet.h (+7-11)
diff --git a/llvm/include/llvm/ADT/SparseSet.h b/llvm/include/llvm/ADT/SparseSet.h
index 4697de097e7eb..2ac23cc4cfa13 100644
--- a/llvm/include/llvm/ADT/SparseSet.h
+++ b/llvm/include/llvm/ADT/SparseSet.h
@@ -59,24 +59,20 @@ template <typename ValueT> struct SparseSetValTraits {
   }
 };
 
-/// SparseSetValFunctor - Helper class for selecting SparseSetValTraits. The
-/// generic implementation handles ValueT classes which either provide
-/// getSparseSetIndex() or specialize SparseSetValTraits<>.
+/// SparseSetValFunctor - Helper class for getting a value's index.
 ///
+/// In the generic case, this is done via SparseSetValTraits. When the value
+/// type is the same as the key type, the KeyFunctor is used directly.
 template <typename KeyT, typename ValueT, typename KeyFunctorT>
 struct SparseSetValFunctor {
   unsigned operator()(const ValueT &Val) const {
-    return SparseSetValTraits<ValueT>::getValIndex(Val);
+    if constexpr (std::is_same_v<KeyT, ValueT>)
+      return KeyFunctorT()(Val);
+    else
+      return SparseSetValTraits<ValueT>::getValIndex(Val);
   }
 };
 
-/// SparseSetValFunctor<KeyT, KeyT> - Helper class for the common case of
-/// identity key/value sets.
-template <typename KeyT, typename KeyFunctorT>
-struct SparseSetValFunctor<KeyT, KeyT, KeyFunctorT> {
-  unsigned operator()(const KeyT &Key) const { return KeyFunctorT()(Key); }
-};
-
 /// SparseSet - Fast set implementation for objects that can be identified by
 /// small unsigned keys.
 ///

@kazutakahirata kazutakahirata merged commit 30e7715 into llvm:main Oct 24, 2025
12 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20251023_ADT_SparseSet_if_constexpr branch October 24, 2025 22:31
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
)

This patch consolidates the two implementations of SparseSetValFunctor
with "if constexpr".  std::is_same_v<KeyT, ValueT> is more readable
than "KeyT, KeyT" in the template parameter list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants