From ebd302f4820855188df16ee8422a947751bf2c50 Mon Sep 17 00:00:00 2001 From: batt Date: Fri, 26 Sep 2008 22:24:36 +0000 Subject: [PATCH] Add macro for shuffling an array. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1860 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cfg/macros.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bertos/cfg/macros.h b/bertos/cfg/macros.h index 69c40485..abf2057d 100644 --- a/bertos/cfg/macros.h +++ b/bertos/cfg/macros.h @@ -126,6 +126,19 @@ #endif /* COMPILER_TYPEOF */ +/** + * Shuffle the content of \a array that counts \a len elements. + */ +#define SHUFFLE(array, len) \ + do { \ + int i, j; \ + for (i = (len) - 1; i > 0; i--) \ + { \ + j = ((i + 1) * (rand() / (RAND_MAX + 1.0))); \ + SWAP((array)[i], (array)[j]); \ + } \ + } while (0) + /** * Macro to swap \a a with \a b, with explicit type \a T for dumb C89 compilers. * -- 2.25.1