expression.c 34.5 KB
Newer Older
1
#include "cconfigspace_internal.h"
Brice Videau's avatar
Brice Videau committed
2
#include "expression_internal.h"
3
#include <math.h>
4
#include <string.h>
5
#include "utarray.h"
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

const int ccs_expression_precedence[] = {
	0,
	1,
	2, 2,
	3, 3, 3, 3,
	4,
	5, 5,
	6, 6, 6,
	7, 7, 7,
	8
};

const char *ccs_expression_symbols[] = {
	"||",
	"&&",
	"==", "!=",
	"<", ">", "<=", ">=",
	"#",
	"+", "-",
	"*", "/", "%",
	"+", "-", "!",
	NULL
};

const int ccs_expression_arity[] = {
	2,
	2,
	2, 2,
	2, 2, 2, 2,
	2,
	2, 2,
	2, 2, 2,
	1, 1, 1,
	-1
};

static inline _ccs_expression_ops_t *
ccs_expression_get_ops(ccs_expression_t expression) {
	return (_ccs_expression_ops_t *)expression->obj.ops;
}

static ccs_error_t
_ccs_expression_del(ccs_object_t o) {
	ccs_expression_t d = (ccs_expression_t)o;
	_ccs_expression_data_t *data = d->data;
	for (size_t i = 0; i < data->num_nodes; i++)
		if (data->nodes[i].type == CCS_OBJECT)
			ccs_release_object(data->nodes[i].value.o);
	return CCS_SUCCESS;
}


static inline ccs_error_t
_ccs_expr_datum_eval(ccs_datum_t               *d,
                     ccs_configuration_space_t  context,
                     ccs_datum_t               *values,
Brice Videau's avatar
Brice Videau committed
63
64
                     ccs_datum_t               *result,
                     ccs_hyperparameter_type_t *ht) {
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	ccs_object_type_t t;
	size_t index;
	ccs_error_t err;
	switch (d->type) {
	case CCS_NONE:
	case CCS_INTEGER:
	case CCS_FLOAT:
	case CCS_BOOLEAN:
	case CCS_STRING:
		*result = *d;
		break;
	case CCS_OBJECT:
		err = ccs_object_get_type(d->value.o, &t);
		if (err)
			return err;
		switch (t) {
		case CCS_EXPRESSION:
Brice Videau's avatar
Brice Videau committed
82
			return ccs_expression_eval((ccs_expression_t)(d->value.o),
83
84
85
86
87
88
89
90
			                           context, values, result);
			break;
		case CCS_HYPERPARAMETER:
			err = ccs_configuration_space_get_hyperparameter_index(
				context, (ccs_hyperparameter_t)(d->value.o), &index);
			if (err)
				return err;
			*result = values[index];
Brice Videau's avatar
Brice Videau committed
91
92
93
94
95
96
			if (ht) {
				err = ccs_hyperparameter_get_type(
				          (ccs_hyperparameter_t)(d->value.o), ht);
				if (err)
					return err;
			}
97
98
			break;
		default:
99
			return -CCS_INVALID_OBJECT;
100
101
102
		}
		break;
	default:
103
		return -CCS_INVALID_VALUE;
104
105
106
107
	}
	return CCS_SUCCESS;
}

108
109
110
111
112
113
114
#define eval_node(data, context, values, node, ht) do { \
	ccs_error_t err; \
	err = _ccs_expr_datum_eval(data->nodes, context, values, &node, ht); \
	if (err) \
		return err; \
} while(0)

Brice Videau's avatar
Brice Videau committed
115
#define eval_left_right(data, context, values, left, right, htl, htr) do { \
116
	ccs_error_t err; \
Brice Videau's avatar
Brice Videau committed
117
	err = _ccs_expr_datum_eval(data->nodes, context, values, &left, htl); \
118
119
	if (err) \
		return err; \
Brice Videau's avatar
Brice Videau committed
120
	err = _ccs_expr_datum_eval(data->nodes + 1, context, values, &right, htr); \
121
122
	if (err) \
		return err; \
Brice Videau's avatar
Brice Videau committed
123
} while (0)
124
125
126
127
128
129
130
131

static ccs_error_t
_ccs_expr_or_eval(_ccs_expression_data_t *data,
                  ccs_configuration_space_t  context,
                  ccs_datum_t               *values,
                  ccs_datum_t               *result) {
	ccs_datum_t left;
	ccs_datum_t right;
Brice Videau's avatar
Brice Videau committed
132
	eval_left_right(data, context, values, left, right, NULL, NULL);
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
	if (left.type != CCS_BOOLEAN || right.type != CCS_BOOLEAN)
		return -CCS_INVALID_VALUE;
	result->type = CCS_BOOLEAN;
	result->value.i = (left.value.i || right.value.i) ? CCS_TRUE : CCS_FALSE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_or_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_or_eval
};

static ccs_error_t
_ccs_expr_and_eval(_ccs_expression_data_t *data,
                  ccs_configuration_space_t  context,
                  ccs_datum_t               *values,
                  ccs_datum_t               *result) {
	ccs_datum_t left;
	ccs_datum_t right;
Brice Videau's avatar
Brice Videau committed
152
	eval_left_right(data, context, values, left, right, NULL, NULL);
153
154
155
156
157
158
159
160
161
162
163
164
	if (left.type != CCS_BOOLEAN || right.type != CCS_BOOLEAN)
		return -CCS_INVALID_VALUE;
	result->type = CCS_BOOLEAN;
	result->value.i = (left.value.i && right.value.i) ? CCS_TRUE : CCS_FALSE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_and_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_and_eval
};

Brice Videau's avatar
Brice Videau committed
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
#define check_values(param, v) do { \
	ccs_bool_t valid; \
	ccs_error_t err; \
	err = ccs_hyperparameter_check_value( \
		(ccs_hyperparameter_t)(param), v, &valid); \
	if (unlikely(err)) \
		return err; \
	if (!valid) \
		return -CCS_INVALID_VALUE; \
} while(0)

#define check_hypers(param, v, t) do { \
	if (t == CCS_ORDINAL || t == CCS_CATEGORICAL) { \
		check_values(param.value.o, v); \
	} else if (t == CCS_NUMERICAL) {\
		if (v.type != CCS_INTEGER && v.type != CCS_FLOAT) \
			return -CCS_INVALID_VALUE; \
	} \
} while(0)

static inline ccs_int_t
_ccs_string_cmp(const char *a, const char *b) {
	if (a == b)
		return 0;
	if (!a)
		return -1;
	if (!b)
		return 1;
	return strcmp(a, b);
}

static inline ccs_error_t
_ccs_datum_test_equal_generic(ccs_datum_t *a, ccs_datum_t *b, ccs_bool_t *equal) {
	if (a->type == b->type) {
		switch(a->type) {
		case CCS_STRING:
			*equal = _ccs_string_cmp(a->value.s, b->value.s) == 0 ? CCS_TRUE : CCS_FALSE;
			break;
		case CCS_NONE:
			*equal = CCS_TRUE;
			break;
		default:
			*equal = memcmp(&(a->value), &(b->value), sizeof(ccs_value_t)) == 0 ? CCS_TRUE : CCS_FALSE;
		}
	} else {
		if (a->type == CCS_INTEGER && b->type == CCS_FLOAT) {
			*equal = (a->value.i == b->value.f) ? CCS_TRUE : CCS_FALSE;
		} else if (a->type == CCS_FLOAT && b->type == CCS_INTEGER) {
			*equal = (a->value.f == b->value.i) ? CCS_TRUE : CCS_FALSE;
		} else {
			*equal = CCS_FALSE;
			return -CCS_INVALID_VALUE;
		}
	}
	return CCS_SUCCESS;
}

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
static inline ccs_error_t
_ccs_datum_cmp_generic(ccs_datum_t *a, ccs_datum_t *b, ccs_int_t *cmp) {
	if (a->type == b->type) {
		switch(a->type) {
		case CCS_STRING:
			*cmp = _ccs_string_cmp(a->value.s, b->value.s);
			break;
		case CCS_INTEGER:
			*cmp = a->value.i < b->value.i ? -1 :
			       a->value.i > b->value.i ?  1 : 0;
			break;
		case CCS_FLOAT:
			*cmp = a->value.f < b->value.f ? -1 :
			       a->value.f > b->value.f ?  1 : 0;
			break;
		default:
			return -CCS_INVALID_VALUE;
		}
	} else {
		if (a->type == CCS_INTEGER && b->type == CCS_FLOAT) {
			*cmp = a->value.i < b->value.f ? -1 :
			       a->value.i > b->value.f ?  1 : 0;
		} else if (a->type == CCS_FLOAT && b->type == CCS_INTEGER) {
			*cmp = a->value.f < b->value.i ? -1 :
			       a->value.f > b->value.i ?  1 : 0;
		} else {
			return -CCS_INVALID_VALUE;
		}
	}
	return CCS_SUCCESS;
}

Brice Videau's avatar
Brice Videau committed
254
255
static ccs_error_t
_ccs_expr_equal_eval(_ccs_expression_data_t *data,
256
257
258
                     ccs_configuration_space_t  context,
                     ccs_datum_t               *values,
                     ccs_datum_t               *result) {
Brice Videau's avatar
Brice Videau committed
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
	ccs_datum_t               left;
	ccs_datum_t               right;
	ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
	ccs_hyperparameter_type_t htr = CCS_HYPERPARAMETER_TYPE_MAX;

	eval_left_right(data, context, values, left, right, &htl, &htr);
	check_hypers(data->nodes[0], right, htl);
	check_hypers(data->nodes[1], left, htr);
	ccs_bool_t equal;
	ccs_error_t err = _ccs_datum_test_equal_generic(&left, &right, &equal);
	if(htl != CCS_HYPERPARAMETER_TYPE_MAX || htr != CCS_HYPERPARAMETER_TYPE_MAX) {
		result->value.i = equal;
	} else {
		if (err)
			return err;
		result->value.i = equal;
	}
	result->type = CCS_BOOLEAN;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_equal_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_equal_eval
};

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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
static ccs_error_t
_ccs_expr_not_equal_eval(_ccs_expression_data_t    *data,
                         ccs_configuration_space_t  context,
                         ccs_datum_t               *values,
                         ccs_datum_t               *result) {
	ccs_datum_t               left;
	ccs_datum_t               right;
	ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
	ccs_hyperparameter_type_t htr = CCS_HYPERPARAMETER_TYPE_MAX;

	eval_left_right(data, context, values, left, right, &htl, &htr);
	check_hypers(data->nodes[0], right, htl);
	check_hypers(data->nodes[1], left, htr);
	ccs_bool_t equal;
	ccs_error_t err = _ccs_datum_test_equal_generic(&left, &right, &equal);
	if(htl != CCS_HYPERPARAMETER_TYPE_MAX || htr != CCS_HYPERPARAMETER_TYPE_MAX) {
		result->value.i = equal ? CCS_FALSE : CCS_TRUE;
	} else {
		if (err)
			return err;
		result->value.i = equal ? CCS_FALSE : CCS_TRUE;
	}
	result->type = CCS_BOOLEAN;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_not_equal_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_not_equal_eval
};

static ccs_error_t
_ccs_expr_less_eval(_ccs_expression_data_t *data,
                    ccs_configuration_space_t  context,
                    ccs_datum_t               *values,
                    ccs_datum_t               *result) {
	ccs_datum_t               left;
	ccs_datum_t               right;
	ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
	ccs_hyperparameter_type_t htr = CCS_HYPERPARAMETER_TYPE_MAX;

	eval_left_right(data, context, values, left, right, &htl, &htr);
	if (htl == CCS_CATEGORICAL || htr == CCS_CATEGORICAL)
		return -CCS_INVALID_VALUE;
	check_hypers(data->nodes[0], right, htl);
	check_hypers(data->nodes[1], left, htr);
	ccs_error_t err;
	if (htl == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[0].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp < 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	if (htr == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[1].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp < 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	ccs_int_t cmp;
	err = _ccs_datum_cmp_generic(&left, &right, &cmp);
	if (err)
		return err;
	result->type = CCS_BOOLEAN;
	result->value.i = cmp < 0 ? CCS_TRUE : CCS_FALSE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_less_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_less_eval
};

static ccs_error_t
_ccs_expr_greater_eval(_ccs_expression_data_t *data,
                       ccs_configuration_space_t  context,
                       ccs_datum_t               *values,
                       ccs_datum_t               *result) {
	ccs_datum_t               left;
	ccs_datum_t               right;
	ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
	ccs_hyperparameter_type_t htr = CCS_HYPERPARAMETER_TYPE_MAX;

	eval_left_right(data, context, values, left, right, &htl, &htr);
	if (htl == CCS_CATEGORICAL || htr == CCS_CATEGORICAL)
		return -CCS_INVALID_VALUE;
	check_hypers(data->nodes[0], right, htl);
	check_hypers(data->nodes[1], left, htr);
	ccs_error_t err;
	if (htl == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[0].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp > 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	if (htr == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[1].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp > 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	ccs_int_t cmp;
	err = _ccs_datum_cmp_generic(&left, &right, &cmp);
	if (err)
		return err;
	result->type = CCS_BOOLEAN;
	result->value.i = cmp > 0 ? CCS_TRUE : CCS_FALSE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_greater_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_greater_eval
};

static ccs_error_t
_ccs_expr_less_or_equal_eval(_ccs_expression_data_t *data,
                             ccs_configuration_space_t  context,
                             ccs_datum_t               *values,
                             ccs_datum_t               *result) {
	ccs_datum_t               left;
	ccs_datum_t               right;
	ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
	ccs_hyperparameter_type_t htr = CCS_HYPERPARAMETER_TYPE_MAX;

	eval_left_right(data, context, values, left, right, &htl, &htr);
	if (htl == CCS_CATEGORICAL || htr == CCS_CATEGORICAL)
		return -CCS_INVALID_VALUE;
	check_hypers(data->nodes[0], right, htl);
	check_hypers(data->nodes[1], left, htr);
	ccs_error_t err;
	if (htl == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[0].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp <= 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	if (htr == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[1].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp <= 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	ccs_int_t cmp;
	err = _ccs_datum_cmp_generic(&left, &right, &cmp);
	if (err)
		return err;
	result->type = CCS_BOOLEAN;
	result->value.i = cmp <= 0 ? CCS_TRUE : CCS_FALSE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_less_or_equal_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_less_or_equal_eval
};

static ccs_error_t
_ccs_expr_greater_or_equal_eval(_ccs_expression_data_t *data,
                                ccs_configuration_space_t  context,
                                ccs_datum_t               *values,
                                ccs_datum_t               *result) {
	ccs_datum_t               left;
	ccs_datum_t               right;
	ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
	ccs_hyperparameter_type_t htr = CCS_HYPERPARAMETER_TYPE_MAX;

	eval_left_right(data, context, values, left, right, &htl, &htr);
	if (htl == CCS_CATEGORICAL || htr == CCS_CATEGORICAL)
		return -CCS_INVALID_VALUE;
	check_hypers(data->nodes[0], right, htl);
	check_hypers(data->nodes[1], left, htr);
	ccs_error_t err;
	if (htl == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[0].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp >= 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	if (htr == CCS_ORDINAL) {
		ccs_int_t cmp;
		err = ccs_ordinal_hyperparameter_compare_values(
			(ccs_hyperparameter_t)(data->nodes[1].value.o),
			left, right, &cmp);
		if (err)
			return err;
		result->type = CCS_BOOLEAN;
		result->value.i = (cmp >= 0 ? CCS_TRUE : CCS_FALSE);
		return CCS_SUCCESS;
	}
	ccs_int_t cmp;
	err = _ccs_datum_cmp_generic(&left, &right, &cmp);
	if (err)
		return err;
	result->type = CCS_BOOLEAN;
	result->value.i = cmp >= 0 ? CCS_TRUE : CCS_FALSE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_greater_or_equal_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_greater_or_equal_eval
};

static ccs_error_t
_ccs_expr_in_eval(_ccs_expression_data_t *data,
                  ccs_configuration_space_t  context,
                  ccs_datum_t               *values,
                  ccs_datum_t               *result) {
	if (data->nodes[1].type != CCS_OBJECT)
		return -CCS_INVALID_VALUE;
	ccs_object_type_t type;
	ccs_error_t err;
	err = ccs_object_get_type(data->nodes[1].value.o, &type);
	if (err)
		return err;
	if (type != CCS_EXPRESSION)
		return -CCS_INVALID_VALUE;
	ccs_expression_type_t etype;
	err = ccs_expression_get_type((ccs_expression_t)(data->nodes[1].value.o), &etype);
	if (err)
		return err;
	if (etype != CCS_LIST)
		return -CCS_INVALID_VALUE;
	size_t num_nodes;
	err = ccs_expression_get_num_nodes((ccs_expression_t)(data->nodes[1].value.o), &num_nodes);
	if (err)
		return err;
	if (num_nodes == 0) {
		result->type = CCS_BOOLEAN;
		result->value.i = CCS_FALSE;
		return CCS_SUCCESS;
	}

	ccs_datum_t               left;
	ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
	eval_node(data, context, values, left, &htl);
	for (size_t i = 0; i < num_nodes; i++) {
		ccs_datum_t right;
		err = ccs_expression_list_eval_node((ccs_expression_t)(data->nodes[1].value.o), context, values, i, &right);
Brice Videau's avatar
Brice Videau committed
560
561
		if (err)
			return err;
562
563
564
		check_hypers(data->nodes[0], right, htl);
		ccs_bool_t equal;
		ccs_error_t err = _ccs_datum_test_equal_generic(&left, &right, &equal);
Brice Videau's avatar
Brice Videau committed
565
566
		if (err) {
			if (err != -CCS_INVALID_VALUE)
567
				return err;
Brice Videau's avatar
Brice Videau committed
568
569
570
571
572
573
574
			else
				continue;
		}
		if (equal) {
			result->type = CCS_BOOLEAN;
			result->value.i = CCS_TRUE;
			return CCS_SUCCESS;
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
		}
	}
	result->type = CCS_BOOLEAN;
	result->value.i = CCS_FALSE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_in_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_in_eval
};

static ccs_error_t
_ccs_expr_add_eval(_ccs_expression_data_t *data,
                   ccs_configuration_space_t  context,
                   ccs_datum_t               *values,
                   ccs_datum_t               *result) {
	ccs_datum_t left;
	ccs_datum_t right;
	eval_left_right(data, context, values, left, right, NULL, NULL);
	if (left.type == CCS_INTEGER) {
		if (right.type == CCS_INTEGER) {
			result->type = CCS_INTEGER;
			result->value.i = left.value.i + right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.i + right.value.f;
			return CCS_SUCCESS;
		}
		return -CCS_INVALID_VALUE;
	}
	if (left.type == CCS_FLOAT) {
		if (right.type == CCS_INTEGER) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.f + right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.f + right.value.f;
			return CCS_SUCCESS;
		}
	}
	return -CCS_INVALID_VALUE;
}

static _ccs_expression_ops_t _ccs_expr_add_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_add_eval
};

static ccs_error_t
_ccs_expr_substract_eval(_ccs_expression_data_t *data,
                         ccs_configuration_space_t  context,
                         ccs_datum_t               *values,
                         ccs_datum_t               *result) {
	ccs_datum_t left;
	ccs_datum_t right;
	eval_left_right(data, context, values, left, right, NULL, NULL);
	if (left.type == CCS_INTEGER) {
		if (right.type == CCS_INTEGER) {
			result->type = CCS_INTEGER;
			result->value.i = left.value.i - right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.i - right.value.f;
			return CCS_SUCCESS;
		}
		return -CCS_INVALID_VALUE;
	}
	if (left.type == CCS_FLOAT) {
		if (right.type == CCS_INTEGER) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.f - right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.f - right.value.f;
			return CCS_SUCCESS;
		}
	}
	return -CCS_INVALID_VALUE;
}

static _ccs_expression_ops_t _ccs_expr_substract_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_substract_eval
};

static ccs_error_t
_ccs_expr_multiply_eval(_ccs_expression_data_t    *data,
                        ccs_configuration_space_t  context,
                        ccs_datum_t               *values,
                        ccs_datum_t               *result) {
	ccs_datum_t left;
	ccs_datum_t right;
	eval_left_right(data, context, values, left, right, NULL, NULL);
	if (left.type == CCS_INTEGER) {
		if (right.type == CCS_INTEGER) {
			result->type = CCS_INTEGER;
			result->value.i = left.value.i * right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.i * right.value.f;
			return CCS_SUCCESS;
		}
		return -CCS_INVALID_VALUE;
	}
	if (left.type == CCS_FLOAT) {
		if (right.type == CCS_INTEGER) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.f * right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			result->type = CCS_FLOAT;
			result->value.f = left.value.f * right.value.f;
			return CCS_SUCCESS;
		}
	}
	return -CCS_INVALID_VALUE;
}

static _ccs_expression_ops_t _ccs_expr_multiply_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_multiply_eval
};

static ccs_error_t
_ccs_expr_divide_eval(_ccs_expression_data_t    *data,
                      ccs_configuration_space_t  context,
                      ccs_datum_t               *values,
                      ccs_datum_t               *result) {
	ccs_datum_t left;
	ccs_datum_t right;
	eval_left_right(data, context, values, left, right, NULL, NULL);
	if (left.type == CCS_INTEGER) {
		if (right.type == CCS_INTEGER) {
			if (right.value.i == 0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_INTEGER;
			result->value.i = left.value.i / right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			if (right.value.f == 0.0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_FLOAT;
			result->value.f = left.value.i / right.value.f;
			return CCS_SUCCESS;
		}
		return -CCS_INVALID_VALUE;
	}
	if (left.type == CCS_FLOAT) {
		if (right.type == CCS_INTEGER) {
			if (right.value.i == 0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_FLOAT;
			result->value.f = left.value.f / right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			if (right.value.f == 0.0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_FLOAT;
			result->value.f = left.value.f / right.value.f;
			return CCS_SUCCESS;
		}
	}
	return -CCS_INVALID_VALUE;
}

static _ccs_expression_ops_t _ccs_expr_divide_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_divide_eval
};

static ccs_error_t
_ccs_expr_modulo_eval(_ccs_expression_data_t    *data,
                      ccs_configuration_space_t  context,
                      ccs_datum_t               *values,
                      ccs_datum_t               *result) {
	ccs_datum_t left;
	ccs_datum_t right;
	eval_left_right(data, context, values, left, right, NULL, NULL);
	if (left.type == CCS_INTEGER) {
		if (right.type == CCS_INTEGER) {
			if (right.value.i == 0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_INTEGER;
			result->value.i = left.value.i % right.value.i;
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			if (right.value.f == 0.0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_FLOAT;
			result->value.f = fmod(left.value.i, right.value.f);
			return CCS_SUCCESS;
		}
		return -CCS_INVALID_VALUE;
	}
	if (left.type == CCS_FLOAT) {
		if (right.type == CCS_INTEGER) {
			if (right.value.i == 0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_FLOAT;
			result->value.f = fmod(left.value.f, right.value.i);
			return CCS_SUCCESS;
		}
		if (right.type == CCS_FLOAT) {
			if (right.value.f == 0.0)
				return -CCS_INVALID_VALUE;
			result->type = CCS_FLOAT;
			result->value.f = fmod(left.value.f, right.value.f);
			return CCS_SUCCESS;
		}
	}
	return -CCS_INVALID_VALUE;
}

static _ccs_expression_ops_t _ccs_expr_modulo_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_modulo_eval
};

static ccs_error_t
_ccs_expr_positive_eval(_ccs_expression_data_t    *data,
                        ccs_configuration_space_t  context,
                        ccs_datum_t               *values,
                        ccs_datum_t               *result) {
	ccs_datum_t node;
	eval_node(data, context, values, node, NULL);
	if (node.type != CCS_INTEGER && node.type != CCS_FLOAT) {
		return -CCS_INVALID_VALUE;
	}
	*result = node;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_positive_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_positive_eval
};

static ccs_error_t
_ccs_expr_negative_eval(_ccs_expression_data_t    *data,
                        ccs_configuration_space_t  context,
                        ccs_datum_t               *values,
                        ccs_datum_t               *result) {
	ccs_datum_t node;
	eval_node(data, context, values, node, NULL);
	if (node.type == CCS_INTEGER) {
		result->type = node.type;
		result->value.i = - node.value.i;
	} else if (node.type == CCS_FLOAT) {
		result->type = node.type;
		result->value.f = - node.value.f;
	} else
		return -CCS_INVALID_VALUE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_negative_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_negative_eval
};

static ccs_error_t
_ccs_expr_not_eval(_ccs_expression_data_t    *data,
                   ccs_configuration_space_t  context,
                   ccs_datum_t               *values,
                   ccs_datum_t               *result) {
	ccs_datum_t node;
	eval_node(data, context, values, node, NULL);
	if (node.type == CCS_BOOLEAN) {
		result->type = node.type;
		result->value.i = node.value.i ? CCS_FALSE : CCS_TRUE;
	} else
		return -CCS_INVALID_VALUE;
	return CCS_SUCCESS;
}

static _ccs_expression_ops_t _ccs_expr_not_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_not_eval
};

static ccs_error_t
_ccs_expr_list_eval(_ccs_expression_data_t    *data,
                   ccs_configuration_space_t  context,
                   ccs_datum_t               *values,
                   ccs_datum_t               *result) {
	return -CCS_UNSUPPORTED_OPERATION;
}

static _ccs_expression_ops_t _ccs_expr_list_ops = {
	{ &_ccs_expression_del },
	&_ccs_expr_list_eval
};

883
884
885
886
887
888
889
890
891
static inline _ccs_expression_ops_t *
_ccs_expression_ops_broker(ccs_expression_type_t  expression_type) {
	switch (expression_type) {
	case CCS_OR:
		return &_ccs_expr_or_ops;
		break;
	case CCS_AND:
		return &_ccs_expr_and_ops;
		break;
Brice Videau's avatar
Brice Videau committed
892
893
894
	case CCS_EQUAL:
		return &_ccs_expr_equal_ops;
		break;
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
	case CCS_NOT_EQUAL:
		return &_ccs_expr_not_equal_ops;
		break;
	case CCS_LESS:
		return &_ccs_expr_less_ops;
		break;
	case CCS_GREATER:
		return &_ccs_expr_greater_ops;
		break;
	case CCS_LESS_OR_EQUAL:
		return &_ccs_expr_less_or_equal_ops;
		break;
	case CCS_GREATER_OR_EQUAL:
		return &_ccs_expr_greater_or_equal_ops;
		break;
	case CCS_IN:
		return &_ccs_expr_in_ops;
		break;
	case CCS_ADD:
		return &_ccs_expr_add_ops;
		break;
	case CCS_SUBSTRACT:
		return &_ccs_expr_substract_ops;
		break;
	case CCS_MULTIPLY:
		return &_ccs_expr_multiply_ops;
		break;
	case CCS_DIVIDE:
		return &_ccs_expr_divide_ops;
		break;
	case CCS_MODULO:
		return &_ccs_expr_modulo_ops;
		break;
	case CCS_POSITIVE:
		return &_ccs_expr_positive_ops;
		break;
	case CCS_NEGATIVE:
		return &_ccs_expr_negative_ops;
		break;
	case CCS_NOT:
		return &_ccs_expr_not_ops;
		break;
	case CCS_LIST:
		return &_ccs_expr_list_ops;
		break;
940
941
942
943
944
945
	default:
		return NULL;
	}
}

ccs_error_t
Brice Videau's avatar
Brice Videau committed
946
ccs_create_expression(ccs_expression_type_t  type,
Brice Videau's avatar
Brice Videau committed
947
948
949
	              size_t                 num_nodes,
                      ccs_datum_t           *nodes,
                      ccs_expression_t      *expression_ret) {
950
951
952
953
	if (num_nodes && !nodes)
		return -CCS_INVALID_VALUE;
	if (!expression_ret)
		return -CCS_INVALID_VALUE;
Brice Videau's avatar
Brice Videau committed
954
	int arity = ccs_expression_arity[type];
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
	if (arity >= 0 && num_nodes != (size_t)arity)
		return -CCS_INVALID_VALUE;

	ccs_error_t err;
	size_t size_strs = 0;
	for(size_t i = 0; i < num_nodes; i++)
		if (nodes[i].type == CCS_STRING) {
			if (!nodes[i].value.s)
				return -CCS_INVALID_VALUE;
			size_strs += strlen(nodes[i].value.s) + 1;
		}

	uintptr_t mem = (uintptr_t)calloc(1,
	    sizeof(struct _ccs_expression_s) +
	    sizeof(struct _ccs_expression_data_s) +
	    num_nodes*sizeof(ccs_datum_t) +
	    size_strs);
	if (!mem)
		return -CCS_ENOMEM;

	for(size_t i = 0; i < num_nodes; i++)
		if (nodes[i].type == CCS_OBJECT) {
			err = ccs_retain_object(nodes[i].value.o);
			if (err) {
				free((void *)mem);
				return err;
			}
		}

	ccs_expression_t expression = (ccs_expression_t)mem;
	_ccs_object_init(&(expression->obj), CCS_EXPRESSION,
Brice Videau's avatar
Brice Videau committed
986
	                 (_ccs_object_ops_t*)_ccs_expression_ops_broker(type));
987
988
	_ccs_expression_data_t *expression_data =
	    (_ccs_expression_data_t *)(mem + sizeof(struct _ccs_expression_s));
Brice Videau's avatar
Brice Videau committed
989
	expression_data->type = type;
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
	expression_data->num_nodes = num_nodes;
	expression_data->nodes = (ccs_datum_t *)(mem +
	    sizeof(struct _ccs_expression_s) +
	    sizeof(struct _ccs_expression_data_s));
	char *str_pool = (char *)(mem +
	    sizeof(struct _ccs_expression_s) +
	    sizeof(struct _ccs_expression_data_s) +
	    num_nodes*sizeof(ccs_datum_t));
	for (size_t i = 0; i < num_nodes; i++) {
		if (nodes[i].type == CCS_STRING) {
			expression_data->nodes[i].type = CCS_STRING;
			expression_data->nodes[i].value.s = str_pool;
			strcpy(str_pool, nodes[i].value.s);
			str_pool += strlen(nodes[i].value.s) + 1;
		} else {
			expression_data->nodes[i] = nodes[i];
		}
	}
	expression->data = expression_data;
	*expression_ret = expression;
	return CCS_SUCCESS;
}

Brice Videau's avatar
Brice Videau committed
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
ccs_error_t
ccs_create_binary_expression(ccs_expression_type_t  type,
                             ccs_datum_t            node_left,
                             ccs_datum_t            node_right,
                             ccs_expression_t      *expression_ret) {
	ccs_datum_t nodes[2];
	nodes[0] = node_left;
	nodes[1] = node_right;
	return ccs_create_expression(type, 2, nodes, expression_ret);
}

ccs_error_t
ccs_create_unary_expression(ccs_expression_type_t  type,
                            ccs_datum_t            node,
                            ccs_expression_t      *expression_ret) {
	return ccs_create_expression(type, 1, &node, expression_ret);
}

1031
ccs_error_t
Brice Videau's avatar
Brice Videau committed
1032
ccs_expression_eval(ccs_expression_t           expression,
1033
1034
1035
1036
                    ccs_configuration_space_t  context,
                    ccs_datum_t               *values,
                    ccs_datum_t               *result) {
	if (!expression || !expression->data)
1037
		return -CCS_INVALID_OBJECT;
1038
	if (!result)
1039
		return -CCS_INVALID_VALUE;
1040
1041
1042
	_ccs_expression_ops_t *ops = ccs_expression_get_ops(expression);
	return ops->eval(expression->data, context, values, result);
}
1043
1044
1045
1046
1047

ccs_error_t
ccs_expression_get_num_nodes(ccs_expression_t  expression,
                             size_t           *num_nodes_ret) {
	if (!expression || !expression->data)
1048
		return -CCS_INVALID_OBJECT;
1049
	if (!num_nodes_ret)
1050
		return -CCS_INVALID_VALUE;
1051
1052
1053
1054
	*num_nodes_ret = expression->data->num_nodes;
	return CCS_SUCCESS;
}

1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
ccs_error_t
ccs_expression_get_nodes(ccs_expression_t  expression,
                         size_t            num_nodes,
                         ccs_datum_t      *nodes,
                         size_t           *num_nodes_ret) {
	if (!expression || !expression->data)
		return -CCS_INVALID_OBJECT;
	if (num_nodes && !nodes)
		return -CCS_INVALID_VALUE;
	if (!num_nodes_ret && !nodes)
		return -CCS_INVALID_VALUE;
	size_t count = expression->data->num_nodes;
	if (nodes) {
		ccs_datum_t *p_nodes = expression->data->nodes;
		if (num_nodes < count)
			return -CCS_INVALID_VALUE;
		for (size_t i = 0; i < count; i++)
			nodes[i] = p_nodes[i];
		for (size_t i = count; i < num_nodes; i++)
			nodes[i] = ccs_none;
	}
	if (num_nodes_ret)
		*num_nodes_ret = count;
	return CCS_SUCCESS;
}

1081
1082
1083
1084
1085
1086
1087
ccs_error_t
ccs_expression_list_eval_node(ccs_expression_t           expression,
                              ccs_configuration_space_t  context,
                              ccs_datum_t               *values,
                              size_t                     index,
                              ccs_datum_t               *result) {
	if (!expression || !expression->data)
1088
		return -CCS_INVALID_OBJECT;
1089
	if (!result)
1090
		return -CCS_INVALID_VALUE;
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
	ccs_error_t err;
	ccs_datum_t node;
	if (index >= expression->data->num_nodes)
		return -CCS_OUT_OF_BOUNDS;
	err = _ccs_expr_datum_eval(expression->data->nodes + index, context, values, &node, NULL);
	if (err)
		return err;
	*result = node;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_expression_get_type(ccs_expression_t       expression,
                        ccs_expression_type_t *type_ret) {
	if (!expression || !expression->data)
1106
		return -CCS_INVALID_OBJECT;
1107
1108
1109
1110
	*type_ret = expression->data->type;
	return CCS_SUCCESS;
}

1111
1112
1113
1114
1115
1116
1117
1118
#undef  utarray_oom
#define utarray_oom() { \
	return -CCS_ENOMEM; \
}

static ccs_error_t _get_hyperparameters(ccs_expression_t       expression,
                                        UT_array              *array) {
	if (!expression || !expression->data)
1119
		return -CCS_INVALID_OBJECT;
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
	ccs_error_t err;
	for (size_t i = 0; i < expression->data->num_nodes; i++) {
		ccs_datum_t *d = expression->data->nodes + i;
		if (d->type == CCS_OBJECT) {
			ccs_object_type_t t;
			err = ccs_object_get_type(d->value.o, &t);
			if (err)
				return err;
			if (t == CCS_HYPERPARAMETER)
				utarray_push_back(array, &(d->value.o));
			else if (t == CCS_EXPRESSION) {
				err = _get_hyperparameters((ccs_expression_t)(d->value.o), array);
				if (err)
					return err;
			}
		}
	}
	return CCS_SUCCESS;
}

static const UT_icd _hyperparameter_icd = {
	sizeof(ccs_hyperparameter_t),
	NULL,
	NULL,
	NULL,
};

static int hyper_sort(const void *a, const void *b) {
	ccs_hyperparameter_t ha = *(ccs_hyperparameter_t *)a;
	ccs_hyperparameter_t hb = *(ccs_hyperparameter_t *)b;
	return ha < hb ? -1 : ha > hb ? 1 : 0;
}

ccs_error_t
ccs_expression_get_hyperparameters(ccs_expression_t      expression,
                                   size_t                num_hyperparameters,
                                   ccs_hyperparameter_t *hyperparameters,
                                   size_t               *num_hyperparameters_ret) {
	if (num_hyperparameters && !hyperparameters)
		return -CCS_INVALID_VALUE;
	if (!hyperparameters && !num_hyperparameters_ret)
		return -CCS_INVALID_VALUE;
	ccs_error_t err;
	UT_array *array;
	utarray_new(array, &_hyperparameter_icd);
	err = _get_hyperparameters(expression, array);
	if (err) {
		utarray_free(array);
		return err;
	}
	utarray_sort(array, &hyper_sort);
	size_t count = 0;
	if (utarray_len(array) > 0) {
		ccs_hyperparameter_t  previous = NULL;
		ccs_hyperparameter_t *p_h = NULL;
		while ( (p_h = (ccs_hyperparameter_t *)utarray_next(array, p_h)) ) {
			if (*p_h != previous) {
				count += 1;
				previous = *p_h;
			}
		}
	} else
		count = 0;
	if (hyperparameters) {
		if (count > num_hyperparameters) {
			utarray_free(array);
			return -CCS_INVALID_VALUE;
		}
		ccs_hyperparameter_t  previous = NULL;
		ccs_hyperparameter_t *p_h = NULL;
		size_t index = 0;
		while ( (p_h = (ccs_hyperparameter_t *)utarray_next(array, p_h)) ) {
			if (*p_h != previous) {
				hyperparameters[index++] = *p_h;
				previous = *p_h;
			}
		}
		for (size_t i = count; i < num_hyperparameters; i++)
			hyperparameters[i] = NULL;
	}
	if (num_hyperparameters_ret)
		*num_hyperparameters_ret = count;
	utarray_free(array);
	return CCS_SUCCESS;
}

1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
ccs_error_t
ccs_expression_check_context(ccs_expression_t          expression,
                             ccs_configuration_space_t context) {
	ccs_error_t err;
	UT_array *array;
	utarray_new(array, &_hyperparameter_icd);
	err = _get_hyperparameters(expression, array);
	utarray_sort(array, &hyper_sort);
	if (err) {
		utarray_free(array);
		return err;
	}
	if (utarray_len(array) > 0) {
		if (!context) {
			utarray_free(array);
			return -CCS_INVALID_VALUE;
		}
		ccs_hyperparameter_t  previous = NULL;
		ccs_hyperparameter_t *p_h = NULL;
		while ( (p_h = (ccs_hyperparameter_t *)utarray_next(array, p_h)) ) {
			if (*p_h != previous) {
				size_t index;
				err = ccs_configuration_space_get_hyperparameter_index(
					context, *p_h, &index);
				if (err) {
					utarray_free(array);
					return err;
				}
				previous = *p_h;
			}
		}
	}
	utarray_free(array);
	return CCS_SUCCESS;
}