diff --git a/src/nfa/operations.cc b/src/nfa/operations.cc index 10280cc85..67c328dd0 100644 --- a/src/nfa/operations.cc +++ b/src/nfa/operations.cc @@ -640,17 +640,22 @@ Nfa& Nfa::uni(const Nfa& aut) { return st + n; }; + // copy the information about aut to save the case when this is the same object as aut. + size_t aut_states = aut.num_of_states(); + SparseSet aut_final_copy = aut.final; + SparseSet aut_initial_copy = aut.initial; + this->delta.allocate(n); this->delta.append(aut.delta.renumber_targets(upd_fnc)); // set accepting states - this->final.reserve(n+aut.num_of_states()); - for(const State& aut_fin : aut.final) { + this->final.reserve(n+aut_states); + for(const State& aut_fin : aut_final_copy) { this->final.insert(upd_fnc(aut_fin)); } // set unitial states - this->initial.reserve(n+aut.num_of_states()); - for(const State& aut_ini : aut.initial) { + this->initial.reserve(n+aut_states); + for(const State& aut_ini : aut_initial_copy) { this->initial.insert(upd_fnc(aut_ini)); } diff --git a/tests/nfa/nfa.cc b/tests/nfa/nfa.cc index ea0753412..c54e5f878 100644 --- a/tests/nfa/nfa.cc +++ b/tests/nfa/nfa.cc @@ -2153,6 +2153,12 @@ TEST_CASE("mata::nfa::union_inplace") { REQUIRE(result.is_in_lang(one)); REQUIRE(result.is_in_lang(zero)); } + + SECTION("same automata") { + size_t lhs_states = lhs.num_of_states(); + Nfa result = lhs.uni(lhs); + REQUIRE(result.num_of_states() == lhs_states * 2); + } } TEST_CASE("mata::nfa::remove_final()")