aboutsummaryrefslogtreecommitdiff
path: root/llvm
diff options
context:
space:
mode:
authorElijah Kin <elijah.m.kin@gmail.com>2025-07-10 10:46:12 -0700
committerGitHub <noreply@github.com>2025-07-10 10:46:12 -0700
commit49b87cd7793fe529c747940f483d0d57f963b6f7 (patch)
treefe0af27ff4ec3c530c23dce9364444633a51a4de /llvm
parentc92d5dad67aafded296653c2b9a369a7fe24ba13 (diff)
downloadllvm-49b87cd7793fe529c747940f483d0d57f963b6f7.zip
llvm-49b87cd7793fe529c747940f483d0d57f963b6f7.tar.gz
llvm-49b87cd7793fe529c747940f483d0d57f963b6f7.tar.bz2
DenseMapInfo: support std::optional<T> (#147851)
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/ADT/DenseMapInfo.h23
-rw-r--r--llvm/unittests/ADT/DenseMapTest.cpp13
2 files changed, 35 insertions, 1 deletions
diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h
index 07c37e3..b850223 100644
--- a/llvm/include/llvm/ADT/DenseMapInfo.h
+++ b/llvm/include/llvm/ADT/DenseMapInfo.h
@@ -17,6 +17,7 @@
#include <cassert>
#include <cstddef>
#include <cstdint>
+#include <optional>
#include <tuple>
#include <type_traits>
#include <utility>
@@ -320,6 +321,28 @@ struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
};
+
+template <typename T> struct DenseMapInfo<std::optional<T>> {
+ using Optional = std::optional<T>;
+ using Info = DenseMapInfo<T>;
+
+ static inline Optional getEmptyKey() { return {Info::getEmptyKey()}; }
+
+ static inline Optional getTombstoneKey() { return {Info::getTombstoneKey()}; }
+
+ static unsigned getHashValue(const Optional &OptionalVal) {
+ return detail::combineHashValue(
+ OptionalVal.has_value(),
+ Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
+ }
+
+ static bool isEqual(const Optional &LHS, const Optional &RHS) {
+ if (LHS && RHS) {
+ return Info::isEqual(LHS.value(), RHS.value());
+ }
+ return !LHS && !RHS;
+ }
+};
} // end namespace llvm
#endif // LLVM_ADT_DENSEMAPINFO_H
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index c95f96c..e3e8e8c 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -15,6 +15,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <map>
+#include <optional>
#include <set>
#include <utility>
#include <variant>
@@ -86,6 +87,14 @@ struct CtorTesterMapInfo {
CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); }
CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); }
+std::optional<uint32_t> getTestKey(int i, std::optional<uint32_t> *) {
+ return i;
+}
+
+std::optional<uint32_t> getTestValue(int i, std::optional<uint32_t> *) {
+ return 42 + i;
+}
+
// Test fixture, with helper functions implemented by forwarding to global
// function overloads selected by component types of the type parameter. This
// allows all of the map implementations to be tested with shared
@@ -117,11 +126,13 @@ typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
DenseMap<uint32_t *, uint32_t *>,
DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>,
DenseMap<EnumClass, uint32_t>,
+ DenseMap<std::optional<uint32_t>, uint32_t>,
SmallDenseMap<uint32_t, uint32_t>,
SmallDenseMap<uint32_t *, uint32_t *>,
SmallDenseMap<CtorTester, CtorTester, 4,
CtorTesterMapInfo>,
- SmallDenseMap<EnumClass, uint32_t>
+ SmallDenseMap<EnumClass, uint32_t>,
+ SmallDenseMap<std::optional<uint32_t>, uint32_t>
> DenseMapTestTypes;
// clang-format on