WIP: Copy combinations logic from demo

This commit is contained in:
David Allemang
2022-02-24 14:58:45 -05:00
parent 63eb9e47b4
commit 0502cb0a7e
3 changed files with 56 additions and 1 deletions

View File

@@ -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)

30
include/combo.hpp Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <set>
#include <algorithm>
template<typename V, typename M>
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<typename V>
std::vector<V> combinations(const V &data, const size_t count) {
std::vector<V> result;
std::vector<bool> 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;
}

22
src/combotest.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <combo.hpp>
#include <iostream>
std::ostream &operator<<(std::ostream &o, const std::vector<int> &data) {
o << "[ ";
for (const auto &el: data) {
o << el << " ";
}
o << "]";
return o;
}
int main() {
std::vector<int> data{1, 2, 3, 4, 5};
for (const auto &combo: combinations(data, 3)) {
std::cout << combo << std::endl;
}
return EXIT_SUCCESS;
}