- Checkpointing work on expanding commands.

This commit is contained in:
Ian Roddis
2021-08-20 10:39:39 -03:00
parent 9f90f54b67
commit 1668117566
3 changed files with 62 additions and 15 deletions

View File

@@ -14,6 +14,8 @@
#include "DAG.hpp"
namespace daggy {
std::string globalSub(std::string string, const std::string &pattern, const std::string &replacement);
std::vector<Command> expandCommands(const std::vector<std::string> &command, const ParameterValues &parameters);
DAG buildDAGFromTasks(const std::vector<Task> &tasks);

View File

@@ -3,28 +3,47 @@
#include <daggy/Utilities.hpp>
namespace daggy {
std::string globalSub(std::string string, const std::string &pattern, const std::string &replacement) {
size_t pos = string.find(pattern);
while (pos != std::string::npos) {
string.replace(pos, pattern.size(), replacement);
pos = string.find(pattern);
}
return string;
}
std::vector<std::vector<std::string>>
expandCommands(const std::vector<std::string> &command, const ParameterValues &parameters) {
std::vector<std::vector<std::string>> commands{{}};
for (const auto &part : command) {
// this isn't an interpolated value
if (parameters.find(part) == parameters.end()) {
for (auto &cmd : commands) cmd.push_back(part);
continue;
}
auto &inVal = parameters.at(part);
if (std::holds_alternative<std::string>(inVal)) {
for (auto &cmd : commands) cmd.push_back(std::get<std::string>(inVal));
continue;
std::vector<std::string> expandedPart;
for (const auto &[param, paramValue] : parameters) {
auto pos = part.find(param);
if (pos == std::string::npos) continue;
std::vector<std::string> newExpandedPart;
if (std::holds_alternative<std::string>(paramValue)) {
for (auto &cmd : expandedPart) {
newExpandedPart.push_back(globalSub(cmd, param, std::get<std::string>(paramValue)));
}
} else {
for (const auto &val : std::get<std::vector<std::string>>(paramValue)) {
for (auto cmd : expandedPart) {
newExpandedPart.push_back(globalSub(cmd, param, val));
}
}
}
expandedPart.swap(newExpandedPart);
}
// Ends up being expensive, as it's a cartesian product
std::vector<std::vector<std::string>> newCommands;
for (const auto &val : std::get<std::vector<std::string>>(inVal)) {
for (const auto &newPart : expandedPart) {
for (auto cmd : commands) {
cmd.push_back(val);
newCommands.push_back(cmd);
cmd.push_back(newPart);
newCommands.emplace_back(cmd);
}
}
commands.swap(newCommands);
@@ -136,4 +155,4 @@ namespace daggy {
os << std::put_time(std::localtime(&t_c), "%Y-%m-%d %H:%M:%S %Z");
return os;
}
}
}