reglibcpp  1.3.0
(Naïve) C++ implementation of models for regular languages
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
reg::dfa::builder::pImpl Struct Reference

Private implementation details of DFA builders. More...

Classes

struct  ValarrayBoolEqual
 Comparison object for valarray<bool>s, so they can be used as unordered_set keys. More...
 
struct  ValarrayBoolEqualTo
 Comparison object for valarray<bool>s against a specific instance. More...
 
struct  ValarrayBoolHasher
 Hasher object for valarray<bool>s, so they can be used as unordered_set keys. More...
 

Public Member Functions

 pImpl ()=default
 Constructs empty private implementation object. More...
 
 pImpl (string &initial, unordered_set< char32_t > &alphabet, unordered_set< string > &acceptingStates, Dtransitionmap &transitions)
 Constructs private implementation object with provided members. More...
 
bool isTrashState (string const &q) const
 Tests whether all of a state's outgoing transitions point to itself. More...
 
string const & generateNewState ()
 Generates a uniquely named new state and adds it to the set of states. More...
 
void complete ()
 Totalizes a partial transition function by pointing any undefined transitions towards a trash state. More...
 

Static Public Member Functions

static bool valarrayFullTrue (valarray< bool > const &a)
 Tests whether all of a valarray<bool>'s entries are true. More...
 

Public Attributes

string initial
 Name of the prospective DFA's initial state. More...
 
unordered_set< char32_t > alphabet
 Set of symbols processable by the prospective DFA. More...
 
unordered_set< string > acceptingStates
 Set of names of the prospective DFA's accept states. More...
 
Dtransitionmap transitions
 Transition function (state × symbol → state) of the prospective DFA. More...
 

Detailed Description

Private implementation details of DFA builders.

Definition at line 383 of file dfa.cpp.

Constructor & Destructor Documentation

◆ pImpl() [1/2]

reg::dfa::builder::pImpl::pImpl ( )
default

Constructs empty private implementation object.

◆ pImpl() [2/2]

reg::dfa::builder::pImpl::pImpl ( string &  initial,
unordered_set< char32_t > &  alphabet,
unordered_set< string > &  acceptingStates,
Dtransitionmap &  transitions 
)
inline

Constructs private implementation object with provided members.

Definition at line 395 of file dfa.cpp.

400  : initial(move(initial)),
401  alphabet(move(alphabet)),
403  transitions(move(transitions)) {}
Dtransitionmap transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:387
unordered_set< char32_t > alphabet
Set of symbols processable by the prospective DFA.
Definition: dfa.cpp:385
string initial
Name of the prospective DFA&#39;s initial state.
Definition: dfa.cpp:384
unordered_set< string > acceptingStates
Set of names of the prospective DFA&#39;s accept states.
Definition: dfa.cpp:386

Member Function Documentation

◆ complete()

void reg::dfa::builder::pImpl::complete ( )
inline

Totalizes a partial transition function by pointing any undefined transitions towards a trash state.

If there is no state satisfying trashiness, a new one will be generated.

Definition at line 435 of file dfa.cpp.

435  {
436  bool trashUsed(false);
437  string const& trashState = [&]{
438  for (auto const& entry : this->transitions) {
439  if (this->isTrashState(entry.first)) {
440  trashUsed = true;
441  return entry.first;
442  }
443  }
444  return this->generateNewState();
445  }();
446  for (auto& from : transitions) {
447  for (char32_t symbol : alphabet) {
448  if (from.second.find(symbol) == from.second.end()) {
449  from.second[symbol] = trashState;
450  trashUsed |= (from.first != trashState);
451  }
452  }
453  }
454  if (!trashUsed) {
455  transitions.erase(trashState);
456  }
457  }
Dtransitionmap transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:387
bool isTrashState(string const &q) const
Tests whether all of a state's outgoing transitions point to itself.
Definition: dfa.cpp:406
unordered_set< char32_t > alphabet
Set of symbols processable by the prospective DFA.
Definition: dfa.cpp:385
string const & generateNewState()
Generates a uniquely named new state and adds it to the set of states.
Definition: dfa.cpp:421

◆ generateNewState()

string const& reg::dfa::builder::pImpl::generateNewState ( )
inline

Generates a uniquely named new state and adds it to the set of states.

Definition at line 421 of file dfa.cpp.

421  {
422  size_t q(0);
423  string newState;
424  while (transitions.find(newState = string("q").append(std::to_string(q))) != transitions.end()) {
425  q++;
426  }
427  if (transitions.empty()) {
428  initial = newState;
429  }
430  return transitions.insert(std::make_pair(newState, unordered_map<char32_t, string>())).first->first;
431  }
Dtransitionmap transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:387
string initial
Name of the prospective DFA&#39;s initial state.
Definition: dfa.cpp:384

◆ isTrashState()

bool reg::dfa::builder::pImpl::isTrashState ( string const &  q) const
inline

Tests whether all of a state's outgoing transitions point to itself.

Definition at line 406 of file dfa.cpp.

406  {
407  if (acceptingStates.count(q) != 0) {
408  return false;
409  }
410  auto const& from = transitions.at(q);
411  for (char32_t symbol : alphabet) {
412  auto via = from.find(symbol);
413  if (via != from.end() && (*via).second != q) {
414  return false;
415  }
416  }
417  return true;
418  }
Dtransitionmap transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:387
unordered_set< char32_t > alphabet
Set of symbols processable by the prospective DFA.
Definition: dfa.cpp:385
unordered_set< string > acceptingStates
Set of names of the prospective DFA&#39;s accept states.
Definition: dfa.cpp:386

◆ valarrayFullTrue()

static bool reg::dfa::builder::pImpl::valarrayFullTrue ( valarray< bool > const &  a)
inlinestatic

Tests whether all of a valarray<bool>'s entries are true.

Definition at line 472 of file dfa.cpp.

472  {
473  bool fullTrue = true;
474  for (bool b : a) {
475  fullTrue &= b;
476  }
477  return fullTrue;
478  }

Member Data Documentation

◆ acceptingStates

unordered_set<string> reg::dfa::builder::pImpl::acceptingStates

Set of names of the prospective DFA's accept states.

Definition at line 386 of file dfa.cpp.

◆ alphabet

unordered_set<char32_t> reg::dfa::builder::pImpl::alphabet

Set of symbols processable by the prospective DFA.

Definition at line 385 of file dfa.cpp.

◆ initial

string reg::dfa::builder::pImpl::initial

Name of the prospective DFA's initial state.

Definition at line 384 of file dfa.cpp.

◆ transitions

Dtransitionmap reg::dfa::builder::pImpl::transitions

Transition function (state × symbol → state) of the prospective DFA.

Also encodes the set of states in its set of keys.

Definition at line 387 of file dfa.cpp.


The documentation for this struct was generated from the following file: