From 0502cb0a7e707148886a7b4d21dbe557c508f9ce Mon Sep 17 00:00:00 2001 From: David Allemang Date: Thu, 24 Feb 2022 14:58:45 -0500 Subject: [PATCH] WIP: Copy combinations logic from demo --- CMakeLists.txt | 5 ++++- include/combo.hpp | 30 ++++++++++++++++++++++++++++++ src/combotest.cpp | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 include/combo.hpp create mode 100644 src/combotest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e6b2952..115aaba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,4 +35,7 @@ add_executable(serial src/serialtest.cpp) target_link_libraries(serial eigen nlohmann_json) add_executable(tctest src/tctest.cpp) -target_link_libraries(tctest PUBLIC eigen tc) +target_link_libraries(tctest eigen tc) + +add_executable(combotest src/combotest.cpp) +target_link_libraries(combotest eigen tc) diff --git a/include/combo.hpp b/include/combo.hpp new file mode 100644 index 0000000..3a5331f --- /dev/null +++ b/include/combo.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +template +V select(const V &data, const M &mask, size_t count) { + V result; + result.reserve(count); + + for (int i = 0; i < mask.size(); ++i) { + if (mask[i]) result.push_back(data[i]); + } + + return result; +} + +template +std::vector combinations(const V &data, const size_t count) { + std::vector result; + + std::vector mask(data.size(), false); + std::fill(mask.begin(), mask.begin() + count, true); + + do { + result.push_back(select(data, mask, count)); + } while (std::next_permutation(mask.begin(), mask.end(), std::greater<>())); + + return result; +} diff --git a/src/combotest.cpp b/src/combotest.cpp new file mode 100644 index 0000000..60cb52d --- /dev/null +++ b/src/combotest.cpp @@ -0,0 +1,22 @@ +#include + +#include + +std::ostream &operator<<(std::ostream &o, const std::vector &data) { + o << "[ "; + for (const auto &el: data) { + o << el << " "; + } + o << "]"; + return o; +} + +int main() { + std::vector data{1, 2, 3, 4, 5}; + + for (const auto &combo: combinations(data, 3)) { + std::cout << combo << std::endl; + } + + return EXIT_SUCCESS; +}