]> git.proxmox.com Git - mirror_ovs.git/blobdiff - lib/svec.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / svec.c
index d083ebb864c4f3e164fe947547327f57372835af..c1b986bab1084fcdf75e657512bc21ef99816dc9 100644 (file)
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
+#include "random.h"
 #include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(svec);
 
@@ -127,7 +128,9 @@ compare_strings(const void *a_, const void *b_)
 void
 svec_sort(struct svec *svec)
 {
-    qsort(svec->names, svec->n, sizeof *svec->names, compare_strings);
+    if (svec->n) {
+        qsort(svec->names, svec->n, sizeof *svec->names, compare_strings);
+    }
 }
 
 void
@@ -172,6 +175,23 @@ svec_compact(struct svec *svec)
     svec->n = j;
 }
 
+static void
+swap_strings(char **a, char **b)
+{
+    char *tmp = *a;
+    *a = *b;
+    *b = tmp;
+}
+
+void
+svec_shuffle(struct svec *svec)
+{
+    for (size_t i = 0; i < svec->n; i++) {
+        size_t j = i + random_range(svec->n - i);
+        swap_strings(&svec->names[i], &svec->names[j]);
+    }
+}
+
 void
 svec_diff(const struct svec *a, const struct svec *b,
           struct svec *a_only, struct svec *both, struct svec *b_only)