Refactor array base

This commit is contained in:
Gregory Lirent 2022-08-17 22:20:40 +03:00
parent 9f83f59502
commit 1b815613a4
2 changed files with 46 additions and 51 deletions

View File

@ -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);

View File

@ -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);