Skip to content

Instantly share code, notes, and snippets.

@morgangallant
Last active August 19, 2019 19:58
Show Gist options
  • Save morgangallant/a3198d1eebf15ebfbbdf868a44e5d207 to your computer and use it in GitHub Desktop.
Save morgangallant/a3198d1eebf15ebfbbdf868a44e5d207 to your computer and use it in GitHub Desktop.
#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