[Bf-blender-cvs] [69e8de434ff] master: Depsgraph: use BLI::Map in RootPChanMap

Jacques Lucke noreply at git.blender.org
Tue Apr 28 16:00:55 CEST 2020


Commit: 69e8de434ffda6b0135b7d0656bc29d60b9dca08
Author: Jacques Lucke
Date:   Tue Apr 28 15:56:53 2020 +0200
Branches: master
https://developer.blender.org/rB69e8de434ffda6b0135b7d0656bc29d60b9dca08

Depsgraph: use BLI::Map in RootPChanMap

Reviewers: sergey, sybren

Differential Revision: https://developer.blender.org/D7521

===================================================================

M	source/blender/depsgraph/intern/builder/deg_builder_pchanmap.cc
M	source/blender/depsgraph/intern/builder/deg_builder_pchanmap.h
M	source/blender/depsgraph/intern/depsgraph_type.h

===================================================================

diff --git a/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.cc b/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.cc
index ecacfcf7ee9..62a14d98159 100644
--- a/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.cc
+++ b/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.cc
@@ -31,99 +31,51 @@
 
 namespace DEG {
 
-static void free_rootpchanmap_valueset(void *val)
-{
-  /* Just need to free the set itself - the names stored are all references. */
-  GSet *values = (GSet *)val;
-  BLI_gset_free(values, nullptr);
-}
-
 RootPChanMap::RootPChanMap()
 {
-  /* Just create empty map. */
-  map_ = BLI_ghash_str_new("RootPChanMap");
 }
 
 RootPChanMap::~RootPChanMap()
 {
-  /* Free the map, and all the value sets. */
-  BLI_ghash_free(map_, nullptr, free_rootpchanmap_valueset);
 }
 
 /* Debug contents of map */
 void RootPChanMap::print_debug()
 {
-  GHashIterator it1;
-  GSetIterator it2;
-
-  printf("Root PChan Map:\n");
-  GHASH_ITER (it1, map_) {
-    const char *item = (const char *)BLI_ghashIterator_getKey(&it1);
-    GSet *values = (GSet *)BLI_ghashIterator_getValue(&it1);
-
-    printf("  %s : { ", item);
-    GSET_ITER (it2, values) {
-      const char *val = (const char *)BLI_gsetIterator_getKey(&it2);
-      printf("%s, ", val);
+  map_.foreach_item([](StringRefNull key, const Set<StringRefNull> &values) {
+    printf("  %s : { ", key.data());
+    for (StringRefNull val : values) {
+      printf("%s, ", val.data());
     }
     printf("}\n");
-  }
+  });
 }
 
 /* Add a mapping. */
 void RootPChanMap::add_bone(const char *bone, const char *root)
 {
-  if (BLI_ghash_haskey(map_, bone)) {
-    /* Add new entry, but only add the root if it doesn't already
-     * exist in there. */
-    GSet *values = (GSet *)BLI_ghash_lookup(map_, bone);
-    BLI_gset_add(values, (void *)root);
-  }
-  else {
-    /* Create new set and mapping. */
-    GSet *values = BLI_gset_new(
-        BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, "RootPChanMap Value Set");
-    BLI_ghash_insert(map_, (void *)bone, (void *)values);
-
-    /* Add new entry now. */
-    BLI_gset_insert(values, (void *)root);
-  }
+  map_.lookup_or_add_default(bone).add(root);
 }
 
 /* Check if there's a common root bone between two bones. */
 bool RootPChanMap::has_common_root(const char *bone1, const char *bone2) const
 {
-  /* Ensure that both are in the map... */
-  if (BLI_ghash_haskey(map_, bone1) == false) {
+  const Set<StringRefNull> *bone1_roots = map_.lookup_ptr(bone1);
+  const Set<StringRefNull> *bone2_roots = map_.lookup_ptr(bone2);
+
+  if (bone1_roots == nullptr) {
     // fprintf("RootPChanMap: bone1 '%s' not found (%s => %s)\n", bone1, bone1, bone2);
     // print_debug();
     return false;
   }
 
-  if (BLI_ghash_haskey(map_, bone2) == false) {
+  if (bone2_roots == nullptr) {
     // fprintf("RootPChanMap: bone2 '%s' not found (%s => %s)\n", bone2, bone1, bone2);
     // print_debug();
     return false;
   }
 
-  GSet *bone1_roots = (GSet *)BLI_ghash_lookup(map_, (void *)bone1);
-  GSet *bone2_roots = (GSet *)BLI_ghash_lookup(map_, (void *)bone2);
-
-  GSetIterator it1, it2;
-  GSET_ITER (it1, bone1_roots) {
-    GSET_ITER (it2, bone2_roots) {
-      const char *v1 = (const char *)BLI_gsetIterator_getKey(&it1);
-      const char *v2 = (const char *)BLI_gsetIterator_getKey(&it2);
-
-      if (strcmp(v1, v2) == 0) {
-        // fprintf("RootPchanMap: %s in common for %s => %s\n", v1, bone1, bone2);
-        return true;
-      }
-    }
-  }
-
-  // fprintf("RootPChanMap: No common root found (%s => %s)\n", bone1, bone2);
-  return false;
+  return Set<StringRefNull>::Intersects(*bone1_roots, *bone2_roots);
 }
 
 }  // namespace DEG
diff --git a/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.h b/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.h
index 1442f547b08..c3c90e5aae4 100644
--- a/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.h
+++ b/source/blender/depsgraph/intern/builder/deg_builder_pchanmap.h
@@ -23,7 +23,7 @@
 
 #pragma once
 
-struct GHash;
+#include "intern/depsgraph_type.h"
 
 namespace DEG {
 
@@ -42,13 +42,11 @@ struct RootPChanMap {
   bool has_common_root(const char *bone1, const char *bone2) const;
 
  protected:
-  /* The actual map:
-   * - Keys are "strings" (const char *) - not dynamically allocated.
-   * - Values are "sets" (const char *) - not dynamically allocated.
-   *
-   * We don't use the C++ maps here, as it's more convenient to use
-   * Blender's GHash and be able to compare by-value instead of by-ref. */
-  struct GHash *map_;
+  /**
+   * The strings are only referenced by this map. Users of RootPChanMap have to make sure that the
+   * life-time of the strings is long enough.
+   */
+  Map<StringRefNull, Set<StringRefNull>> map_;
 };
 
 }  // namespace DEG
diff --git a/source/blender/depsgraph/intern/depsgraph_type.h b/source/blender/depsgraph/intern/depsgraph_type.h
index 2b8b5471d0f..6b07f95bed6 100644
--- a/source/blender/depsgraph/intern/depsgraph_type.h
+++ b/source/blender/depsgraph/intern/depsgraph_type.h
@@ -43,6 +43,8 @@
 
 #include "BLI_map.hh"
 #include "BLI_set.hh"
+#include "BLI_string_ref.hh"
+#include "BLI_vector.hh"
 
 struct Depsgraph;
 
@@ -51,8 +53,12 @@ struct CustomData_MeshMasks;
 namespace DEG {
 
 /* Commonly used types. */
+using BLI::ArrayRef;
 using BLI::Map;
 using BLI::Set;
+using BLI::StringRef;
+using BLI::StringRefNull;
+using BLI::Vector;
 using std::deque;
 using std::map;
 using std::pair;



More information about the Bf-blender-cvs mailing list