-
Notifications
You must be signed in to change notification settings - Fork 14.4k
DenseMapInfo: support std::optional<T> #147851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-adt Author: Elijah Kin (elijahkin) ChangesFull diff: https://github.com/llvm/llvm-project/pull/147851.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h
index 07c37e353a40b..65c607910d089 100644
--- a/llvm/include/llvm/ADT/DenseMapInfo.h
+++ b/llvm/include/llvm/ADT/DenseMapInfo.h
@@ -320,6 +320,35 @@ 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 std::make_optional(Info::getEmptyKey());
+ }
+
+ static inline Optional getTombstoneKey() {
+ return std::make_optional(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.has_value() && RHS.has_value()) {
+ return Info::isEqual(LHS.value(), RHS.value());
+ } else if (!LHS.has_value() && !RHS.has_value()) {
+ return true;
+ }
+ return false;
+ }
+};
} // end namespace llvm
#endif // LLVM_ADT_DENSEMAPINFO_H
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index c95f96c4bb3c6..ca04dc055fd11 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>
@@ -117,11 +118,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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor thing, otherwise looks good.
✅ With the latest revision this PR passed the C/C++ code formatter. |
using Info = DenseMapInfo<T>; | ||
|
||
static inline Optional getEmptyKey() { | ||
return std::make_optional(Info::getEmptyKey()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to use make_optional
here? I'd think a simple return {foo};
or return Optional{foo};
should work. Also below.
No description provided.