From f0426cfd5580d3f3b638cb078c9392c78b9b2559 Mon Sep 17 00:00:00 2001 From: tqfx Date: Tue, 29 Oct 2024 00:05:38 +0800 Subject: [PATCH] fix -Wdeclaration-after-statement --- src/avl.c | 25 ++++++++++++------------- src/rbt.c | 4 ++-- src/vec.c | 24 ++++++++++++++---------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/avl.c b/src/avl.c index bd24d56..774f8ff 100644 --- a/src/avl.c +++ b/src/avl.c @@ -204,14 +204,13 @@ Indeed, a single node insertion cannot require that more than one (single or dou static A_INLINE a_bool a_avl_handle_growth(a_avl *root, a_avl_node *parent, a_avl_node *node, int sign) { int const cur_factor = a_avl_factor(parent); + int const new_factor = cur_factor + sign; if (cur_factor == 0) { /* `parent` is still sufficiently balanced (-1 or +1 balance factor), but must have increased in height. Continue up the tree. */ a_avl_set_factor(parent, sign); return A_FALSE; } - - int const new_factor = cur_factor + sign; if (new_factor == 0) { /* `parent` is now perfectly balanced (0 balance factor). It cannot have increased in height, so there is nothing more to do. */ @@ -329,20 +328,21 @@ void a_avl_insert_adjust(a_avl *root, a_avl_node *node) /* The subtree rooted at parent increased in height by 1. */ - a_bool done; - do { + for (a_bool ok;;) + { node = parent; parent = a_avl_parent(node); - if (!parent) { return; } + if (!parent) { break; } if (parent->left == node) { - done = a_avl_handle_growth(root, parent, node, -1); + ok = a_avl_handle_growth(root, parent, node, -1); } else { - done = a_avl_handle_growth(root, parent, node, +1); + ok = a_avl_handle_growth(root, parent, node, +1); } - } while (!done); + if (ok) { break; } + } } /* @@ -373,6 +373,8 @@ Also in the latter case, *left will be set. static A_INLINE a_avl_node *a_avl_handle_shrink(a_avl *root, a_avl_node *parent, int sign, a_bool *left) { int const cur_factor = a_avl_factor(parent); + int const new_factor = cur_factor + sign; + a_avl_node *node; if (cur_factor == 0) { /* @@ -382,9 +384,6 @@ static A_INLINE a_avl_node *a_avl_handle_shrink(a_avl *root, a_avl_node *parent, a_avl_set_factor(parent, sign); return A_NULL; } - - a_avl_node *node; - int const new_factor = cur_factor + sign; if (new_factor == 0) { /* @@ -602,10 +601,10 @@ a_avl_node *a_avl_insert(a_avl *root, a_avl_node *node, int (*cmp)(void const *, { a_avl_node *parent = root->node; a_avl_node **link = &root->node; - while (*link) + for (int res; *link;) { parent = *link; - int const res = cmp(node, parent); + res = cmp(node, parent); if (res < 0) { link = &parent->left; diff --git a/src/rbt.c b/src/rbt.c index d349628..a8f69ae 100644 --- a/src/rbt.c +++ b/src/rbt.c @@ -526,10 +526,10 @@ a_rbt_node *a_rbt_insert(a_rbt *root, a_rbt_node *node, int (*cmp)(void const *, { a_rbt_node *parent = root->node; a_rbt_node **link = &root->node; - while (*link) + for (int res; *link;) { parent = *link; - int const res = cmp(node, parent); + res = cmp(node, parent); if (res < 0) { link = &parent->left; diff --git a/src/vec.c b/src/vec.c index 8544e11..cea092e 100644 --- a/src/vec.c +++ b/src/vec.c @@ -24,12 +24,12 @@ static int a_vec_alloc(a_vec *ctx, a_size num) { if (ctx->mem_ <= num) { + void *ptr; a_size mem = ctx->mem_; do { mem += (mem >> 1) + 1; } while (mem < num); - a_size const siz = a_size_up(sizeof(void *), ctx->siz_ * mem); - void *ptr = a_alloc(ctx->ptr_, siz); + ptr = a_alloc(ctx->ptr_, a_size_up(sizeof(void *), ctx->siz_ * mem)); if (A_UNLIKELY(!ptr)) { return A_FAILURE; } ctx->ptr_ = ptr; ctx->mem_ = mem; @@ -269,14 +269,18 @@ void *a_vec_remove(a_vec *ctx, a_size idx) { if (ctx->num_ && idx < ctx->num_ - 1) { - if (A_UNLIKELY(a_vec_alloc(ctx, ctx->num_))) { return A_NULL; } - a_byte *const ptr = (a_byte *)ctx->ptr_ + ctx->siz_ * ctx->num_; - a_byte *const dst = (a_byte *)ctx->ptr_ + ctx->siz_ * (idx + 0); - a_byte *const src = (a_byte *)ctx->ptr_ + ctx->siz_ * (idx + 1); - a_copy(ptr, dst, ctx->siz_); - a_move(dst, src, (a_size)(ptr - src)); - --ctx->num_; - return ptr; + if (a_vec_alloc(ctx, ctx->num_) == A_SUCCESS) + { + a_byte *const ptr = (a_byte *)ctx->ptr_ + ctx->siz_ * ctx->num_; + a_byte *const dst = (a_byte *)ctx->ptr_ + ctx->siz_ * (idx + 0); + a_byte *const src = (a_byte *)ctx->ptr_ + ctx->siz_ * (idx + 1); + a_size const siz = (a_size)(ptr - src); + a_copy(ptr, dst, ctx->siz_); + a_move(dst, src, siz); + --ctx->num_; + return ptr; + } + return A_NULL; } return ctx->num_ ? a_vec_dec_(ctx) : A_NULL; } -- 2.11.4.GIT