Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove useless XBT_DEBUG
[simgrid.git] / examples / msg / app-chainsend / iterator.c
1 /* Copyright (c) 2012-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "iterator.h"
7
8 /* http://stackoverflow.com/a/3348142 */
9 /**************************************/
10
11 /* Allocates and initializes a new xbt_dynar iterator for list, using criteria_fn as iteration criteria
12    criteria_fn: given an array size, it must generate a list containing the indices of every item in some order */
13 xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, xbt_dynar_t (*criteria_fn)(int))
14 {
15   xbt_dynar_iterator_t it = xbt_new(xbt_dynar_iterator_s, 1);
16   
17   it->list = list;
18   it->length = xbt_dynar_length(list);
19   it->indices_list = criteria_fn(it->length); // Creates and fills a dynar of int
20   it->criteria_fn = criteria_fn;
21   it->current = 0;
22
23   return it;
24 }
25
26 /* Returns the next element iterated by iterator it, NULL if there are no more elements */
27 void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it)
28 {
29   if (it->current >= it->length) {
30     return NULL;
31   } else {
32     int* next = xbt_dynar_get_ptr(it->indices_list, it->current);
33     it->current++;
34     return xbt_dynar_get_ptr(it->list, *next);
35   }
36 }
37
38 void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it)
39 {
40   xbt_dynar_free_container(&(it->indices_list));
41   xbt_free_ref(&it);
42 }
43
44 xbt_dynar_t forward_indices_list(int size)
45 {
46   xbt_dynar_t indices_list = xbt_dynar_new(sizeof(int), NULL);
47   for (int i = 0; i < size; i++)
48     xbt_dynar_push_as(indices_list, int, i);
49   return indices_list;
50 }