Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics
[simgrid.git] / src / xbt / dynar.c
index 812ff7f..e289e65 100644 (file)
@@ -1,8 +1,7 @@
-/* $Id$ */
-
 /* a generic DYNamic ARray implementation.                                  */
 
 /* a generic DYNamic ARray implementation.                                  */
 
-/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
+/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -301,7 +300,7 @@ void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
   size_wanted = dynar->used + empty_slots_wanted;
   if (size_wanted != dynar->size) {
     dynar->size = size_wanted;
   size_wanted = dynar->used + empty_slots_wanted;
   if (size_wanted != dynar->size) {
     dynar->size = size_wanted;
-    dynar->data = xbt_realloc(dynar->data, sizeof(void *) * dynar->size);
+    dynar->data = xbt_realloc(dynar->data, dynar->elmsize * dynar->size);
   }
   _dynar_unlock(dynar);
 }
   }
   _dynar_unlock(dynar);
 }
@@ -336,6 +335,16 @@ XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
   return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
 }
 
   return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
 }
 
+/**@brief check if a dynar is empty
+ *
+ *\param dynar the dynat we want to check
+ */
+
+XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
+{
+       return (xbt_dynar_length(dynar) == 0);
+}
+
 /** @brief Retrieve a copy of the Nth element of a dynar.
  *
  * \param dynar information dealer
 /** @brief Retrieve a copy of the Nth element of a dynar.
  *
  * \param dynar information dealer
@@ -521,7 +530,7 @@ xbt_dynar_remove_at(xbt_dynar_t const dynar,
  *
  * Raises not_found_error if not found.
  */
  *
  * Raises not_found_error if not found.
  */
-int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
+unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
 {
   unsigned long it;
 
 {
   unsigned long it;
 
@@ -686,6 +695,22 @@ XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar)
   _dynar_unlock(dynar);
 }
 
   _dynar_unlock(dynar);
 }
 
+/** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
+ *
+ * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
+ *
+ * Remark: if the elements stored in the dynar are structures, the compar_fn
+ * function has to retrieve the field to sort first.
+ */
+XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar, int_f_cpvoid_cpvoid_t compar_fn){
+
+       _dynar_lock(dynar);
+
+       qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
+
+       _dynar_unlock(dynar);
+}
+
 #ifdef SIMGRID_TEST
 
 #define NB_ELEM 5000
 #ifdef SIMGRID_TEST
 
 #define NB_ELEM 5000