BLI: update behavior of Map.lookup_or_add

Previously, this function would expect a callback function as parameter.
This behavior is now in Map.lookup_or_add_cb. The new version just
takes the key and value directly.
This commit is contained in:
Jacques Lucke 2020-06-10 14:12:24 +02:00
parent f367f1e5a5
commit 84a0a6d16c
5 changed files with 85 additions and 23 deletions

View File

@ -24,8 +24,8 @@ namespace BKE {
static const NodeTreeRef &get_tree_ref(NodeTreeRefMap &node_tree_refs, bNodeTree *btree)
{
return *node_tree_refs.lookup_or_add(btree,
[&]() { return blender::make_unique<NodeTreeRef>(btree); });
return *node_tree_refs.lookup_or_add_cb(
btree, [&]() { return blender::make_unique<NodeTreeRef>(btree); });
}
DerivedNodeTree::DerivedNodeTree(bNodeTree *btree, NodeTreeRefMap &node_tree_refs) : m_btree(btree)
@ -365,7 +365,7 @@ static Dot::Cluster *get_cluster_for_parent(Dot::DirectedGraph &graph,
if (parent == nullptr) {
return nullptr;
}
return clusters.lookup_or_add(parent, [&]() {
return clusters.lookup_or_add_cb(parent, [&]() {
Dot::Cluster *parent_cluster = get_cluster_for_parent(graph, clusters, parent->parent());
bNodeTree *btree = (bNodeTree *)parent->node_ref().bnode()->id;
Dot::Cluster *new_cluster = &graph.new_cluster(parent->node_ref().name() + " / " +

View File

@ -513,6 +513,38 @@ class Map {
}
}
/**
* Returns a reference to the value corresponding to the given key. If the key is not in the map,
* a new key-value-pair is added and a reference to the value in the map is returned.
*/
Value &lookup_or_add(const Key &key, const Value &value)
{
return this->lookup_or_add_as(key, value);
}
Value &lookup_or_add(const Key &key, Value &&value)
{
return this->lookup_or_add_as(key, std::move(value));
}
Value &lookup_or_add(Key &&key, const Value &value)
{
return this->lookup_or_add_as(std::move(key), value);
}
Value &lookup_or_add(Key &&key, Value &&value)
{
return this->lookup_or_add_as(std::move(key), std::move(value));
}
/**
* Same as `lookup_or_add`, but accepts other key types that are supported by the hash
* function.
*/
template<typename ForwardKey, typename ForwardValue>
Value &lookup_or_add_as(ForwardKey &&key, ForwardValue &&value)
{
return this->lookup_or_add__impl(
std::forward<ForwardKey>(key), std::forward<ForwardValue>(value), m_hash(key));
}
/**
* Returns a reference to the value that corresponds to the given key. If the key is not yet in
* the map, it will be newly added.
@ -521,22 +553,24 @@ class Map {
* take no parameters and return the value to be inserted.
*/
template<typename CreateValueF>
Value &lookup_or_add(const Key &key, const CreateValueF &create_value)
Value &lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
{
return this->lookup_or_add_as(key, create_value);
return this->lookup_or_add_cb_as(key, create_value);
}
template<typename CreateValueF> Value &lookup_or_add(Key &&key, const CreateValueF &create_value)
template<typename CreateValueF>
Value &lookup_or_add_cb(Key &&key, const CreateValueF &create_value)
{
return this->lookup_or_add_as(std::move(key), create_value);
return this->lookup_or_add_cb_as(std::move(key), create_value);
}
/**
* Same as `lookup_or_add`, but accepts other key types that are supported by the hash function.
* Same as `lookup_or_add_cb`, but accepts other key types that are supported by the hash
* function.
*/
template<typename ForwardKey, typename CreateValueF>
Value &lookup_or_add_as(ForwardKey &&key, const CreateValueF &create_value)
Value &lookup_or_add_cb_as(ForwardKey &&key, const CreateValueF &create_value)
{
return this->lookup_or_add__impl(std::forward<ForwardKey>(key), create_value, m_hash(key));
return this->lookup_or_add_cb__impl(std::forward<ForwardKey>(key), create_value, m_hash(key));
}
/**
@ -558,7 +592,7 @@ class Map {
*/
template<typename ForwardKey> Value &lookup_or_add_default_as(ForwardKey &&key)
{
return this->lookup_or_add(std::forward<ForwardKey>(key), []() { return Value(); });
return this->lookup_or_add_cb(std::forward<ForwardKey>(key), []() { return Value(); });
}
/**
@ -1024,7 +1058,7 @@ class Map {
}
template<typename ForwardKey, typename CreateValueF>
Value &lookup_or_add__impl(ForwardKey &&key, const CreateValueF &create_value, uint32_t hash)
Value &lookup_or_add_cb__impl(ForwardKey &&key, const CreateValueF &create_value, uint32_t hash)
{
this->ensure_can_add();
@ -1041,6 +1075,24 @@ class Map {
MAP_SLOT_PROBING_END();
}
template<typename ForwardKey, typename ForwardValue>
Value &lookup_or_add__impl(ForwardKey &&key, ForwardValue &&value, uint32_t hash)
{
this->ensure_can_add();
MAP_SLOT_PROBING_BEGIN (hash, slot) {
if (slot.is_empty()) {
slot.occupy(std::forward<ForwardKey>(key), std::forward<ForwardValue>(value), hash);
m_occupied_and_removed_slots++;
return *slot.value();
}
if (slot.contains(key, m_is_equal, hash)) {
return *slot.value();
}
}
MAP_SLOT_PROBING_END();
}
template<typename ForwardKey, typename ForwardValue>
bool add_overwrite__impl(ForwardKey &&key, ForwardValue &&value, uint32_t hash)
{

View File

@ -364,7 +364,7 @@ RNANodeIdentifier RNANodeQuery::construct_node_identifier(const PointerRNA *ptr,
RNANodeQueryIDData *RNANodeQuery::ensure_id_data(const ID *id)
{
unique_ptr<RNANodeQueryIDData> &id_data = id_data_map_.lookup_or_add(
unique_ptr<RNANodeQueryIDData> &id_data = id_data_map_.lookup_or_add_cb(
id, [&]() { return blender::make_unique<RNANodeQueryIDData>(id); });
return id_data.get();
}

View File

@ -167,7 +167,7 @@ ListBase *build_effector_relations(Depsgraph *graph, Collection *collection)
graph->physics_relations[DEG_PHYSICS_EFFECTOR] = new Map<const ID *, ListBase *>();
hash = graph->physics_relations[DEG_PHYSICS_EFFECTOR];
}
return hash->lookup_or_add(&collection->id, [&]() {
return hash->lookup_or_add_cb(&collection->id, [&]() {
::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph *>(graph);
return BKE_effector_relations_create(depsgraph, graph->view_layer, collection);
});
@ -183,7 +183,7 @@ ListBase *build_collision_relations(Depsgraph *graph,
graph->physics_relations[type] = new Map<const ID *, ListBase *>();
hash = graph->physics_relations[type];
}
return hash->lookup_or_add(&collection->id, [&]() {
return hash->lookup_or_add_cb(&collection->id, [&]() {
::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph *>(graph);
return BKE_collision_relations_create(depsgraph, collection, modifier_type);
});

View File

@ -197,25 +197,25 @@ static float return_42()
return 42.0f;
}
TEST(map, LookupOrAdd_SeparateFunction)
TEST(map, LookupOrAddCB_SeparateFunction)
{
Map<int, float> map;
EXPECT_EQ(map.lookup_or_add(0, return_42), 42.0f);
EXPECT_EQ(map.lookup_or_add_cb(0, return_42), 42.0f);
EXPECT_EQ(map.lookup(0), 42);
map.keys();
}
TEST(map, LookupOrAdd_Lambdas)
TEST(map, LookupOrAddCB_Lambdas)
{
Map<int, float> map;
auto lambda1 = []() { return 11.0f; };
EXPECT_EQ(map.lookup_or_add(0, lambda1), 11.0f);
EXPECT_EQ(map.lookup_or_add_cb(0, lambda1), 11.0f);
auto lambda2 = []() { return 20.0f; };
EXPECT_EQ(map.lookup_or_add(1, lambda2), 20.0f);
EXPECT_EQ(map.lookup_or_add_cb(1, lambda2), 20.0f);
EXPECT_EQ(map.lookup_or_add(0, lambda2), 11.0f);
EXPECT_EQ(map.lookup_or_add(1, lambda1), 20.0f);
EXPECT_EQ(map.lookup_or_add_cb(0, lambda2), 11.0f);
EXPECT_EQ(map.lookup_or_add_cb(1, lambda1), 20.0f);
}
TEST(map, AddOrModify)
@ -258,6 +258,15 @@ TEST(map, LookupOrAddDefault)
EXPECT_EQ(map.lookup(3), 10);
}
TEST(map, LookupOrAdd)
{
Map<int, int> map;
EXPECT_EQ(map.lookup_or_add(6, 4), 4);
EXPECT_EQ(map.lookup_or_add(6, 5), 4);
map.lookup_or_add(6, 4) += 10;
EXPECT_EQ(map.lookup(6), 14);
}
TEST(map, MoveConstructorSmall)
{
Map<int, float> map1;
@ -342,10 +351,11 @@ TEST(map, UniquePtrValue)
map.add_new(1, std::move(value1));
map.add(2, std::move(value2));
map.add_overwrite(3, std::move(value3));
map.lookup_or_add(4, []() { return std::unique_ptr<int>(new int()); });
map.lookup_or_add_cb(4, []() { return std::unique_ptr<int>(new int()); });
map.add_new(5, std::unique_ptr<int>(new int()));
map.add(6, std::unique_ptr<int>(new int()));
map.add_overwrite(7, std::unique_ptr<int>(new int()));
map.lookup_or_add(8, std::unique_ptr<int>(new int()));
EXPECT_EQ(map.lookup(1).get(), value1_ptr);
EXPECT_EQ(map.lookup_ptr(100), nullptr);