DDC 0.1.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 RetType, class Element, std::size_t N, class Functor, class... Is>
20void for_each_serial(
21 std::array<Element, N> const& begin,
22 std::array<Element, N> const& end,
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(RetType(is...));
29 } else {
30 for (Element ii = begin[I]; ii < end[I]; ++ii) {
31 for_each_serial<RetType>(begin, end, 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... DDims, class Functor>
43void for_each(DiscreteDomain<DDims...> const& domain, Functor&& f) noexcept
44{
45 DiscreteElement<DDims...> const ddc_begin = domain.front();
46 DiscreteElement<DDims...> const ddc_end = domain.front() + domain.extents();
47 std::array const begin = detail::array(ddc_begin);
48 std::array const end = detail::array(ddc_end);
49 detail::for_each_serial<DiscreteElement<DDims...>>(begin, end, std::forward<Functor>(f));
50}
51
52} // namespace ddc
friend class DiscreteDomain
The top-level namespace of DDC.
void for_each(DiscreteDomain< DDims... > const &domain, Functor &&f) noexcept
iterates over a nD domain in serial
Definition for_each.hpp:43