Michele Caini 3 роки тому
батько
коміт
2b0045e23e
2 змінених файлів з 32 додано та 6 видалено
  1. 21 0
      src/entt/container/dense_map.hpp
  2. 11 6
      test/entt/container/dense_map.cpp

+ 21 - 0
src/entt/container/dense_map.hpp

@@ -748,6 +748,27 @@ public:
         return insert_or_do_nothing(std::move(key)).first->second;
     }
 
+    /**
+     * @brief Returns the number of elements matching a key (either 1 or 0).
+     * @param key Key value of an element to search for.
+     * @return Number of elements matching the key (either 1 or 0).
+     */
+    [[nodiscard]] size_type count(const key_type &key) const {
+        return find(key) != end();
+    }
+
+    /**
+     * @brief Returns the number of elements matching a key (either 1 or 0).
+     * @tparam Other Type of the key value of an element to search for.
+     * @param key Key value of an element to search for.
+     * @return Number of elements matching the key (either 1 or 0).
+     */
+    template<typename Other>
+    [[nodiscard]] std::enable_if_t<is_transparent_v<hasher> && is_transparent_v<key_equal>, std::conditional_t<false, Other, size_type>>
+    count(const Other &key) const {
+        return find(key) != end();
+    }
+
     /**
      * @brief Finds an element with a given key.
      * @param key Key value of an element to search for.

+ 11 - 6
test/entt/container/dense_map.cpp

@@ -26,7 +26,7 @@ struct transparent_equal_to {
 };
 
 TEST(DenseMap, Functionalities) {
-    entt::dense_map<std::size_t, std::size_t, entt::identity, transparent_equal_to> map;
+    entt::dense_map<int, int, entt::identity, transparent_equal_to> map;
 
     ASSERT_NO_THROW([[maybe_unused]] auto alloc = map.get_allocator());
 
@@ -34,7 +34,7 @@ TEST(DenseMap, Functionalities) {
     ASSERT_EQ(map.size(), 0u);
     ASSERT_EQ(map.load_factor(), 0.f);
     ASSERT_EQ(map.max_load_factor(), .875f);
-    ASSERT_EQ(map.max_size(), (std::vector<entt::internal::dense_map_node<std::size_t, std::size_t>>{}.max_size()));
+    ASSERT_EQ(map.max_size(), (std::vector<entt::internal::dense_map_node<int, int>>{}.max_size()));
 
     map.max_load_factor(.9f);
 
@@ -68,7 +68,12 @@ TEST(DenseMap, Functionalities) {
     ASSERT_EQ(map.hash_function()(42), 42);
     ASSERT_TRUE(map.key_eq()(42, 42));
 
-    map.emplace(0u, 0u);
+    map.emplace(0, 0);
+
+    ASSERT_EQ(map.count(0), 1u);
+    ASSERT_EQ(map.count(4.2), 0u);
+    ASSERT_EQ(std::as_const(map).count(0.0), 1u);
+    ASSERT_EQ(std::as_const(map).count(42), 0u);
 
     ASSERT_FALSE(map.empty());
     ASSERT_EQ(map.size(), 1u);
@@ -77,8 +82,8 @@ TEST(DenseMap, Functionalities) {
     ASSERT_NE(std::as_const(map).begin(), std::as_const(map).end());
     ASSERT_NE(map.cbegin(), map.cend());
 
-    ASSERT_TRUE(map.contains(0u));
-    ASSERT_EQ(map.bucket(0u), 0u);
+    ASSERT_TRUE(map.contains(0));
+    ASSERT_EQ(map.bucket(0), 0u);
 
     map.clear();
 
@@ -89,7 +94,7 @@ TEST(DenseMap, Functionalities) {
     ASSERT_EQ(std::as_const(map).begin(), std::as_const(map).end());
     ASSERT_EQ(map.cbegin(), map.cend());
 
-    ASSERT_FALSE(map.contains(0u));
+    ASSERT_FALSE(map.contains(0));
 }
 
 TEST(DenseMap, Constructors) {