[Bf-blender-cvs] [48f2f3cea6a] functions: change cluster usage in dot exporter

Jacques Lucke noreply at git.blender.org
Tue Nov 26 17:31:36 CET 2019


Commit: 48f2f3cea6a30524719cabe2f26fe766ea565248
Author: Jacques Lucke
Date:   Tue Nov 26 12:57:47 2019 +0100
Branches: functions
https://developer.blender.org/rB48f2f3cea6a30524719cabe2f26fe766ea565248

change cluster usage in dot exporter

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

M	source/blender/blenlib/BLI_dot_export.h
M	source/blender/blenlib/intern/dot_export.cc

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

diff --git a/source/blender/blenlib/BLI_dot_export.h b/source/blender/blenlib/BLI_dot_export.h
index 8ba4d3b14c5..146edcefc4c 100644
--- a/source/blender/blenlib/BLI_dot_export.h
+++ b/source/blender/blenlib/BLI_dot_export.h
@@ -12,6 +12,7 @@
 #include "BLI_optional.h"
 #include "BLI_string_map.h"
 #include "BLI_map.h"
+#include "BLI_set.h"
 #include "BLI_utility_mixins.h"
 
 #include "BLI_dot_export_attribute_enums.h"
@@ -50,11 +51,15 @@ class Graph {
   Vector<std::unique_ptr<Node>> m_nodes;
   Vector<std::unique_ptr<Cluster>> m_clusters;
 
+  Set<Node *> m_top_level_nodes;
+  Set<Cluster *> m_top_level_clusters;
+
   friend Cluster;
+  friend Node;
 
  public:
   Node &new_node(StringRef label);
-  Cluster &new_cluster();
+  Cluster &new_cluster(StringRef label = "");
 
   void export__declare_nodes_and_clusters(std::stringstream &ss) const;
 
@@ -69,49 +74,98 @@ class Graph {
   }
 };
 
-class UndirectedGraph final : public Graph {
+class Cluster {
  private:
-  Vector<std::unique_ptr<UndirectedEdge>> m_edges;
-
- public:
-  std::string to_dot_string() const;
+  AttributeList m_attributes;
+  Graph &m_graph;
+  Cluster *m_parent = nullptr;
+  Set<Cluster *> m_children;
+  Set<Node *> m_nodes;
 
-  UndirectedEdge &new_edge(NodePort a, NodePort b);
-};
+  friend Graph;
+  friend Node;
 
-class DirectedGraph final : public Graph {
- private:
-  Vector<std::unique_ptr<DirectedEdge>> m_edges;
+  Cluster(Graph &graph) : m_graph(graph)
+  {
+  }
 
  public:
-  std::string to_dot_string() const;
+  void export__declare_nodes_and_clusters(std::stringstream &ss) const;
 
-  DirectedEdge &new_edge(NodePort from, NodePort to);
+  void set_attribute(StringRef key, StringRef value)
+  {
+    m_attributes.set(key, value);
+  }
+
+  void set_parent_cluster(Cluster *cluster);
+  void set_parent_cluster(Cluster &cluster)
+  {
+    this->set_parent_cluster(&cluster);
+  }
 };
 
-class Cluster {
+class Node {
  private:
   AttributeList m_attributes;
-  Graph *m_graph;
-  Cluster *m_parent;
-  Vector<std::unique_ptr<Cluster>> m_clusters;
-  Vector<std::unique_ptr<Node>> m_nodes;
+  Graph &m_graph;
+  Cluster *m_cluster = nullptr;
 
- public:
-  Cluster(Graph &graph, Cluster *parent) : m_graph(&graph), m_parent(parent)
+  friend Graph;
+
+  Node(Graph &graph) : m_graph(graph)
   {
   }
 
-  void export__declare_nodes_and_clusters(std::stringstream &ss) const;
+ public:
+  const AttributeList &attributes() const
+  {
+    return m_attributes;
+  }
 
-  Cluster &new_cluster();
+  AttributeList &attributes()
+  {
+    return m_attributes;
+  }
 
-  Node &new_node(StringRef label);
+  void set_parent_cluster(Cluster *cluster);
+  void set_parent_cluster(Cluster &cluster)
+  {
+    this->set_parent_cluster(&cluster);
+  }
 
   void set_attribute(StringRef key, StringRef value)
   {
     m_attributes.set(key, value);
   }
+
+  void set_shape(Attr_shape::Enum shape)
+  {
+    this->set_attribute("shape", Attr_shape::to_string(shape));
+  }
+
+  void export__as_id(std::stringstream &ss) const;
+
+  void export__as_declaration(std::stringstream &ss) const;
+};
+
+class UndirectedGraph final : public Graph {
+ private:
+  Vector<std::unique_ptr<UndirectedEdge>> m_edges;
+
+ public:
+  std::string to_dot_string() const;
+
+  UndirectedEdge &new_edge(NodePort a, NodePort b);
+};
+
+class DirectedGraph final : public Graph {
+ private:
+  Vector<std::unique_ptr<DirectedEdge>> m_edges;
+
+ public:
+  std::string to_dot_string() const;
+
+  DirectedEdge &new_edge(NodePort from, NodePort to);
 };
 
 class NodePort {
@@ -178,36 +232,6 @@ class UndirectedEdge : public Edge {
   void export__as_edge_statement(std::stringstream &ss) const;
 };
 
-class Node {
- private:
-  AttributeList m_attributes;
-
- public:
-  const AttributeList &attributes() const
-  {
-    return m_attributes;
-  }
-
-  AttributeList &attributes()
-  {
-    return m_attributes;
-  }
-
-  void set_attribute(StringRef key, StringRef value)
-  {
-    m_attributes.set(key, value);
-  }
-
-  void set_shape(Attr_shape::Enum shape)
-  {
-    this->set_attribute("shape", Attr_shape::to_string(shape));
-  }
-
-  void export__as_id(std::stringstream &ss) const;
-
-  void export__as_declaration(std::stringstream &ss) const;
-};
-
 namespace Utils {
 
 class NodeWithSocketsWrapper {
diff --git a/source/blender/blenlib/intern/dot_export.cc b/source/blender/blenlib/intern/dot_export.cc
index 4373c5f54f1..be029e2e2b0 100644
--- a/source/blender/blenlib/intern/dot_export.cc
+++ b/source/blender/blenlib/intern/dot_export.cc
@@ -3,6 +3,84 @@
 namespace BLI {
 namespace DotExport {
 
+/* Graph Building
+ ************************************************/
+
+Node &Graph::new_node(StringRef label)
+{
+  Node *node = new Node(*this);
+  m_nodes.append(std::unique_ptr<Node>(node));
+  m_top_level_nodes.add_new(node);
+  node->set_attribute("label", label);
+  return *node;
+}
+
+Cluster &Graph::new_cluster(StringRef label)
+{
+  Cluster *cluster = new Cluster(*this);
+  m_clusters.append(std::unique_ptr<Cluster>(cluster));
+  m_top_level_clusters.add_new(cluster);
+  cluster->set_attribute("label", label);
+  return *cluster;
+}
+
+UndirectedEdge &UndirectedGraph::new_edge(NodePort a, NodePort b)
+{
+  UndirectedEdge *edge = new UndirectedEdge(a, b);
+  m_edges.append(std::unique_ptr<UndirectedEdge>(edge));
+  return *edge;
+}
+
+DirectedEdge &DirectedGraph::new_edge(NodePort from, NodePort to)
+{
+  DirectedEdge *edge = new DirectedEdge(from, to);
+  m_edges.append(std::unique_ptr<DirectedEdge>(edge));
+  return *edge;
+}
+
+void Cluster::set_parent_cluster(Cluster *new_parent)
+{
+  if (m_parent == new_parent) {
+    return;
+  }
+  else if (m_parent == nullptr) {
+    m_graph.m_top_level_clusters.remove(this);
+    new_parent->m_children.add_new(this);
+  }
+  else if (new_parent == nullptr) {
+    m_parent->m_children.remove(this);
+    m_graph.m_top_level_clusters.add_new(this);
+  }
+  else {
+    m_parent->m_children.remove(this);
+    new_parent->m_children.add_new(this);
+  }
+  m_parent = new_parent;
+}
+
+void Node::set_parent_cluster(Cluster *cluster)
+{
+  if (m_cluster == cluster) {
+    return;
+  }
+  else if (m_cluster == nullptr) {
+    m_graph.m_top_level_nodes.remove(this);
+    cluster->m_nodes.add_new(this);
+  }
+  else if (cluster == nullptr) {
+    m_cluster->m_nodes.remove(this);
+    m_graph.m_top_level_nodes.add_new(this);
+  }
+  else {
+    m_cluster->m_nodes.remove(this);
+    cluster->m_nodes.add_new(this);
+  }
+  m_cluster = cluster;
+}
+
+/* Dot Generation
+ **********************************************/
+
 std::string DirectedGraph::to_dot_string() const
 {
   std::stringstream ss;
@@ -41,11 +119,11 @@ void Graph::export__declare_nodes_and_clusters(std::stringstream &ss) const
   m_attributes.export__as_bracket_list(ss);
   ss << "\n\n";
 
-  for (auto &node : m_nodes) {
+  for (Node *node : m_top_level_nodes) {
     node->export__as_declaration(ss);
   }
 
-  for (auto &cluster : m_clusters) {
+  for (Cluster *cluster : m_top_level_clusters) {
     cluster->export__declare_nodes_and_clusters(ss);
   }
 }
@@ -58,11 +136,11 @@ void Cluster::export__declare_nodes_and_clusters(std::stringstream &ss) const
   m_attributes.export__as_bracket_list(ss);
   ss << "\n\n";
 
-  for (auto &node : m_nodes) {
+  for (Node *node : m_nodes) {
     node->export__as_declaration(ss);
   }
 
-  for (auto &cluster : m_clusters) {
+  for (Cluster *cluster : m_children) {
     cluster->export__declare_nodes_and_clusters(ss);
   }
 
@@ -102,43 +180,6 @@ void AttributeList::export__as_bracket_list(std::stringstream &ss) const
   ss << "]";
 }
 
-Node &Graph::new_node(StringRef label)
-{
-  Node *node = new Node();
-  m_nodes.append(std::unique_ptr<Node>(node));
-  node->set_attribute("label", label);
-  return *node;
-}
-
-Cluster &Graph::new_cluster()
-{
-  Cluster *cluster = new Cluster(*this, nullptr);
-  m_clusters.append(std::unique_ptr<Cluster>(cluster));
-  return *cluster;
-}
-
-Node &Cluster::new_node(StringRef label)
-{
-  Node *node = new Node();
-  m_nodes.append(std::unique_ptr<Node>(node));
-  node->set_attribute("label", label);
-  return *node;
-}
-
-UndirectedEdge &UndirectedGraph::new_edge(NodePort a, NodePort b)
-{
-  UndirectedEdge *edge = new UndirectedEdge(a, b);
-  m_edges.append(std::unique_ptr<UndirectedEdge>(edge));
-  return *edge;
-}
-
-DirectedEdge &DirectedGraph::new_edge(NodePort from, NodePort to)
-{
-  DirectedEdge *edge = new DirectedEdge(from, to);
-  m_edges.append(std::unique_ptr<DirectedEdge>(edge));
-  return *edge;
-}
-
 void Node::export__as_id(std::stringstream &ss) const
 {
   ss << '"' << (const void *)this << '"';



More information about the Bf-blender-cvs mailing list