SimGrid  3.7
Scalable simulation of distributed systems
Modules
Dynar: generic dynamic array
Usual data structures

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
 Speed optimized access to dynars of scalars
 Cursors on dynar

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

  xbt_dynar_t d;
  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_dynar_shift(d, &i);
    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));
  }

  /* 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

  xbt_dynar_t d;
  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 = 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 */
}


Back to the main Simgrid Documentation page The version of SimGrid documented here is v3.7.
Documentation of other versions can be found in their respective archive files (directory doc/html).
Generated by doxygen