From 1b815613a42f57e0ea117968e2d954914864faf4 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Wed, 17 Aug 2022 22:20:40 +0300 Subject: [PATCH] Refactor array base --- src/array/base.c | 31 ++++++++++++++--------- src/array/copy.c | 66 ++++++++++++++++++++---------------------------- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/array/base.c b/src/array/base.c index 49ee49f..f674b0c 100644 --- a/src/array/base.c +++ b/src/array/base.c @@ -4,6 +4,23 @@ #include "include.h" #include "../__internal/assert.h" +typedef void (*type_free)(void*); + +static inline type_free get_type_free(vtype type) { + switch (type) { + default: + #ifndef NDEBUG + abort(); + #endif + case VTYPE_STRING: return (void*)string_free; + case VTYPE_ARRAY: return (void*) array_free; + case VTYPE_LIST: return (void*) list_free; + case VTYPE_MAP: return (void*) map_free; + case VTYPE_SET: return (void*) vset_free; + case VTYPE_DICT: return (void*) dict_free; + } +} + /*#####################################################################################################################*/ hash_t array_hash(const arr_t* s) { @@ -29,21 +46,11 @@ void array_init(arr_t* x, vtype t) { void array_free(arr_t* x) { if (x->size && x->type >= VTYPE_STRING) { void* p = x->mem; - void (*free_)(void*); + type_free free_; assert(!is_null(p)); - switch (x->type) { - default: - #ifndef NDEBUG - abort(); - #endif - case VTYPE_STRING: free_ = (void*)string_free; break; - case VTYPE_ARRAY: free_ = (void*) array_free; break; - case VTYPE_LIST: free_ = (void*) list_free; break; - case VTYPE_MAP: free_ = (void*) map_free; break; - case VTYPE_SET: free_ = (void*) vset_free; break; - } + free_ = get_type_free(x->type); do { free_(p); diff --git a/src/array/copy.c b/src/array/copy.c index 967e74e..0100ce7 100644 --- a/src/array/copy.c +++ b/src/array/copy.c @@ -4,6 +4,22 @@ #include "include.h" #include "../__internal/assert.h" +typedef void (*type_initializer)(void*, const void*); + +static inline type_initializer get_type_initializer(vtype type) { + switch (type) { + default: + #ifndef NDEBUG + abort(); + #endif + case VTYPE_STRING: return (void*)string_copy_init; + case VTYPE_ARRAY: return (void*) array_copy_init; + case VTYPE_LIST: return (void*) list_copy_init; + case VTYPE_MAP: return (void*) map_copy_init; + case VTYPE_SET: return (void*) vset_copy_init; + case VTYPE_DICT: return (void*) dict_copy_init; + } +} arr_t array_copy(const arr_t* s) { arr_t x = { .mem = 0, .size = 0, .type = 0 }; @@ -14,19 +30,12 @@ arr_t array_copy(const arr_t* s) { if (s->type >= VTYPE_STRING) { void *p, *v, *e; - void (*init)(void*, const void*); + type_initializer init; x.mem = p = malloc(x.size*vtype_size(x.type)); v = s->mem; e = array_end(&x); - - switch (s->type) { default: abort(); - case VTYPE_STRING: init = (void*)string_copy_init; break; - case VTYPE_ARRAY: init = (void*) array_copy_init; break; - case VTYPE_LIST: init = (void*) list_copy_init; break; - case VTYPE_MAP: init = (void*) map_copy_init; break; - case VTYPE_SET: init = (void*) vset_copy_init; break; - } + init = get_type_initializer(s->type); do { init(p, v); @@ -50,19 +59,12 @@ arr_t* array_duplicate(const arr_t* s) { if (s->type >= VTYPE_STRING) { void *p, *v, *e; - void (*init)(void*, const void*); + type_initializer init; x->mem = p = malloc(x->size*vtype_size(x->type)); v = s->mem; e = array_end(x); - - switch (s->type) { default: abort(); - case VTYPE_STRING: init = (void*)string_copy_init; break; - case VTYPE_ARRAY: init = (void*) array_copy_init; break; - case VTYPE_LIST: init = (void*) list_copy_init; break; - case VTYPE_MAP: init = (void*) map_copy_init; break; - case VTYPE_SET: init = (void*) vset_copy_init; break; - } + init = get_type_initializer(s->type); do { init(p, v); @@ -84,19 +86,12 @@ void array_copy_init(arr_t* x, const arr_t* s) { if (s->type >= VTYPE_STRING) { void *p, *v, *e; - void (*init)(void*, const void*); + type_initializer init; x->mem = p = malloc(x->size*vtype_size(x->type)); v = s->mem; e = array_end(x); - - switch (s->type) { default: abort(); - case VTYPE_STRING: init = (void*)string_copy_init; break; - case VTYPE_ARRAY: init = (void*) array_copy_init; break; - case VTYPE_LIST: init = (void*) list_copy_init; break; - case VTYPE_MAP: init = (void*) map_copy_init; break; - case VTYPE_SET: init = (void*) vset_copy_init; break; - } + init = get_type_initializer(s->type); do { init(p, v); @@ -125,19 +120,12 @@ size_t array_slice(arr_t* x, arr_t* s, ssize_t i, size_t n, _Bool cut) { if (!cut && s->type >= VTYPE_STRING) { void *p, *v, *e; - void (*init)(void*, const void*); + type_initializer init; - p = x->mem; - v = array_internal_at(s, i); - e = array_internal_at(x, n); - - switch (s->type) { default: abort(); - case VTYPE_STRING: init = (void*)string_copy_init; break; - case VTYPE_ARRAY: init = (void*) array_copy_init; break; - case VTYPE_LIST: init = (void*) list_copy_init; break; - case VTYPE_MAP: init = (void*) map_copy_init; break; - case VTYPE_SET: init = (void*) vset_copy_init; break; - } + p = x->mem; + v = array_internal_at(s, i); + e = array_internal_at(x, n); + init = get_type_initializer(s->type); do { init(p, v);