libcdsb/src/dict/copy.c

147 lines
3.0 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/list.h"
#include "include.h"
static inline dnode_t* libcdsb_builtin_duplicate(const dnode_t* s, dnode_t* p) {
dnode_t* x = malloc(sizeof(*x));
x->prev = p;
x->key = vnode_duplicate(&s->key, s->key_type);
x->value = vnode_duplicate(&s->value, s->value_type);
x->key_type = s->key_type;
x->value_type = s->value_type;
return x;
}
/*#####################################################################################################################*/
dict_t dict_copy(const dict_t* s) {
dict_t x;
size_t i;
dnode_t *n;
x.capacity = i = s->capacity;
x.size = s->size;
x.nodes = calloc(x.capacity, sizeof(*x.nodes));
while (i--) {
n = s->nodes[i];
while (!is_null(n)) {
x.nodes[i] = libcdsb_builtin_duplicate(n, x.nodes[i]);
n = n->prev;
}
}
return x;
}
dict_t* dict_duplicate(const dict_t* s) {
dict_t *x = malloc(sizeof(*x));
size_t i;
dnode_t *n;
x->capacity = i = s->capacity;
x->size = s->size;
x->nodes = calloc(x->capacity, sizeof(*x->nodes));
while (i--) {
n = s->nodes[i];
while (!is_null(n)) {
x->nodes[i] = libcdsb_builtin_duplicate(n, x->nodes[i]);
n = n->prev;
}
}
return x;
}
void dict_copy_init(dict_t* x, const dict_t* s) {
size_t i;
dnode_t *n;
x->capacity = i = s->capacity;
x->size = s->size;
x->nodes = calloc(x->capacity, sizeof(*x->nodes));
while (i--) {
n = s->nodes[i];
while (!is_null(n)) {
x->nodes[i] = libcdsb_builtin_duplicate(n, x->nodes[i]);
n = n->prev;
}
}
}
vtype_list libcdsb_dict_copy_keys(const vtype_dict* s) {
vtype_list x;
dnode_t *c;
size_t i;
i = s->capacity;
list_init(&x);
while (i--) {
c = s->nodes[i];
while (!is_null(c)) {
libcdsb_list_insert(&x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1);
c = c->prev;
}
}
return x;
}
vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) {
vtype_list* x;
dnode_t *c;
size_t i;
x = malloc(sizeof(*x));
i = s->capacity;
list_init(x);
while (i--) {
c = s->nodes[i];
while (!is_null(c)) {
libcdsb_list_insert(x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1);
c = c->prev;
}
}
return x;
}
void libcdsb_dict_init_keys(vtype_list* x, const vtype_dict* s) {
dnode_t *c;
size_t i;
x = malloc(sizeof(*x));
i = s->capacity;
list_init(x);
while (i--) {
c = s->nodes[i];
while (!is_null(c)) {
libcdsb_list_insert(x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1);
c = c->prev;
}
}
}