reglibcpp  1.0.0
(Naïve) C++ implementation of models for regular languages
Classes | Public Member Functions | List of all members
reg::dfa Class Reference

Represents deterministic finite automata. More...

#include <dfa.h>

Classes

class  builder
 Constructs DFAs step by step. More...
 
struct  pImpl
 Private implementation details of DFAs. More...
 

Public Member Functions

 dfa ()
 Constructs a DFA accepting the empty language ∅. More...
 
 dfa (dfa &d)
 Copy-constructs a DFA by copying another one's private implementation object. More...
 
 dfa (dfa &&d)
 Move-constructs a DFA by stealing another one's private implementation object. More...
 
 dfa (builder &b)
 Constructs a DFA from a builder. More...
 
dfaoperator= (const dfa &d)
 Copy-assigns this DFA by copying another one's private implementation object. More...
 
dfaoperator= (dfa &&d)
 Move-assigns this DFA by stealing another one's private implementation object. More...
 
bool operator== (const dfa &d) const
 Tests whether this DFA accepts exactly the same language as another one. More...
 
bool operator!= (const dfa &d) const
 Tests whether this DFA doesn't accept the same language as another one. More...
 
size_t delta (size_t q, char32_t symbol) const
 Computes this DFA's transition function for a state and a symbol. More...
 
size_t delta (size_t q, std::string const &utf8Symbol) const
 Same as above for a UTF-8-encoded symbol. More...
 
size_t deltaHat (size_t q, std::u32string const &word) const
 Computes this DFA's transition function recursively for a string of symbols, starting in a specified state. More...
 
size_t deltaHat (size_t q, std::string const &utf8Word) const
 Same as above for a string of UTF-8-encoded symbols. More...
 
std::string const & getLabelOf (size_t q) const
 Puts a name to an index. More...
 
std::vector< char32_t > const & getAlphabet () const
 Fetches this DFA's set of processable symbols. More...
 
std::vector< std::string > const & getUtf8Alphabet () const
 Fetches this DFA's set of processable symbols as UTF-8-encoded strings. More...
 
size_t getNumberOfStates () const
 Counts this DFA's states. More...
 
bool isAccepting (size_t q) const
 Tests whether a state is an accept state within this DFA. More...
 

Detailed Description

Represents deterministic finite automata.

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

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

Definition at line 22 of file dfa.h.

Constructor & Destructor Documentation

◆ dfa() [1/4]

reg::dfa::dfa ( )

Constructs a DFA accepting the empty language ∅.

Definition at line 107 of file dfa.cpp.

107 : p(new pImpl) {}

◆ dfa() [2/4]

reg::dfa::dfa ( dfa d)

Copy-constructs a DFA by copying another one's private implementation object.

Definition at line 117 of file dfa.cpp.

117 : p(new pImpl(*(d.p))) {}

◆ dfa() [3/4]

reg::dfa::dfa ( dfa &&  d)

Move-constructs a DFA by stealing another one's private implementation object.

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

Definition at line 121 of file dfa.cpp.

121 : p(new pImpl) {p.swap(d.p);}

◆ dfa() [4/4]

reg::dfa::dfa ( dfa::builder b)

Constructs a DFA from a builder.

Definition at line 124 of file dfa.cpp.

124 : p(move(b.build().p)) {}

Member Function Documentation

◆ delta() [1/2]

size_t reg::dfa::delta ( size_t  q,
char32_t  symbol 
) const

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

Parameters
qthe state's index (< getNumberOfStates)
symbolthe symbol to process (∈ getAlphabet)
Returns
the reached state's index

Definition at line 223 of file dfa.cpp.

223  {
224  size_t i = distance(
225  p->alphabet.begin(),
226  find(p->alphabet.begin(), p->alphabet.end(), symbol)
227  );
228  if (i >= p->alphabet.size()) {
229  #ifdef __EXCEPTIONS
230  throw std::domain_error(
231  string(u8"δ is not defined for symbol ").append(1, symbol)
232  );
233  #else
234  return SIZE_MAX;
235  #endif
236  }
237  if (q >= p->labels.size()) {
238  #ifdef __EXCEPTIONS
239  throw std::out_of_range(
240  string("There is no state with index ").append(std::to_string(q))
241  );
242  #else
243  return SIZE_MAX-1;
244  #endif
245  }
246  return p->transitions[q][i];
247 }

◆ delta() [2/2]

size_t reg::dfa::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 250 of file dfa.cpp.

250  {
251  return delta(q, expression::converter->from_bytes(utf8Symbol)[0]);
252 }
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
size_t delta(size_t q, char32_t symbol) const
Computes this DFA&#39;s transition function for a state and a symbol.
Definition: dfa.cpp:223

◆ deltaHat() [1/2]

size_t reg::dfa::deltaHat ( size_t  q,
std::u32string const &  word 
) const

Computes this DFA'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
the reached state's index

Definition at line 261 of file dfa.cpp.

261  {
262  for (char32_t symbol : word) {
263  q = delta(q, symbol);
264  }
265  return q;
266 }
size_t delta(size_t q, char32_t symbol) const
Computes this DFA&#39;s transition function for a state and a symbol.
Definition: dfa.cpp:223

◆ deltaHat() [2/2]

size_t reg::dfa::deltaHat ( size_t  q,
std::string const &  utf8Word 
) const

Same as above for a string of UTF-8-encoded 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 269 of file dfa.cpp.

269  {
270  return deltaHat(q, expression::converter->from_bytes(utf8Word));
271 }
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
size_t deltaHat(size_t q, std::u32string const &word) const
Computes this DFA&#39;s transition function recursively for a string of symbols, starting in a specified ...
Definition: dfa.cpp:261

◆ getAlphabet()

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

Fetches this DFA's set of processable symbols.

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

Definition at line 287 of file dfa.cpp.

287  {
288  return p->alphabet;
289 }

◆ getLabelOf()

string const & reg::dfa::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 278 of file dfa.cpp.

278  {
279  return p->labels[q];
280 }

◆ getNumberOfStates()

size_t reg::dfa::getNumberOfStates ( ) const

Counts this DFA's states.

Returns
the number of states this DFA has

Definition at line 305 of file dfa.cpp.

305  {
306  return p->labels.size();
307 }

◆ getUtf8Alphabet()

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

Fetches this DFA'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 296 of file dfa.cpp.

296  {
297  return p->utf8Alphabet;
298 }

◆ isAccepting()

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

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

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

Definition at line 315 of file dfa.cpp.

315  {
316  return p->accepting[q];
317 }

◆ operator!=()

bool reg::dfa::operator!= ( const dfa d) const

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

Definition at line 214 of file dfa.cpp.

214 {return !operator==(d);}
bool operator==(const dfa &d) const
Tests whether this DFA accepts exactly the same language as another one.
Definition: dfa.cpp:152

◆ operator=() [1/2]

dfa & reg::dfa::operator= ( const dfa d)

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

Definition at line 127 of file dfa.cpp.

127  {
128  if (this != &d) {
129  p.reset(new pImpl(*(d.p)));
130  }
131  return *this;
132 }

◆ operator=() [2/2]

dfa & reg::dfa::operator= ( dfa &&  d)

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

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

Definition at line 136 of file dfa.cpp.

136  {
137  if (this != &d) {
138  p.reset(new pImpl);
139  p.swap(d.p);
140  }
141  return *this;
142 }

◆ operator==()

bool reg::dfa::operator== ( const dfa d) const

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

More specifically, checks whether this DFA's initial state is indistinguishable from the other one's.

Definition at line 152 of file dfa.cpp.

152  {
153  forward_list<char32_t> unitedAlphabet(p->alphabet.begin(), p->alphabet.end());
154  size_t unitedAlphabetSize(p->alphabet.size());
155  for (char32_t symbol : d.getAlphabet()) {
156  if (find(p->alphabet.begin(), p->alphabet.end(), symbol) == p->alphabet.end()) {
157  unitedAlphabet.push_front(symbol);
158  unitedAlphabetSize++;
159  }
160  }
161  vector<vector<size_t>> tTable(getNumberOfStates()+d.getNumberOfStates()+1, vector<size_t>(unitedAlphabetSize));
162  valarray<bool> accepting(false, getNumberOfStates()+d.getNumberOfStates()+1);
163  for (size_t q(0); q < getNumberOfStates(); q++) {
164  size_t s(0);
165  vector<size_t>& row = tTable[q];
166  for (char32_t symbol : unitedAlphabet) {
167  #ifdef __EXCEPTIONS
168  try {
169  row[s] = delta(q, symbol);
170  } catch (std::domain_error e) {
171  row[s] = getNumberOfStates()+d.getNumberOfStates();
172  }
173  #else
174  size_t r(delta(q, symbol));
175  if (r >= SIZE_MAX-1) {
176  row[s] = getNumberOfStates()+d.getNumberOfStates();
177  } else {
178  row[s] = r;
179  }
180  #endif
181  s++;
182  }
183  accepting[q] = isAccepting(q);
184  }
185  for (size_t q(0); q < d.getNumberOfStates(); q++) {
186  size_t s(0);
187  vector<size_t>& row = tTable[q+getNumberOfStates()];
188  for (char32_t symbol : unitedAlphabet) {
189  #ifdef __EXCEPTIONS
190  try {
191  row[s] = getNumberOfStates()+d.delta(q, symbol);
192  } catch (std::domain_error e) {
193  row[s] = getNumberOfStates()+d.getNumberOfStates();
194  }
195  #else
196  size_t r(delta(q, symbol));
197  if (r >= SIZE_MAX-1) {
198  row[s] = getNumberOfStates()+d.getNumberOfStates();
199  } else {
200  row[s] = r;
201  }
202  #endif
203  s++;
204  }
205  accepting[q+getNumberOfStates()] = d.isAccepting(q);
206  }
207  for (size_t s(0); s < unitedAlphabetSize; s++) {
208  tTable[getNumberOfStates()+d.getNumberOfStates()][s] = getNumberOfStates()+d.getNumberOfStates();
209  }
210  return pImpl::indistinguishableStates(tTable, accepting)[0][getNumberOfStates()];
211 }
bool isAccepting(size_t q) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:315
size_t delta(size_t q, char32_t symbol) const
Computes this DFA&#39;s transition function for a state and a symbol.
Definition: dfa.cpp:223
static vector< valarray< bool > > indistinguishableStates(vector< vector< size_t >> const &transitions, valarray< bool > const &accepting)
Builds the table of indistinguishable states w.r.t. a transition function.
Definition: dfa.cpp:64
size_t getNumberOfStates() const
Counts this DFA&#39;s states.
Definition: dfa.cpp:305

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