gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r9016 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r9016 - gnunet/src/util
Date: Tue, 22 Sep 2009 15:57:17 -0600

Author: nevans
Date: 2009-09-22 15:57:17 -0600 (Tue, 22 Sep 2009)
New Revision: 9016

Modified:
   gnunet/src/util/Makefile.am
   gnunet/src/util/container_heap.c
   gnunet/src/util/test_container_heap.c
Log:
heap merge from .8, fixed testcase

Modified: gnunet/src/util/Makefile.am
===================================================================
--- gnunet/src/util/Makefile.am 2009-09-22 20:33:29 UTC (rev 9015)
+++ gnunet/src/util/Makefile.am 2009-09-22 21:57:17 UTC (rev 9016)
@@ -84,6 +84,7 @@
  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.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

Modified: gnunet/src/util/container_heap.c
===================================================================
--- gnunet/src/util/container_heap.c    2009-09-22 20:33:29 UTC (rev 9015)
+++ gnunet/src/util/container_heap.c    2009-09-22 21:57:17 UTC (rev 9016)
@@ -81,7 +81,10 @@
  */
 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,21 +138,18 @@
 {
   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 @@
   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 @@
       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 @@
   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;

Modified: gnunet/src/util/test_container_heap.c
===================================================================
--- gnunet/src/util/test_container_heap.c       2009-09-22 20:33:29 UTC (rev 
9015)
+++ gnunet/src/util/test_container_heap.c       2009-09-22 21:57:17 UTC (rev 
9016)
@@ -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 */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]