[Bf-blender-cvs] [f6e39cc95da] functions: add StringMap.add method
Jacques Lucke
noreply at git.blender.org
Tue Dec 3 15:11:56 CET 2019
Commit: f6e39cc95da8dead77b8d74349b9a6f32c3b6a10
Author: Jacques Lucke
Date: Tue Dec 3 15:11:46 2019 +0100
Branches: functions
https://developer.blender.org/rBf6e39cc95da8dead77b8d74349b9a6f32c3b6a10
add StringMap.add method
===================================================================
M source/blender/blenlib/BLI_string_map.h
M source/blender/simulations/bparticles/world_state.hpp
M tests/gtests/blenlib/BLI_string_map_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_string_map.h b/source/blender/blenlib/BLI_string_map.h
index 458a48cad5e..1378c3ae79e 100644
--- a/source/blender/blenlib/BLI_string_map.h
+++ b/source/blender/blenlib/BLI_string_map.h
@@ -190,6 +190,22 @@ template<typename T, typename Allocator = GuardedAllocator> class StringMap {
this->add_new__impl(key, std::move(value));
}
+ /**
+ * Add a new element to the map if the key does not exist yet.
+ */
+ void add(StringRef key, const T &value)
+ {
+ if (!this->contains(key)) {
+ this->add_new(key, value);
+ }
+ }
+ void add(StringRef key, T &&value)
+ {
+ if (!this->contains(key)) {
+ this->add_new(key, std::move(value));
+ }
+ }
+
/**
* Return true when the key exists in the map, otherwise false.
*/
diff --git a/source/blender/simulations/bparticles/world_state.hpp b/source/blender/simulations/bparticles/world_state.hpp
index fbdd1f4c298..6b842700421 100644
--- a/source/blender/simulations/bparticles/world_state.hpp
+++ b/source/blender/simulations/bparticles/world_state.hpp
@@ -69,17 +69,17 @@ class WorldState {
public:
void store_state(StringRef main_id, StringRef sub_id, float value)
{
- m_states_float.add_new(main_id + sub_id, value);
+ m_states_float.add(main_id + sub_id, value);
}
void store_state(StringRef main_id, StringRef sub_id, float3 value)
{
- m_states_float3.add_new(main_id + sub_id, value);
+ m_states_float3.add(main_id + sub_id, value);
}
void store_state(StringRef main_id, StringRef sub_id, float4x4 value)
{
- m_states_float4x4.add_new(main_id + sub_id, value);
+ m_states_float4x4.add(main_id + sub_id, value);
}
};
diff --git a/tests/gtests/blenlib/BLI_string_map_test.cc b/tests/gtests/blenlib/BLI_string_map_test.cc
index 59620dd2799..2fc238d9a1d 100644
--- a/tests/gtests/blenlib/BLI_string_map_test.cc
+++ b/tests/gtests/blenlib/BLI_string_map_test.cc
@@ -43,6 +43,21 @@ TEST(string_map, MoveConstructor)
EXPECT_EQ(map2.lookup("B")[5], 6);
}
+TEST(string_map, Add)
+{
+ StringMap<int> map;
+ EXPECT_EQ(map.size(), 0);
+
+ map.add("test", 1);
+ EXPECT_EQ(map.lookup("test"), 1);
+
+ map.add("test", 2);
+ EXPECT_EQ(map.lookup("test"), 1);
+
+ map.add("test2", 2);
+ EXPECT_EQ(map.lookup("test2"), 2);
+}
+
TEST(string_map, AddNew)
{
StringMap<int> map;
More information about the Bf-blender-cvs
mailing list