Skip to content

Commit

Permalink
fix code format and indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
GIC-de committed Nov 28, 2024
1 parent cee644a commit a9cb37e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 84 deletions.
24 changes: 9 additions & 15 deletions src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -309,34 +307,30 @@ 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*));

for (unsigned i = 0; i < table->size; i++) {
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;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/hashtable2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
76 changes: 37 additions & 39 deletions src/hb_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/pr_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions src/rb_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 8 additions & 11 deletions src/skiplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
10 changes: 3 additions & 7 deletions src/tree_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a9cb37e

Please sign in to comment.