dense_map: transparent lookup for at (#1270)

This commit is contained in:
Terens
2025-07-31 14:29:44 +02:00
committed by GitHub
parent ada11c4be1
commit 05e0a22fb3
2 changed files with 21 additions and 0 deletions
+18
View File
@@ -725,6 +725,24 @@ public:
return it->second;
}
/*! @copydoc at */
template<typename Other>
[[nodiscard]] std::enable_if_t<is_transparent_v<hasher> && is_transparent_v<key_equal>, std::conditional_t<false, Other, mapped_type const &>>
at(const Other &key) const {
auto it = find(key);
ENTT_ASSERT(it != cend(), "Invalid key");
return it->second;
}
/*! @copydoc at */
template<typename Other>
[[nodiscard]] std::enable_if_t<is_transparent_v<hasher> && is_transparent_v<key_equal>, std::conditional_t<false, Other, mapped_type &>>
at(const Other &key) {
auto it = find(key);
ENTT_ASSERT(it != end(), "Invalid key");
return it->second;
}
/**
* @brief Accesses or inserts a given element.
* @param key A key of an element to find or insert.
+3
View File
@@ -66,6 +66,9 @@ TEST(DenseMap, Functionalities) {
map.emplace(0, 0);
ASSERT_EQ(map.at(0), 0); // regular key lookup
ASSERT_EQ(map.at(0.0), 0); // transparent key lookup
ASSERT_EQ(map.count(0), 1u);
ASSERT_EQ(map.count(6.4), 0u);
ASSERT_EQ(cmap.count(0.0), 1u);