Skip to content

Commit 41798ca

Browse files
committed
fix copy paste bugs
1 parent e5658a1 commit 41798ca

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed

include/hal_core/netlist/net.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ namespace hal
208208
*/
209209
bool is_a_source(const Endpoint* ep) const;
210210

211+
/**
212+
* Returns the position of the source endpoint in vector
213+
* @param[in] ep - The endpoint.
214+
* @returns the position index or -1 if not found in vector
215+
*/
216+
int get_source_index(const Endpoint* ep) const;
217+
211218
/**
212219
* Get the number of sources of the net.
213220
* The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
@@ -291,7 +298,7 @@ namespace hal
291298

292299
/**
293300
* Check whether an endpoint is a destination of the net.
294-
* The endpoint is specified by a tuple of a gate and the name of an input pin of that gate.
301+
* The endpoint is specified by a tuple of a gate and the name of an input pin of that gate.bool
295302
*
296303
* @param[in] gate - The gate.
297304
* @param[in] pin_name - The name of an input pin of the gate.
@@ -318,6 +325,13 @@ namespace hal
318325
*/
319326
bool is_a_destination(const Endpoint* ep) const;
320327

328+
/**
329+
* Returns the position of the destination endpoint in vector
330+
* @param[in] ep - The endpoint.
331+
* @returns the position index or -1 if not found in vector
332+
*/
333+
int get_destination_index(const Endpoint* ep) const;
334+
321335
/**
322336
* Get the number of destinations of the net.
323337
* The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.

include/hal_core/netlist/netlist_internal_manager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ namespace hal
7676
Endpoint* net_add_source(Net* net, Gate* gate, GatePin* pin);
7777
Endpoint* net_add_destination(Net* net, Gate* gate, GatePin* pin);
7878
bool net_remove_source(Net* net, Endpoint* ep);
79-
bool net_remove_source_internal(Net* net, Endpoint* ep);
8079
bool net_remove_destination(Net* net, Endpoint* ep);
81-
bool net_remove_destination_internal(Net* net, Endpoint* ep);
80+
bool remove_source_by_index(Net* net, int inx);
81+
bool remove_destination_by_index(Net* net, int inx);
82+
8283

8384
// module functions
8485
Module* create_module(u32 id, Module* parent, const std::string& name);

src/netlist/net.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ namespace hal
243243
return std::find(m_sources_raw.begin(), m_sources_raw.end(), ep) != m_sources_raw.end();
244244
}
245245

246+
int Net::get_source_index(const Endpoint* ep) const
247+
{
248+
for (int inx = 0; inx < m_sources_raw.size(); inx++)
249+
{
250+
if (m_sources_raw.at(inx) == ep) return inx;
251+
}
252+
return -1;
253+
}
254+
246255
u32 Net::get_num_of_sources(const std::function<bool(Endpoint* ep)>& filter) const
247256
{
248257
if (!filter)
@@ -405,6 +414,15 @@ namespace hal
405414
return std::find(m_destinations_raw.begin(), m_destinations_raw.end(), ep) != m_destinations_raw.end();
406415
}
407416

417+
int Net::get_destination_index(const Endpoint* ep) const
418+
{
419+
for (int inx = 0; inx < m_destinations_raw.size(); inx++)
420+
{
421+
if (m_destinations_raw.at(inx) == ep) return inx;
422+
}
423+
return -1;
424+
}
425+
408426
u32 Net::get_num_of_destinations(const std::function<bool(Endpoint* ep)>& filter) const
409427
{
410428
if (!filter)

src/netlist/netlist_internal_manager.cpp

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ namespace hal
448448
for (auto dst : dsts)
449449
{
450450
gates_to_check.insert(dst->get_gate());
451-
if (!this->net_remove_destination_internal(net, dst))
451+
int inx = net->get_destination_index(dst);
452+
if (inx < 0)
453+
{
454+
return false;
455+
}
456+
if (!this->remove_destination_by_index(net, inx))
452457
{
453458
return false;
454459
}
@@ -458,7 +463,12 @@ namespace hal
458463
for (auto src : srcs)
459464
{
460465
gates_to_check.insert(src->get_gate());
461-
if (!this->net_remove_source_internal(net, src))
466+
int inx = net->get_source_index(src);
467+
if (inx < 0)
468+
{
469+
return false;
470+
}
471+
if (!this->remove_source_by_index(net, inx))
462472
{
463473
return false;
464474
}
@@ -573,6 +583,57 @@ namespace hal
573583
return new_endpoint_raw;
574584
}
575585

586+
bool NetlistInternalManager::remove_destination_by_index(Net* net, int inx)
587+
{
588+
size_t n = net->m_destinations.size();
589+
if (inx >= n) return false;
590+
auto ep = net->m_destinations_raw.at(inx);
591+
auto gate = ep->get_gate();
592+
593+
if (!m_netlist->is_gate_in_netlist(gate))
594+
{
595+
return false;
596+
}
597+
utils::unordered_vector_erase(gate->m_in_endpoints, ep);
598+
utils::unordered_vector_erase(gate->m_in_nets, net);
599+
600+
if (inx < n-1)
601+
{
602+
net->m_destinations[inx] = std::move(net->m_destinations.back());
603+
net->m_destinations_raw[inx] = net->m_destinations_raw.back();
604+
}
605+
net->m_destinations.pop_back();
606+
net->m_destinations_raw.pop_back();
607+
m_event_handler->notify(NetEvent::event::dst_removed, net, gate->get_id());
608+
return true;
609+
}
610+
611+
bool NetlistInternalManager::remove_source_by_index(Net* net, int inx)
612+
{
613+
size_t n = net->m_sources.size();
614+
if (inx >= n) return false;
615+
auto ep = net->m_sources_raw.at(inx);
616+
auto gate = ep->get_gate();
617+
618+
if (!m_netlist->is_gate_in_netlist(gate))
619+
{
620+
return false;
621+
}
622+
utils::unordered_vector_erase(gate->m_out_endpoints, ep);
623+
utils::unordered_vector_erase(gate->m_out_nets, net);
624+
625+
if (inx < n-1)
626+
{
627+
net->m_sources[inx] = std::move(net->m_sources.back());
628+
net->m_sources_raw[inx] = net->m_sources_raw.back();
629+
}
630+
net->m_sources.pop_back();
631+
net->m_sources_raw.pop_back();
632+
m_event_handler->notify(NetEvent::event::src_removed, net, gate->get_id());
633+
return true;
634+
}
635+
636+
/*
576637
bool NetlistInternalManager::net_remove_source_internal(Net* net, Endpoint* ep)
577638
{
578639
auto gate = ep->get_gate();
@@ -599,17 +660,26 @@ namespace hal
599660
600661
return false;
601662
}
663+
*/
602664

603665
bool NetlistInternalManager::net_remove_source(Net* net, Endpoint* ep)
604666
{
667+
int inx = net->get_source_index(ep);
668+
if (inx < 0)
669+
{
670+
log_warning("net", "given endpoint is not a source of net '{}' with ID {}.",
671+
net->get_name(), net->get_id());
672+
return false;
673+
}
674+
605675
auto gate = ep->get_gate();
606676

607-
if (!m_netlist->is_net_in_netlist(net) || !m_netlist->is_gate_in_netlist(gate) || !net->is_a_source(ep))
677+
if (!m_netlist->is_net_in_netlist(net) || !m_netlist->is_gate_in_netlist(gate))
608678
{
609679
return false;
610680
}
611681

612-
if (!net_remove_source_internal(net, ep))
682+
if (!remove_source_by_index(net, inx))
613683
{
614684
log_warning("net",
615685
"output pin '{}' of gate '{}' with ID {} is not a source of net '{}' with ID {} in netlist with ID {}",
@@ -718,6 +788,7 @@ namespace hal
718788
return new_endpoint_raw;
719789
}
720790

791+
/*
721792
bool NetlistInternalManager::net_remove_destination_internal(Net* net, Endpoint* ep)
722793
{
723794
auto gate = ep->get_gate();
@@ -744,17 +815,26 @@ namespace hal
744815
745816
return false;
746817
}
818+
*/
747819

748820
bool NetlistInternalManager::net_remove_destination(Net* net, Endpoint* ep)
749821
{
822+
int inx = net->get_destination_index(ep);
823+
if (inx < 0)
824+
{
825+
log_warning("net", "given endpoint is not a destination of net '{}' with ID {}.",
826+
net->get_name(), net->get_id());
827+
return false;
828+
}
829+
750830
auto gate = ep->get_gate();
751831

752-
if (!m_netlist->is_net_in_netlist(net) || !m_netlist->is_gate_in_netlist(gate) || !net->is_a_destination(ep))
832+
if (!m_netlist->is_net_in_netlist(net) || !m_netlist->is_gate_in_netlist(gate))
753833
{
754834
return false;
755835
}
756836

757-
if (!net_remove_destination_internal(net,ep))
837+
if (!remove_destination_by_index(net, inx))
758838
{
759839
log_warning("net",
760840
"input pin '{}' of gate '{}' with ID {} is not a destination of net '{}' with ID {} in netlist with ID {}",

0 commit comments

Comments
 (0)