BLI: add MultiValueMap

This is a convenience wrapper for `Map<Key, Vector<Value>>`.
It does not provide any performance benefits (yet). I need this
kind of map in a couple of places and before I was duplicating
the lookup logic in many places.
This commit is contained in:
2020-07-24 12:15:13 +02:00
parent 141deeefff
commit b53c46d760
11 changed files with 277 additions and 60 deletions

View File

@@ -28,6 +28,7 @@
#include "BLI_disjoint_set.hh"
#include "BLI_ghash.h"
#include "BLI_map.hh"
#include "BLI_multi_value_map.hh"
#include "BLI_rand.h"
#include "BLI_stack.hh"
@@ -403,15 +404,15 @@ static Array<uint64_t> compute_node_hashes(MFNetwork &network)
return node_hashes;
}
static Map<uint64_t, Vector<MFNode *, 1>> group_nodes_by_hash(MFNetwork &network,
Span<uint64_t> node_hashes)
static MultiValueMap<uint64_t, MFNode *> group_nodes_by_hash(MFNetwork &network,
Span<uint64_t> node_hashes)
{
Map<uint64_t, Vector<MFNode *, 1>> nodes_by_hash;
MultiValueMap<uint64_t, MFNode *> nodes_by_hash;
for (int id : IndexRange(network.node_id_amount())) {
MFNode *node = network.node_or_null_by_id(id);
if (node != nullptr) {
uint64_t node_hash = node_hashes[id];
nodes_by_hash.lookup_or_add_default(node_hash).append(node);
nodes_by_hash.add(node_hash, node);
}
}
return nodes_by_hash;
@@ -456,7 +457,7 @@ static bool nodes_output_same_values(DisjointSet &cache, const MFNode &a, const
}
static void relink_duplicate_nodes(MFNetwork &network,
Map<uint64_t, Vector<MFNode *, 1>> &nodes_by_hash)
MultiValueMap<uint64_t, MFNode *> &nodes_by_hash)
{
DisjointSet same_node_cache{network.node_id_amount()};
@@ -494,7 +495,7 @@ static void relink_duplicate_nodes(MFNetwork &network,
void common_subnetwork_elimination(MFNetwork &network)
{
Array<uint64_t> node_hashes = compute_node_hashes(network);
Map<uint64_t, Vector<MFNode *, 1>> nodes_by_hash = group_nodes_by_hash(network, node_hashes);
MultiValueMap<uint64_t, MFNode *> nodes_by_hash = group_nodes_by_hash(network, node_hashes);
relink_duplicate_nodes(network, nodes_by_hash);
}