Commit d8daea20 authored by Brice Videau's avatar Brice Videau
Browse files

C99 compliant.

parent 0385cf93
......@@ -80,55 +80,23 @@ enum ccs_numeric_type_e {
typedef enum ccs_numeric_type_e ccs_numeric_type_t;
union ccs_object_u {
void *ptr;
ccs_rng_t rng;
ccs_configuration_space_t configuration_space;
ccs_configuration_t configuration;
ccs_distribution_t distribution;
ccs_hyperparameter_t hyperparameter;
ccs_expression_t expression;
ccs_condition_t condition;
ccs_forbidden_clause_t forbidden_clause;
#ifdef __cplusplus
ccs_object_u(void *v) : ptr(v) {}
ccs_object_u(ccs_rng_t v) : rng(v) {}
ccs_object_u(ccs_configuration_space_t v) : configuration_space(v) {}
ccs_object_u(ccs_configuration_t v) : configuration(v) {}
ccs_object_u(ccs_distribution_t v) : distribution(v) {}
ccs_object_u(ccs_hyperparameter_t v) : hyperparameter(v) {}
ccs_object_u(ccs_expression_t v) : expression(v) {}
ccs_object_u(ccs_condition_t v) : condition(v) {}
ccs_object_u(ccs_forbidden_clause_t v) : forbidden_clause(v) {}
#endif
};
typedef union ccs_object_u ccs_object_t;
typedef void * ccs_object_t;
union ccs_numeric_u {
ccs_float_t f;
ccs_int_t i;
#ifdef __cplusplus
ccs_numeric_u() : i(0L) {}
ccs_numeric_u(ccs_float_t v) : f(v) {}
ccs_numeric_u(ccs_int_t v) : i(v) {}
#endif
};
typedef union ccs_numeric_u ccs_numeric_t;
#define CCSF(v) ( (ccs_numeric_t){ .f = v })
#define CCSI(v) ( (ccs_numeric_t){ .i = v })
union ccs_value_u {
ccs_float_t f;
ccs_int_t i;
char *s;
ccs_object_t o;
#ifdef __cplusplus
ccs_value_u() : i(0L) {}
ccs_value_u(ccs_float_t v) : f(v) {}
ccs_value_u(ccs_int_t v) : i(v) {}
ccs_value_u(char *v) : s(v) {}
ccs_value_u(ccs_object_t v) : o(v) {}
#endif
};
typedef union ccs_value_u ccs_value_t;
......@@ -146,9 +114,6 @@ typedef struct ccs_interval_s ccs_interval_t;
struct ccs_datum_u {
ccs_value_t value;
ccs_data_type_t type;
#ifdef __cplusplus
ccs_datum_u() : value(0L), type(CCS_NONE) {}
#endif
};
typedef struct ccs_datum_u ccs_datum_t;
......
......@@ -34,14 +34,12 @@ ccs_create_distribution(ccs_distribution_type_t distribution_type,
ccs_distribution_t *distribution_ret);
extern ccs_error_t
_ccs_create_normal_distribution(ccs_numeric_type_t data_type,
ccs_float_t mu,
ccs_float_t sigma,
ccs_scale_type_t scale,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret);
#define ccs_create_normal_distribution(t, m, s, sc, q, d) \
_ccs_create_normal_distribution(t, m, s, sc, (ccs_numeric_t)(q), d)
ccs_create_normal_distribution(ccs_numeric_type_t data_type,
ccs_float_t mu,
ccs_float_t sigma,
ccs_scale_type_t scale,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret);
extern ccs_error_t
ccs_create_normal_int_distribution(ccs_float_t mu,
......@@ -58,15 +56,12 @@ ccs_create_normal_float_distribution(ccs_float_t mu,
ccs_distribution_t *distribution_ret);
extern ccs_error_t
_ccs_create_uniform_distribution(ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_scale_type_t scale_type,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret);
#define ccs_create_uniform_distribution(t, l, u, s, q, d) \
_ccs_create_uniform_distribution(t, (ccs_numeric_t)(l), (ccs_numeric_t)(u), s, (ccs_numeric_t)(q), d)
ccs_create_uniform_distribution(ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_scale_type_t scale_type,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret);
extern ccs_error_t
ccs_create_uniform_int_distribution(ccs_int_t lower,
......
......@@ -17,17 +17,15 @@ typedef enum ccs_hyperparameter_type_e ccs_hyperparameter_type_t;
// Hyperparameter Interface
extern ccs_error_t
_ccs_create_numerical_hyperparameter(const char *name,
ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_numeric_t quantization,
ccs_numeric_t default_value,
ccs_distribution_t distribution,
void *user_data,
ccs_hyperparameter_t *hyperparameter_ret);
#define ccs_create_numerical_hyperparameter(n, t, l, u, q, d, dis, us, h) \
_ccs_create_numerical_hyperparameter(n, t, (ccs_value_t)(l), (ccs_value_t)(u), (ccs_value_t)(q), (ccs_value_t)(d), dis, us, h)
ccs_create_numerical_hyperparameter(const char *name,
ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_numeric_t quantization,
ccs_numeric_t default_value,
ccs_distribution_t distribution,
void *user_data,
ccs_hyperparameter_t *hyperparameter_ret);
extern ccs_error_t
ccs_create_categorical_hyperparameter(const char *name,
......
......@@ -37,7 +37,6 @@ ccs_interval_include(ccs_interval_t *interval, ccs_numeric_t value) {
}
}
#ifdef __cplusplus
}
#endif
......
......@@ -10,7 +10,7 @@ ccs_init() {
ccs_error_t
_ccs_retain_object(ccs_object_t object) {
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object.ptr;
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
if (!obj || obj->refcount <= 0)
return -CCS_INVALID_OBJECT;
obj->refcount += 1;
......@@ -19,7 +19,7 @@ _ccs_retain_object(ccs_object_t object) {
ccs_error_t
_ccs_release_object(ccs_object_t object) {
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object.ptr;
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
if (!obj || obj->refcount <= 0)
return -CCS_INVALID_OBJECT;
obj->refcount -= 1;
......@@ -27,7 +27,7 @@ _ccs_release_object(ccs_object_t object) {
ccs_error_t err = obj->ops->del(object);
if (err)
return err;
free(object.ptr);
free(object);
}
return CCS_SUCCESS;
}
......@@ -35,7 +35,7 @@ _ccs_release_object(ccs_object_t object) {
ccs_error_t
_ccs_object_get_type(ccs_object_t object,
ccs_object_type_t *type_ret) {
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object.ptr;
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
if (!obj)
return -CCS_INVALID_OBJECT;
if (!type_ret)
......@@ -47,7 +47,7 @@ _ccs_object_get_type(ccs_object_t object,
ccs_error_t
_ccs_object_get_refcount(ccs_object_t object,
int32_t *refcount_ret) {
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object.ptr;
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
if (!obj)
return -CCS_INVALID_OBJECT;
if (!refcount_ret)
......
......@@ -28,7 +28,7 @@ _ccs_distribution_normal_samples(_ccs_distribution_data_t *data,
size_t num_values,
ccs_numeric_t *values);
_ccs_distribution_ops_t _ccs_distribution_normal_ops = {
static _ccs_distribution_ops_t _ccs_distribution_normal_ops = {
{ &_ccs_distribution_del },
&_ccs_distribution_normal_samples,
&_ccs_distribution_normal_get_bounds
......@@ -208,12 +208,12 @@ _ccs_distribution_normal_samples(_ccs_distribution_data_t *data,
}
extern ccs_error_t
_ccs_create_normal_distribution(ccs_numeric_type_t data_type,
ccs_float_t mu,
ccs_float_t sigma,
ccs_scale_type_t scale_type,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret) {
ccs_create_normal_distribution(ccs_numeric_type_t data_type,
ccs_float_t mu,
ccs_float_t sigma,
ccs_scale_type_t scale_type,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret) {
if (!distribution_ret)
return -CCS_INVALID_VALUE;
if (data_type != CCS_NUM_FLOAT && data_type != CCS_NUM_INTEGER)
......@@ -270,4 +270,3 @@ ccs_normal_distribution_get_parameters(ccs_distribution_t distribution,
return CCS_SUCCESS;
}
......@@ -31,7 +31,7 @@ _ccs_distribution_uniform_samples(_ccs_distribution_data_t *data,
size_t num_values,
ccs_numeric_t *values);
_ccs_distribution_ops_t _ccs_distribution_uniform_ops = {
static _ccs_distribution_ops_t _ccs_distribution_uniform_ops = {
{ &_ccs_distribution_del },
&_ccs_distribution_uniform_samples,
&_ccs_distribution_uniform_get_bounds
......@@ -118,12 +118,12 @@ _ccs_distribution_uniform_samples(_ccs_distribution_data_t *data,
}
ccs_error_t
_ccs_create_uniform_distribution(ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_scale_type_t scale_type,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret) {
ccs_create_uniform_distribution(ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_scale_type_t scale_type,
ccs_numeric_t quantization,
ccs_distribution_t *distribution_ret) {
if (!distribution_ret)
return -CCS_INVALID_VALUE;
if (data_type != CCS_NUM_FLOAT && data_type != CCS_NUM_INTEGER)
......@@ -186,7 +186,7 @@ _ccs_create_uniform_distribution(ccs_numeric_type_t data_type,
return CCS_SUCCESS;
}
extern ccs_error_t
ccs_error_t
ccs_uniform_distribution_get_parameters(ccs_distribution_t distribution,
ccs_numeric_t *lower,
ccs_numeric_t *upper) {
......
......@@ -10,13 +10,11 @@ typedef struct _ccs_hyperparameter_numerical_data_s _ccs_hyperparameter_numerica
static ccs_error_t
_ccs_hyperparameter_numerical_del(ccs_object_t o) {
ccs_hyperparameter_t d = o.hyperparameter;
ccs_hyperparameter_t d = (ccs_hyperparameter_t)o;
_ccs_hyperparameter_numerical_data_t *data = (_ccs_hyperparameter_numerical_data_t *)(d->data);
return ccs_release_object(data->common_data.distribution);
}
static inline
ccs_bool_t _check_value(_ccs_hyperparameter_numerical_data_t *d,
ccs_numeric_t value) {
......@@ -25,9 +23,9 @@ ccs_bool_t _check_value(_ccs_hyperparameter_numerical_data_t *d,
static ccs_error_t
_ccs_hyperparameter_numerical_samples(_ccs_hyperparameter_data_t *data,
ccs_rng_t rng,
size_t num_values,
ccs_datum_t *values) {
ccs_rng_t rng,
size_t num_values,
ccs_datum_t *values) {
_ccs_hyperparameter_numerical_data_t *d = (_ccs_hyperparameter_numerical_data_t *)data;
ccs_error_t err;
ccs_numeric_t *vs = (ccs_numeric_t *)values;
......@@ -83,21 +81,21 @@ _ccs_hyperparameter_numerical_samples(_ccs_hyperparameter_data_t *data,
return CCS_SUCCESS;
}
_ccs_hyperparameter_ops_t _ccs_hyperparameter_numerical_ops = {
static _ccs_hyperparameter_ops_t _ccs_hyperparameter_numerical_ops = {
{ &_ccs_hyperparameter_numerical_del },
&_ccs_hyperparameter_numerical_samples
};
ccs_error_t
_ccs_create_numerical_hyperparameter(const char *name,
ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_numeric_t quantization,
ccs_numeric_t default_value,
ccs_distribution_t distribution,
void *user_data,
ccs_hyperparameter_t *hyperparameter_ret) {
ccs_create_numerical_hyperparameter(const char *name,
ccs_numeric_type_t data_type,
ccs_numeric_t lower,
ccs_numeric_t upper,
ccs_numeric_t quantization,
ccs_numeric_t default_value,
ccs_distribution_t distribution,
void *user_data,
ccs_hyperparameter_t *hyperparameter_ret) {
if (!hyperparameter_ret)
return -CCS_INVALID_VALUE;
if (data_type != CCS_NUM_FLOAT && data_type != CCS_NUM_INTEGER)
......
......@@ -39,8 +39,8 @@ ccs_rng_create(ccs_rng_t *rng_ret) {
static ccs_error_t
_ccs_rng_del(ccs_object_t object) {
gsl_rng_free(object.rng->data->rng);
object.rng->data->rng = NULL;
gsl_rng_free(((ccs_rng_t)object)->data->rng);
((ccs_rng_t)object)->data->rng = NULL;
return CCS_SUCCESS;
}
......
......@@ -211,6 +211,38 @@ void test_equal_int() {
assert( equal );
}
void test_interval_include_float() {
ccs_interval_t interval;
interval.type = CCS_NUM_FLOAT;
interval.lower.f = -3.0;
interval.upper.f = 5.0;
interval.lower_included = CCS_TRUE;
interval.upper_included = CCS_FALSE;
assert( ccs_interval_include(&interval, CCSF(0.0)) );
assert( ccs_interval_include(&interval, CCSF(-3.0)) );
assert( !ccs_interval_include(&interval, CCSF(-3.1)) );
assert( ccs_interval_include(&interval, CCSF(4.9)) );
assert( !ccs_interval_include(&interval, CCSF(5.0)) );
}
void test_interval_include_int() {
ccs_interval_t interval;
interval.type = CCS_NUM_INTEGER;
interval.lower.i = -3;
interval.upper.i = 5;
interval.lower_included = CCS_TRUE;
interval.upper_included = CCS_FALSE;
assert( ccs_interval_include(&interval, CCSI(0)) );
assert( ccs_interval_include(&interval, CCSI(-3)) );
assert( !ccs_interval_include(&interval, CCSI(-4)) );
assert( ccs_interval_include(&interval, CCSI(4)) );
assert( !ccs_interval_include(&interval, CCSI(5)) );
}
int main(int argc, char *argv[]) {
ccs_init();
test_empty_float();
......@@ -219,6 +251,8 @@ int main(int argc, char *argv[]) {
test_intersect_int();
test_equal_float();
test_equal_int();
test_interval_include_float();
test_interval_include_int();
return 0;
}
......@@ -23,7 +23,7 @@ static void test_create_normal_distribution() {
1.0,
2.0,
CCS_LINEAR,
0.0,
CCSF(0.0),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -78,7 +78,7 @@ static void test_create_normal_distribution_errors() {
1.0,
2.0,
CCS_LINEAR,
0.0,
CCSF(0.0),
&distrib);
assert( err == -CCS_INVALID_TYPE );
......@@ -88,7 +88,7 @@ static void test_create_normal_distribution_errors() {
1.0,
2.0,
(ccs_scale_type_t)0xdeadbeef,
0.0,
CCSF(0.0),
&distrib);
assert( err == -CCS_INVALID_SCALE );
......@@ -98,7 +98,7 @@ static void test_create_normal_distribution_errors() {
1.0,
2.0,
CCS_LINEAR,
-1.0,
CCSF(-1.0),
&distrib);
assert( err == -CCS_INVALID_VALUE );
......@@ -108,7 +108,7 @@ static void test_create_normal_distribution_errors() {
1.0,
2.0,
CCS_LINEAR,
0.0,
CCSF(0.0),
NULL);
assert( err == -CCS_INVALID_VALUE );
......@@ -145,7 +145,7 @@ static void test_normal_distribution_int() {
mu,
sigma,
CCS_LINEAR,
0L,
CCSI(0),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -194,7 +194,7 @@ static void test_normal_distribution_float() {
mu,
sigma,
CCS_LINEAR,
0.0,
CCSF(0.0),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -243,7 +243,7 @@ static void test_normal_distribution_int_log() {
mu,
sigma,
CCS_LOGARITHMIC,
0L,
CCSI(0),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -302,7 +302,7 @@ static void test_normal_distribution_float_log() {
mu,
sigma,
CCS_LOGARITHMIC,
0.0,
CCSF(0.0),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -352,7 +352,7 @@ static void test_normal_distribution_int_quantize() {
mu,
sigma,
CCS_LINEAR,
q,
CCSI(q),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -401,7 +401,7 @@ static void test_normal_distribution_float_quantize() {
mu,
sigma,
CCS_LINEAR,
0.2,
CCSF(0.2),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -451,7 +451,7 @@ static void test_normal_distribution_int_log_quantize() {
mu,
sigma,
CCS_LOGARITHMIC,
quantize,
CCSI(quantize),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -512,7 +512,7 @@ static void test_normal_distribution_float_log_quantize() {
mu,
sigma,
CCS_LOGARITHMIC,
quantization,
CCSF(quantization),
&distrib);
assert( err == CCS_SUCCESS );
......
......@@ -18,10 +18,10 @@ static void test_create_uniform_distribution() {
err = ccs_create_uniform_distribution(
CCS_NUM_INTEGER,
l,
u,
CCSI(l),
CCSI(u),
CCS_LINEAR,
q,
CCSI(q),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -73,50 +73,50 @@ static void test_create_uniform_distribution_errors() {
// check wrong data_type
err = ccs_create_uniform_distribution(
(ccs_numeric_type_t)2,
-10L,
11L,
CCSI(-10),
CCSI(11),
CCS_LINEAR,
0L,
CCSI(0),
&distrib);
assert( err == -CCS_INVALID_TYPE );
// check wrong data_type
err = ccs_create_uniform_distribution(
CCS_NUM_INTEGER,
-10L,
11L,
CCSI(-10),
CCSI(11),
(ccs_scale_type_t)3,
0L,
CCSI(0),
&distrib);
assert( err == -CCS_INVALID_SCALE );
// check wrong bounds
err = ccs_create_uniform_distribution(
CCS_NUM_INTEGER,
10L,
-11L,
CCSI(10),
CCSI(-11),
CCS_LINEAR,
0L,
CCSI(0),
&distrib);
assert( err == -CCS_INVALID_VALUE );
// check wrong quantization
err = ccs_create_uniform_distribution(
CCS_NUM_INTEGER,
-10L,
11L,
CCSI(-10),
CCSI(11),
CCS_LINEAR,
-1L,
CCSI(-1),
&distrib);
assert( err == -CCS_INVALID_VALUE );
// check wrong pointer
err = ccs_create_uniform_distribution(
CCS_NUM_INTEGER,
10L,
-11L,
CCSI(10),
CCSI(-11),
CCS_LINEAR,
0L,
CCSI(0),
NULL);
assert( err == -CCS_INVALID_VALUE );
......@@ -128,18 +128,18 @@ static void test_uniform_distribution_int() {
ccs_rng_t rng = NULL;
ccs_error_t err = CCS_SUCCESS;
const size_t num_samples = 100;
const ccs_int_t lower = -10;
const ccs_int_t upper = 11;
ccs_int_t lower = -10;
ccs_int_t upper = 11;
ccs_numeric_t samples[num_samples];
err = ccs_rng_create(&rng);
assert( err == CCS_SUCCESS );
err = ccs_create_uniform_distribution(
CCS_NUM_INTEGER,
lower,
upper,
CCSI(lower),
CCSI(upper),
CCS_LINEAR,
0L,
CCSI(0),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -162,18 +162,18 @@ static void test_uniform_distribution_int_log() {
ccs_rng_t rng = NULL;
ccs_error_t err = CCS_SUCCESS;
const size_t num_samples = 1000;
const ccs_int_t lower = 1;
const ccs_int_t upper = 100;
ccs_int_t lower = 1;
ccs_int_t upper = 100;
ccs_numeric_t samples[num_samples];
err = ccs_rng_create(&rng);
assert( err == CCS_SUCCESS );
err = ccs_create_uniform_distribution(
CCS_NUM_INTEGER,
lower,
upper,
CCSI(lower),
CCSI(upper),
CCS_LOGARITHMIC,
0L,
CCSI(0),
&distrib);
assert( err == CCS_SUCCESS );
......@@ -196,19 +196,19 @@ static void test_uniform_distribution_int_log_quantize() {
ccs_rng_t rng = NULL;