commit dc91755: [Minor] Add ptr array shuffle routine

Vsevolod Stakhov vsevolod at highsecure.ru
Sat Aug 22 12:14:07 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-08-22 12:51:06 +0100
URL: https://github.com/rspamd/rspamd/commit/dc917551811ef6850831f0770016e65f3d2e51dd

[Minor] Add ptr array shuffle routine

---
 src/libutil/util.c | 17 +++++++++++++++++
 src/libutil/util.h |  6 ++++++
 2 files changed, 23 insertions(+)

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,


More information about the Commits mailing list