Refactor usage of the stack_t initialization
This commit is contained in:
parent
e8d3b55ec9
commit
c9ec0d110f
@ -18,11 +18,12 @@ static inline void lnode_swap(lnode_t* s0, lnode_t* s1) {
|
||||
|
||||
|
||||
void list_sort(list_t* x) {
|
||||
stack_t z = { .prev = 0, .value = x->last};
|
||||
stack_t z;
|
||||
lnode_t *l;
|
||||
lnode_t *r;
|
||||
|
||||
stack_push(&z, x->first);
|
||||
stack_init(&z);
|
||||
stack_push_many(&z, 2, x->last, x->first);
|
||||
|
||||
while (z.value) {
|
||||
|
||||
|
@ -19,37 +19,35 @@ static inline hash_t mnode_hash(const mnode_t* s, vtype t) {
|
||||
|
||||
hash_t map_hash(const map_t* s) {
|
||||
|
||||
stack_t z;
|
||||
mnode_t *c0, *c1;
|
||||
hash_t hash, v;
|
||||
stack_t z;
|
||||
|
||||
if (mnode_is_empty(s->root)) return 0;
|
||||
if (mnode_is_empty(s->root))
|
||||
return 0;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root->left);
|
||||
|
||||
z.prev = 0;
|
||||
hash = 1;
|
||||
c1 = s->root;
|
||||
|
||||
if (!rbnode_is_empty(z.value = c1->left)) {
|
||||
if (!mnode_is_empty(c0 = stack_pop(&z))) {
|
||||
do {
|
||||
c0 = stack_pop(&z);
|
||||
++hash;
|
||||
|
||||
if (!mnode_is_empty(c0->right)) stack_push(&z, c1 = c0->right);
|
||||
if (!mnode_is_empty(c0->right)) stack_push(&z, c0->right);
|
||||
if (!mnode_is_empty(c0->left)) stack_push(&z, c1 = c0->left);
|
||||
} while (!is_null(z.value));
|
||||
} while (!is_null(c0 = stack_pop(&z)));
|
||||
}
|
||||
|
||||
v = mnode_hash(c1, s->type);
|
||||
c1 = s->root;
|
||||
stack_push(&z, s->root->right);
|
||||
|
||||
if (!rbnode_is_empty(z.value = c1->right)) {
|
||||
if (!mnode_is_empty(c0 = stack_pop(&z))) {
|
||||
do {
|
||||
c0 = stack_pop(&z);
|
||||
++hash;
|
||||
|
||||
if (!mnode_is_empty(c0->right)) stack_push(&z, c1 = c0->right);
|
||||
if (!mnode_is_empty(c0->left)) stack_push(&z, c1 = c0->left);
|
||||
} while (!is_null(z.value));
|
||||
if (!mnode_is_empty(c0->left)) stack_push(&z, c0->left);
|
||||
} while (!is_null(c0 = stack_pop(&z)));
|
||||
}
|
||||
|
||||
v += mnode_hash(c1, s->type);
|
||||
@ -98,10 +96,15 @@ void map_free(map_t* x) {
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
size_t map_size(const map_t* x) {
|
||||
stack_t z = { .prev = 0, .value = x->root };
|
||||
size_t n = 0;
|
||||
stack_t z;
|
||||
size_t n;
|
||||
rbnode_t* c;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, x->root);
|
||||
|
||||
n = 0;
|
||||
|
||||
if (!mnode_is_empty(x->root)) {
|
||||
while ((c = stack_pop(&z))) {
|
||||
++n;
|
||||
@ -116,20 +119,20 @@ size_t map_size(const map_t* x) {
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
int map_compare(const map_t* s0, const map_t* s1) {
|
||||
mnode_t *c0, *c1;
|
||||
stack_t z;
|
||||
int c = 0;
|
||||
mnode_t *c0, *c1;
|
||||
int cmp;
|
||||
|
||||
if (s0 == s1 || s0->root == s1->root)
|
||||
return 0;
|
||||
if (s0->type != s1->type)
|
||||
return s0->type - s1->type;
|
||||
|
||||
z.prev = 0;
|
||||
z.value = 0;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push_many(&z, 2, (void*)s1, (void*)s0);
|
||||
|
||||
cmp = 0;
|
||||
|
||||
do {
|
||||
c0 = stack_pop(&z);
|
||||
c1 = stack_pop(&z);
|
||||
@ -139,18 +142,18 @@ int map_compare(const map_t* s0, const map_t* s1) {
|
||||
stack_flush(&z);
|
||||
return mnode_is_empty(c0) ? -1 : 1;
|
||||
}
|
||||
} else if ((c = mnode_compare(c0, c1, s0->type))) {
|
||||
} else if ((cmp = mnode_compare(c0, c1, s0->type))) {
|
||||
if (c0->left == c1->right) { // == mnode_empty
|
||||
c = mnode_compare(c0->right, c1, s0->type);
|
||||
if (!c) c = mnode_compare(c0, c1->left, s0->type);
|
||||
cmp = mnode_compare(c0->right, c1, s0->type);
|
||||
if (!cmp) cmp = mnode_compare(c0, c1->left, s0->type);
|
||||
} else if (c0->right == c1->left) { // == mnode_empty
|
||||
c = mnode_compare(c0, c1->right, s0->type);
|
||||
if (!c) c = mnode_compare(c0->left, c1, s0->type);
|
||||
cmp = mnode_compare(c0, c1->right, s0->type);
|
||||
if (!cmp) cmp = mnode_compare(c0->left, c1, s0->type);
|
||||
}
|
||||
|
||||
if (c) {
|
||||
if (cmp) {
|
||||
stack_flush(&z);
|
||||
return c;
|
||||
return cmp;
|
||||
}
|
||||
} else stack_push_many(&z, 4, c1->right, c0->right, c1->left, c0->left);
|
||||
|
||||
|
@ -20,7 +20,10 @@ static inline mnode_t* mnode_duplicate(const mnode_t* s, mnode_t* p, const vtype
|
||||
map_t map_copy(const map_t* s) {
|
||||
|
||||
map_t x;
|
||||
stack_t z = { .prev = 0, .value = s->root };
|
||||
stack_t z;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root);
|
||||
|
||||
x.type = s->type;
|
||||
|
||||
@ -52,9 +55,13 @@ map_t map_copy(const map_t* s) {
|
||||
|
||||
map_t* map_duplicate(const map_t* s) {
|
||||
|
||||
map_t* x = malloc(sizeof(*x));
|
||||
stack_t z = { .prev = 0, .value = s->root };
|
||||
map_t* x;
|
||||
stack_t z;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root);
|
||||
|
||||
x = malloc(sizeof(*x));
|
||||
x->type = s->type;
|
||||
|
||||
if (!mnode_is_empty(s->root)) {
|
||||
@ -85,7 +92,10 @@ map_t* map_duplicate(const map_t* s) {
|
||||
|
||||
void map_copy_init(map_t* x, const map_t* s) {
|
||||
|
||||
stack_t z = { .prev = 0, .value = s->root };
|
||||
stack_t z;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root);
|
||||
|
||||
x->type = s->type;
|
||||
|
||||
|
@ -80,11 +80,17 @@ int libcdsb_map_find(map_t* x, const void* k, vtype t, void* _, map_access_callb
|
||||
|
||||
|
||||
int libcdsb_map_foreach(map_t* x, void* dt, map_access_callback callback, bool flush) {
|
||||
stack_t z = { .prev = 0, .value = x->root };
|
||||
int r = 0;
|
||||
stack_t z;
|
||||
int r;
|
||||
mnode_t* c;
|
||||
|
||||
if (mnode_is_empty(x->root)) return 0;
|
||||
stack_init(&z);
|
||||
stack_push(&z, x->root);
|
||||
|
||||
r = 0;
|
||||
|
||||
if (mnode_is_empty(x->root))
|
||||
return 0;
|
||||
|
||||
while ((c = stack_pop(&z))) {
|
||||
if ((r = callback(vnode_peek(&c->key, x->type), x->type, vnode_peek(&c->value, c->type), c->type, dt)))
|
||||
|
@ -18,37 +18,35 @@ static inline hash_t rbnode_hash(const rbnode_t* s, vtype t) {
|
||||
|
||||
hash_t vset_hash(const set_t* s) {
|
||||
|
||||
stack_t z;
|
||||
rbnode_t *c0, *c1;
|
||||
hash_t hash, v;
|
||||
stack_t z;
|
||||
|
||||
if (rbnode_is_empty(s->root)) return 0;
|
||||
if (rbnode_is_empty(s->root))
|
||||
return 0;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root->left);
|
||||
|
||||
z.prev = 0;
|
||||
hash = 1;
|
||||
c1 = s->root;
|
||||
|
||||
if (!rbnode_is_empty(z.value = c1->left)) {
|
||||
if (!rbnode_is_empty(c0 = stack_pop(&z))) {
|
||||
do {
|
||||
c0 = stack_pop(&z);
|
||||
++hash;
|
||||
|
||||
if (!rbnode_is_empty(c0->right)) stack_push(&z, c1 = c0->right);
|
||||
if (!rbnode_is_empty(c0->right)) stack_push(&z, c0->right);
|
||||
if (!rbnode_is_empty(c0->left)) stack_push(&z, c1 = c0->left);
|
||||
} while (!is_null(z.value));
|
||||
} while (!is_null(c0 = stack_pop(&z)));
|
||||
}
|
||||
|
||||
v = rbnode_hash(c1, s->type);
|
||||
c1 = s->root;
|
||||
stack_push(&z, s->root->right);
|
||||
|
||||
if (!rbnode_is_empty(z.value = c1->right)) {
|
||||
if (!rbnode_is_empty(c0 = stack_pop(&z))) {
|
||||
do {
|
||||
c0 = stack_pop(&z);
|
||||
++hash;
|
||||
|
||||
if (!rbnode_is_empty(c0->right)) stack_push(&z, c1 = c0->right);
|
||||
if (!rbnode_is_empty(c0->left)) stack_push(&z, c1 = c0->left);
|
||||
} while (!is_null(z.value));
|
||||
if (!rbnode_is_empty(c0->left)) stack_push(&z, c0->left);
|
||||
} while (!is_null(c0 = stack_pop(&z)));
|
||||
}
|
||||
|
||||
v += rbnode_hash(c1, s->type);
|
||||
@ -96,10 +94,15 @@ void vset_free(set_t* x) {
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
size_t vset_size(const set_t* x) {
|
||||
stack_t z = { .prev = 0, .value = x->root };
|
||||
size_t n = 0;
|
||||
stack_t z;
|
||||
size_t n;
|
||||
rbnode_t* c;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, x->root);
|
||||
|
||||
n = 0;
|
||||
|
||||
if (!rbnode_is_empty(x->root)) {
|
||||
while ((c = stack_pop(&z))) {
|
||||
++n;
|
||||
@ -114,20 +117,20 @@ size_t vset_size(const set_t* x) {
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
int vset_compare(const set_t* s0, const set_t* s1) {
|
||||
rbnode_t *c0, *c1;
|
||||
stack_t z;
|
||||
int c = 0;
|
||||
rbnode_t *c0, *c1;
|
||||
int cmp;
|
||||
|
||||
if (s0 == s1 || s0->root == s1->root)
|
||||
return 0;
|
||||
if (s0->type != s1->type)
|
||||
return s0->type - s1->type;
|
||||
|
||||
z.prev = 0;
|
||||
z.value = 0;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push_many(&z, 2, (void*)s1, (void*)s0);
|
||||
|
||||
cmp = 0;
|
||||
|
||||
do {
|
||||
c0 = stack_pop(&z);
|
||||
c1 = stack_pop(&z);
|
||||
@ -137,18 +140,18 @@ int vset_compare(const set_t* s0, const set_t* s1) {
|
||||
stack_flush(&z);
|
||||
return rbnode_is_empty(c0) ? -1 : 1;
|
||||
}
|
||||
} else if ((c = rbnode_compare(c0, c1, s0->type))) {
|
||||
} else if ((cmp = rbnode_compare(c0, c1, s0->type))) {
|
||||
if (c0->left == c1->right) { // == rbnode_empty
|
||||
c = rbnode_compare(c0->right, c1, s0->type);
|
||||
if (!c) c = rbnode_compare(c0, c1->left, s0->type);
|
||||
cmp = rbnode_compare(c0->right, c1, s0->type);
|
||||
if (!cmp) cmp = rbnode_compare(c0, c1->left, s0->type);
|
||||
} else if (c0->right == c1->left) { // == rbnode_empty
|
||||
c = rbnode_compare(c0, c1->right, s0->type);
|
||||
if (!c) c = rbnode_compare(c0->left, c1, s0->type);
|
||||
cmp = rbnode_compare(c0, c1->right, s0->type);
|
||||
if (!cmp) cmp = rbnode_compare(c0->left, c1, s0->type);
|
||||
}
|
||||
|
||||
if (c) {
|
||||
if (cmp) {
|
||||
stack_flush(&z);
|
||||
return c;
|
||||
return cmp;
|
||||
}
|
||||
} else stack_push_many(&z, 4, c1->right, c0->right, c1->left, c0->left);
|
||||
|
||||
|
@ -6,12 +6,16 @@
|
||||
|
||||
set_t vset_copy(const set_t* s) {
|
||||
|
||||
set_t x = { .type = s->type };
|
||||
stack_t z = { .prev = 0, .value = s->root };
|
||||
vtype t = s->type;
|
||||
set_t x;
|
||||
stack_t z;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root);
|
||||
|
||||
x.type = s->type;
|
||||
|
||||
if (!rbnode_is_empty(s->root)) {
|
||||
x.root = rbnode_create(vnode_duplicate(&s->root->value, t), rbnode_empty, 0);
|
||||
x.root = rbnode_create(vnode_duplicate(&s->root->value, s->type), rbnode_empty, 0);
|
||||
stack_push(&z, x.root);
|
||||
|
||||
do {
|
||||
@ -19,12 +23,12 @@ set_t vset_copy(const set_t* s) {
|
||||
rbnode_t *p1 = stack_pop(&z);
|
||||
|
||||
if (!rbnode_is_empty(p1->left)) {
|
||||
p0->left = rbnode_create(vnode_duplicate(&p1->left->value, t), p0, p1->left->colored);
|
||||
p0->left = rbnode_create(vnode_duplicate(&p1->left->value, s->type), p0, p1->left->colored);
|
||||
stack_push_many(&z, 2, p1->left, p0->left);
|
||||
}
|
||||
|
||||
if (!rbnode_is_empty(p1->right)) {
|
||||
p0->right = rbnode_create(vnode_duplicate(&p1->right->value, t), p0, p1->right->colored);
|
||||
p0->right = rbnode_create(vnode_duplicate(&p1->right->value, s->type), p0, p1->right->colored);
|
||||
stack_push_many(&z, 2, p1->right, p0->right);
|
||||
}
|
||||
|
||||
@ -38,12 +42,17 @@ set_t vset_copy(const set_t* s) {
|
||||
|
||||
set_t* vset_duplicate(const set_t* s) {
|
||||
|
||||
set_t* x = malloc(sizeof(*x));
|
||||
stack_t z = { .prev = 0, .value = s->root };
|
||||
vtype t = x->type = s->type;
|
||||
set_t* x;
|
||||
stack_t z;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root);
|
||||
|
||||
x = malloc(sizeof(*x));
|
||||
x->type = s->type;
|
||||
|
||||
if (!rbnode_is_empty(s->root)) {
|
||||
x->root = rbnode_create(vnode_duplicate(&s->root->value, t), rbnode_empty, 0);
|
||||
x->root = rbnode_create(vnode_duplicate(&s->root->value, s->type), rbnode_empty, 0);
|
||||
stack_push(&z, x->root);
|
||||
|
||||
do {
|
||||
@ -51,12 +60,12 @@ set_t* vset_duplicate(const set_t* s) {
|
||||
rbnode_t *p1 = stack_pop(&z);
|
||||
|
||||
if (!rbnode_is_empty(p1->left)) {
|
||||
p0->left = rbnode_create(vnode_duplicate(&p1->left->value, t), p0, p1->left->colored);
|
||||
p0->left = rbnode_create(vnode_duplicate(&p1->left->value, s->type), p0, p1->left->colored);
|
||||
stack_push_many(&z, 2, p1->left, p0->left);
|
||||
}
|
||||
|
||||
if (!rbnode_is_empty(p1->right)) {
|
||||
p0->right = rbnode_create(vnode_duplicate(&p1->right->value, t), p0, p1->right->colored);
|
||||
p0->right = rbnode_create(vnode_duplicate(&p1->right->value, s->type), p0, p1->right->colored);
|
||||
stack_push_many(&z, 2, p1->right, p0->right);
|
||||
}
|
||||
|
||||
@ -70,11 +79,15 @@ set_t* vset_duplicate(const set_t* s) {
|
||||
|
||||
void vset_copy_init(set_t* x, const set_t* s) {
|
||||
|
||||
stack_t z = { .prev = 0, .value = s->root };
|
||||
vtype t = x->type = s->type;
|
||||
stack_t z;
|
||||
|
||||
stack_init(&z);
|
||||
stack_push(&z, s->root);
|
||||
|
||||
x->type = s->type;
|
||||
|
||||
if (!rbnode_is_empty(s->root)) {
|
||||
x->root = rbnode_create(vnode_duplicate(&s->root->value, t), rbnode_empty, 0);
|
||||
x->root = rbnode_create(vnode_duplicate(&s->root->value, s->type), rbnode_empty, 0);
|
||||
stack_push(&z, x->root);
|
||||
|
||||
do {
|
||||
@ -82,12 +95,12 @@ void vset_copy_init(set_t* x, const set_t* s) {
|
||||
rbnode_t *p1 = stack_pop(&z);
|
||||
|
||||
if (!rbnode_is_empty(p1->left)) {
|
||||
p0->left = rbnode_create(vnode_duplicate(&p1->left->value, t), p0, p1->left->colored);
|
||||
p0->left = rbnode_create(vnode_duplicate(&p1->left->value, s->type), p0, p1->left->colored);
|
||||
stack_push_many(&z, 2, p1->left, p0->left);
|
||||
}
|
||||
|
||||
if (!rbnode_is_empty(p1->right)) {
|
||||
p0->right = rbnode_create(vnode_duplicate(&p1->right->value, t), p0, p1->right->colored);
|
||||
p0->right = rbnode_create(vnode_duplicate(&p1->right->value, s->type), p0, p1->right->colored);
|
||||
stack_push_many(&z, 2, p1->right, p0->right);
|
||||
}
|
||||
|
||||
|
@ -71,11 +71,17 @@ int libcdsb_vset_find(vtype_set* x, const void* v, vtype t, void* _, vset_access
|
||||
|
||||
|
||||
int libcdsb_vset_foreach(set_t* x, void* data, vset_access_callback callback, bool flush) {
|
||||
stack_t z = { .prev = 0, .value = x->root };
|
||||
int r = 0;
|
||||
stack_t z;
|
||||
int r;
|
||||
rbnode_t* c;
|
||||
|
||||
if (rbnode_is_empty(x->root)) return 0;
|
||||
stack_init(&z);
|
||||
stack_push(&z, x->root);
|
||||
|
||||
r = 0;
|
||||
|
||||
if (rbnode_is_empty(x->root))
|
||||
return 0;
|
||||
|
||||
while ((c = stack_pop(&z))) {
|
||||
if ((r = callback(vnode_peek(&c->value, x->type), x->type, data)))
|
||||
|
Loading…
Reference in New Issue
Block a user