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 ==
119 NULL) ? 0 : 1 + node->left_child->tree_size) +
120 ((node->right_child ==
121 NULL) ? 0 : 1 + node->right_child->tree_size));
122 check (node->left_child);
123 check (node->right_child);
127 #define CHECK(n) check(n)
129 #define CHECK(n) do {} while (0)
136 * @param order how should the heap be sorted?
137 * @return handle to the heap
139 struct GNUNET_CONTAINER_Heap *
140 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order)
142 struct GNUNET_CONTAINER_Heap *heap;
144 heap = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_Heap));
151 * Destroys the heap. Only call on a heap that
154 * @param heap heap to destroy
157 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
159 GNUNET_break (heap->size == 0);
165 * Get element stored at root of heap.
167 * @param heap heap to inspect
168 * @return NULL if heap is empty
171 GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap)
173 if (heap->root == NULL)
175 return heap->root->element;
180 * Get the current size of the heap
182 * @param heap the heap to get the size of
183 * @return number of elements stored
186 GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap)
193 * Get the current cost of the node
195 * @param node the node to get the cost of
196 * @return cost of the node
198 GNUNET_CONTAINER_HeapCostType
199 GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
206 * Iterate over the children of the given node.
208 * @param heap argument to give to iterator
209 * @param node node to iterate over
210 * @param iterator function to call on each node
211 * @param iterator_cls closure for iterator
212 * @return GNUNET_YES to continue to iterate
215 node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
216 struct GNUNET_CONTAINER_HeapNode *node,
217 GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls)
222 node_iterator (heap, node->left_child, iterator, iterator_cls))
225 node_iterator (heap, node->right_child, iterator, iterator_cls))
227 return iterator (iterator_cls, node, node->element, node->cost);
232 * Iterate over all entries in the heap.
234 * @param heap the heap
235 * @param iterator function to call on each entry
236 * @param iterator_cls closure for iterator
239 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
240 GNUNET_CONTAINER_HeapIterator iterator,
243 (void) node_iterator (heap, heap->root, iterator, iterator_cls);
248 * Perform a random walk of the tree. The walk is biased
249 * towards elements closer to the root of the tree (since
250 * each walk starts at the root and ends at a random leaf).
251 * The heap internally tracks the current position of the
254 * @param heap heap to walk
255 * @return data stored at the next random node in the walk;
256 * NULL if the tree is empty.
259 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
261 struct GNUNET_CONTAINER_HeapNode *pos;
264 if (heap->root == NULL)
266 pos = heap->walk_pos;
269 element = pos->element;
272 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
273 2)) ? pos->right_child : pos->left_child;
279 * Insert the given node 'node' into the subtree starting
280 * at 'pos' (while keeping the tree somewhat balanced).
282 * @param heap heap to modify
283 * @param pos existing tree
284 * @param node node to insert (which may be a subtree itself)
287 insert_node (struct GNUNET_CONTAINER_Heap *heap,
288 struct GNUNET_CONTAINER_HeapNode *pos,
289 struct GNUNET_CONTAINER_HeapNode *node)
291 struct GNUNET_CONTAINER_HeapNode *parent;
293 GNUNET_assert (node->parent == NULL);
294 while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >=
296 : (pos->cost <= node->cost))
298 /* node is descendent of pos */
299 pos->tree_size += (1 + node->tree_size);
300 if (pos->left_child == NULL)
302 pos->left_child = node;
306 if (pos->right_child == NULL)
308 pos->right_child = node;
312 /* keep it balanced by descending into smaller subtree */
313 if (pos->left_child->tree_size < pos->right_child->tree_size)
314 pos = pos->left_child;
316 pos = pos->right_child;
318 /* make 'node' parent of 'pos' */
319 parent = pos->parent;
321 node->parent = parent;
328 if (parent->left_child == pos)
329 parent->left_child = node;
331 parent->right_child = node;
333 /* insert 'pos' below 'node' */
334 insert_node (heap, node, pos);
340 * Inserts a new element into the heap.
342 * @param heap heap to modify
343 * @param element element to insert
344 * @param cost cost for the element
345 * @return node for the new element
347 struct GNUNET_CONTAINER_HeapNode *
348 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
349 GNUNET_CONTAINER_HeapCostType cost)
351 struct GNUNET_CONTAINER_HeapNode *node;
353 node = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_HeapNode));
355 node->element = element;
358 if (NULL == heap->root)
361 insert_node (heap, heap->root, node);
362 GNUNET_assert (heap->size == heap->root->tree_size + 1);
369 * Remove root of the heap.
371 * @param heap heap to modify
372 * @return element data stored at the root node, NULL if heap is empty
375 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
378 struct GNUNET_CONTAINER_HeapNode *root;
380 if (NULL == (root = heap->root))
384 if (root->left_child == NULL)
386 heap->root = root->right_child;
387 if (root->right_child != NULL)
388 root->right_child->parent = NULL;
390 else if (root->right_child == NULL)
392 heap->root = root->left_child;
393 root->left_child->parent = NULL;
397 root->left_child->parent = NULL;
398 root->right_child->parent = NULL;
399 heap->root = root->left_child;
400 insert_node (heap, heap->root, root->right_child);
404 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
405 (heap->size == heap->root->tree_size + 1));
413 * Remove the given node 'node' from the tree and update
414 * the 'tree_size' fields accordingly. Preserves the
415 * children of 'node' and does NOT change the overall
416 * 'size' field of the tree.
419 remove_node (struct GNUNET_CONTAINER_HeapNode *node)
421 struct GNUNET_CONTAINER_HeapNode *ancestor;
422 struct GNUNET_CONTAINER_Heap *heap = node->heap;
424 /* update 'size' of the ancestors */
426 while (NULL != (ancestor = ancestor->parent))
427 ancestor->tree_size--;
429 /* update 'size' of node itself */
430 if (node->left_child != NULL)
431 node->tree_size -= (1 + node->left_child->tree_size);
432 if (node->right_child != NULL)
433 node->tree_size -= (1 + node->right_child->tree_size);
435 /* unlink 'node' itself and insert children in its place */
436 if (node->parent == NULL)
438 if (node->left_child != NULL)
440 heap->root = node->left_child;
441 node->left_child->parent = NULL;
442 if (node->right_child != NULL)
444 node->right_child->parent = NULL;
445 insert_node (heap, heap->root, node->right_child);
450 heap->root = node->right_child;
451 if (node->right_child != NULL)
452 node->right_child->parent = NULL;
457 if (node->parent->left_child == node)
458 node->parent->left_child = NULL;
460 node->parent->right_child = NULL;
461 if (node->left_child != NULL)
463 node->left_child->parent = NULL;
464 node->parent->tree_size -= (1 + node->left_child->tree_size);
465 insert_node (heap, node->parent, node->left_child);
467 if (node->right_child != NULL)
469 node->right_child->parent = NULL;
470 node->parent->tree_size -= (1 + node->right_child->tree_size);
471 insert_node (heap, node->parent, node->right_child);
475 node->left_child = NULL;
476 node->right_child = NULL;
477 GNUNET_assert (node->tree_size == 0);
483 * Removes a node from the heap.
485 * @param node node to remove
486 * @return element data stored at the node
489 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
492 struct GNUNET_CONTAINER_Heap *heap;
496 if (heap->walk_pos == node)
497 (void) GNUNET_CONTAINER_heap_walk_get_next (heap);
501 if (heap->walk_pos == node)
502 heap->walk_pos = NULL;
506 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
507 (heap->size == heap->root->tree_size + 1));
514 * Updates the cost of any node in the tree
516 * @param heap heap to modify
517 * @param node node for which the cost is to be changed
518 * @param new_cost new cost for the node
521 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
522 struct GNUNET_CONTAINER_HeapNode *node,
523 GNUNET_CONTAINER_HeapCostType new_cost)
526 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
527 (heap->size == heap->root->tree_size + 1));
533 GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) ||
534 (heap->size == heap->root->tree_size + 2));
536 node->cost = new_cost;
537 if (heap->root == NULL)
540 insert_node (heap, heap->root, node);
543 GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
544 (heap->size == heap->root->tree_size + 1));