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