Skip to content

Commit c93c4c4

Browse files
authored
[ADT] Implement DenseMapInfo of std::optional<T>
1 parent 071e302 commit c93c4c4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

llvm/include/llvm/ADT/DenseMapInfo.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,35 @@ struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
320320

321321
static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
322322
};
323+
324+
template <typename T>
325+
struct DenseMapInfo<std::optional<T>> {
326+
using Optional = std::optional<T>;
327+
using Info = DenseMapInfo<T>;
328+
329+
static inline Optional getEmptyKey() {
330+
return std::make_optional(Info::getEmptyKey());
331+
}
332+
333+
static inline Optional getTombstoneKey() {
334+
return std::make_optional(Info::getTombstoneKey());
335+
}
336+
337+
static unsigned getHashValue(const Optional &OptionalVal) {
338+
return detail::combineHashValue(
339+
OptionalVal.has_value(),
340+
Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey())));
341+
}
342+
343+
static bool isEqual(const Optional &LHS, const Optional &RHS) {
344+
if (LHS.has_value() && RHS.has_value()) {
345+
return Info::isEqual(LHS.value(), RHS.value());
346+
} else if (!LHS.has_value() && !RHS.has_value()) {
347+
return true;
348+
}
349+
return false;
350+
}
351+
};
323352
} // end namespace llvm
324353

325354
#endif // LLVM_ADT_DENSEMAPINFO_H

0 commit comments

Comments
 (0)