mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 03:52:48 -05:00
WIP: Copy combinations logic from demo
This commit is contained in:
@@ -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
30
include/combo.hpp
Normal 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
22
src/combotest.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user