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 * Iterate over the children of the given node.
193 * @param heap argument to give to iterator
194 * @param node node to iterate over
195 * @param iterator function to call on each node
196 * @param iterator_cls closure for iterator
197 * @return GNUNET_YES to continue to iterate
200 node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
201 struct GNUNET_CONTAINER_HeapNode *node,
202 GNUNET_CONTAINER_HeapIterator iterator,
207 if (GNUNET_YES != node_iterator (heap,
212 if (GNUNET_YES != node_iterator (heap,
217 return iterator (iterator_cls,
225 * Iterate over all entries in the heap.
227 * @param heap the heap
228 * @param iterator function to call on each entry
229 * @param iterator_cls closure for iterator
232 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
233 GNUNET_CONTAINER_HeapIterator iterator,
236 (void) node_iterator (heap, heap->root, iterator, iterator_cls);
241 * Perform a random walk of the tree. The walk is biased
242 * towards elements closer to the root of the tree (since
243 * each walk starts at the root and ends at a random leaf).
244 * The heap internally tracks the current position of the
247 * @param heap heap to walk
248 * @return data stored at the next random node in the walk;
249 * NULL if the tree is empty.
252 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
254 struct GNUNET_CONTAINER_HeapNode *pos;
257 if (heap->root == NULL)
259 pos = heap->walk_pos;
262 element = pos->element;
264 = (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2))
272 * Insert the given node 'node' into the subtree starting
273 * at 'pos' (while keeping the tree somewhat balanced).
275 * @param heap heap to modify
276 * @param pos existing tree
277 * @param node node to insert (which may be a subtree itself)
280 insert_node (struct GNUNET_CONTAINER_Heap *heap,
281 struct GNUNET_CONTAINER_HeapNode *pos,
282 struct GNUNET_CONTAINER_HeapNode *node)
284 struct GNUNET_CONTAINER_HeapNode *parent;
286 GNUNET_assert (node->parent == NULL);
287 while ( (heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX)
288 ? (pos->cost >= node->cost)
289 : (pos->cost <= node->cost) )
291 /* node is descendent of pos */
292 pos->tree_size += (1 + node->tree_size);
293 if (pos->left_child == NULL)
295 pos->left_child = node;
299 if (pos->right_child == NULL)
301 pos->right_child = node;
305 /* keep it balanced by descending into smaller subtree */
306 if (pos->left_child->tree_size < pos->right_child->tree_size)
307 pos = pos->left_child;
309 pos = pos->right_child;
311 /* make 'node' parent of 'pos' */
312 parent = pos->parent;
314 node->parent = parent;
321 if (parent->left_child == pos)
322 parent->left_child = node;
324 parent->right_child = node;
326 /* insert 'pos' below 'node' */
327 insert_node (heap, node, pos);
333 * Inserts a new element into the heap.
335 * @param heap heap to modify
336 * @param element element to insert
337 * @param cost cost for the element
338 * @return node for the new element
340 struct GNUNET_CONTAINER_HeapNode *
341 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
343 GNUNET_CONTAINER_HeapCostType cost)
345 struct GNUNET_CONTAINER_HeapNode *node;
347 node = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_HeapNode));
349 node->element = element;
352 if (NULL == heap->root)
355 insert_node (heap, heap->root, node);
356 GNUNET_assert (heap->size == heap->root->tree_size + 1);
363 * Remove root of the heap.
365 * @param heap heap to modify
366 * @return element data stored at the root node, NULL if heap is empty
369 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
372 struct GNUNET_CONTAINER_HeapNode *root;
374 if (NULL == (root = heap->root))
378 if (root->left_child == NULL)
380 heap->root = root->right_child;
381 if (root->right_child != NULL)
382 root->right_child->parent = NULL;
384 else if (root->right_child == NULL)
386 heap->root = root->left_child;
387 root->left_child->parent = NULL;
391 root->left_child->parent = NULL;
392 root->right_child->parent = NULL;
393 heap->root = root->left_child;
394 insert_node (heap, heap->root, root->right_child);
398 GNUNET_assert (( (heap->size == 0) &&
399 (heap->root == NULL) ) ||
400 (heap->size == heap->root->tree_size + 1) );
408 * Remove the given node 'node' from the tree and update
409 * the 'tree_size' fields accordingly. Preserves the
410 * children of 'node' and does NOT change the overall
411 * 'size' field of the tree.
414 remove_node (struct GNUNET_CONTAINER_HeapNode *node)
416 struct GNUNET_CONTAINER_HeapNode *ancestor;
417 struct GNUNET_CONTAINER_Heap *heap = node->heap;
419 /* update 'size' of the ancestors */
421 while (NULL != (ancestor = ancestor->parent))
422 ancestor->tree_size--;
424 /* update 'size' of node itself */
425 if (node->left_child != NULL)
426 node->tree_size -= (1 + node->left_child->tree_size);
427 if (node->right_child != NULL)
428 node->tree_size -= (1 + node->right_child->tree_size);
430 /* unlink 'node' itself and insert children in its place */
431 if (node->parent == NULL)
433 if (node->left_child != NULL)
435 heap->root = node->left_child;
436 node->left_child->parent = NULL;
437 if (node->right_child != NULL)
439 node->right_child->parent = NULL;
440 insert_node (heap, heap->root, node->right_child);
445 heap->root = node->right_child;
446 if (node->right_child != NULL)
447 node->right_child->parent = NULL;
452 if (node->parent->left_child == node)
453 node->parent->left_child = NULL;
455 node->parent->right_child = NULL;
456 if (node->left_child != NULL)
458 node->left_child->parent = NULL;
459 node->parent->tree_size -= (1 + node->left_child->tree_size);
460 insert_node (heap, node->parent, node->left_child);
462 if (node->right_child != NULL)
464 node->right_child->parent = NULL;
465 node->parent->tree_size -= (1 + node->right_child->tree_size);
466 insert_node (heap, node->parent, node->right_child);
470 node->left_child = NULL;
471 node->right_child = NULL;
472 GNUNET_assert (node->tree_size == 0);
478 * Removes a node from the heap.
480 * @param node node to remove
481 * @return element data stored at the node
484 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
487 struct GNUNET_CONTAINER_Heap *heap;
491 if (heap->walk_pos == node)
492 (void) GNUNET_CONTAINER_heap_walk_get_next (heap);
496 if (heap->walk_pos == node)
497 heap->walk_pos = NULL;
501 GNUNET_assert (( (heap->size == 0) &&
502 (heap->root == NULL) ) ||
503 (heap->size == heap->root->tree_size + 1) );
510 * Updates the cost of any node in the tree
512 * @param heap heap to modify
513 * @param node node for which the cost is to be changed
514 * @param new_cost new cost for the node
517 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
518 struct GNUNET_CONTAINER_HeapNode *node,
519 GNUNET_CONTAINER_HeapCostType new_cost)
522 GNUNET_assert ( ( (heap->size == 0) &&
523 (heap->root == NULL) ) ||
524 (heap->size == heap->root->tree_size + 1) );
530 GNUNET_assert ( ( (heap->size == 1) &&
531 (heap->root == NULL) ) ||
532 (heap->size == heap->root->tree_size + 2) );
534 node->cost = new_cost;
535 if (heap->root == NULL)
538 insert_node (heap, heap->root, node);
541 GNUNET_assert ( ( (heap->size == 0) &&
542 (heap->root == NULL) ) ||
543 (heap->size == heap->root->tree_size + 1) );