From 4d85d982d66375b1f8d333aa14e9bb8246b08bcc Mon Sep 17 00:00:00 2001 From: Michel Lang Date: Fri, 18 Nov 2016 11:17:39 +0100 Subject: [PATCH] cleanup c code --- src/Makevars | 7 - src/avl.c | 812 +++++++++++++++++++++++++-------------------------- src/avl.h | 30 +- src/hv.c | 176 ++--------- src/hv.h | 6 +- src/macros.h | 6 +- 6 files changed, 457 insertions(+), 580 deletions(-) delete mode 100644 src/Makevars diff --git a/src/Makevars b/src/Makevars deleted file mode 100644 index 66950e623..000000000 --- a/src/Makevars +++ /dev/null @@ -1,7 +0,0 @@ -all: $(SHLIB) - -hv.o: hv.c - $(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) -DVARIANT=4 -c -o hv.o hv.c - -avl.o: avl.c - $(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) -DVARIANT=4 -c -o avl.o avl.c diff --git a/src/avl.c b/src/avl.c index 5771b97e2..4cb198daf 100644 --- a/src/avl.c +++ b/src/avl.c @@ -51,38 +51,38 @@ static void avl_rebalance(avl_tree_t *, avl_node_t *); #ifndef AVL_DEPTH /* Also known as ffs() (from BSD) */ static int lg(unsigned int u) { - int r = 1; - if(!u) return 0; - if(u & 0xffff0000) { u >>= 16; r += 16; } - if(u & 0x0000ff00) { u >>= 8; r += 8; } - if(u & 0x000000f0) { u >>= 4; r += 4; } - if(u & 0x0000000c) { u >>= 2; r += 2; } - if(u & 0x00000002) r++; - return r; + int r = 1; + if(!u) return 0; + if(u & 0xffff0000) { u >>= 16; r += 16; } + if(u & 0x0000ff00) { u >>= 8; r += 8; } + if(u & 0x000000f0) { u >>= 4; r += 4; } + if(u & 0x0000000c) { u >>= 2; r += 2; } + if(u & 0x00000002) r++; + return r; } #endif static int avl_check_balance(avl_node_t *avlnode) { #ifdef AVL_DEPTH - int d; - d = R_DEPTH(avlnode) - L_DEPTH(avlnode); - return d<-1?-1:d>1?1:0; + int d; + d = R_DEPTH(avlnode) - L_DEPTH(avlnode); + return d<-1?-1:d>1?1:0; #else -/* int d; - * d = lg(R_COUNT(avlnode)) - lg(L_COUNT(avlnode)); - * d = d<-1?-1:d>1?1:0; +/* int d; + * d = lg(R_COUNT(avlnode)) - lg(L_COUNT(avlnode)); + * d = d<-1?-1:d>1?1:0; */ #ifdef AVL_COUNT - int pl, r; + int pl, r; - pl = lg(L_COUNT(avlnode)); - r = R_COUNT(avlnode); + pl = lg(L_COUNT(avlnode)); + r = R_COUNT(avlnode); - if(r>>pl+1) - return 1; - if(pl<2 || r>>pl-2) - return 0; - return -1; + if(r>>pl+1) + return 1; + if(pl<2 || r>>pl-2) + return 0; + return -1; #else #error No balancing possible. #endif @@ -91,78 +91,78 @@ static int avl_check_balance(avl_node_t *avlnode) { #ifdef AVL_COUNT unsigned int avl_count(const avl_tree_t *avltree) { - return NODE_COUNT(avltree->top); + return NODE_COUNT(avltree->top); } avl_node_t *avl_at(const avl_tree_t *avltree, unsigned int index) { - avl_node_t *avlnode; - unsigned int c; - - avlnode = avltree->top; - - while(avlnode) { - c = L_COUNT(avlnode); - - if(index < c) { - avlnode = avlnode->left; - } else if(index > c) { - avlnode = avlnode->right; - index -= c+1; - } else { - return avlnode; - } - } - return NULL; + avl_node_t *avlnode; + unsigned int c; + + avlnode = avltree->top; + + while(avlnode) { + c = L_COUNT(avlnode); + + if(index < c) { + avlnode = avlnode->left; + } else if(index > c) { + avlnode = avlnode->right; + index -= c+1; + } else { + return avlnode; + } + } + return NULL; } unsigned int avl_index(const avl_node_t *avlnode) { - avl_node_t *next; - unsigned int c; + avl_node_t *next; + unsigned int c; - c = L_COUNT(avlnode); + c = L_COUNT(avlnode); - while((next = avlnode->parent)) { - if(avlnode == next->right) - c += L_COUNT(next) + 1; - avlnode = next; - } + while((next = avlnode->parent)) { + if(avlnode == next->right) + c += L_COUNT(next) + 1; + avlnode = next; + } - return c; + return c; } #endif int avl_search_closest(const avl_tree_t *avltree, const void *item, avl_node_t **avlnode) { - avl_node_t *node; - avl_compare_t cmp; - int c; - - if(!avlnode) - avlnode = &node; - - node = avltree->top; - - if(!node) - return *avlnode = NULL, 0; - - cmp = avltree->cmp; - - for(;;) { - c = cmp(item, node->item); - - if(c < 0) { - if(node->left) - node = node->left; - else - return *avlnode = node, -1; - } else if(c > 0) { - if(node->right) - node = node->right; - else - return *avlnode = node, 1; - } else { - return *avlnode = node, 0; - } - } + avl_node_t *node; + avl_compare_t cmp; + int c; + + if(!avlnode) + avlnode = &node; + + node = avltree->top; + + if(!node) + return *avlnode = NULL, 0; + + cmp = avltree->cmp; + + for(;;) { + c = cmp(item, node->item); + + if(c < 0) { + if(node->left) + node = node->left; + else + return *avlnode = node, -1; + } else if(c > 0) { + if(node->right) + node = node->right; + else + return *avlnode = node, 1; + } else { + return *avlnode = node, 0; + } + } } /* @@ -171,42 +171,42 @@ int avl_search_closest(const avl_tree_t *avltree, const void *item, avl_node_t * * If no such item is in the tree, then NULL is returned. */ avl_node_t *avl_search(const avl_tree_t *avltree, const void *item) { - avl_node_t *node; - return avl_search_closest(avltree, item, &node) ? NULL : node; + avl_node_t *node; + return avl_search_closest(avltree, item, &node) ? NULL : node; } avl_tree_t *avl_init_tree(avl_tree_t *rc, avl_compare_t cmp, avl_freeitem_t freeitem) { - if(rc) { - rc->head = NULL; - rc->tail = NULL; - rc->top = NULL; - rc->cmp = cmp; - rc->freeitem = freeitem; - } - return rc; + if(rc) { + rc->head = NULL; + rc->tail = NULL; + rc->top = NULL; + rc->cmp = cmp; + rc->freeitem = freeitem; + } + return rc; } avl_tree_t *avl_alloc_tree(avl_compare_t cmp, avl_freeitem_t freeitem) { - return avl_init_tree(malloc(sizeof(avl_tree_t)), cmp, freeitem); + return avl_init_tree(malloc(sizeof(avl_tree_t)), cmp, freeitem); } void avl_clear_tree(avl_tree_t *avltree) { - avltree->top = avltree->head = avltree->tail = NULL; + avltree->top = avltree->head = avltree->tail = NULL; } void avl_free_nodes(avl_tree_t *avltree) { - avl_node_t *node, *next; - avl_freeitem_t freeitem; - - freeitem = avltree->freeitem; - - for(node = avltree->head; node; node = next) { - next = node->next; - if(freeitem) - freeitem(node->item); - free(node); - } - avl_clear_tree(avltree); + avl_node_t *node, *next; + avl_freeitem_t freeitem; + + freeitem = avltree->freeitem; + + for(node = avltree->head; node; node = next) { + next = node->next; + if(freeitem) + freeitem(node->item); + free(node); + } + avl_clear_tree(avltree); } /* @@ -216,101 +216,101 @@ void avl_free_nodes(avl_tree_t *avltree) { * tree, and they are deleted as well. */ void avl_free_tree(avl_tree_t *avltree) { - avl_free_nodes(avltree); - free(avltree); + avl_free_nodes(avltree); + free(avltree); } static void avl_clear_node(avl_node_t *newnode) { - newnode->left = newnode->right = NULL; - #ifdef AVL_COUNT - newnode->count = 1; - #endif - #ifdef AVL_DEPTH - newnode->depth = 1; - #endif + newnode->left = newnode->right = NULL; + #ifdef AVL_COUNT + newnode->count = 1; + #endif + #ifdef AVL_DEPTH + newnode->depth = 1; + #endif } avl_node_t *avl_init_node(avl_node_t *newnode, void *item) { - if(newnode) { - avl_clear_node(newnode); - newnode->item = item; - } - return newnode; + if(newnode) { + avl_clear_node(newnode); + newnode->item = item; + } + return newnode; } avl_node_t *avl_insert_top(avl_tree_t *avltree, avl_node_t *newnode) { - avl_clear_node(newnode); - newnode->prev = newnode->next = newnode->parent = NULL; - avltree->head = avltree->tail = avltree->top = newnode; - return newnode; + avl_clear_node(newnode); + newnode->prev = newnode->next = newnode->parent = NULL; + avltree->head = avltree->tail = avltree->top = newnode; + return newnode; } avl_node_t *avl_insert_before(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode) { - if(!node) - return avltree->tail - ? avl_insert_after(avltree, avltree->tail, newnode) - : avl_insert_top(avltree, newnode); + if(!node) + return avltree->tail + ? avl_insert_after(avltree, avltree->tail, newnode) + : avl_insert_top(avltree, newnode); - if(node->left) - return avl_insert_after(avltree, node->prev, newnode); + if(node->left) + return avl_insert_after(avltree, node->prev, newnode); - avl_clear_node(newnode); + avl_clear_node(newnode); - newnode->next = node; - newnode->parent = node; + newnode->next = node; + newnode->parent = node; - newnode->prev = node->prev; - if(node->prev) - node->prev->next = newnode; - else - avltree->head = newnode; - node->prev = newnode; + newnode->prev = node->prev; + if(node->prev) + node->prev->next = newnode; + else + avltree->head = newnode; + node->prev = newnode; - node->left = newnode; - avl_rebalance(avltree, node); - return newnode; + node->left = newnode; + avl_rebalance(avltree, node); + return newnode; } avl_node_t *avl_insert_after(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode) { - if(!node) - return avltree->head - ? avl_insert_before(avltree, avltree->head, newnode) - : avl_insert_top(avltree, newnode); + if(!node) + return avltree->head + ? avl_insert_before(avltree, avltree->head, newnode) + : avl_insert_top(avltree, newnode); - if(node->right) - return avl_insert_before(avltree, node->next, newnode); + if(node->right) + return avl_insert_before(avltree, node->next, newnode); - avl_clear_node(newnode); + avl_clear_node(newnode); - newnode->prev = node; - newnode->parent = node; + newnode->prev = node; + newnode->parent = node; - newnode->next = node->next; - if(node->next) - node->next->prev = newnode; - else - avltree->tail = newnode; - node->next = newnode; + newnode->next = node->next; + if(node->next) + node->next->prev = newnode; + else + avltree->tail = newnode; + node->next = newnode; - node->right = newnode; - avl_rebalance(avltree, node); - return newnode; + node->right = newnode; + avl_rebalance(avltree, node); + return newnode; } avl_node_t *avl_insert_node(avl_tree_t *avltree, avl_node_t *newnode) { - avl_node_t *node; + avl_node_t *node; - if(!avltree->top) - return avl_insert_top(avltree, newnode); + if(!avltree->top) + return avl_insert_top(avltree, newnode); - switch(avl_search_closest(avltree, newnode->item, &node)) { - case -1: - return avl_insert_before(avltree, node, newnode); - case 1: - return avl_insert_after(avltree, node, newnode); - } + switch(avl_search_closest(avltree, newnode->item, &node)) { + case -1: + return avl_insert_before(avltree, node, newnode); + case 1: + return avl_insert_after(avltree, node, newnode); + } - return NULL; + return NULL; } /* @@ -319,16 +319,16 @@ avl_node_t *avl_insert_node(avl_tree_t *avltree, avl_node_t *newnode) { * Returns the new node on success or NULL if no memory could be allocated. */ avl_node_t *avl_insert(avl_tree_t *avltree, void *item) { - avl_node_t *newnode; - - newnode = avl_init_node(malloc(sizeof(avl_node_t)), item); - if(newnode) { - if(avl_insert_node(avltree, newnode)) - return newnode; - free(newnode); - errno = EEXIST; - } - return NULL; + avl_node_t *newnode; + + newnode = avl_init_node(malloc(sizeof(avl_node_t)), item); + if(newnode) { + if(avl_insert_node(avltree, newnode)) + return newnode; + free(newnode); + errno = EEXIST; + } + return NULL; } /* @@ -338,109 +338,109 @@ avl_node_t *avl_insert(avl_tree_t *avltree, void *item) { * (In other words, it is not referenced by this function.) */ void avl_unlink_node(avl_tree_t *avltree, avl_node_t *avlnode) { - avl_node_t *parent; - avl_node_t **superparent; - avl_node_t *subst, *left, *right; - avl_node_t *balnode; - - if(avlnode->prev) - avlnode->prev->next = avlnode->next; - else - avltree->head = avlnode->next; - - if(avlnode->next) - avlnode->next->prev = avlnode->prev; - else - avltree->tail = avlnode->prev; - - parent = avlnode->parent; - - superparent = parent - ? avlnode == parent->left ? &parent->left : &parent->right - : &avltree->top; - - left = avlnode->left; - right = avlnode->right; - if(!left) { - *superparent = right; - if(right) - right->parent = parent; - balnode = parent; - } else if(!right) { - *superparent = left; - left->parent = parent; - balnode = parent; - } else { - subst = avlnode->prev; - if(subst == left) { - balnode = subst; - } else { - balnode = subst->parent; - balnode->right = subst->left; - if(balnode->right) - balnode->right->parent = balnode; - subst->left = left; - left->parent = subst; - } - subst->right = right; - subst->parent = parent; - right->parent = subst; - *superparent = subst; - } - - avl_rebalance(avltree, balnode); + avl_node_t *parent; + avl_node_t **superparent; + avl_node_t *subst, *left, *right; + avl_node_t *balnode; + + if(avlnode->prev) + avlnode->prev->next = avlnode->next; + else + avltree->head = avlnode->next; + + if(avlnode->next) + avlnode->next->prev = avlnode->prev; + else + avltree->tail = avlnode->prev; + + parent = avlnode->parent; + + superparent = parent + ? avlnode == parent->left ? &parent->left : &parent->right + : &avltree->top; + + left = avlnode->left; + right = avlnode->right; + if(!left) { + *superparent = right; + if(right) + right->parent = parent; + balnode = parent; + } else if(!right) { + *superparent = left; + left->parent = parent; + balnode = parent; + } else { + subst = avlnode->prev; + if(subst == left) { + balnode = subst; + } else { + balnode = subst->parent; + balnode->right = subst->left; + if(balnode->right) + balnode->right->parent = balnode; + subst->left = left; + left->parent = subst; + } + subst->right = right; + subst->parent = parent; + right->parent = subst; + *superparent = subst; + } + + avl_rebalance(avltree, balnode); } void *avl_delete_node(avl_tree_t *avltree, avl_node_t *avlnode) { - void *item = NULL; - if(avlnode) { - item = avlnode->item; - avl_unlink_node(avltree, avlnode); - if(avltree->freeitem) - avltree->freeitem(item); - free(avlnode); - } - return item; + void *item = NULL; + if(avlnode) { + item = avlnode->item; + avl_unlink_node(avltree, avlnode); + if(avltree->freeitem) + avltree->freeitem(item); + free(avlnode); + } + return item; } void *avl_delete(avl_tree_t *avltree, const void *item) { - return avl_delete_node(avltree, avl_search(avltree, item)); + return avl_delete_node(avltree, avl_search(avltree, item)); } avl_node_t *avl_fixup_node(avl_tree_t *avltree, avl_node_t *newnode) { - avl_node_t *oldnode = NULL, *node; - - if(!avltree || !newnode) - return NULL; - - node = newnode->prev; - if(node) { - oldnode = node->next; - node->next = newnode; - } else { - avltree->head = newnode; - } - - node = newnode->next; - if(node) { - oldnode = node->prev; - node->prev = newnode; - } else { - avltree->tail = newnode; - } - - node = newnode->parent; - if(node) { - if(node->left == oldnode) - node->left = newnode; - else - node->right = newnode; - } else { - oldnode = avltree->top; - avltree->top = newnode; - } - - return oldnode; + avl_node_t *oldnode = NULL, *node; + + if(!avltree || !newnode) + return NULL; + + node = newnode->prev; + if(node) { + oldnode = node->next; + node->next = newnode; + } else { + avltree->head = newnode; + } + + node = newnode->next; + if(node) { + oldnode = node->prev; + node->prev = newnode; + } else { + avltree->tail = newnode; + } + + node = newnode->parent; + if(node) { + if(node->left == oldnode) + node->left = newnode; + else + node->right = newnode; + } else { + oldnode = avltree->top; + avltree->top = newnode; + } + + return oldnode; } /* @@ -453,137 +453,137 @@ avl_node_t *avl_fixup_node(avl_tree_t *avltree, avl_node_t *newnode) { * longer going to be the same node. */ void avl_rebalance(avl_tree_t *avltree, avl_node_t *avlnode) { - avl_node_t *child; - avl_node_t *gchild; - avl_node_t *parent; - avl_node_t **superparent; - - parent = avlnode; - - while(avlnode) { - parent = avlnode->parent; - - superparent = parent - ? avlnode == parent->left ? &parent->left : &parent->right - : &avltree->top; - - switch(avl_check_balance(avlnode)) { - case -1: - child = avlnode->left; - #ifdef AVL_DEPTH - if(L_DEPTH(child) >= R_DEPTH(child)) { - #else - #ifdef AVL_COUNT - if(L_COUNT(child) >= R_COUNT(child)) { - #else - #error No balancing possible. - #endif - #endif - avlnode->left = child->right; - if(avlnode->left) - avlnode->left->parent = avlnode; - child->right = avlnode; - avlnode->parent = child; - *superparent = child; - child->parent = parent; - #ifdef AVL_COUNT - avlnode->count = CALC_COUNT(avlnode); - child->count = CALC_COUNT(child); - #endif - #ifdef AVL_DEPTH - avlnode->depth = CALC_DEPTH(avlnode); - child->depth = CALC_DEPTH(child); - #endif - } else { - gchild = child->right; - avlnode->left = gchild->right; - if(avlnode->left) - avlnode->left->parent = avlnode; - child->right = gchild->left; - if(child->right) - child->right->parent = child; - gchild->right = avlnode; - if(gchild->right) - gchild->right->parent = gchild; - gchild->left = child; - if(gchild->left) - gchild->left->parent = gchild; - *superparent = gchild; - gchild->parent = parent; - #ifdef AVL_COUNT - avlnode->count = CALC_COUNT(avlnode); - child->count = CALC_COUNT(child); - gchild->count = CALC_COUNT(gchild); - #endif - #ifdef AVL_DEPTH - avlnode->depth = CALC_DEPTH(avlnode); - child->depth = CALC_DEPTH(child); - gchild->depth = CALC_DEPTH(gchild); - #endif - } - break; - case 1: - child = avlnode->right; - #ifdef AVL_DEPTH - if(R_DEPTH(child) >= L_DEPTH(child)) { - #else - #ifdef AVL_COUNT - if(R_COUNT(child) >= L_COUNT(child)) { - #else - #error No balancing possible. - #endif - #endif - avlnode->right = child->left; - if(avlnode->right) - avlnode->right->parent = avlnode; - child->left = avlnode; - avlnode->parent = child; - *superparent = child; - child->parent = parent; - #ifdef AVL_COUNT - avlnode->count = CALC_COUNT(avlnode); - child->count = CALC_COUNT(child); - #endif - #ifdef AVL_DEPTH - avlnode->depth = CALC_DEPTH(avlnode); - child->depth = CALC_DEPTH(child); - #endif - } else { - gchild = child->left; - avlnode->right = gchild->left; - if(avlnode->right) - avlnode->right->parent = avlnode; - child->left = gchild->right; - if(child->left) - child->left->parent = child; - gchild->left = avlnode; - if(gchild->left) - gchild->left->parent = gchild; - gchild->right = child; - if(gchild->right) - gchild->right->parent = gchild; - *superparent = gchild; - gchild->parent = parent; - #ifdef AVL_COUNT - avlnode->count = CALC_COUNT(avlnode); - child->count = CALC_COUNT(child); - gchild->count = CALC_COUNT(gchild); - #endif - #ifdef AVL_DEPTH - avlnode->depth = CALC_DEPTH(avlnode); - child->depth = CALC_DEPTH(child); - gchild->depth = CALC_DEPTH(gchild); - #endif - } - break; - default: - #ifdef AVL_COUNT - avlnode->count = CALC_COUNT(avlnode); - #endif - #ifdef AVL_DEPTH - avlnode->depth = CALC_DEPTH(avlnode); - #endif - } - avlnode = parent; - } + avl_node_t *child; + avl_node_t *gchild; + avl_node_t *parent; + avl_node_t **superparent; + + parent = avlnode; + + while(avlnode) { + parent = avlnode->parent; + + superparent = parent + ? avlnode == parent->left ? &parent->left : &parent->right + : &avltree->top; + + switch(avl_check_balance(avlnode)) { + case -1: + child = avlnode->left; + #ifdef AVL_DEPTH + if(L_DEPTH(child) >= R_DEPTH(child)) { + #else + #ifdef AVL_COUNT + if(L_COUNT(child) >= R_COUNT(child)) { + #else + #error No balancing possible. + #endif + #endif + avlnode->left = child->right; + if(avlnode->left) + avlnode->left->parent = avlnode; + child->right = avlnode; + avlnode->parent = child; + *superparent = child; + child->parent = parent; + #ifdef AVL_COUNT + avlnode->count = CALC_COUNT(avlnode); + child->count = CALC_COUNT(child); + #endif + #ifdef AVL_DEPTH + avlnode->depth = CALC_DEPTH(avlnode); + child->depth = CALC_DEPTH(child); + #endif + } else { + gchild = child->right; + avlnode->left = gchild->right; + if(avlnode->left) + avlnode->left->parent = avlnode; + child->right = gchild->left; + if(child->right) + child->right->parent = child; + gchild->right = avlnode; + if(gchild->right) + gchild->right->parent = gchild; + gchild->left = child; + if(gchild->left) + gchild->left->parent = gchild; + *superparent = gchild; + gchild->parent = parent; + #ifdef AVL_COUNT + avlnode->count = CALC_COUNT(avlnode); + child->count = CALC_COUNT(child); + gchild->count = CALC_COUNT(gchild); + #endif + #ifdef AVL_DEPTH + avlnode->depth = CALC_DEPTH(avlnode); + child->depth = CALC_DEPTH(child); + gchild->depth = CALC_DEPTH(gchild); + #endif + } + break; + case 1: + child = avlnode->right; + #ifdef AVL_DEPTH + if(R_DEPTH(child) >= L_DEPTH(child)) { + #else + #ifdef AVL_COUNT + if(R_COUNT(child) >= L_COUNT(child)) { + #else + #error No balancing possible. + #endif + #endif + avlnode->right = child->left; + if(avlnode->right) + avlnode->right->parent = avlnode; + child->left = avlnode; + avlnode->parent = child; + *superparent = child; + child->parent = parent; + #ifdef AVL_COUNT + avlnode->count = CALC_COUNT(avlnode); + child->count = CALC_COUNT(child); + #endif + #ifdef AVL_DEPTH + avlnode->depth = CALC_DEPTH(avlnode); + child->depth = CALC_DEPTH(child); + #endif + } else { + gchild = child->left; + avlnode->right = gchild->left; + if(avlnode->right) + avlnode->right->parent = avlnode; + child->left = gchild->right; + if(child->left) + child->left->parent = child; + gchild->left = avlnode; + if(gchild->left) + gchild->left->parent = gchild; + gchild->right = child; + if(gchild->right) + gchild->right->parent = gchild; + *superparent = gchild; + gchild->parent = parent; + #ifdef AVL_COUNT + avlnode->count = CALC_COUNT(avlnode); + child->count = CALC_COUNT(child); + gchild->count = CALC_COUNT(gchild); + #endif + #ifdef AVL_DEPTH + avlnode->depth = CALC_DEPTH(avlnode); + child->depth = CALC_DEPTH(child); + gchild->depth = CALC_DEPTH(gchild); + #endif + } + break; + default: + #ifdef AVL_COUNT + avlnode->count = CALC_COUNT(avlnode); + #endif + #ifdef AVL_DEPTH + avlnode->depth = CALC_DEPTH(avlnode); + #endif + } + avlnode = parent; + } } diff --git a/src/avl.h b/src/avl.h index 880e3e179..242bb8b23 100644 --- a/src/avl.h +++ b/src/avl.h @@ -27,8 +27,8 @@ *****************************************************************************/ -#ifndef _AVL_H -#define _AVL_H +#ifndef MLRMBO_AVL_H_ +#define MLRMBO_AVL_H_ /* We need either depths, counts or both (the latter being the default) */ #if !defined(AVL_DEPTH) && !defined(AVL_COUNT) @@ -50,26 +50,26 @@ typedef int (*avl_compare_t)(const void *, const void *); typedef void (*avl_freeitem_t)(void *); typedef struct avl_node_t { - struct avl_node_t *next; - struct avl_node_t *prev; - struct avl_node_t *parent; - struct avl_node_t *left; - struct avl_node_t *right; - void *item; + struct avl_node_t *next; + struct avl_node_t *prev; + struct avl_node_t *parent; + struct avl_node_t *left; + struct avl_node_t *right; + void *item; #ifdef AVL_COUNT - unsigned int count; + unsigned int count; #endif #ifdef AVL_DEPTH - unsigned char depth; + unsigned char depth; #endif } avl_node_t; typedef struct avl_tree_t { - avl_node_t *head; - avl_node_t *tail; - avl_node_t *top; - avl_compare_t cmp; - avl_freeitem_t freeitem; + avl_node_t *head; + avl_node_t *tail; + avl_node_t *top; + avl_compare_t cmp; + avl_freeitem_t freeitem; } avl_tree_t; /* Initializes a new tree for elements that will be ordered using diff --git a/src/hv.c b/src/hv.c index f258c3bb0..a7787b685 100644 --- a/src/hv.c +++ b/src/hv.c @@ -52,59 +52,27 @@ #include #include -#if !defined(VARIANT) || VARIANT < 1 || VARIANT > 4 -#error VARIANT must be either 1, 2, 3 or 4, e.g., 'make VARIANT=4' -#endif - -#if __GNUC__ >= 3 -# define __hv_unused __attribute__ ((unused)) -#else -# define __hv_unused /* no 'unused' attribute available */ -#endif - -#if VARIANT < 3 -# define __variant3_only __hv_unused -#else -# define __variant3_only -#endif - -#if VARIANT < 2 -# define __variant2_only __hv_unused -#else -# define __variant2_only -#endif - typedef struct dlnode { double *x; /* The data vector */ struct dlnode **next; /* Next-node vector */ struct dlnode **prev; /* Previous-node vector */ struct avl_node_t * tnode; int ignore; -#if VARIANT >= 2 double *area; /* Area */ -#endif -#if VARIANT >= 3 double *vol; /* Volume */ -#endif } dlnode_t; static avl_tree_t *tree; -#if VARIANT < 4 -int stop_dimension = 1; /* default: stop on dimension 2 */ -#else int stop_dimension = 2; /* default: stop on dimension 3 */ -#endif -static int compare_node( const void *p1, const void* p2) -{ +static int compare_node( const void *p1, const void* p2) { const double x1 = *((*(const dlnode_t **)p1)->x); const double x2 = *((*(const dlnode_t **)p2)->x); return (x1 < x2) ? -1 : (x1 > x2) ? 1 : 0; } -static int compare_tree_asc( const void *p1, const void *p2) -{ +static int compare_tree_asc( const void *p1, const void *p2) { const double x1 = *((const double *)p1 + 1); const double x2 = *((const double *)p2 + 1); @@ -116,9 +84,7 @@ static int compare_tree_asc( const void *p1, const void *p2) * Setup circular double-linked list in each dimension */ -static dlnode_t * -setup_cdllist(double *data, int d, int n) -{ +static dlnode_t * setup_cdllist(double *data, int d, int n) { dlnode_t *head; dlnode_t **scratch; int i, j; @@ -131,12 +97,8 @@ setup_cdllist(double *data, int d, int n) head->prev = malloc( d * (n+1) * sizeof(dlnode_t*)); head->tnode = malloc ((n+1) * sizeof(avl_node_t)); -#if VARIANT >= 2 head->area = malloc(d * (n+1) * sizeof(double)); -#endif -#if VARIANT >= 3 head->vol = malloc(d * (n+1) * sizeof(double)); -#endif for (i = 1; i <= n; i++) { head[i].x = head[i-1].x + d ;/* this will be fixed a few lines below... */ @@ -144,12 +106,8 @@ setup_cdllist(double *data, int d, int n) head[i].next = head[i-1].next + d; head[i].prev = head[i-1].prev + d; head[i].tnode = head[i-1].tnode + 1; -#if VARIANT >= 2 head[i].area = head[i-1].area + d; -#endif -#if VARIANT >= 3 head[i].vol = head[i-1].vol + d; -#endif } head->x = NULL; /* head contains no data */ @@ -175,61 +133,41 @@ setup_cdllist(double *data, int d, int n) for (i = 1; i <= n; i++) avl_init_node(head[i].tnode, head[i].x); - -#if VARIANT >= 2 + for (i = 0; i < d; i++) head->area[i] = 0; -#endif return head; } -static void free_cdllist(dlnode_t * head) -{ +static void free_cdllist(dlnode_t * head) { free(head->tnode); /* Frees _all_ nodes. */ free(head->next); free(head->prev); -#if VARIANT >= 2 free(head->area); -#endif -#if VARIANT >= 3 free(head->vol); -#endif free(head); } -static void delete (dlnode_t *nodep, int dim, double * bound __variant3_only) -{ - int i; - - for (i = 0; i < dim; i++) { +static void delete (dlnode_t *nodep, int dim, double * bound) { + for (int i = 0; i < dim; i++) { nodep->prev[i]->next[i] = nodep->next[i]; nodep->next[i]->prev[i] = nodep->prev[i]; -#if VARIANT >= 3 if (bound[i] > nodep->x[i]) bound[i] = nodep->x[i]; -#endif } } -static void reinsert (dlnode_t *nodep, int dim, double * bound __variant3_only) -{ - int i; - - for (i = 0; i < dim; i++) { +static void reinsert (dlnode_t *nodep, int dim, double * bound) { + for (int i = 0; i < dim; i++) { nodep->prev[i]->next[i] = nodep; nodep->next[i]->prev[i] = nodep; -#if VARIANT >= 3 if (bound[i] > nodep->x[i]) bound[i] = nodep->x[i]; -#endif } } -static double -hv_recursive(dlnode_t *list, int dim, int c, const double * ref, - double * bound) -{ +static double hv_recursive(dlnode_t *list, int dim, int c, const double * ref, double * bound) { /* ------------------------------------------------------ General case for dimensions higher than stop_dimension ------------------------------------------------------ */ @@ -237,24 +175,17 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, dlnode_t *p0 = list; dlnode_t *p1 = list->prev[dim]; double hyperv = 0; -#if VARIANT == 1 - double hypera; -#endif -#if VARIANT >= 2 dlnode_t *pp; for (pp = p1; pp->x; pp = pp->prev[dim]) { if (pp->ignore < dim) pp->ignore = 0; } -#endif while (c > 1 -#if VARIANT >= 3 /* We delete all points x[dim] > bound[dim]. In case of repeated coordinates, we also delete all points x[dim] == bound[dim] except one. */ - && (p1->x[dim] > bound[dim] + && (p1->x[dim] > bound[dim] || p1->prev[dim]->x[dim] >= bound[dim]) -#endif ) { p0 = p1; delete(p0, dim, bound); @@ -262,22 +193,16 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, c--; } -#if VARIANT == 1 - hypera = hv_recursive(list, dim-1, c, ref, bound); -#elif VARIANT >= 3 if (c > 1) { hyperv = p1->prev[dim]->vol[dim] + p1->prev[dim]->area[dim] * (p1->x[dim] - p1->prev[dim]->x[dim]); p1->vol[dim] = hyperv; } else { - int i; - p1->area[0] = 1; - for (i = 1; i <= dim; i++) + p1->area[0] = 1; + for (int i = 1; i <= dim; i++) p1->area[i] = p1->area[i-1] * (ref[i-1] - p1->x[i-1]); p1->vol[dim] = 0; } -#endif -#if VARIANT >= 2 if (p1->ignore >= dim) { p1->area[dim] = p1->prev[dim]->area[dim]; } else { @@ -285,28 +210,16 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, if (p1->area[dim] <= p1->prev[dim]->area[dim]) p1->ignore = dim; } -#endif while (p0->x != NULL) { -#if VARIANT == 1 - hyperv += hypera * (p0->x[dim] - p1->x[dim]); -#elif VARIANT >= 2 hyperv += p1->area[dim] * (p0->x[dim] - p1->x[dim]); -#endif -#if VARIANT >= 3 bound[dim] = p0->x[dim]; -#endif reinsert(p0, dim, bound); c++; p1 = p0; p0 = p0->next[dim]; -#if VARIANT >= 3 p1->vol[dim] = hyperv; -#endif -#if VARIANT == 1 - hypera = hv_recursive(list, dim-1, c, ref, NULL); -#elif VARIANT >= 2 if (p1->ignore >= dim) { p1->area[dim] = p1->prev[dim]->area[dim]; } else { @@ -314,13 +227,8 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, if (p1->area[dim] <= p1->prev[dim]->area[dim]) p1->ignore = dim; } -#endif } -#if VARIANT == 1 - hyperv += hypera * (ref[dim] - p1->x[dim]); -#elif VARIANT >= 2 hyperv += p1->area[dim] * (ref[dim] - p1->x[dim]); -#endif return hyperv; } @@ -335,7 +243,7 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, hypera = (ref[0] - pp->x[0]) * (ref[1] - pp->x[1]); - height = (c == 1) + height = (c == 1) ? ref[2] - pp->x[2] : pp->next[2]->x[2] - pp->x[2]; @@ -345,17 +253,15 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, return hyperv; avl_insert_top(tree, pp->tnode); - + pp = pp->next[2]; do { height = (pp == list->prev[2]) ? ref[2] - pp->x[2] : pp->next[2]->x[2] - pp->x[2]; -#if VARIANT >= 2 if (pp->ignore >= 2) hyperv += hypera * height; else { -#endif const double * prv_ip, * nxt_ip; avl_node_t *tnode; @@ -379,7 +285,7 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, const double * cur_ip; tnode = pp->tnode->prev; - /* cur_ip = point dominated by pp with + /* cur_ip = point dominated by pp with highest [0]-coordinate */ cur_ip = (double *)(tnode->item); while (tnode->prev) { @@ -409,10 +315,7 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, if (height > 0) hyperv += hypera * height; - -#if VARIANT >= 2 } -#endif pp = pp->next[2]; } while (pp->x != NULL); @@ -422,7 +325,7 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, /* special case of dimension 2 */ else if (dim == 1) { - const dlnode_t *p1 = list->next[1]; + const dlnode_t *p1 = list->next[1]; double hypera = p1->x[0]; double hyperv = 0; const dlnode_t *p0; @@ -440,14 +343,8 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, /* special case of dimension 1 */ else if (dim == 0) { return (ref[0] - list->next[0]->x[0]); - } + } else { - /* - fprintf(stderr, "%s:%d: unreachable condition! \n" - "This is a bug, please report it to " - "manuel.lopez-ibanez@ulb.ac.be\n", __FILE__, __LINE__); - exit(EXIT_FAILURE); - */ error("hv: UNREACHABLE CODE REACHED. Please report this to the package author."); return -1.0; /* Never reached. */ } @@ -456,14 +353,10 @@ hv_recursive(dlnode_t *list, int dim, int c, const double * ref, /* Removes the point from the circular double-linked list. */ -static void -filter_delete_node(dlnode_t *node, int d) -{ - int i; - +static void filter_delete_node(dlnode_t *node, int d) { /* The memory allocated for the deleted node is lost (leaked) until the end of the program, but this should not be a problem. */ - for (i = 0; i < d; i++) { + for (int i = 0; i < d; i++) { node->next[i]->prev[i] = node->prev[i]; node->prev[i]->next[i] = node->next[i]; } @@ -474,17 +367,13 @@ filter_delete_node(dlnode_t *node, int d) point. This is needed to assure that the points left are only those which are needed to calculate the hypervolume. */ -static int -filter(dlnode_t *list, int d, int n, const double *ref) -{ - int i, j; - +static int filter(dlnode_t *list, int d, int n, const double *ref) { /* fprintf (stderr, "%d points initially\n", n); */ - for (i = 0; i < d; i++) { + for (int i = 0; i < d; i++) { dlnode_t *aux = list->prev[i]; int np = n; - for (j = 0; j < np; j++) { - if (aux->x[i] < ref[i]) + for (int j = 0; j < np; j++) { + if (aux->x[i] < ref[i]) break; filter_delete_node (aux, d); aux = aux->prev[i]; @@ -495,18 +384,13 @@ filter(dlnode_t *list, int d, int n, const double *ref) return n; } -double fpli_hv(double *data, int d, int n, const double *ref) -{ +double fpli_hv(double *data, int d, int n, const double *ref) { dlnode_t *list; double hyperv; double * bound = NULL; -#if VARIANT >= 3 - int i; - bound = malloc (d * sizeof(double)); - for (i = 0; i < d; i++) bound[i] = -DBL_MAX; -#endif + for (int i = 0; i < d; i++) bound[i] = -DBL_MAX; tree = avl_alloc_tree ((avl_compare_t) compare_tree_asc, (avl_freeitem_t) NULL); @@ -514,16 +398,16 @@ double fpli_hv(double *data, int d, int n, const double *ref) list = setup_cdllist(data, d, n); n = filter(list, d, n, ref); - if (n == 0) { + if (n == 0) { /* Returning here would leak memory. */ - hyperv = 0.0; + hyperv = 0.0; } else { - hyperv = hv_recursive(list, d-1, n, ref, bound); + hyperv = hv_recursive(list, d-1, n, ref, bound); } /* Clean up. */ free_cdllist (list); free (tree); /* The nodes are freed by free_cdllist (). */ - free (bound); + free (bound); return hyperv; } diff --git a/src/hv.h b/src/hv.h index 4c81e5957..36fdf5c9e 100644 --- a/src/hv.h +++ b/src/hv.h @@ -1,6 +1,6 @@ /************************************************************************* - hv.h + hv.h --------------------------------------------------------------------- @@ -31,8 +31,8 @@ *************************************************************************/ -#ifndef HV_H_ -#define HV_H_ +#ifndef MLRMBO_HV_H_ +#define MLRMBO_HV_H_ #ifdef __cplusplus extern "C" { diff --git a/src/macros.h b/src/macros.h index 041360b60..b047b07f6 100644 --- a/src/macros.h +++ b/src/macros.h @@ -1,5 +1,5 @@ -#ifndef FOO_MACROS_H -#define FOO_MACROS_H +#ifndef MLRMBO_MACROS_H_ +#define MLRMBO_MACROS_H_ #include #include @@ -18,6 +18,6 @@ #define UNPACK_REAL_MATRIX_2(S, D, N) \ double *D = REAL(S); \ - const R_len_t N = nrows(S); + const R_len_t N = nrows(S); #endif