Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MPI_File* bindings to SMPI, even if they are unimplemented.
[simgrid.git] / examples / msg / chainsend / iterator.c
1 /* Copyright (c) 2012, 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "iterator.h"
8
9 /* http://stackoverflow.com/a/3348142 */
10 /**************************************/
11
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))
15 {
16   xbt_dynar_iterator_t it = xbt_new(xbt_dynar_iterator_s, 1);
17   
18   it->list = list;
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;
22   it->current = 0;
23
24   return it;
25 }
26
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)
29 {
30   int *next;
31   if (it->current >= it->length) {
32     return NULL;
33   } else {
34     next = xbt_dynar_get_ptr(it->indices_list, it->current);
35     it->current++;
36     return xbt_dynar_get_ptr(it->list, *next);
37   }
38 }
39
40 void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it)
41 {
42   xbt_dynar_free_container(&(it->indices_list));
43   xbt_free_ref(&it);
44 }
45
46 xbt_dynar_t forward_indices_list(int size)
47 {
48   xbt_dynar_t indices_list = xbt_dynar_new(sizeof(int), NULL);
49   int i;
50   for (i = 0; i < size; i++)
51     xbt_dynar_push_as(indices_list, int, i);
52   return indices_list;
53 }