Skip to content

Commit

Permalink
Fix signal name assignment bug in names_view.hpp (#638)
Browse files Browse the repository at this point in the history
* 🐛 Added a conditional check to ensure current_pis vector is not empty before accessing its elements.

* ✅ Add test to cover behavior when names_view is constructed from an empty network.

* ✅ update test.

* 🎨 update ``names_view<Ntk>& operator=`` operator.

* ✅ update tests.

* 🎨 add ``get_signal_names`` and ``get_output_names``.

* 🎨 small fix.

* 🎨 Check for self-assignment.

* minor fixes

---------

Co-authored-by: Siang-Yun (Sonia) Lee <[email protected]>
  • Loading branch information
Drewniok and lee30sonia authored Apr 18, 2024
1 parent 93a37e8 commit 8062d03
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
21 changes: 8 additions & 13 deletions include/mockturtle/views/names_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "../traits.hpp"

#include <map>
#include <string>

namespace mockturtle
{
Expand All @@ -64,19 +65,13 @@ class names_view : public Ntk

names_view<Ntk>& operator=( names_view<Ntk> const& named_ntk )
{
std::map<signal, std::string> new_signal_names;
std::vector<signal> current_pis;
Ntk::foreach_pi( [this, &current_pis]( auto const& n ) {
current_pis.emplace_back( Ntk::make_signal( n ) );
} );
named_ntk.foreach_pi( [&]( auto const& n, auto i ) {
if ( const auto it = _signal_names.find( current_pis[i] ); it != _signal_names.end() )
new_signal_names[named_ntk.make_signal( n )] = it->second;
} );

Ntk::operator=( named_ntk );
_signal_names = new_signal_names;
_network_name = named_ntk._network_name;
if ( this != &named_ntk ) // Check for self-assignment
{
Ntk::operator=( named_ntk );
_signal_names = named_ntk._signal_names;
_network_name = named_ntk._network_name;
_output_names = named_ntk._output_names;
}
return *this;
}

Expand Down
8 changes: 7 additions & 1 deletion test/views/names_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,18 @@ void test_copy_names_view()
named_ntk.set_name( c, "c" );
named_ntk.set_output_name( 0, "f" );

// test operator= for empty and non-empty ntk.
names_view<Ntk> named_ntk_empty{};
named_ntk_empty = ntk;
CHECK( named_ntk_empty.get_network_name() == "" );

CHECK( named_ntk.has_name( a ) );
CHECK( named_ntk.has_name( b ) );
CHECK( named_ntk.has_name( c ) );
CHECK( named_ntk.has_output_name( 0 ) );

names_view<Ntk> new_named_ntk = named_ntk;
names_view<Ntk> new_named_ntk{};
new_named_ntk = named_ntk;
CHECK( new_named_ntk.has_name( a ) );
CHECK( new_named_ntk.has_name( b ) );
CHECK( new_named_ntk.has_name( c ) );
Expand Down

0 comments on commit 8062d03

Please sign in to comment.