HAL
netlist.cpp
Go to the documentation of this file.
2 #include "gui/gui_globals.h"
3 
6 
7 // TODO Consider these for moving into the core if they are useful
8 
9 #include <unordered_set>
10 
11 namespace hal
12 {
13  namespace gui_utility
14  {
16  {
17  std::unordered_set<u32> parents_m1;
18  while (m1 != nullptr)
19  {
20  parents_m1.insert(m1->get_id());
21  m1 = m1->get_parent_module();
22  }
23  while (m2 != nullptr)
24  {
25  if (parents_m1.find(m2->get_id()) != parents_m1.end())
26  {
27  return m2;
28  }
29  m2 = m2->get_parent_module();
30  }
31  return nullptr;
32  }
33 
34  Module* firstCommonAncestor(std::unordered_set<Module*> modules, const std::unordered_set<Gate*>& gates)
35  {
36  if (modules.empty() && gates.empty())
37  {
38  return nullptr;
39  }
40  std::unordered_set<Module*> modules_resolved;
41  // resolve all modules to their parent modules (otherwise a module could be returned as
42  // its own parent)
43  for (auto m : modules)
44  {
45  auto p = m->get_parent_module();
46  // this happens m is the top module (which has no parents, thus we can't find
47  // any common ancestors)
48  if (p == nullptr)
49  return nullptr;
50  modules_resolved.insert(p);
51  }
52  // resolve all gates to their parent modules, since we don't want to work with gates at all
53  for (const auto& g : gates)
54  {
55  modules_resolved.insert(g->get_module());
56  }
57  // pick two modules and resolve them to their first common ancestor,
58  auto module_list = std::vector<Module*>(modules_resolved.begin(), modules_resolved.end());
59  auto result = module_list[0];
60  for (u32 i = 1; i < module_list.size(); ++i)
61  {
62  // if the top module is the first common ancestor at any time, we can stop searching (early exit)
63  if (result->get_parent_module() == nullptr)
64  {
65  break;
66  }
67  result = firstCommonAncestor(result, module_list[i]);
68  }
69  // the final module is the first common ancestor of all elements
70  return result;
71  }
72 
74  {
75  assert(m);
76  QSet<u32> parents;
77  while ((m = m->get_parent_module()) != nullptr)
78  {
79  parents.insert(m->get_id());
80  }
81  return parents;
82  }
83 
85  {
86  assert(g);
87  Module* m = g->get_module();
88  QSet<u32> parents = parentModules(m);
89  parents.insert(m->get_id());
90  return parents;
91  }
92  } // namespace gui_utility
93 }
Definition: gate.h:58
Module * get_parent_module() const
Definition: module.cpp:125
u32 get_id() const
Definition: module.cpp:82
Module * firstCommonAncestor(std::unordered_set< Module * > modules, const std::unordered_set< Gate * > &gates)
Definition: netlist.cpp:34
QSet< u32 > parentModules(Gate *g)
Definition: netlist.cpp:84
quint32 u32
QSet::iterator insert(const T &value)