- dont resend useless channel_destroy messages
[oweals/gnunet.git] / src / util / container_heap.c
index c1d478e06a018ee4f7812329c92830237bb9b601..0f63f957dd5d46acf5755c614aae0b224e0a0d83 100644 (file)
@@ -1,17 +1,17 @@
 /*
   This file is part of GNUnet.
   (C) 2008, 2009 Christian Grothoff (and other contributing authors)
-  
+
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
-  by the Free Software Foundation; either version 2, or (at your
+  by the Free Software Foundation; either version 3, or (at your
   option) any later version.
-  
+
   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
-  
+
   You should have received a copy of the GNU General Public License
   along with GNUnet; see the file COPYING.  If not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
@@ -28,8 +28,9 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
-#define DEBUG 0
+#define EXTRA_CHECKS 0
 
 /**
  * Node in the heap.
@@ -103,7 +104,7 @@ struct GNUNET_CONTAINER_Heap
 };
 
 
-#if DEBUG
+#if EXTRA_CHECKS
 /**
  * Check if internal invariants hold for the given node.
  *
@@ -218,11 +219,11 @@ node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
 {
   if (node == NULL)
     return GNUNET_YES;
-  if (GNUNET_YES != node_iterator (heap,
-                                   node->left_child, iterator, iterator_cls))
+  if (GNUNET_YES !=
+      node_iterator (heap, node->left_child, iterator, iterator_cls))
     return GNUNET_NO;
-  if (GNUNET_YES != node_iterator (heap,
-                                   node->right_child, iterator, iterator_cls))
+  if (GNUNET_YES !=
+      node_iterator (heap, node->right_child, iterator, iterator_cls))
     return GNUNET_NO;
   return iterator (iterator_cls, node, node->element, node->cost);
 }
@@ -267,9 +268,10 @@ GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
   if (pos == NULL)
     pos = heap->root;
   element = pos->element;
-  heap->walk_pos
-      = (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2))
-      ? pos->right_child : pos->left_child;
+  heap->walk_pos =
+      (0 ==
+       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                 2)) ? pos->right_child : pos->left_child;
   return element;
 }
 
@@ -290,8 +292,9 @@ insert_node (struct GNUNET_CONTAINER_Heap *heap,
   struct GNUNET_CONTAINER_HeapNode *parent;
 
   GNUNET_assert (node->parent == NULL);
-  while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX)
-         ? (pos->cost >= node->cost) : (pos->cost <= node->cost))
+  while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >=
+                                                             node->cost)
+         : (pos->cost <= node->cost))
   {
     /* node is descendent of pos */
     pos->tree_size += (1 + node->tree_size);
@@ -343,8 +346,8 @@ insert_node (struct GNUNET_CONTAINER_Heap *heap,
  * @return node for the new element
  */
 struct GNUNET_CONTAINER_HeapNode *
-GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
-                              void *element, GNUNET_CONTAINER_HeapCostType cost)
+GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
+                              GNUNET_CONTAINER_HeapCostType cost)
 {
   struct GNUNET_CONTAINER_HeapNode *node;
 
@@ -397,10 +400,11 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
     heap->root = root->left_child;
     insert_node (heap, heap->root, root->right_child);
   }
+  if (heap->walk_pos == root)
+    heap->walk_pos = heap->root;
   GNUNET_free (root);
-#if DEBUG
-  GNUNET_assert (((heap->size == 0) &&
-                  (heap->root == NULL)) ||
+#if EXTRA_CHECKS
+  GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
 #endif
@@ -480,7 +484,7 @@ remove_node (struct GNUNET_CONTAINER_HeapNode *node)
 
 /**
  * Removes a node from the heap.
- * 
+ *
  * @param node node to remove
  * @return element data stored at the node
  */
@@ -500,10 +504,9 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
   if (heap->walk_pos == node)
     heap->walk_pos = NULL;
   GNUNET_free (node);
-#if DEBUG
+#if EXTRA_CHECKS
   CHECK (heap->root);
-  GNUNET_assert (((heap->size == 0) &&
-                  (heap->root == NULL)) ||
+  GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
 #endif
   return ret;
@@ -522,17 +525,15 @@ GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
                                    struct GNUNET_CONTAINER_HeapNode *node,
                                    GNUNET_CONTAINER_HeapCostType new_cost)
 {
-#if DEBUG
-  GNUNET_assert (((heap->size == 0) &&
-                  (heap->root == NULL)) ||
+#if EXTRA_CHECKS
+  GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
 #endif
   remove_node (node);
-#if DEBUG
+#if EXTRA_CHECKS
   CHECK (heap->root);
-  GNUNET_assert (((heap->size == 1) &&
-                  (heap->root == NULL)) ||
+  GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 2));
 #endif
   node->cost = new_cost;
@@ -540,10 +541,9 @@ GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
     heap->root = node;
   else
     insert_node (heap, heap->root, node);
-#if DEBUG
+#if EXTRA_CHECKS
   CHECK (heap->root);
-  GNUNET_assert (((heap->size == 0) &&
-                  (heap->root == NULL)) ||
+  GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
 #endif
 }