Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix warnings + refactor
[simgrid.git] / src / xbt / heap.c
index 5bfca96..db2431e 100644 (file)
@@ -1,6 +1,6 @@
 /* a generic and efficient heap                                             */
 
-/* Copyright (c) 2004-2005, 2007-2015. The SimGrid Team.
+/* Copyright (c) 2004-2005, 2007-2017. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -83,7 +83,8 @@ inline int xbt_heap_size(xbt_heap_t H)
  */
 void xbt_heap_push(xbt_heap_t H, void *content, double key)
 {
-  int count = ++(H->count);
+  H->count += 1;
+  int count = H->count;
 
   int size = H->size;
   xbt_heap_item_t item;
@@ -91,6 +92,7 @@ void xbt_heap_push(xbt_heap_t H, void *content, double key)
   if (count > size) {
     H->size = (size << 1) + 1;
     H->items = (void *) xbt_realloc(H->items, (H->size) * sizeof(struct xbt_heap_item));
+    xbt_assert(H->items != NULL);
   }
 
   item = &(H->items[count - 1]);
@@ -98,7 +100,6 @@ void xbt_heap_push(xbt_heap_t H, void *content, double key)
   item->content = content;
   xbt_heap_increase_key(H, count - 1);
   XBT_DEBUG("Heap has now %d elements and max elem is %g",xbt_heap_size(H),xbt_heap_maxkey(H));
-  return;
 }
 
 /**
@@ -158,18 +159,19 @@ void *xbt_heap_remove(xbt_heap_t H, int i)
 
   return xbt_heap_pop(H);
 }
+
 /** @brief Remove an arbitrary element from the heap
- *  @param H the heap we're working on
- *  @param content the object you want to add to the heap
- *  @param key the key associated to this object
+ *  \param H the heap we're working on
+ *  \param content the object you want to remove from the heap
+ *  \param key the key associated to this object
+ *  \return the removed element if found, NULL otherwise
  */
-void xbt_heap_rm_elm(xbt_heap_t H, void *content, double key) {
+void *xbt_heap_rm_elm(xbt_heap_t H, void *content, double key)
+{
   int i=0;
   while (i < H->count && (KEY(H, i) != key || CONTENT(H, i) != content))
     i++;
-  if (i == H->count)
-    return;
-  xbt_heap_remove(H,i);
+  return xbt_heap_remove(H, i);
 }
 
 /**
@@ -277,5 +279,4 @@ static void xbt_heap_increase_key(xbt_heap_t H, int i)
   }
   if (H->update_callback)
     H->update_callback(items[i].content, i);
-  return;
 }