ForEach.hpp 2.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
#pragma once

#include <initializer_list>

#include <amdis/common/Index.hpp>
#include <amdis/common/Range.hpp>

namespace AMDiS
{
10
  namespace Ranges
11
  {
12
    template <class Functor, class... Args>
13
    constexpr void forVariadic(Functor&& f, Args&&... args)
14
15
16
17
    {
      (f(FWD(args)),...);
    }

18
    template <std::size_t... I, class Tuple, class Functor>
19
    constexpr void forEach(std::index_sequence<I...>, Tuple&& tuple, Functor&& f)
20
21
22
23
24
    {
      using std::get;
      (f(get<I>(tuple)),...);
    }

25
26
    template <class Tuple, class Functor,
      class = decltype(std::get<0>(std::declval<Tuple>()))>
27
    constexpr void forEach(Tuple&& tuple, Functor&& f)
28
    {
29
      Ranges::forEach(std::make_index_sequence<static_size_v<Tuple>>{}, FWD(tuple), FWD(f));
30
31
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
32
    template <class T, std::size_t n, class Functor>
33
    constexpr void forEach(std::array<T,n> const& a, Functor&& f)
Praetorius, Simon's avatar
Praetorius, Simon committed
34
35
36
37
38
39
    {
      for (auto const& ai : a)
        f(ai);
    }

    template <class T, class Functor>
40
    constexpr void forEach(std::vector<T> const& v, Functor&& f)
Praetorius, Simon's avatar
Praetorius, Simon committed
41
42
43
44
45
    {
      for (auto const& vi : v)
        f(vi);
    }

46
47

    template <std::size_t I0 = 0, std::size_t... I, class Functor>
48
    constexpr void forIndices(std::index_sequence<I...>, Functor&& f)
49
50
    {
      (f(index_t<I0+I>{}),...);
51
52
53
    }

    template <std::size_t I0, std::size_t I1, class Functor>
54
    constexpr void forIndices(index_t<I0>, index_t<I1>, Functor&& f)
55
    {
56
      Ranges::forIndices<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
57
58
59
    }

    template <std::size_t N, class Functor>
60
    constexpr void forIndices(index_t<N>, Functor&& f)
61
    {
62
      Ranges::forIndices(std::make_index_sequence<N>{}, FWD(f));
63
64
65
    }

    template <std::size_t I0, std::size_t I1, class Functor>
66
    constexpr void forIndices(Functor&& f)
67
    {
68
      Ranges::forIndices<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
69
70
    }

71
72
73
74
75
76
77
    template <std::size_t N, class Functor>
    constexpr void forIndices(Functor&& f)
    {
      Ranges::forIndices<0>(std::make_index_sequence<N>{}, FWD(f));
    }

  } // end namespace Ranges
78
} // end namespace AMDiS