reglibcpp  1.0.0
(Naïve) C++ implementation of models for regular languages
Classes | 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  pImpl
 Private implementation details of NFAs. 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...
 
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 q, char32_t symbol) const
 Computes this NFA's transition function for a state and a symbol. More...
 
std::valarray< bool > const & delta (size_t q, std::string const &utf8Symbol) const
 Same as above for a UTF-8-encoded symbol. More...
 
std::valarray< bool > deltaHat (size_t q, std::u32string const &word) const
 Computes this NFA's transition function recursively for a string of symbols, starting in a specified state. More...
 
std::valarray< bool > deltaHat (size_t 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...
 
std::valarray< bool > const & epsilonClosure (size_t 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...
 
std::string const & getLabelOf (size_t q) const
 Puts a name to an index. More...
 
std::string getLabelOf (std::valarray< bool > qs) const
 Puts a name to a set of indices. 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...
 
bool isAccepting (size_t q) const
 Tests whether a state is an accept state within this 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 22 of file nfa.h.

Constructor & Destructor Documentation

◆ nfa() [1/3]

reg::nfa::nfa ( )

Constructs an NFA accepting the empty language ∅.

Definition at line 82 of file nfa.cpp.

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

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

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

Member Function Documentation

◆ delta() [1/2]

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

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

Parameters
qthe state's index (< getNumberOfStates)
symbolthe symbol to process (∈ getAlphabet)
Returns
valarray with true at reached states' indices

Definition at line 106 of file nfa.cpp.

106  {
107  size_t i = distance(
108  p->alphabet.begin(),
109  find(p->alphabet.begin(), p->alphabet.end(), symbol)
110  );
111  if (i >= p->alphabet.size()) {
112  #ifdef __EXCEPTIONS
113  throw std::domain_error(
114  string(u8"δ is not defined for symbol ").append(1, symbol)
115  );
116  #endif
117  }
118  if (q >= p->labels.size()) {
119  #ifdef __EXCEPTIONS
120  throw std::out_of_range(
121  string("There is no state with index ").append(std::to_string(q))
122  );
123  #endif
124  }
125  return p->transitions[q][i];
126 }

◆ delta() [2/2]

valarray< bool > const & reg::nfa::delta ( size_t  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 129 of file nfa.cpp.

129  {
130  return delta(q, expression::converter->from_bytes(utf8Symbol)[0]);
131 }
static std::unique_ptr< std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > > const converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: expression.h:97
std::valarray< bool > const & delta(size_t q, char32_t symbol) const
Computes this NFA&#39;s transition function for a state and a symbol.
Definition: nfa.cpp:106

◆ deltaHat() [1/4]

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

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

Parameters
qthe starting state's index (< getNumberOfStates)
wordthe string of symbols to process (all of which ∈ getAlphabet)
Returns
valarray with true at reached states' indices

Definition at line 140 of file nfa.cpp.

140  {
141  valarray<bool> qs(p->labels.size());
142  qs[q] = true;
143  return deltaHat(qs, word);
144 }
std::valarray< bool > deltaHat(size_t q, std::u32string const &word) const
Computes this NFA&#39;s transition function recursively for a string of symbols, starting in a specified ...
Definition: nfa.cpp:140

◆ deltaHat() [2/4]

valarray< bool > reg::nfa::deltaHat ( size_t  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 147 of file nfa.cpp.

147  {
148  return deltaHat(q, expression::converter->from_bytes(utf8Word));
149 }
static std::unique_ptr< std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > > const converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: expression.h:97
std::valarray< bool > deltaHat(size_t q, std::u32string const &word) const
Computes this NFA&#39;s transition function recursively for a string of symbols, starting in a specified ...
Definition: nfa.cpp:140

◆ deltaHat() [3/4]

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 (the size of which == getNumberOfStates)
wordthe string of symbols to process (all of which ∈ getAlphabet)
Returns
valarray with true at reached states' indices

Definition at line 158 of file nfa.cpp.

158  {
159  valarray<bool> ps = epsilonClosure(qs);
160  for (char32_t symbol : word) {
161  valarray<bool> reached(p->labels.size());
162  size_t qi(0);
163  for (bool qb : ps) {
164  if (qb) {
165  reached |= epsilonClosure(delta(qi, symbol));
166  }
167  qi++;
168  }
169  ps = move(reached);
170  }
171  return ps;
172 }
std::valarray< bool > const & delta(size_t q, char32_t symbol) const
Computes this NFA&#39;s transition function for a state and a symbol.
Definition: nfa.cpp:106
std::valarray< bool > const & epsilonClosure(size_t q) const
Computes a state&#39;s ε-closure.
Definition: nfa.cpp:185

◆ deltaHat() [4/4]

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

175  {
176  return deltaHat(qs, expression::converter->from_bytes(utf8Word));
177 }
static std::unique_ptr< std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > > const converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: expression.h:97
std::valarray< bool > deltaHat(size_t q, std::u32string const &word) const
Computes this NFA&#39;s transition function recursively for a string of symbols, starting in a specified ...
Definition: nfa.cpp:140

◆ epsilonClosure() [1/2]

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

Computes a state's ε-closure.

Parameters
qthe state's index (< getNumberOfStates)
Returns
valarray with true at indices of states reachable without actual inputs

Definition at line 185 of file nfa.cpp.

185  {
186  if (p->epsClosures[q].size() == 0) {
187  p->epsClosures[q].resize(p->labels.size());
188  p->epsClosures[q][q] = true;
189  int growth(1);
190  while (growth > 0) {
191  growth = 0;
192  valarray<bool> old(p->epsClosures[q]);
193  size_t qi(0);
194  for (bool qb : old) {
195  if (qb) {
196  size_t pi(0);
197  for (bool pb : p->transitions[qi][0]) {
198  if (pb) {
199  if (!p->epsClosures[q][pi]) {
200  growth++;
201  p->epsClosures[q][pi] = true;
202  }
203  }
204  pi++;
205  } // going through the true bools in transitions[qi][0]
206  }
207  qi++;
208  } // going through the true bools in old
209  }
210  }
211  return p->epsClosures[q];
212 }

◆ epsilonClosure() [2/2]

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 (the size of which == getNumberOfStates)
Returns
valarray with true at indices of states reachable without actual inputs

Definition at line 220 of file nfa.cpp.

220  {
221  valarray<bool> closure(p->labels.size());
222  size_t qi(0);
223  for (bool qb : qs) {
224  if (qb) {
225  closure |= epsilonClosure(qi);
226  }
227  qi++;
228  } // going through the true bools in qs
229  return closure;
230 }
std::valarray< bool > const & epsilonClosure(size_t q) const
Computes a state&#39;s ε-closure.
Definition: nfa.cpp:185

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

268  {
269  return p->alphabet;
270 }

◆ getLabelOf() [1/2]

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

Puts a name to an index.

Parameters
qthe state's index (< getNumberOfStates)
Returns
the state's name

Definition at line 237 of file nfa.cpp.

237  {
238  return p->labels[q];
239 }

◆ getLabelOf() [2/2]

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

Puts a name to a set of indices.

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

Definition at line 246 of file nfa.cpp.

246  {
247  string label;
248  size_t qi(0);
249  for (bool qb : qs) {
250  if (qb) {
251  label = label.append(1, label.length() == 0 ? '{' : ',');
252  label = label.append(p->labels[qi]);
253  }
254  qi++;
255  }
256  if (label.length() == 0) {
257  return string("{}");
258  } else {
259  return label.append(1, '}');
260  }
261 }

◆ getNumberOfStates()

size_t reg::nfa::getNumberOfStates ( ) const

Counts this NFA's states.

Returns
the number of states this NFA has

Definition at line 286 of file nfa.cpp.

286  {
287  return p->labels.size();
288 }

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

277  {
278  return p->utf8Alphabet;
279 }

◆ isAccepting()

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

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

Parameters
qthe state's index (< getNumberOfStates)
Returns
true if the state is in the set of accept states, false else

Definition at line 296 of file nfa.cpp.

296  {
297  return p->accepting[q];
298 }

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

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

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

308  {
309  if (this != &n) {
310  p.reset(new pImpl(*(n.p)));
311  }
312  return *this;
313 }

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

317  {
318  if (this != &n) {
319  p.reset(new pImpl);
320  p.swap(n.p);
321  }
322  return *this;
323 }

◆ operator==()

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

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

Definition at line 90 of file nfa.cpp.

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

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