Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge 'master' into mc
[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 static int rand_int(int n)
11 {
12   int limit = RAND_MAX - RAND_MAX % n;
13   int rnd;
14
15   do {
16     rnd = rand();
17   } while (rnd >= limit);
18   
19   return rnd % n;
20 }
21
22 void xbt_dynar_shuffle_in_place(xbt_dynar_t indices_list)
23 {
24   int i, j;
25
26   for (i = xbt_dynar_length(indices_list) - 1; i > 0; i--) {
27     j = rand_int(i + 1);
28     xbt_dynar_swap_elements(indices_list, int, i, j);
29   }
30 }
31 /**************************************/
32
33 /* Allocates and initializes a new xbt_dynar iterator for list, using criteria_fn as iteration criteria
34    criteria_fn: given an array size, it must generate a list containing the indices of every item in some order */
35 xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, xbt_dynar_t (*criteria_fn)(int))
36 {
37   xbt_dynar_iterator_t it = xbt_new(xbt_dynar_iterator_s, 1);
38   
39   it->list = list;
40   it->length = xbt_dynar_length(list);
41   it->indices_list = criteria_fn(it->length); //xbt_dynar_new(sizeof(int), NULL);
42   it->criteria_fn = criteria_fn;
43   it->current = 0;
44
45   return it;
46 }
47
48 void xbt_dynar_iterator_reset(xbt_dynar_iterator_t it)
49 {
50   xbt_dynar_free_container(&(it->indices_list));
51   it->indices_list = it->criteria_fn(it->length);
52   it->current = 0;
53 }
54
55 void xbt_dynar_iterator_seek(xbt_dynar_iterator_t it, int pos)
56 {
57   it->current = pos;
58 }
59
60 /* Returns the next element iterated by iterator it, NULL if there are no more elements */
61 void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it)
62 {
63   int *next;
64   if (it->current >= it->length) {
65     return NULL;
66   } else {
67     next = xbt_dynar_get_ptr(it->indices_list, it->current);
68     it->current++;
69     return xbt_dynar_get_ptr(it->list, *next);
70   }
71 }
72
73 void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it)
74 {
75   xbt_dynar_free_container(&(it->indices_list));
76   xbt_free_ref(&it);
77 }
78
79 xbt_dynar_t forward_indices_list(int size)
80 {
81   xbt_dynar_t indices_list = xbt_dynar_new(sizeof(int), NULL);
82   int i;
83   for (i = 0; i < size; i++)
84     xbt_dynar_push_as(indices_list, int, i);
85   return indices_list;
86 }
87
88 xbt_dynar_t reverse_indices_list(int size)
89 {
90   xbt_dynar_t indices_list = xbt_dynar_new(sizeof(int), NULL);
91   int i;
92   for (i = size-1; i >= 0; i--)
93     xbt_dynar_push_as(indices_list, int, i);
94   return indices_list;
95 }
96
97 xbt_dynar_t random_indices_list(int size)
98 {
99   xbt_dynar_t indices_list = forward_indices_list(size);
100   xbt_dynar_shuffle_in_place(indices_list);
101   return indices_list;
102 }