forked from nwf/nwf-openamd-localizer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspaceparttree.c
313 lines (267 loc) · 7.12 KB
/
spaceparttree.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib/ghash.h>
#include "spaceparttree_priv.h"
#include "spaceparttree.h"
#define MAX_LINE_SIZE 256
static const char *spt_read_delim = " \t\r\n";
void spt_free_node(gpointer);
#define SPT_NODE_TYPE_BSP_CHAR 'B'
#define SPT_NODE_TYPE_LABEL_CHAR 'L'
spt *
spt_read_from_file(FILE *f)
{
spt *ret = calloc(1,sizeof(spt));
ret->nodebyname = g_hash_table_new_full(g_str_hash,
g_str_equal,
NULL,
spt_free_node);
int lineno = 0;
char line[MAX_LINE_SIZE];
char *toksave;
spt_node_header *snh = NULL;
if(!ret || !ret->nodebyname) { goto failoom; }
while(fgets(line, MAX_LINE_SIZE, f) != NULL) {
lineno++;
snh = NULL;
char *_nodelabel = strtok_r(line, spt_read_delim, &toksave);
char *_nodetype = strtok_r(NULL, spt_read_delim, &toksave);
/* Empty lines and comments */
if(!_nodelabel || *_nodelabel == '#') { continue; }
if(!_nodetype) {
fprintf(stderr
,"SPT reader aborting on line %d with malformed line.\n"
,lineno);
goto fail;
}
if(strlen(_nodetype) != 1) {
fprintf(stderr
,"SPT reader aborting on line %d with malformed type: '%s'\n"
,lineno, _nodetype);
goto fail;
}
switch(*_nodetype) {
case SPT_NODE_TYPE_LABEL_CHAR: {
spt_node_label *snl = calloc(1, sizeof(spt_node_label));
snh = &snl->header;
if(!snl) { goto failoom; }
snl->printlabel = strdup(strtok_r(NULL, spt_read_delim, &toksave));
if(!snl->printlabel) { goto failoom; }
snl->header.nodetype = SPT_NODE_TYPE_LABEL;
break; }
case SPT_NODE_TYPE_BSP_CHAR: {
spt_node_bsp *snb = calloc(1, sizeof(spt_node_bsp));
snh = &snb->header;
if(!snb) { goto failoom; }
char *_var = strtok_r(NULL, spt_read_delim, &toksave);
char *_comp = strtok_r(NULL, spt_read_delim, &toksave);
char *_value = strtok_r(NULL, spt_read_delim, &toksave);
char *_tname = strtok_r(NULL, spt_read_delim, &toksave);
char *_fname = strtok_r(NULL, spt_read_delim, &toksave);
if(!_fname) {
fprintf(stderr
,"SPT reader aborting on line %d"
" with malformed line (too few parameters)\n"
,lineno);
goto fail;
}
if(strlen(_var) != 1) {
fprintf(stderr
,"SPT reader aborting on line %d"
" with malformed variable: '%s'\n"
,lineno, _var);
goto fail;
}
switch(*_var) {
case 'X': snb->var = SPT_VARIABLE_X; break;
case 'Y': snb->var = SPT_VARIABLE_Y; break;
case 'Z': snb->var = SPT_VARIABLE_Z; break;
default :
fprintf(stderr
,"SPT reader aborting on line %d"
" with unknown comparison variable: '%s'\n"
,lineno, _comp);
goto fail;
}
if(!strcmp(_comp, "<")) {
snb->comp = SPT_COMP_LT;
} else if (!strcmp(_comp, "<=")) {
snb->comp = SPT_COMP_LE;
} else if (!strcmp(_comp, ">=")) {
snb->comp = SPT_COMP_GE;
} else if (!strcmp(_comp, ">")) {
snb->comp = SPT_COMP_GT;
} else {
fprintf(stderr
,"SPT reader aborting on line %d"
" with malformed comparison operator: '%s'\n"
,lineno, _comp);
goto fail;
}
char *endptr;
snb->value = strtod(_value, &endptr);
if(endptr == _value) {
fprintf(stderr
,"SPT reader aborting on line %d"
" with malformed value operator: '%s'\n"
,lineno, _value);
goto fail;
}
snb->t = g_hash_table_lookup(ret->nodebyname, _tname);
if(!snb->t) {
fprintf(stderr
,"SPT reader aborting on line %d"
" with unknown node reference: '%s'\n"
,lineno, _tname);
goto fail;
}
snb->f = g_hash_table_lookup(ret->nodebyname, _fname);
if(!snb->f) {
fprintf(stderr
,"SPT reader aborting on line %d"
" with unknown node reference: '%s'\n"
,lineno, _fname);
goto fail;
}
snb->header.nodetype = SPT_NODE_TYPE_BSP;
break; }
default :
fprintf(stderr
,"SPT reader aborting on line %d with unknown type: '%s'\n"
,lineno,_nodetype);
goto fail;
}
snh->nodelabel = strdup(_nodelabel);
if(!snh->nodelabel) { goto failoom; }
if(g_hash_table_lookup(ret->nodebyname, snh->nodelabel)) {
fprintf(stderr
,"SPT reader aborting on line %d with duplicate node defn\n"
,lineno);
goto fail;
}
g_hash_table_insert(ret->nodebyname, snh->nodelabel, snh);
}
ret->root = snh;
return ret;
failoom:
fprintf(stderr, "SPT reader OOM on line %d!\n", lineno);
goto fail;
fail:
if(snh) free(snh);
g_hash_table_destroy(ret->nodebyname);
free(ret);
return NULL;
}
static char
spt_variable_name(spt_variable v) {
switch(v) {
case SPT_VARIABLE_X: return 'X';
case SPT_VARIABLE_Y: return 'Y';
case SPT_VARIABLE_Z: return 'Z';
default: assert(0);
}
}
static char *
spt_comparison_name(spt_comparison c) {
switch(c) {
case SPT_COMP_LT: return "<";
case SPT_COMP_LE: return "<=";
case SPT_COMP_GE: return ">=";
case SPT_COMP_GT: return ">";
default: assert(0);
}
}
static void
spt_print_internal(FILE *f, int pc, spt_node_header *snh) {
if(snh->printcount == pc) { return; }
snh->printcount = pc;
switch(snh->nodetype) {
case SPT_NODE_TYPE_BSP: {
spt_node_bsp *snb = (spt_node_bsp*)snh;
spt_print_internal(f, pc, snb->t);
spt_print_internal(f, pc, snb->f);
fprintf(f, "%s %c %c %s %g %s %s\n",
snh->nodelabel,
SPT_NODE_TYPE_BSP_CHAR,
spt_variable_name(snb->var),
spt_comparison_name(snb->comp),
snb->value,
snb->t->nodelabel,
snb->f->nodelabel);
break; }
case SPT_NODE_TYPE_LABEL:
fprintf(f, "%s %c %s\n",
snh->nodelabel,
SPT_NODE_TYPE_LABEL_CHAR,
((spt_node_label*)snh)->printlabel);
break;
default: assert(0);
}
}
void
spt_print(spt *s, FILE *f)
{
int pc = ++s->printcount;
spt_print_internal(f, pc, s->root);
fflush(f);
}
static spt_node_label *
spt_lookup_leaf(spt_variables *vs, spt_node_header *snh)
{
switch(snh->nodetype) {
case SPT_NODE_TYPE_LABEL:
return ((spt_node_label*)snh);
case SPT_NODE_TYPE_BSP: {
spt_node_bsp *snb = (spt_node_bsp*)snh;
double v1;
switch(snb->var) {
case SPT_VARIABLE_X: v1 = vs->x; break;
case SPT_VARIABLE_Y: v1 = vs->y; break;
case SPT_VARIABLE_Z: v1 = vs->z; break;
default: assert(0);
}
spt_node_header *targ = snb->f;
switch(snb->comp) {
case SPT_COMP_LT: if(v1 < snb->value) { targ = snb->t; }; break;
case SPT_COMP_LE: if(v1 <= snb->value) { targ = snb->t; }; break;
case SPT_COMP_GE: if(v1 >= snb->value) { targ = snb->t; }; break;
case SPT_COMP_GT: if(v1 > snb->value) { targ = snb->t; }; break;
}
return spt_lookup_leaf(vs, targ); }
default : assert(0);
}
}
char *
spt_label(spt *s, double x, double y, double z)
{
if(s == NULL) { return NULL; }
spt_variables vs = {.x = x, .y = y, .z = z};
spt_node_label *snl = spt_lookup_leaf(&vs, s->root);
return snl->printlabel;
}
void
spt_free_node(gpointer _snh)
{
spt_node_header *snh = (spt_node_header *)_snh;
free(snh->nodelabel);
switch(snh->nodetype) {
case SPT_NODE_TYPE_LABEL:
free(((spt_node_label*)snh)->printlabel);
break;
case SPT_NODE_TYPE_BSP:
break;
default : assert(0);
}
free(snh);
}
void
spt_cleanup(spt **s)
{
if(*s == NULL) { return; }
g_hash_table_destroy((*s)->nodebyname);
free(*s);
*s = NULL;
}