141 lines
3.6 KiB
C
141 lines
3.6 KiB
C
/* This software is licensed by the MIT License, see LICENSE file */
|
|
/* Copyright © 2022 Gregory Lirent */
|
|
|
|
#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 };
|
|
|
|
if (s->size > 0) {
|
|
x.type = s->type;
|
|
x.size = s->size;
|
|
|
|
if (s->type >= VTYPE_STRING) {
|
|
void *p, *v, *e;
|
|
type_initializer init;
|
|
|
|
x.mem = p = malloc(x.size*vtype_size(x.type));
|
|
v = s->mem;
|
|
e = array_end(&x);
|
|
init = get_type_initializer(s->type);
|
|
|
|
do {
|
|
init(p, v);
|
|
p += vtype_size(x.type);
|
|
v += vtype_size(x.type);
|
|
} while (p < e);
|
|
|
|
} else x.mem = memndup(s->mem, x.size*vtype_size(x.type));
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
|
|
arr_t* array_duplicate(const arr_t* s) {
|
|
arr_t* x = malloc(sizeof(*x));
|
|
|
|
if (s->size > 0) {
|
|
x->type = s->type;
|
|
x->size = s->size;
|
|
|
|
if (s->type >= VTYPE_STRING) {
|
|
void *p, *v, *e;
|
|
type_initializer init;
|
|
|
|
x->mem = p = malloc(x->size*vtype_size(x->type));
|
|
v = s->mem;
|
|
e = array_end(x);
|
|
init = get_type_initializer(s->type);
|
|
|
|
do {
|
|
init(p, v);
|
|
p += vtype_size(x->type);
|
|
v += vtype_size(x->type);
|
|
} while (p < e);
|
|
|
|
} else x->mem = memndup(s->mem, x->size*vtype_size(x->type));
|
|
} else memset(x, 0, sizeof(*x));
|
|
|
|
return x;
|
|
}
|
|
|
|
|
|
void array_copy_init(arr_t* x, const arr_t* s) {
|
|
if (s->size > 0) {
|
|
x->type = s->type;
|
|
x->size = s->size;
|
|
|
|
if (s->type >= VTYPE_STRING) {
|
|
void *p, *v, *e;
|
|
type_initializer init;
|
|
|
|
x->mem = p = malloc(x->size*vtype_size(x->type));
|
|
v = s->mem;
|
|
e = array_end(x);
|
|
init = get_type_initializer(s->type);
|
|
|
|
do {
|
|
init(p, v);
|
|
p += vtype_size(x->type);
|
|
v += vtype_size(x->type);
|
|
} while (p < e);
|
|
|
|
} else x->mem = memndup(s->mem, x->size*vtype_size(x->type));
|
|
} else memset(x, 0, sizeof(*x));
|
|
}
|
|
|
|
|
|
size_t array_slice(arr_t* x, arr_t* s, ssize_t i, size_t n, _Bool cut) {
|
|
|
|
if (!n || !s->size) {
|
|
memset(x, 0, sizeof(*x));
|
|
return 0;
|
|
}
|
|
|
|
if (i < 0 && (i += s->size) < 0) i = 0;
|
|
if (i + n > s->size) n = s->size - (i + 1);
|
|
|
|
x->type = s->type;
|
|
x->size = n;
|
|
x->mem = malloc(x->size*vtype_size(x->type));
|
|
|
|
if (!cut && s->type >= VTYPE_STRING) {
|
|
void *p, *v, *e;
|
|
type_initializer init;
|
|
|
|
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);
|
|
p += vtype_size(x->type);
|
|
v += vtype_size(x->type);
|
|
} while (p < e);
|
|
} else memcpy(x->mem, array_internal_at(s, i), n*vtype_size(x->type));
|
|
|
|
if (cut) array_cut(s, i, n);
|
|
|
|
return n;
|
|
}
|