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

#include <initializer_list>

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

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

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

25
26
27
    template <class Tuple, class Functor>
    constexpr void for_each(Tuple&& tuple, Functor&& f)
    {
28
      Tools::for_each(std::make_index_sequence<static_size_v<Tuple>>{}, FWD(tuple), FWD(f));
29
30
    }

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

    template <class T, class Functor>
    constexpr void for_each(std::vector<T> const& v, Functor&& f)
    {
      for (auto const& vi : v)
        f(vi);
    }

45
46
47
48
49

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

    template <std::size_t I0, std::size_t I1, class Functor>
Praetorius, Simon's avatar
Praetorius, Simon committed
53
    constexpr void for_range(index_t<I0>, index_t<I1>, Functor&& f)
54
    {
55
      Tools::for_range<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
56
57
58
59
60
    }

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

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

  } // end namespace Tools
} // end namespace AMDiS