Update type generics
This commit is contained in:
parent
511516b218
commit
4e6d83c4cf
@ -226,6 +226,7 @@ void* libcdsb_vnode_peek(const vnode_t* x, vtype t) {
|
||||
case VTYPE_ARRAY:
|
||||
case VTYPE_LIST:
|
||||
case VTYPE_SET:
|
||||
case VTYPE_DICT:
|
||||
pt_: return *x;
|
||||
}
|
||||
}
|
||||
@ -260,7 +261,8 @@ void libcdsb_vnode_free(vnode_t* x, vtype t) {
|
||||
case VTYPE_MAP: map_free(*x); goto pt_;
|
||||
case VTYPE_ARRAY: array_free(*x); goto pt_;
|
||||
case VTYPE_LIST: list_free(*x); goto pt_;
|
||||
case VTYPE_SET: vset_free(*x);
|
||||
case VTYPE_SET: vset_free(*x); goto pt_;
|
||||
case VTYPE_DICT: dict_free(*x);
|
||||
pt_: free(*x);
|
||||
break;
|
||||
}
|
||||
@ -304,6 +306,9 @@ vnode_t libcdsb_vnode_create_target(vtype xt, const void* v, vtype t) {
|
||||
|
||||
case VTYPE_SET: _.ptr = vset_duplicate(v);
|
||||
break;
|
||||
|
||||
case VTYPE_DICT: _.ptr = dict_duplicate(v);
|
||||
break;
|
||||
}
|
||||
|
||||
return _.ptr;
|
||||
|
@ -59,6 +59,7 @@ const char* libcdsb_vtype_name(vtype t) {
|
||||
case VTYPE_ARRAY: return "VTYPE_ARRAY";
|
||||
case VTYPE_LIST: return "VTYPE_LIST";
|
||||
case VTYPE_SET: return "VTYPE_SET";
|
||||
case VTYPE_DICT: return "VTYPE_DICT";
|
||||
}
|
||||
}
|
||||
|
||||
|
50
src/vtype.c
50
src/vtype.c
@ -30,6 +30,17 @@ static ldbl_t normalize_value(const void* v, vtype t) {
|
||||
} else return abs(*(ldbl_t*)v) <= LDBL_EPSILON ? 0 : *(ldbl_t*)v;
|
||||
}
|
||||
|
||||
static hash_t ldouble_hash(ldbl_t s) {
|
||||
hash_t x, y;
|
||||
ldbl_t m;
|
||||
|
||||
m = modfl(s, &s);
|
||||
x = llrintl(s);
|
||||
y = floorl(m * 10000);
|
||||
|
||||
return x ^ y;
|
||||
}
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
int libcdsb_vtype_compare_values(const void* s0, vtype t0, const void* s1, vtype t1) {
|
||||
@ -66,9 +77,9 @@ int libcdsb_vtype_compare_values_eq(const void* s0, const void* s1, vtype t) {
|
||||
case VTYPE_UINT8: return compare( u8_t, s0, s1);
|
||||
case VTYPE_UINT16: return compare(u16_t, s0, s1);
|
||||
case VTYPE_UINT32:
|
||||
u32_: return compare(u32_t, s0, s1);
|
||||
x86_ptr: return compare(u32_t, s0, s1);
|
||||
|
||||
case VTYPE_POINTER: if (sizeof(void*) != 8) goto u32_;
|
||||
case VTYPE_POINTER: if (!is_x64) goto x86_ptr;
|
||||
case VTYPE_UINT64: return compare(u64_t, s0, s1);
|
||||
|
||||
case VTYPE_LDOUBLE: return (abs(*(ldbl_t*)s0 - *(ldbl_t*)s1) <= LDBL_EPSILON) ? 0 : (*(ldbl_t*)s0 < *(ldbl_t*)s1 ? -1 : 1);
|
||||
@ -80,7 +91,42 @@ int libcdsb_vtype_compare_values_eq(const void* s0, const void* s1, vtype t) {
|
||||
case VTYPE_LIST: return list_compare(s0, s1);
|
||||
case VTYPE_MAP: return map_compare(s0, s1);
|
||||
case VTYPE_SET: return vset_compare(s0, s1);
|
||||
case VTYPE_DICT: return dict_compare(s0, s1);
|
||||
}
|
||||
|
||||
#undef compare
|
||||
}
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
hash_t libcdsb_vtype_hash(void* v, vtype t) {
|
||||
|
||||
switch (t) { default: abort();
|
||||
|
||||
case VTYPE_BOOLEAN:
|
||||
case VTYPE_INT8:
|
||||
case VTYPE_UINT8: return (hash_t)(*(u8_t*)v);
|
||||
|
||||
case VTYPE_INT16:
|
||||
case VTYPE_UINT16: return (hash_t)(*(u16_t*)v);
|
||||
|
||||
case VTYPE_INT32:
|
||||
case VTYPE_UINT32:
|
||||
x86_ptr: return (hash_t)(*(u32_t*)v);
|
||||
|
||||
case VTYPE_POINTER: if (!is_x64) goto x86_ptr;
|
||||
case VTYPE_INT64:
|
||||
case VTYPE_UINT64: return (hash_t)(*(u64_t*)v);
|
||||
|
||||
case VTYPE_STRING: return string_hash(v);
|
||||
case VTYPE_ARRAY: return array_hash(v);
|
||||
case VTYPE_LIST: return list_hash(v);
|
||||
case VTYPE_MAP: return map_hash(v);
|
||||
case VTYPE_SET: return vset_hash(v);
|
||||
case VTYPE_DICT: return dict_hash(v);
|
||||
|
||||
case VTYPE_FLOAT: return ldouble_hash(*(fl_t*)v);
|
||||
case VTYPE_DOUBLE: return ldouble_hash(*(dbl_t*)v);
|
||||
case VTYPE_LDOUBLE: return ldouble_hash(*(ldbl_t*)v);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user