1

I am trying to implement a simple deterministic finite automaton. I have an automaton with the std::vector<transitions*>. Now whenever an input is received, it should be determined which transition to execute. This is what I want to achieve with the following function!

Now this is what the function needs to do: search in a vector for the unique transition, for which both cursor->input = inputValue and cursor->from = inputFrom is true, then return cursor (This is done by the emit transitionToExecute(cursor), don't worry about that).

This is what I've got so far.

void automaton::getTransition(int inputValue, state inputFrom){
    iterA cursor = find(transitions.begin(), transitions.end(), inputValue);
    if(cursor!=transitions.end()){
        if(cursor->from.getId() == inputFrom.getId()){
            emit transitionToExecute(cursor);
            return;
        }
    }
}

EDIT: I've tried the answers given to the linked question, but I don't really understand how they work. Does anyone care to explain? Here's what I tried. I don't understand how this technique works, so I'd appreciated a detailed answer.

struct isCorrectTransition : std::unary_function<transition*, bool> {

    // The first variable that must be matched.
   int inputValue;

    // I want to use this second variable, too! How to do that?
    state inputFrom;

    // How does the following line work? What does it do? 
    // How to add in state inputFrom?
    isCorrectTransition(int inputValue):inputValue(inputValue){ }
    bool operator()(transition* const& inputTransition) const {
        return ((inputTransition->getInput() == inputValue) && (inputTransition->getFrom().getId() == inputFrom.getId()));
    }
};

void automaton::getTransition(int inputValue, state inputFrom){
    searchInput = inputValue;
    searchState = inputFrom;
    iterA cursor = find_if(transitions.begin(), transitions.end(), isCorrectTransition(inputValue));
    if(cursor!=transitions.end()){
        emit transitionToExecute(*cursor);
        return;
    }
}
Alex
  • 751
  • 1
  • 6
  • 34

0 Answers0