ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
for_each_3d.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 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
20#pragma once
21
22#include <concepts>
23#include <cstddef>
24
25namespace detail {
26
27constexpr inline void noop_projector(unsigned, int) {}
28
29template <typename T>
30concept IndexVectorConcept = requires(T vector) {
31 { vector[0] } -> std::convertible_to<std::size_t>;
32};
33
34} // namespace detail
35
36/**
37 * @brief Repeat an operation on every element of a 3D grid.
38 *
39 * Intermediate values that depend on the iterated coordinates
40 * are calculated and stored once per iteration. This is useful
41 * when the operation is costly.
42 *
43 * @param start Initial values for the loop counters.
44 * @param stop Final values (one-past-the-end) for the loop counters.
45 * @param counters Loop counters.
46 * @param kernel Functor to execute.
47 * @param projector Projection of the current loop counter.
48 * @tparam Kernel Nullary function.
49 * @tparam Projector Binary function that takes a nesting depth and a loop
50 * counter as arguments and projects a value.
51 */
52template <class Kernel, class Projector = decltype(detail::noop_projector)>
53 requires std::invocable<Kernel> and std::invocable<Projector, unsigned, int>
54void for_each_3d(detail::IndexVectorConcept auto &&start,
55 detail::IndexVectorConcept auto &&stop,
56 detail::IndexVectorConcept auto &&counters, Kernel &&kernel,
57 Projector &&projector = detail::noop_projector) {
58 auto &nx = counters[0u];
59 auto &ny = counters[1u];
60 auto &nz = counters[2u];
61 for (nx = start[0u]; nx < stop[0u]; ++nx) {
62 projector(0u, nx);
63 for (ny = start[1u]; ny < stop[1u]; ++ny) {
64 projector(1u, ny);
65 for (nz = start[2u]; nz < stop[2u]; ++nz) {
66 projector(2u, nz);
67 kernel();
68 }
69 }
70 }
71}
and std::invocable< Projector, unsigned, int > void for_each_3d(detail::IndexVectorConcept auto &&start, detail::IndexVectorConcept auto &&stop, detail::IndexVectorConcept auto &&counters, Kernel &&kernel, Projector &&projector=detail::noop_projector)
Repeat an operation on every element of a 3D grid.
std::vector< T, allocator< T > > vector
Definition vector.hpp:52