mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 12:02:47 -05:00
Since the number of generators never exceeds 6, the maximum number of combinations is (6, 3) = 20, so the space optimizations of using an iterator is mute. Doing this way also allows to use set_difference and set_union to deal with collections of subgroups. This is not easily possible otherwise.
45 lines
970 B
C++
45 lines
970 B
C++
#pragma once
|
|
|
|
#include <vector>
|
|
#include <set>
|
|
#include <algorithm>
|
|
|
|
template<class V>
|
|
V select(const V &options, const std::vector<bool> &mask, size_t count) {
|
|
V result;
|
|
result.reserve(count);
|
|
|
|
for (int i = 0; i < mask.size(); ++i) {
|
|
if (mask[i]) result.push_back(options[i]);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
template<class V>
|
|
std::set<V> combinations(const V &options, size_t count) {
|
|
std::set<V> result;
|
|
|
|
std::vector<bool> mask(options.size(), false);
|
|
std::fill(mask.begin(), mask.begin() + count, true);
|
|
|
|
do {
|
|
result.insert(select(options, mask, count));
|
|
} while (std::next_permutation(mask.begin(), mask.end(), std::greater<>()));
|
|
|
|
return result;
|
|
}
|
|
|
|
template<class V>
|
|
std::set<V> difference(const std::set<V> &a, const std::set<V> &b) {
|
|
std::set<V> result;
|
|
|
|
std::set_difference(
|
|
a.begin(), a.end(),
|
|
b.begin(), b.end(),
|
|
std::inserter(result, result.end())
|
|
);
|
|
|
|
return result;
|
|
}
|