-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.hpp
31 lines (28 loc) · 836 Bytes
/
util.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef UTIL_WILLIAM_KILLIAN_2018_ADVENT_HPP_
#define UTIL_WILLIAM_KILLIAN_2018_ADVENT_HPP_
#include <algorithm>
#include <iterator>
#include <optional>
#include <type_traits>
#include <vector>
template <typename Container, typename Fn>
auto
combinations(Container const& c, size_t k, Fn&& f) {
using T = std::add_const_t<typename Container::value_type>;
using R = decltype(f(std::declval<std::vector<std::reference_wrapper<T>>>()));
std::vector<std::reference_wrapper<T>> res(std::begin(c), std::begin(c) + k);
std::vector<bool> v(k, true);
v.resize(std::size(c));
do {
for (size_t i{0}, j{0}; i < v.size(); ++i) {
if (v[i]) {
res[j++] = c[i];
}
}
if (auto val = f(res); val) {
return val;
}
} while (std::prev_permutation(std::begin(v), std::end(v)));
return R{};
}
#endif