DDC 0.5.0
Loading...
Searching...
No Matches
for_each.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <utility>
10
11#include "ddc/discrete_domain.hpp"
12#include "ddc/discrete_element.hpp"
13#include "ddc/discrete_vector.hpp"
14
15namespace ddc {
16
17namespace detail {
18
19template <class Support, class Element, std::size_t N, class Functor, class... Is>
20void for_each_serial(
21 Support const& support,
22 std::array<Element, N> const& size,
23 Functor const& f,
24 Is const&... is) noexcept
25{
26 static constexpr std::size_t I = sizeof...(Is);
27 if constexpr (I == N) {
28 f(support(typename Support::discrete_vector_type(is...)));
29 } else {
30 for (Element ii = 0; ii < size[I]; ++ii) {
31 for_each_serial(support, size, f, is..., ii);
32 }
33 }
34}
35
36} // namespace detail
37
38/** iterates over a nD domain in serial
39 * @param[in] domain the domain over which to iterate
40 * @param[in] f a functor taking an index as parameter
41 */
42template <class Support, class Functor>
43void for_each(Support const& domain, Functor&& f) noexcept
44{
45 std::array const size = detail::array(domain.extents());
46 detail::for_each_serial(domain, size, std::forward<Functor>(f));
47}
48
49} // namespace ddc
The top-level namespace of DDC.
void for_each(Support const &domain, Functor &&f) noexcept
iterates over a nD domain in serial
Definition for_each.hpp:43