reglibcpp  1.5.0
(Naïve) C++ implementation of models for regular languages
Classes | Public Types | Public Member Functions | Static Public Member Functions | 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
 Searches the shortest UTF-32-encoded word accepted by this NFA. More...
 
std::string getShortestUtf8Word () const
 Same as above for a UTF-8-encoded word. More...
 
std::string const & getLabelOf (size_t q) const
 Puts a name to an index. More...
 
std::string getLabelOf (std::valarray< bool > const &qs) const
 Puts a name to a set of indices. More...
 
std::string getLabelOf (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
 Counts this NFA's states. More...
 
size_t getNumberOfSymbols () const
 Counts this NFA's alphabet symbols. More...
 
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 hasAccepting (std::valarray< bool > const &qs) const
 Tests whether a set of states contains an accept state within this NFA. More...
 
bool hasAccepting (state_set const &qs) const
 Tests whether a set of states contains an accept state within this NFA. More...
 

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...
 

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 80 of file nfa.cpp.

80 : 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 621 of file nfa.cpp.

621 : 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 625 of file nfa.cpp.

625 : 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 631 of file nfa.cpp.

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

◆ nfa() [5/5]

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

Constructs an NFA from a builder.

Definition at line 628 of file nfa.cpp.

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

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 616 of file nfa.cpp.

616  {
617  return nfa::builder(dfa::builder(n).complement().minimize().build());
618 }
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:616

◆ 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 405 of file nfa.cpp.

405  {
406  state_set states;
407  states.reserve(getNumberOfStates());
408  size_t range = min(qs.size(), getNumberOfStates());
409  for (size_t q = 0; q < range; q++) {
410  if (qs[q]) {
411  states.emplace(getStates()[q]);
412  }
413  }
414  return states;
415 }
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
Counts this NFA's states.
Definition: nfa.cpp:482
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442

◆ 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 105 of file nfa.cpp.

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

◆ 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 123 of file nfa.cpp.

123  {
124  try {
125  return delta(qi, index_of(p->alphabet, symbol));
126  } catch (std::domain_error e) {
127  throw std::domain_error(u8"δ is not defined for symbol " + converter.to_bytes(symbol));
128  }
129 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
size_t index_of(vector< T > const &vec, 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.cpp:1080
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:105

◆ 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 132 of file nfa.cpp.

132  {
133  return delta(qi, converter.from_bytes(utf8Symbol)[0]);
134 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
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:105

◆ 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 144 of file nfa.cpp.

144  {
145  try {
146  return decodeSet(delta(index_of(getStates(), q), symbol));
147  } catch (std::out_of_range e) {
148  throw std::out_of_range("There is no state named " + q);
149  }
150 }
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:405
size_t index_of(vector< T > const &vec, 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.cpp:1080
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:105
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442

◆ 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 153 of file nfa.cpp.

153  {
154  return delta(q, converter.from_bytes(utf8Symbol)[0]);
155 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
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:105

◆ 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 165 of file nfa.cpp.

165  {
166  if (qi >= p->labels.size()) {
167  throw std::out_of_range("There is no state with index " + to_string(qi));
168  }
169  valarray<bool> qs(p->labels.size());
170  qs[qi] = true;
171  return deltaHat(qs, word);
172 }
T to_string(T... args)
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:165

◆ 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 175 of file nfa.cpp.

175  {
176  return deltaHat(qi, converter.from_bytes(utf8Word));
177 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
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:165

◆ 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 187 of file nfa.cpp.

187  {
188  try {
189  return decodeSet(deltaHat(index_of(getStates(), q), word));
190  } catch (std::out_of_range e) {
191  throw std::out_of_range("There is no state named " + q);
192  }
193 }
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:405
size_t index_of(vector< T > const &vec, 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.cpp:1080
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:165
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442

◆ 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 196 of file nfa.cpp.

196  {
197  return deltaHat(q, converter.from_bytes(utf8Word));
198 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
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:165

◆ 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 207 of file nfa.cpp.

207  {
209  for (char32_t symbol : word) {
210  valarray<bool> reached(p->labels.size());
211  size_t qi(0);
212  for (bool qb : ps) {
213  if (qb) {
214  reached |= epsilonClosure(delta(qi, symbol));
215  }
216  qi++;
217  }
218  ps = move(reached);
219  }
220  return ps;
221 }
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:105
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:287

◆ 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 224 of file nfa.cpp.

224  {
225  return deltaHat(qs, converter.from_bytes(utf8Word));
226 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
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:165

◆ 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 235 of file nfa.cpp.

235  {
236  return decodeSet(deltaHat(encodeSet(qs), word));
237 }
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:422
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:405
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:165

◆ 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 240 of file nfa.cpp.

240  {
241  return deltaHat(qs, converter.from_bytes(utf8Word));
242 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
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:165

◆ 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 422 of file nfa.cpp.

422  {
424  for (size_t qi = 0; qi < getNumberOfStates(); qi++) {
425  states[qi] = qs.count(getStates()[qi]);
426  }
427  return states;
428 }
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:482
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442

◆ 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 287 of file nfa.cpp.

287  {
288  if (qi >= p->labels.size()) {
289  throw std::out_of_range(
290  string("There is no state with index ").append(std::to_string(qi))
291  );
292  }
293  if (p->epsClosures[qi].size() == 0) {
294  p->epsClosures[qi].resize(p->labels.size());
295  p->epsClosures[qi][qi] = true;
296  int growth(1);
297  while (growth > 0) {
298  growth = 0;
299  valarray<bool> old(p->epsClosures[qi]);
300  size_t qqi(0);
301  for (bool qqb : old) {
302  if (qqb) {
303  size_t pi(0);
304  for (bool pb : p->transitions[qqi][0]) {
305  if (pb) {
306  if (!p->epsClosures[qi][pi]) {
307  growth++;
308  p->epsClosures[qi][pi] = true;
309  }
310  }
311  pi++;
312  } // going through the true bools in transitions[qqi][0]
313  }
314  qqi++;
315  } // going through the true bools in old
316  }
317  }
318  return p->epsClosures[qi];
319 }

◆ 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 327 of file nfa.cpp.

327  {
328  try {
330  } catch (std::out_of_range e) {
331  throw std::out_of_range("There is no state named " + q);
332  }
333 }
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:405
size_t index_of(vector< T > const &vec, 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.cpp:1080
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:287
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442

◆ 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 340 of file nfa.cpp.

340  {
341  valarray<bool> closure(p->labels.size());
342  size_t range = min(qs.size(), getNumberOfStates());
343  for (size_t q = 0; q < range; q++) {
344  if (qs[q]) {
345  closure |= epsilonClosure(q);
346  }
347  }
348  return closure;
349 }
T min(T... args)
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:482
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:287

◆ 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 356 of file nfa.cpp.

356  {
357  return decodeSet(epsilonClosure(encodeSet(qs)));
358 }
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:422
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:405
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:287

◆ 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 466 of file nfa.cpp.

466  {
467  return p->alphabet;
468 }

◆ getInitialState()

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

Names this NFA's initial state.

Returns
the initial state's name

Definition at line 434 of file nfa.cpp.

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

◆ getLabelOf() [1/3]

string const & reg::nfa::getLabelOf ( size_t  qi) const

Puts a name to an index.

Parameters
qithe state's index (< getNumberOfStates)
Returns
the state's name
Exceptions
std::out_of_rangeif qgetNumberOfStates

Definition at line 366 of file nfa.cpp.

366  {
367  return p->labels.at(qi);
368 }

◆ getLabelOf() [2/3]

string reg::nfa::getLabelOf ( 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 375 of file nfa.cpp.

375  {
376  string label;
377  size_t range = min(qs.size(), getNumberOfStates());
378  for (size_t q = 0; q < range; q++) {
379  if (qs[q]) {
380  label = label.append(1, label.length() == 0 ? '{' : ',');
381  label = label.append(p->labels.at(q));
382  }
383  }
384  if (label.length() == 0) {
385  return string("{}");
386  } else {
387  return label.append(1, '}');
388  }
389 }
T min(T... args)
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:482

◆ getLabelOf() [3/3]

string reg::nfa::getLabelOf ( 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 396 of file nfa.cpp.

396  {
397  return getLabelOf(encodeSet(qs));
398 }
std::string const & getLabelOf(size_t q) const
Puts a name to an index.
Definition: nfa.cpp:366
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:422

◆ getNumberOfStates()

size_t reg::nfa::getNumberOfStates ( ) const

Counts this NFA's states.

Returns
the number of states this NFA has

Definition at line 482 of file nfa.cpp.

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

◆ getNumberOfSymbols()

size_t reg::nfa::getNumberOfSymbols ( ) const

Counts this NFA's alphabet symbols.

Returns
the number of symbols this DFA can process

Definition at line 490 of file nfa.cpp.

490  {
491  return getAlphabet().size();
492 }
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA's set of processable symbols.
Definition: nfa.cpp:466

◆ getShortestUtf8Word()

string reg::nfa::getShortestUtf8Word ( ) const

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

Definition at line 277 of file nfa.cpp.

277  {
278  return converter.to_bytes(getShortestWord());
279 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1088
std::u32string getShortestWord() const
Searches the shortest UTF-32-encoded word accepted by this NFA.
Definition: nfa.cpp:249

◆ getShortestWord()

u32string reg::nfa::getShortestWord ( ) const

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

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

Definition at line 249 of file nfa.cpp.

249  {
250  if (p->accepting[0]) {
251  return U"";
252  }
253  unordered_map<size_t, u32string> shortestWords(p->labels.size());
254  size_t oldSize = 0;
255  shortestWords.emplace(0, U"");
256  while (shortestWords.size() > oldSize) {
257  oldSize = shortestWords.size();
258  for (auto const& stateWord : shortestWords) {
259  for (auto symbol : p->alphabet) {
260  valarray<bool> reached = deltaHat(stateWord.first, u32string(!!symbol, symbol));
261  u32string shWord = stateWord.second + u32string(!!symbol, symbol);
262  for (size_t q = 0; q < reached.size(); q++) { if (reached[q]) {
263  if (p->accepting[q]) {
264  return shWord;
265  }
266  if (shortestWords.find(q) == shortestWords.end()) {
267  shortestWords.emplace(q, shWord);
268  }
269  }}
270  }
271  }
272  }
273  throw std::logic_error("This NFA doesn't accept any words!");
274 }
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:165

◆ 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 442 of file nfa.cpp.

442  {
443  return p->labels;
444 }

◆ 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 458 of file nfa.cpp.

458  {
459  return valarray<bool>(true, getNumberOfStates());
460 }
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:482

◆ 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 450 of file nfa.cpp.

450  {
451  return state_set(getStates().begin(), getStates().end());
452 }
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:442

◆ 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 474 of file nfa.cpp.

474  {
475  return p->utf8Alphabet;
476 }

◆ hasAccepting() [1/2]

bool reg::nfa::hasAccepting ( 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 528 of file nfa.cpp.

528  {
529  size_t range = min(qs.size(), getNumberOfStates());
530  for (size_t q = 0; q < range; q++) {
531  if (qs[q] && p->accepting[q]) {
532  return true;
533  }
534  }
535  return false;
536 }
T min(T... args)
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:482

◆ hasAccepting() [2/2]

bool reg::nfa::hasAccepting ( 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 543 of file nfa.cpp.

543  {
544  for (size_t qi = 0; qi < getNumberOfStates(); qi++) {
545  if (p->accepting[qi] && qs.count(getStates()[qi])) {
546  return true;
547  }
548  }
549  return false;
550 }
size_t getNumberOfStates() const
Counts this NFA's states.
Definition: nfa.cpp:482
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442

◆ 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 587 of file nfa.cpp.

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

◆ isAccepting() [1/2]

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 500 of file nfa.cpp.

500  {
501  if (qi >= p->labels.size()) {
502  throw std::out_of_range(
503  string("There is no state with index ").append(std::to_string(qi))
504  );
505  }
506  return p->accepting[qi];
507 }

◆ isAccepting() [2/2]

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 515 of file nfa.cpp.

515  {
516  try {
517  return isAccepting(index_of(getStates(), q));
518  } catch (std::out_of_range e) {
519  throw std::out_of_range("There is no state named " + q);
520  }
521 }
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:500
size_t index_of(vector< T > const &vec, 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.cpp:1080
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:442

◆ 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 93 of file nfa.cpp.

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

◆ 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 634 of file nfa.cpp.

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

◆ 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 643 of file nfa.cpp.

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

◆ operator==()

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

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

Definition at line 88 of file nfa.cpp.

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

◆ 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 599 of file nfa.cpp.

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

◆ 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 575 of file nfa.cpp.

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

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