-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraversal_defs.h
184 lines (155 loc) · 4.44 KB
/
Traversal_defs.h
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
#ifndef __TRAVERSAL_DEFS_H__
#define __TRAVERSAL_DEFS_H__
#include "DataManager.h"
template<typename T>
void Traversal<T>::preorderTraversal(Node<T> *root, CutoffWorker<T> *worker){
stack<Node<T>*> stk;
stk.push(root);
while(!stk.empty()){
Node<T> *t_node = stk.top();
stk.pop();
int ret = worker->work(t_node);
if(ret > 0){
Node<T> *t_children = t_node->getChildren();
for(int i = t_node->getNumChildren()-1; i >= 0; i--){
stk.push(t_children+i);
}
}
}
}
template<typename T>
void Traversal<T>::topDownTraversal_local(Node<T> *root, CutoffWorker<T> *worker){
stack<Node<T>*> ptrStk;
int ret = worker->work(root);
if(ret == 0) return;
ptrStk.push(root);
Node<T> *tmp[BRANCH_FACTOR];
int numChildrenToExpand;
while(!ptrStk.empty()){
Node<T> *t_parent = ptrStk.top();
Node<T> *t_children = t_parent->getChildren();
int t_numChildren = t_parent->getNumChildren();
ptrStk.pop();
numChildrenToExpand = 0;
for(int i = 0; i < t_numChildren; i++){
Node<T> *t_node = t_children+i;
ret = worker->work(t_node);
if(ret > 0 && t_node->getNumChildren() > 0){
tmp[numChildrenToExpand] = t_node;
numChildrenToExpand++;
}
}
for(int i = numChildrenToExpand-1; i >= 0; i--){
ptrStk.push(tmp[i]);
}
}
}
template<typename T>
void Traversal<T>::topDownTraversal(Node<T> *root, CutoffWorker<T> *worker, State *state){
stack<Node<T>*> ptrStk;
int ret = worker->work(root);
if(ret == 0) return;
ptrStk.push(root);
Node<T> *tmp[BRANCH_FACTOR];
int numChildrenToExpand;
while(!ptrStk.empty()){
Node<T> *t_parent = ptrStk.top();
Node<T> *t_children = t_parent->getChildren();
int t_numChildren = t_parent->getNumChildren();
ptrStk.pop();
numChildrenToExpand = 0;
if(t_numChildren == 0){
processLeaf(t_parent,worker,state);
continue;
}
for(int i = 0; i < t_numChildren; i++){
Node<T> *t_node = t_children+i;
ret = worker->work(t_node);
if(ret > 0){
tmp[numChildrenToExpand] = t_node;
numChildrenToExpand++;
}
}
for(int i = numChildrenToExpand-1; i >= 0; i--){
ptrStk.push(tmp[i]);
}
}
}
template<typename T>
void Traversal<T>::processLeaf(Node<T> *leaf, CutoffWorker<T> *worker, State *state){
NodeType type = leaf->getType();
CkAssert(type != Internal);
CkAssert(type != Boundary);
if(type == EmptyBucket || type == RemoteEmptyBucket) return;
if(type == Bucket){
Particle *particles = leaf->getParticles();
int np = leaf->getNumParticles();
CkAssert(particles);
CkAssert(np > 0);
for(int i = 0; i < np; i++){
worker->work(particles+i);
}
worker->bucketDone(leaf->getKey());
}
else if(type == RemoteBucket){
ExternalParticle *particles = (ExternalParticle *)leaf->getParticles();
int np = leaf->getNumParticles();
if(particles == NULL){
state->incrPending();
dm->requestParticles(leaf,worker,state,this);
return;
}
for(int i = 0; i < np; i++){
worker->work(particles+i);
}
worker->bucketDone(leaf->getKey());
}
else if(type == Remote){
state->incrPending();
dm->requestNode(leaf,worker,state,this);
}
}
template<typename T>
void Traversal<T>::postorderTraversal(Node<T> *root, CutoffWorker<T> *worker){
// value returned from work() is ignored
// since it doesn't make sense in a postorder
// traversal
stack<Node<T>*> cstk;
stack<Node<T>*> nstk;
cstk.push(root);
nstk.push(root);
while(!cstk.empty()){
Node<T> *c_node = cstk.top();
Node<T> *n_node = nstk.top();
// we have processed the children of
// n_node, but not n_node itself
if(c_node != n_node){
worker->work(n_node);
nstk.pop();
continue;
}
// here, we have a node whose children
// haven't yet been processed
cstk.pop();
// first check whether the node has children
if(c_node->getNumChildren() > 0){
// we must explore the children of this node
for(int i = c_node->getNumChildren()-1; i >= 0; i--){
Node<T> *child = c_node->getChild(i);
int num = child->getNumChildren();
cstk.push(child);
nstk.push(child);
}
} else {
// this node does not have any children
// so it is ok to visit it
worker->work(n_node);
nstk.pop();
}
}
while(!nstk.empty()){
worker->work(nstk.top());
nstk.pop();
}
}
#endif // __TRAVERSAL_DEFS_H__