X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/df50c0f6e4133119155fb67f743251b3bb199978..befbbbe1fbb31663a8f91e24ce12df271cf4ae79:/src/xbt/dynar.cpp diff --git a/src/xbt/dynar.cpp b/src/xbt/dynar.cpp index 8281480d73..b1a20fd41f 100644 --- a/src/xbt/dynar.cpp +++ b/src/xbt/dynar.cpp @@ -23,14 +23,14 @@ static inline void _sanity_check_dynar(xbt_dynar_t dynar) static inline void _sanity_check_idx(int idx) { - xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx)); + xbt_assert(idx >= 0, "dynar idx(=%d) < 0", idx); } static inline void _check_inbound_idx(xbt_dynar_t dynar, int idx) { - if (idx < 0 || idx >= (int)dynar->used) { + if (idx < 0 || idx >= static_cast(dynar->used)) { THROWF(bound_error, idx, "dynar is not that long. You asked %d, but it's only %lu long", - (int) (idx), (unsigned long) dynar->used); + idx, static_cast(dynar->used)); } } @@ -114,6 +114,14 @@ extern "C" void xbt_dynar_init(xbt_dynar_t dynar, const unsigned long elmsize, v dynar->free_f = free_f; } +/** @brief Destroy a dynar that was created with xbt_dynar_init */ +extern "C" void xbt_dynar_free_data(xbt_dynar_t dynar) +{ + xbt_dynar_reset(dynar); + if (dynar) + free(dynar->data); +} + /** @brief Destructor of the structure not touching to the content * * \param dynar poor victim @@ -357,9 +365,6 @@ extern "C" void xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx, cons */ extern "C" void xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx, void* const object) { - unsigned long nb_shift; - unsigned long offset; - _sanity_check_dynar(dynar); _check_inbound_idx(dynar, idx); @@ -369,10 +374,10 @@ extern "C" void xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx, void dynar->free_f(_xbt_dynar_elm(dynar, idx)); } - nb_shift = dynar->used - 1 - idx; + unsigned long nb_shift = dynar->used - 1 - idx; if (nb_shift) { - offset = nb_shift * dynar->elmsize; + unsigned long offset = nb_shift * dynar->elmsize; memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1), offset); } @@ -390,16 +395,16 @@ extern "C" void xbt_dynar_remove_n_at(xbt_dynar_t const dynar, const unsigned in { unsigned long nb_shift; unsigned long offset; - unsigned long cur; - if (!n) return; + if (not 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++) { + for (unsigned long cur = idx; cur < idx + n; cur++) { dynar->free_f(_xbt_dynar_elm(dynar, cur)); } } @@ -422,13 +427,13 @@ extern "C" void xbt_dynar_remove_n_at(xbt_dynar_t const dynar, const unsigned in * \code * signed int position = -1; * xbt_dynar_foreach(dynar, iter, elem) { - * if (!memcmp(elem, searched_element, sizeof(*elem))) { + * if (not 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. */ @@ -437,7 +442,7 @@ extern "C" unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void* const el unsigned long it; for (it = 0; it < dynar->used; it++) - if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { + if (not memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { return it; } @@ -449,7 +454,7 @@ extern "C" unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void* const el * * 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). */ @@ -458,14 +463,14 @@ extern "C" signed int xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void unsigned long it; for (it = 0; it < dynar->used; it++) - if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { + if (not memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { return it; } 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. @@ -475,7 +480,7 @@ extern "C" int xbt_dynar_member(xbt_dynar_t const dynar, void* const elem) unsigned long it; for (it = 0; it < dynar->used; it++) - if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { + if (not memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { return 1; } @@ -565,7 +570,8 @@ extern "C" void xbt_dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op) */ extern "C" void xbt_dynar_cursor_rm(xbt_dynar_t dynar, unsigned int* const cursor) { - xbt_dynar_remove_at(dynar, (*cursor)--, nullptr); + xbt_dynar_remove_at(dynar, *cursor, nullptr); + *cursor -= 1; } /** @brief Sorts a dynar according to the function compar_fn @@ -642,10 +648,12 @@ extern "C" void xbt_dynar_three_way_partition(xbt_dynar_t const dynar, int_f_pvo ++i; } else { if (colori == 0) { - elm = _xbt_dynar_elm(dynar, ++p); + ++p; + elm = _xbt_dynar_elm(dynar, p); ++i; } else { /* colori == 2 */ - elm = _xbt_dynar_elm(dynar, --q); + --q; + elm = _xbt_dynar_elm(dynar, q); } if (elm != elmi) { memcpy(tmp, elm, elmsize); @@ -656,7 +664,7 @@ extern "C" void xbt_dynar_three_way_partition(xbt_dynar_t const dynar, int_f_pvo } } -/** @brief Transform a dynar into a nullptr terminated array. +/** @brief Transform a dynar into a nullptr terminated array. * * \param dynar the dynar to transform * \return pointer to the first element of the array @@ -688,9 +696,9 @@ extern "C" int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2, int (*compar)(c { int i ; int size; - if((!d1) && (!d2)) return 0; - if((!d1) || (!d2)) - { + if ((not d1) && (not d2)) + return 0; + if ((not d1) || (not d2)) { XBT_DEBUG("nullptr dynar d1=%p d2=%p",d1,d2); xbt_dynar_free(&d2); return 1; @@ -732,7 +740,6 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") /* Vars_decl [doxygen cruft] */ int i; unsigned int cursor; - int *iptr; xbt_test_add("==== Traverse the empty dynar"); xbt_dynar_t d = xbt_dynar_new(sizeof(int), nullptr); @@ -755,7 +762,7 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") /* 2. Traverse manually the dynar */ for (cursor = 0; cursor < NB_ELEM; cursor++) { - iptr = (int*) xbt_dynar_get_ptr(d, cursor); + int* iptr = (int*)xbt_dynar_get_ptr(d, cursor); xbt_test_assert(cursor == (unsigned int)*iptr, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, *iptr); } @@ -1058,25 +1065,17 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") s1 = xbt_strdup(buf); xbt_dynar_push(d, &s1); } - for (int cpt = 0; cpt < NB_ELEM; cpt++) { - snprintf(buf,1023, "%d", cpt); - s1 = xbt_strdup(buf); - xbt_dynar_replace(d, cpt, &s1); - } - for (int cpt = 0; cpt < NB_ELEM; cpt++) { - snprintf(buf,1023, "%d", cpt); - s1 = xbt_strdup(buf); - xbt_dynar_replace(d, cpt, &s1); - } - for (int cpt = 0; cpt < NB_ELEM; cpt++) { - snprintf(buf,1023, "%d", cpt); - s1 = xbt_strdup(buf); - xbt_dynar_replace(d, cpt, &s1); + for (int i = 0 ; i < 3 ; i++) { + for (int cpt = 0; cpt < NB_ELEM; cpt++) { + snprintf(buf,1023, "%d", cpt); + s1 = xbt_strdup(buf); + xbt_dynar_replace(d, cpt, &s1); + } } for (int cpt = 0; cpt < NB_ELEM; cpt++) { snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); - xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s2); + xbt_test_assert(not strcmp(buf, s2), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s2); free(s2); } xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */ @@ -1093,13 +1092,13 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") /* 2. Traverse the dynar with the macro */ xbt_dynar_foreach(d, iter, s1) { snprintf(buf,1023, "%u", NB_ELEM - iter - 1); - xbt_test_assert(!strcmp(buf, s1), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s1); + xbt_test_assert(not strcmp(buf, s1), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s1); } /* 3. Traverse the dynar with the macro */ for (int cpt = 0; cpt < NB_ELEM; cpt++) { snprintf(buf,1023, "%d", cpt); xbt_dynar_pop(d, &s2); - xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s2); + xbt_test_assert(not strcmp(buf, s2), "The retrieved value is not the same than the injected one (%s!=%s)", buf, s2); free(s2); } /* 4. Free the resources */ @@ -1123,22 +1122,22 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") for (int cpt = 0; cpt < NB_ELEM / 2; cpt++) { snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); - xbt_test_assert(!strcmp(buf, s2), - "The retrieved value is not the same than the injected one at the begining (%s!=%s)", buf, s2); + xbt_test_assert(not strcmp(buf, s2), + "The retrieved value is not the same than the injected one at the begining (%s!=%s)", buf, s2); free(s2); } for (int cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) { snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); - xbt_test_assert(!strcmp(buf, s2), - "The retrieved value is not the same than the injected one in the middle (%s!=%s)", buf, s2); + xbt_test_assert(not strcmp(buf, s2), + "The retrieved value is not the same than the injected one in the middle (%s!=%s)", buf, s2); free(s2); } for (int cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) { snprintf(buf,1023, "%d", cpt); xbt_dynar_shift(d, &s2); - xbt_test_assert(!strcmp(buf, s2), "The retrieved value is not the same than the injected one at the end (%s!=%s)", - buf, s2); + xbt_test_assert(not strcmp(buf, s2), + "The retrieved value is not the same than the injected one at the end (%s!=%s)", buf, s2); free(s2); } xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */ @@ -1155,7 +1154,7 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings") for (int cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) { snprintf(buf,1023, "%d", cpt); xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2); - xbt_test_assert(!strcmp(buf, s2), "Remove a bad value. Got %s, expected %s", s2, buf); + xbt_test_assert(not strcmp(buf, s2), "Remove a bad value. Got %s, expected %s", s2, buf); free(s2); } xbt_dynar_free(&d); /* end_of_doxygen */