X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/2aaa919aa7451546f9576b3a71fc41f896c24b31..5a1a25a87636041e2863cfb26b85c19c3a3d2ca6:/examples/msg/chainsend/iterator.c diff --git a/examples/msg/chainsend/iterator.c b/examples/msg/chainsend/iterator.c index a4788bcfe2..2d0eccc271 100644 --- a/examples/msg/chainsend/iterator.c +++ b/examples/msg/chainsend/iterator.c @@ -1,27 +1,12 @@ -#include "iterator.h" - -/* http://stackoverflow.com/a/3348142 */ -static int rand_int(int n) -{ - int limit = RAND_MAX - RAND_MAX % n; - int rnd; +/* Copyright (c) 2012, 2014. The SimGrid Team. + * All rights reserved. */ - do { - rnd = rand(); - } while (rnd >= limit); - - return rnd % n; -} +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ -void xbt_dynar_shuffle_in_place(xbt_dynar_t indices_list) -{ - int i, j; +#include "iterator.h" - for (i = xbt_dynar_length(indices_list) - 1; i > 0; i--) { - j = rand_int(i + 1); - xbt_dynar_swap_elements(indices_list, int, i, j); - } -} +/* http://stackoverflow.com/a/3348142 */ /**************************************/ /* Allocates and initializes a new xbt_dynar iterator for list, using criteria_fn as iteration criteria @@ -35,27 +20,15 @@ xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, xbt_dynar_t (*crit it->indices_list = criteria_fn(it->length); //xbt_dynar_new(sizeof(int), NULL); it->criteria_fn = criteria_fn; it->current = 0; -} -void xbt_dynar_iterator_reset(xbt_dynar_iterator_t it) -{ - xbt_dynar_free_container(&(it->indices_list)); - it->indices_list = it->criteria_fn(it->length); - it->current = 0; -} - -void xbt_dynar_iterator_seek(xbt_dynar_iterator_t it, int pos) -{ - it->current = pos; + return it; } /* Returns the next element iterated by iterator it, NULL if there are no more elements */ void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it) { int *next; - //XBT_INFO("%d current\n", next); if (it->current >= it->length) { - //XBT_INFO("Nothing to return!\n"); return NULL; } else { next = xbt_dynar_get_ptr(it->indices_list, it->current); @@ -78,19 +51,3 @@ xbt_dynar_t forward_indices_list(int size) xbt_dynar_push_as(indices_list, int, i); return indices_list; } - -xbt_dynar_t reverse_indices_list(int size) -{ - xbt_dynar_t indices_list = xbt_dynar_new(sizeof(int), NULL); - int i; - for (i = size-1; i >= 0; i--) - xbt_dynar_push_as(indices_list, int, i); - return indices_list; -} - -xbt_dynar_t random_indices_list(int size) -{ - xbt_dynar_t indices_list = forward_indices_list(size); - xbt_dynar_shuffle_in_place(indices_list); - return indices_list; -}