From: suter Date: Mon, 14 Jun 2010 08:43:15 +0000 (+0000) Subject: Add a new function xbt_dynar_sort() which is a wrapper on qsort. X-Git-Tag: v3_5~965 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/4a858f824d2969b164b967ae3e320593af0a9f9d Add a new function xbt_dynar_sort() which is a wrapper on qsort. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7843 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/ChangeLog b/ChangeLog index 7c7cd98f04..f44f2268c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,7 @@ SimGrid (3.5) unstable; urgency=low * New data container: setset (set of sets of elements) * New module: mmalloc (mapped malloc) * New function: xbt_dict_cursor_set_data() + * New function: xbt_dynar_sort() * New function: xbt_fifo_get_last_item() * Bug fix in xbt_dynar_shrink(): use the right element size * Use library init/fini functions for our initialization. diff --git a/include/xbt/dynar.h b/include/xbt/dynar.h index a045e106d6..6fb99a8947 100644 --- a/include/xbt/dynar.h +++ b/include/xbt/dynar.h @@ -97,6 +97,8 @@ XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx, XBT_PUBLIC(int) xbt_dynar_search(xbt_dynar_t const dynar, void *elem); XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem); +XBT_PUBLIC(void) xbt_dynar_sort(xbt_dynar_t const dynar, int_f_pvoid_pvoid_t compar_fn); + /** @} */ /** @defgroup XBT_dynar_perl Perl-like use of dynars * @ingroup XBT_dynar diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 1937efbd24..5498ba110b 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -685,6 +685,22 @@ XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar) _dynar_unlock(dynar); } +/** @brief Sorts a dynar according to the function compar_fn + * + * \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_pvoid_pvoid_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