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

[PATCH 3/6] Convert diffcore-rename's rename_src to the new sorted-array API.

From
Yann Dirson <ydirson@altern.org>
Date
Dec 8, 2010, 22:51 UTC
Message-ID
<1291848695-24601-4-git-send-email-ydirson@altern.org>
In-Reply-To
<1291848695-24601-1-git-send-email-ydirson@altern.org>

There was no compelling reason to pass separately two members of a single struct to the insert function. That's a happy coincidence.

Signed-off-by: Yann Dirson <ydirson@altern.org>
---
 diffcore-rename.c |   53 ++++++++++++++++-------------------------------------
 1 files changed, 16 insertions(+), 37 deletions(-)
diff --git a/diffcore-rename.c b/diffcore-rename.c
index ca3f54c..f7afdeb 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -33,46 +33,25 @@ declare_sorted_array_insert_checkbool(static, struct diff_rename_dst, register_r
 				      rename_dst, rename_dst_cmp, rename_dst_init);
 
 /* Table of rename/copy src files */
-static struct diff_rename_src {
+
+struct diff_rename_src {
 	struct diff_filespec *one;
 	unsigned short score; /* to remember the break score */
-} *rename_src;
-static int rename_src_nr, rename_src_alloc;
+};
 
-static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
-						   unsigned short score)
+static int rename_src_cmp(struct diff_filepair *ref_pair, struct diff_rename_src *elem)
 {
-	int first, last;
-
-	first = 0;
-	last = rename_src_nr;
-	while (last > first) {
-		int next = (last + first) >> 1;
-		struct diff_rename_src *src = &(rename_src[next]);
-		int cmp = strcmp(one->path, src->one->path);
-		if (!cmp)
-			return src;
-		if (cmp < 0) {
-			last = next;
-			continue;
-		}
-		first = next+1;
-	}
-
-	/* insert to make it at "first" */
-	if (rename_src_alloc <= rename_src_nr) {
-		rename_src_alloc = alloc_nr(rename_src_alloc);
-		rename_src = xrealloc(rename_src,
-				      rename_src_alloc * sizeof(*rename_src));
-	}
-	rename_src_nr++;
-	if (first < rename_src_nr)
-		memmove(rename_src + first + 1, rename_src + first,
-			(rename_src_nr - first - 1) * sizeof(*rename_src));
-	rename_src[first].one = one;
-	rename_src[first].score = score;
-	return &(rename_src[first]);
+	return strcmp(ref_pair->one->path, elem->one->path);
+}
+static void rename_src_init(struct diff_rename_src *elem, struct diff_filepair *ref_pair)
+{
+	elem->one = ref_pair->one;
+	elem->score = ref_pair->score;
 }
+declare_sorted_array(static, struct diff_rename_src, rename_src);
+declare_sorted_array_insertonly_checkbool(static, struct diff_rename_src, register_rename_src,
+					  struct diff_filepair *,
+					  rename_src, rename_src_cmp, rename_src_init);
 
 static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
 {
@@ -429,7 +408,7 @@ void diffcore_rename(struct diff_options *options)
 			 */
 			if (p->broken_pair && !p->score)
 				p->one->rename_used++;
-			register_rename_src(p->one, p->score);
+			register_rename_src(p);
 		}
 		else if (detect_rename == DIFF_DETECT_COPY) {
 			/*
@@ -437,7 +416,7 @@ void diffcore_rename(struct diff_options *options)
 			 * one, to indicate ourselves as a user.
 			 */
 			p->one->rename_used++;
-			register_rename_src(p->one, p->score);
+			register_rename_src(p);
 		}
 	}
 	if (rename_dst_nr == 0 || rename_src_nr == 0)
-- 
1.7.2.3
Previous: Junio C HamanoNext: Yann Dirson
Message 9 of 15 in “generalizing sorted-array handling”
  1. generalizing sorted-array handlingYann Dirson, Dec 8, 2010
  2. 1/6 Introduce sorted-array binary-search function.Yann Dirson, Dec 8, 2010
  3. Junio C HamanoDec 10, 2010
  4. Yann DirsonDec 30, 2010
  5. Erik Faye-LundDec 30, 2010
  6. Yann DirsonDec 30, 2010
  7. 2/6 Convert diffcore-rename's rename_dst to the new sorted-array API.Yann Dirson, Dec 8, 2010
  8. Junio C HamanoDec 10, 2010
  9. 3/6 Convert diffcore-rename's rename_src to the new sorted-array API.Yann Dirson, Dec 8, 2010
  10. 4/6 Convert pack-objects.c to the new sorted-array API.Yann Dirson, Dec 8, 2010
  11. 5/6 Use sorted-array API for commit.c's commit_graft.Yann Dirson, Dec 8, 2010
  12. 6/6 [RFC] subvert sorted-array to replace binary-search in unpack-objects.Yann Dirson, Dec 8, 2010
  13. Junio C HamanoDec 10, 2010
  14. Junio C HamanoDec 10, 2010
  15. Yann DirsonDec 30, 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.