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 heap heap to modify
271 * @param pos existing tree
272 * @param node node to insert (which may be a subtree itself)
275 insert_node (struct GNUNET_CONTAINER_Heap *heap,
276 struct GNUNET_CONTAINER_HeapNode *pos,
277 struct GNUNET_CONTAINER_HeapNode *node)
279 struct GNUNET_CONTAINER_HeapNode *parent;
281 GNUNET_assert (node->parent == NULL);
282 while ( (heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX)
283 ? (pos->cost >= node->cost)
284 : (pos->cost <= node->cost) )
286 /* node is descendent of pos */
287 pos->tree_size += (1 + node->tree_size);
288 if (pos->left_child == NULL)
290 pos->left_child = node;
294 if (pos->right_child == NULL)
296 pos->right_child = node;
300 /* keep it balanced by descending into smaller subtree */
301 if (pos->left_child->tree_size < pos->right_child->tree_size)
302 pos = pos->left_child;
304 pos = pos->right_child;
306 /* make 'node' parent of 'pos' */
307 parent = pos->parent;
309 node->parent = parent;
316 if (parent->left_child == pos)
317 parent->left_child = node;
319 parent->right_child = node;
321 /* insert 'pos' below 'node' */
322 insert_node (heap, node, pos);
328 * Inserts a new element into the heap.
330 * @param heap heap to modify
331 * @param element element to insert
332 * @param cost cost for the element
333 * @return node for the new element
335 struct GNUNET_CONTAINER_HeapNode *
336 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
338 GNUNET_CONTAINER_HeapCostType cost)
340 struct GNUNET_CONTAINER_HeapNode *node;
342 node = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_HeapNode));
343 node->element = element;
346 if (NULL == heap->root)
349 insert_node (heap, heap->root, node);
350 GNUNET_assert (heap->size == heap->root->tree_size + 1);
357 * Remove root of the heap.
359 * @param heap heap to modify
360 * @return element data stored at the root node, NULL if heap is empty
363 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
366 struct GNUNET_CONTAINER_HeapNode *root;
368 if (NULL == (root = heap->root))
372 if (root->left_child == NULL)
374 heap->root = root->right_child;
375 if (root->right_child != NULL)
376 root->right_child->parent = NULL;
378 else if (root->right_child == NULL)
380 heap->root = root->left_child;
381 root->left_child->parent = NULL;
385 root->left_child->parent = NULL;
386 root->right_child->parent = NULL;
387 heap->root = root->left_child;
388 insert_node (heap, heap->root, root->right_child);
392 GNUNET_assert (( (heap->size == 0) &&
393 (heap->root == NULL) ) ||
394 (heap->size == heap->root->tree_size + 1) );
402 * Remove the given node 'node' from the tree and update
403 * the 'tree_size' fields accordingly. Preserves the
404 * children of 'node' and does NOT change the overall
405 * 'size' field of the tree.
408 remove_node (struct GNUNET_CONTAINER_Heap *heap,
409 struct GNUNET_CONTAINER_HeapNode *node)
411 struct GNUNET_CONTAINER_HeapNode *ancestor;
413 /* update 'size' of the ancestors */
415 while (NULL != (ancestor = ancestor->parent))
416 ancestor->tree_size--;
418 /* update 'size' of node itself */
419 if (node->left_child != NULL)
420 node->tree_size -= (1 + node->left_child->tree_size);
421 if (node->right_child != NULL)
422 node->tree_size -= (1 + node->right_child->tree_size);
424 /* unlink 'node' itself and insert children in its place */
425 if (node->parent == NULL)
427 if (node->left_child != NULL)
429 heap->root = node->left_child;
430 node->left_child->parent = NULL;
431 if (node->right_child != NULL)
433 node->right_child->parent = NULL;
434 insert_node (heap, heap->root, node->right_child);
439 heap->root = node->right_child;
440 if (node->right_child != NULL)
441 node->right_child->parent = NULL;
446 if (node->parent->left_child == node)
447 node->parent->left_child = NULL;
449 node->parent->right_child = NULL;
450 if (node->left_child != NULL)
452 node->left_child->parent = NULL;
453 node->parent->tree_size -= (1 + node->left_child->tree_size);
454 insert_node (heap, node->parent, node->left_child);
456 if (node->right_child != NULL)
458 node->right_child->parent = NULL;
459 node->parent->tree_size -= (1 + node->right_child->tree_size);
460 insert_node (heap, node->parent, node->right_child);
464 node->left_child = NULL;
465 node->right_child = NULL;
466 GNUNET_assert (node->tree_size == 0);
472 * Removes a node from the heap.
474 * @param heap heap to modify
475 * @param node node to remove
476 * @return element data stored at the node
479 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *heap,
480 struct GNUNET_CONTAINER_HeapNode *node)
485 if (heap->walk_pos == node)
486 (void) GNUNET_CONTAINER_heap_walk_get_next (heap);
487 remove_node (heap, node);
490 if (heap->walk_pos == node)
491 heap->walk_pos = NULL;
495 GNUNET_assert (( (heap->size == 0) &&
496 (heap->root == NULL) ) ||
497 (heap->size == heap->root->tree_size + 1) );
504 * Updates the cost of any node in the tree
506 * @param heap heap to modify
507 * @param node node for which the cost is to be changed
508 * @param new_cost new cost for the node
511 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
512 struct GNUNET_CONTAINER_HeapNode *node,
513 GNUNET_CONTAINER_HeapCostType new_cost)
516 GNUNET_assert ( ( (heap->size == 0) &&
517 (heap->root == NULL) ) ||
518 (heap->size == heap->root->tree_size + 1) );
521 remove_node (heap, node);
524 GNUNET_assert ( ( (heap->size == 1) &&
525 (heap->root == NULL) ) ||
526 (heap->size == heap->root->tree_size + 2) );
528 node->cost = new_cost;
529 if (heap->root == NULL)
532 insert_node (heap, heap->root, node);
535 GNUNET_assert ( ( (heap->size == 0) &&
536 (heap->root == NULL) ) ||
537 (heap->size == heap->root->tree_size + 1) );