[Bf-blender-cvs] [ead9a15c157] temp-asset-browser-catalogs: Simplify catalog tree creation using std::map

Julian Eisel noreply at git.blender.org
Thu Sep 2 12:30:47 CEST 2021


Commit: ead9a15c1575515f238625ad948ddc5119b075f8
Author: Julian Eisel
Date:   Thu Sep 2 12:26:57 2021 +0200
Branches: temp-asset-browser-catalogs
https://developer.blender.org/rBead9a15c1575515f238625ad948ddc5119b075f8

Simplify catalog tree creation using std::map

By using std::map with the catalog-component name as key simplifies
creation quite a bit. Generally it seems like the most appropriate
container for storing children in the tree.

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

M	source/blender/blenkernel/BKE_asset_catalog.hh
M	source/blender/blenkernel/intern/asset_catalog.cc

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

diff --git a/source/blender/blenkernel/BKE_asset_catalog.hh b/source/blender/blenkernel/BKE_asset_catalog.hh
index ca30b92f6cb..d978868c7da 100644
--- a/source/blender/blenkernel/BKE_asset_catalog.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog.hh
@@ -27,11 +27,10 @@
 #include "BLI_filesystem.hh"
 #include "BLI_function_ref.hh"
 #include "BLI_map.hh"
-#include "BLI_set.hh"
 #include "BLI_string_ref.hh"
 #include "BLI_vector.hh"
 
-#include <filesystem>
+#include <map>
 #include <memory>
 #include <string>
 
@@ -109,9 +108,7 @@ class AssetCatalogTreeItem {
   friend class AssetCatalogService;
 
  public:
-  /* Would be nice to avoid needing a vector of pointers. But child items want to keep a pointer to
-   * the parent, which would get invalidated once the vector grows and reallocates. */
-  using ChildVec = std::vector<std::unique_ptr<AssetCatalogTreeItem>>;
+  using ChildSet = std::map<std::string, AssetCatalogTreeItem>;
   using ItemIterFn = FunctionRef<void(const AssetCatalogTreeItem &)>;
 
   AssetCatalogTreeItem(StringRef name, const AssetCatalogTreeItem *parent = nullptr);
@@ -119,10 +116,11 @@ class AssetCatalogTreeItem {
   StringRef get_name() const;
   int count_parents() const;
 
-  static void foreach_item_recursive(const ChildVec &children_, const ItemIterFn callback);
+  static void foreach_item_recursive(const ChildSet &children_, const ItemIterFn callback);
 
  protected:
-  ChildVec children_;
+  /** Child tree items, ordered by their names. */
+  ChildSet children_;
   /** The user visible name of this component. */
   CatalogPathComponent name_;
 
@@ -143,7 +141,8 @@ class AssetCatalogTree {
   void foreach_item(const AssetCatalogTreeItem::ItemIterFn callback) const;
 
  protected:
-  AssetCatalogTreeItem::ChildVec children_;
+  /** Child tree items, ordered by their names. */
+  AssetCatalogTreeItem::ChildSet children_;
 };
 
 /** Keeps track of which catalogs are defined in a certain file on disk.
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc
index 3eb0d7b48fb..60c15804be4 100644
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@ -242,7 +242,7 @@ std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
     fs::path catalog_path = catalog->path;
 
     const AssetCatalogTreeItem *parent = nullptr;
-    AssetCatalogTreeItem::ChildVec *insert_to_vec = &tree->children_;
+    AssetCatalogTreeItem::ChildSet *insert_to_set = &tree->children_;
 
     BLI_assert_msg(catalog_path.is_relative() && !catalog_path.has_root_path(),
                    "Malformed catalog path: Path should be a relative path, with no root-name or "
@@ -250,22 +250,13 @@ std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
     for (const fs::path &component : catalog_path) {
       std::string component_name = component.string();
 
-      auto matching_item = std::find_if(
-          insert_to_vec->begin(), insert_to_vec->end(), [&component_name](auto &iter_item) {
-            return component_name.c_str() == iter_item->get_name();
-          });
-      if (matching_item != insert_to_vec->end()) {
-        /* The component already exists in the tree. Walk further into the path. */
-        parent = matching_item->get();
-        insert_to_vec = &(*matching_item)->children_;
-        continue;
-      }
-
-      /* Create a new component and walk further into the path. */
-      AssetCatalogTreeItem &new_item = *insert_to_vec->emplace_back(
-          new AssetCatalogTreeItem(component_name, parent));
-      parent = &new_item;
-      insert_to_vec = &new_item.children_;
+      /* Insert new tree element - if no matching one is there yet! */
+      auto [item, was_inserted] = insert_to_set->emplace(
+          component_name, AssetCatalogTreeItem(component_name, parent));
+
+      /* Walk further into the path (no matter if a new item was created or not). */
+      parent = &item->second;
+      insert_to_set = &item->second.children_;
     }
   }
 
@@ -308,12 +299,12 @@ void AssetCatalogTree::foreach_item(const AssetCatalogTreeItem::ItemIterFn callb
   AssetCatalogTreeItem::foreach_item_recursive(children_, callback);
 }
 
-void AssetCatalogTreeItem::foreach_item_recursive(const AssetCatalogTreeItem::ChildVec &children,
+void AssetCatalogTreeItem::foreach_item_recursive(const AssetCatalogTreeItem::ChildSet &children,
                                                   const ItemIterFn callback)
 {
-  for (const std::unique_ptr<AssetCatalogTreeItem> &item : children) {
-    callback(*item);
-    foreach_item_recursive(item->children_, callback);
+  for (const auto &[key, item] : children) {
+    callback(item);
+    foreach_item_recursive(item.children_, callback);
   }
 }



More information about the Bf-blender-cvs mailing list