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

[PATCH 13/15] sort_ref_dir(): simplify logic

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Apr 10, 2012, 05:30 UTC
Message-ID
<1334035827-20331-14-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1334035827-20331-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>
Use the more usual indexing idiom for clarity.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/refs.c b/refs.c
index 4e0cfb2..a3b8428 100644
--- a/refs.c
+++ b/refs.c
@@ -227,11 +227,13 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
 }
 
 /*
- * Sort the entries in dir (if they are not already sorted).
+ * Sort the entries in dir (if they are not already sorted)
+ * and remove any duplicate entries.
  */
 static void sort_ref_dir(struct ref_dir *dir)
 {
 	int i, j;
+	struct ref_entry *last = NULL;
 
 	/*
 	 * This check also prevents passing a zero-length array to qsort(),
@@ -242,16 +244,16 @@ static void sort_ref_dir(struct ref_dir *dir)
 
 	qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
 
-	/* Remove any duplicates from the ref_dir */
-	i = 0;
-	for (j = 1; j < dir->nr; j++) {
-		if (is_dup_ref(dir->entries[i], dir->entries[j])) {
-			free_ref_entry(dir->entries[j]);
-			continue;
+	/* Remove any duplicates: */
+	for (i = 0, j = 0; j < dir->nr; j++) {
+		struct ref_entry *entry = dir->entries[j];
+		if (last && is_dup_ref(last, entry)) {
+			free_ref_entry(entry);
+		} else {
+			last = dir->entries[i++] = entry;
 		}
-		dir->entries[++i] = dir->entries[j];
 	}
-	dir->sorted = dir->nr = i + 1;
+	dir->sorted = dir->nr = i;
 }
 
 #define DO_FOR_EACH_INCLUDE_BROKEN 01
-- 
1.7.10
Previous: mhagger@alum.mit.eduNext: mhagger@alum.mit.edu
Message 14 of 18 in “Hierarchical reference cache (once again)”
  1. 00/15 Hierarchical reference cache (once again)mhagger@alum.mit.edu, Apr 10, 2012
  2. 01/15 refs.c: reorder definitions more logicallymhagger@alum.mit.edu, Apr 10, 2012
  3. 02/15 refs: manage current_ref within do_one_ref()mhagger@alum.mit.edu, Apr 10, 2012
  4. 03/15 do_for_each_ref_in_array(): new functionmhagger@alum.mit.edu, Apr 10, 2012
  5. 04/15 do_for_each_ref_in_arrays(): new functionmhagger@alum.mit.edu, Apr 10, 2012
  6. 05/15 repack_without_ref(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Apr 10, 2012
  7. 06/15 names_conflict(): new function, extracted from is_refname_available()mhagger@alum.mit.edu, Apr 10, 2012
  8. 07/15 names_conflict(): simplify implementationmhagger@alum.mit.edu, Apr 10, 2012
  9. 08/15 is_refname_available(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Apr 10, 2012
  10. 09/15 free_ref_entry(): new functionmhagger@alum.mit.edu, Apr 10, 2012
  11. 10/15 check_refname_component(): return 0 for zero-length componentsmhagger@alum.mit.edu, Apr 10, 2012
  12. 11/15 struct ref_entry: nest the value part in a unionmhagger@alum.mit.edu, Apr 10, 2012
  13. 12/15 refs.c: rename ref_array -> ref_dirmhagger@alum.mit.edu, Apr 10, 2012
  14. 13/15 sort_ref_dir(): simplify logicmhagger@alum.mit.edu, Apr 10, 2012
  15. 14/15 refs: store references hierarchicallymhagger@alum.mit.edu, Apr 10, 2012
  16. 15/15 do_for_each_ref(): only iterate over the subtree that was requestedmhagger@alum.mit.edu, Apr 10, 2012
  17. Jeff KingApr 12, 2012
  18. Junio C HamanoApr 12, 2012

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.