*/
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;
}
{
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;
}
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;
}
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);
}
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;
/**
* @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 */