SimGrid  3.13
Versatile Simulation of Distributed Systems
Dynar: generic dynamic array

DynArr are dynamically sized vector which may contain any type of variables. More...

Modules

 Dynar constructor and destructor
 
 Dynar as a regular array
 
 Dynar miscellaneous functions
 
 Perl-like use of dynars
 
 Direct manipulation to the dynars content
 Those functions do not retrieve the content, but only their address.
 
 Speed optimized access to dynars of scalars
 While the other functions use a memcpy to retrieve the content into the user provided area, those ones use a regular affectation.
 
 Cursors on dynar
 Cursors are used to iterate over the structure.
 

Detailed Description

DynArr are dynamically sized vector which may contain any type of variables.

These are the SimGrid version of the dynamically size arrays, which all C programmer recode one day or another.

For performance concerns, the content of DynArr must be homogeneous (in contrary to dictionnaries – see the Dict: generic dictionnary section). You thus have to provide the function which will be used to free the content at structure creation (of type void_f_ppvoid_t or void_f_pvoid_t).

Example with scalar

int i, cpt;
unsigned int cursor;
int *iptr;
/* 1. Populate the dynar */
d = xbt_dynar_new(sizeof(int), NULL);
for (cpt = 0; cpt < NB_ELEM; cpt++) {
xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
/* xbt_dynar_push(d,&cpt); This would also work */
xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
}
/* 2. Traverse manually the dynar */
for (cursor = 0; cursor < NB_ELEM; cursor++) {
iptr = xbt_dynar_get_ptr(d, cursor);
xbt_test_assert(cursor == *iptr, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt);
}
/* 3. Traverse the dynar using the neat macro to that extend */
xbt_dynar_foreach(d, cursor, cpt) {
xbt_test_assert(cursor == cpt, "The retrieved value is not the same than the injected one (%u!=%d)", cursor, cpt);
}
/* end_of_traversal */
/* 4. Shift all the values */
for (cpt = 0; cpt < NB_ELEM; cpt++) {
xbt_test_assert(i == cpt, "The retrieved value is not the same than the injected one (%d!=%d)", i, cpt);
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 */

Example with pointed data

int cpt;
unsigned int iter;
char buf[1024];
char *s1, *s2;
d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
/* 1. Populate the dynar */
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
s1 = xbt_strdup(buf);
xbt_dynar_push(d, &s1);
}
/* 2. Traverse the dynar with the macro */
xbt_dynar_foreach(d, iter, s1) {
sprintf(buf, "%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);
}
/* 3. Traverse the dynar with the macro */
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%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);
free(s2);
}
/* 4. 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); /* end_of_doxygen */
}

Note that if you use dynars to store pointed data, the xbt_dynar_search(), xbt_dynar_search_or_negative() and xbt_dynar_member() won't be for you. Instead of comparing your pointed elements, they compare the pointer to them. See the documentation of xbt_dynar_search() for more info.