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);
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))
401 root_node = root->root;
402 ret = root_node->element;
403 last = getPos (root, root->size);
405 if ((root_node == last) && (root->size == 1))
407 /* We are removing the last node in the heap! */
410 root->traversal_pos = NULL;
411 GNUNET_assert (0 == --root->size);
415 if (last->parent->left_child == last)
416 last->parent->left_child = NULL;
417 else if (last->parent->right_child == last)
418 last->parent->right_child = NULL;
420 root_node->element = last->element;
421 root_node->cost = last->cost;
423 if (root->traversal_pos == last)
424 root->traversal_pos = root->root;
427 percolateDownHeap (root->root, root);
432 updatedCost (struct GNUNET_CONTAINER_Heap *root,
433 struct GNUNET_CONTAINER_heap_node *node)
435 struct GNUNET_CONTAINER_heap_node *parent;
438 return GNUNET_SYSERR;
440 parent = node->parent;
442 if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX) && (parent != NULL)
443 && (node->cost > parent->cost))
444 percolateHeap (node, root);
445 else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN) && (parent != NULL)
446 && (node->cost < parent->cost))
447 percolateHeap (node, root);
448 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
449 percolateDownHeap (node, root);
450 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
451 percolateDownHeap (node, root);
458 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *root,
460 GNUNET_CONTAINER_HeapCost new_cost)
462 struct GNUNET_CONTAINER_heap_node *node;
463 int ret = GNUNET_YES;
464 node = find_element (root->root, element);
468 node->cost = new_cost;
469 ret = updatedCost (root, node);
474 internal_iterator (struct GNUNET_CONTAINER_Heap *root,
475 struct GNUNET_CONTAINER_heap_node *node,
476 GNUNET_CONTAINER_HeapIterator iterator, void *cls)
480 internal_iterator (root, node->left_child, iterator, cls);
481 internal_iterator (root, node->right_child, iterator, cls);
482 iterator (cls, node->element, node->cost);
486 GNUNET_CONTAINER_heap_iterate (struct GNUNET_CONTAINER_Heap *heap,
487 GNUNET_CONTAINER_HeapIterator iterator,
490 internal_iterator (heap, heap->root, iterator, cls);
495 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *root)
500 if ((root->traversal_pos == NULL) && (root->root != NULL))
502 root->traversal_pos = root->root;
505 if (root->traversal_pos == NULL)
508 element = root->traversal_pos->element;
510 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
515 root->traversal_pos = root->traversal_pos->right_child;
518 root->traversal_pos = root->traversal_pos->left_child;
527 GNUNET_CONTAINER_heap_get_size (struct GNUNET_CONTAINER_Heap *heap)