diff options
author | Ramkumar Ramachandra <ramkumar.ramachandra@codasip.com> | 2024-06-19 10:29:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-19 10:29:37 +0100 |
commit | d72b57bbf7c3448077f82147e3ce1a72b2c86a34 (patch) | |
tree | c2c0e248bc8f64ebc35afe28c49b13dc0fcb344e | |
parent | 116384a028f0e3801b9443e227e0840897202168 (diff) | |
download | llvm-d72b57bbf7c3448077f82147e3ce1a72b2c86a34.zip llvm-d72b57bbf7c3448077f82147e3ce1a72b2c86a34.tar.gz llvm-d72b57bbf7c3448077f82147e3ce1a72b2c86a34.tar.bz2 |
DenseMap: support enum class keys (#95972)
Implemented using std::underlying_type.
-rw-r--r-- | llvm/include/llvm/ADT/DenseMapInfo.h | 18 | ||||
-rw-r--r-- | llvm/unittests/ADT/DenseMapTest.cpp | 16 |
2 files changed, 32 insertions, 2 deletions
diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h index 5b7dce7..c405f74 100644 --- a/llvm/include/llvm/ADT/DenseMapInfo.h +++ b/llvm/include/llvm/ADT/DenseMapInfo.h @@ -297,6 +297,24 @@ template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> { } }; +// Provide DenseMapInfo for enum classes. +template <typename Enum> +struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> { + using UnderlyingType = std::underlying_type_t<Enum>; + using Info = DenseMapInfo<UnderlyingType>; + + static Enum getEmptyKey() { return static_cast<Enum>(Info::getEmptyKey()); } + + static Enum getTombstoneKey() { + return static_cast<Enum>(Info::getTombstoneKey()); + } + + static unsigned getHashValue(const Enum &Val) { + return Info::getHashValue(static_cast<UnderlyingType>(Val)); + } + + static bool isEqual(const Enum &LHS, const Enum &RHS) { 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 cb45a6e..d1bbdde 100644 --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -21,7 +21,6 @@ using namespace llvm; namespace { - uint32_t getTestKey(int i, uint32_t *) { return i; } uint32_t getTestValue(int i, uint32_t *) { return 42 + i; } @@ -36,6 +35,14 @@ uint32_t *getTestValue(int i, uint32_t **) { return &dummy_arr1[i]; } +enum class EnumClass { Val }; + +EnumClass getTestKey(int i, EnumClass *) { + // We can't possibly support 100 values for the swap test, so just return an + // invalid EnumClass for testing. + return static_cast<EnumClass>(i); +} + /// A test class that tries to check that construction and destruction /// occur correctly. class CtorTester { @@ -104,14 +111,19 @@ template <typename T> typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = nullptr; // Register these types for testing. +// clang-format off typedef ::testing::Types<DenseMap<uint32_t, uint32_t>, DenseMap<uint32_t *, uint32_t *>, DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>, + DenseMap<EnumClass, uint32_t>, SmallDenseMap<uint32_t, uint32_t>, SmallDenseMap<uint32_t *, uint32_t *>, SmallDenseMap<CtorTester, CtorTester, 4, - CtorTesterMapInfo> + CtorTesterMapInfo>, + SmallDenseMap<EnumClass, uint32_t> > DenseMapTestTypes; +// clang-format on + TYPED_TEST_SUITE(DenseMapTest, DenseMapTestTypes, ); // Empty map tests |