forked from tslarkin/Mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.m
executable file
·295 lines (264 loc) · 5.69 KB
/
Value.m
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
/*
* Value.c
* Hermes
*
* Created by Timothy Larkin on 11/24/06.
* Copyright 2006 Abstract Tools. All rights reserved.
*
*/
#import "Value.h"
#import <Foundation/Foundation.h>
#import "HMPort.h"
unsigned int columns(Value *val)
{
return val->length1;
}
static NSArray *typeNames = nil;
unsigned int rows(Value *val)
{
return val->length2;
}
NSString *stringValue(Value *val)
{
NSString *result;
switch (val->utype) {
case undefined:
result = @"undefined";
break;
case floattype:
result = [NSString stringWithFormat:@"%f", val->u.dval];
break;
case arraytype: {
NSMutableArray *array = [NSMutableArray array];
int i;
for (i = 0; i < val->length1; i++) {
NSString *x = [NSString stringWithFormat:@"%f", val->u.aval[i]];
[array addObject:x];
}
result = [NSString stringWithFormat:@"{%@}", [array componentsJoinedByString:@", "]];
break;
}
case lookuptype:
case matrixtype: {
NSMutableArray *rows = [NSMutableArray array];
int i, j;
for (j = 0; j < val->length2; j++) {
NSMutableArray *array = [NSMutableArray array];
for (i = 0; i < val->length1; i++) {
NSString *x = [NSString stringWithFormat:@"%f", val->u.mval[j][i]];
[array addObject:x];
}
[rows addObject: [NSString stringWithFormat:@"{%@}", [array componentsJoinedByString:@", "]]];
}
result= [NSString stringWithFormat:@"{%@}", [rows componentsJoinedByString:@", "]];
break;
}
case pathtype:
result = [NSString stringWithFormat:@"%s", val->u.sval];
break;
default: {
result = @"error";
break;
}
}
return result;
}
void freeValue(Value *val)
{
int i;
switch (val->utype) {
case undefined:
break;
case arraytype:
free(val->u.aval);
val->u.aval = nil;
break;
case lookuptype:
case matrixtype:
{
for (i = 0; i < val->length2; i++) {
free(val->u.mval[i]);
val->u.mval[i] = nil;
}
free(val->u.mval);
val->u.mval = nil;
break;
}
case pathtype:
free(val->u.sval);
val->u.sval = nil;
break;
}
val->length1 = val->length2 = 0;
val->u.dval = 0.0;
}
void checkDataType(Value *val, ValueType type)
{
HMPort *thePort = (HMPort*)val->port;
if (!typeNames) {
typeNames = [NSArray arrayWithObjects: @"undefined", @"error", @"float", @"array",
@"matrix", @"path", @"lookup", nil];
}
if (val->utype != type) {
HMPort *source;
source = [thePort finalSource];
[NSException raise:@"Variable type error"
format:@"The input %@ expected to be assigned a value "
"of type %@, but instead it received a value "
"of type %@ from %@",
[thePort fullPath],
[typeNames objectAtIndex:type],
[typeNames objectAtIndex:[source value]->utype],
[source fullPath]];
}
}
void setIntValue(Value *val, int d)
{
freeValue(val);
val->utype = floattype;
val->u.dval = d;
}
void setFloatValue(Value *val, float d)
{
freeValue(val);
val->utype = floattype;
val->u.dval = d;
}
void setZeroArrayValue(Value *val, int n)
{
freeValue(val);
val->length1 = n;
val->utype = arraytype;
val->u.aval = calloc(n, sizeof(float));
}
void setArrayValue(Value *val, float *d, int n)
{
freeValue(val);
val->length1 = n;
val->utype = arraytype;
val->u.aval = d;
}
void setZeroMatrixValue(Value *val, int n1, int n2)
{
freeValue(val);
val->length1 = n1;
val->length2 = n2;
val->utype = matrixtype;
float **m;
m = calloc(n2, sizeof(float*));
int i;
for (i = 0; i < n2; i++) {
m[i] = calloc(n1, sizeof(float));
}
val->u.mval = m;
}
void setMatrixValue(Value *val, float **d, int n1, int n2)
{
freeValue(val);
val->length1 = n1;
val->length2 = n2;
val->utype = matrixtype;
val->u.mval = d;
}
void setLookupValue(Value *val, float **d, int n1, int n2)
{
setMatrixValue(val, d, n1, n2);
}
char *pathValue(Value *val)
{
checkDataType(val, pathtype);
return val->u.sval;
}
void setPathValue(Value *val, char *s)
{
freeValue(val);
int n = strlen(s) + 1;
val->u.sval = (char*)malloc(n * sizeof(char));
val->utype = pathtype;
strncpy(val->u.sval, s, n);
}
void setUndefined(Value *val)
{
freeValue(val);
val->utype = undefined;
}
int intValue(Value *val)
{
int i = floatValue(val);
return i;
}
float floatValue(Value *val)
{
checkDataType(val, floattype);
return val->u.dval;
}
float *arrayValue(Value *val)
{
checkDataType(val, arraytype);
return val->u.aval;
}
float **matrixValue(Value *val)
{
checkDataType(val, matrixtype);
return val->u.mval;
}
float **lookupValue(Value *val)
{
ValueType type = val->utype;
if (type == matrixtype) {
checkDataType(val, matrixtype);
}
else {
checkDataType(val, lookuptype);
}
return val->u.mval;
}
void promoteFloat(Value *v, float x, int count)
{
setZeroArrayValue(v, count);int i;
for(i = 0; i < count; i++) {
v->u.aval[i] = x;
}
}
void promoteArray(Value *v, float *x, int columns, int rows)
{
setZeroMatrixValue(v, columns, rows);int i;
for (i = 0; i < rows; i++) {
memcpy(v->u.mval[i], x, columns * sizeof(float));
}
}
void copyValue(Value *target, Value *source)
{
switch (source->utype) {
case floattype:
setFloatValue(target, floatValue(source));
break;
case arraytype:
{
int n = source->length1;
float *z = malloc(n * sizeof(float));
memcpy(z, source->u.aval, n * sizeof(float));
setArrayValue(target, z, n);
}
break;
case lookuptype:
case matrixtype:
{
int n1 = source->length1, n2 = source->length2;
float **z = malloc(n2 * sizeof(float*));
int i;
for (i = 0; i < n2; i++) {
float *zi = malloc(n1 * sizeof(float));
memcpy(zi, source->u.mval[i], n1 * sizeof(float));
z[i] = zi;
}
setMatrixValue(target, z, n1, n2);
}
break;
case pathtype:
setPathValue(target, pathValue(source));
break;
default:
break;
}
}