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>

Stefan Beller committed Nov 28, 2018 at 16:27 UTC 161b1cf3bdca7fecf5ec03ffdb74260312cd0229
3 files changed +25
Documentation/technical/api-oid-array.txt
+5
@@ -48,6 +48,11 @@ Functions
48 is not sorted, this function has the side effect of sorting
49 it.
50
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 +
56 Examples
57 --------
58
sha1-array.c
+17
@@ -77,3 +77,20 @@ int oid_array_for_each_unique(struct oid_array *array,
77 }
78 return 0;
79 }
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
+3
@@ -22,5 +22,8 @@ int oid_array_for_each(struct oid_array *array,
22 int oid_array_for_each_unique(struct oid_array *array,
23 for_each_oid_fn fn,
24 void *data);
25 +void oid_array_filter(struct oid_array *array,
26 + for_each_oid_fn want,
27 + void *cbdata);
28
29 #endif /* SHA1_ARRAY_H */