ForEach.hpp 1.51 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
31
32
33
34
35
    }


    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>{}),...);
36
37
38
39
40
    }

    template <std::size_t I0, std::size_t I1, class Functor>
    constexpr void for_range(index_t<I0> i0, index_t<I1> i1, Functor&& f)
    {
41
      Tools::for_range<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
42
43
44
45
46
    }

    template <std::size_t N, class Functor>
    constexpr void for_range(index_t<N>, Functor&& f)
    {
47
      Tools::for_range(std::make_index_sequence<N>{}, FWD(f));
48
49
50
51
52
    }

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

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