GenericLoops.h 6.61 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************************************************
 *
 * Extension of AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: Simon Praetorius et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/

#ifndef EXTENSIONS_GENERIC_LOOPS_H
#define EXTENSIONS_GENERIC_LOOPS_H

#include "boost/tuple/tuple.hpp"

#define _GET_ boost::tuples::get
#define _GET_TYPE_ boost::tuples::element
#define _LENGTH_ boost::tuples::length

namespace tools {
  
  
  // for-loop over sequence of values
  template<typename Seq, long I, long N, class F>
  struct FOR_SUBSEQ
  {
    static void loop() {
      F::call(); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop();
    }
    static void loop_idx() {
      F::call(I); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop_idx();
    }
    static void loop(Seq& s) {
      F::call(_GET_<I>(s)); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop(s);
    }
    static void loop_idx(Seq& s) {
      F::call(I,_GET_<I>(s)); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop_idx(s);
    }
    
    template<typename T>
    static void loop1(Seq& s, T& t) {
      F::call(_GET_<I>(s), t); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop1(s, t);
    }
    template<typename T>
    static void loop1_idx(Seq& s, T& t) {
      F::call(I, _GET_<I>(s), t); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop1_idx(s, t);
    }
    template<typename T1, typename T2>
    static void loop1(Seq& s, T1& t1, T2& t2) {
      F::call(_GET_<I>(s), t1, t2); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop1(s, t1, t2);
    }
    template<typename T1, typename T2>
    static void loop1_idx(Seq& s, T1& t1, T2& t2) {
      F::call(I, _GET_<I>(s), t1, t2); // your implementation here
      FOR_SUBSEQ<Seq, I+1, N, F>::loop1_idx(s, t1, t2);
    }
    
    template<typename T>
    static void loop2(T& t, Seq& s) {
      F::call(t, _GET_<I>(s));
      FOR_SUBSEQ<Seq, I+1, N, F>::loop2(t, s);
    }
    template<typename T>
    static void loop2_idx(T& t, Seq& s) {
      F::template call<I>(t, _GET_<I>(s));
      FOR_SUBSEQ<Seq, I+1, N, F>::loop2_idx(t, s);
    }
    template<typename T0, typename T2>
    static void loop2(T0& t0, Seq& s, T2& t2) {
      F::call(t0, _GET_<I>(s), t2);
      FOR_SUBSEQ<Seq, I+1, N, F>::loop2(t0, s, t2);
    }
    template<typename T0, typename T2>
    static void loop2_idx(T0& t0, Seq& s, T2& t2) {
      F::template call<I>(t0, _GET_<I>(s), t2);
      FOR_SUBSEQ<Seq, I+1, N, F>::loop2_idx(t0, s, t2);
    }
    
    template<typename T0, typename T1>
    static void loop3(T0& t0, T1& t1, Seq& s) {
      F::call(t0, t1, _GET_<I>(s));
      FOR_SUBSEQ<Seq, I+1, N, F>::loop3(t0, t1, s);
    }
    template<typename T0, typename T1>
    static void loop3_idx(T0& t0, T1& t1, Seq& s) {
      F::call(I, t0, t1, _GET_<I>(s));
      FOR_SUBSEQ<Seq, I+1, N, F>::loop3_idx(t0, t1, s);
    }
  };

  // initial/break condition for 'for-loop' over sequences: I==N
  template<typename Seq, long N, class F> 
  struct FOR_SUBSEQ<Seq,N,N,F> 
  { 
    static void loop() {} 
    static void loop_idx() {} 
    static void loop(Seq& t) {}
    static void loop_idx(Seq& t) {}
    
    template<typename T>
    static void loop1(Seq& s, T& t) {}
    template<typename T>
    static void loop1_idx(Seq& s, T& t) {}    
    template<typename T1, typename T2>
    static void loop1(Seq& s, T1& t1, T2& t2) {}
    template<typename T1, typename T2>
    static void loop1_idx(Seq& s, T1& t1, T2& t2) {}
    
    template<typename T>
    static void loop2(T& t, Seq& s) {}
    template<typename T>
    static void loop2_idx(T& t, Seq& s) {}
    template<typename T0, typename T2>
    static void loop2(T0& t0, Seq& s, T2& t2) {}
    template<typename T0, typename T2>
    static void loop2_idx(T0& t0, Seq& s, T2& t2) {}
    
    template<typename T0, typename T1>
    static void loop3(T0& t0, T1& t1, Seq& s) {}
    template<typename T0, typename T1>
    static void loop3_idx(T0& t0, T1& t1, Seq& s) {}
  };

  /// \brief
  /// interface for loop over sequence of values
  /// for i in {seq[0]..seq[end]}
  ///
  template<class F> 
  struct FOR_EACH
  {
    template<typename Seq>
    static void loop() { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop(); } 
    template<typename Seq>
    static void loop_idx() { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop_idx(); } 
    template<typename Seq>
    static void loop(Seq& s) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop(s); } 
    template<typename Seq>
    static void loop_idx(Seq& s) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop_idx(s); } 
    
    template<typename Seq, typename T>
    static void loop1(Seq& s, T& t) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop1(s, t); } 
    template<typename Seq, typename T>
    static void loop1_idx(Seq& s, T& t) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop1_idx(s, t); } 
    template<typename Seq, typename T1, typename T2>
    static void loop1(Seq& s, T1& t1, T2& t2) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop1(s, t1, t2); } 
    template<typename Seq, typename T1, typename T2>
    static void loop1_idx(Seq& s, T1& t1, T2& t2) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop1_idx(s, t1, t2); } 
    
    template<typename T, typename Seq>
    static void loop2(T& t, Seq& s) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop2(t, s); } 
    template<typename T, typename Seq>
    static void loop2_idx(T& t, Seq& s) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop2_idx(t, s); } 
    template<typename T0, typename Seq, typename T2>
    static void loop2(T0& t0, Seq& s, T2& t2) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop2(t0, s, t2); } 
    template<typename T0, typename Seq, typename T2>
    static void loop2_idx(T0& t0, Seq& s, T2& t2) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop2_idx(t0, s, t2); } 
    
    template<typename T0, typename T1, typename Seq>
    static void loop3(T0& t0, T1& t1, Seq& s) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop3(t0, t1, s); } 
    template<typename T0, typename T1, typename Seq>
    static void loop3_idx(T0& t0, T1& t1, Seq& s) { FOR_SUBSEQ<Seq, 0, _LENGTH_<Seq>::value, F>::loop3_idx(t0, t1, s); } 
  };
  
} // namespace tools

#endif // EXTENSIONS_GENERIC_LOOPS_H