Skip to content

Commit aeb4a51

Browse files
mhaggergitster
authored andcommitted
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>
1 parent ff5f5f2 commit aeb4a51

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

object.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ void add_object_array_with_mode(struct object *obj, const char *name, struct obj
278278
array->nr = ++nr;
279279
}
280280

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+
281297
void object_array_remove_duplicates(struct object_array *array)
282298
{
283299
unsigned int ref, src, dst;

object.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ int object_list_contains(struct object_list *list, struct object *obj);
8585
/* Object array handling .. */
8686
void add_object_array(struct object *obj, const char *name, struct object_array *array);
8787
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+
8899
void object_array_remove_duplicates(struct object_array *);
89100

90101
void clear_object_flags(unsigned flags);

0 commit comments

Comments
 (0)