X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d5c11dbc7ac52c97c04aee539dbf9e69a2a68f68..bc48db087894fd960073b3120cebf90e6b2f8c02:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 247a834d77..10a1086d2a 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -1,6 +1,6 @@ /* a generic DYNamic ARray implementation. */ -/* Copyright (c) 2004-2013. The SimGrid Team. +/* Copyright (c) 2004-2015. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -15,33 +15,33 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn, xbt, "Dynamic arrays"); -static XBT_INLINE void _sanity_check_dynar(xbt_dynar_t dynar) +static inline void _sanity_check_dynar(xbt_dynar_t dynar) { xbt_assert(dynar, "dynar is NULL"); } -static XBT_INLINE void _sanity_check_idx(int idx) +static inline void _sanity_check_idx(int idx) { xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx)); } -static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx) +static inline void _check_inbound_idx(xbt_dynar_t dynar, int idx) { - if (idx < 0 || idx >= dynar->used) { + if (idx < 0 || idx >= (int)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); } } -static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar) +static inline void _check_populated_dynar(xbt_dynar_t dynar) { if (dynar->used == 0) { THROWF(bound_error, 0, "dynar %p is empty", dynar); } } -static XBT_INLINE +static inline void _xbt_dynar_resize(xbt_dynar_t dynar, unsigned long new_size) { if (new_size != dynar->size) { @@ -50,7 +50,7 @@ void _xbt_dynar_resize(xbt_dynar_t dynar, unsigned long new_size) } } -static XBT_INLINE +static inline void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb) { const unsigned long old_size = dynar->size; @@ -63,7 +63,7 @@ static XBT_INLINE } } -static XBT_INLINE +static inline void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx) { char *const data = (char *) dynar->data; @@ -72,7 +72,7 @@ static XBT_INLINE return data + idx * elmsize; } -static XBT_INLINE +static inline void _xbt_dynar_get_elm(void *const dst, const xbt_dynar_t dynar, const unsigned long idx) @@ -82,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", @@ -145,7 +134,7 @@ void xbt_dynar_free_container(xbt_dynar_t * dynar) * * \param dynar who to squeeze */ -XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar) +inline void xbt_dynar_reset(xbt_dynar_t const dynar) { _sanity_check_dynar(dynar); @@ -205,7 +194,7 @@ void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted) * kilkil a dynar and its content */ -XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar) +inline void xbt_dynar_free(xbt_dynar_t * dynar) { if (dynar && *dynar) { xbt_dynar_reset(*dynar); @@ -224,7 +213,7 @@ void xbt_dynar_free_voidp(void *d) * * \param dynar the dynar we want to mesure */ -XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar) +inline unsigned long xbt_dynar_length(const xbt_dynar_t dynar) { return (dynar ? (unsigned long) dynar->used : (unsigned long) 0); } @@ -234,7 +223,7 @@ XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar) *\param dynar the dynat we want to check */ -XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar) +inline int xbt_dynar_is_empty(const xbt_dynar_t dynar) { return (xbt_dynar_length(dynar) == 0); } @@ -245,7 +234,7 @@ XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar) * \param idx index of the slot we want to retrieve * \param[out] dst where to put the result to. */ -XBT_INLINE void +inline void xbt_dynar_get_cpy(const xbt_dynar_t dynar, const unsigned long idx, void *const dst) { @@ -264,7 +253,7 @@ xbt_dynar_get_cpy(const xbt_dynar_t dynar, * \warning The returned value is the actual content of the dynar. * Make a copy before fooling with it. */ -XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar, +inline void *xbt_dynar_get_ptr(const xbt_dynar_t dynar, const unsigned long idx) { @@ -276,7 +265,7 @@ XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar, return res; } -XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar, +inline void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar, const unsigned long idx) { _sanity_check_dynar(dynar); @@ -300,7 +289,7 @@ XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar, * * If you want to free the previous content, use xbt_dynar_replace(). */ -XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx, +inline void xbt_dynar_set(xbt_dynar_t dynar, const int idx, const void *const src) { @@ -370,7 +359,7 @@ void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx) * moving the previously existing value and all subsequent ones to one * position right in the dynar. */ -XBT_INLINE void +inline void xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx, const void *const src) { @@ -482,8 +471,8 @@ unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem) return it; } - THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem, - dynar); + THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem, dynar); + return -1; // Won't happen, just to please eclipse } /** @brief Returns the position of the element in the dynar (or -1 if not found) @@ -536,13 +525,13 @@ int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem) * You can then use regular affectation to set its value instead of relying * on the slow memcpy. This is what xbt_dynar_push_as() does. */ -XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar) +inline void *xbt_dynar_push_ptr(xbt_dynar_t const dynar) { return xbt_dynar_insert_at_ptr(dynar, dynar->used); } /** @brief Add an element at the end of the dynar */ -XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar, +inline void xbt_dynar_push(xbt_dynar_t const dynar, const void *const src) { /* checks done in xbt_dynar_insert_at_ptr */ @@ -554,7 +543,7 @@ XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar, * You can then use regular affectation to set its value instead of relying * on the slow memcpy. This is what xbt_dynar_pop_as() does. */ -XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar) +inline void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar) { _check_populated_dynar(dynar); XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar); @@ -563,7 +552,7 @@ XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar) } /** @brief Get and remove the last element of the dynar */ -XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst) +inline void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst) { /* sanity checks done by remove_at */ @@ -575,7 +564,7 @@ XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst) * * This is less efficient than xbt_dynar_push() */ -XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar, +inline void xbt_dynar_unshift(xbt_dynar_t const dynar, const void *const src) { @@ -587,7 +576,7 @@ XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar, * * This is less efficient than xbt_dynar_pop() */ -XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst) +inline void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst) { /* sanity checks done by remove_at */ @@ -599,7 +588,7 @@ XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst) * The mapped function may change the value of the element itself, * but should not mess with the structure of the dynar. */ -XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar, +inline void xbt_dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op) { char *const data = (char *) dynar->data; @@ -620,7 +609,7 @@ XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar, * * This function can be used while traversing without problem. */ -XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar, +inline void xbt_dynar_cursor_rm(xbt_dynar_t dynar, unsigned int *const cursor) { @@ -635,12 +624,22 @@ XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar, * 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, +inline void xbt_dynar_sort(xbt_dynar_t dynar, int_f_cpvoid_cpvoid_t compar_fn) { qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn); } +static int strcmp_voidp(const void *pa, const void *pb) { + return strcmp(*(const char **)pa, *(const char **)pb); +} +/** @brief Sorts a dynar of strings (ie, char* data) */ +xbt_dynar_t xbt_dynar_sort_strings(xbt_dynar_t dynar) +{ + xbt_dynar_sort(dynar, strcmp_voidp); + return dynar; // to enable functional uses +} + /** @brief Sorts a dynar according to their color assuming elements can have only three colors. * Since there are only three colors, it is linear and much faster than a classical sort. * See for example http://en.wikipedia.org/wiki/Dutch_national_flag_problem @@ -687,10 +686,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) +inline void *xbt_dynar_to_array(xbt_dynar_t dynar) { void *res; xbt_dynar_shrink(dynar, 1); @@ -700,8 +702,17 @@ 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. */ int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2, int(*compar)(const void *, const void *)) @@ -826,6 +837,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 */ @@ -1125,22 +1150,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++) { @@ -1159,7 +1184,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 */ @@ -1190,12 +1215,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); } @@ -1233,7 +1258,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++) {