Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix: setset cursors were incorrectly implemented. Now they work fine.
[simgrid.git] / src / xbt / setset.c
index cbdb2b8..4a45406 100644 (file)
@@ -29,10 +29,14 @@ xbt_setset_t xbt_setset_new(unsigned int size)
  */
 void xbt_setset_destroy(xbt_setset_t setset)
 {
+  xbt_fifo_item_t item;
+  xbt_setset_set_t set;
   xbt_dynar_free(&setset->elm_array);
+  xbt_fifo_foreach(setset->sets, item, set, xbt_setset_set_t){
+    xbt_setset_destroy_set(set);
+  }
   xbt_fifo_free(setset->sets);
   xbt_free(setset);
-  /* FIXME: check if we should trash the stored objects */
 }
 
 /* Add an element to the setset, this will assign to it an index */
@@ -44,7 +48,7 @@ xbt_setset_elm_entry_t _xbt_setset_elm_add(xbt_setset_t setset, void *obj)
   xbt_setset_elm_entry_t first_elm = 
     (xbt_setset_elm_entry_t)xbt_dynar_get_ptr(setset->elm_array, 0);
   
-  /* Before create a new elm entry check if there is one in the free elm list.*/
+  /* Before create a new elm entry check if there is one in the free elm list. */
   /* If there is not free elm entries, then create a new one  */
   if(first_elm->free.next != 0){
     e->ID = first_elm->free.next;
@@ -65,13 +69,14 @@ void _xbt_setset_elm_remove(xbt_setset_t setset, unsigned long idx)
 {
   xbt_setset_elm_entry_t e_entry = xbt_dynar_get_ptr(setset->elm_array, idx);
   xbt_setset_elm_entry_t first_free = NULL;
-  
+
   /* Decrease the refcount and proceed only if it is 0 */
   if(--e_entry->info.refcount > 0)
     return;
 
   /* Erase object ID */
-  e_entry->info.obj->ID = 0;
+  /* FIXME: do not assume that the object still exists, it might be deallocated */
+  /*e_entry->info.obj->ID = 0;*/
   
   /* Link the elm entry to the list of free ones */
   first_free = xbt_dynar_get_ptr(setset->elm_array, 0);
@@ -341,7 +346,7 @@ void xbt_setset_cursor_first(xbt_setset_set_t set, xbt_setset_cursor_t *cursor)
   for(i=0; i < set->size; i++){
     if(set->bitmap[i] != 0){
       for(k=0; k < BITS_INT; k++){
-        if(_is_bit_set(k,set->bitmap[i])){
+        if(_is_bit_set(k, set->bitmap[i])){
           (*cursor)->idx = i * BITS_INT + k;
           return; 
         }
@@ -366,17 +371,13 @@ int xbt_setset_cursor_get_data(xbt_setset_cursor_t cursor, void **data)
 /* Advance a cursor to the next element */
 void xbt_setset_cursor_next(xbt_setset_cursor_t cursor)
 {
- int i,k;
   cursor->idx++;
-  /* Traverse the set and point the cursor to the first element */
-  for(i = cursor->idx / BITS_INT; i < cursor->set->size; i++){
-    if(cursor->set->bitmap[i] != 0){
-      for(k = cursor->idx % BITS_INT; k < BITS_INT; k++){
-        if(_is_bit_set(k,cursor->set->bitmap[i])){
-          cursor->idx = i * BITS_INT + k;
-          return; 
-        }
-      }
+  while(cursor->idx < cursor->set->size * BITS_INT)
+  {
+    if(_is_bit_set(cursor->idx % BITS_INT, cursor->set->bitmap[cursor->idx / BITS_INT])){
+      return;
+    }else{
+      cursor->idx += cursor->set->bitmap[cursor->idx / BITS_INT] ? 1 : BITS_INT;
     }
   }
   cursor->idx = 0; 
@@ -398,12 +399,4 @@ void _set_bit(unsigned int bit, unsigned int *bitmap)
 void _unset_bit(unsigned int bit, unsigned int *bitmap)
 {
   bitmap[bit / BITS_INT] &= ~(0x1 << (bit % BITS_INT));
-}
-
-
-
-
-
-
-
-
+}
\ No newline at end of file