libcdsb/src/dict/copy.c

83 lines
1.9 KiB
C
Raw Normal View History

2022-08-18 02:27:26 +03:00
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
/*#####################################################################################################################*/
static inline dnode_t* dnode_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] = dnode_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] = dnode_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] = dnode_duplicate(n, x->nodes[i]);
n = n->prev;
}
}
}