]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object_array_remove_duplicates(): rewrite to reduce copying
authorMichael Haggerty <mhagger@alum.mit.edu>
Sat, 25 May 2013 09:08:10 +0000 (11:08 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 28 May 2013 16:25:01 +0000 (09:25 -0700)
The old version copied one entry to its destination position, then
deleted any matching entries from the tail of the array.  This
required the tail of the array to be copied multiple times.  It didn't
affect the complexity of the algorithm because the whole tail has to
be searched through anyway.  But all the copying was unnecessary.

Instead, check for the existence of an entry with the same name in the
*head* of the list before copying an entry to its final position.
This way each entry has to be copied at most one time.

Extract a helper function contains_name() to do a bit of the work.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object.c
object.h

index fcd4a82c131623d45bd7978067022e8bc9864009..10b534946126f7d7e640f9634ebd2991373eae32 100644 (file)
--- a/object.c
+++ b/object.c
@@ -294,22 +294,32 @@ void object_array_filter(struct object_array *array,
        array->nr = dst;
 }
 
+/*
+ * Return true iff array already contains an entry with name.
+ */
+static int contains_name(struct object_array *array, const char *name)
+{
+       unsigned nr = array->nr, i;
+       struct object_array_entry *object = array->objects;
+
+       for (i = 0; i < nr; i++, object++)
+               if (!strcmp(object->name, name))
+                       return 1;
+       return 0;
+}
+
 void object_array_remove_duplicates(struct object_array *array)
 {
-       unsigned int ref, src, dst;
+       unsigned nr = array->nr, src;
        struct object_array_entry *objects = array->objects;
 
-       for (ref = 0; ref + 1 < array->nr; ref++) {
-               for (src = ref + 1, dst = src;
-                    src < array->nr;
-                    src++) {
-                       if (!strcmp(objects[ref].name, objects[src].name))
-                               continue;
-                       if (src != dst)
-                               objects[dst] = objects[src];
-                       dst++;
+       array->nr = 0;
+       for (src = 0; src < nr; src++) {
+               if (!contains_name(array, objects[src].name)) {
+                       if (src != array->nr)
+                               objects[array->nr] = objects[src];
+                       array->nr++;
                }
-               array->nr = dst;
        }
 }
 
index 0d39ff4f9d16e1b33be42acb3e1af209a6cd4e42..6c1c27fba697f436c3f22d1d11b9babd5d42f493 100644 (file)
--- a/object.h
+++ b/object.h
@@ -96,7 +96,11 @@ typedef int (*object_array_each_func_t)(struct object_array_entry *, void *);
 void object_array_filter(struct object_array *array,
                         object_array_each_func_t want, void *cb_data);
 
-void object_array_remove_duplicates(struct object_array *);
+/*
+ * Remove from array all but the first entry with a given name.
+ * Warning: this function uses an O(N^2) algorithm.
+ */
+void object_array_remove_duplicates(struct object_array *array);
 
 void clear_object_flags(unsigned flags);