Skip to content

Commit

Permalink
renaming uni to union_nondet, adding union_product,
Browse files Browse the repository at this point in the history
and also moving definition of intersection and renaming intersection.cc to product.cc
  • Loading branch information
kilohsakul committed Sep 29, 2023
1 parent 0c0bbfb commit 80f4e82
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
6 changes: 5 additions & 1 deletion include/mata/nfa/nfa.hh
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ OnTheFlyAlphabet create_alphabet(const std::vector<Nfa*>& nfas);
*/
OnTheFlyAlphabet create_alphabet(const std::vector<const Nfa*>& nfas);

Nfa uni(const Nfa &lhs, const Nfa &rhs);
///Non-deterministic union. (does not add e-transitions, just unites initial and final states).
Nfa union_nondet(const Nfa &lhs, const Nfa &rhs);

///Union by product construction, preserves determinism.
Nfa union_product(const Nfa &lhs, const Nfa &rhs, const Symbol first_epsilon = EPSILON, std::unordered_map<std::pair<State,State>,State> *prod_map = nullptr);

/**
* @brief Compute intersection of two NFAs.
Expand Down
2 changes: 1 addition & 1 deletion include/mata/nfa/plumbing.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void construct(Nfa* result, const ParsedObject& parsed, Alphabet* alphabet = nul
*result = builder::construct(parsed, alphabet, state_map);
}

inline void uni(Nfa *unionAutomaton, const Nfa &lhs, const Nfa &rhs) { *unionAutomaton = uni(lhs, rhs); }
inline void uni(Nfa *unionAutomaton, const Nfa &lhs, const Nfa &rhs) { *unionAutomaton = union_nondet(lhs, rhs); }

/**
* @brief Compute intersection of two NFAs.
Expand Down
12 changes: 0 additions & 12 deletions src/nfa/intersection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ using InvertedProductStorage = std::vector<State>;

namespace mata::nfa {

Nfa intersection(const Nfa& lhs, const Nfa& rhs, const Symbol first_epsilon, ProductMap *prod_map) {

auto both_final = [&](const State lhs_state,const State rhs_state) {
return lhs.final.contains(lhs_state) && rhs.final.contains(rhs_state);
};

if (lhs.final.empty() || lhs.initial.empty() || rhs.initial.empty() || rhs.final.empty())
return Nfa{};

return algorithms::product(lhs, rhs, both_final, first_epsilon, prod_map);
}

//TODO: move this method to nfa.hh? It is something one might want to use (e.g. for union, inclusion, equivalence of DFAs).
Nfa mata::nfa::algorithms::product(
const Nfa& lhs, const Nfa& rhs, const std::function<bool(State,State)>&& final_condition,
Expand Down
31 changes: 30 additions & 1 deletion src/nfa/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,36 @@ Nfa mata::nfa::minimize(
return algo(aut);
}

Nfa mata::nfa::uni(const Nfa &lhs, const Nfa &rhs) {
Nfa mata::nfa::intersection(const Nfa& lhs, const Nfa& rhs, const Symbol first_epsilon, std::unordered_map<std::pair<State, State>, State> *prod_map) {

auto both_final = [&](const State lhs_state,const State rhs_state) {
return lhs.final.contains(lhs_state) && rhs.final.contains(rhs_state);
};

if (lhs.final.empty() || lhs.initial.empty() || rhs.initial.empty() || rhs.final.empty())
return Nfa{};

return algorithms::product(lhs, rhs, both_final, first_epsilon, prod_map);
}

Nfa mata::nfa::union_product(const Nfa &lhs, const Nfa &rhs, const Symbol first_epsilon, std::unordered_map<std::pair<State,State>,State> *prod_map) {
auto one_final = [&](const State lhs_state,const State rhs_state) {
return lhs.final.contains(lhs_state) || rhs.final.contains(rhs_state);
};

Check warning on line 618 in src/nfa/operations.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/operations.cc#L615-L618

Added lines #L615 - L618 were not covered by tests

if ( lhs.final.empty() || lhs.initial.empty() ) {
Nfa result = rhs;
return result;

Check warning on line 622 in src/nfa/operations.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/operations.cc#L621-L622

Added lines #L621 - L622 were not covered by tests
}
if ( rhs.final.empty() || rhs.initial.empty() ) {
Nfa result = lhs;
return result;

Check warning on line 626 in src/nfa/operations.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/operations.cc#L625-L626

Added lines #L625 - L626 were not covered by tests
}

return algorithms::product(lhs, rhs, one_final, first_epsilon, prod_map);

Check warning on line 629 in src/nfa/operations.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/operations.cc#L629

Added line #L629 was not covered by tests
}

Nfa mata::nfa::union_nondet(const Nfa &lhs, const Nfa &rhs) {
Nfa union_nfa{ rhs };

StateRenaming lhs_state_renaming;
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_executable(tests
nfa/nfa.cc
nfa/builder.cc
nfa/nfa-concatenation.cc
nfa/nfa-intersection.cc
nfa/nfa-product.cc
nfa/nfa-profiling.cc
nfa/nfa-plumbing.cc
strings/nfa-noodlification.cc
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/nfa/nfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,7 @@ TEST_CASE("mata::nfa::union_norename()") {
REQUIRE(!rhs.is_in_lang(zero));

SECTION("failing minimal scenario") {
Nfa result = uni(lhs, rhs);
Nfa result = union_nondet(lhs, rhs);
REQUIRE(result.is_in_lang(one));
REQUIRE(result.is_in_lang(zero));
}
Expand Down

0 comments on commit 80f4e82

Please sign in to comment.