HAL
grouping.cpp
Go to the documentation of this file.
2 
6 #include "hal_core/netlist/net.h"
9 
10 namespace hal
11 {
12  Grouping::Grouping(NetlistInternalManager* internal_manager, EventHandler* event_handler, u32 id, std::string name)
13  {
14  m_id = id;
15  m_name = name;
16  m_color = next_color();
17  m_internal_manager = internal_manager;
18 
19  m_event_handler = event_handler;
20  }
21 
23  {
24  return m_id;
25  }
26 
27  void Grouping::set_name(std::string name)
28  {
29  if (utils::trim(name).empty())
30  {
31  log_error("grouping", "grouping name cannot be empty.");
32  return;
33  }
34 
35  if (name != m_name)
36  {
37  m_name = name;
38  m_event_handler->notify(GroupingEvent::event::name_changed, this);
39  }
40  }
41 
42  std::string Grouping::get_name() const
43  {
44  return m_name;
45  }
46 
48  {
49  return m_color;
50  }
51 
53  {
54  m_color = c;
55  m_event_handler->notify(GroupingEvent::event::color_changed, this);
56  }
57 
59  {
60  int baseCol = (get_id() - 1) * 37;
61  return utils::Color(baseCol % 255, 200, std::max(250 - baseCol / 255 * 50, 50));
62  }
63 
65  {
66  return m_internal_manager->m_netlist;
67  }
68 
69  bool Grouping::assign_gate(Gate* gate, bool force)
70  {
71  return m_internal_manager->grouping_assign_gate(this, gate, force);
72  }
73 
74  bool Grouping::assign_gate_by_id(const u32 gate_id, bool force)
75  {
76  Gate* gate = m_internal_manager->m_netlist->get_gate_by_id(gate_id);
77  if (gate == nullptr)
78  {
79  return false;
80  }
81 
82  return assign_gate(gate, force);
83  }
84 
85  const std::vector<Gate*>& Grouping::get_gates() const
86  {
87  return m_gates;
88  }
89 
90  std::vector<Gate*> Grouping::get_gates(const std::function<bool(Gate*)>& filter) const
91  {
92  std::vector<Gate*> res;
93 
94  if (!filter)
95  {
96  res = m_gates;
97  }
98  else
99  {
100  for (Gate* gate : m_gates)
101  {
102  if (!filter(gate))
103  {
104  continue;
105  }
106  res.push_back(gate);
107  }
108  }
109 
110  return res;
111  }
112 
113  std::vector<u32> Grouping::get_gate_ids(const std::function<bool(Gate*)>& filter) const
114  {
115  std::vector<u32> gate_ids;
116 
117  for (const Gate* const gate : get_gates(filter))
118  {
119  gate_ids.push_back(gate->get_id());
120  }
121 
122  return gate_ids;
123  }
124 
126  {
127  return m_internal_manager->grouping_remove_gate(this, gate);
128  }
129 
130  bool Grouping::remove_gate_by_id(const u32 gate_id)
131  {
132  Gate* gate = m_internal_manager->m_netlist->get_gate_by_id(gate_id);
133  if (gate == nullptr)
134  {
135  return false;
136  }
137 
138  return remove_gate(gate);
139  }
140 
141  bool Grouping::contains_gate(Gate* gate) const
142  {
143  if (gate == nullptr)
144  {
145  return false;
146  }
147 
148  return contains_gate_by_id(gate->get_id());
149  }
150 
151  bool Grouping::contains_gate_by_id(const u32 gate_id) const
152  {
153  return m_gates_map.find(gate_id) != m_gates_map.end();
154  }
155 
156  bool Grouping::assign_net(Net* net, bool force)
157  {
158  return m_internal_manager->grouping_assign_net(this, net, force);
159  }
160 
161  bool Grouping::assign_net_by_id(const u32 net_id, bool force)
162  {
163  Net* net = m_internal_manager->m_netlist->get_net_by_id(net_id);
164  if (net == nullptr)
165  {
166  return false;
167  }
168 
169  return assign_net(net, force);
170  }
171 
172  const std::vector<Net*>& Grouping::get_nets() const
173  {
174  return m_nets;
175  }
176 
177  std::vector<Net*> Grouping::get_nets(const std::function<bool(Net*)>& filter) const
178  {
179  std::vector<Net*> res;
180 
181  if (!filter)
182  {
183  res = m_nets;
184  }
185  else
186  {
187  for (Net* net : m_nets)
188  {
189  if (!filter(net))
190  {
191  continue;
192  }
193  res.push_back(net);
194  }
195  }
196 
197  return res;
198  }
199 
200  std::vector<u32> Grouping::get_net_ids(const std::function<bool(Net*)>& filter) const
201  {
202  std::vector<u32> net_ids;
203 
204  for (const Net* const net : get_nets(filter))
205  {
206  net_ids.push_back(net->get_id());
207  }
208 
209  return net_ids;
210  }
211 
213  {
214  return m_internal_manager->grouping_remove_net(this, net);
215  }
216 
217  bool Grouping::remove_net_by_id(const u32 net_id)
218  {
219  Net* net = m_internal_manager->m_netlist->get_net_by_id(net_id);
220  if (net == nullptr)
221  {
222  return false;
223  }
224 
225  return remove_net(net);
226  }
227 
229  {
230  if (net == nullptr)
231  {
232  return false;
233  }
234 
235  return contains_net_by_id(net->get_id());
236  }
237 
238  bool Grouping::contains_net_by_id(const u32 net_id) const
239  {
240  return m_nets_map.find(net_id) != m_nets_map.end();
241  }
242 
244  {
245  return m_internal_manager->grouping_assign_module(this, module, force);
246  }
247 
248  bool Grouping::assign_module_by_id(const u32 module_id, bool force)
249  {
250  Module* module = m_internal_manager->m_netlist->get_module_by_id(module_id);
251  if (module == nullptr)
252  {
253  return false;
254  }
255 
256  return assign_module(module, force);
257  }
258 
259  const std::vector<Module*>& Grouping::get_modules() const
260  {
261  return m_modules;
262  }
263 
264  std::vector<Module*> Grouping::get_modules(const std::function<bool(Module*)>& filter) const
265  {
266  std::vector<Module*> res;
267 
268  if (!filter)
269  {
270  res = m_modules;
271  }
272  else
273  {
274  for (Module* module : m_modules)
275  {
276  if (!filter(module))
277  {
278  continue;
279  }
280  res.push_back(module);
281  }
282  }
283 
284  return res;
285  }
286 
287  std::vector<u32> Grouping::get_module_ids(const std::function<bool(Module*)>& filter) const
288  {
289  std::vector<u32> module_ids;
290 
291  for (const Module* const module : get_modules(filter))
292  {
293  module_ids.push_back(module->get_id());
294  }
295 
296  return module_ids;
297  }
298 
300  {
301  return m_internal_manager->grouping_remove_module(this, module);
302  }
303 
304  bool Grouping::remove_module_by_id(const u32 module_id)
305  {
306  Module* module = m_internal_manager->m_netlist->get_module_by_id(module_id);
307  if (module == nullptr)
308  {
309  return false;
310  }
311 
312  return remove_module(module);
313  }
314 
316  {
317  if (module == nullptr)
318  {
319  return false;
320  }
321 
323  }
324 
325  bool Grouping::contains_module_by_id(const u32 module_id) const
326  {
327  return m_modules_map.find(module_id) != m_modules_map.end();
328  }
329 } // namespace hal
NETLIST_API void notify(NetlistEvent::event ev, Netlist *netlist, u32 associated_data=0xFFFFFFFF)
Definition: gate.h:58
u32 get_id() const
Definition: gate.cpp:95
@ color_changed
no associated_data
@ name_changed
no associated_data
std::vector< u32 > get_gate_ids(const std::function< bool(Gate *)> &filter=nullptr) const
Definition: grouping.cpp:113
const std::vector< Gate * > & get_gates() const
Definition: grouping.cpp:85
bool remove_gate_by_id(const u32 gate_id)
Definition: grouping.cpp:130
bool contains_module_by_id(const u32 module_id) const
Definition: grouping.cpp:325
bool assign_module(Module *module, bool force=false)
Definition: grouping.cpp:243
bool contains_net_by_id(const u32 net_id) const
Definition: grouping.cpp:238
std::vector< u32 > get_module_ids(const std::function< bool(Module *)> &filter=nullptr) const
Definition: grouping.cpp:287
utils::Color get_color() const
Definition: grouping.cpp:47
bool remove_net(Net *net)
Definition: grouping.cpp:212
Netlist * get_netlist() const
Definition: grouping.cpp:64
void set_color(utils::Color c)
Definition: grouping.cpp:52
bool contains_net(Net *net) const
Definition: grouping.cpp:228
std::vector< u32 > get_net_ids(const std::function< bool(Net *)> &filter=nullptr) const
Definition: grouping.cpp:200
bool remove_module_by_id(const u32 module_id)
Definition: grouping.cpp:304
bool remove_net_by_id(const u32 net_id)
Definition: grouping.cpp:217
bool assign_gate(Gate *gate, bool force=false)
Definition: grouping.cpp:69
bool assign_module_by_id(const u32 module_id, bool force=false)
Definition: grouping.cpp:248
std::string get_name() const
Definition: grouping.cpp:42
bool remove_gate(Gate *gate)
Definition: grouping.cpp:125
void set_name(std::string name)
Definition: grouping.cpp:27
const std::vector< Net * > & get_nets() const
Definition: grouping.cpp:172
u32 get_id() const
Definition: grouping.cpp:22
bool contains_module(Module *module) const
Definition: grouping.cpp:315
const std::vector< Module * > & get_modules() const
Definition: grouping.cpp:259
bool remove_module(Module *module)
Definition: grouping.cpp:299
bool contains_gate_by_id(const u32 gate_id) const
Definition: grouping.cpp:151
bool contains_gate(Gate *gate) const
Definition: grouping.cpp:141
bool assign_net_by_id(const u32 net_id, bool force=false)
Definition: grouping.cpp:161
utils::Color next_color()
Definition: grouping.cpp:58
bool assign_gate_by_id(const u32 gate_id, bool force=false)
Definition: grouping.cpp:74
bool assign_net(Net *net, bool force=false)
Definition: grouping.cpp:156
u32 get_id() const
Definition: module.cpp:82
Definition: net.h:58
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:193
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:613
Net * get_net_by_id(u32 net_id) const
Definition: netlist.cpp:353
#define log_error(channel,...)
Definition: log.h:78
const Module * module(const Gate *g, const NodeBoxes &boxes)
CORE_API T trim(const T &s, const char *to_remove=" \t\r\n")
Definition: utils.h:358
quint32 u32
Net * net
std::string name
i32 id