From: Yann Dirson <ydirson@altern.org>
To: git@vger.kernel.org
Cc: Yann Dirson <ydirson@altern.org>
Subject: [PATCH v3 2/3] Separate sorted-array type declaration from array declaration.
Date: Mon, 8 Nov 2010 23:39:01 +0100 [thread overview]
Message-ID: <1289255942-19705-3-git-send-email-ydirson@altern.org> (raw)
In-Reply-To: <1289255942-19705-1-git-send-email-ydirson@altern.org>
Instead of a single locate() function hardcoding references to a fixed
array, we declare a generic function taking references to the array and
its metadata. Declaring an array produces a lightweight wrapper for
this specific array, not modifying calling API any further and keeping
it readable.
This will allow to declare several arrays of the same type without
causing duplication of the locate function.
Signed-off-by: Yann Dirson <ydirson@altern.org>
---
diffcore-rename.c | 5 +++--
sorted-array.h | 39 ++++++++++++++++++++++++---------------
2 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/diffcore-rename.c b/diffcore-rename.c
index 1626bdc..a146adf 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -24,8 +24,9 @@ static void rename_dst_init(struct diff_rename_dst *elem, struct diff_filespec *
fill_filespec(elem->two, ref_spec->sha1, ref_spec->mode);
elem->pair = NULL;
}
-declare_sorted_array(static, struct diff_rename_dst, rename_dst,
- rename_dst_cmp, rename_dst_init)
+declare_sorted_array_type(static, struct diff_rename_dst, rename_dst,
+ rename_dst_cmp, rename_dst_init);
+declare_sorted_array(static, struct diff_rename_dst, rename_dst, rename_dst);
/* Table of rename/copy src files */
static struct diff_rename_src {
diff --git a/sorted-array.h b/sorted-array.h
index 03d5d1e..54fad20 100644
--- a/sorted-array.h
+++ b/sorted-array.h
@@ -1,15 +1,15 @@
-#define declare_sorted_array(MAYBESTATIC,ELEMTYPE,LIST,CMP,INIT) \
-MAYBESTATIC ELEMTYPE *LIST; \
-MAYBESTATIC int LIST##_nr, LIST##_alloc; \
-MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok) \
+#define declare_sorted_array_type(MAYBESTATIC,ELEMTYPE,TYPENICK,CMP,INIT) \
+MAYBESTATIC ELEMTYPE *locate_type_##TYPENICK( \
+ ELEMTYPE **list_p, int *list_nr_p, int *list_nr_alloc, \
+ void *data, int insert_ok) \
{ \
int first, last; \
\
first = 0; \
- last = LIST##_nr; \
+ last = *list_nr_p; \
while (last > first) { \
int next = (last + first) >> 1; \
- ELEMTYPE *nextelem = &(LIST[next]); \
+ ELEMTYPE *nextelem = &((*list_p)[next]); \
int cmp = CMP(data, nextelem); \
if (!cmp) \
return nextelem; \
@@ -23,14 +23,23 @@ MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok) \
if (!insert_ok) \
return NULL; \
/* insert to make it at "first" */ \
- if (LIST##_alloc <= LIST##_nr) { \
- LIST##_alloc = alloc_nr(LIST##_alloc); \
- LIST = xrealloc(LIST, LIST##_alloc * sizeof(*LIST)); \
+ if (*list_nr_alloc <= *list_nr_p) { \
+ (*list_nr_alloc) = alloc_nr((*list_nr_alloc)); \
+ *list_p = xrealloc(*list_p, (*list_nr_alloc) * sizeof(**list_p)); \
} \
- LIST##_nr++; \
- if (first < LIST##_nr) \
- memmove(LIST + first + 1, LIST + first, \
- (LIST##_nr - first - 1) * sizeof(*LIST)); \
- INIT(&LIST[first], data); \
- return &(LIST[first]); \
+ (*list_nr_p)++; \
+ if (first < *list_nr_p) \
+ memmove(*list_p + first + 1, *list_p + first, \
+ (*list_nr_p - first - 1) * sizeof(**list_p)); \
+ INIT(&(*list_p)[first], data); \
+ return &((*list_p)[first]); \
+}
+
+#define declare_sorted_array(MAYBESTATIC,ELEMTYPE,TYPENICK,LIST) \
+MAYBESTATIC ELEMTYPE *LIST; \
+MAYBESTATIC int LIST##_nr, LIST##_alloc; \
+MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok) \
+{ \
+ return locate_type_##TYPENICK(&LIST, &LIST##_nr, &LIST##_alloc, \
+ data, insert_ok); \
}
--
1.7.2.3
next prev parent reply other threads:[~2010-11-08 22:39 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-08 22:38 [PATCH v3] generalizing sorted-array handling Yann Dirson
2010-11-08 22:39 ` [PATCH v3 1/3] Introduce sorted-array binary-search function Yann Dirson
2010-11-16 17:27 ` Junio C Hamano
2010-11-16 22:04 ` Yann Dirson
2010-11-08 22:39 ` Yann Dirson [this message]
2010-11-08 22:39 ` [PATCH v3 3/3] Convert diffcore-rename's rename_src to the new sorted-array API Yann Dirson
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1289255942-19705-3-git-send-email-ydirson@altern.org \
--to=ydirson@altern.org \
--cc=git@vger.kernel.org \
/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.
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 NNTP newsgroup(s).