diff --git a/example/path.cpp b/example/path.cpp index 7192404..56f586f 100644 --- a/example/path.cpp +++ b/example/path.cpp @@ -7,14 +7,12 @@ int main() { auto cube = tc::group::B(3); auto vars = cube.solve(); - auto words = vars.path.walk( - "", - {"a", "b", "c"}, - [](auto a, auto b) { return a + b; } - ); + std::string start; + std::vector names = {"a", "b", "c"}; + auto words = vars.path.walk(start, names, std::plus<>()); - for (const auto &word : words) { - std::cout << word << std::endl; + for (const auto &word: words) { + std::cout << (word.empty() ? "-" : word) << std::endl; } return 0; diff --git a/include/tc/core/path.hpp b/include/tc/core/path.hpp index 5063855..2e8b9a1 100644 --- a/include/tc/core/path.hpp +++ b/include/tc/core/path.hpp @@ -28,61 +28,27 @@ struct Path { return path.size(); } - template - void walk( - C &res, - T start, - std::vector gens, - std::function op - ) const { - size_t s = size(); - res.reserve(s); - res.push_back(start); - - for (int i = 1; i < s; ++i) { - auto &action = path[i]; - auto &from = res.get(action.from_idx); - auto &val = gens[action.gen]; - res.push_back(op(from, val)); - } - } - - template - [[nodiscard]] std::vector walk( - T start, - std::vector gens, - std::function op - ) const { + template + std::vector walk(const T &start, const F &op) { std::vector res; - res.reserve(size()); + res.reserve(path.size()); res.push_back(start); - for (int i = 1; i < size(); ++i) { + for (size_t i = 1; i < path.size(); ++i) { auto &action = path[i]; auto &from = res[action.from_idx]; - auto &val = gens[action.gen]; - res.push_back(op(from, val)); + auto &gen = action.gen; + + res.push_back(op(from, gen)); } return res; } - template - [[nodiscard]] std::vector walk( - T start, - std::function op - ) const { - std::vector res; - res.reserve(size()); - res.push_back(start); - - for (int i = 1; i < size(); ++i) { - auto &action = path[i]; - auto &from = res[action.from_idx]; - auto &val = action.gen; - res[i] = op(from, val); - } - - return res; + template + std::vector walk(const T &start, const E &gens, const F &op) { + return walk(start, [&](const T &from, const int gen) { + return op(from, gens[gen]); + }); } };