/* a generic DYNamic ARray implementation. */
-/* Copyright (c) 2004-2014. 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
static XBT_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);
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)
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
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 */
/* 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++) {
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 */
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);
}
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++) {