reglibcpp  1.7.0
(Naïve) C++ implementation of models for regular languages
Classes | Public Member Functions | Static Public Member Functions | Friends | 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 const &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...
 
dfaoperator= (dfa const &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...
 
 operator nfa const & () const
 Returns an NFA accepting the same language as this DFA. More...
 
bool operator== (dfa const &d) const
 Tests whether this DFA accepts exactly the same language as another one. More...
 
bool operator!= (dfa const &d) const
 Tests whether this DFA doesn't accept the same language as another one. More...
 
bool operator== (nfa const &d) const
 Tests whether this DFA accepts exactly the same language as another object. More...
 
bool operator!= (nfa const &d) const
 Tests whether this DFA doesn't accept the same language as another object. More...
 
size_t delta (size_t qi, size_t si) const
 Computes this DFA's transition function for a state index and a symbol index. More...
 
size_t delta (size_t qi, char32_t symbol) const
 Computes this DFA's transition function for a state index and a symbol. More...
 
size_t delta (size_t qi, std::string const &utf8Symbol) const
 Same as above for a UTF-8-encoded symbol. More...
 
std::string const & delta (std::string const &q, char32_t symbol) const
 Computes this DFA's transition function for a state label and a symbol. More...
 
std::string const & delta (std::string const &q, std::string const &utf8Symbol) const
 Same as above for a UTF-8-encoded symbol. More...
 
size_t deltaHat (size_t qi, std::u32string const &word) const
 Computes this DFA's transition function recursively for a string of symbols, starting in a state specified by its index. More...
 
size_t deltaHat (size_t qi, std::string const &utf8Word) const
 Same as above for a string of UTF-8-encoded symbols. More...
 
std::string const & deltaHat (std::string const &q, std::u32string const &word) const
 Computes this DFA's transition function recursively for a string of symbols, starting in a state specified by its name. More...
 
std::string const & deltaHat (std::string const &q, std::string const &utf8Word) const
 Same as above for a string of UTF-8-encoded symbols. More...
 
std::u32string getShortestWord () const
 
std::string getShortestUtf8Word () const
 
std::string const & getLabelOf (size_t qi) const
 
std::string const & getInitialState () const
 Names this DFA's initial state. More...
 
std::vector< std::string > const & getStates () const
 Fetches this DFA's set of states. 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
 
size_t getNumberOfSymbols () const
 
bool isAccepting (size_t qi) const
 Tests whether a state is an accept state within this DFA. More...
 
bool isAccepting (std::string const &q) const
 Tests whether a state is an accept state within this DFA. More...
 

Static Public Member Functions

static dfa::builder unite (dfa const &d1, dfa const &d2)
 Creates a builder for a DFA accepting the union of the languages of two DFAs. More...
 
static dfa::builder intersect (dfa const &d1, dfa const &d2)
 Creates a builder for a DFA accepting the intersection of the languages of two DFAs. More...
 
static dfa::builder subtract (dfa const &d1, dfa const &d2)
 Creates a builder for a DFA accepting the set difference of the languages of two DFAs. More...
 
static dfa::builder complement (dfa const &d)
 Creates a builder for a DFA accepting the complement of the language of a DFA. More...
 

Friends

std::u32string findShortestWord (dfa const &d)
 Searches the shortest UTF-32-encoded word accepted by a given DFA. More...
 
std::string findShortestUtf8Word (dfa const &d)
 Same as above for a UTF-8-encoded word. 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 21 of file dfa.h.

Constructor & Destructor Documentation

◆ dfa() [1/3]

reg::dfa::dfa ( )

Constructs a DFA accepting the empty language ∅.

Definition at line 127 of file dfa.cpp.

127 : p(new pImpl) {}

◆ dfa() [2/3]

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

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

Definition at line 137 of file dfa.cpp.

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

◆ dfa() [3/3]

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 141 of file dfa.cpp.

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

Member Function Documentation

◆ complement()

dfa::builder reg::dfa::complement ( dfa const &  d)
static

Creates a builder for a DFA accepting the complement of the language of a DFA.

The input DFAs' state names will be retained in the created DFA.

Parameters
dthe DFA
Returns
builder fo a DFA accepting all words not accepted by the input DFA (provided they can be built from symbols of that DFA's alphabet)

Definition at line 497 of file dfa.cpp.

497  {
498  return dfa::builder(d).complement();
499 }

◆ delta() [1/5]

size_t reg::dfa::delta ( size_t  qi,
size_t  si 
) const

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

Parameters
qithe state's index (< getNumberOfStates)
siindex of the symbol to process (< getNumberOfSymbols)
Returns
the reached state's index
Exceptions
std::out_of_rangeif qigetNumberOfStates
std::domain_errorif sigetAlphabet

Definition at line 239 of file dfa.cpp.

239  {
240  if (si >= p->alphabet.size()) {
241  throw std::domain_error("There is no symbol with index " + to_string(si));
242  }
243  if (qi >= p->labels.size()) {
244  throw std::out_of_range("There is no state with index " + to_string(qi));
245  }
246  return p->transitions[qi][si];
247 }
T to_string(T... args)

◆ delta() [2/5]

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

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

Parameters
qithe state's index (< getNumberOfStates)
symbolthe symbol to process (∈ getAlphabet)
Returns
the reached state's index
Exceptions
std::out_of_rangeif qigetNumberOfStates
std::domain_errorif symbolgetAlphabet

Definition at line 257 of file dfa.cpp.

257  {
258  try {
259  return delta(qi, index_of(p->alphabet, symbol));
260  } catch (std::domain_error e) {
261  throw std::domain_error(u8"δ is not defined for symbol " + converter.to_bytes(symbol));
262  }
263 }
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 delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:239
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

◆ delta() [3/5]

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

266  {
267  return delta(qi, converter.from_bytes(utf8Symbol)[0]);
268 }
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 delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:239

◆ delta() [4/5]

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

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

Parameters
qthe state's label (∈ getStates)
symbolthe symbol to process (∈ getAlphabet)
Returns
the reached state's label
Exceptions
std::out_of_rangeif qgetStates
std::domain_errorif symbolgetAlphabet

Definition at line 278 of file dfa.cpp.

278  {
279  try {
280  return getStates()[delta(index_of(getStates(), q), symbol)];
281  } catch (std::out_of_range e) {
282  throw std::out_of_range("There is no state named " + q);
283  }
284 }
size_t delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:239
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::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:355

◆ delta() [5/5]

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

287  {
288  return delta(q, converter.from_bytes(utf8Symbol)[0]);
289 }
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 delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:239

◆ deltaHat() [1/4]

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

Computes this DFA'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
the reached state's index
Exceptions
std::out_of_rangeif qigetNumberOfStates
std::domain_errorif one of the word symbols ∉ getAlphabet

Definition at line 299 of file dfa.cpp.

299  {
300  for (char32_t symbol : word) {
301  qi = delta(qi, symbol);
302  }
303  return qi;
304 }
size_t delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:239

◆ deltaHat() [2/4]

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 307 of file dfa.cpp.

307  {
308  return deltaHat(q, converter.from_bytes(utf8Word));
309 }
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 deltaHat(size_t qi, std::u32string const &word) const
Computes this DFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: dfa.cpp:299

◆ deltaHat() [3/4]

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

Computes this DFA'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
the reached state's index
Exceptions
std::out_of_rangeif qgetStates
std::domain_errorif one of the word symbols ∉ getAlphabet

Definition at line 319 of file dfa.cpp.

319  {
320  return p->labels[deltaHat(index_of(getStates(), q), word)];
321 }
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
size_t deltaHat(size_t qi, std::u32string const &word) const
Computes this DFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: dfa.cpp:299
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:355

◆ deltaHat() [4/4]

string const & reg::dfa::deltaHat ( std::string const &  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 324 of file dfa.cpp.

324  {
325  return deltaHat(q, converter.from_bytes(utf8Word));
326 }
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 deltaHat(size_t qi, std::u32string const &word) const
Computes this DFA's transition function recursively for a string of symbols, starting in a state spec...
Definition: dfa.cpp:299

◆ 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 363 of file dfa.cpp.

363  {
364  return p->alphabet;
365 }

◆ getInitialState()

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

Names this DFA's initial state.

Returns
the initial state's name

Definition at line 347 of file dfa.cpp.

347  {
348  return getStates()[0];
349 }
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:355

◆ getLabelOf()

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

Definition at line 339 of file dfa.cpp.

339  {
340  return getStates().at(q);
341 }
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:355

◆ getNumberOfStates()

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

Definition at line 376 of file dfa.cpp.

376  {
377  return getStates().size();
378 }
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:355

◆ getNumberOfSymbols()

size_t reg::dfa::getNumberOfSymbols ( ) const
Deprecated:
Use getAlphabet and std::vector::size instead.

Definition at line 381 of file dfa.cpp.

381  {
382  return getAlphabet().size();
383 }
std::vector< char32_t > const & getAlphabet() const
Fetches this DFA's set of processable symbols.
Definition: dfa.cpp:363

◆ getShortestUtf8Word()

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

Definition at line 334 of file dfa.cpp.

334  {
335  return findShortestUtf8Word(*this);
336 }
friend std::string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:445

◆ getShortestWord()

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

Definition at line 329 of file dfa.cpp.

329  {
330  return findShortestWord(*this);
331 }
friend std::u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:418

◆ getStates()

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

Fetches this DFA's set of states.

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

Definition at line 355 of file dfa.cpp.

355  {
356  return p->labels;
357 }

◆ 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 371 of file dfa.cpp.

371  {
372  return p->utf8Alphabet;
373 }

◆ intersect()

dfa::builder reg::dfa::intersect ( dfa const &  d1,
dfa const &  d2 
)
static

Creates a builder for a DFA accepting the intersection of the languages of two DFAs.

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

Parameters
d1the first DFA
d2the other DFA
Returns
builder for a DFA accepting all the words accepted by both of the input DFAs

Definition at line 469 of file dfa.cpp.

469  {
470  return builder(d1).intersect(d2);
471 }

◆ isAccepting() [1/2]

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

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

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 391 of file dfa.cpp.

391  {
392  if (qi >= p->labels.size()) {
393  throw std::out_of_range("There is no state with index " + to_string(qi));
394  }
395  return p->accepting[qi];
396 }
T to_string(T... args)

◆ isAccepting() [2/2]

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

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

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 404 of file dfa.cpp.

404  {
405  try {
406  return isAccepting(index_of(getStates(), q));
407  } catch (std::out_of_range e) {
408  throw std::out_of_range("There is no state named " + q);
409  }
410 }
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:391
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::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:355

◆ operator nfa const &()

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

Returns an NFA accepting the same language as this DFA.

◆ operator!=() [1/2]

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

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

Definition at line 221 of file dfa.cpp.

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

◆ operator!=() [2/2]

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

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

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 229 of file dfa.cpp.

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

◆ operator=() [1/2]

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

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

Definition at line 144 of file dfa.cpp.

144  {
145  if (this != &d) {
146  p.reset(new pImpl(*(d.p)));
147  }
148  return *this;
149 }

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

153  {
154  if (this != &d) {
155  p.reset(new pImpl);
156  p.swap(d.p);
157  }
158  return *this;
159 }

◆ operator==() [1/2]

bool reg::dfa::operator== ( dfa const &  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 177 of file dfa.cpp.

177  {
178  forward_list<char32_t> unitedAlphabet(p->alphabet.begin(), p->alphabet.end());
179  size_t unitedAlphabetSize(p->alphabet.size());
180  for (char32_t symbol : d.getAlphabet()) {
181  if (find(p->alphabet.begin(), p->alphabet.end(), symbol) == p->alphabet.end()) {
182  unitedAlphabet.push_front(symbol);
183  unitedAlphabetSize++;
184  }
185  }
186  vector<vector<size_t>> tTable(getNumberOfStates()+d.getNumberOfStates()+1, vector<size_t>(unitedAlphabetSize));
187  valarray<bool> accepting(false, getNumberOfStates()+d.getNumberOfStates()+1);
188  for (size_t q(0); q < getNumberOfStates(); q++) {
189  size_t s(0);
190  vector<size_t>& row = tTable[q];
191  for (char32_t symbol : unitedAlphabet) {
192  try {
193  row[s] = delta(q, symbol);
194  } catch (std::domain_error e) {
195  row[s] = getNumberOfStates()+d.getNumberOfStates();
196  }
197  s++;
198  }
199  accepting[q] = isAccepting(q);
200  }
201  for (size_t q(0); q < d.getNumberOfStates(); q++) {
202  size_t s(0);
203  vector<size_t>& row = tTable[q+getNumberOfStates()];
204  for (char32_t symbol : unitedAlphabet) {
205  try {
206  row[s] = getNumberOfStates()+d.delta(q, symbol);
207  } catch (std::domain_error e) {
208  row[s] = getNumberOfStates()+d.getNumberOfStates();
209  }
210  s++;
211  }
212  accepting[q+getNumberOfStates()] = d.isAccepting(q);
213  }
214  for (size_t s(0); s < unitedAlphabetSize; s++) {
215  tTable[getNumberOfStates()+d.getNumberOfStates()][s] = getNumberOfStates()+d.getNumberOfStates();
216  }
217  return pImpl::indistinguishableStates(tTable, accepting)[0][getNumberOfStates()];
218 }
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:391
size_t delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:239
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:84
size_t getNumberOfStates() const
Definition: dfa.cpp:376
T find(T... args)

◆ operator==() [2/2]

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

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

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

224  {
225  return operator==(static_cast<dfa const&>(n));
226 }
bool operator==(dfa const &d) const
Tests whether this DFA accepts exactly the same language as another one.
Definition: dfa.cpp:177

◆ subtract()

dfa::builder reg::dfa::subtract ( dfa const &  d1,
dfa const &  d2 
)
static

Creates a builder for a DFA accepting the set difference of the languages of two DFAs.

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

Parameters
d1the first DFA
d2the other DFA
Returns
builder for a DFA accepting all the words accepted by the first but not the other input DFA

Definition at line 481 of file dfa.cpp.

481  {
482  builder b1(d1);
483  for (auto symbol : d2.getAlphabet()) {
484  b1.addSymbol(symbol);
485  }
486  return b1.complement().unite(d2).complement();
487 }

◆ unite()

dfa::builder reg::dfa::unite ( dfa const &  d1,
dfa const &  d2 
)
static

Creates a builder for a DFA accepting the union of the languages of two DFAs.

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

Parameters
d1the first DFA
d2the other DFA
Returns
builder for a DFA accepting all the words accepted by any of the input DFAs

Definition at line 457 of file dfa.cpp.

457  {
458  return builder(d1).unite(d2);
459 }

Friends And Related Function Documentation

◆ findShortestUtf8Word

std::string findShortestUtf8Word ( dfa const &  d)
friend

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

Definition at line 445 of file dfa.cpp.

445  {
446  return converter.to_bytes(findShortestWord(d));
447 }
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(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:418

◆ findShortestWord

std::u32string findShortestWord ( dfa const &  d)
friend

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

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

Definition at line 418 of file dfa.cpp.

418  {
419  auto const& p = d.p;
420  if (p->accepting[0]) {
421  return U"";
422  }
423  unordered_map<size_t, u32string> shortestWords(p->labels.size());
424  size_t oldSize = 0;
425  shortestWords.emplace(0, U"");
426  while (shortestWords.size() > oldSize) {
427  oldSize = shortestWords.size();
428  for (auto const& stateWord : shortestWords) {
429  for (auto symbol : p->alphabet) {
430  size_t reached = d.delta(stateWord.first, symbol);
431  u32string shWord = stateWord.second + symbol;
432  if (p->accepting[reached]) {
433  return shWord;
434  }
435  if (shortestWords.find(reached) == shortestWords.end()) {
436  shortestWords.emplace(reached, shWord);
437  }
438  }
439  }
440  }
441  throw std::logic_error("This DFA doesn't accept any words!");
442 }

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