git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 2/3] Separate sorted-array type declaration from array declaration.

From
Yann Dirson <ydirson@altern.org>
Date
Nov 8, 2010, 22:39 UTC
Message-ID
<1289255942-19705-3-git-send-email-ydirson@altern.org>
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
Previous: Yann DirsonNext: Yann Dirson
Message 5 of 6 in “generalizing sorted-array handling”
  1. generalizing sorted-array handlingYann Dirson, Nov 8, 2010
  2. 1/3 Introduce sorted-array binary-search function.Yann Dirson, Nov 8, 2010
  3. Junio C HamanoNov 16, 2010
  4. Yann DirsonNov 16, 2010
  5. 2/3 Separate sorted-array type declaration from array declaration.Yann Dirson, Nov 8, 2010
  6. 3/3 Convert diffcore-rename's rename_src to the new sorted-array API.Yann Dirson, Nov 8, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.