X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/85897b1307bb68f05f31385803f1b3a5b806a73f..8750e7f378b5d48477e1b0b2796a437366a3ea2b:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index e34331a518..923b90ebbe 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -11,6 +11,7 @@ #include "xbt/misc.h" #include "xbt/sysdep.h" #include "xbt/log.h" +#include "xbt/ex.h" #include "xbt/dynar.h" #include @@ -33,7 +34,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,xbt,"Dynamic arrays"); (int) (idx), (unsigned long) dynar->used) #define __check_sloppy_inbound_idx(dynar, idx) \ xbt_assert2(idx <= dynar->used, \ - "dynar is not that long. You asked %d, but it's only %lu long", \ + "dynar is not that long. You asked %d, but it's only %lu long (could have been equal to it)", \ (int) (idx), (unsigned long) dynar->used) #define __check_populated_dynar(dynar) \ xbt_assert1(dynar->used, \ @@ -95,9 +96,8 @@ _xbt_dynar_get_elm(void * const dst, const xbt_dynar_t dynar, const unsigned long idx) { void * const elm = _xbt_dynar_elm(dynar, idx); - const unsigned long elmsize = dynar->elmsize; - memcpy(dst, elm, elmsize); + memcpy(dst, elm, dynar->elmsize); } static _XBT_INLINE @@ -127,7 +127,7 @@ xbt_dynar_dump(xbt_dynar_t dynar) { * types (int, char, double, etc) or pointer of pointers (struct **). */ xbt_dynar_t -xbt_dynar_new(const unsigned long elmsize, +xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t * const free_f) { xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1); @@ -201,7 +201,11 @@ xbt_dynar_free(xbt_dynar_t * dynar) { xbt_dynar_free_container(dynar); } } - +/** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */ +void xbt_dynar_free_voidp(void *d) { + xbt_dynar_free( (xbt_dynar_t*) d); +} + /** @brief Count of dynar's elements * * \param dynar the dynar we want to mesure @@ -214,7 +218,7 @@ xbt_dynar_length(const xbt_dynar_t dynar) { /** @brief Retrieve a copy of the Nth element of a dynar. * * \param dynar information dealer - * \param idx index of the slot we want to retrive + * \param idx index of the slot we want to retrieve * \param[out] dst where to put the result to. */ void @@ -239,8 +243,7 @@ xbt_dynar_get_cpy(const xbt_dynar_t dynar, * Make a copy before fooling with it. */ void* -xbt_dynar_get_ptr(const xbt_dynar_t dynar, - const int idx) { +xbt_dynar_get_ptr(const xbt_dynar_t dynar, const int idx) { __sanity_check_dynar(dynar); __sanity_check_idx(idx); @@ -386,6 +389,41 @@ xbt_dynar_remove_at(xbt_dynar_t const dynar, dynar->used--; } +/** @brief Returns the position of the element in the dynar + * + * Raises not_found_error if not found. + */ +int +xbt_dynar_search(xbt_dynar_t const dynar, + void *const elem) { + int it; + + for (it=0; it< dynar->size; it++) + if (!memcmp(_xbt_dynar_elm(dynar, it),elem,dynar->elmsize)) + return it; + + THROW2(not_found_error,0,"Element %p not part of dynar %p",elem,dynar); +} + +/** @brief Returns a boolean indicating whether the element is part of the dynar */ +int +xbt_dynar_member(xbt_dynar_t const dynar, + void *const elem) { + + xbt_ex_t e; + + TRY { + xbt_dynar_search(dynar,elem); + } CATCH(e) { + if (e.category == not_found_error) { + xbt_ex_free(e); + return 0; + } + RETHROW; + } + return 1; +} + /** @brief Make room at the end of the dynar for a new element, and return a pointer to it. * * You can then use regular affectation to set its value instead of relying @@ -831,8 +869,8 @@ XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") { xbt_dynar_free(&d); xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM); - d=xbt_dynar_new(sizeof(char*),&free_string); /* Populate_str [doxygen cruft] */ + d=xbt_dynar_new(sizeof(char*),&free_string); /* 1. Populate the dynar */ for (cpt=0; cpt< NB_ELEM; cpt++) { sprintf(buf,"%d",cpt);