reglibcpp  1.6.0
(Naïve) C++ implementation of models for regular languages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
reg::nfa Class Reference

Represents nondeterministic finite automata with ε-moves. More...

#include <nfa.h>

Classes

class  builder
 Constructs NFAs step by step. More...
 
struct  equal_to_reference_string_const
 Provides std::unordered_set::key_eq for state_set. More...
 
struct  hash_reference_string_const
 Provides std::unordered_set::hash_function for state_set. More...
 
struct  pImpl
 Private implementation details of NFAs. More...
 

Public Types

typedef std::unordered_set< std::reference_wrapper< std::string const >, hash_reference_string_const, equal_to_reference_string_conststate_set
 Nicer name for sets of names of states. Should store references to names in getStates. More...
 

Public Member Functions

 nfa ()
 Constructs an NFA accepting the empty language ∅. More...
 
 nfa (nfa const &n)
 Copy-constructs an NFA by copying another one's private implementation object. More...
 
 nfa (nfa &&n)
 Move-constructs an NFA by stealing another one's private implementation object. More...
 
 nfa (dfa const &d)
 Constructs an NFA from a DFA. More...
 
 nfa (builder &b)
 Constructs an NFA from a builder. More...
 
nfaoperator= (nfa const &n)
 Copy-assigns this NFA by copying another one's private implementation object. More...
 
nfaoperator= (nfa &&n)
 Move-assigns this NFA by stealing another one's private implementation object. More...
 
bool operator== (nfa const &n) const
 Tests whether this NFA accepts exactly the same language as another one. More...
 
bool operator!= (nfa const &n) const
 Tests whether this NFA doesn't accept the same language as another one. More...
 
std::valarray< bool > const & delta (size_t qi, size_t si) const
 Computes this NFA's transition function for a state index and a symbol index. More...
 
std::valarray< bool > const & delta (size_t qi, char32_t symbol) const
 Computes this NFA's transition function for a state index and a symbol. More...
 
std::valarray< bool > const & delta (size_t qi, std::string const &utf8Symbol) const
 Same as above for a UTF-8-encoded symbol. More...
 
state_set delta (std::string const &q, char32_t symbol) const
 Computes this NFA's transition function for a state index and a symbol. More...
 
state_set delta (std::string const &q, std::string const &utf8Symbol) const
 Same as above for a UTF-8-encoded symbol. More...
 
std::valarray< bool > deltaHat (size_t qi, std::u32string const &word) const
 Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its index. More...
 
std::valarray< bool > deltaHat (size_t qi, std::string const &utf8Word) const
 Same as above for a UTF-8-encoded string of symbols. More...
 
state_set deltaHat (std::string const &q, std::u32string const &word) const
 Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its name. More...
 
state_set deltaHat (std::string const &q, std::string const &utf8Word) const
 Same as above for a UTF-8-encoded string of symbols. More...
 
std::valarray< bool > deltaHat (std::valarray< bool > const &qs, std::u32string const &word) const
 Computes this NFA's transition function recursively for a string of symbols, starting in multiple specified states. More...
 
std::valarray< bool > deltaHat (std::valarray< bool > const &qs, std::string const &utf8Word) const
 Same as above for a UTF-8-encoded string of symbols. More...
 
state_set deltaHat (state_set const &qs, std::u32string const &word) const
 Computes this NFA's transition function recursively for a string of symbols, starting in multiple states specified by their names. More...
 
state_set deltaHat (state_set const &qs, std::string const &utf8Word) const
 Same as above for a UTF-8-encoded string of symbols. More...
 
std::valarray< bool > const & epsilonClosure (size_t qi) const
 Computes a state's ε-closure. More...
 
state_set epsilonClosure (std::string const &q) const
 Computes a state's ε-closure. More...
 
std::valarray< bool > epsilonClosure (std::valarray< bool > const &qs) const
 Computes the union of multiple states' ε-closures. More...
 
state_set epsilonClosure (state_set const &qs) const
 Computes the union of multiple states' ε-closures. More...
 
std::u32string getShortestWord () const
 
std::string getShortestUtf8Word () const
 
std::string const & getLabelOf (size_t q) const
 
std::string getLabelOf (std::valarray< bool > const &qs) const
 
std::string getLabelOf (state_set const &qs) const
 
std::string to_string (std::valarray< bool > const &qs) const
 Puts a name to a set of indices. More...
 
std::string to_string (state_set const &qs) const
 Puts a name to a set of states. More...
 
state_set decodeSet (std::valarray< bool > const &qs) const
 Translates a set of states from bool to set representation. More...
 
std::valarray< bool > encodeSet (state_set const &qs) const
 Translates a set of states from set to bool representation. More...
 
std::string const & getInitialState () const
 Names this NFA's initial state. More...
 
std::vector< std::string > const & getStates () const
 Fetches this NFA's set of states in order of internal representation. More...
 
state_set getStatesSet () const
 Fetches this NFA's set of states as a set of (references to) strings. More...
 
std::valarray< bool > getStatesBools () const
 Fetches this NFA's set of states encoded as an array of bools. More...
 
std::vector< char32_t > const & getAlphabet () const
 Fetches this NFA's set of processable symbols. More...
 
std::vector< std::string > const & getUtf8Alphabet () const
 Fetches this NFA's set of processable symbols as UTF-8-encoded strings. More...
 
size_t getNumberOfStates () const
 
size_t getNumberOfSymbols () const
 
bool isAccepting (size_t qi) const
 Tests whether a state is an accept state within this NFA. More...
 
bool isAccepting (std::string const &q) const
 Tests whether a state is an accept state within this NFA. More...
 
bool isAccepting (std::valarray< bool > const &qs) const
 Tests whether a set of states contains an accept state within this NFA. More...
 
bool isAccepting (state_set const &qs) const
 Tests whether a set of states contains an accept state within this NFA. More...
 
bool hasAccepting (std::valarray< bool > const &qs) const
 
bool hasAccepting (state_set const &qs) const
 

Static Public Member Functions

static nfa::builder unite (nfa const &n1, nfa const &n2)
 Creates a builder for an NFA accepting the union of the languages of two NFAs. More...
 
static nfa::builder intersect (nfa const &n1, nfa const &n2)
 Creates a builder for an NFA accepting the intersection of the languages of two NFAs. More...
 
static nfa::builder subtract (nfa const &n1, nfa const &n2)
 Creates a builder for an NFA accepting the set difference of the languages of two NFAs. More...
 
static nfa::builder complement (nfa const &n)
 Creates a builder for an NFA accepting the complement of the language of an NFA. More...
 

Friends

std::u32string findShortestWord (nfa const &n)
 Searches the shortest UTF-32-encoded word accepted by a given NFA. More...
 
std::string findShortestUtf8Word (nfa const &n)
 Same as above for a UTF-8-encoded word. More...
 

Detailed Description

Represents nondeterministic finite automata with ε-moves.

Instances of this class are completely immutable. The builder class is the preferred way of constructing NFAs.

By convention, the state with index 0 is the initial state.

Definition at line 25 of file nfa.h.

Member Typedef Documentation

◆ state_set

Nicer name for sets of names of states. Should store references to names in getStates.

Definition at line 28 of file nfa.h.

Constructor & Destructor Documentation

◆ nfa() [1/5]

reg::nfa::nfa ( )

Constructs an NFA accepting the empty language ∅.

Definition at line 79 of file nfa.cpp.

79 : p(new pImpl) {}

◆ nfa() [2/5]

reg::nfa::nfa ( nfa const &  n)

Copy-constructs an NFA by copying another one's private implementation object.

Definition at line 622 of file nfa.cpp.

622 : p(new pImpl(*(n.p))) {}

◆ nfa() [3/5]

reg::nfa::nfa ( nfa &&  n)

Move-constructs an NFA by stealing another one's private implementation object.

The other NFA will be accepting the empty language ∅ afterwards.

Definition at line 626 of file nfa.cpp.

626 : p(new pImpl) {p.swap(n.p);}

◆ nfa() [4/5]

reg::nfa::nfa ( dfa const &  d)

Constructs an NFA from a DFA.

Definition at line 632 of file nfa.cpp.

632 : nfa(builder(d).build()) {}
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:79

◆ nfa() [5/5]

reg::nfa::nfa ( nfa::builder b)

Constructs an NFA from a builder.

Definition at line 629 of file nfa.cpp.

629 : nfa(b.build()) {}
nfa()
Constructs an NFA accepting the empty language ∅.
Definition: nfa.cpp:79

Member Function Documentation

◆ complement()

nfa::builder reg::nfa::complement ( nfa const &  n)
static

Creates a builder for an NFA accepting the complement of the language of an NFA.

The input NFAs' state names will probably be mangled in the created NFA.

Parameters
nthe NFA
Returns
builder for an NFA accepting all words not accepted by the input NFA (provided they can be built from symbols of that NFA's alphabet)

Definition at line 617 of file nfa.cpp.

617  {
618  return nfa::builder(dfa::builder(n).complement().minimize().build());
619 }
static nfa::builder complement(nfa const &n)
Creates a builder for an NFA accepting the complement of the language of an NFA.
Definition: nfa.cpp:617

◆ decodeSet()

nfa::state_set reg::nfa::decodeSet ( std::valarray< bool > const &  qs) const

Translates a set of states from bool to set representation.

Parameters
qsvalarray with true at indices of states in question
Returns
set of labels of the states in question

Definition at line 380 of file nfa.cpp.

380  {
381  state_set states;
382  states.reserve(getNumberOfStates());
383  size_t range = min(qs.size(), getNumberOfStates());
384  for (size_t q = 0; q < range; q++) {
385  if (qs[q]) {
386  states.emplace(getStates()[q]);
387  }
388  }
389  return states;
390 }
T min(T... args)
std::unordered_set< std::reference_wrapper< std::string const >, hash_reference_string_const, equal_to_reference_string_const > state_set
Nicer name for sets of names of states. Should store references to names in getStates.
Definition: nfa.h:28
size_t getNumberOfStates() const
Definition: nfa.cpp:454
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ delta() [1/5]

valarray< bool > const & reg::nfa::delta ( size_t  qi,
size_t  si 
) const

Computes this NFA's transition function for a state index and a symbol index.

Parameters
qithe state's index (< getNumberOfStates)
siindex of the symbol to process (∈ getAlphabet)
Returns
valarray with true at reached states' indices
Exceptions
std::out_of_rangeif qigetNumberOfStates
std::domain_errorif sigetNumberOfSymbols

Definition at line 104 of file nfa.cpp.

104  {
105  if (si >= p->alphabet.size()) {
106  throw std::domain_error("There is no symbol with index " + std::to_string(si));
107  }
108  if (qi >= p->labels.size()) {
109  throw std::out_of_range("There is no state with index " + std::to_string(qi));
110  }
111  return p->transitions[qi][si];
112 }

◆ delta() [2/5]

valarray< bool > const & reg::nfa::delta ( size_t  qi,
char32_t  symbol 
) const

Computes this NFA's transition function for a state index and a symbol.

Parameters
qithe state's index (< getNumberOfStates)
symbolthe symbol to process (∈ getAlphabet)
Returns
valarray with true at reached states' indices
Exceptions
std::out_of_rangeif qigetNumberOfStates
std::domain_errorif symbolgetAlphabet

Definition at line 122 of file nfa.cpp.

122  {
123  try {
124  return delta(qi, index_of(p->alphabet, symbol));
125  } catch (std::domain_error e) {
126  throw std::domain_error(u8"δ is not defined for symbol " + converter.to_bytes(symbol));
127  }
128 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
size_t index_of(C const &container, T const &element)
Basically Java&#39;s List interface&#39;s indexOf, but as a non-member function and returning the container&#39;s...
Definition: dfa.h:120
std::valarray< bool > const & delta(size_t qi, size_t si) const
Computes this NFA's transition function for a state index and a symbol index.
Definition: nfa.cpp:104

◆ delta() [3/5]

valarray< bool > const & reg::nfa::delta ( size_t  qi,
std::string const &  utf8Symbol 
) const

Same as above for a UTF-8-encoded symbol.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 131 of file nfa.cpp.

131  {
132  return delta(qi, converter.from_bytes(utf8Symbol)[0]);
133 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
std::valarray< bool > const & delta(size_t qi, size_t si) const
Computes this NFA's transition function for a state index and a symbol index.
Definition: nfa.cpp:104

◆ delta() [4/5]

nfa::state_set reg::nfa::delta ( std::string const &  q,
char32_t  symbol 
) const

Computes this NFA's transition function for a state index and a symbol.

Parameters
qthe state's label (∈ getStates)
symbolthe symbol to process (∈ getAlphabet)
Returns
set of reached states' labels
Exceptions
std::out_of_rangeif qigetNumberOfStates
std::domain_errorif symbolgetAlphabet

Definition at line 143 of file nfa.cpp.

143  {
144  try {
145  return decodeSet(delta(index_of(getStates(), q), symbol));
146  } catch (std::out_of_range e) {
147  throw std::out_of_range("There is no state named " + q);
148  }
149 }
size_t index_of(C const &container, T const &element)
Basically Java&#39;s List interface&#39;s indexOf, but as a non-member function and returning the container&#39;s...
Definition: dfa.h:120
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:380
std::valarray< bool > const & delta(size_t qi, size_t si) const
Computes this NFA's transition function for a state index and a symbol index.
Definition: nfa.cpp:104
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ delta() [5/5]

nfa::state_set reg::nfa::delta ( std::string const &  q,
std::string const &  utf8Symbol 
) const

Same as above for a UTF-8-encoded symbol.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 152 of file nfa.cpp.

152  {
153  return delta(q, converter.from_bytes(utf8Symbol)[0]);
154 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
std::valarray< bool > const & delta(size_t qi, size_t si) const
Computes this NFA's transition function for a state index and a symbol index.
Definition: nfa.cpp:104

◆ deltaHat() [1/8]

valarray< bool > reg::nfa::deltaHat ( size_t  qi,
std::u32string const &  word 
) const

Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its index.

Parameters
qithe starting state's index (< getNumberOfStates)
wordthe string of symbols to process (all of which ∈ getAlphabet)
Returns
valarray with true at reached states' indices
Exceptions
std::out_of_rangeif qigetNumberOfStates
std::domain_errorif one the word symbols ∉ getAlphabet

Definition at line 164 of file nfa.cpp.

164  {
165  if (qi >= p->labels.size()) {
166  throw std::out_of_range("There is no state with index " + std::to_string(qi));
167  }
168  valarray<bool> qs(p->labels.size());
169  qs[qi] = true;
170  return deltaHat(qs, word);
171 }
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:164

◆ deltaHat() [2/8]

valarray< bool > reg::nfa::deltaHat ( size_t  qi,
std::string const &  utf8Word 
) const

Same as above for a UTF-8-encoded string of symbols.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 174 of file nfa.cpp.

174  {
175  return deltaHat(qi, converter.from_bytes(utf8Word));
176 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:164

◆ deltaHat() [3/8]

nfa::state_set reg::nfa::deltaHat ( std::string const &  q,
std::u32string const &  word 
) const

Computes this NFA's transition function recursively for a string of symbols, starting in a state specified by its name.

Parameters
qthe starting state's label (∈ getStates)
wordthe string of symbols to process (all of which ∈ getAlphabet)
Returns
set of reached states' labels
Exceptions
std::out_of_rangeif qgetStates
std::domain_errorif one the word symbols ∉ getAlphabet

Definition at line 186 of file nfa.cpp.

186  {
187  try {
188  return decodeSet(deltaHat(index_of(getStates(), q), word));
189  } catch (std::out_of_range e) {
190  throw std::out_of_range("There is no state named " + q);
191  }
192 }
size_t index_of(C const &container, T const &element)
Basically Java&#39;s List interface&#39;s indexOf, but as a non-member function and returning the container&#39;s...
Definition: dfa.h:120
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:380
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:164
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ deltaHat() [4/8]

nfa::state_set reg::nfa::deltaHat ( std::string const &  q,
std::string const &  utf8Word 
) const

Same as above for a UTF-8-encoded string of symbols.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 195 of file nfa.cpp.

195  {
196  return deltaHat(q, converter.from_bytes(utf8Word));
197 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:164

◆ deltaHat() [5/8]

valarray< bool > reg::nfa::deltaHat ( std::valarray< bool > const &  qs,
std::u32string const &  word 
) const

Computes this NFA's transition function recursively for a string of symbols, starting in multiple specified states.

Parameters
qsvalarray with true at starting states' indices
wordthe string of symbols to process (all of which ∈ getAlphabet)
Returns
valarray with true at reached states' indices
Exceptions
std::domain_errorif one of the word symbols ∉ getAlphabet

Definition at line 206 of file nfa.cpp.

206  {
208  for (char32_t symbol : word) {
209  valarray<bool> reached(p->labels.size());
210  size_t qi(0);
211  for (bool qb : ps) {
212  if (qb) {
213  reached |= epsilonClosure(delta(qi, symbol));
214  }
215  qi++;
216  }
217  ps = move(reached);
218  }
219  return ps;
220 }
T move(T... args)
std::valarray< bool > const & delta(size_t qi, size_t si) const
Computes this NFA's transition function for a state index and a symbol index.
Definition: nfa.cpp:104
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:259

◆ deltaHat() [6/8]

valarray< bool > reg::nfa::deltaHat ( std::valarray< bool > const &  qs,
std::string const &  utf8Word 
) const

Same as above for a UTF-8-encoded string of symbols.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 223 of file nfa.cpp.

223  {
224  return deltaHat(qs, converter.from_bytes(utf8Word));
225 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:164

◆ deltaHat() [7/8]

nfa::state_set reg::nfa::deltaHat ( nfa::state_set const &  qs,
std::u32string const &  word 
) const

Computes this NFA's transition function recursively for a string of symbols, starting in multiple states specified by their names.

Parameters
qsset of starting states' labels
wordthe string of symbols to process (all of which ∈ getAlphabet)
Returns
set of reached states' labels
Exceptions
std::domain_errorif one of the word symbols ∉ getAlphabet

Definition at line 234 of file nfa.cpp.

234  {
235  return decodeSet(deltaHat(encodeSet(qs), word));
236 }
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:397
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:380
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:164

◆ deltaHat() [8/8]

nfa::state_set reg::nfa::deltaHat ( nfa::state_set const &  qs,
std::string const &  utf8Word 
) const

Same as above for a UTF-8-encoded string of symbols.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 239 of file nfa.cpp.

239  {
240  return deltaHat(qs, converter.from_bytes(utf8Word));
241 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
std::valarray< bool > deltaHat(size_t qi, std::u32string const &word) const
Computes this NFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: nfa.cpp:164

◆ encodeSet()

valarray< bool > reg::nfa::encodeSet ( nfa::state_set const &  qs) const

Translates a set of states from set to bool representation.

Parameters
qsset of labels of the states in question
Returns
valarray with true at indices of states in question

Definition at line 397 of file nfa.cpp.

397  {
399  for (size_t qi = 0; qi < getNumberOfStates(); qi++) {
400  states[qi] = qs.count(getStates()[qi]);
401  }
402  return states;
403 }
size_t getNumberOfStates() const
Definition: nfa.cpp:454
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ epsilonClosure() [1/4]

valarray< bool > const & reg::nfa::epsilonClosure ( size_t  qi) const

Computes a state's ε-closure.

Parameters
qithe state's index (< getNumberOfStates)
Returns
valarray with true at indices of states reachable without actual inputs
Exceptions
std::out_of_rangeif qigetNumberOfStates

Definition at line 259 of file nfa.cpp.

259  {
260  if (qi >= p->labels.size()) {
261  throw std::out_of_range("There is no state with index " + std::to_string(qi));
262  }
263  if (p->epsClosures[qi].size() == 0) {
264  p->epsClosures[qi].resize(p->labels.size());
265  p->epsClosures[qi][qi] = true;
266  int growth(1);
267  while (growth > 0) {
268  growth = 0;
269  valarray<bool> old(p->epsClosures[qi]);
270  size_t qqi(0);
271  for (bool qqb : old) {
272  if (qqb) {
273  size_t pi(0);
274  for (bool pb : p->transitions[qqi][0]) {
275  if (pb) {
276  if (!p->epsClosures[qi][pi]) {
277  growth++;
278  p->epsClosures[qi][pi] = true;
279  }
280  }
281  pi++;
282  } // going through the true bools in transitions[qqi][0]
283  }
284  qqi++;
285  } // going through the true bools in old
286  }
287  }
288  return p->epsClosures[qi];
289 }

◆ epsilonClosure() [2/4]

nfa::state_set reg::nfa::epsilonClosure ( std::string const &  q) const

Computes a state's ε-closure.

Parameters
qthe state's label (∈ getStates)
Returns
set of labels of states reachable without actual inputs
Exceptions
std::out_of_rangeif qgetNumberOfStates

Definition at line 297 of file nfa.cpp.

297  {
298  try {
300  } catch (std::out_of_range e) {
301  throw std::out_of_range("There is no state named " + q);
302  }
303 }
size_t index_of(C const &container, T const &element)
Basically Java&#39;s List interface&#39;s indexOf, but as a non-member function and returning the container&#39;s...
Definition: dfa.h:120
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:380
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:259
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ epsilonClosure() [3/4]

valarray< bool > reg::nfa::epsilonClosure ( std::valarray< bool > const &  qs) const

Computes the union of multiple states' ε-closures.

Parameters
qsvalarray with true at indices of states in question
Returns
valarray with true at indices of states reachable without actual inputs

Definition at line 310 of file nfa.cpp.

310  {
311  valarray<bool> closure(p->labels.size());
312  size_t range = min(qs.size(), getNumberOfStates());
313  for (size_t q = 0; q < range; q++) {
314  if (qs[q]) {
315  closure |= epsilonClosure(q);
316  }
317  }
318  return closure;
319 }
T min(T... args)
size_t getNumberOfStates() const
Definition: nfa.cpp:454
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:259

◆ epsilonClosure() [4/4]

nfa::state_set reg::nfa::epsilonClosure ( nfa::state_set const &  qs) const

Computes the union of multiple states' ε-closures.

Parameters
qsset of labels of states in question
Returns
set of labels of states reachable without actual inputs

Definition at line 326 of file nfa.cpp.

326  {
327  return decodeSet(epsilonClosure(encodeSet(qs)));
328 }
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:397
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:380
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:259

◆ getAlphabet()

vector< char32_t > const & reg::nfa::getAlphabet ( ) const

Fetches this NFA's set of processable symbols.

Returns
a vector containing all the valid symbol inputs for delta and deltaHat

Definition at line 441 of file nfa.cpp.

441  {
442  return p->alphabet;
443 }

◆ getInitialState()

string const & reg::nfa::getInitialState ( ) const

Names this NFA's initial state.

Returns
the initial state's name

Definition at line 409 of file nfa.cpp.

409  {
410  return getStates()[0];
411 }
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ getLabelOf() [1/3]

string const & reg::nfa::getLabelOf ( size_t  qi) const
Deprecated:
Use getStates and std::vector::at instead.

Definition at line 331 of file nfa.cpp.

331  {
332  return p->labels.at(qi);
333 }

◆ getLabelOf() [2/3]

string reg::nfa::getLabelOf ( std::valarray< bool > const &  qs) const
Deprecated:
Old name of to_string.

Definition at line 366 of file nfa.cpp.

366  {
367  return to_string(qs);
368 }
std::string to_string(std::valarray< bool > const &qs) const
Puts a name to a set of indices.
Definition: nfa.cpp:340

◆ getLabelOf() [3/3]

string reg::nfa::getLabelOf ( nfa::state_set const &  qs) const
Deprecated:
Old name of to_string.

Definition at line 371 of file nfa.cpp.

371  {
372  return to_string(qs);
373 }
std::string to_string(std::valarray< bool > const &qs) const
Puts a name to a set of indices.
Definition: nfa.cpp:340

◆ getNumberOfStates()

size_t reg::nfa::getNumberOfStates ( ) const
Deprecated:
Use getStates and std::vector::size instead.

Definition at line 454 of file nfa.cpp.

454  {
455  return getStates().size();
456 }
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ getNumberOfSymbols()

size_t reg::nfa::getNumberOfSymbols ( ) const
Deprecated:
Use getStates and std::vector::size instead.

Definition at line 459 of file nfa.cpp.

459  {
460  return getAlphabet().size();
461 }
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA's set of processable symbols.
Definition: nfa.cpp:441

◆ getShortestUtf8Word()

string reg::nfa::getShortestUtf8Word ( ) const
Deprecated:
Old way of accessing reg::findShortestUtf8Word.

Definition at line 249 of file nfa.cpp.

249  {
250  return findShortestUtf8Word(*this);
251 }
friend std::string findShortestUtf8Word(nfa const &n)
Same as above for a UTF-8-encoded word.
Definition: nfa.cpp:564

◆ getShortestWord()

u32string reg::nfa::getShortestWord ( ) const
Deprecated:
Old way of accessing reg::findShortestWord.

Definition at line 244 of file nfa.cpp.

244  {
245  return findShortestWord(*this);
246 }
friend std::u32string findShortestWord(nfa const &n)
Searches the shortest UTF-32-encoded word accepted by a given NFA.
Definition: nfa.cpp:535

◆ getStates()

vector< string > const & reg::nfa::getStates ( ) const

Fetches this NFA's set of states in order of internal representation.

Returns
a vector containing the names of states that can be used as input for delta and deltaHat

Definition at line 417 of file nfa.cpp.

417  {
418  return p->labels;
419 }

◆ getStatesBools()

valarray< bool > reg::nfa::getStatesBools ( ) const

Fetches this NFA's set of states encoded as an array of bools.

Returns
an array of length getNumberOfStates, filled with true values

Definition at line 433 of file nfa.cpp.

433  {
434  return valarray<bool>(true, getNumberOfStates());
435 }
size_t getNumberOfStates() const
Definition: nfa.cpp:454

◆ getStatesSet()

nfa::state_set reg::nfa::getStatesSet ( ) const

Fetches this NFA's set of states as a set of (references to) strings.

Returns
a set containing references to the original state names

Definition at line 425 of file nfa.cpp.

425  {
426  return state_set(getStates().begin(), getStates().end());
427 }
T end(T... args)
std::unordered_set< std::reference_wrapper< std::string const >, hash_reference_string_const, equal_to_reference_string_const > state_set
Nicer name for sets of names of states. Should store references to names in getStates.
Definition: nfa.h:28
T begin(T... args)
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ getUtf8Alphabet()

vector< string > const & reg::nfa::getUtf8Alphabet ( ) const

Fetches this NFA's set of processable symbols as UTF-8-encoded strings.

Returns
a vector containing all the valid symbol inputs for delta and deltaHat

Definition at line 449 of file nfa.cpp.

449  {
450  return p->utf8Alphabet;
451 }

◆ hasAccepting() [1/2]

bool reg::nfa::hasAccepting ( std::valarray< bool > const &  qs) const
Deprecated:
Old name of hasAccepting.

Definition at line 520 of file nfa.cpp.

520  {
521  return isAccepting(qs);
522 }
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:469

◆ hasAccepting() [2/2]

bool reg::nfa::hasAccepting ( nfa::state_set const &  qs) const
Deprecated:
Old name of hasAccepting.

Definition at line 525 of file nfa.cpp.

525  {
526  return isAccepting(qs);
527 }
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:469

◆ intersect()

nfa::builder reg::nfa::intersect ( nfa const &  n1,
nfa const &  n2 
)
static

Creates a builder for an NFA accepting the intersection of the languages of two NFAs.

The input NFAs' state names will be concatenated and collisions resolved by appending _ in the created NFA.

Parameters
n1the first NFA
n2the other NFA
Returns
builder for an NFA accepting all the words accepted by both of the input NFAs

Definition at line 588 of file nfa.cpp.

588  {
589  return builder(n1).intersect(n2);
590 }

◆ isAccepting() [1/4]

bool reg::nfa::isAccepting ( size_t  qi) const

Tests whether a state is an accept state within this NFA.

Parameters
qithe state's index (< getNumberOfStates)
Returns
true if the state is in the set of accept states, false else
Exceptions
std::out_of_rangeif qigetNumberOfStates

Definition at line 469 of file nfa.cpp.

469  {
470  if (qi >= p->labels.size()) {
471  throw std::out_of_range("There is no state with index " + std::to_string(qi));
472  }
473  return p->accepting[qi];
474 }

◆ isAccepting() [2/4]

bool reg::nfa::isAccepting ( std::string const &  q) const

Tests whether a state is an accept state within this NFA.

Parameters
qthe state's label (∈ getStates)
Returns
true if the state is in the set of accept states, false else
Exceptions
std::out_of_rangeif qgetStates

Definition at line 482 of file nfa.cpp.

482  {
483  try {
484  return isAccepting(index_of(getStates(), q));
485  } catch (std::out_of_range e) {
486  throw std::out_of_range("There is no state named " + q);
487  }
488 }
size_t index_of(C const &container, T const &element)
Basically Java&#39;s List interface&#39;s indexOf, but as a non-member function and returning the container&#39;s...
Definition: dfa.h:120
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:469
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ isAccepting() [3/4]

bool reg::nfa::isAccepting ( std::valarray< bool > const &  qs) const

Tests whether a set of states contains an accept state within this NFA.

Parameters
qsvalarray with true at indices of states in question
Returns
true if the set of states contains an accept states, false else

Definition at line 495 of file nfa.cpp.

495  {
496  size_t range = min(qs.size(), getNumberOfStates());
497  for (size_t q = 0; q < range; q++) {
498  if (qs[q] && p->accepting[q]) {
499  return true;
500  }
501  }
502  return false;
503 }
T min(T... args)
size_t getNumberOfStates() const
Definition: nfa.cpp:454

◆ isAccepting() [4/4]

bool reg::nfa::isAccepting ( nfa::state_set const &  qs) const

Tests whether a set of states contains an accept state within this NFA.

Parameters
qsset of labels of states in question
Returns
true if the set of states contains an accept states, false else

Definition at line 510 of file nfa.cpp.

510  {
511  for (size_t qi = 0; qi < getNumberOfStates(); qi++) {
512  if (p->accepting[qi] && qs.count(getStates()[qi])) {
513  return true;
514  }
515  }
516  return false;
517 }
size_t getNumberOfStates() const
Definition: nfa.cpp:454
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:417

◆ operator!=()

bool reg::nfa::operator!= ( nfa const &  n) const

Tests whether this NFA doesn't accept the same language as another one.

Definition at line 92 of file nfa.cpp.

92  {
93  return p->getEquivalentDfa(this) != n.p->getEquivalentDfa(&n);
94 }

◆ operator=() [1/2]

nfa & reg::nfa::operator= ( nfa const &  n)

Copy-assigns this NFA by copying another one's private implementation object.

Definition at line 635 of file nfa.cpp.

635  {
636  if (this != &n) {
637  p.reset(new pImpl(*(n.p)));
638  }
639  return *this;
640 }

◆ operator=() [2/2]

nfa & reg::nfa::operator= ( nfa &&  n)

Move-assigns this NFA by stealing another one's private implementation object.

The other NFA will be accepting the empty language ∅ afterwards.

Definition at line 644 of file nfa.cpp.

644  {
645  if (this != &n) {
646  p.reset(new pImpl);
647  p.swap(n.p);
648  }
649  return *this;
650 }

◆ operator==()

bool reg::nfa::operator== ( nfa const &  n) const

Tests whether this NFA accepts exactly the same language as another one.

Definition at line 87 of file nfa.cpp.

87  {
88  return p->getEquivalentDfa(this) == n.p->getEquivalentDfa(&n);
89 }

◆ subtract()

nfa::builder reg::nfa::subtract ( nfa const &  n1,
nfa const &  n2 
)
static

Creates a builder for an NFA accepting the set difference of the languages of two NFAs.

The input NFAs' state names will probably be mangled in the created NFA.

Parameters
n1the first NFA
n2the other NFA
Returns
builder for an NFA accepting all the words accepted by the first but not the other input NFA

Definition at line 600 of file nfa.cpp.

600  {
601  dfa::builder b2(n2);
602  for (auto symbol : n1.getAlphabet()) {
603  b2.addSymbol(symbol);
604  }
605  b2.complement().minimize();
606  return builder(n1).intersect(builder(b2.build()).build());
607 }

◆ to_string() [1/2]

string reg::nfa::to_string ( std::valarray< bool > const &  qs) const

Puts a name to a set of indices.

Parameters
qsvalarray with true at indices of states in question
Returns
a comma-separated list of the states' names, surrounded by curly brackets

Definition at line 340 of file nfa.cpp.

340  {
341  string label;
342  size_t range = min(qs.size(), getNumberOfStates());
343  for (size_t q = 0; q < range; q++) {
344  if (qs[q]) {
345  label = label.append(1, label.length() == 0 ? '{' : ',');
346  label = label.append(p->labels.at(q));
347  }
348  }
349  if (label.length() == 0) {
350  return string("{}");
351  } else {
352  return label.append(1, '}');
353  }
354 }
T min(T... args)
size_t getNumberOfStates() const
Definition: nfa.cpp:454

◆ to_string() [2/2]

string reg::nfa::to_string ( nfa::state_set const &  qs) const

Puts a name to a set of states.

Parameters
qsset of labels of states in question
Returns
a comma-separated list of the states' names, surrounded by curly brackets

Definition at line 361 of file nfa.cpp.

361  {
362  return getLabelOf(encodeSet(qs));
363 }
std::string const & getLabelOf(size_t q) const
Definition: nfa.cpp:331
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:397

◆ unite()

nfa::builder reg::nfa::unite ( nfa const &  n1,
nfa const &  n2 
)
static

Creates a builder for an NFA accepting the union of the languages of two NFAs.

The input NFAs' state names will be prepended with either 1 or 2 in the created NFA.

Parameters
n1the first NFA
n2the other NFA
Returns
builder for an NFA accepting all the words accepted by any of the input NFAs

Definition at line 576 of file nfa.cpp.

576  {
577  return builder(n1).unite(n2);
578 }

Friends And Related Function Documentation

◆ findShortestUtf8Word

std::string findShortestUtf8Word ( nfa const &  n)
friend

Same as above for a UTF-8-encoded word.

Definition at line 564 of file nfa.cpp.

564  {
565  return converter.to_bytes(findShortestWord(n));
566 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
friend std::u32string findShortestWord(nfa const &n)
Searches the shortest UTF-32-encoded word accepted by a given NFA.
Definition: nfa.cpp:535

◆ findShortestWord

std::u32string findShortestWord ( nfa const &  n)
friend

Searches the shortest UTF-32-encoded word accepted by a given NFA.

Parameters
nthe NFA
Returns
the shortest word leading to one of the NFA's accept states
Exceptions
std::logic_errorif the NFA doesn't accept any words

Definition at line 535 of file nfa.cpp.

535  {
536  auto const& p = n.p;
537  if (p->accepting[0]) {
538  return U"";
539  }
540  unordered_map<size_t, u32string> shortestWords(p->labels.size());
541  size_t oldSize = 0;
542  shortestWords.emplace(0, U"");
543  while (shortestWords.size() > oldSize) {
544  oldSize = shortestWords.size();
545  for (auto const& stateWord : shortestWords) {
546  for (auto symbol : p->alphabet) {
547  valarray<bool> reached = n.deltaHat(stateWord.first, u32string(!!symbol, symbol));
548  u32string shWord = stateWord.second + u32string(!!symbol, symbol);
549  for (size_t q = 0; q < reached.size(); q++) { if (reached[q]) {
550  if (p->accepting[q]) {
551  return shWord;
552  }
553  if (shortestWords.find(q) == shortestWords.end()) {
554  shortestWords.emplace(q, shWord);
555  }
556  }}
557  }
558  }
559  }
560  throw std::logic_error("This NFA doesn't accept any words!");
561 }

The documentation for this class was generated from the following files: