reglibcpp  1.6.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...
 
 dfa (nfa const &n)
 Constructs a DFA from an NFA via powerset construction. 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 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 27 of file dfa.h.

Constructor & Destructor Documentation

◆ dfa() [1/5]

reg::dfa::dfa ( )

Constructs a DFA accepting the empty language ∅.

Definition at line 126 of file dfa.cpp.

126 : p(new pImpl) {}

◆ dfa() [2/5]

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

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

Definition at line 136 of file dfa.cpp.

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

◆ dfa() [3/5]

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

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

◆ dfa() [4/5]

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

Constructs a DFA from an NFA via powerset construction.

Definition at line 143 of file dfa.cpp.

143 : dfa(builder(n).build()) {}
dfa()
Constructs a DFA accepting the empty language ∅.
Definition: dfa.cpp:126

◆ dfa() [5/5]

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

Constructs a DFA from a builder.

Definition at line 146 of file dfa.cpp.

146 : dfa(b.build()) {}
dfa()
Constructs a DFA accepting the empty language ∅.
Definition: dfa.cpp:126

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

485  {
486  return dfa::builder(d).complement();
487 }

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

227  {
228  if (si >= p->alphabet.size()) {
229  throw std::domain_error("There is no symbol with index " + to_string(si));
230  }
231  if (qi >= p->labels.size()) {
232  throw std::out_of_range("There is no state with index " + to_string(qi));
233  }
234  return p->transitions[qi][si];
235 }
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 245 of file dfa.cpp.

245  {
246  try {
247  return delta(qi, index_of(p->alphabet, symbol));
248  } catch (std::domain_error e) {
249  throw std::domain_error(u8"δ is not defined for symbol " + converter.to_bytes(symbol));
250  }
251 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
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:227
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: dfa.h:120

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

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

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

266  {
267  try {
268  return getStates()[delta(index_of(getStates(), q), symbol)];
269  } catch (std::out_of_range e) {
270  throw std::out_of_range("There is no state named " + q);
271  }
272 }
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:227
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: dfa.h:120
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:343

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

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

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

287  {
288  for (char32_t symbol : word) {
289  qi = delta(qi, symbol);
290  }
291  return qi;
292 }
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:227

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

295  {
296  return deltaHat(q, converter.from_bytes(utf8Word));
297 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
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:287

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

307  {
308  return p->labels[deltaHat(index_of(getStates(), q), word)];
309 }
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: dfa.h:120
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:287
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:343

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

312  {
313  return deltaHat(q, converter.from_bytes(utf8Word));
314 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
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:287

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

351  {
352  return p->alphabet;
353 }

◆ getInitialState()

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

Names this DFA's initial state.

Returns
the initial state's name

Definition at line 335 of file dfa.cpp.

335  {
336  return getStates()[0];
337 }
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:343

◆ getLabelOf()

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

Definition at line 327 of file dfa.cpp.

327  {
328  return getStates().at(q);
329 }
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:343

◆ getNumberOfStates()

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

Definition at line 364 of file dfa.cpp.

364  {
365  return getStates().size();
366 }
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:343

◆ getNumberOfSymbols()

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

Definition at line 369 of file dfa.cpp.

369  {
370  return getAlphabet().size();
371 }
std::vector< char32_t > const & getAlphabet() const
Fetches this DFA's set of processable symbols.
Definition: dfa.cpp:351

◆ getShortestUtf8Word()

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

Definition at line 322 of file dfa.cpp.

322  {
323  return findShortestUtf8Word(*this);
324 }
friend std::string findShortestUtf8Word(dfa const &d)
Same as above for a UTF-8-encoded word.
Definition: dfa.cpp:433

◆ getShortestWord()

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

Definition at line 317 of file dfa.cpp.

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

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

343  {
344  return p->labels;
345 }

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

359  {
360  return p->utf8Alphabet;
361 }

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

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

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

379  {
380  if (qi >= p->labels.size()) {
381  throw std::out_of_range("There is no state with index " + to_string(qi));
382  }
383  return p->accepting[qi];
384 }
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 392 of file dfa.cpp.

392  {
393  try {
394  return isAccepting(index_of(getStates(), q));
395  } catch (std::out_of_range e) {
396  throw std::out_of_range("There is no state named " + q);
397  }
398 }
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:379
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: dfa.h:120
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:343

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

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

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

149  {
150  if (this != &d) {
151  p.reset(new pImpl(*(d.p)));
152  }
153  return *this;
154 }

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

158  {
159  if (this != &d) {
160  p.reset(new pImpl);
161  p.swap(d.p);
162  }
163  return *this;
164 }

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

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

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

469  {
470  builder b1(d1);
471  for (auto symbol : d2.getAlphabet()) {
472  b1.addSymbol(symbol);
473  }
474  return b1.complement().unite(d2).complement();
475 }

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

445  {
446  return builder(d1).unite(d2);
447 }

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

433  {
434  return converter.to_bytes(findShortestWord(d));
435 }
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: dfa.cpp:1060
friend std::u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:406

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

406  {
407  auto const& p = d.p;
408  if (p->accepting[0]) {
409  return U"";
410  }
411  unordered_map<size_t, u32string> shortestWords(p->labels.size());
412  size_t oldSize = 0;
413  shortestWords.emplace(0, U"");
414  while (shortestWords.size() > oldSize) {
415  oldSize = shortestWords.size();
416  for (auto const& stateWord : shortestWords) {
417  for (auto symbol : p->alphabet) {
418  size_t reached = d.delta(stateWord.first, symbol);
419  u32string shWord = stateWord.second + symbol;
420  if (p->accepting[reached]) {
421  return shWord;
422  }
423  if (shortestWords.find(reached) == shortestWords.end()) {
424  shortestWords.emplace(reached, shWord);
425  }
426  }
427  }
428  }
429  throw std::logic_error("This DFA doesn't accept any words!");
430 }

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