1 /* Copyright (c) 2012, 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 /* http://stackoverflow.com/a/3348142 */
10 /**************************************/
12 /* Allocates and initializes a new xbt_dynar iterator for list, using criteria_fn as iteration criteria
13 criteria_fn: given an array size, it must generate a list containing the indices of every item in some order */
14 xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, xbt_dynar_t (*criteria_fn)(int))
16 xbt_dynar_iterator_t it = xbt_new(xbt_dynar_iterator_s, 1);
19 it->length = xbt_dynar_length(list);
20 it->indices_list = criteria_fn(it->length); //xbt_dynar_new(sizeof(int), NULL);
21 it->criteria_fn = criteria_fn;
27 /* Returns the next element iterated by iterator it, NULL if there are no more elements */
28 void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it)
31 if (it->current >= it->length) {
34 next = xbt_dynar_get_ptr(it->indices_list, it->current);
36 return xbt_dynar_get_ptr(it->list, *next);
40 void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it)
42 xbt_dynar_free_container(&(it->indices_list));
46 xbt_dynar_t forward_indices_list(int size)
48 xbt_dynar_t indices_list = xbt_dynar_new(sizeof(int), NULL);
50 for (i = 0; i < size; i++)
51 xbt_dynar_push_as(indices_list, int, i);