libcdsb/src/set/base.c

65 lines
1.7 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"
2022-08-14 18:24:16 +03:00
/*#####################################################################################################################*/
hash_t vset_hash(const set_t* s) {
return rbtree_hash(s->root, nullptr, (void*)&s->type) + VTYPE_SET;
}
2022-06-06 11:23:33 +03:00
2022-06-08 09:58:49 +03:00
void vset_init(set_t* x, vtype t) {
2022-06-06 11:23:33 +03:00
x->root = rbnode_empty;
x->type = t;
}
2022-06-08 09:58:49 +03:00
void vset_free(set_t* x) {
2022-08-14 18:24:16 +03:00
rbtree_free(x->root, nullptr, &x->type);
2022-06-06 11:23:33 +03:00
2022-08-14 18:24:16 +03:00
x->root = rbnode_empty;
x->type = 0;
}
2022-08-14 18:24:16 +03:00
/*#####################################################################################################################*/
2022-06-06 11:23:33 +03:00
2022-08-14 18:24:16 +03:00
size_t vset_size(const set_t* x) {
return rbtree_size(x->root);
}
2022-06-06 11:23:33 +03:00
2022-08-14 18:24:16 +03:00
/*#####################################################################################################################*/
2022-06-06 11:23:33 +03:00
2022-08-14 18:24:16 +03:00
int vset_compare(const set_t* s0, const set_t* s1) {
if (s0 == s1) return 0;
if (s0->type != s1->type) return s0->type - s1->type;
2022-06-06 11:23:33 +03:00
2022-08-14 18:24:16 +03:00
return rbtree_compare(s0->root, s1->root, nullptr, (void*)&s0->type);
2022-06-06 11:23:33 +03:00
}
2022-08-14 18:24:16 +03:00
/*#####################################################################################################################*/
2022-06-06 11:23:33 +03:00
2022-08-14 18:24:16 +03:00
set_t vset_copy(const set_t* s) {
set_t x;
x.type = s->type;
x.root = rbtree_duplicate(s->root, nullptr, &x.type);
return x;
2022-06-06 11:23:33 +03:00
}
2022-08-14 18:24:16 +03:00
set_t* vset_duplicate(const set_t* s) {
set_t *x = malloc(sizeof(*x));
2022-06-06 11:23:33 +03:00
2022-08-14 18:24:16 +03:00
x->type = s->type;
x->root = rbtree_duplicate(s->root, nullptr, &x->type);
2022-08-14 18:24:16 +03:00
return x;
}
2022-08-14 18:24:16 +03:00
void vset_copy_init(set_t* x, const set_t* s) {
x->type = s->type;
x->root = rbtree_duplicate(s->root, nullptr, &x->type);
2022-06-06 11:23:33 +03:00
}