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
70 void *GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap)
72 if ((heap == NULL) || (heap->root == NULL))
75 return heap->root->element;
79 next_power_of_2(int v)
92 internal_print (struct GNUNET_CONTAINER_heap_node *root)
94 fprintf (stdout, "%llu\n", (unsigned long long) root->cost);
95 if (root->left_child != NULL)
97 fprintf (stdout, "LEFT of %llu\n", (unsigned long long) root->cost);
98 internal_print (root->left_child);
100 if (root->right_child != NULL)
102 fprintf (stdout, "RIGHT of %llu\n", (unsigned long long) root->cost);
103 internal_print (root->right_child);
108 printTree (struct GNUNET_CONTAINER_Heap *root)
110 internal_print (root->root);
114 struct GNUNET_CONTAINER_Heap *
115 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder type)
117 struct GNUNET_CONTAINER_Heap *heap;
118 heap = malloc (sizeof (struct GNUNET_CONTAINER_Heap));
122 heap->traversal_pos = NULL;
129 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
131 while (heap->size > 0)
132 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);
329 old_cost = del_node->cost;
330 del_node->element = last->element;
331 del_node->cost = last->cost;
333 if (last->parent->left_child == last)
334 last->parent->left_child = NULL;
335 if (last->parent->right_child == last)
336 last->parent->right_child = NULL;
338 if (root->traversal_pos == last)
340 root->traversal_pos = root->root;
345 if (del_node->cost > old_cost)
347 if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
348 percolateHeap (del_node, root);
349 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
350 percolateDownHeap (del_node, root);
352 else if (del_node->cost < old_cost)
354 if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
355 percolateDownHeap (del_node, root);
356 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
357 percolateHeap (del_node, root);
364 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *root,
365 void *element, GNUNET_CONTAINER_HeapCost cost)
367 struct GNUNET_CONTAINER_heap_node *new_pos;
371 if (root->max_size > root->size)
373 new_pos = getNextPos (root);
374 new_pos->element = element;
375 new_pos->cost = cost;
378 percolateHeap (new_pos, root);
389 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *root)
392 struct GNUNET_CONTAINER_heap_node *root_node;
393 struct GNUNET_CONTAINER_heap_node *last;
395 if ((root == NULL) || (root->size == 0) || (root->root == NULL))
398 root_node = root->root;
399 ret = root_node->element;
400 last = getPos (root, root->size);
402 if ((root_node == last) && (root->size == 1))
404 /* We are removing the last node in the heap! */
406 root->traversal_pos = NULL;
407 GNUNET_assert (0 == --root->size);
411 if (last->parent->left_child == last)
412 last->parent->left_child = NULL;
413 else if (last->parent->right_child == last)
414 last->parent->right_child = NULL;
416 root_node->element = last->element;
417 root_node->cost = last->cost;
419 if (root->traversal_pos == last)
421 root->traversal_pos = root->root;
426 percolateDownHeap (root->root, root);
431 updatedCost (struct GNUNET_CONTAINER_Heap *root,
432 struct GNUNET_CONTAINER_heap_node *node)
434 struct GNUNET_CONTAINER_heap_node *parent;
437 return GNUNET_SYSERR;
439 parent = node->parent;
441 if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX) && (parent != NULL)
442 && (node->cost > parent->cost))
443 percolateHeap (node, root);
444 else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN) && (parent != NULL)
445 && (node->cost < parent->cost))
446 percolateHeap (node, root);
447 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
448 percolateDownHeap (node, root);
449 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
450 percolateDownHeap (node, root);
457 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *root,
459 GNUNET_CONTAINER_HeapCost new_cost)
461 struct GNUNET_CONTAINER_heap_node *node;
462 int ret = GNUNET_YES;
463 node = find_element (root->root, element);
467 node->cost = new_cost;
468 ret = updatedCost (root, node);
473 internal_iterator (struct GNUNET_CONTAINER_Heap *root,
474 struct GNUNET_CONTAINER_heap_node *node,
475 GNUNET_CONTAINER_HeapIterator iterator, void *cls)
479 internal_iterator (root, node->left_child, iterator, cls);
480 internal_iterator (root, node->right_child, iterator, cls);
481 iterator (cls, node->element, node->cost);
485 GNUNET_CONTAINER_heap_iterate (struct GNUNET_CONTAINER_Heap *heap,
486 GNUNET_CONTAINER_HeapIterator iterator,
489 internal_iterator (heap, heap->root, iterator, cls);
494 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *root)
499 if ((root->traversal_pos == NULL) && (root->root != NULL))
501 root->traversal_pos = root->root;
504 if (root->traversal_pos == NULL)
507 element = root->traversal_pos->element;
509 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
514 root->traversal_pos = root->traversal_pos->right_child;
517 root->traversal_pos = root->traversal_pos->left_child;
526 GNUNET_CONTAINER_heap_get_size (struct GNUNET_CONTAINER_Heap *heap)