diff --git a/include/array.h b/include/array.h index 0e54ed0..8f79146 100644 --- a/include/array.h +++ b/include/array.h @@ -11,13 +11,15 @@ typedef int (*array_access_callback)(void* value, ssize_t index, vtype type, voi /*#####################################################################################################################*/ -extern void array_init (vtype_array* x, vtype type) Nonnull__(1); +inline void array_init (vtype_array* x, vtype type) Always_inline__ Nonnull__(1); extern size_t array_slice (vtype_array* x, vtype_array* src, ssize_t index, size_t count, bool cut) Nonnull__(1); extern void array_sort (vtype_array* x) Nonnull__(1); extern void array_reverse (vtype_array* x) Nonnull__(1); extern void* at_array(const vtype_array* s, ssize_t index) Nonnull__(1); +inline void array_init (vtype_array* x, vtype type) { x->type = type; x->mem = (void*)(x->size = 0); } + /*#####################################################################################################################*/ #define array_pop(x, value, data, callback) libcdsb_array_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 1) diff --git a/include/dict.h b/include/dict.h index c5a827b..7e3b6f8 100644 --- a/include/dict.h +++ b/include/dict.h @@ -11,7 +11,9 @@ typedef int (*dict_access_callback)(const void* key, vtype key_type, void* value /*#####################################################################################################################*/ -extern void dict_init(vtype_dict* x) Nonnull__(1); +inline void dict_init(vtype_dict* x) Always_inline__ Nonnull__(1); + +inline void dict_init(vtype_dict* x) { x->nodes = (void*)(x->capacity = x->size = 0); } /*#####################################################################################################################*/ diff --git a/include/list.h b/include/list.h index 31b545a..e1aa9d0 100644 --- a/include/list.h +++ b/include/list.h @@ -11,12 +11,14 @@ typedef int (*list_access_callback)(void* value, ssize_t index, vtype type, void /*#####################################################################################################################*/ -extern void list_init (vtype_list* x) Nonnull__(1); +inline void list_init (vtype_list* x) Always_inline__ Nonnull__(1); extern void list_extend (vtype_list* x, const vtype_list* s) Nonnull__(1,2); extern size_t list_slice (vtype_list* x, vtype_list* src, ssize_t index, size_t count, bool cut) Nonnull__(1,2); extern void list_sort (vtype_list* x) Nonnull__(1); extern void list_reverse(vtype_list* x) Nonnull__(1); +inline void list_init (vtype_list* x) { x->first = x->last = 0; } + /*#####################################################################################################################*/ #define list_pop(x, value, data, callback) libcdsb_list_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 1) diff --git a/include/vtype.h b/include/vtype.h index e3328f8..999078f 100644 --- a/include/vtype.h +++ b/include/vtype.h @@ -33,6 +33,7 @@ typedef enum libcdsb_value_types { VTYPE_DICT = 18, } vtype; +/*#####################################################################################################################*/ struct libcdsb_string { char* buffer; }; struct libcdsb_array { void* mem; size_t size; vtype type; }; @@ -43,6 +44,8 @@ struct libcdsb_set { struct libcdsb_rbtree_node* root; vtype type; }; struct libcdsb_list { struct libcdsb_list_node* last; struct libcdsb_list_node* first; }; struct libcdsb_dict { struct libcdsb_dict_node** nodes; size_t capacity; size_t size; }; +/*#####################################################################################################################*/ + typedef void* vtype_pointer; typedef bool vtype_bool; @@ -69,15 +72,17 @@ typedef struct libcdsb_list vtype_list; typedef struct libcdsb_dict vtype_dict; typedef struct libcdsb_string vtype_string; -extern size_t string_size(const vtype_string* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t string_nmemb(const vtype_string* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t array_size(const vtype_array* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t array_nmemb(const vtype_array* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t list_size(const vtype_list* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t map_size(const vtype_map* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t vset_size(const vtype_set* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t dict_size(const vtype_dict* x) Pure__ Warn_unused_result__ Nonnull__(1); -extern size_t dict_capacity(const vtype_dict* x) Pure__ Warn_unused_result__ Nonnull__(1); +/*#####################################################################################################################*/ + +extern size_t string_size (const vtype_string* x) Pure__ Warn_unused_result__ Nonnull__(1); +extern size_t string_nmemb (const vtype_string* x) Pure__ Warn_unused_result__ Nonnull__(1); +extern size_t array_nmemb (const vtype_array* x) Pure__ Warn_unused_result__ Nonnull__(1); +extern size_t list_size (const vtype_list* x) Pure__ Warn_unused_result__ Nonnull__(1); +extern size_t map_size (const vtype_map* x) Pure__ Warn_unused_result__ Nonnull__(1); +extern size_t vset_size (const vtype_set* x) Pure__ Warn_unused_result__ Nonnull__(1); +inline size_t array_size (const vtype_array* x) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); +inline size_t dict_size (const vtype_dict* x) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); +inline size_t dict_capacity(const vtype_dict* x) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); extern int string_compare(const vtype_string* s0, const vtype_string* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); extern int array_compare (const vtype_array* s0, const vtype_array* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); @@ -127,4 +132,10 @@ extern vtype_hash map_hash (const vtype_map* s) Pure__ Warn_unused_result__ extern vtype_hash vset_hash (const vtype_set* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_hash dict_hash (const vtype_dict* s) Pure__ Warn_unused_result__ Nonnull__(1); +/*#####################################################################################################################*/ + +inline size_t array_size (const vtype_array* x) { return x->size; } +inline size_t dict_size (const vtype_dict* x) { return x->size; } +inline size_t dict_capacity(const vtype_dict* x) { return x->capacity; } + #endif /* LIBCDSB_VTYPE_H */ diff --git a/src/array/compute.c b/src/array/compute.c index eee0bdd..637fb37 100644 --- a/src/array/compute.c +++ b/src/array/compute.c @@ -4,14 +4,11 @@ #include "include.h" #include "../__internal/assert.h" -size_t array_size (const arr_t* x) { - return x->size; -} - size_t array_nmemb(const arr_t* x) { return x->size*vtype_size(x->type); } + hash_t array_hash(const arr_t* s) { hash_t hash = 0; @@ -26,6 +23,7 @@ hash_t array_hash(const arr_t* s) { return hash + VTYPE_ARRAY; } + size_t libcdsb_array_count(const arr_t* s, const void* v, vtype t) { void *p; void *e; diff --git a/src/array/memory.c b/src/array/memory.c index 5a84119..06c0caa 100644 --- a/src/array/memory.c +++ b/src/array/memory.c @@ -23,12 +23,6 @@ static inline type_free get_type_free(vtype type) { /*#####################################################################################################################*/ -void array_init(arr_t* x, vtype t) { - x->type = t; - x->size = 0; - x->mem = nullptr; -} - void array_free(arr_t* x) { if (x->size && x->type >= VTYPE_STRING) { void* p = x->mem; diff --git a/src/array/modify.c b/src/array/modify.c index 0fe9e05..7565815 100644 --- a/src/array/modify.c +++ b/src/array/modify.c @@ -20,16 +20,15 @@ ssize_t libcdsb_array_insert(arr_t* x, const void* v, vtype t) { if (vtype_size(x->type) > sizeof(vnode_t)) vnode_free(&n, x->type); - } else if (x->type == t) { + } else { + type_assert(x->type, t); get_type_initializer(t)(array_internal_at(x, i), v); } - #ifndef NDEBUG - else abort(); - #endif return i; } + ssize_t libcdsb_array_attach(arr_t* x, const void* v, vtype t) { ssize_t i; vnode_t n; @@ -44,12 +43,10 @@ ssize_t libcdsb_array_attach(arr_t* x, const void* v, vtype t) { if (vtype_size(x->type) > sizeof(vnode_t)) vnode_free(&n, x->type); - } else if (x->type == t) { + } else { + type_assert(x->type, t); memcpy(array_internal_at(x, i), v, vtype_size(t)); } - #ifndef NDEBUG - else abort(); - #endif return i; } diff --git a/src/dict/memory.c b/src/dict/memory.c index 4b2a5b2..59234ea 100644 --- a/src/dict/memory.c +++ b/src/dict/memory.c @@ -3,10 +3,6 @@ #include "include.h" -void dict_init(dict_t* x) { - memset(x, 0, sizeof(*x)); -} - void dict_free(dict_t* x) { while (x->capacity--) { diff --git a/src/list/memory.c b/src/list/memory.c index d3a1c53..b215538 100644 --- a/src/list/memory.c +++ b/src/list/memory.c @@ -3,11 +3,6 @@ #include "include.h" -void list_init(list_t* x) { - memset(x, 0, sizeof(*x)); -} - - void list_free(list_t* x) { lnode_t* c; lnode_t* next; diff --git a/src/list/modify.c b/src/list/modify.c index 3dd9593..5e1a604 100644 --- a/src/list/modify.c +++ b/src/list/modify.c @@ -92,16 +92,7 @@ bool libcdsb_list_attach(list_t* x, ssize_t i, const void* v, vtype t, int ins) } else vnode_free(&c->node, c->type); - if (t < VTYPE_STRING) { - c->node = vnode_create(v, t); - } else if (sizeof(str_t) == sizeof(void*) && t == VTYPE_STRING) { - c->node = *(char**)v; - } else { - c->node = malloc(vtype_size(t)); - memcpy(c->node, v, vtype_size(t)); - } - - c->type = t; + vnode_attach(&c->node, v, c->type = t); return true; } diff --git a/src/set/modify.c b/src/set/modify.c index dd5e4c2..42066da 100644 --- a/src/set/modify.c +++ b/src/set/modify.c @@ -9,12 +9,43 @@ bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) { int cmp; rbnode_t* n; rbnode_t* p; + + if (!rbnode_is_empty(n = x->root)) { + do { + p = n; + cmp = vtype_compare(v, t, vnode_peek(&n->value, t), t); + + if (cmp == 0) return false; + + n = (cmp < 0) ? n->left : n->right; + } while (!rbnode_is_empty(n)); + + n = rbnode_create(nullptr, p, 1); + + if (cmp < 0) p->left = n; + else p->right = n; + + if (!rbnode_is_root(p)) + rbnode_fixup(&x->root, n); + + } else n = x->root = rbnode_create(nullptr, rbnode_empty, 0); + + n->value = vnode_tcreate(x->type, v, t); + + return true; +} + + +bool libcdsb_vset_attach(set_t* x, const void* v, vtype t) { + int cmp; + rbnode_t* n; + rbnode_t* p; vnode_t vn; - n = x->root; - vn = vnode_tcreate(x->type, v, t); - t = x->type; - v = vnode_peek(&vn, t); + vnode_tattach(&vn, x->type, v, t); + n = x->root; + t = x->type; + v = vnode_peek(&vn, t); if (!rbnode_is_empty(n)) { do { @@ -41,48 +72,3 @@ bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) { return true; } - - -bool libcdsb_vset_attach(set_t* x, const void* v, vtype t) { - int cmp; - rbnode_t* n; - rbnode_t* p; - - n = x->root; - t = x->type; - - if (!rbnode_is_empty(n)) { - do { - p = n; - cmp = vtype_compare(v, t, vnode_peek(&n->value, t), t); - - if (cmp == 0) return false; - - n = (cmp < 0) ? n->left : n->right; - } while (!rbnode_is_empty(n)); - - n = rbnode_create(nullptr, p, 1); - - if (cmp < 0) p->left = n; - else p->right = n; - - if (!rbnode_is_root(p)) - rbnode_fixup(&x->root, n); - - } else n = x->root = rbnode_create(nullptr, rbnode_empty, 0); - - if (t < VTYPE_STRING) { - n->value = vnode_tcreate(x->type, v, t); - } else { - type_assert(x->type, t); - - if (sizeof(str_t) == sizeof(void*) && t == VTYPE_STRING) { - n->value = *(char**)v; - } else { - n->value = malloc(vtype_size(t)); - memcpy(n->value, v, vtype_size(t)); - } - } - - return true; -}