]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add ptr array shuffle routine
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 22 Aug 2020 11:51:06 +0000 (12:51 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 22 Aug 2020 11:51:06 +0000 (12:51 +0100)
src/libutil/util.c
src/libutil/util.h

index 19f07e82d1c40c32f9708d68f5369b08914e25a9..9e5147ab57ee462a2f1ad343aa02b18a104b4055 100644 (file)
@@ -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
index c1c29ab30f2d3ef6c35de5b940a937a6f110d322..e947b0a54378b7ef5c3ee5c87e19f73c5b1360b1 100644 (file)
@@ -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,