reglibcpp  1.0.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, unordered_map< string, unordered_map< char32_t, string >> &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...
 
unordered_map< string, unordered_map< char32_t, string > > transitions
 Transition function (state × symbol → state) of the prospective DFA. More...
 

Detailed Description

Private implementation details of DFA builders.

Definition at line 320 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,
unordered_map< string, unordered_map< char32_t, string >> &  transitions 
)
inline

Constructs private implementation object with provided members.

Definition at line 332 of file dfa.cpp.

337  : initial(move(initial)),
338  alphabet(move(alphabet)),
340  transitions(move(transitions)) {}
unordered_map< string, unordered_map< char32_t, string > > transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:324
unordered_set< char32_t > alphabet
Set of symbols processable by the prospective DFA.
Definition: dfa.cpp:322
string initial
Name of the prospective DFA&#39;s initial state.
Definition: dfa.cpp:321
unordered_set< string > acceptingStates
Set of names of the prospective DFA&#39;s accept states.
Definition: dfa.cpp:323

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

372  {
373  bool trashUsed(false);
374  string const& trashState = [&]{
375  for (auto const& entry : this->transitions) {
376  if (this->isTrashState(entry.first)) {
377  trashUsed = true;
378  return entry.first;
379  }
380  }
381  return this->generateNewState();
382  }();
383  for (auto& from : transitions) {
384  for (char32_t symbol : alphabet) {
385  if (from.second.find(symbol) == from.second.end()) {
386  from.second[symbol] = trashState;
387  trashUsed |= (from.first != trashState);
388  }
389  }
390  }
391  if (!trashUsed) {
392  transitions.erase(trashState);
393  }
394  }
unordered_map< string, unordered_map< char32_t, string > > transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:324
bool isTrashState(string const &q) const
Tests whether all of a state&#39;s outgoing transitions point to itself.
Definition: dfa.cpp:343
unordered_set< char32_t > alphabet
Set of symbols processable by the prospective DFA.
Definition: dfa.cpp:322
string const & generateNewState()
Generates a uniquely named new state and adds it to the set of states.
Definition: dfa.cpp:358

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

358  {
359  size_t q(0);
360  string newState;
361  while (transitions.find(newState = string("q").append(to_string(q))) != transitions.end()) {
362  q++;
363  }
364  if (transitions.empty()) {
365  initial = newState;
366  }
367  return transitions.insert(std::make_pair(newState, unordered_map<char32_t, string>())).first->first;
368  }
unordered_map< string, unordered_map< char32_t, string > > transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:324
string initial
Name of the prospective DFA&#39;s initial state.
Definition: dfa.cpp:321

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

343  {
344  if (acceptingStates.count(q) != 0) {
345  return false;
346  }
347  auto const& from = transitions.at(q);
348  for (char32_t symbol : alphabet) {
349  auto via = from.find(symbol);
350  if (via != from.end() && (*via).second != q) {
351  return false;
352  }
353  }
354  return true;
355  }
unordered_map< string, unordered_map< char32_t, string > > transitions
Transition function (state × symbol → state) of the prospective DFA.
Definition: dfa.cpp:324
unordered_set< char32_t > alphabet
Set of symbols processable by the prospective DFA.
Definition: dfa.cpp:322
unordered_set< string > acceptingStates
Set of names of the prospective DFA&#39;s accept states.
Definition: dfa.cpp:323

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

409  {
410  bool fullTrue = true;
411  for (bool b : a) {
412  fullTrue &= b;
413  }
414  return fullTrue;
415  }

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

◆ initial

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

Name of the prospective DFA's initial state.

Definition at line 321 of file dfa.cpp.

◆ transitions

unordered_map<string,unordered_map<char32_t,string> > 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 324 of file dfa.cpp.


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