ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
cartesian_product.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef UTILS_CARTESIAN_PRODUCT_HPP
20#define UTILS_CARTESIAN_PRODUCT_HPP
21
22#include <iterator>
23#include <utility>
24
25namespace Utils {
26namespace detail {
27
28template <class Body, class...> struct cart_prod_impl {
29 template <class... Is> void operator()(const Body &op, Is... is) const {
30 op((*is)...);
31 }
32
33 void operator()(const Body &) const { ; }
34};
35
36template <class Body, class Head, class... Tail>
37struct cart_prod_impl<Body, Head, Tail...> {
38 template <class... Is>
39 void operator()(const Head &head, const Tail... tail, Is... is) const {
40 for (auto it = std::begin(head); it != std::end(head); ++it) {
41 detail::cart_prod_impl<Body, Tail...>{}(tail..., is..., it);
42 }
43 }
44};
45} // namespace detail
46
47/**
48 * @brief Call op with each element of the cartesian product set of rng.
49 *
50 * @param op Operation to call for each element of the product set.
51 * @param rng Ranges to form the product over
52 */
53template <typename Body, typename... ForwardRange>
54void cartesian_product(const Body &op, const ForwardRange &...rng) {
55 detail::cart_prod_impl<Body, ForwardRange...>{}(rng..., op);
56}
57} // namespace Utils
58
59#endif
void cartesian_product(const Body &op, const ForwardRange &...rng)
Call op with each element of the cartesian product set of rng.