diff mbox series

[2/9] sha1-array: provide oid_array_filter

Message ID 20180911234951.14129-3-sbeller@google.com (mailing list archive)
State New, archived
Headers show
Series fetch: make sure submodule oids are fetched | expand

Commit Message

Stefan Beller Sept. 11, 2018, 11:49 p.m. UTC
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
---
 sha1-array.c | 18 ++++++++++++++++++
 sha1-array.h |  5 +++++
 2 files changed, 23 insertions(+)

Comments

Junio C Hamano Sept. 12, 2018, 6:02 p.m. UTC | #1
Stefan Beller <sbeller@google.com> writes:

> Helped-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  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.

> +	}
> +	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 */
diff mbox series

Patch

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,
+		     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++;
+		}
+	}
+	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 */
+int oid_array_filter(struct oid_array *array,
+		     for_each_oid_fn fn,
+		     void *cbdata);
+
 #endif /* SHA1_ARRAY_H */