object_array: add function object_array_filter()

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>

Michael Haggerty committed May 25, 2013 at 11:08 UTC aeb4a51ef82c71c9a65d11f77aeedb53bea0e983
2 files changed +27
object.c
+16
@@ -278,6 +278,22 @@ void add_object_array_with_mode(struct object *obj, const char *name, struct obj
278 array->nr = ++nr;
279 }
280
281 +void object_array_filter(struct object_array *array,
282 + object_array_each_func_t want, void *cb_data)
283 +{
284 + unsigned nr = array->nr, src, dst;
285 + struct object_array_entry *objects = array->objects;
286 +
287 + for (src = dst = 0; src < nr; src++) {
288 + if (want(&objects[src], cb_data)) {
289 + if (src != dst)
290 + objects[dst] = objects[src];
291 + dst++;
292 + }
293 + }
294 + array->nr = dst;
295 +}
296 +
297 void object_array_remove_duplicates(struct object_array *array)
298 {
299 unsigned int ref, src, dst;
object.h
+11
@@ -85,6 +85,17 @@ int object_list_contains(struct object_list *list, struct object *obj);
85 /* Object array handling .. */
86 void add_object_array(struct object *obj, const char *name, struct object_array *array);
87 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode);
88 +
89 +typedef int (*object_array_each_func_t)(struct object_array_entry *, void *);
90 +
91 +/*
92 + * Apply want to each entry in array, retaining only the entries for
93 + * which the function returns true. Preserve the order of the entries
94 + * that are retained.
95 + */
96 +void object_array_filter(struct object_array *array,
97 + object_array_each_func_t want, void *cb_data);
98 +
99 void object_array_remove_duplicates(struct object_array *);
100
101 void clear_object_flags(unsigned flags);