Re: [PATCH 2/9] sha1-array: provide oid_array_filter
From: Junio C Hamano <hidden>
Date: 2018-09-12 18:02:20
Stefan Beller [off-list ref] writes:
quoted hunk
Helped-by: Junio C Hamano [off-list ref] Signed-off-by: Stefan Beller <redacted> --- sha1-array.c | 18 ++++++++++++++++++ sha1-array.h | 5 +++++ 2 files changed, 23 insertions(+)diff --git a/sha1-array.c b/sha1-array.c index 265941fbf40..76323935dd7 100644 --- a/sha1-array.c +++ b/sha1-array.c@@ -77,3 +77,21 @@ int oid_array_for_each_unique(struct oid_array *array, } return 0; } + +int oid_array_filter(struct oid_array *array, + for_each_oid_fn fn,
It probably makes sense to call it "want" instead of "fn" to match object_array_filter().
+ void *cbdata)
+{
+ int src, dst;
+
+ for (src = dst = 0; src < array->nr; src++) {
+ if (fn(&array->oid[src], cbdata)) {
+ if (dst < src)
+ oidcpy(&array->oid[dst], &array->oid[src]);
+ dst++;
+ }In fact, matching the implementation of object_array_fiter() may also make sense, as I do not see a strong reason why the resulting code would become better by rewriting "dst != src" over there to "dst < src" here.
quoted hunk
+ } + array->nr = dst; + + return 0; +}diff --git a/sha1-array.h b/sha1-array.h index 232bf950172..a2d7c210835 100644 --- a/sha1-array.h +++ b/sha1-array.h@@ -23,4 +23,9 @@ int oid_array_for_each_unique(struct oid_array *array, for_each_oid_fn fn, void *data); +/* Call fn for each oid, and remove it if fn returns 0, retain it otherwise */
Also perhaps mimic the wording of object_array_filter()'s comment? I find it easier that the latter says "retaining only if X" instead of saying "remove if !X, retain otherwise"; it's both shorter and more to the point. It also is nicer that it notes that the order is preserved.
+int oid_array_filter(struct oid_array *array, + for_each_oid_fn fn, + void *cbdata); +
Other than that, the function makes sense very much, and the callsite we see in patch 8/9 does, too. Thanks.
#endif /* SHA1_ARRAY_H */