From dc917551811ef6850831f0770016e65f3d2e51dd Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 22 Aug 2020 12:51:06 +0100 Subject: [Minor] Add ptr array shuffle routine --- src/libutil/util.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/libutil/util.c') diff --git a/src/libutil/util.c b/src/libutil/util.c index 19f07e82d..9e5147ab5 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2437,4 +2437,21 @@ rspamd_set_counter_ema (struct rspamd_counter_data *cd, cd->number ++; return cd->mean; +} + +void +rspamd_ptr_array_shuffle (GPtrArray *ar) +{ + if (ar->len < 2) { + return; + } + + guint n = ar->len; + + for (guint i = 0; i < n - 1; i++) { + guint j = i + rspamd_random_uint64_fast () % (n - i); + gpointer t = g_ptr_array_index (ar, j); + g_ptr_array_index (ar, j) = g_ptr_array_index (ar, i); + g_ptr_array_index (ar, i) = t; + } } \ No newline at end of file -- cgit v1.2.3