42 lines
970 B
C
42 lines
970 B
C
/* This software is licensed by the MIT License, see LICENSE file */
|
|
/* Copyright © 2022 Gregory Lirent */
|
|
|
|
#include "include.h"
|
|
|
|
void map_init(map_t* x, vtype t) {
|
|
x->root = mnode_empty;
|
|
x->type = t;
|
|
}
|
|
|
|
|
|
void map_free(map_t* x) {
|
|
mnode_t *t, *c;
|
|
|
|
c = x->root;
|
|
|
|
while (!mnode_is_empty(x->root)) {
|
|
if (!mnode_is_empty(c->left)) {
|
|
c = c->left;
|
|
} else if (!mnode_is_empty(c->right)) {
|
|
c = c->right;
|
|
} else if (!mnode_is_root(c)) {
|
|
vnode_free(&c->key, x->type);
|
|
vnode_free(&c->value, c->type);
|
|
|
|
t = c;
|
|
c = c->parent;
|
|
|
|
if (t == c->left) c->left = mnode_empty;
|
|
else c->right = mnode_empty;
|
|
|
|
free(t);
|
|
} else {
|
|
vnode_free(&c->key, x->type);
|
|
vnode_free(&c->value, c->type);
|
|
x->root = mnode_empty;
|
|
}
|
|
}
|
|
|
|
x->type = 0;
|
|
}
|