Skip to content

Commit

Permalink
concat: fix when this == aut
Browse files Browse the repository at this point in the history
  • Loading branch information
vhavlena committed Sep 29, 2023
1 parent fed6016 commit 5143cde
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/nfa/concatenation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,26 @@ Nfa& Nfa::concatenate(const Nfa& aut) {
return st + n;
};

// copy the information about aut to save the case when this is the same object as aut.
utils::SparseSet<mata::nfa::State> aut_initial = aut.initial;
utils::SparseSet<mata::nfa::State> aut_final = aut.final;
size_t aut_n = aut.num_of_states();

this->delta.allocate(n);
this->delta.append(aut.delta.renumber_targets(upd_fnc));

// set accepting states
utils::SparseSet<State> new_fin{};
new_fin.reserve(n+aut.num_of_states());
for(const State& aut_fin : aut.final) {
new_fin.reserve(n+aut_n);
for(const State& aut_fin : aut_final) {
new_fin.insert(upd_fnc(aut_fin));
}

// connect both parts
for(const State& ini : aut.initial) {
for(const State& ini : aut_initial) {
const StatePost& ini_post = this->delta[upd_fnc(ini)];
// is ini state also final?
bool is_final = aut.final[ini];
bool is_final = aut_final[ini];
for(const State& fin : this->final) {
if(is_final) {
new_fin.insert(fin);
Expand Down
18 changes: 18 additions & 0 deletions tests/nfa/nfa-concatenation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,24 @@ TEST_CASE("mata::nfa::concatenate() inplace") {
CHECK(!result.is_lang_empty());
}

SECTION("the same automata") {
Nfa lhs{};

lhs.add_state();
lhs.initial.insert(0);
lhs.final.insert(0);
lhs.delta.add(0, 58, 0);
lhs.delta.add(0, 65, 0);
lhs.delta.add(0, 102, 0);
lhs.delta.add(0, 112, 0);
lhs.delta.add(0, 115, 0);
lhs.delta.add(0, 116, 0);

size_t lhs_size = lhs.num_of_states();
Nfa result = lhs.concatenate(lhs);
CHECK(result.num_of_states() == lhs_size * 2);
}

}

TEST_CASE("Concat_inplace performance", "[.profiling]") {
Expand Down

0 comments on commit 5143cde

Please sign in to comment.