2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @author Nathan Evans
23 * @file util/container_heap.c
24 * @brief Implementation of heap operations
28 #include "gnunet_protocols.h"
29 #include "gnunet_util_lib.h"
32 * Generic heap node structure, contains pointer to parent
33 * left child, right child, and actual neighbor.
35 struct GNUNET_CONTAINER_heap_node
37 struct GNUNET_CONTAINER_heap_node *parent;
39 struct GNUNET_CONTAINER_heap_node *left_child;
41 struct GNUNET_CONTAINER_heap_node *right_child;
43 GNUNET_CONTAINER_HeapCost cost;
49 struct GNUNET_CONTAINER_Heap
53 unsigned int max_size;
55 enum GNUNET_CONTAINER_HeapOrder type;
57 struct GNUNET_CONTAINER_heap_node *root;
59 struct GNUNET_CONTAINER_heap_node *traversal_pos;
65 * Returns element stored at root of tree, doesn't effect anything
67 * @param heap the heap
68 * @return NULL if the heap is empty
71 GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap)
73 if ((heap == NULL) || (heap->root == NULL))
76 return heap->root->element;
80 next_power_of_2 (int v)
93 internal_print (struct GNUNET_CONTAINER_heap_node *root)
95 fprintf (stdout, "%llu\n", (unsigned long long) root->cost);
96 if (root->left_child != NULL)
98 fprintf (stdout, "LEFT of %llu\n", (unsigned long long) root->cost);
99 internal_print (root->left_child);
101 if (root->right_child != NULL)
103 fprintf (stdout, "RIGHT of %llu\n", (unsigned long long) root->cost);
104 internal_print (root->right_child);
109 printTree (struct GNUNET_CONTAINER_Heap *root)
111 internal_print (root->root);
115 struct GNUNET_CONTAINER_Heap *
116 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder type)
118 struct GNUNET_CONTAINER_Heap *heap;
119 heap = malloc (sizeof (struct GNUNET_CONTAINER_Heap));
123 heap->traversal_pos = NULL;
130 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
132 while (heap->size > 0)
133 GNUNET_CONTAINER_heap_remove_root (heap);
137 static struct GNUNET_CONTAINER_heap_node *
138 find_element (struct GNUNET_CONTAINER_heap_node *node, void *element)
140 struct GNUNET_CONTAINER_heap_node *ret;
145 if (node->element == element)
148 if (node->left_child != NULL)
149 ret = find_element (node->left_child, element);
151 if ((ret == NULL) && (node->right_child != NULL))
152 ret = find_element (node->right_child, element);
157 static struct GNUNET_CONTAINER_heap_node *
158 getNextPos (struct GNUNET_CONTAINER_Heap *root)
160 struct GNUNET_CONTAINER_heap_node *ret;
161 struct GNUNET_CONTAINER_heap_node *parent;
165 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_heap_node));
166 pos = root->size + 1;
167 ret->left_child = NULL;
168 ret->right_child = NULL;
178 for (i = next_power_of_2 (pos) >> 2; i > 1; i >>= 1)
180 if (((pos / i) % 2) == 0)
181 parent = parent->left_child;
183 parent = parent->right_child;
186 ret->parent = parent;
188 parent->left_child = ret;
190 parent->right_child = ret;
198 static struct GNUNET_CONTAINER_heap_node *
199 getPos (struct GNUNET_CONTAINER_Heap *root, unsigned int pos)
201 struct GNUNET_CONTAINER_heap_node *ret;
205 if (pos > root->size)
212 for (i = next_power_of_2 (pos) >> 2; i > 0; i >>= 1)
214 if (((pos / i) % 2) == 0)
215 ret = ret->left_child;
217 ret = ret->right_child;
226 swapNodes (struct GNUNET_CONTAINER_heap_node *first,
227 struct GNUNET_CONTAINER_heap_node *second,
228 struct GNUNET_CONTAINER_Heap *root)
231 GNUNET_CONTAINER_HeapCost temp_cost;
233 temp_element = first->element;
234 temp_cost = first->cost;
235 first->element = second->element;
236 first->cost = second->cost;
237 second->element = temp_element;
238 second->cost = temp_cost;
244 percolateHeap (struct GNUNET_CONTAINER_heap_node *pos,
245 struct GNUNET_CONTAINER_Heap *root)
248 while ((pos->parent != NULL) &&
249 (((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
250 && (pos->parent->cost < pos->cost))
251 || ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
252 && (pos->parent->cost > pos->cost))))
254 swapNodes (pos, pos->parent, root);
264 percolateDownHeap (struct GNUNET_CONTAINER_heap_node *pos,
265 struct GNUNET_CONTAINER_Heap *root)
267 struct GNUNET_CONTAINER_heap_node *switchNeighbor;
269 switchNeighbor = pos;
271 if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX))
273 if ((pos->left_child != NULL)
274 && (pos->left_child->cost > switchNeighbor->cost))
276 switchNeighbor = pos->left_child;
279 if ((pos->right_child != NULL)
280 && (pos->right_child->cost > switchNeighbor->cost))
282 switchNeighbor = pos->right_child;
285 else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN))
287 if ((pos->left_child != NULL)
288 && (pos->left_child->cost < switchNeighbor->cost))
290 switchNeighbor = pos->left_child;
293 if ((pos->right_child != NULL)
294 && (pos->right_child->cost < switchNeighbor->cost))
296 switchNeighbor = pos->right_child;
300 if (switchNeighbor != pos)
302 swapNodes (switchNeighbor, pos, root);
303 percolateDownHeap (switchNeighbor, root);
310 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *root,
314 struct GNUNET_CONTAINER_heap_node *del_node;
315 struct GNUNET_CONTAINER_heap_node *last;
316 GNUNET_CONTAINER_HeapCost old_cost;
319 del_node = find_element (root->root, element);
321 if (del_node == NULL)
323 else if (del_node == root->root)
324 return GNUNET_CONTAINER_heap_remove_root (root);
326 ret = del_node->element;
327 last = getPos (root, root->size);
331 old_cost = del_node->cost;
332 del_node->element = last->element;
333 del_node->cost = last->cost;
335 if (last->parent->left_child == last)
336 last->parent->left_child = NULL;
337 if (last->parent->right_child == last)
338 last->parent->right_child = NULL;
340 if (root->traversal_pos == last)
342 root->traversal_pos = root->root;
345 if (last == del_node)
353 if (del_node->cost > old_cost)
355 if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
356 percolateHeap (del_node, root);
357 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
358 percolateDownHeap (del_node, root);
360 else if (del_node->cost < old_cost)
362 if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
363 percolateDownHeap (del_node, root);
364 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
365 percolateHeap (del_node, root);
372 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *root,
373 void *element, GNUNET_CONTAINER_HeapCost cost)
375 struct GNUNET_CONTAINER_heap_node *new_pos;
379 if (root->max_size > root->size)
381 new_pos = getNextPos (root);
382 new_pos->element = element;
383 new_pos->cost = cost;
386 percolateHeap (new_pos, root);
397 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *root)
400 struct GNUNET_CONTAINER_heap_node *root_node;
401 struct GNUNET_CONTAINER_heap_node *last;
403 if ((root == NULL) || (root->size == 0) || (root->root == NULL))
409 root_node = root->root;
410 ret = root_node->element;
411 last = getPos (root, root->size);
413 if ((root_node == last) && (root->size == 1))
415 /* We are removing the last node in the heap! */
418 root->traversal_pos = NULL;
419 GNUNET_assert (0 == --root->size);
423 if (last->parent->left_child == last)
424 last->parent->left_child = NULL;
425 else if (last->parent->right_child == last)
426 last->parent->right_child = NULL;
428 root_node->element = last->element;
429 root_node->cost = last->cost;
431 if (root->traversal_pos == last)
432 root->traversal_pos = root->root;
435 percolateDownHeap (root->root, root);
440 updatedCost (struct GNUNET_CONTAINER_Heap *root,
441 struct GNUNET_CONTAINER_heap_node *node)
443 struct GNUNET_CONTAINER_heap_node *parent;
446 return GNUNET_SYSERR;
448 parent = node->parent;
450 if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX) && (parent != NULL)
451 && (node->cost > parent->cost))
452 percolateHeap (node, root);
453 else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN) && (parent != NULL)
454 && (node->cost < parent->cost))
455 percolateHeap (node, root);
456 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
457 percolateDownHeap (node, root);
458 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
459 percolateDownHeap (node, root);
466 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *root,
468 GNUNET_CONTAINER_HeapCost new_cost)
470 struct GNUNET_CONTAINER_heap_node *node;
471 int ret = GNUNET_YES;
472 node = find_element (root->root, element);
476 node->cost = new_cost;
477 ret = updatedCost (root, node);
482 internal_iterator (struct GNUNET_CONTAINER_Heap *root,
483 struct GNUNET_CONTAINER_heap_node *node,
484 GNUNET_CONTAINER_HeapIterator iterator, void *cls)
488 internal_iterator (root, node->left_child, iterator, cls);
489 internal_iterator (root, node->right_child, iterator, cls);
490 iterator (cls, node->element, node->cost);
494 GNUNET_CONTAINER_heap_iterate (struct GNUNET_CONTAINER_Heap *heap,
495 GNUNET_CONTAINER_HeapIterator iterator,
498 internal_iterator (heap, heap->root, iterator, cls);
503 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *root)
508 if ((root->traversal_pos == NULL) && (root->root != NULL))
510 root->traversal_pos = root->root;
513 if (root->traversal_pos == NULL)
516 element = root->traversal_pos->element;
518 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
523 root->traversal_pos = root->traversal_pos->right_child;
526 root->traversal_pos = root->traversal_pos->left_child;
535 GNUNET_CONTAINER_heap_get_size (struct GNUNET_CONTAINER_Heap *heap)