From a9cb37edbb7c993906397ab42d33d995ba605811 Mon Sep 17 00:00:00 2001 From: Christian Giese Date: Thu, 28 Nov 2024 16:33:32 +0000 Subject: [PATCH] fix code format and indentation --- src/hashtable.c | 24 ++++++--------- src/hashtable2.c | 6 ++-- src/hb_tree.c | 76 +++++++++++++++++++++++------------------------ src/pr_tree.c | 4 +-- src/rb_tree.c | 8 ++--- src/skiplist.c | 19 +++++------- src/tree_common.c | 10 ++----- 7 files changed, 63 insertions(+), 84 deletions(-) diff --git a/src/hashtable.c b/src/hashtable.c index 973033c..55974ee 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -254,8 +254,7 @@ hashtable_clear(hashtable* table, dict_delete_func delete_func) hash_node* node = table->table[slot]; while (node != NULL) { hash_node* next = node->next; - if (delete_func) - delete_func(node->key, node->datum); + if (delete_func) delete_func(node->key, node->datum); FREE(node); node = next; } @@ -274,8 +273,7 @@ hashtable_traverse(hashtable* table, dict_visit_func visit, void* user_data) for (unsigned i = 0; i < table->size; i++) { for (hash_node* node = table->table[i]; node; node = node->next) { ++count; - if (!visit(node->key, node->datum, user_data)) - return count; + if (!visit(node->key, node->datum, user_data)) return count; } } return count; @@ -309,13 +307,11 @@ hashtable_resize(hashtable* table, unsigned new_size) ASSERT(new_size > 0); new_size = dict_prime_geq(new_size); - if (table->size == new_size) - return true; + if (table->size == new_size) return true; /* TODO: investigate whether using realloc would be advantageous. */ hash_node** ntable = MALLOC(new_size * sizeof(hash_node*)); - if (!ntable) - return false; + if (!ntable) return false; memset(ntable, 0, new_size * sizeof(hash_node*)); @@ -323,20 +319,18 @@ hashtable_resize(hashtable* table, unsigned new_size) for (hash_node* node = table->table[i]; node;) { hash_node* const next = node->next; const unsigned mhash = node->hash % new_size; - hash_node* search = ntable[mhash]; hash_node* prev = NULL; while (search && node->hash >= search->hash) { - prev = search; - search = search->next; + prev = search; + search = search->next; } if ((node->next = search) != NULL) - search->prev = node; + search->prev = node; if ((node->prev = prev) != NULL) - prev->next = node; + prev->next = node; else - ntable[mhash] = node; - + ntable[mhash] = node; node = next; } } diff --git a/src/hashtable2.c b/src/hashtable2.c index 1283bf1..580d1c6 100644 --- a/src/hashtable2.c +++ b/src/hashtable2.c @@ -308,8 +308,7 @@ hashtable2_clear(hashtable2* table, dict_delete_func delete_func) hash_node *const end = table->table + table->size; for (; node != end; ++node) { if (node->hash) { - if (delete_func) - delete_func(node->key, node->datum); + if (delete_func) delete_func(node->key, node->datum); node->key = node->datum = NULL; node->hash = 0; } @@ -328,8 +327,7 @@ hashtable2_traverse(hashtable2* table, dict_visit_func visit, void* user_data) for (hash_node *const end = table->table + table->size; node != end; ++node) { if (node->hash) { ++count; - if (!visit(node->key, node->datum, user_data)) - break; + if (!visit(node->key, node->datum, user_data)) break; } } return count; diff --git a/src/hb_tree.c b/src/hb_tree.c index 071f9c0..6fdbce8 100644 --- a/src/hb_tree.c +++ b/src/hb_tree.c @@ -149,10 +149,7 @@ hb_tree_clear(hb_tree* tree, dict_delete_func delete_func) node = node->llink ? node->llink : node->rlink; continue; } - - if (delete_func) - delete_func(node->key, node->datum); - + if (delete_func) delete_func(node->key, node->datum); hb_node* const parent = PARENT(node); FREE(node); *(parent ? (parent->llink == node ? &parent->llink : &parent->rlink) : &tree->root) = NULL; @@ -203,7 +200,7 @@ rotate_r(hb_tree* restrict const tree, hb_node* restrict const q) /* q->parent <- ql; q->bal <- -(ql_bal == 0); */ q->bal = (intptr_t)ql | ((ql_bal == 0) << 1); if ((q->llink = qlr) != NULL) - qlr->bal = (intptr_t)q | (qlr->bal & BAL_MASK); /* qlr->parent <- q; */ + qlr->bal = (intptr_t)q | (qlr->bal & BAL_MASK); /* qlr->parent <- q; */ /* ql->parent <- qp; ql->bal <- (ql_bal == 0); */ ql->bal = (intptr_t)qp | (ql_bal == 0); @@ -242,7 +239,7 @@ rotate_rl(hb_tree* restrict const tree, hb_node* restrict const q) /* qr->parent <- qrl; qr->bal <- (qrl_bal == -1); */ qr->bal = (intptr_t)qrl | (qrl_bal == 2); if ((qr->llink = qrlr) != NULL) - qrlr->bal = (intptr_t)qr | (qrlr->bal & BAL_MASK); + qrlr->bal = (intptr_t)qr | (qrlr->bal & BAL_MASK); } /* LR: rotate |q->llink| left, then rotate |q| right. */ @@ -284,16 +281,17 @@ hb_tree_insert(hb_tree* tree, void* key) hb_node* node = tree->root; hb_node* parent = NULL; hb_node* q = NULL; + while (node) { - cmp = tree->cmp_func(key, node->key); - if (cmp < 0) { - parent = node; node = node->llink; - } else if (cmp > 0) { - parent = node; node = node->rlink; - } else - return (dict_insert_result) { &node->datum, false }; - if (parent->bal & BAL_MASK) - q = parent; + cmp = tree->cmp_func(key, node->key); + if (cmp < 0) { + parent = node; node = node->llink; + } else if (cmp > 0) { + parent = node; node = node->rlink; + } else + return (dict_insert_result) { &node->datum, false }; + if (parent->bal & BAL_MASK) + q = parent; } hb_node* const add = node = node_new(key); @@ -313,9 +311,9 @@ hb_tree_insert(hb_tree* tree, void* key) while (parent != q) { ASSERT((parent->bal & BAL_MASK) == 0); if (parent->llink == node) - parent->bal |= 2; + parent->bal |= 2; else - parent->bal |= 1; + parent->bal |= 1; node = parent; parent = PARENT(parent); } @@ -324,11 +322,11 @@ hb_tree_insert(hb_tree* tree, void* key) if (q->llink == node) { if (BAL_NEG(q)) { /* if q->balance == -1 */ if (BAL_POS(q->llink)) { /* if q->llink->balance == +1 */ - tree->rotation_count += 2; - rotate_lr(tree, q); + tree->rotation_count += 2; + rotate_lr(tree, q); } else { - tree->rotation_count += 1; - ASSERT(!rotate_r(tree, q)); + tree->rotation_count += 1; + ASSERT(!rotate_r(tree, q)); } } else { /* else, q->balance == +1 */ ASSERT(BAL_POS(q)); @@ -338,11 +336,11 @@ hb_tree_insert(hb_tree* tree, void* key) ASSERT(q->rlink == node); if (BAL_POS(q)) { /* if q->balance == +1 */ if (BAL_NEG(q->rlink)) { /* if q->rlink->balance == -1 */ - tree->rotation_count += 2; - rotate_rl(tree, q); + tree->rotation_count += 2; + rotate_rl(tree, q); } else { - tree->rotation_count += 1; - ASSERT(!rotate_l(tree, q)); + tree->rotation_count += 1; + ASSERT(!rotate_l(tree, q)); } } else { /* else, q->balance == -1 */ ASSERT(BAL_NEG(q)); @@ -398,22 +396,22 @@ remove_node(hb_tree* tree, hb_node* node) if (left) { ASSERT(p->llink == node); if (BAL_POS(p)) { /* if p->balance == +1 */ - if (BAL_NEG(p->rlink)) { /* if p->rlink->balance == -1 */ - rotations += 2; - rotate_rl(tree, p); - } else { - rotations += 1; - if (rotate_l(tree, p)) - break; - } - node = PARENT(p); + if (BAL_NEG(p->rlink)) { /* if p->rlink->balance == -1 */ + rotations += 2; + rotate_rl(tree, p); + } else { + rotations += 1; + if (rotate_l(tree, p)) + break; + } + node = PARENT(p); } else if (BAL_NEG(p)) { /* else if p->balance == -1 */ - p->bal &= ~BAL_MASK; /* p->balance <- 0 */ - node = p; + p->bal &= ~BAL_MASK; /* p->balance <- 0 */ + node = p; } else { /* else, p->balance == 0 */ - ASSERT((p->bal & BAL_MASK) == 0); - p->bal |= 1; /* p->balance <- +1 */ - break; + ASSERT((p->bal & BAL_MASK) == 0); + p->bal |= 1; /* p->balance <- +1 */ + break; } } else { ASSERT(p->rlink == node); diff --git a/src/pr_tree.c b/src/pr_tree.c index 4fd3bcb..24979f3 100644 --- a/src/pr_tree.c +++ b/src/pr_tree.c @@ -225,13 +225,13 @@ fixup(pr_tree* tree, pr_node* node) lr->llink = l; l->parent = lr; if ((l->rlink = a) != NULL) - a->parent = l; + a->parent = l; pr_node* const b = lr->rlink; lr->rlink = node; node->parent = lr; if ((node->llink = b) != NULL) - b->parent = node; + b->parent = node; lr->parent = parent; *(parent ? (parent->llink == node ? &parent->llink : &parent->rlink) : &tree->root) = lr; diff --git a/src/rb_tree.c b/src/rb_tree.c index 2e4537a..a534b49 100644 --- a/src/rb_tree.c +++ b/src/rb_tree.c @@ -156,10 +156,7 @@ rb_tree_clear(rb_tree* tree, dict_delete_func delete_func) node = node->llink ? node->llink : node->rlink; continue; } - - if (delete_func) - delete_func(node->key, node->datum); - + if (delete_func) delete_func(node->key, node->datum); rb_node* const parent = PARENT(node); FREE(node); *(parent ? (parent->llink == node ? &parent->llink : &parent->rlink) : &tree->root) = NULL; @@ -394,8 +391,7 @@ rb_tree_traverse(rb_tree* tree, dict_visit_func visit, void* user_data) rb_node* node = tree_node_min(tree->root); for (; node != NULL; node = node_next(node)) { ++count; - if (!visit(node->key, node->datum, user_data)) - break; + if (!visit(node->key, node->datum, user_data)) break; } return count; } diff --git a/src/skiplist.c b/src/skiplist.c index 6b91cea..64f04a1 100644 --- a/src/skiplist.c +++ b/src/skiplist.c @@ -189,14 +189,14 @@ skiplist_insert(skiplist* list, void* key) for (;;) { skip_node* const y = x->link[k]; if (!y) - break; + break; const int cmp = list->cmp_func(key, y->key); if (cmp < 0) { - while (k > 0 && x->link[k - 1] == y) - update[k--] = x; - break; + while (k > 0 && x->link[k - 1] == y) + update[k--] = x; + break; } else if (cmp == 0) - return (dict_insert_result) { &y->datum, false }; + return (dict_insert_result) { &y->datum, false }; x = y; } update[k] = x; @@ -389,8 +389,7 @@ skiplist_remove(skiplist* list, const void* key) for (unsigned k = 0; k <= list->top_link; k++) { ASSERT(update[k] != NULL); ASSERT(update[k]->link_count > k); - if (update[k]->link[k] != x) - break; + if (update[k]->link[k] != x) break; update[k]->link[k] = x->link[k]; } if (x->prev) @@ -411,8 +410,7 @@ skiplist_clear(skiplist* list, dict_delete_func delete_func) skip_node* node = list->head->link[0]; while (node) { skip_node* next = node->link[0]; - if (delete_func) - delete_func(node->key, node->datum); + if (delete_func) delete_func(node->key, node->datum); FREE(node); node = next; } @@ -432,8 +430,7 @@ skiplist_traverse(skiplist* list, dict_visit_func visit, void* user_data) size_t count = 0; for (skip_node* node = list->head->link[0]; node; node = node->link[0]) { ++count; - if (!visit(node->key, node->datum, user_data)) - break; + if (!visit(node->key, node->datum, user_data)) break; } return count; } diff --git a/src/tree_common.c b/src/tree_common.c index 0c81629..cf6b738 100644 --- a/src/tree_common.c +++ b/src/tree_common.c @@ -49,7 +49,7 @@ tree_node_rot_left(void* Tree, void* Node) tree_node* const nr = n->rlink; ASSERT(nr != NULL); if ((n->rlink = nr->llink) != NULL) - n->rlink->parent = n; + n->rlink->parent = n; nr->llink = n; tree_node* p = n->parent; @@ -259,8 +259,7 @@ tree_traverse(void* Tree, dict_visit_func visit, void* user_data) tree_node* node = tree_node_min(t->root); do { ++count; - if (!visit(node->key, node->datum, user_data)) - break; + if (!visit(node->key, node->datum, user_data)) break; node = tree_node_next(node); } while (node); } @@ -310,10 +309,7 @@ tree_clear(void* Tree, dict_delete_func delete_func) node = node->llink ? node->llink : node->rlink; continue; } - - if (delete_func) - delete_func(node->key, node->datum); - + if (delete_func) delete_func(node->key, node->datum); tree_node* const parent = node->parent; FREE(node); *(parent ? (parent->llink == node ? &parent->llink : &parent->rlink) : &t->root) = NULL;