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 * Struct that is stored in hashmap, pointers to
33 * locations in min_heap and max_heap.
35 struct GNUNET_CONTAINER_heap_info
37 struct GNUNET_CONTAINER_heap_node *min_loc;
39 struct GNUNET_CONTAINER_heap_node *max_loc;
44 * Generic heap node structure, contains pointer to parent
45 * left child, right child, and actual neighbor.
47 struct GNUNET_CONTAINER_heap_node
49 struct GNUNET_CONTAINER_heap_node *parent;
51 struct GNUNET_CONTAINER_heap_node *left_child;
53 struct GNUNET_CONTAINER_heap_node *right_child;
55 GNUNET_CONTAINER_HeapCost cost;
61 struct GNUNET_CONTAINER_Heap
65 unsigned int max_size;
67 enum GNUNET_CONTAINER_HeapOrder type;
69 struct GNUNET_CONTAINER_heap_node *root;
71 struct GNUNET_CONTAINER_heap_node *traversal_pos;
77 * Returns element stored at root of tree, doesn't effect anything
79 * @param heap the heap
80 * @return NULL if the heap is empty
82 void *GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap)
89 internal_print (struct GNUNET_CONTAINER_heap_node *root)
91 fprintf (stdout, "%llu\n", (unsigned long long) root->cost);
92 if (root->left_child != NULL)
94 fprintf (stdout, "LEFT of %llu\n", (unsigned long long) root->cost);
95 internal_print (root->left_child);
97 if (root->right_child != NULL)
99 fprintf (stdout, "RIGHT of %llu\n", (unsigned long long) root->cost);
100 internal_print (root->right_child);
105 printTree (struct GNUNET_CONTAINER_Heap *root)
107 internal_print (root->root);
110 struct GNUNET_CONTAINER_Heap *
111 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder type)
113 struct GNUNET_CONTAINER_Heap *heap;
114 heap = malloc (sizeof (struct GNUNET_CONTAINER_Heap));
118 heap->traversal_pos = NULL;
125 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
127 while (heap->size > 0)
128 GNUNET_CONTAINER_heap_remove_root (heap);
133 struct GNUNET_CONTAINER_heap_node *
134 find_element (struct GNUNET_CONTAINER_heap_node *node, void *element)
136 struct GNUNET_CONTAINER_heap_node *ret;
138 if ((node != NULL) && (node->element == element))
143 if ((ret == NULL) && (node->left_child != NULL))
145 ret = find_element (node->left_child, element);
148 if ((ret == NULL) && (node->right_child != NULL))
150 ret = find_element (node->right_child, element);
156 static struct GNUNET_CONTAINER_heap_node *
157 getNextPos (struct GNUNET_CONTAINER_Heap *root)
159 struct GNUNET_CONTAINER_heap_node *ret;
160 struct GNUNET_CONTAINER_heap_node *parent;
165 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_heap_node));
166 pos = root->size + 1;
167 depth = (int) log2 (pos);
168 ret->left_child = NULL;
169 ret->right_child = NULL;
179 for (i = depth; i > 1; i--)
181 if (((pos / (1 << (i - 1))) % 2) == 0)
182 parent = parent->left_child;
184 parent = parent->right_child;
187 ret->parent = parent;
189 parent->left_child = ret;
191 parent->right_child = ret;
199 static struct GNUNET_CONTAINER_heap_node *
200 getPos (struct GNUNET_CONTAINER_Heap *root, unsigned int pos)
202 struct GNUNET_CONTAINER_heap_node *ret;
207 depth = (int) log2 (pos);
209 if (pos > root->size)
216 for (i = depth; i > 0; i--)
218 if (((pos / (1 << (i - 1))) % 2) == 0)
219 ret = ret->left_child;
221 ret = ret->right_child;
230 swapNodes (struct GNUNET_CONTAINER_heap_node *first,
231 struct GNUNET_CONTAINER_heap_node *second,
232 struct GNUNET_CONTAINER_Heap *root)
235 GNUNET_CONTAINER_HeapCost temp_cost;
237 temp_element = first->element;
238 temp_cost = first->cost;
239 first->element = second->element;
240 first->cost = second->cost;
241 second->element = temp_element;
242 second->cost = temp_cost;
245 * I still worry that there is some good reason for
246 * elements being location aware... but it eludes me
248 if ((root->type == GNUNET_DV_MAX_HEAP))
250 first->neighbor->max_loc = first;
251 second->neighbor->max_loc = second;
253 else if ((root->type == GNUNET_DV_MIN_HEAP))
255 first->neighbor->min_loc = first;
256 second->neighbor->min_loc = second;
263 percolateHeap (struct GNUNET_CONTAINER_heap_node *pos,
264 struct GNUNET_CONTAINER_Heap *root)
267 while ((pos->parent != NULL) &&
268 (((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
269 && (pos->parent->cost < pos->cost))
270 || ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
271 && (pos->parent->cost > pos->cost))))
273 swapNodes (pos, pos->parent, root);
283 percolateDownHeap (struct GNUNET_CONTAINER_heap_node *pos,
284 struct GNUNET_CONTAINER_Heap *root)
286 struct GNUNET_CONTAINER_heap_node *switchNeighbor;
288 switchNeighbor = pos;
290 if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX))
292 if ((pos->left_child != NULL)
293 && (pos->left_child->cost > switchNeighbor->cost))
295 switchNeighbor = pos->left_child;
298 if ((pos->right_child != NULL)
299 && (pos->right_child->cost > switchNeighbor->cost))
301 switchNeighbor = pos->right_child;
304 else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN))
306 if ((pos->left_child != NULL)
307 && (pos->left_child->cost < switchNeighbor->cost))
309 switchNeighbor = pos->left_child;
312 if ((pos->right_child != NULL)
313 && (pos->right_child->cost < switchNeighbor->cost))
315 switchNeighbor = pos->right_child;
319 if (switchNeighbor != pos)
321 swapNodes (switchNeighbor, pos, root);
322 percolateDownHeap (switchNeighbor, root);
329 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *root,
333 struct GNUNET_CONTAINER_heap_node *del_node;
334 struct GNUNET_CONTAINER_heap_node *last;
335 GNUNET_CONTAINER_HeapCost old_cost;
338 del_node = find_element (root->root, element);
340 if (del_node == NULL)
342 else if (del_node == root->root)
343 return GNUNET_CONTAINER_heap_remove_root (root);
345 ret = del_node->element;
346 last = getPos (root, root->size);
348 old_cost = del_node->cost;
349 del_node->element = last->element;
350 del_node->cost = last->cost;
352 if (last->parent->left_child == last)
353 last->parent->left_child = NULL;
354 if (last->parent->right_child == last)
355 last->parent->right_child = NULL;
357 if (root->traversal_pos == last)
359 root->traversal_pos = root->root;
364 if (del_node->cost > old_cost)
366 if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
367 percolateHeap (del_node, root);
368 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
369 percolateDownHeap (del_node, root);
371 else if (del_node->cost < old_cost)
373 if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
374 percolateDownHeap (del_node, root);
375 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
376 percolateHeap (del_node, root);
383 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *root,
384 void *element, GNUNET_CONTAINER_HeapCost cost)
386 struct GNUNET_CONTAINER_heap_node *new_pos;
390 if (root->max_size > root->size)
392 new_pos = getNextPos (root);
393 new_pos->element = element;
394 new_pos->cost = cost;
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; */
402 percolateHeap (new_pos, root);
413 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *root)
416 struct GNUNET_CONTAINER_heap_node *root_node;
417 struct GNUNET_CONTAINER_heap_node *last;
419 root_node = root->root;
420 ret = root_node->element;
421 last = getPos (root, root->size);
423 if ((root_node == last) && (root->size == 1))
425 /* We are removing the last node in the heap! */
427 root->traversal_pos = NULL;
432 if (last->parent->left_child == last)
433 last->parent->left_child = NULL;
434 else if (last->parent->right_child == last)
435 last->parent->right_child = NULL;
437 root_node->element = last->element;
438 root_node->cost = last->cost;
440 if (root->traversal_pos == last)
442 root->traversal_pos = root->root;
447 percolateDownHeap (root->root, root);
452 updatedCost (struct GNUNET_CONTAINER_Heap *root,
453 struct GNUNET_CONTAINER_heap_node *node)
455 struct GNUNET_CONTAINER_heap_node *parent;
458 return GNUNET_SYSERR;
460 parent = node->parent;
462 if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX) && (parent != NULL)
463 && (node->cost > parent->cost))
464 percolateHeap (node, root);
465 else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN) && (parent != NULL)
466 && (node->cost < parent->cost))
467 percolateHeap (node, root);
468 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
469 percolateDownHeap (node, root);
470 else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
471 percolateDownHeap (node, root);
478 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *root,
480 GNUNET_CONTAINER_HeapCost new_cost)
482 struct GNUNET_CONTAINER_heap_node *node;
483 int ret = GNUNET_YES;
484 node = find_element (root->root, element);
488 node->cost = new_cost;
489 ret = updatedCost (root, node);
494 internal_iterator (struct GNUNET_CONTAINER_Heap *root,
495 struct GNUNET_CONTAINER_heap_node *node,
496 GNUNET_CONTAINER_HeapIterator iterator, void *cls)
500 internal_iterator (root, node->left_child, iterator, cls);
501 internal_iterator (root, node->right_child, iterator, cls);
502 iterator (cls, node->element, node->cost);
506 GNUNET_CONTAINER_heap_iterate (struct GNUNET_CONTAINER_Heap *heap,
507 GNUNET_CONTAINER_HeapIterator iterator,
510 internal_iterator (heap, heap->root, iterator, cls);
515 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *root)
520 if ((root->traversal_pos == NULL) && (root->root != NULL))
522 root->traversal_pos = root->root;
525 if (root->traversal_pos == NULL)
528 element = root->traversal_pos->element;
530 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
535 root->traversal_pos = root->traversal_pos->right_child;
538 root->traversal_pos = root->traversal_pos->left_child;
547 GNUNET_CONTAINER_heap_get_size (struct GNUNET_CONTAINER_Heap *heap)