diff options
author | Nathan S. Evans <evans@in.tum.de> | 2009-09-22 21:57:17 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2009-09-22 21:57:17 +0000 |
commit | c04768147bc897cfafb343aeeb02221ecd086795 (patch) | |
tree | f5c59f607de670e8ebe6de3c551eda44b4032de1 /src/util/container_heap.c | |
parent | 272d1eccc183b8cec9f5bf823afbbeac02087dbc (diff) | |
download | gnunet-c04768147bc897cfafb343aeeb02221ecd086795.tar.gz gnunet-c04768147bc897cfafb343aeeb02221ecd086795.zip |
heap merge from .8, fixed testcase
Diffstat (limited to 'src/util/container_heap.c')
-rw-r--r-- | src/util/container_heap.c | 51 |
1 files changed, 17 insertions, 34 deletions
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 | |||
81 | */ | 81 | */ |
82 | void *GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap) | 82 | void *GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap) |
83 | { | 83 | { |
84 | return heap->root; | 84 | if ((heap == NULL) || (heap->root == NULL)) |
85 | return NULL; | ||
86 | |||
87 | return heap->root->element; | ||
85 | } | 88 | } |
86 | 89 | ||
87 | 90 | ||
@@ -135,20 +138,17 @@ find_element (struct GNUNET_CONTAINER_heap_node *node, void *element) | |||
135 | { | 138 | { |
136 | struct GNUNET_CONTAINER_heap_node *ret; | 139 | struct GNUNET_CONTAINER_heap_node *ret; |
137 | ret = NULL; | 140 | ret = NULL; |
138 | if ((node != NULL) && (node->element == element)) | 141 | if (node == NULL) |
139 | { | 142 | return NULL; |
140 | ret = node; | ||
141 | } | ||
142 | 143 | ||
143 | if ((ret == NULL) && (node->left_child != NULL)) | 144 | if (node->element == element) |
144 | { | 145 | return node; |
145 | ret = find_element (node->left_child, element); | ||
146 | } | ||
147 | 146 | ||
148 | if ((ret == NULL) && (node->right_child != NULL)) | 147 | if (node->left_child != NULL) |
149 | { | 148 | ret = find_element (node->left_child, element); |
150 | ret = find_element (node->right_child, element); | 149 | |
151 | } | 150 | if (node->right_child != NULL) |
151 | ret = find_element (node->right_child, element); | ||
152 | 152 | ||
153 | return ret; | 153 | return ret; |
154 | } | 154 | } |
@@ -241,21 +241,6 @@ swapNodes (struct GNUNET_CONTAINER_heap_node *first, | |||
241 | second->element = temp_element; | 241 | second->element = temp_element; |
242 | second->cost = temp_cost; | 242 | second->cost = temp_cost; |
243 | 243 | ||
244 | /* | ||
245 | * I still worry that there is some good reason for | ||
246 | * elements being location aware... but it eludes me | ||
247 | * for the moment... | ||
248 | if ((root->type == GNUNET_DV_MAX_HEAP)) | ||
249 | { | ||
250 | first->neighbor->max_loc = first; | ||
251 | second->neighbor->max_loc = second; | ||
252 | } | ||
253 | else if ((root->type == GNUNET_DV_MIN_HEAP)) | ||
254 | { | ||
255 | first->neighbor->min_loc = first; | ||
256 | second->neighbor->min_loc = second; | ||
257 | } | ||
258 | */ | ||
259 | return; | 244 | return; |
260 | } | 245 | } |
261 | 246 | ||
@@ -393,11 +378,6 @@ GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *root, | |||
393 | new_pos->element = element; | 378 | new_pos->element = element; |
394 | new_pos->cost = cost; | 379 | new_pos->cost = cost; |
395 | root->size++; | 380 | root->size++; |
396 | /*We no longer can tolerate pointers between heaps :( */ | ||
397 | /*if (root->type == GNUNET_DV_MIN_HEAP) | ||
398 | new_pos->neighbor->min_loc = new_pos; | ||
399 | else if (root->type == GNUNET_DV_MAX_HEAP) | ||
400 | new_pos->neighbor->max_loc = new_pos; */ | ||
401 | 381 | ||
402 | percolateHeap (new_pos, root); | 382 | percolateHeap (new_pos, root); |
403 | } | 383 | } |
@@ -416,11 +396,14 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *root) | |||
416 | struct GNUNET_CONTAINER_heap_node *root_node; | 396 | struct GNUNET_CONTAINER_heap_node *root_node; |
417 | struct GNUNET_CONTAINER_heap_node *last; | 397 | struct GNUNET_CONTAINER_heap_node *last; |
418 | 398 | ||
399 | if ((root == NULL) || (root->size == 0) || (root->root == NULL)) | ||
400 | return NULL; | ||
401 | |||
419 | root_node = root->root; | 402 | root_node = root->root; |
420 | ret = root_node->element; | 403 | ret = root_node->element; |
421 | last = getPos (root, root->size); | 404 | last = getPos (root, root->size); |
422 | 405 | ||
423 | if ((root_node == last) && (root->size == 1)) | 406 | if ((root_node == last) && (root->size == 1)) |
424 | { | 407 | { |
425 | /* We are removing the last node in the heap! */ | 408 | /* We are removing the last node in the heap! */ |
426 | root->root = NULL; | 409 | root->root = NULL; |