X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/0d532db1711fc2383a3df2cb20a583281ad5adce..8b31411ad567a87b51085f1514b1ecaca54c47b6:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 0fa7f41328..fc937cb37b 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -1,6 +1,6 @@ /* a generic DYNamic ARray implementation. */ -/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team. +/* Copyright (c) 2004-2014. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -34,16 +34,6 @@ static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx) } } -static XBT_INLINE void _check_sloppy_inbound_idx(xbt_dynar_t dynar, - int idx) -{ - if (idx > dynar->used) { - THROWF(bound_error, idx, - "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); - } -} - static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar) { if (dynar->used == 0) { @@ -92,17 +82,6 @@ _xbt_dynar_get_elm(void *const dst, memcpy(dst, elm, dynar->elmsize); } -static XBT_INLINE - void -_xbt_dynar_put_elm(const xbt_dynar_t dynar, - const unsigned long idx, const void *const src) -{ - void *const elm = _xbt_dynar_elm(dynar, idx); - const unsigned long elmsize = dynar->elmsize; - - memcpy(elm, src, elmsize); -} - void xbt_dynar_dump(xbt_dynar_t dynar) { XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p", @@ -159,7 +138,7 @@ XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar) { _sanity_check_dynar(dynar); - XBT_DEBUG("Reset the dynar %p", (void *) dynar); + XBT_CDEBUG(xbt_dyn, "Reset the dynar %p", (void *) dynar); if (dynar->free_f) { xbt_dynar_map(dynar, dynar->free_f); } @@ -425,8 +404,60 @@ xbt_dynar_remove_at(xbt_dynar_t const dynar, dynar->used--; } +/** @brief Remove a slice of the dynar, sliding the rest of the values to the left + * + * This function removes an n-sized slice that starts at element idx. It is equivalent + * to xbt_dynar_remove_at with a NULL object argument if n equals to 1. + * + * Each of the removed elements is freed using the free_f function passed at dynar + * creation. + */ +void +xbt_dynar_remove_n_at(xbt_dynar_t const dynar, + const unsigned int n, const int idx) +{ + unsigned long nb_shift; + unsigned long offset; + unsigned long cur; + + if (!n) return; + + _sanity_check_dynar(dynar); + _check_inbound_idx(dynar, idx); + _check_inbound_idx(dynar, idx + n - 1); + + if (dynar->free_f) { + for (cur = idx; cur < idx + n; cur++) { + dynar->free_f(_xbt_dynar_elm(dynar, cur)); + } + } + + nb_shift = dynar->used - n - idx; + + if (nb_shift) { + offset = nb_shift * dynar->elmsize; + memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + n), + offset); + } + + dynar->used -= n; +} + /** @brief Returns the position of the element in the dynar * + * Beware that if your dynar contains pointed values (such as strings) instead + * of scalar, this function compares the pointer value, not what's pointed. The only + * solution to search for a pointed value is then to write the foreach loop yourself: + * \code + * signed int position = -1; + * xbt_dynar_foreach(dynar, iter, elem) { + * if (!memcmp(elem, searched_element, sizeof(*elem))) { + * position = iter; + * break; + * } + * } + * \endcode + * * Raises not_found_error if not found. If you have less than 2 millions elements, * you probably want to use #xbt_dynar_search_or_negative() instead, so that you * don't have to TRY/CATCH on element not found. @@ -446,6 +477,10 @@ unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem) /** @brief Returns the position of the element in the dynar (or -1 if not found) * + * Beware that if your dynar contains pointed values (such as + * strings) instead of scalar, this function is probably not what you + * want. Check the documentation of xbt_dynar_search() for more info. + * * Note that usually, the dynar indices are unsigned integers. If you have more * than 2 million elements in your dynar, this very function will not work (but the other will). */ @@ -461,7 +496,12 @@ signed int xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const ele return -1; } -/** @brief Returns a boolean indicating whether the element is part of the dynar */ +/** @brief Returns a boolean indicating whether the element is part of the dynar + * + * Beware that if your dynar contains pointed values (such as + * strings) instead of scalar, this function is probably not what you + * want. Check the documentation of xbt_dynar_search() for more info. + */ int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem) { @@ -487,9 +527,6 @@ int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem) */ XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar) { - /* we have to inline xbt_dynar_insert_at_ptr here to make sure that - dynar->used don't change between reading it and getting the lock - within xbt_dynar_insert_at_ptr */ return xbt_dynar_insert_at_ptr(dynar, dynar->used); } @@ -498,8 +535,7 @@ XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar, const void *const src) { /* checks done in xbt_dynar_insert_at_ptr */ - memcpy(xbt_dynar_insert_at_ptr(dynar, dynar->used), src, - dynar->elmsize); + memcpy(xbt_dynar_insert_at_ptr(dynar, dynar->used), src, dynar->elmsize); } /** @brief Mark the last dynar's element as unused and return a pointer to it. @@ -510,7 +546,7 @@ XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar, XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar) { _check_populated_dynar(dynar); - XBT_DEBUG("Pop %p", (void *) dynar); + XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar); dynar->used--; return _xbt_dynar_elm(dynar, dynar->used); } @@ -520,7 +556,7 @@ XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst) { /* sanity checks done by remove_at */ - XBT_DEBUG("Pop %p", (void *) dynar); + XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar); xbt_dynar_remove_at(dynar, dynar->used - 1, dst); } @@ -591,11 +627,7 @@ XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar, XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar, int_f_cpvoid_cpvoid_t compar_fn) { -#ifdef HAVE_MERGESORT - mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn); -#else qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn); -#endif } /** @brief Sorts a dynar according to their color assuming elements can have only three colors. @@ -644,10 +676,13 @@ XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar, } /** @brief Transform a dynar into a NULL terminated array. - * The dynar won't be usable afterwards. - * \param dynar the dynar to transform + * + * \param dynar the dynar to transform + * \return pointer to the first element of the array + * + * Note: The dynar won't be usable afterwards. */ -XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar) +XBT_INLINE void *xbt_dynar_to_array(xbt_dynar_t dynar) { void *res; xbt_dynar_shrink(dynar, 1); @@ -657,10 +692,19 @@ XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar) return res; } -/* - * Return 0 if d1 and d2 are equal and 1 if not equal +/** @brief Compare two dynars + * + * \param d1 first dynar to compare + * \param d2 second dynar to compare + * \param compar function to use to compare elements + * \return 0 if d1 and d2 are equal and 1 if not equal + * + * d1 and d2 should be dynars of pointers. The compar function takes two + * elements and returns 0 when they are considered equal, and a value different + * of zero when they are considered different. Finally, d2 is destroyed + * afterwards. */ -XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2, +int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2, int(*compar)(const void *, const void *)) { int i ; @@ -783,6 +827,20 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d)); } + int* pi; + xbt_dynar_foreach_ptr(d, cursor, pi) { + *pi = 0; + } + xbt_dynar_foreach(d, cursor, i) { + xbt_test_assert(i == 0, "The value is not the same as the expected one."); + } + xbt_dynar_foreach_ptr(d, cursor, pi) { + *pi = 1; + } + xbt_dynar_foreach(d, cursor, i) { + xbt_test_assert(i == 1, "The value is not the same as the expected one."); + } + /* 5. Free the resources */ xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */ xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */ @@ -1082,22 +1140,22 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") /* 1. Populate the dynar */ for (cpt = 0; cpt < NB_ELEM; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_push(d, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_replace(d, cpt, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_replace(d, cpt, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_replace(d, cpt, &s1); } for (cpt = 0; cpt < NB_ELEM; cpt++) { @@ -1116,7 +1174,7 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") d = xbt_dynar_new(sizeof(char **), &xbt_free_ref); for (cpt = 0; cpt < NB_ELEM; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_unshift(d, &s1); } /* 2. Traverse the dynar with the macro */ @@ -1147,12 +1205,12 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") d = xbt_dynar_new(sizeof(char *), &xbt_free_ref); for (cpt = 0; cpt < NB_ELEM; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_push(d, &s1); } for (cpt = 0; cpt < NB_ELEM / 5; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_insert_at(d, NB_ELEM / 2, &s1); } @@ -1190,7 +1248,7 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") d = xbt_dynar_new(sizeof(char *), &xbt_free_ref); for (cpt = 0; cpt < NB_ELEM; cpt++) { sprintf(buf, "%d", cpt); - s1 = strdup(buf); + s1 = xbt_strdup(buf); xbt_dynar_push(d, &s1); } for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {