libcdsb/src/set/copy.c

60 lines
1.9 KiB
C
Raw Normal View History

2022-06-06 11:23:33 +03:00
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/set.h"
#include "../__internal/rbtree.h"
/*#####################################################################################################################*/
static void copy_child(rbnode_t* x, vtype t, const rbnode_t* l, const rbnode_t* r) {
if (!rbnode_is_empty(l)) {
x->left = rbnode_create(vnode_duplicate(&l->value, t), x, l->colored);
copy_child(x->left, t, l->left, l->right);
} else x->left = rbnode_empty;
if (!rbnode_is_empty(r)) {
x->left = rbnode_create(vnode_duplicate(&r->value, t), x, r->colored);
copy_child(x->right, t, r->right, r->right);
} else x->right = rbnode_empty;
}
/*#####################################################################################################################*/
vtype_set vset_copy(const vtype_set* s) {
set_t x = { .type = s->type };
if (!rbnode_is_empty(s->root)) {
x.root = rbnode_create(vnode_duplicate(&s->root->value, s->type), rbnode_empty, 0);
copy_child(x.root, s->type, s->root->left, s->root->right);
} else x.root = rbnode_empty;
return x;
}
vtype_set* vset_duplicate(const vtype_set* s) {
set_t* x = malloc(sizeof(*x));
x->type = s->type;
if (!rbnode_is_empty(s->root)) {
x->root = rbnode_create(vnode_duplicate(&s->root->value, s->type), rbnode_empty, 0);
copy_child(x->root, s->type, s->root->left, s->root->right);
} else x->root = rbnode_empty;
return x;
}
void vset_copy_init(vtype_set* x, const vtype_set* s) {
x->type = s->type;
if (!rbnode_is_empty(s->root)) {
x->root = rbnode_create(vnode_duplicate(&s->root->value, s->type), rbnode_empty, 0);
copy_child(x->root, s->type, s->root->left, s->root->right);
} else x->root = rbnode_empty;
}