OSDN Git Service

object_array: add function object_array_filter()
authorMichael Haggerty <mhagger@alum.mit.edu>
Sat, 25 May 2013 09:08:08 +0000 (11:08 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 28 May 2013 16:25:01 +0000 (09:25 -0700)
Add a function that allows unwanted entries in an object_array to be
removed.  This encapsulation is a step towards giving object_array
ownership of its entries' name memory.

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

index 20703f5..fcd4a82 100644 (file)
--- a/object.c
+++ b/object.c
@@ -278,6 +278,22 @@ void add_object_array_with_mode(struct object *obj, const char *name, struct obj
        array->nr = ++nr;
 }
 
+void object_array_filter(struct object_array *array,
+                        object_array_each_func_t want, void *cb_data)
+{
+       unsigned nr = array->nr, src, dst;
+       struct object_array_entry *objects = array->objects;
+
+       for (src = dst = 0; src < nr; src++) {
+               if (want(&objects[src], cb_data)) {
+                       if (src != dst)
+                               objects[dst] = objects[src];
+                       dst++;
+               }
+       }
+       array->nr = dst;
+}
+
 void object_array_remove_duplicates(struct object_array *array)
 {
        unsigned int ref, src, dst;
index 97d384b..0d39ff4 100644 (file)
--- a/object.h
+++ b/object.h
@@ -85,6 +85,17 @@ int object_list_contains(struct object_list *list, struct object *obj);
 /* Object array handling .. */
 void add_object_array(struct object *obj, const char *name, struct object_array *array);
 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode);
+
+typedef int (*object_array_each_func_t)(struct object_array_entry *, void *);
+
+/*
+ * Apply want to each entry in array, retaining only the entries for
+ * which the function returns true.  Preserve the order of the entries
+ * that are retained.
+ */
+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 *);
 
 void clear_object_flags(unsigned flags);