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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
| /* ===============================================================
* Tree traversal
* recursive() below is for reference only. The macro
* foreach_cell() is a stack-based implementation of
* recursive(). It is about 12% slower than the recursive
* version and 60% slower than simple array traversal.
*
* This article was useful:
* http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and
*
* =============================================================== */
#define _BOTTOM (2*point.j - GHOSTS)
#define _TOP (_BOTTOM + 1)
#define _LEFT (2*point.i - GHOSTS)
#define _RIGHT (_LEFT + 1)
#define _BACK (2*point.k - GHOSTS)
#define _FRONT (_BACK + 1)
#if 0
void recursive (Point point)
{
if (point.level == grid->depth) {
/* do something */
}
else {
Point p1 = point; p1.level = point.level + 1;
p1.i = _LEFT; p1.j = _TOP; recursive (p1);
p1.i = _RIGHT; p1.j = _TOP; recursive (p1);
p1.i = _LEFT; p1.j = _BOTTOM; recursive (p1);
p1.i = _RIGHT; p1.j = _BOTTOM; recursive (p1);
}
}
#endif
#define STACKSIZE 20
#if dimension == 1
#define _push(b,c,d,e,f) \
{ _s++; stack[_s].l = b; \
stack[_s].i = c; \
stack[_s].stage = f; }
#define _pop() \
{ point.level = stack[_s].l; \
point.i = stack[_s].i; \
stage = stack[_s].stage; _s--; }
#elif dimension == 2
#define _push(b,c,d,e,f) \
{ _s++; stack[_s].l = b; \
stack[_s].i = c; stack[_s].j = d; \
stack[_s].stage = f; }
#define _pop() \
{ point.level = stack[_s].l; \
point.i = stack[_s].i; point.j = stack[_s].j; \
stage = stack[_s].stage; _s--; }
#else // dimension == 3
#define _push(b,c,d,e,f) \
{ _s++; stack[_s].l = b; \
stack[_s].i = c; stack[_s].j = d; stack[_s].k = e; \
stack[_s].stage = f; }
#define _pop() \
{ point.level = stack[_s].l; \
point.i = stack[_s].i; point.j = stack[_s].j; point.k = stack[_s].k; \
stage = stack[_s].stage; _s--; }
#endif
macro2 foreach_cell_root (Point root)
{
{
int ig = 0, jg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg);
Point point = {0};
#if dimension == 1
struct { int l, i, stage; } stack[STACKSIZE];
#elif dimension == 2
struct { int l, i, j, stage; } stack[STACKSIZE];
#else // dimension == 3
int kg = 0; NOT_UNUSED(kg);
struct { int l, i, j, k, stage; } stack[STACKSIZE];
#endif
int _s = -1;
_push (0, root.i, root.j, root.k, 0);
while (_s >= 0) {
int stage;
_pop();
if (!allocated (0,0,0))
continue;
switch (stage) {
case 0: {
{...}
if (point.level < grid->depth) {
_push (point.level, point.i, point.j, point.k, 1);
_push (point.level + 1, _LEFT, _BOTTOM, _BACK, 0);
}
break;
}
#if dimension == 1
case 1: _push (point.level + 1, _RIGHT, _BOTTOM, _BACK, 0); break;
#elif dimension == 2
case 1: _push (point.level, point.i, point.j, point.k, 2);
_push (point.level + 1, _LEFT, _TOP, _BACK, 0); break;
case 2: _push (point.level, point.i, point.j, point.k, 3);
_push (point.level + 1, _RIGHT, _BOTTOM, _BACK, 0); break;
case 3: _push (point.level + 1, _RIGHT, _TOP, _BACK, 0); break;
#elif dimension == 3
case 1: _push (point.level, point.i, point.j, point.k, 2);
_push (point.level + 1, _LEFT, _BOTTOM, _FRONT, 0); break;
case 2: _push (point.level, point.i, point.j, point.k, 3);
_push (point.level + 1, _LEFT, _TOP, _BACK, 0); break;
case 3: _push (point.level, point.i, point.j, point.k, 4);
_push (point.level + 1, _LEFT, _TOP, _FRONT, 0); break;
case 4: _push (point.level, point.i, point.j, point.k, 5);
_push (point.level + 1, _RIGHT, _BOTTOM, _BACK, 0); break;
case 5: _push (point.level, point.i, point.j, point.k, 6);
_push (point.level + 1, _RIGHT, _BOTTOM, _FRONT, 0); break;
case 6: _push (point.level, point.i, point.j, point.k, 7);
_push (point.level + 1, _RIGHT, _TOP, _BACK, 0); break;
case 7: _push (point.level + 1, _RIGHT, _TOP, _FRONT, 0); break;
#endif
}
}
}
}
macro2 foreach_cell()
{
{
#if dimension == 1
Point root = {GHOSTS,0};
#elif dimension == 2
Point root = {GHOSTS,GHOSTS,0};
#else // dimension == 3
Point root = {GHOSTS,GHOSTS,GHOSTS,0};
#endif
foreach_cell_root (root)
{...}
}
}
macro2 foreach_cell_all() {
{
Point root = {0};
for (root.i = GHOSTS*Period.x; root.i <= GHOSTS*(2 - Period.x); root.i++)
#if dimension >= 2
for (root.j = GHOSTS*Period.y; root.j <= GHOSTS*(2 - Period.y); root.j++)
#endif
#if dimension >= 3
for (root.k = GHOSTS*Period.z; root.k <= GHOSTS*(2 - Period.z); root.k++)
#endif
foreach_cell_root (root)
{...}
}
}
macro2 foreach_cell_post_root (bool condition, Point root)
{
{
int ig = 0, jg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg);
Point point = {0};
#if dimension == 1
struct { int l, i, stage; } stack[STACKSIZE];
#elif dimension == 2
struct { int l, i, j, stage; } stack[STACKSIZE];
#else // dimension == 3
int kg = 0; NOT_UNUSED(kg);
struct { int l, i, j, k, stage; } stack[STACKSIZE];
#endif
int _s = -1;
_push (0, root.i, root.j, root.k, 0); /* the root cell */
while (_s >= 0) {
int stage;
_pop();
if (!allocated (0,0,0))
continue;
switch (stage) {
case 0: {
if (point.level == grid->depth) {
_push (point.level, point.i, point.j, point.k, 8);
}
else {
_push (point.level, point.i, point.j, point.k, 1);
if (condition)
_push (point.level + 1, _LEFT, _BOTTOM, _BACK, 0);
}
break;
}
#if dimension == 1
case 1:
_push (point.level, point.i, point.j, point.k, 2);
if (condition)
_push (point.level + 1, _RIGHT, _BOTTOM, _BACK, 0);
break;
#elif dimension == 2
case 1:
_push (point.level, point.i, point.j, point.k, 2);
if (condition)
_push (point.level + 1, _LEFT, _TOP, _BACK, 0);
break;
case 2:
_push (point.level, point.i, point.j, point.k, 3);
if (condition)
_push (point.level + 1, _RIGHT, _BOTTOM, _BACK, 0);
break;
case 3:
_push (point.level, point.i, point.j, point.k, 4);
if (condition)
_push (point.level + 1, _RIGHT, _TOP, _BACK, 0);
break;
#elif dimension == 3
case 1:
_push (point.level, point.i, point.j, point.k, 2);
if (condition)
_push (point.level + 1, _LEFT, _BOTTOM, _FRONT, 0);
break;
case 2:
_push (point.level, point.i, point.j, point.k, 3);
if (condition)
_push (point.level + 1, _LEFT, _TOP, _BACK, 0);
break;
case 3:
_push (point.level, point.i, point.j, point.k, 4);
if (condition)
_push (point.level + 1, _LEFT, _TOP, _FRONT, 0);
break;
case 4:
_push (point.level, point.i, point.j, point.k, 5);
if (condition)
_push (point.level + 1, _RIGHT, _BOTTOM, _BACK, 0);
break;
case 5:
_push (point.level, point.i, point.j, point.k, 6);
if (condition)
_push (point.level + 1, _RIGHT, _BOTTOM, _FRONT, 0);
break;
case 6:
_push (point.level, point.i, point.j, point.k, 7);
if (condition)
_push (point.level + 1, _RIGHT, _TOP, _BACK, 0);
break;
case 7:
_push (point.level, point.i, point.j, point.k, 8);
if (condition)
_push (point.level + 1, _RIGHT, _TOP, _FRONT, 0);
break;
#endif
default:
{...}
}
}
}
}
macro2 foreach_cell_post (bool condition)
{
{
#if dimension == 1
Point root = {GHOSTS,0};
#elif dimension == 2
Point root = {GHOSTS,GHOSTS,0};
#else // dimension == 3
Point root = {GHOSTS,GHOSTS,GHOSTS,0};
#endif
foreach_cell_post_root (condition, root)
{...}
}
}
macro2 foreach_cell_post_all (bool condition)
{
{
Point root = {0};
for (root.i = 0; root.i <= 2*GHOSTS; root.i++)
#if dimension >= 2
for (root.j = 0; root.j <= 2*GHOSTS; root.j++)
#endif
#if dimension >= 3
for (root.k = 0; root.k <= 2*GHOSTS; root.k++)
#endif
foreach_cell_post_root (condition, root)
{...}
}
}
macro2 foreach_leaf()
{
foreach_cell()
if (is_leaf (cell)) {
if (is_active(cell) && is_local(cell))
{...}
continue;
}
}
|