Skip to content

Commit

Permalink
uni: fixing the case when aut == this
Browse files Browse the repository at this point in the history
  • Loading branch information
vhavlena committed Sep 29, 2023
1 parent 8c44493 commit b71fac0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/nfa/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<mata::nfa::State> aut_final_copy = aut.final;
SparseSet<mata::nfa::State> 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));
}

Expand Down
6 changes: 6 additions & 0 deletions tests/nfa/nfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
Expand Down

0 comments on commit b71fac0

Please sign in to comment.