gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r10251 - GNUnet/src/util/containers


From: gnunet
Subject: [GNUnet-SVN] r10251 - GNUnet/src/util/containers
Date: Mon, 8 Feb 2010 12:36:13 +0100

Author: grothoff
Date: 2010-02-08 12:36:13 +0100 (Mon, 08 Feb 2010)
New Revision: 10251

Modified:
   GNUnet/src/util/containers/heap.c
Log:
actually make use of dirty flag, code cleanup

Modified: GNUnet/src/util/containers/heap.c
===================================================================
--- GNUnet/src/util/containers/heap.c   2010-02-08 11:31:53 UTC (rev 10250)
+++ GNUnet/src/util/containers/heap.c   2010-02-08 11:36:13 UTC (rev 10251)
@@ -102,19 +102,19 @@
    */
   enum GNUNET_CONTAINER_HeapOrder order;
 
-  /*
-   * Is the heap dirty (needs expunged)?
+  /**
+   * Is the heap dirty (needs purge of deleted nodes after iterators are done)?
    */
   unsigned int dirty;
 
-  /*
+  /**
    * How many iterations are we into this heap?
    *
    * 0 - if no iteration(s) taking place
    *   > 0 if iteration(s) in progress
    *   < 0 if we are currently cleaning up the heap (removing dead nodes)!
    */
-  int iterator_count;
+  unsigned int iterator_count;
 
 };
 
@@ -230,11 +230,9 @@
   if (GNUNET_YES != node_iterator (heap,
                                    node->right_child, iterator, iterator_cls))
     return GNUNET_NO;
-
-  if (node->delete == GNUNET_NO)
-    return iterator (iterator_cls, node, node->element, node->cost);
-  else
-    return GNUNET_NO;
+  if (node->delete != GNUNET_NO)
+    return GNUNET_YES;
+  return iterator (iterator_cls, node, node->element, node->cost);
 }
 
 
@@ -247,33 +245,25 @@
  * @param iterator_cls closure for iterator
  * @return GNUNET_YES to continue to iterate
  */
-void
+static void
 cleanup_node_iterator (struct GNUNET_CONTAINER_Heap *heap,
                struct GNUNET_CONTAINER_HeapNode *node)
 {
   if (node == NULL)
     return;
-
   if (node->left_child != NULL)
     cleanup_node_iterator(heap, node->left_child);
   if (node->right_child != NULL)
     cleanup_node_iterator(heap, node->right_child);
-
-  if (node->delete == GNUNET_YES)
-    {
-      if (heap->root == node)
-        GNUNET_CONTAINER_heap_remove_root (heap);
-      else
-        GNUNET_CONTAINER_heap_remove_node (heap, node);
-    }
-  return;
+  if (node->delete != GNUNET_YES)
+    return;
+  if (heap->root == node)
+    GNUNET_CONTAINER_heap_remove_root (heap);
+  else
+    GNUNET_CONTAINER_heap_remove_node (heap, node);
 }
 
-void cleanup_heap(struct GNUNET_CONTAINER_Heap *heap)
-{
-  cleanup_node_iterator(heap, heap->root);
-  heap->dirty = GNUNET_NO;
-}
+
 /**
  * Iterate over all entries in the heap.
  *
@@ -288,12 +278,13 @@
 {
   heap->iterator_count++;
   (void) node_iterator (heap, heap->root, iterator, iterator_cls);
-  heap->iterator_count--;
-
-  if (heap->iterator_count == 0)
-  {
-    cleanup_heap(heap);
-  }
+  heap->iterator_count--;  
+  if ( (heap->iterator_count == 0) &&
+       (heap->dirty == GNUNET_YES) )    
+    {
+      cleanup_node_iterator(heap, heap->root);
+      heap->dirty = GNUNET_NO;
+    }
 }
 
 
@@ -431,14 +422,10 @@
     return NULL;
 
   ret = root->element;
-  if (heap->iterator_count != 0)
+  if (heap->iterator_count > 0)
     {
       heap->root->delete = GNUNET_YES;
-      if (heap->dirty == GNUNET_NO)
-      {
-        heap->dirty = GNUNET_YES;
-      }
-
+      heap->dirty = GNUNET_YES;     
       return ret;
     }
   heap->size--;
@@ -560,13 +547,10 @@
   CHECK (heap->root);
 
   ret = node->element;
-  if (heap->iterator_count != 0)
+  if (heap->iterator_count > 0)
     {
       node->delete = GNUNET_YES;
-      if (heap->dirty == GNUNET_NO)
-      {
-        heap->dirty = GNUNET_YES;
-      }
+      heap->dirty = GNUNET_YES;      
       return ret;
     }
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]