lib: Fix netbsd compilation error.
[cascardo/ovs.git] / lib / sset.c
index 443538d..f9d4fc0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
+ * Copyright (c) 2011, 2012, 2013, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -269,21 +269,26 @@ sset_at_position(const struct sset *set, uint32_t *bucketp, uint32_t *offsetp)
     return SSET_NODE_FROM_HMAP_NODE(hmap_node);
 }
 
-static int
-compare_string_pointers(const void *a_, const void *b_)
+/* Replaces 'a' by the intersection of 'a' and 'b'.  That is, removes from 'a'
+ * all of the strings that are not also in 'b'. */
+void
+sset_intersect(struct sset *a, const struct sset *b)
 {
-    const char *const *a = a_;
-    const char *const *b = b_;
+    const char *name, *next;
 
-    return strcmp(*a, *b);
+    SSET_FOR_EACH_SAFE (name, next, a) {
+        if (!sset_contains(b, name)) {
+            sset_delete(a, SSET_NODE_FROM_NAME(name));
+        }
+    }
 }
 
-/* Returns a null-terminated array of pointers to the strings in 'set', sorted
- * alphabetically.  The caller must free the returned array when it is no
+/* Returns a null-terminated array of pointers to the strings in 'set', in no
+ * particular order.  The caller must free the returned array when it is no
  * longer needed, but the strings in the array belong to 'set' and thus must
  * not be modified or freed. */
 const char **
-sset_sort(const struct sset *set)
+sset_array(const struct sset *set)
 {
     size_t n = sset_count(set);
     const char **array;
@@ -298,7 +303,26 @@ sset_sort(const struct sset *set)
     ovs_assert(i == n);
     array[n] = NULL;
 
-    qsort(array, n, sizeof *array, compare_string_pointers);
+    return array;
+}
+
+static int
+compare_string_pointers(const void *a_, const void *b_)
+{
+    const char *const *a = a_;
+    const char *const *b = b_;
+
+    return strcmp(*a, *b);
+}
 
+/* Returns a null-terminated array of pointers to the strings in 'set', sorted
+ * alphabetically.  The caller must free the returned array when it is no
+ * longer needed, but the strings in the array belong to 'set' and thus must
+ * not be modified or freed. */
+const char **
+sset_sort(const struct sset *set)
+{
+    const char **array = sset_array(set);
+    qsort(array, sset_count(set), sizeof *array, compare_string_pointers);
     return array;
 }