ForEach.hpp 1.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

#include <initializer_list>

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

namespace AMDiS
{
  namespace Tools
  {
    namespace Impl_
    {
      template <class T>
      void ignored_evaluation(std::initializer_list<T>&&) { /* do nothing */ }
    }

18
19
20
21
22
23
24
25
26
27
28
29
30
    template <std::size_t... I, class Tuple, class Functor>
    constexpr void for_each(std::index_sequence<I...>, Tuple&& tuple, Functor&& f)
    {
      using std::get;
#if AMDIS_HAS_EXPANSION_STATEMENTS
      for... (auto&& t : tuple) { f(FWD(t)); }
#elif AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      (f(get<I>(tuple)),...);
#else
      Impl_::ignored_evaluation<int>({0, (f(get<I>(tuple)), 0)...});
#endif
    }

31
32
33
    template <class Tuple, class Functor>
    constexpr void for_each(Tuple&& tuple, Functor&& f)
    {
34
35
36
37
38
39
40
41
42
43
44
45
      Tools::for_each(std::make_index_sequence<Size_v<std::remove_reference_t<Tuple>>>{}, FWD(tuple), FWD(f));
    }


    template <std::size_t I0 = 0, std::size_t... I, class Functor>
    constexpr void for_range(std::index_sequence<I...>, Functor&& f)
    {
#if AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      (f(index_t<I0+I>{}),...);
#else
      Impl_::ignored_evaluation<int>({0, (f(index_t<I0+I>{}), 0)...});
#endif
46
47
48
49
50
    }

    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)
    {
51
      Tools::for_range<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
52
53
54
55
56
    }

    template <std::size_t N, class Functor>
    constexpr void for_range(index_t<N>, Functor&& f)
    {
57
      Tools::for_range(std::make_index_sequence<N>{}, FWD(f));
58
59
60
61
62
    }

    template <std::size_t I0, std::size_t I1, class Functor>
    constexpr void for_range(Functor&& f)
    {
63
      Tools::for_range<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
64
65
66
67
    }

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