2 This file is part of GNUnet.
3 (C) 2008, 2009 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 * @file util/container_heap.c
23 * @brief Implementation of a heap
24 * @author Nathan Evans
25 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
37 struct GNUNET_CONTAINER_HeapNode
40 * Heap this node belongs to.
42 struct GNUNET_CONTAINER_Heap *heap;
47 struct GNUNET_CONTAINER_HeapNode *parent;
52 struct GNUNET_CONTAINER_HeapNode *left_child;
57 struct GNUNET_CONTAINER_HeapNode *right_child;
65 * Cost for this element.
67 GNUNET_CONTAINER_HeapCostType cost;
70 * Number of elements below this node in the heap
71 * (excluding this node itself).
73 unsigned int tree_size;
78 * Handle to a node in a heap.
80 struct GNUNET_CONTAINER_Heap
86 struct GNUNET_CONTAINER_HeapNode *root;
89 * Current position of our random walk.
91 struct GNUNET_CONTAINER_HeapNode *walk_pos;
94 * Number of elements in the heap.
99 * How is the heap sorted?
101 enum GNUNET_CONTAINER_HeapOrder order;
108 * Check if internal invariants hold for the given node.
110 * @param node subtree to check
113 check (const struct GNUNET_CONTAINER_HeapNode *node)
117 GNUNET_assert (node->tree_size ==
118 ( (node->left_child == NULL) ? 0 : 1 + node->left_child->tree_size) +
119 ( (node->right_child == NULL) ? 0 : 1 + node->right_child->tree_size) );
120 check (node->left_child);
121 check (node->right_child);
125 #define CHECK(n) check(n)
127 #define CHECK(n) do {} while (0)
134 * @param order how should the heap be sorted?
135 * @return handle to the heap
137 struct GNUNET_CONTAINER_Heap *
138 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order)
140 struct GNUNET_CONTAINER_Heap *heap;
142 heap = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_Heap));
149 * Destroys the heap. Only call on a heap that
152 * @param heap heap to destroy
155 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
157 GNUNET_break (heap->size == 0);
163 * Get element stored at root of heap.
165 * @param heap heap to inspect
166 * @return NULL if heap is empty
169 GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap)
171 if (heap->root == NULL)
173 return heap->root->element;
178 * Get the current size of the heap
180 * @param heap the heap to get the size of
181 * @return number of elements stored
184 GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap)
191 * Get the current cost of the node
193 * @param node the node to get the cost of
194 * @return cost of the node
196 GNUNET_CONTAINER_HeapCostType
197 GNUNET_CONTAINER_node_get_cost (const struct GNUNET_CONTAINER_HeapNode *node)
203 * Iterate over the children of the given node.
205 * @param heap argument to give to iterator
206 * @param node node to iterate over
207 * @param iterator function to call on each node
208 * @param iterator_cls closure for iterator
209 * @return GNUNET_YES to continue to iterate
212 node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
213 struct GNUNET_CONTAINER_HeapNode *node,
214 GNUNET_CONTAINER_HeapIterator iterator,
219 if (GNUNET_YES != node_iterator (heap,
224 if (GNUNET_YES != node_iterator (heap,
229 return iterator (iterator_cls,
237 * Iterate over all entries in the heap.
239 * @param heap the heap
240 * @param iterator function to call on each entry
241 * @param iterator_cls closure for iterator
244 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
245 GNUNET_CONTAINER_HeapIterator iterator,
248 (void) node_iterator (heap, heap->root, iterator, iterator_cls);
253 * Perform a random walk of the tree. The walk is biased
254 * towards elements closer to the root of the tree (since
255 * each walk starts at the root and ends at a random leaf).
256 * The heap internally tracks the current position of the
259 * @param heap heap to walk
260 * @return data stored at the next random node in the walk;
261 * NULL if the tree is empty.
264 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
266 struct GNUNET_CONTAINER_HeapNode *pos;
269 if (heap->root == NULL)
271 pos = heap->walk_pos;
274 element = pos->element;
276 = (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2))
284 * Insert the given node 'node' into the subtree starting
285 * at 'pos' (while keeping the tree somewhat balanced).
287 * @param heap heap to modify
288 * @param pos existing tree
289 * @param node node to insert (which may be a subtree itself)
292 insert_node (struct GNUNET_CONTAINER_Heap *heap,
293 struct GNUNET_CONTAINER_HeapNode *pos,
294 struct GNUNET_CONTAINER_HeapNode *node)
296 struct GNUNET_CONTAINER_HeapNode *parent;
298 GNUNET_assert (node->parent == NULL);
299 while ( (heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX)
300 ? (pos->cost >= node->cost)
301 : (pos->cost <= node->cost) )
303 /* node is descendent of pos */
304 pos->tree_size += (1 + node->tree_size);
305 if (pos->left_child == NULL)
307 pos->left_child = node;
311 if (pos->right_child == NULL)
313 pos->right_child = node;
317 /* keep it balanced by descending into smaller subtree */
318 if (pos->left_child->tree_size < pos->right_child->tree_size)
319 pos = pos->left_child;
321 pos = pos->right_child;
323 /* make 'node' parent of 'pos' */
324 parent = pos->parent;
326 node->parent = parent;
333 if (parent->left_child == pos)
334 parent->left_child = node;
336 parent->right_child = node;
338 /* insert 'pos' below 'node' */
339 insert_node (heap, node, pos);
345 * Inserts a new element into the heap.
347 * @param heap heap to modify
348 * @param element element to insert
349 * @param cost cost for the element
350 * @return node for the new element
352 struct GNUNET_CONTAINER_HeapNode *
353 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
355 GNUNET_CONTAINER_HeapCostType cost)
357 struct GNUNET_CONTAINER_HeapNode *node;
359 node = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_HeapNode));
361 node->element = element;
364 if (NULL == heap->root)
367 insert_node (heap, heap->root, node);
368 GNUNET_assert (heap->size == heap->root->tree_size + 1);
375 * Remove root of the heap.
377 * @param heap heap to modify
378 * @return element data stored at the root node, NULL if heap is empty
381 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
384 struct GNUNET_CONTAINER_HeapNode *root;
386 if (NULL == (root = heap->root))
390 if (root->left_child == NULL)
392 heap->root = root->right_child;
393 if (root->right_child != NULL)
394 root->right_child->parent = NULL;
396 else if (root->right_child == NULL)
398 heap->root = root->left_child;
399 root->left_child->parent = NULL;
403 root->left_child->parent = NULL;
404 root->right_child->parent = NULL;
405 heap->root = root->left_child;
406 insert_node (heap, heap->root, root->right_child);
410 GNUNET_assert (( (heap->size == 0) &&
411 (heap->root == NULL) ) ||
412 (heap->size == heap->root->tree_size + 1) );
420 * Remove the given node 'node' from the tree and update
421 * the 'tree_size' fields accordingly. Preserves the
422 * children of 'node' and does NOT change the overall
423 * 'size' field of the tree.
426 remove_node (struct GNUNET_CONTAINER_HeapNode *node)
428 struct GNUNET_CONTAINER_HeapNode *ancestor;
429 struct GNUNET_CONTAINER_Heap *heap = node->heap;
431 /* update 'size' of the ancestors */
433 while (NULL != (ancestor = ancestor->parent))
434 ancestor->tree_size--;
436 /* update 'size' of node itself */
437 if (node->left_child != NULL)
438 node->tree_size -= (1 + node->left_child->tree_size);
439 if (node->right_child != NULL)
440 node->tree_size -= (1 + node->right_child->tree_size);
442 /* unlink 'node' itself and insert children in its place */
443 if (node->parent == NULL)
445 if (node->left_child != NULL)
447 heap->root = node->left_child;
448 node->left_child->parent = NULL;
449 if (node->right_child != NULL)
451 node->right_child->parent = NULL;
452 insert_node (heap, heap->root, node->right_child);
457 heap->root = node->right_child;
458 if (node->right_child != NULL)
459 node->right_child->parent = NULL;
464 if (node->parent->left_child == node)
465 node->parent->left_child = NULL;
467 node->parent->right_child = NULL;
468 if (node->left_child != NULL)
470 node->left_child->parent = NULL;
471 node->parent->tree_size -= (1 + node->left_child->tree_size);
472 insert_node (heap, node->parent, node->left_child);
474 if (node->right_child != NULL)
476 node->right_child->parent = NULL;
477 node->parent->tree_size -= (1 + node->right_child->tree_size);
478 insert_node (heap, node->parent, node->right_child);
482 node->left_child = NULL;
483 node->right_child = NULL;
484 GNUNET_assert (node->tree_size == 0);
490 * Removes a node from the heap.
492 * @param node node to remove
493 * @return element data stored at the node
496 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
499 struct GNUNET_CONTAINER_Heap *heap;
503 if (heap->walk_pos == node)
504 (void) GNUNET_CONTAINER_heap_walk_get_next (heap);
508 if (heap->walk_pos == node)
509 heap->walk_pos = NULL;
513 GNUNET_assert (( (heap->size == 0) &&
514 (heap->root == NULL) ) ||
515 (heap->size == heap->root->tree_size + 1) );
522 * Updates the cost of any node in the tree
524 * @param heap heap to modify
525 * @param node node for which the cost is to be changed
526 * @param new_cost new cost for the node
529 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
530 struct GNUNET_CONTAINER_HeapNode *node,
531 GNUNET_CONTAINER_HeapCostType new_cost)
534 GNUNET_assert ( ( (heap->size == 0) &&
535 (heap->root == NULL) ) ||
536 (heap->size == heap->root->tree_size + 1) );
542 GNUNET_assert ( ( (heap->size == 1) &&
543 (heap->root == NULL) ) ||
544 (heap->size == heap->root->tree_size + 2) );
546 node->cost = new_cost;
547 if (heap->root == NULL)
550 insert_node (heap, heap->root, node);
553 GNUNET_assert ( ( (heap->size == 0) &&
554 (heap->root == NULL) ) ||
555 (heap->size == heap->root->tree_size + 1) );