Skip to content

Commit

Permalink
feat: Extract utility function to get symbols to work with
Browse files Browse the repository at this point in the history
  • Loading branch information
Adda0 committed Jul 9, 2024
1 parent beabb24 commit c106e3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
6 changes: 6 additions & 0 deletions include/mata/nfa/nfa.hh
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,12 @@ Nfa remove_epsilon(const Nfa& aut, Symbol epsilon = EPSILON);
// What are the symbol names and their sequences?
Run encode_word(const Alphabet* alphabet, const std::vector<std::string>& input);

/**
* Get the set of symbols to work with during operations.
* @param[in] shared_alphabet Optional alphabet shared between NFAs passed as an argument to a function.
*/
utils::OrdVector<Symbol> get_symbols_to_work_with(const nfa::Nfa& nfa, const Alphabet* const shared_alphabet = nullptr);

} // namespace mata::nfa.

namespace std {
Expand Down
34 changes: 12 additions & 22 deletions src/nfa/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,15 +482,7 @@ std::ostream &std::operator<<(std::ostream &os, const mata::nfa::Transition &tra
}

bool mata::nfa::Nfa::make_complete(const Alphabet* const alphabet, const std::optional<State> sink_state) {
OrdVector<Symbol> symbols;
if (alphabet != nullptr) {
symbols = alphabet->get_alphabet_symbols();
} else if (this->alphabet != nullptr) {
symbols = this->alphabet->get_alphabet_symbols();
} else {
symbols = delta.get_used_symbols();
}
return make_complete(symbols, sink_state);
return make_complete(get_symbols_to_work_with(*this, alphabet), sink_state);
}

bool mata::nfa::Nfa::make_complete(const OrdVector<Symbol>& symbols, const std::optional<State> sink_state) {
Expand Down Expand Up @@ -782,18 +774,10 @@ bool mata::nfa::Nfa::is_deterministic() const {
return true;
}
bool mata::nfa::Nfa::is_complete(Alphabet const* alphabet) const {
if (alphabet == nullptr) {
if (this->alphabet != nullptr) {
alphabet = this->alphabet;
} else {
throw std::runtime_error("Checking for completeness without any alphabet to check againts.");
}
}
utils::OrdVector<Symbol> symbs_ls = alphabet->get_alphabet_symbols();
utils::OrdVector<Symbol> symbs(symbs_ls);
utils::OrdVector<Symbol> symbols{ get_symbols_to_work_with(*this, alphabet) };
utils::OrdVector<Symbol> symbs_ls{ symbols };

// TODO: make a general function for traversal over reachable states that can
// be shared by other functions?
// TODO: make a general function for traversal over reachable states that can be shared by other functions?
std::list<State> worklist(initial.begin(), initial.end());
std::unordered_set<State> processed(initial.begin(), initial.end());

Expand All @@ -805,7 +789,7 @@ bool mata::nfa::Nfa::is_complete(Alphabet const* alphabet) const {
if (!delta.empty()) {
for (const auto &symb_stateset: delta[state]) {
++n;
if (!haskey(symbs, symb_stateset.symbol)) {
if (!haskey(symbols, symb_stateset.symbol)) {
throw std::runtime_error(std::to_string(__func__) +
": encountered a symbol that is not in the provided alphabet");
}
Expand All @@ -818,7 +802,7 @@ bool mata::nfa::Nfa::is_complete(Alphabet const* alphabet) const {
}
}

if (symbs.size() != n) { return false; }
if (symbols.size() != n) { return false; }
}

return true;
Expand Down Expand Up @@ -1218,6 +1202,12 @@ std::set<mata::Word> mata::nfa::Nfa::get_words(unsigned max_length) {
return result;
}

OrdVector<Symbol> mata::nfa::get_symbols_to_work_with(const Nfa& nfa, const mata::Alphabet *const shared_alphabet) {
if (shared_alphabet != nullptr) { return shared_alphabet->get_alphabet_symbols(); }
else if (nfa.alphabet != nullptr) { return nfa.alphabet->get_alphabet_symbols(); }
else { return nfa.delta.get_used_symbols(); }
}

std::optional<mata::Word> Nfa::get_word(const Symbol first_epsilon) const {
if (initial.empty() || final.empty()) { return std::nullopt; }

Expand Down

0 comments on commit c106e3b

Please sign in to comment.