diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-08-22 12:51:06 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-08-22 12:51:06 +0100 |
commit | dc917551811ef6850831f0770016e65f3d2e51dd (patch) | |
tree | bbf1cf36f424713cefdc9f1c914d9e3d1addeeee /src | |
parent | d1cab3edeef224d8d2611087d4fa12623e68165d (diff) | |
download | rspamd-dc917551811ef6850831f0770016e65f3d2e51dd.tar.gz rspamd-dc917551811ef6850831f0770016e65f3d2e51dd.zip |
[Minor] Add ptr array shuffle routine
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/util.c | 17 | ||||
-rw-r--r-- | src/libutil/util.h | 6 |
2 files changed, 23 insertions, 0 deletions
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 diff --git a/src/libutil/util.h b/src/libutil/util.h index c1c29ab30..e947b0a54 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -478,6 +478,12 @@ double rspamd_set_counter_ema (struct rspamd_counter_data *cd, double rspamd_set_counter (struct rspamd_counter_data *cd, gdouble value); +/** + * Shuffle elements in an array inplace + * @param ar + */ +void rspamd_ptr_array_shuffle (GPtrArray *ar); + enum rspamd_pbkdf_version_id { RSPAMD_PBKDF_ID_V1 = 1, RSPAMD_PBKDF_ID_V2 = 2, |