[PATCH 2/6] string-list: add functions to work with unsorted lists

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Up to now, string-lists were sorted at all times.  But sometimes it
is much more convenient to build the list and sort it at the end,
or sort it not at all.

Add string_list_append() and sort_string_list() to allow that.

Also, add the unsorted_string_list_has_string() function, to do a
linear search.

Signed-off-by: Johannes Schindelin <johannes.schindelin@xxxxxx>
---
 Documentation/CodingGuidelines |    5 +++--
 string-list.c                  |   29 +++++++++++++++++++++++++++++
 string-list.h                  |    8 +++++++-
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/Documentation/CodingGuidelines b/Documentation/CodingGuidelines
index fe7c74b..7507053 100644
--- a/Documentation/CodingGuidelines
+++ b/Documentation/CodingGuidelines
@@ -91,8 +91,9 @@ For C programs:
 
  - Use the API.  No, really.  We have a strbuf (variable length
    string), several arrays with the ALLOC_GROW() macro, a
-   string_list for sorted string lists, a hash map (mapping struct
-   objects) named "struct decorate", amongst other things.
+   string_list for sorted and unsorted string lists, a hash map
+   (mapping struct objects) named "struct decorate", amongst other
+   things.
 
  - When you come up with an API, document it.
 
diff --git a/string-list.c b/string-list.c
index 699e754..ddd83c8 100644
--- a/string-list.c
+++ b/string-list.c
@@ -103,3 +103,32 @@ void print_string_list(const char *text, const struct string_list *p)
 		printf("%s:%p\n", p->items[i].string, p->items[i].util);
 }
 
+struct string_list_item *string_list_append(const char *string, struct string_list *list)
+{
+	ALLOC_GROW(list->items, list->nr + 1, list->alloc);
+	list->items[list->nr].string =
+		list->strdup_strings ? xstrdup(string) : (char *)string;
+	return list->items + list->nr++;
+}
+
+static int cmp_items(const void *a, const void *b)
+{
+	const struct string_list_item *one = a;
+	const struct string_list_item *two = b;
+	return strcmp(one->string, two->string);
+}
+
+void sort_string_list(struct string_list *list)
+{
+	qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
+}
+
+int unsorted_string_list_has_string(struct string_list *list, const char *string)
+{
+	int i;
+	for (i = 0; i < list->nr; i++)
+		if (!strcmp(string, list->items[i].string))
+			return 1;
+	return 0;
+}
+
diff --git a/string-list.h b/string-list.h
index 6195791..4d6a705 100644
--- a/string-list.h
+++ b/string-list.h
@@ -13,10 +13,16 @@ struct string_list
 };
 
 void print_string_list(const char *text, const struct string_list *p);
+void string_list_clear(struct string_list *list, int free_util);
 
+/* Use these functions only on sorted lists: */
 int string_list_has_string(const struct string_list *list, const char *string);
-void string_list_clear(struct string_list *list, int free_util);
 struct string_list_item *string_list_insert(const char *string, struct string_list *list);
 struct string_list_item *string_list_lookup(const char *string, struct string_list *list);
 
+/* Use these functions only on unsorted lists: */
+struct string_list_item *string_list_append(const char *string, struct string_list *list);
+void sort_string_list(struct string_list *list);
+int unsorted_string_list_has_string(struct string_list *list, const char *string);
+
 #endif /* PATH_LIST_H */
-- 
1.5.4.3.446.gbe8932


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux