This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
forked from matthiaskramm/mrscake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
constant.c
349 lines (330 loc) · 8.39 KB
/
constant.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* constant.c
Implementation of primitive datatypes.
Part of the data prediction package.
Copyright (c) 2010-2011 Matthias Kramm <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include "constant.h"
#include "stringpool.h"
char*type_name[] = {"undefined","float","category","int","bool","missing","deprecated array","string",
"int_array", "float_array", "category_array", "string_array", "mixed_array"};
array_t* array_new(int size)
{
array_t*array = (array_t*)calloc(1, sizeof(array_t)+sizeof(constant_t)*size);
array->size = size;
return array;
}
void array_fill(array_t*a, constant_t c)
{
int t;
for(t=0;t<a->size;t++) {
a->entries[t] = c;
}
}
array_t* array_create(int size, ...)
{
va_list arglist;
va_start(arglist, size);
int t;
array_t*array = array_new(size);
for(t=0;t<size;t++) {
array->entries[t] = category_constant(va_arg(arglist,category_t));
}
va_end(arglist);
return array;
}
void array_destroy(array_t*a)
{
free(a);
}
bool constant_equals(const constant_t*c1, const constant_t*c2)
{
if(c1->type != c2->type)
return false;
switch(c1->type) {
case CONSTANT_FLOAT:
return c1->f == c2->f;
case CONSTANT_INT:
return c1->i == c2->i;
case CONSTANT_CATEGORY:
return c1->c == c2->c;
case CONSTANT_BOOL:
return c1->b == c2->b;
case CONSTANT_STRING:
return !strcmp(c1->s, c2->s);
case CONSTANT_MISSING:
return true;
default:
/* FIXME */
//fprintf(stderr, "Can't compare types %s\n", type_name[c1->type]);
return false;
}
}
unsigned int constant_hash(const constant_t*o)
{
switch(o->type) {
case CONSTANT_FLOAT:
return hash_block(&o->f, sizeof(o->f));
case CONSTANT_INT:
return hash_block(&o->i, sizeof(o->i));
case CONSTANT_CATEGORY:
return hash_block(&o->c, sizeof(o->c));
case CONSTANT_BOOL:
return hash_block(&o->b, sizeof(o->b));
case CONSTANT_STRING:
return hash_block(&o->s, strlen(o->s));
case CONSTANT_MISSING:
return 0;
default:
return 0;
}
}
void* constant_dup(const void*o)
{
return (void*)o;
}
void constant_free(void*o)
{
return;
}
type_t constant_hash_type = {
equals: (equals_func)constant_equals,
hash: (hash_func)constant_hash,
dup: (dup_func)constant_dup,
free: (free_func)constant_free,
};
constant_type_t constant_array_subtype(constant_t*c)
{
switch(c->type) {
case CONSTANT_INT_ARRAY:
return CONSTANT_INT;
break;
case CONSTANT_FLOAT_ARRAY:
return CONSTANT_FLOAT;
break;
case CONSTANT_STRING_ARRAY:
return CONSTANT_STRING;
break;
case CONSTANT_CATEGORY_ARRAY:
return CONSTANT_CATEGORY;
break;
case CONSTANT_MIXED_ARRAY:
return CONSTANT_MISSING;
break;
default:
assert(!"can't determine array subtype of non-array");
}
}
constant_t bool_constant(bool b)
{
constant_t v;
v.type = CONSTANT_BOOL;
v.b = b;
return v;
}
constant_t float_constant(float f)
{
constant_t v;
v.type = CONSTANT_FLOAT;
v.f = f;
return v;
}
constant_t missing_constant()
{
constant_t v;
v.type = CONSTANT_MISSING;
return v;
}
constant_t category_constant(category_t c)
{
constant_t v;
v.type = CONSTANT_CATEGORY;
v.c = c;
return v;
}
constant_t int_constant(int i)
{
constant_t v;
v.type = CONSTANT_INT;
v.i = i;
return v;
}
bool array_is_homogeneous(array_t*a)
{
int t;
for(t=0;t<a->size;t++) {
if(a->entries[0].type != a->entries[t].type)
return false;
}
return true;
}
constant_t int_array_constant(array_t*a)
{
constant_t v;
v.type = CONSTANT_INT_ARRAY;
v.a = a;
return v;
}
constant_t float_array_constant(array_t*a)
{
constant_t v;
v.type = CONSTANT_FLOAT_ARRAY;
v.a = a;
return v;
}
constant_t string_array_constant(array_t*a)
{
constant_t v;
v.type = CONSTANT_STRING_ARRAY;
v.a = a;
return v;
}
constant_t category_array_constant(array_t*a)
{
constant_t v;
v.type = CONSTANT_CATEGORY_ARRAY;
v.a = a;
return v;
}
constant_t mixed_array_constant(array_t*a)
{
constant_t v;
v.type = CONSTANT_CATEGORY_ARRAY;
v.a = a;
return v;
}
constant_t string_constant(const char*s)
{
constant_t v;
v.type = CONSTANT_STRING;
v.s = register_string(s);
return v;
}
void constant_print(constant_t*v)
{
int t;
switch(v->type) {
case CONSTANT_FLOAT:
printf("%.2f", v->f);
break;
case CONSTANT_INT:
printf("%d", v->i);
break;
case CONSTANT_CATEGORY:
printf("C%d", v->c);
break;
case CONSTANT_BOOL:
printf("%s", v->b?"true":"false");
break;
case CONSTANT_STRING:
printf("\"%s\"", v->s);
break;
case CONSTANT_MISSING:
printf("<missing>");
break;
case CONSTANT_MIXED_ARRAY:
case CONSTANT_INT_ARRAY:
case CONSTANT_FLOAT_ARRAY:
case CONSTANT_CATEGORY_ARRAY:
case CONSTANT_STRING_ARRAY:
printf("[");
array_t*a = AS_MIXED_ARRAY(*v);
for(t=0;t<a->size;t++) {
if(t>0)
printf(",");
constant_print(&a->entries[t]);
}
printf("]");
break;
default:
printf("<bad value>");
break;
}
}
void constant_clear(constant_t*v)
{
switch(v->type) {
case CONSTANT_INT_ARRAY:
case CONSTANT_FLOAT_ARRAY:
case CONSTANT_CATEGORY_ARRAY:
case CONSTANT_MIXED_ARRAY:
case CONSTANT_STRING_ARRAY:
free(v->a);
v->a = 0;
break;
case CONSTANT_STRING:
/* FIXME */
//free(v->s);
v->s = 0;
break;
}
}
int constant_check_type(constant_t v, uint8_t type)
{
if(type == CONSTANT_MIXED_ARRAY) {
switch(v.type) {
case CONSTANT_INT_ARRAY:
case CONSTANT_FLOAT_ARRAY:
case CONSTANT_CATEGORY_ARRAY:
case CONSTANT_MIXED_ARRAY:
case CONSTANT_STRING_ARRAY:
return 1;
default:
fprintf(stderr, "expected array, got %s\n", type_name[v.type]);
}
}
if(v.type!=type) {
fprintf(stderr, "expected (%d) %s, got (%d) %s\n", type, type_name[type], v.type, type_name[v.type]);
*(int*)0=0;
return 0;
}
return 1;
}
constant_t variable_to_constant(variable_t*v)
{
switch(v->type) {
case CATEGORICAL:
return category_constant(v->category);
case CONTINUOUS:
return float_constant(v->value);
case TEXT:
return string_constant(v->text);
default:
fprintf(stderr, "invalid variable type %d\n", v->type);
assert(0);
}
}
variable_t constant_to_variable(constant_t* c)
{
switch(c->type) {
case CONSTANT_STRING:
return variable_new_text(c->s);
break;
case CONSTANT_CATEGORY:
return variable_new_categorical(c->c);
break;
case CONSTANT_FLOAT:
return variable_new_continuous(c->f);
break;
case CONSTANT_MISSING:
return variable_new_missing();
break;
default:
fprintf(stderr, "Can't convert constant type %d to variable\n", c->type);
break;
}
}