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
42 struct GNUNET_CONTAINER_HeapNode *parent;
47 struct GNUNET_CONTAINER_HeapNode *left_child;
52 struct GNUNET_CONTAINER_HeapNode *right_child;
60 * Cost for this element.
62 GNUNET_CONTAINER_HeapCostType cost;
65 * Number of elements below this node in the heap
66 * (excluding this node itself).
68 unsigned int tree_size;
73 * Handle to a node in a heap.
75 struct GNUNET_CONTAINER_Heap
81 struct GNUNET_CONTAINER_HeapNode *root;
84 * Current position of our random walk.
86 struct GNUNET_CONTAINER_HeapNode *walk_pos;
89 * Number of elements in the heap.
94 * How is the heap sorted?
96 enum GNUNET_CONTAINER_HeapOrder order;
103 * Check if internal invariants hold for the given node.
105 * @param node subtree to check
108 check (const struct GNUNET_CONTAINER_HeapNode *node)
112 GNUNET_assert (node->tree_size ==
113 ( (node->left_child == NULL) ? 0 : 1 + node->left_child->tree_size) +
114 ( (node->right_child == NULL) ? 0 : 1 + node->right_child->tree_size) );
115 check (node->left_child);
116 check (node->right_child);
120 #define CHECK(n) check(n)
122 #define CHECK(n) do {} while (0)
129 * @param order how should the heap be sorted?
130 * @return handle to the heap
132 struct GNUNET_CONTAINER_Heap *
133 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order)
135 struct GNUNET_CONTAINER_Heap *heap;
137 heap = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_Heap));
144 * Destroys the heap. Only call on a heap that
147 * @param heap heap to destroy
150 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
152 GNUNET_break (heap->size == 0);
158 * Get element stored at root of heap.
160 * @param heap heap to inspect
161 * @return NULL if heap is empty
164 GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap)
166 if (heap->root == NULL)
168 return heap->root->element;
173 * Get the current size of the heap
175 * @param heap the heap to get the size of
176 * @return number of elements stored
179 GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap)
186 * Iterate over the children of the given node.
188 * @param heap argument to give to iterator
189 * @param node node to iterate over
190 * @param iterator function to call on each node
191 * @param iterator_cls closure for iterator
192 * @return GNUNET_YES to continue to iterate
195 node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
196 struct GNUNET_CONTAINER_HeapNode *node,
197 GNUNET_CONTAINER_HeapIterator iterator,
202 if (GNUNET_YES != node_iterator (heap,
207 if (GNUNET_YES != node_iterator (heap,
212 return iterator (iterator_cls,
220 * Iterate over all entries in the heap.
222 * @param heap the heap
223 * @param iterator function to call on each entry
224 * @param iterator_cls closure for iterator
227 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
228 GNUNET_CONTAINER_HeapIterator iterator,
231 (void) node_iterator (heap, heap->root, iterator, iterator_cls);
236 * Perform a random walk of the tree. The walk is biased
237 * towards elements closer to the root of the tree (since
238 * each walk starts at the root and ends at a random leaf).
239 * The heap internally tracks the current position of the
242 * @param heap heap to walk
243 * @return data stored at the next random node in the walk;
244 * NULL if the tree is empty.
247 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
249 struct GNUNET_CONTAINER_HeapNode *pos;
252 if (heap->root == NULL)
254 pos = heap->walk_pos;
257 element = pos->element;
259 = (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2))
267 * Insert the given node 'node' into the subtree starting
268 * at 'pos' (while keeping the tree somewhat balanced).
270 * @param pos existing tree
271 * @param node node to insert (which may be a subtree itself)
274 insert_node (struct GNUNET_CONTAINER_Heap *heap,
275 struct GNUNET_CONTAINER_HeapNode *pos,
276 struct GNUNET_CONTAINER_HeapNode *node)
278 struct GNUNET_CONTAINER_HeapNode *parent;
280 GNUNET_assert (node->parent == NULL);
281 while ( (pos->cost < node->cost) ^ (heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) )
283 /* node is descendent of pos */
284 pos->tree_size += (1 + node->tree_size);
285 if (pos->left_child == NULL)
287 pos->left_child = node;
291 if (pos->right_child == NULL)
293 pos->right_child = node;
297 /* keep it balanced by descending into smaller subtree */
298 if (pos->left_child->tree_size < pos->right_child->tree_size)
299 pos = pos->left_child;
301 pos = pos->right_child;
303 /* make 'node' parent of 'pos' */
304 parent = pos->parent;
306 node->parent = parent;
313 if (parent->left_child == pos)
314 parent->left_child = node;
316 parent->right_child = node;
318 /* insert 'pos' below 'node' */
319 insert_node (heap, node, pos);
325 * Inserts a new element into the heap.
327 * @param heap heap to modify
328 * @param element element to insert
329 * @param cost cost for the element
330 * @return node for the new element
332 struct GNUNET_CONTAINER_HeapNode *
333 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
335 GNUNET_CONTAINER_HeapCostType cost)
337 struct GNUNET_CONTAINER_HeapNode *node;
339 node = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_HeapNode));
340 node->element = element;
343 if (NULL == heap->root)
346 insert_node (heap, heap->root, node);
347 GNUNET_assert (heap->size == heap->root->tree_size + 1);
354 * Remove root of the heap.
356 * @param heap heap to modify
357 * @return element data stored at the root node, NULL if heap is empty
360 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
363 struct GNUNET_CONTAINER_HeapNode *root;
365 if (NULL == (root = heap->root))
369 if (root->left_child == NULL)
371 heap->root = root->right_child;
372 if (root->right_child != NULL)
373 root->right_child->parent = NULL;
375 else if (root->right_child == NULL)
377 heap->root = root->left_child;
378 if (root->left_child != NULL)
379 root->left_child->parent = NULL;
383 root->left_child->parent = NULL;
384 root->right_child->parent = NULL;
385 heap->root = root->left_child;
386 insert_node (heap, heap->root, root->right_child);
390 GNUNET_assert (( (heap->size == 0) &&
391 (heap->root == NULL) ) ||
392 (heap->size == heap->root->tree_size + 1) );
400 * Remove the given node 'node' from the tree and update
401 * the 'tree_size' fields accordingly. Preserves the
402 * children of 'node' and does NOT change the overall
403 * 'size' field of the tree.
406 remove_node (struct GNUNET_CONTAINER_Heap *heap,
407 struct GNUNET_CONTAINER_HeapNode *node)
409 struct GNUNET_CONTAINER_HeapNode *ancestor;
411 /* update 'size' of the ancestors */
413 while (NULL != (ancestor = ancestor->parent))
414 ancestor->tree_size--;
416 /* update 'size' of node itself */
417 if (node->left_child != NULL)
418 node->tree_size -= (1 + node->left_child->tree_size);
419 if (node->right_child != NULL)
420 node->tree_size -= (1 + node->right_child->tree_size);
422 /* unlink 'node' itself and insert children in its place */
423 if (node->parent == NULL)
425 if (node->left_child != NULL)
427 heap->root = node->left_child;
428 node->left_child->parent = NULL;
429 if (node->right_child != NULL)
431 node->right_child->parent = NULL;
432 insert_node (heap, heap->root, node->right_child);
437 heap->root = node->right_child;
438 if (node->right_child != NULL)
439 node->right_child->parent = NULL;
444 if (node->parent->left_child == node)
445 node->parent->left_child = NULL;
447 node->parent->right_child = NULL;
448 if (node->left_child != NULL)
450 node->left_child->parent = NULL;
451 node->parent->tree_size -= (1 + node->left_child->tree_size);
452 insert_node (heap, node->parent, node->left_child);
454 if (node->right_child != NULL)
456 node->right_child->parent = NULL;
457 node->parent->tree_size -= (1 + node->right_child->tree_size);
458 insert_node (heap, node->parent, node->right_child);
462 node->left_child = NULL;
463 node->right_child = NULL;
464 GNUNET_assert (node->tree_size == 0);
470 * Removes a node from the heap.
472 * @param heap heap to modify
473 * @param node node to remove
474 * @return element data stored at the node
477 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *heap,
478 struct GNUNET_CONTAINER_HeapNode *node)
483 if (heap->walk_pos == node)
484 (void) GNUNET_CONTAINER_heap_walk_get_next (heap);
485 remove_node (heap, node);
488 if (heap->walk_pos == node)
489 heap->walk_pos = NULL;
493 GNUNET_assert (( (heap->size == 0) &&
494 (heap->root == NULL) ) ||
495 (heap->size == heap->root->tree_size + 1) );
502 * Updates the cost of any node in the tree
504 * @param heap heap to modify
505 * @param node node for which the cost is to be changed
506 * @param new_cost new cost for the node
509 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
510 struct GNUNET_CONTAINER_HeapNode *node,
511 GNUNET_CONTAINER_HeapCostType new_cost)
514 GNUNET_assert ( ( (heap->size == 0) &&
515 (heap->root == NULL) ) ||
516 (heap->size == heap->root->tree_size + 1) );
519 remove_node (heap, node);
522 GNUNET_assert ( ( (heap->size == 1) &&
523 (heap->root == NULL) ) ||
524 (heap->size == heap->root->tree_size + 2) );
526 node->cost = new_cost;
527 if (heap->root == NULL)
530 insert_node (heap, heap->root, node);
533 GNUNET_assert ( ( (heap->size == 0) &&
534 (heap->root == NULL) ) ||
535 (heap->size == heap->root->tree_size + 1) );