Cerata
A library to generate structural hardware designs
transform.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cerata/transform.h"
16 
17 #include <vector>
18 
19 #include "cerata/graph.h"
20 #include "cerata/type.h"
21 #include "cerata/object.h"
22 #include "cerata/node.h"
23 #include "cerata/array.h"
24 
25 namespace cerata {
26 
27 void GetAllGraphs(Graph *top_graph, std::vector<Graph *> *graphs_out, bool include_components) {
28  // Insert this graph
29  graphs_out->push_back(top_graph);
30 
31  // If this is a component, it may have child graphs.
32  if (top_graph->IsComponent()) {
33  // Obtain children, that must always be instances.
34  auto comp = dynamic_cast<Component *>(top_graph);
35  auto instances = comp->children();
36  graphs_out->insert(graphs_out->end(), instances.begin(), instances.end());
37 
38  // Recursively add instance components if required.
39  if (include_components) {
40  for (const auto &instance : instances) {
41  Component *instance_component = instance->component();
42  GetAllGraphs(instance_component, graphs_out);
43  }
44  }
45  }
46 }
47 
48 void GetAllObjects(Component *top_component, std::vector<Object *> *objects, bool include_instances) {
49  std::vector<Graph *> graphs;
50 
51  if (include_instances) {
52  GetAllGraphs(top_component, &graphs, include_instances);
53  }
54  // Add all pointers to the objects of the sub-graphs
55  for (const auto &graph : graphs) {
56  auto comp_objs = graph->objects();
57  objects->insert(objects->end(), comp_objs.begin(), comp_objs.end());
58  }
59 }
60 
61 void GetAllTypes(Component *top_component, std::vector<Type *> *types, bool include_instances) {
62  std::vector<Object *> objects;
63  GetAllObjects(top_component, &objects, include_instances);
64 
65  for (const auto &o : objects) {
66  if (o->IsNode()) {
67  auto *n = dynamic_cast<Node *>(o);
68  types->push_back(n->type());
69  } else if (o->IsArray()) {
70  auto *a = dynamic_cast<NodeArray *>(o);
71  types->push_back(a->type());
72  }
73  }
74  // Filter out duplicates.
75  FilterDuplicates(types);
76 }
77 
78 } // namespace cerata
cerata::Component
A Component graph.
Definition: graph.h:158
cerata::GetAllTypes
void GetAllTypes(Component *top_component, std::vector< Type * > *types, bool include_instances)
Get all types used in a component, including nested types.
Definition: transform.cc:61
cerata::GetAllObjects
void GetAllObjects(Component *top_component, std::vector< Object * > *objects, bool include_instances)
Get all objects from a component.
Definition: transform.cc:48
cerata::Graph
A graph representing a hardware structure.
Definition: graph.h:37
cerata::NodeArray
An array of nodes.
Definition: array.h:31
cerata::FilterDuplicates
void FilterDuplicates(std::vector< T > *vec)
Filter duplicate entries from a vector.
Definition: utils.h:174
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::Graph::IsComponent
bool IsComponent() const
Return true if this graph is a component, false otherwise.
Definition: graph.h:51
cerata::Node
A node.
Definition: node.h:42
cerata::Component::children
std::vector< Instance * > children() const
Returns all Instance graphs from this Component.
Definition: graph.h:187
cerata::GetAllGraphs
void GetAllGraphs(Graph *top_graph, std::vector< Graph * > *graphs_out, bool include_components)
Get all potential child graphs of a graph.
Definition: transform.cc:27