From: Nathan S. Evans Date: Tue, 22 Sep 2009 21:57:17 +0000 (+0000) Subject: heap merge from .8, fixed testcase X-Git-Tag: initial-import-from-subversion-38251~23472 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c04768147bc897cfafb343aeeb02221ecd086795;p=oweals%2Fgnunet.git heap merge from .8, fixed testcase --- diff --git a/src/util/Makefile.am b/src/util/Makefile.am index c0e64b3f4..240c86247 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -84,6 +84,7 @@ check_PROGRAMS = \ test_container_bloomfilter \ test_container_meta_data \ test_container_multihashmap \ + test_container_heap \ test_crypto_aes \ test_crypto_aes_weak \ test_crypto_crc \ @@ -156,6 +157,11 @@ test_container_multihashmap_SOURCES = \ test_container_multihashmap.c test_container_multihashmap_LDADD = \ $(top_builddir)/src/util/libgnunetutil.la + +test_container_heap_SOURCES = \ + test_container_heap.c +test_container_heap_LDADD = \ + $(top_builddir)/src/util/libgnunetutil.la test_crypto_aes_SOURCES = \ test_crypto_aes.c diff --git a/src/util/container_heap.c b/src/util/container_heap.c index c420a0ae4..c7afd6f71 100644 --- a/src/util/container_heap.c +++ b/src/util/container_heap.c @@ -81,7 +81,10 @@ struct GNUNET_CONTAINER_Heap */ void *GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap) { - return heap->root; + if ((heap == NULL) || (heap->root == NULL)) + return NULL; + + return heap->root->element; } @@ -135,20 +138,17 @@ find_element (struct GNUNET_CONTAINER_heap_node *node, void *element) { struct GNUNET_CONTAINER_heap_node *ret; ret = NULL; - if ((node != NULL) && (node->element == element)) - { - ret = node; - } + if (node == NULL) + return NULL; - if ((ret == NULL) && (node->left_child != NULL)) - { - ret = find_element (node->left_child, element); - } + if (node->element == element) + return node; - if ((ret == NULL) && (node->right_child != NULL)) - { - ret = find_element (node->right_child, element); - } + if (node->left_child != NULL) + ret = find_element (node->left_child, element); + + if (node->right_child != NULL) + ret = find_element (node->right_child, element); return ret; } @@ -241,21 +241,6 @@ swapNodes (struct GNUNET_CONTAINER_heap_node *first, second->element = temp_element; second->cost = temp_cost; -/* - * I still worry that there is some good reason for - * elements being location aware... but it eludes me - * for the moment... - if ((root->type == GNUNET_DV_MAX_HEAP)) - { - first->neighbor->max_loc = first; - second->neighbor->max_loc = second; - } - else if ((root->type == GNUNET_DV_MIN_HEAP)) - { - first->neighbor->min_loc = first; - second->neighbor->min_loc = second; - } -*/ return; } @@ -393,11 +378,6 @@ GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *root, new_pos->element = element; new_pos->cost = cost; root->size++; - /*We no longer can tolerate pointers between heaps :( */ - /*if (root->type == GNUNET_DV_MIN_HEAP) - new_pos->neighbor->min_loc = new_pos; - else if (root->type == GNUNET_DV_MAX_HEAP) - new_pos->neighbor->max_loc = new_pos; */ percolateHeap (new_pos, root); } @@ -416,11 +396,14 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *root) struct GNUNET_CONTAINER_heap_node *root_node; struct GNUNET_CONTAINER_heap_node *last; + if ((root == NULL) || (root->size == 0) || (root->root == NULL)) + return NULL; + root_node = root->root; ret = root_node->element; last = getPos (root, root->size); - if ((root_node == last) && (root->size == 1)) + if ((root_node == last) && (root->size == 1)) { /* We are removing the last node in the heap! */ root->root = NULL; diff --git a/src/util/test_container_heap.c b/src/util/test_container_heap.c index 84f992686..9e6a29ea4 100644 --- a/src/util/test_container_heap.c +++ b/src/util/test_container_heap.c @@ -20,94 +20,74 @@ /** * @author Nathan Evans - * @file util/containers/heaptest.c + * @file util/test_container_heap.c * @brief Test of heap operations */ -#include "gnunet_util.h" -#include "gnunet_util_containers.h" -#include "dv.h" +#include "platform.h" +#include "gnunet_common.h" +#include "gnunet_container_lib.h" + +struct TestItem +{ + unsigned int cost; +}; static int -iterator_callback (void *element, GNUNET_CONTAINER_HeapCost cost, - struct GNUNET_CONTAINER_Heap *root, void *cls) +iterator_callback (void *cls, void *element, GNUNET_CONTAINER_HeapCost cost) { - struct GNUNET_dv_neighbor *node; - node = (struct GNUNET_dv_neighbor *) element; + struct TestItem *node; + node = (struct TestItem *) element; +#ifdef VERBOSE fprintf (stdout, "%d\n", node->cost); - //fprintf (stdout, "%d\n", ((struct GNUNET_dv_neighbor *)element)->cost); +#endif return GNUNET_OK; } - int main (int argc, char **argv) { struct GNUNET_CONTAINER_Heap *myHeap; - struct GNUNET_dv_neighbor *neighbor1; - struct GNUNET_dv_neighbor *neighbor2; - struct GNUNET_dv_neighbor *neighbor3; - struct GNUNET_dv_neighbor *neighbor4; - struct GNUNET_dv_neighbor *neighbor5; - struct GNUNET_dv_neighbor *neighbor6; + struct TestItem neighbor1; + struct TestItem neighbor2; + struct TestItem neighbor3; + struct TestItem neighbor4; + struct TestItem neighbor5; + struct TestItem neighbor6; GNUNET_log_setup ("test-container-heap", "WARNING", NULL); myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX); - neighbor1 = malloc (sizeof (struct GNUNET_dv_neighbor)); - neighbor2 = malloc (sizeof (struct GNUNET_dv_neighbor)); - neighbor3 = malloc (sizeof (struct GNUNET_dv_neighbor)); - neighbor4 = malloc (sizeof (struct GNUNET_dv_neighbor)); - neighbor5 = malloc (sizeof (struct GNUNET_dv_neighbor)); - neighbor6 = malloc (sizeof (struct GNUNET_dv_neighbor)); - - neighbor1->cost = 60; - neighbor2->cost = 50; - neighbor3->cost = 70; - neighbor4->cost = 120; - neighbor5->cost = 100; - neighbor6->cost = 30; + neighbor1.cost = 60; + neighbor2.cost = 50; + neighbor3.cost = 70; + neighbor4.cost = 120; + neighbor5.cost = 100; + neighbor6.cost = 30; - GNUNET_CONTAINER_heap_insert (myHeap, neighbor1, neighbor1->cost); + GNUNET_CONTAINER_heap_insert (myHeap, &neighbor1, neighbor1.cost); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - - fprintf (stdout, "\n"); - GNUNET_CONTAINER_heap_insert (myHeap, neighbor2, neighbor2->cost); - + GNUNET_CONTAINER_heap_insert (myHeap, &neighbor2, neighbor2.cost); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); - GNUNET_CONTAINER_heap_insert (myHeap, neighbor3, neighbor3->cost); - + GNUNET_CONTAINER_heap_insert (myHeap, &neighbor3, neighbor3.cost); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); - GNUNET_CONTAINER_heap_insert (myHeap, neighbor4, neighbor4->cost); - + GNUNET_CONTAINER_heap_insert (myHeap, &neighbor4, neighbor4.cost); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); - GNUNET_CONTAINER_heap_insert (myHeap, neighbor5, neighbor5->cost); - + GNUNET_CONTAINER_heap_insert (myHeap, &neighbor5, neighbor5.cost); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); - GNUNET_CONTAINER_heap_insert (myHeap, neighbor6, neighbor6->cost); - + GNUNET_CONTAINER_heap_insert (myHeap, &neighbor6, neighbor6.cost); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); - GNUNET_CONTAINER_heap_remove_node (myHeap, neighbor5); - + GNUNET_CONTAINER_heap_remove_node (myHeap, &neighbor5); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); GNUNET_CONTAINER_heap_remove_root (myHeap); - GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); - GNUNET_CONTAINER_heap_update_cost (myHeap, neighbor6, 200); - + GNUNET_CONTAINER_heap_update_cost (myHeap, &neighbor6, 200); GNUNET_CONTAINER_heap_iterate (myHeap, iterator_callback, NULL); - fprintf (stdout, "\n"); GNUNET_CONTAINER_heap_destroy (myHeap); + return 0; } -/* end of heaptest.c */ +/* end of test_container_heap.c */