ForEach.hpp 2.01 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
    template <class Tuple, class Functor>
26
    constexpr void forEach(Tuple&& tuple, Functor&& f)
27
    {
28
      Ranges::forEach(std::make_index_sequence<static_size_v<Tuple>>{}, FWD(tuple), FWD(f));
29
30
    }

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

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

45
46

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

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

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

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

70
71
72
73
74
75
76
    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
77
} // end namespace AMDiS