Blame SOURCES/0021-util-add-_adcli_strv_remove_unsorted.patch

48b328
From 4208646609da9b25b70c21f5f39c92fabbd59dfc Mon Sep 17 00:00:00 2001
48b328
From: Sumit Bose <sbose@redhat.com>
48b328
Date: Thu, 14 Jun 2018 16:48:22 +0200
48b328
Subject: [PATCH 21/23] util: add _adcli_strv_remove_unsorted
48b328
48b328
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1547014
48b328
---
48b328
 library/adprivate.h |   4 ++
48b328
 library/adutil.c    |  21 ++++++++
48b328
 library/seq.c       | 149 +++++++++++++++++++++++++++++++++++++++++++++++++---
48b328
 library/seq.h       |  12 +++++
48b328
 4 files changed, 179 insertions(+), 7 deletions(-)
48b328
48b328
diff --git a/library/adprivate.h b/library/adprivate.h
48b328
index 7485249..bc9df6d 100644
48b328
--- a/library/adprivate.h
48b328
+++ b/library/adprivate.h
48b328
@@ -111,6 +111,10 @@ char **        _adcli_strv_add               (char **strv,
48b328
                                               char *string,
48b328
                                               int *length) GNUC_WARN_UNUSED;
48b328
 
48b328
+void           _adcli_strv_remove_unsorted   (char **strv,
48b328
+                                              const char *string,
48b328
+                                              int *length);
48b328
+
48b328
 void           _adcli_strv_free              (char **strv);
48b328
 
48b328
 int            _adcli_strv_has               (char **strv,
48b328
diff --git a/library/adutil.c b/library/adutil.c
48b328
index a27bd68..6334b52 100644
48b328
--- a/library/adutil.c
48b328
+++ b/library/adutil.c
48b328
@@ -221,6 +221,27 @@ _adcli_strv_add (char **strv,
48b328
 	return seq_push (strv, length, string);
48b328
 }
48b328
 
48b328
+#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
48b328
+
48b328
+void
48b328
+_adcli_strv_remove_unsorted (char **strv,
48b328
+                             const char *string,
48b328
+                             int *length)
48b328
+{
48b328
+	int len;
48b328
+
48b328
+	return_if_fail (string != NULL);
48b328
+
48b328
+	if (!length) {
48b328
+		len = seq_count (strv);
48b328
+		length = &len;
48b328
+	}
48b328
+
48b328
+	return seq_remove_unsorted (strv, length, discard_const (string),
48b328
+	                            (seq_compar)strcasecmp, free);
48b328
+}
48b328
+
48b328
+
48b328
 int
48b328
 _adcli_strv_has (char **strv,
48b328
                  const char *str)
48b328
diff --git a/library/seq.c b/library/seq.c
48b328
index 627dcaf..8e7475d 100644
48b328
--- a/library/seq.c
48b328
+++ b/library/seq.c
48b328
@@ -111,6 +111,24 @@ seq_push (seq_voidp sequence,
48b328
 	return seq;
48b328
 }
48b328
 
48b328
+static int
48b328
+linear_search (void **seq,
48b328
+               int low,
48b328
+               int high,
48b328
+               void *match,
48b328
+               seq_compar compar)
48b328
+{
48b328
+	int at;
48b328
+
48b328
+	for (at = low; at < high; at++) {
48b328
+		if (compar (match, seq[at]) == 0) {
48b328
+			break;
48b328
+		}
48b328
+	}
48b328
+
48b328
+	return at;
48b328
+}
48b328
+
48b328
 static int
48b328
 binary_search (void **seq,
48b328
                int low,
48b328
@@ -171,12 +189,13 @@ seq_insert (seq_voidp sequence,
48b328
 	return seq;
48b328
 }
48b328
 
48b328
-void
48b328
-seq_remove (seq_voidp sequence,
48b328
-            int *length,
48b328
-            void *match,
48b328
-            seq_compar compar,
48b328
-            seq_destroy destroy)
48b328
+static void
48b328
+seq_remove_int (seq_voidp sequence,
48b328
+                int *length,
48b328
+                void *match,
48b328
+                seq_search search,
48b328
+                seq_compar compar,
48b328
+                seq_destroy destroy)
48b328
 {
48b328
 	void **seq = sequence;
48b328
 	int at;
48b328
@@ -187,7 +206,7 @@ seq_remove (seq_voidp sequence,
48b328
 	assert (match != NULL);
48b328
 
48b328
 	len = *length;
48b328
-	at = binary_search (seq, 0, len, match, compar);
48b328
+	at = search (seq, 0, len, match, compar);
48b328
 
48b328
 	/* We have a matching value */
48b328
 	if (at < len && compar (match, seq[at]) == 0) {
48b328
@@ -201,6 +220,26 @@ seq_remove (seq_voidp sequence,
48b328
 	*length = len;
48b328
 }
48b328
 
48b328
+void
48b328
+seq_remove (seq_voidp sequence,
48b328
+            int *length,
48b328
+            void *match,
48b328
+            seq_compar compar,
48b328
+            seq_destroy destroy)
48b328
+{
48b328
+	return seq_remove_int (sequence, length, match, binary_search, compar, destroy);
48b328
+}
48b328
+
48b328
+void
48b328
+seq_remove_unsorted (seq_voidp sequence,
48b328
+                     int *length,
48b328
+                     void *match,
48b328
+                     seq_compar compar,
48b328
+                     seq_destroy destroy)
48b328
+{
48b328
+	return seq_remove_int (sequence, length, match, linear_search, compar, destroy);
48b328
+}
48b328
+
48b328
 void
48b328
 seq_filter (seq_voidp sequence,
48b328
             int *length,
48b328
@@ -430,6 +469,99 @@ test_remove (void)
48b328
 	seq_free (seq, NULL);
48b328
 }
48b328
 
48b328
+static void
48b328
+test_remove_unsorted (void)
48b328
+{
48b328
+	void **seq = NULL;
48b328
+	int len = 0;
48b328
+
48b328
+	seq = seq_push (seq, &len, "3");
48b328
+	seq = seq_push (seq, &len, "5");
48b328
+	seq = seq_push (seq, &len, "1");
48b328
+	seq = seq_push (seq, &len, "4");
48b328
+	seq = seq_push (seq, &len, "2");
48b328
+
48b328
+	assert_str_eq (seq[0], "3");
48b328
+	assert_str_eq (seq[1], "5");
48b328
+	assert_str_eq (seq[2], "1");
48b328
+	assert_str_eq (seq[3], "4");
48b328
+	assert_str_eq (seq[4], "2");
48b328
+	assert (seq[5] == NULL);
48b328
+	assert_num_eq (len, 5);
48b328
+
48b328
+	seq_remove_unsorted (seq, &len, "3", (seq_compar)strcmp, NULL);
48b328
+	seq_remove_unsorted (seq, &len, "2", (seq_compar)strcmp, NULL);
48b328
+
48b328
+	assert_str_eq (seq[0], "5");
48b328
+	assert_str_eq (seq[1], "1");
48b328
+	assert_str_eq (seq[2], "4");
48b328
+	assert (seq[3] == NULL);
48b328
+	assert_num_eq (len, 3);
48b328
+
48b328
+	seq_free (seq, NULL);
48b328
+}
48b328
+
48b328
+static void
48b328
+test_remove_first (void)
48b328
+{
48b328
+	void **seq = NULL;
48b328
+	int len = 0;
48b328
+
48b328
+	seq = seq_insert (seq, &len, "3", (seq_compar)strcmp, NULL);
48b328
+	seq = seq_insert (seq, &len, "5", (seq_compar)strcmp, NULL);
48b328
+	seq = seq_insert (seq, &len, "1", (seq_compar)strcmp, NULL);
48b328
+	seq = seq_insert (seq, &len, "4", (seq_compar)strcmp, NULL);
48b328
+	seq = seq_insert (seq, &len, "2", (seq_compar)strcmp, NULL);
48b328
+
48b328
+	assert_str_eq (seq[0], "1");
48b328
+	assert_str_eq (seq[1], "2");
48b328
+	assert_str_eq (seq[2], "3");
48b328
+	assert_str_eq (seq[3], "4");
48b328
+	assert_str_eq (seq[4], "5");
48b328
+	assert (seq[5] == NULL);
48b328
+	assert_num_eq (len, 5);
48b328
+
48b328
+	seq_remove (seq, &len, "1", (seq_compar)strcmp, NULL);
48b328
+
48b328
+	assert_str_eq (seq[0], "2");
48b328
+	assert_str_eq (seq[1], "3");
48b328
+	assert_str_eq (seq[2], "4");
48b328
+	assert_str_eq (seq[3], "5");
48b328
+	assert (seq[4] == NULL);
48b328
+	assert_num_eq (len, 4);
48b328
+
48b328
+	seq_free (seq, NULL);
48b328
+}
48b328
+
48b328
+static void
48b328
+test_remove_last (void)
48b328
+{
48b328
+	void **seq = NULL;
48b328
+	int len = 0;
48b328
+
48b328
+	seq = seq_insert (seq, &len, "3", (seq_compar)strcmp, NULL);
48b328
+	seq = seq_insert (seq, &len, "1", (seq_compar)strcmp, NULL);
48b328
+	seq = seq_insert (seq, &len, "4", (seq_compar)strcmp, NULL);
48b328
+	seq = seq_insert (seq, &len, "2", (seq_compar)strcmp, NULL);
48b328
+
48b328
+	assert_str_eq (seq[0], "1");
48b328
+	assert_str_eq (seq[1], "2");
48b328
+	assert_str_eq (seq[2], "3");
48b328
+	assert_str_eq (seq[3], "4");
48b328
+	assert (seq[4] == NULL);
48b328
+	assert_num_eq (len, 4);
48b328
+
48b328
+	seq_remove (seq, &len, "4", (seq_compar)strcmp, NULL);
48b328
+
48b328
+	assert_str_eq (seq[0], "1");
48b328
+	assert_str_eq (seq[1], "2");
48b328
+	assert_str_eq (seq[2], "3");
48b328
+	assert (seq[3] == NULL);
48b328
+	assert_num_eq (len, 3);
48b328
+
48b328
+	seq_free (seq, NULL);
48b328
+}
48b328
+
48b328
 static int
48b328
 compar_even (void *match,
48b328
              void *value)
48b328
@@ -631,6 +763,9 @@ main (int argc,
48b328
 	test_func (test_insert, "/seq/insert");
48b328
 	test_func (test_insert_destroys, "/seq/insert_destroys");
48b328
 	test_func (test_remove, "/seq/remove");
48b328
+	test_func (test_remove_unsorted, "/seq/remove_unsorted");
48b328
+	test_func (test_remove_first, "/seq/remove_first");
48b328
+	test_func (test_remove_last, "/seq/remove_last");
48b328
 	test_func (test_remove_destroys, "/seq/remove_destroys");
48b328
 	test_func (test_filter, "/seq/filter");
48b328
 	test_func (test_filter_null, "/seq/filter_null");
48b328
diff --git a/library/seq.h b/library/seq.h
48b328
index 694965b..5d48848 100644
48b328
--- a/library/seq.h
48b328
+++ b/library/seq.h
48b328
@@ -44,6 +44,12 @@ typedef void *     (* seq_copy)               (void *value);
48b328
 
48b328
 typedef void       (* seq_destroy)            (void *value);
48b328
 
48b328
+typedef int        (* seq_search)             (void **seq,
48b328
+                                               int low,
48b328
+                                               int high,
48b328
+                                               void *match,
48b328
+                                               seq_compar compar);
48b328
+
48b328
 seq_voidp          seq_push                   (seq_voidp seq,
48b328
                                                int *length,
48b328
                                                void *value) WARN_UNUSED;
48b328
@@ -62,6 +68,12 @@ void               seq_remove                 (seq_voidp seq,
48b328
                                                seq_compar compar,
48b328
                                                seq_destroy destroy);
48b328
 
48b328
+void               seq_remove_unsorted        (seq_voidp seq,
48b328
+                                               int *length,
48b328
+                                               void *match,
48b328
+                                               seq_compar compar,
48b328
+                                               seq_destroy destroy);
48b328
+
48b328
 seq_voidp          seq_lookup                 (seq_voidp seq,
48b328
                                                int *length,
48b328
                                                void *match,
48b328
-- 
48b328
2.14.4
48b328