reglibcpp  1.6.0
(Naïve) C++ implementation of models for regular languages
Classes | Typedefs | Functions | Variables
reg Namespace Reference

Where this library lives. More...

Classes

class  dfa
 Represents deterministic finite automata. More...
 
class  expression
 Represents formal regular expressions. More...
 
class  gnfa
 Represents generalized nondeterministic finite automata. More...
 
class  nfa
 Represents nondeterministic finite automata with ε-moves. More...
 

Typedefs

using Dtransitionmap = unordered_map< string, unordered_map< char32_t, string > >
 Shorthand for the map from state name and transition symbol to target state. More...
 
using Ntransitionmap = unordered_map< string, unordered_map< char32_t, unordered_set< string > >>
 Shorthand for the map from state name and transition symbol to set of target states. More...
 

Functions

u32string findShortestWord (dfa const &d)
 Searches the shortest UTF-32-encoded word accepted by a given DFA. More...
 
string findShortestUtf8Word (dfa const &d)
 Same as above for a UTF-8-encoded word. More...
 
template<class C , class T >
size_t index_of (C const &container, T const &element)
 Basically Java's List interface's indexOf, but as a non-member function and returning the container's size upon failure. More...
 
u32string findShortestWord (nfa const &n)
 Searches the shortest UTF-32-encoded word accepted by a given NFA. More...
 
string findShortestUtf8Word (nfa const &n)
 Same as above for a UTF-8-encoded word. More...
 

Variables

std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
 Converts between UTF-8-encoded and UTF-32-encoded strings. More...
 

Detailed Description

Where this library lives.

Typedef Documentation

◆ Dtransitionmap

Shorthand for the map from state name and transition symbol to target state.

Definition at line 490 of file dfa.cpp.

◆ Ntransitionmap

Shorthand for the map from state name and transition symbol to set of target states.

Definition at line 655 of file nfa.cpp.

Function Documentation

◆ findShortestUtf8Word() [1/2]

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

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
u32string findShortestWord(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:406

◆ findShortestUtf8Word() [2/2]

std::string reg::findShortestUtf8Word ( nfa const &  n)

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

Definition at line 564 of file nfa.cpp.

564  {
565  return converter.to_bytes(findShortestWord(n));
566 }
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
u32string findShortestWord(nfa const &n)
Searches the shortest UTF-32-encoded word accepted by a given NFA.
Definition: nfa.cpp:535

◆ findShortestWord() [1/2]

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

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 }

◆ findShortestWord() [2/2]

std::u32string reg::findShortestWord ( nfa const &  n)

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

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

◆ index_of()

template<class C , class T >
size_t reg::index_of ( C const &  container,
T const &  element 
)

Basically Java's List interface's indexOf, but as a non-member function and returning the container's size upon failure.

Parameters
containerthe container to search through
elementthe element to find the index of
Returns
the first i with container.begin()[i]==element or container.size() if none is found

Definition at line 120 of file dfa.h.

120  {
121  static_assert(std::is_same<typename C::value_type,T>::value, "C must be a container with T as value_type.");
122  return static_cast<size_t>(std::distance(container.begin(), std::find(container.begin(), container.end(), element)));
123 }

Variable Documentation

◆ converter

std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > reg::converter

Converts between UTF-8-encoded and UTF-32-encoded strings.

Definition at line 1060 of file dfa.cpp.