Commit 9f3c6ad8 authored by Brice Videau's avatar Brice Videau
Browse files

Added flags to datums.

parent a09d9d59
......@@ -218,7 +218,7 @@ class ccs_error(CEnumeration):
'OUT_OF_MEMORY',
'UNSUPPORTED_OPERATION' ]
class ccs_data_type(CEnumeration64):
class ccs_data_type(CEnumeration):
_members_ = [
('NONE', 0),
'INTEGER',
......@@ -227,7 +227,13 @@ class ccs_data_type(CEnumeration64):
'STRING',
'INACTIVE',
'OBJECT' ]
class ccs_datum_flag(CEnumeration):
_members_ = [
('FLAG_DEFAULT', 0) ]
ccs_datum_flags = ct.c_uint
class ccs_numeric_type(CEnumeration64):
_members_ = [
('NUM_INTEGER', ccs_data_type.INTEGER),
......@@ -265,11 +271,13 @@ class ccs_value(ct.Union):
class ccs_datum_fix(ct.Structure):
_fields_ = [('value', ccs_int),
('type', ccs_data_type)]
('type', ccs_data_type),
('flags', ccs_datum_flags)]
class ccs_datum(ct.Structure):
_fields_ = [('_value', ccs_value),
('_type', ccs_data_type)]
('_type', ccs_data_type),
('flags', ccs_datum_flags)]
def __init__(self, v = None):
super().__init__()
......@@ -307,26 +315,33 @@ class ccs_datum(ct.Structure):
if v is None:
self.type = ccs_data_type.NONE
self._value.i = 0
self.flags = 0
elif isinstance(v, bool):
self.type = ccs_data_type.BOOLEAN
self._value.i = 1 if v else 0
self.flags = 0
elif isinstance(v, int):
self.type = ccs_data_type.INTEGER
self._value.i = v
self.flags = 0
elif isinstance(v, float):
self.type = ccs_data_type.FLOAT
self._value.f = v
self.flags = 0
elif isinstance(v, str):
self.type = ccs_data_type.STRING
self._string = str.encode(v)
self._value.s = ct.c_char_p(self._string)
self.flags = 0
elif v is ccs_inactive:
self.type = ccs_data_type.INACTIVE
self._value.i = 0
self.flags = 0
elif isinstance(v, Object):
self.type = ccs_data_type.OBJECT
self._object = v
self._value.o = v.handle
self.flags = 0
else:
raise Error(ccs_error(ccs_error.INVALID_VALUE))
......
......@@ -130,7 +130,7 @@ module CCS
end
end
DataType = enum FFI::Type::INT64, :ccs_data_type_t, [
DataType = enum FFI::Type::INT32, :ccs_data_type_t, [
:CCS_NONE,
:CCS_INTEGER,
:CCS_FLOAT,
......@@ -139,6 +139,12 @@ module CCS
:CCS_INACTIVE,
:CCS_OBJECT ]
DatumFlag = enum FFI::Type::INT32, :ccs_datum_flag_t, [
:CCS_FLAG_DEFAULT ]
DatumFlags = bitmask FFI::Type::INT32, :ccs_datum_flags_t, [
]
NumericType = enum FFI::Type::INT64, :ccs_numeric_type_t, [
:CCS_NUM_INTEGER, DataType.to_native(:CCS_INTEGER, nil),
:CCS_NUM_FLOAT, DataType.to_native(:CCS_FLOAT, nil) ]
......@@ -212,21 +218,26 @@ module CCS
class Datum < FFI::Struct
layout :value, :ccs_value_t,
:type, :ccs_data_type_t
:type, :ccs_data_type_t,
:flags, :ccs_datum_flags_t
end
class Datum
NONE = self::new
NONE[:type] = :CCS_NONE
NONE[:value][:i] = 0
NONE[:flags] = 0
TRUE = self::new
TRUE[:type] = :CCS_BOOLEAN
TRUE[:value][:i] = CCS::TRUE
TRUE[:flags] = 0
FALSE = self::new
FALSE[:type] = :CCS_BOOLEAN
FALSE[:value][:i] = CCS::FALSE
FALSE[:flags] = 0
INACTIVE = self::new
INACTIVE[:type] = :CCS_INACTIVE
INACTIVE[:value][:i] = 0
INACTIVE[:flags] = 0
def value
case self[:type]
when :CCS_NONE
......@@ -255,21 +266,27 @@ module CCS
when nil
self[:type] = :CCS_NONE
self[:value][:i] = 0
self[:flags] = 0
when true
self[:type] = :CCS_BOOLEAN
self[:value][:i] = 1
self[:flags] = 0
when false
self[:type] = :CCS_BOOLEAN
self[:value][:i] = 0
self[:flags] = 0
when Inactive
self[:type] = :CCS_INACTIVE
self[:value][:i] = 0
self[:flags] = 0
when Float
self[:type] = :CCS_FLOAT
self[:value][:f] = v
self[:flags] = 0
when Integer
self[:type] = :CCS_INTEGER
self[:value][:i] = v
self[:flags] = 0
when String
ptr = MemoryPointer::from_string(v)
if string_store
......@@ -279,6 +296,7 @@ module CCS
end
self[:type] = :CCS_STRING
self[:value][:s] = ptr
self[:flags] = 0
when Object
if object_store
object_store.push v
......@@ -287,6 +305,7 @@ module CCS
end
self[:type] = :CCS_OBJECT
self[:value][:o] = v.handle
self[:flags] = 0
else
raise CCSError, :CCS_INVALID_TYPE
end
......@@ -307,11 +326,13 @@ module CCS
d = self::new
d[:type] = :CCS_FLOAT
d[:value][:f] = v
d[:flags] = 0
d
when Integer
d = self::new
d[:type] = :CCS_INTEGER
d[:value][:i] = v
d[:flags] = 0
d
when String
d = self::new
......@@ -319,11 +340,13 @@ module CCS
d.instance_variable_set(:@string, ptr)
d[:type] = :CCS_STRING
d[:value][:s] = ptr
d[:flags] = 0
d
when Object
d = self::new
d[:type] = :CCS_OBJECT
d[:value][:o] = v.handle
d[:flags] = 0
d.instance_variable_set(:@object, v)
d
else
......
......@@ -64,7 +64,7 @@ enum ccs_error_e {
CCS_OUT_OF_MEMORY,
CCS_UNSUPPORTED_OPERATION,
CCS_ERROR_MAX,
CCS_ERROR_FORCE_32BIT = INT_MAX
CCS_ERROR_FORCE_32BIT = INT32_MAX
};
typedef enum ccs_error_e ccs_error_t;
......@@ -81,7 +81,7 @@ enum ccs_object_type_e {
CCS_EVALUATION,
CCS_TUNER,
CCS_OBJECT_TYPE_MAX,
CCS_OBJECT_TYPE_FORCE_32BIT = INT_MAX
CCS_OBJECT_TYPE_FORCE_32BIT = INT32_MAX
};
typedef enum ccs_object_type_e ccs_object_type_t;
......@@ -95,11 +95,20 @@ enum ccs_data_type_e {
CCS_INACTIVE,
CCS_OBJECT,
CCS_DATA_TYPE_MAX,
CCS_DATA_TYPE_FORCE_64BIT = INT64_MAX
CCS_DATA_TYPE_FORCE_32BIT = INT32_MAX
};
typedef enum ccs_data_type_e ccs_data_type_t;
typedef uint32_t ccs_datum_flags_t;
enum ccs_datum_flag_e {
CCS_FLAG_DEFAULT = 0,
CCS_DATUM_FLAG_FORCE_32BIT = INT32_MAX
};
typedef enum ccs_datum_flag_e ccs_datum_flag_t;
enum ccs_numeric_type_e {
CCS_NUM_INTEGER = CCS_INTEGER,
CCS_NUM_FLOAT = CCS_FLOAT,
......@@ -154,6 +163,7 @@ typedef union ccs_value_u ccs_value_t;
struct ccs_datum_s {
ccs_value_t value;
ccs_data_type_t type;
ccs_datum_flags_t flags;
};
typedef struct ccs_datum_s ccs_datum_t;
......@@ -163,6 +173,7 @@ ccs_bool(ccs_bool_t v) {
ccs_datum_t d;
d.type = CCS_BOOLEAN;
d.value.i = v;
d.flags = CCS_FLAG_DEFAULT;
return d;
}
......@@ -171,6 +182,7 @@ ccs_float(ccs_float_t v) {
ccs_datum_t d;
d.type = CCS_FLOAT;
d.value.f = v;
d.flags = CCS_FLAG_DEFAULT;
return d;
}
......@@ -179,6 +191,7 @@ ccs_int(ccs_int_t v) {
ccs_datum_t d;
d.type = CCS_INTEGER;
d.value.i = v;
d.flags = CCS_FLAG_DEFAULT;
return d;
}
......@@ -187,6 +200,7 @@ ccs_object(ccs_object_t v) {
ccs_datum_t d;
d.type = CCS_OBJECT;
d.value.o = v;
d.flags = CCS_FLAG_DEFAULT;
return d;
}
......@@ -195,14 +209,24 @@ ccs_string(const char *v) {
ccs_datum_t d;
d.type = CCS_STRING;
d.value.s = v;
d.flags = CCS_FLAG_DEFAULT;
return d;
}
extern const ccs_datum_t ccs_none;
extern const ccs_datum_t ccs_inactive;
extern const ccs_datum_t ccs_true;
extern const ccs_datum_t ccs_false;
#define CCS_NONE_VAL {{0}, CCS_NONE}
#define CCS_INACTIVE_VAL {{0}, CCS_INACTIVE}
#define CCS_NONE_VAL {{0}, CCS_NONE, CCS_FLAG_DEFAULT}
#define CCS_INACTIVE_VAL {{0}, CCS_INACTIVE, CCS_FLAG_DEFAULT}
#ifdef __cplusplus
#define CCS_TRUE_VAL {{(ccs_int_t)CCS_TRUE}, CCS_BOOLEAN, CCS_FLAG_DEFAULT}
#define CCS_FALSE_VAL {{(ccs_int_t)CCS_FALSE}, CCS_BOOLEAN, CCS_FLAG_DEFAULT}
#else
#define CCS_TRUE_VAL {{.i = CCS_TRUE}, CCS_BOOLEAN, CCS_FLAG_DEFAULT}
#define CCS_FALSE_VAL {{.i = CCS_FALSE}, CCS_BOOLEAN, CCS_FLAG_DEFAULT}
#endif
extern ccs_result_t
ccs_init();
......
......@@ -4,6 +4,8 @@
const ccs_datum_t ccs_none = CCS_NONE_VAL;
const ccs_datum_t ccs_inactive = CCS_INACTIVE_VAL;
const ccs_datum_t ccs_true = CCS_TRUE_VAL;
const ccs_datum_t ccs_false = CCS_FALSE_VAL;
const ccs_version_t ccs_version = { 0, 1, 0, 0 };
ccs_result_t
......
......@@ -68,23 +68,24 @@ static inline ccs_hash_t _hash_combine(ccs_hash_t h1, ccs_hash_t h2) {
static inline unsigned _hash_datum(ccs_datum_t *d) {
unsigned h;
unsigned h1, h2;
switch(d->type) {
case CCS_STRING:
if (d->value.s) {
unsigned h1, h2;
HASH_JEN(&(d->type), sizeof(d->type), h1);
HASH_JEN(&(d->type), sizeof(d->type), h1);
if (d->value.s)
HASH_JEN(d->value.s, strlen(d->value.s), h2);
h = _hash_combine(h1, h2);
}
else
HASH_JEN(d, sizeof(ccs_datum_t), h);
HASH_JEN(&(d->value), sizeof(d->value), h2);
h = _hash_combine(h1, h2);
break;
case CCS_NONE:
case CCS_INACTIVE:
HASH_JEN(&(d->type), sizeof(d->type), h);
break;
default:
HASH_JEN(d, sizeof(ccs_datum_t), h);
HASH_JEN(&(d->type), sizeof(d->type), h1);
HASH_JEN(&(d->value), sizeof(d->value), h2);
h = _hash_combine(h1, h2);
}
return h;
}
......
......@@ -149,8 +149,7 @@ _ccs_expr_or_eval(_ccs_expression_data_t *data,
if (left.type != CCS_BOOLEAN)
return -CCS_INVALID_VALUE;
if (left.value.i) {
result->type = CCS_BOOLEAN;
result->value.i = CCS_TRUE;
*result = ccs_true;
return CCS_SUCCESS;
}
}
......@@ -162,8 +161,7 @@ _ccs_expr_or_eval(_ccs_expression_data_t *data,
if (right.type != CCS_BOOLEAN)
return -CCS_INVALID_VALUE;
if (right.value.i) {
result->type = CCS_BOOLEAN;
result->value.i = CCS_TRUE;
*result = ccs_true;
return CCS_SUCCESS;
}
}
......@@ -173,8 +171,7 @@ _ccs_expr_or_eval(_ccs_expression_data_t *data,
if (errr)
return errr;
result->type = CCS_BOOLEAN;
result->value.i = CCS_FALSE;
*result = ccs_false;
return CCS_SUCCESS;
}
......@@ -193,8 +190,7 @@ _ccs_expr_and_eval(_ccs_expression_data_t *data,
eval_left_right(data, context, values, left, right, NULL, NULL);
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;
*result = ((left.value.i && right.value.i) ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
......@@ -308,14 +304,11 @@ _ccs_expr_equal_eval(_ccs_expression_data_t *data,
check_hypers(data->nodes[1], left, htr);
ccs_bool_t equal;
ccs_result_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;
if (htl == CCS_HYPERPARAMETER_TYPE_MAX &&
htr == CCS_HYPERPARAMETER_TYPE_MAX &&
err)
return err;
*result = (equal ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
......@@ -340,13 +333,12 @@ _ccs_expr_not_equal_eval(_ccs_expression_data_t *data,
ccs_bool_t equal;
ccs_result_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;
*result = (equal ? ccs_false : ccs_true);
} else {
if (err)
return err;
result->value.i = equal ? CCS_FALSE : CCS_TRUE;
*result = (equal ? ccs_false : ccs_true);
}
result->type = CCS_BOOLEAN;
return CCS_SUCCESS;
}
......@@ -380,8 +372,7 @@ _ccs_expr_less_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp < 0 ? CCS_TRUE : CCS_FALSE);
*result = (cmp < 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
if (htr == CCS_ORDINAL) {
......@@ -393,16 +384,14 @@ _ccs_expr_less_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp < 0 ? CCS_TRUE : CCS_FALSE);
*result = (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;
*result = (cmp < 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
......@@ -436,8 +425,7 @@ _ccs_expr_greater_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp > 0 ? CCS_TRUE : CCS_FALSE);
*result = (cmp > 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
if (htr == CCS_ORDINAL) {
......@@ -449,16 +437,14 @@ _ccs_expr_greater_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp > 0 ? CCS_TRUE : CCS_FALSE);
*result = (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;
*result = (cmp > 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
......@@ -492,8 +478,7 @@ _ccs_expr_less_or_equal_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp <= 0 ? CCS_TRUE : CCS_FALSE);
*result = (cmp <= 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
if (htr == CCS_ORDINAL) {
......@@ -505,16 +490,14 @@ _ccs_expr_less_or_equal_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp <= 0 ? CCS_TRUE : CCS_FALSE);
*result = (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;
*result = (cmp <= 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
......@@ -548,8 +531,7 @@ _ccs_expr_greater_or_equal_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp >= 0 ? CCS_TRUE : CCS_FALSE);
*result = (cmp >= 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
if (htr == CCS_ORDINAL) {
......@@ -561,16 +543,14 @@ _ccs_expr_greater_or_equal_eval(_ccs_expression_data_t *data,
left, right, &cmp);
if (err)
return err;
result->type = CCS_BOOLEAN;
result->value.i = (cmp >= 0 ? CCS_TRUE : CCS_FALSE);
*result = (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;
*result = (cmp >= 0 ? ccs_true : ccs_false);
return CCS_SUCCESS;
}
......@@ -596,8 +576,7 @@ _ccs_expr_in_eval(_ccs_expression_data_t *data,
if (err)
return err;
if (num_nodes == 0) {
result->type = CCS_BOOLEAN;
result->value.i = CCS_FALSE;
*result = ccs_false;
return CCS_SUCCESS;
}
......@@ -619,13 +598,11 @@ _ccs_expr_in_eval(_ccs_expression_data_t *data,
continue;
}
if (equal) {
result->type = CCS_BOOLEAN;
result->value.i = CCS_TRUE;
*result = ccs_true;
return CCS_SUCCESS;
}
}
result->type = CCS_BOOLEAN;
result->value.i = CCS_FALSE;
*result = ccs_false;
return CCS_SUCCESS;
}
......@@ -644,26 +621,22 @@ _ccs_expr_add_eval(_ccs_expression_data_t *data,
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;
*result = ccs_int(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;
*result = ccs_float(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;
*result = ccs_float(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;
*result = ccs_float(left.value.f + right.value.f);
return CCS_SUCCESS;
}
}
......@@ -685,26 +658,22 @@ _ccs_expr_substract_eval(_ccs_expression_data_t *data,
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;
*result = ccs_int(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;
*result = ccs_float(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;
*result = ccs_float(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;
*result = ccs_float(left.value.f - right.value.f);
return CCS_SUCCESS;
}
}
......@@ -726,26 +695,22 @@ _ccs_expr_multiply_eval(_ccs_expression_data_t *data,
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;
*result = ccs_int(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;
*result = ccs_float(left.value.i * right.