reglibcpp  1.7.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 existing state names. 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...
 
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...
 
 operator dfa const & () const
 Returns a DFA accepting the same language as this NFA. More...
 
bool operator== (nfa const &n) const
 Checks whether this NFA describes the same regular language as another object. More...
 
bool operator!= (nfa const &n) const
 Checks whether this NFA describes a different regular language than another object. 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 23 of file nfa.h.

Member Typedef Documentation

◆ state_set

Nicer name for sets of names of states. Should store references to existing state names.

Definition at line 26 of file nfa.h.

Constructor & Destructor Documentation

◆ nfa() [1/3]

reg::nfa::nfa ( )

Constructs an NFA accepting the empty language ∅.

Definition at line 68 of file nfa.cpp.

68 : p(new pImpl) {}

◆ nfa() [2/3]

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

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

Definition at line 626 of file nfa.cpp.

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

◆ nfa() [3/3]

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

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

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

621  {
622  return nfa::builder(dfa::builder(n).complement().minimize().build());
623 }
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:621

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

384  {
385  nfa::state_set states;
386  states.reserve(getNumberOfStates());
387  size_t range = min(qs.size(), getNumberOfStates());
388  for (size_t q = 0; q < range; q++) {
389  if (qs[q]) {
390  states.emplace(getStates()[q]);
391  }
392  }
393  return states;
394 }
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 existing state names...
Definition: nfa.h:26
size_t getNumberOfStates() const
Definition: nfa.cpp:458
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:421

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

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

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

126  {
127  try {
128  return delta(qi, index_of(p->alphabet, symbol));
129  } catch (std::domain_error e) {
130  throw std::domain_error(u8"δ is not defined for symbol " + converter.to_bytes(symbol));
131  }
132 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
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: utils.h:19
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:108

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

135  {
136  return delta(qi, converter.from_bytes(utf8Symbol)[0]);
137 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
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:108

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

147  {
148  try {
149  return decodeSet(delta(index_of(getStates(), q), symbol));
150  } catch (std::out_of_range e) {
151  throw std::out_of_range("There is no state named " + q);
152  }
153 }
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: utils.h:19
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:384
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:108
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:421

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

156  {
157  return delta(q, converter.from_bytes(utf8Symbol)[0]);
158 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
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:108

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

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

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

178  {
179  return deltaHat(qi, converter.from_bytes(utf8Word));
180 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
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:168

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

190  {
191  try {
192  return decodeSet(deltaHat(index_of(getStates(), q), word));
193  } catch (std::out_of_range e) {
194  throw std::out_of_range("There is no state named " + q);
195  }
196 }
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: utils.h:19
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:384
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:168
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:421

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

199  {
200  return deltaHat(q, converter.from_bytes(utf8Word));
201 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
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:168

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

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

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

227  {
228  return deltaHat(qs, converter.from_bytes(utf8Word));
229 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
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:168

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

238  {
239  return decodeSet(deltaHat(encodeSet(qs), word));
240 }
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:401
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:384
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:168

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

243  {
244  return deltaHat(qs, converter.from_bytes(utf8Word));
245 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
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:168

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

401  {
403  for (size_t qi = 0; qi < getNumberOfStates(); qi++) {
404  states[qi] = qs.count(getStates()[qi]);
405  }
406  return states;
407 }
size_t getNumberOfStates() const
Definition: nfa.cpp:458
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:421

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

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

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

301  {
302  try {
304  } catch (std::out_of_range e) {
305  throw std::out_of_range("There is no state named " + q);
306  }
307 }
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: utils.h:19
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:384
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:263
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:421

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

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

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

330  {
331  return decodeSet(epsilonClosure(encodeSet(qs)));
332 }
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:401
state_set decodeSet(std::valarray< bool > const &qs) const
Translates a set of states from bool to set representation.
Definition: nfa.cpp:384
std::valarray< bool > const & epsilonClosure(size_t qi) const
Computes a state's ε-closure.
Definition: nfa.cpp:263

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

445  {
446  return p->alphabet;
447 }

◆ getInitialState()

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

Names this NFA's initial state.

Returns
the initial state's name

Definition at line 413 of file nfa.cpp.

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

◆ getLabelOf() [1/3]

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

Definition at line 335 of file nfa.cpp.

335  {
336  return p->labels.at(qi);
337 }

◆ getLabelOf() [2/3]

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

Definition at line 370 of file nfa.cpp.

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

◆ getLabelOf() [3/3]

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

Definition at line 375 of file nfa.cpp.

375  {
376  return to_string(qs);
377 }
std::string to_string(std::valarray< bool > const &qs) const
Puts a name to a set of indices.
Definition: nfa.cpp:344

◆ getNumberOfStates()

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

Definition at line 458 of file nfa.cpp.

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

◆ getNumberOfSymbols()

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

Definition at line 463 of file nfa.cpp.

463  {
464  return getAlphabet().size();
465 }
std::vector< char32_t > const & getAlphabet() const
Fetches this NFA's set of processable symbols.
Definition: nfa.cpp:445

◆ getShortestUtf8Word()

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

Definition at line 253 of file nfa.cpp.

253  {
254  return findShortestUtf8Word(*this);
255 }
friend std::string findShortestUtf8Word(nfa const &n)
Same as above for a UTF-8-encoded word.
Definition: nfa.cpp:568

◆ getShortestWord()

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

Definition at line 248 of file nfa.cpp.

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

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

421  {
422  return p->labels;
423 }

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

437  {
438  return valarray<bool>(true, getNumberOfStates());
439 }
size_t getNumberOfStates() const
Definition: nfa.cpp:458

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

429  {
430  return nfa::state_set(getStates().begin(), getStates().end());
431 }
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 existing state names...
Definition: nfa.h:26
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:421

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

453  {
454  return p->utf8Alphabet;
455 }

◆ hasAccepting() [1/2]

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

Definition at line 524 of file nfa.cpp.

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

◆ hasAccepting() [2/2]

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

Definition at line 529 of file nfa.cpp.

529  {
530  return isAccepting(qs);
531 }
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:473

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

592  {
593  return builder(n1).intersect(n2);
594 }

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

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

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

486  {
487  try {
488  return isAccepting(index_of(getStates(), q));
489  } catch (std::out_of_range e) {
490  throw std::out_of_range("There is no state named " + q);
491  }
492 }
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: utils.h:19
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this NFA.
Definition: nfa.cpp:473
std::vector< std::string > const & getStates() const
Fetches this NFA's set of states in order of internal representation.
Definition: nfa.cpp:421

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

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

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

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

◆ operator dfa const &()

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

Returns a DFA accepting the same language as this NFA.

◆ operator!=()

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

Checks whether this NFA describes a different regular language than another object.

Returns
false if this NFA's language is exactly the same as the other object's, true else

Definition at line 96 of file nfa.cpp.

96  {
97  return static_cast<dfa const&>(*this).operator!=(n);
98 }

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

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

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

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

◆ operator==()

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

Checks whether this NFA describes the same regular language as another object.

Returns
false if this NFA's language is exactly the same as the other object's, true else

Definition at line 88 of file nfa.cpp.

88  {
89  return static_cast<dfa const&>(*this).operator==(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 604 of file nfa.cpp.

604  {
605  dfa::builder b2(n2);
606  for (auto symbol : n1.getAlphabet()) {
607  b2.addSymbol(symbol);
608  }
609  b2.complement().minimize();
610  return builder(n1).intersect(builder(b2.build()).build());
611 }

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

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

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

365  {
366  return getLabelOf(encodeSet(qs));
367 }
std::string const & getLabelOf(size_t q) const
Definition: nfa.cpp:335
std::valarray< bool > encodeSet(state_set const &qs) const
Translates a set of states from set to bool representation.
Definition: nfa.cpp:401

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

580  {
581  return builder(n1).unite(n2);
582 }

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

568  {
569  return converter.to_bytes(findShortestWord(n));
570 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.cpp:4
friend std::u32string findShortestWord(nfa const &n)
Searches the shortest UTF-32-encoded word accepted by a given NFA.
Definition: nfa.cpp:539

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

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

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