Skip to content

Commit 161b1cf

Browse files
stefanbellergitster
authored andcommitted
sha1-array: provide oid_array_filter
Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 965798d commit 161b1cf

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Documentation/technical/api-oid-array.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Functions
4848
is not sorted, this function has the side effect of sorting
4949
it.
5050

51+
`oid_array_filter`::
52+
Apply the callback function `want` to each entry in the array,
53+
retaining only the entries for which the function returns true.
54+
Preserve the order of the entries that are retained.
55+
5156
Examples
5257
--------
5358

sha1-array.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,20 @@ int oid_array_for_each_unique(struct oid_array *array,
7777
}
7878
return 0;
7979
}
80+
81+
void oid_array_filter(struct oid_array *array,
82+
for_each_oid_fn want,
83+
void *cb_data)
84+
{
85+
unsigned nr = array->nr, src, dst;
86+
struct object_id *oids = array->oid;
87+
88+
for (src = dst = 0; src < nr; src++) {
89+
if (want(&oids[src], cb_data)) {
90+
if (src != dst)
91+
oidcpy(&oids[dst], &oids[src]);
92+
dst++;
93+
}
94+
}
95+
array->nr = dst;
96+
}

sha1-array.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ int oid_array_for_each(struct oid_array *array,
2222
int oid_array_for_each_unique(struct oid_array *array,
2323
for_each_oid_fn fn,
2424
void *data);
25+
void oid_array_filter(struct oid_array *array,
26+
for_each_oid_fn want,
27+
void *cbdata);
2528

2629
#endif /* SHA1_ARRAY_H */

0 commit comments

Comments
 (0)