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