Last active
August 19, 2019 19:58
-
-
Save morgangallant/a3198d1eebf15ebfbbdf868a44e5d207 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iterator> | |
#include <sstream> | |
// Brute force naive solution. | |
std::optional<std::function<void()>> brute_force( | |
const std::string &msg) const { | |
// If this node is a leaf node, return the action. | |
if (action_ != std::nullopt) return action_; | |
// Tokenize the input string. | |
std::istringstream iss(msg); | |
std::vector<std::string> tokens{std::istream_iterator<std::string>{iss}, | |
std::istream_iterator<std::string>{}}; | |
// Begin the recursive brute force search. | |
for (const auto node : nodes_) { | |
for (const std::string token : tokens) { | |
for (const std::string target : node->triggers_) { | |
if (token.compare(target) == 0) { | |
auto res = node->brute_force(msg); | |
if (res != std::nullopt) { | |
return res; | |
} | |
} | |
} | |
} | |
} | |
// If we don't find a match, return an empty optional. | |
return std::nullopt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment