20 using std::make_unique;
29 #include <unordered_set> 32 #include <unordered_map> 35 #include <forward_list> 81 alphabet.reserve(this->u32alphabet.size());
82 for (char32_t symbol : this->u32alphabet) {
98 : pim(new
impl(move(alphabet), move(transitions), move(labels),
99 move(acceptingStates))) {}
114 pim.reset(
new impl(*(d.pim)));
130 dfa::~dfa() =
default;
132 #ifndef DOXYGEN_SHOULD_SKIP_THIS 133 dfa::operator
nfa const&()
const {
134 if (!pim->equivalent) {
137 return *pim->equivalent;
139 #endif // DOXYGEN_SHOULD_SKIP_THIS 152 unitedAlphabet.push_front(symbol);
153 unitedAlphabetSize++;
160 for (
size_t q(0); q <
getStates().size(); q++) {
163 for (char32_t symbol : unitedAlphabet) {
165 row[s] =
delta_(q, symbol);
173 for (
size_t q(0); q < d.
getStates().size(); q++) {
176 for (char32_t symbol : unitedAlphabet) {
186 for (
size_t s(0); s < unitedAlphabetSize; s++) {
199 return operator==(static_cast<dfa const&>(n));
216 if (si >= pim->alphabet.size()) {
219 if (qi >= pim->labels.size()) {
222 return pim->transitions[qi][si];
280 string const&
dfa::delta_(
string const& q, char32_t u32symbol)
const {
303 string const&
dfa::delta(
string const& q,
string const& symbol)
const {
321 for (char32_t symbol : u32word) {
421 if (qi >= pim->labels.size()) {
424 return pim->accepting[qi];
448 auto const& pim = d.pim;
449 if (pim->accepting[0]) {
454 shortestWords.emplace(0, U
"");
455 while (shortestWords.size() > oldSize) {
456 oldSize = shortestWords.size();
457 for (
auto const& stateWord : shortestWords) {
459 size_t reached = d.
delta_(stateWord.first, symbol);
460 u32string shWord = stateWord.second + symbol;
461 if (pim->accepting[reached]) {
464 if (!shortestWords.count(reached)) {
465 shortestWords.emplace(reached, move(shWord));
561 distinguishable.reserve(transitions.size());
562 for (
size_t q(0); q < transitions.size(); q++) {
563 bool qAcc(accepting[q]);
564 distinguishable.push_back(
valarray<bool>(
false, transitions.size()));
565 for (
size_t p(0); p < q; p++) {
566 bool pAcc(accepting[p]);
567 distinguishable[q][p] = (qAcc != pAcc);
568 distinguishable[p][q] = (qAcc != pAcc);
574 for (
size_t q(0); q < transitions.size(); q++) {
575 for (
size_t p(0); p < q; p++) {
576 if (distinguishable[q][p]) {
579 for (
size_t s(0); s < transitions[q].size(); s++) {
580 size_t qS(transitions[q][s]);
581 size_t pS(transitions[p][s]);
582 if (distinguishable[qS][pS]) {
584 distinguishable[q][p] =
true;
585 distinguishable[p][q] =
true;
593 for (
size_t i(0); i < distinguishable.size(); i++) {
594 distinguishable[i] ^= allTrue;
596 return distinguishable;
u32string findShortestWord_(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
vector< string > labels
Stores the names of states.
std::string const & getInitialState() const
Names this DFA's initial state.
string findShortestWord(dfa const &d)
Searches the shortest UTF-8-encoded word accepted by a given DFA.
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
dfa()
Constructs a DFA accepting the empty language ∅.
Represents nondeterministic finite automata with ε-moves.
vector< vector< size_t > > transitions
Stores the transition function as a table viz state index × symbol index → state index...
bool operator==(dfa const &d) const
Tests whether this DFA accepts exactly the same language as another one.
Signals that an input symbol was used that the FA doesn't recognize.
std::shared_ptr< nfa const > equivalent
Holds an equivalent NFA in case it is ever needed.
size_t deltaHat_(size_t qi, std::u32string const &u32word) const
Computes this DFA's transition function recursively for a UTF-32-encoded string of symbols...
Represents deterministic finite automata.
fabuilder & intersect(nfa const &other)
Makes the prospective FA accept only words accepted also by another FA.
size_t deltaHat(size_t qi, std::string const &word) const
Computes this DFA's transition function recursively for a UTF-8-encoded string of symbols...
size_t delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Contains utility classes, objects and functions used throughout the library.
vector< char32_t > u32alphabet
Represents the set of processable symbols.
bool operator!=(dfa const &d) const
Tests whether this DFA doesn't accept the same language as another one.
size_t index_of(C const &container, T const &element)
Basically Java's List interface's indexOf, but as a non-member function and returning the container's...
Contains the reg::nfa class definition.
static fabuilder complement(dfa const &d)
Creates a builder for a DFA accepting the complement of the language of a DFA.
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
std::vector< std::string > const & getAlphabet() const
Fetches this DFA's set of processable symbols as UTF-8-encoded strings.
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
fabuilder & addSymbol_(char32_t u32symbol)
Adds a symbol to the prospective FA's alphabet.
fabuilder & unite(nfa const &other)
Makes the prospective FA also accept every word of another FA's language.
Signals that a state was mentioned that isn't part of the FA.
Constructs NFAs step by step.
impl()
Constructs private implementation object for a DFA accepting the empty language ∅.
static std::vector< std::valarray< bool > > indistinguishableStates(std::vector< std::vector< size_t >> const &transitions, std::valarray< bool > const &accepting)
Builds the table of indistinguishable states w.r.t. a transition function.
Where this library lives.
impl(vector< char32_t > &&u32alphabet, vector< vector< size_t >> &&transitions, vector< string > &&labels, valarray< bool > &&accepting)
Constructs private implementation object with provided members.
static fabuilder unite(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the union of the languages of two DFAs.
static fabuilder subtract(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the set difference of the languages of two DFAs.
Private implementation details of DFAs.
valarray< bool > accepting
A true value marks an index as belonging to an accept state.
nfa buildNfa()
Builds an NFA as defined by previous operations.
vector< string > alphabet
Represents the set of processable symbols as UTF-8-encoded strings.
Contains the reg::dfa class definition.
static fabuilder intersect(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the intersection of the languages of two DFAs.
std::vector< char32_t > const & getAlphabet_() const
Fetches this DFA's set of processable symbols.
size_t delta_(size_t qi, char32_t u32symbol) const
Computes this DFA's transition function for a state index and a UTF-32-encoded symbol.
Contains the reg::fabuilder class definition.
fabuilder & complement(bool force=false)
Inverts the prospective DFA's language with respect to all possible strings over its alphabet...
dfa & operator=(dfa const &d)
Copy-assigns this DFA by copying another one's private implementation object.