git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH 2/6] string-list: add functions to work with unsorted lists
Date: Sat, 1 Mar 2008 13:15:47 +0000 (GMT)	[thread overview]
Message-ID: <alpine.LSU.1.00.0803011315400.22527@racer.site> (raw)
In-Reply-To: <alpine.LSU.1.00.0803011313020.22527@racer.site>


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@gmx.de>
---
 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



  parent reply	other threads:[~2008-03-01 13:17 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-29  1:44 [PATCH 0/5] Builtin "git remote" Johannes Schindelin
2008-02-29  1:44 ` [PATCH 1/5] path-list: add functions to work with unsorted lists Johannes Schindelin
2008-02-29  2:04   ` Junio C Hamano
2008-02-29  2:15     ` Johannes Schindelin
2008-03-01 13:14       ` [PATCH 0/6] Builtin remote, v2 Johannes Schindelin
2008-03-01 13:15         ` [PATCH 1/6] Rename path_list to string_list Johannes Schindelin
2008-03-11 16:20           ` David Kågedal
2008-03-11 16:26             ` Johannes Schindelin
2008-03-11 16:44               ` David Kågedal
2008-03-01 13:15         ` Johannes Schindelin [this message]
2008-03-01 13:16         ` [PATCH 3/6] parseopt: add flag to stop on first non option Johannes Schindelin
2008-03-01 13:16         ` [PATCH 4/6] Test "git remote show" and "git remote prune" Johannes Schindelin
2008-03-01 13:16         ` [PATCH 5/6] Make git-remote a builtin Johannes Schindelin
2008-03-01 13:17         ` [PATCH 6/6] builtin-remote: prune remotes correctly that were added with --mirror Johannes Schindelin
2008-02-29  1:45 ` [PATCH 2/5] parseopt: add flag to stop on first non option Johannes Schindelin
2008-02-29  1:45 ` [PATCH 3/5] Test "git remote show" and "git remote prune" Johannes Schindelin
2008-02-29  1:45 ` [PATCH 4/5] Make git-remote a builtin Johannes Schindelin
2008-02-29  1:46 ` [PATCH 5/5] builtin-remote: prune remotes correctly that were added with --mirror Johannes Schindelin
2008-02-29  1:56 ` [PATCH 0/5] Builtin "git remote" Junio C Hamano
2008-02-29  2:13   ` Johannes Schindelin
2008-02-29  2:17   ` Daniel Barkalow
2008-02-29 11:21     ` Johannes Schindelin
2008-03-02 15:15       ` Johannes Schindelin
2008-03-02 15:40         ` Johannes Schindelin
2008-03-02 15:52           ` [PATCH 0/2] format-patch --cover-letter improvements Johannes Schindelin
2008-03-02 15:53             ` [PATCH 1/2] format-patch: use the diff options for the cover letter, too Johannes Schindelin
2008-03-02 15:53             ` [PATCH 2/2] format-patch: wrap cover-letter's shortlog sensibly Johannes Schindelin
2008-03-02 17:05               ` Junio C Hamano
2008-03-02 17:38                 ` Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LSU.1.00.0803011315400.22527@racer.site \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).